Patch Detail
Show a patch.
GET /api/patches/8745/?format=api
{ "id": 8745, "url": "https://patchwork.libcamera.org/api/patches/8745/?format=api", "web_url": "https://patchwork.libcamera.org/patch/8745/", "project": { "id": 1, "url": "https://patchwork.libcamera.org/api/projects/1/?format=api", "name": "libcamera", "link_name": "libcamera", "list_id": "libcamera_core", "list_email": "libcamera-devel@lists.libcamera.org", "web_url": "", "scm_url": "", "webscm_url": "" }, "msgid": "<20200712144419.21457-1-laurent.pinchart@ideasonboard.com>", "date": "2020-07-12T14:44:16", "name": "[libcamera-devel,1/4] libcamera: file: Add read/write support", "commit_ref": null, "pull_url": null, "state": "superseded", "archived": false, "hash": "c96a42b8ae8ca3c9215510206949b3b42484c93c", "submitter": { "id": 2, "url": "https://patchwork.libcamera.org/api/people/2/?format=api", "name": "Laurent Pinchart", "email": "laurent.pinchart@ideasonboard.com" }, "delegate": null, "mbox": "https://patchwork.libcamera.org/patch/8745/mbox/", "series": [ { "id": 1098, "url": "https://patchwork.libcamera.org/api/series/1098/?format=api", "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=1098", "date": "2020-07-12T14:44:16", "name": "[libcamera-devel,1/4] libcamera: file: Add read/write support", "version": 1, "mbox": "https://patchwork.libcamera.org/series/1098/mbox/" } ], "comments": "https://patchwork.libcamera.org/api/patches/8745/comments/", "check": "pending", "checks": "https://patchwork.libcamera.org/api/patches/8745/checks/", "tags": {}, "headers": { "Return-Path": "<libcamera-devel-bounces@lists.libcamera.org>", "X-Original-To": "parsemail@patchwork.libcamera.org", "Delivered-To": "parsemail@patchwork.libcamera.org", "Received": [ "from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id 87411BD792\n\tfor <parsemail@patchwork.libcamera.org>;\n\tSun, 12 Jul 2020 14:44:48 +0000 (UTC)", "from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id D9FC36059B;\n\tSun, 12 Jul 2020 16:44:47 +0200 (CEST)", "from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 9C17960553\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tSun, 12 Jul 2020 16:44:45 +0200 (CEST)", "from pendragon.bb.dnainternet.fi (81-175-216-236.bb.dnainternet.fi\n\t[81.175.216.236])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 14B4F514\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tSun, 12 Jul 2020 16:44:45 +0200 (CEST)" ], "Authentication-Results": "lancelot.ideasonboard.com;\n\tdkim=fail reason=\"signature verification failed\" (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"OHUX6JQ6\"; dkim-atps=neutral", "DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1594565085;\n\tbh=THdfDQdrAo8snr4U/HP5Lz0tEW0rvScablw+oZ3uEXM=;\n\th=From:To:Subject:Date:From;\n\tb=OHUX6JQ6twTddXYp4JNnlba5qTdgQ/hV+6tL1eGwgDTDKctTArFP5m4IGPJP6oiVT\n\tj6GPy36Fu/I7qalBaB1pv3HK0yVYLAXaRYJ9JjUr41mZCFl0jHZKZpJtCk4vj8G/UL\n\tX68QptRpVm+H7jv8eNnTgDV83bRWpxVBuErPDoNY=", "From": "Laurent Pinchart <laurent.pinchart@ideasonboard.com>", "To": "libcamera-devel@lists.libcamera.org", "Date": "Sun, 12 Jul 2020 17:44:16 +0300", "Message-Id": "<20200712144419.21457-1-laurent.pinchart@ideasonboard.com>", "X-Mailer": "git-send-email 2.27.0", "MIME-Version": "1.0", "Subject": "[libcamera-devel] [PATCH 1/4] libcamera: file: Add read/write\n\tsupport", "X-BeenThere": "libcamera-devel@lists.libcamera.org", "X-Mailman-Version": "2.1.29", "Precedence": "list", "List-Id": "<libcamera-devel.lists.libcamera.org>", "List-Unsubscribe": "<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>", "List-Archive": "<https://lists.libcamera.org/pipermail/libcamera-devel/>", "List-Post": "<mailto:libcamera-devel@lists.libcamera.org>", "List-Help": "<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>", "List-Subscribe": "<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>", "Content-Type": "text/plain; charset=\"us-ascii\"", "Content-Transfer-Encoding": "7bit", "Errors-To": "libcamera-devel-bounces@lists.libcamera.org", "Sender": "\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>" }, "content": "Add basic support to read and write data from/to a file, along with\nretrieving and setting the current read/write position.\n\nSigned-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n---\n include/libcamera/internal/file.h | 7 ++\n src/libcamera/file.cpp | 111 +++++++++++++++++++++++++++++-\n 2 files changed, 116 insertions(+), 2 deletions(-)", "diff": "diff --git a/include/libcamera/internal/file.h b/include/libcamera/internal/file.h\nindex e3e72132e336..ead94cdf29c9 100644\n--- a/include/libcamera/internal/file.h\n+++ b/include/libcamera/internal/file.h\n@@ -49,6 +49,12 @@ public:\n \tint error() const { return error_; }\n \tssize_t size() const;\n \n+\toff_t pos() const { return pos_; }\n+\toff_t seek(off_t pos);\n+\n+\tssize_t read(const Span<uint8_t> &data);\n+\tssize_t write(const Span<const uint8_t> &data);\n+\n \tSpan<uint8_t> map(off_t offset = 0, ssize_t size = -1,\n \t\t\t MapFlag flags = MapNoOption);\n \tbool unmap(uint8_t *addr);\n@@ -61,6 +67,7 @@ private:\n \tstd::string name_;\n \tint fd_;\n \tOpenMode mode_;\n+\toff_t pos_;\n \n \tint error_;\n \tstd::map<void *, size_t> maps_;\ndiff --git a/src/libcamera/file.cpp b/src/libcamera/file.cpp\nindex c471bde3fc68..8bd109090919 100644\n--- a/src/libcamera/file.cpp\n+++ b/src/libcamera/file.cpp\n@@ -73,7 +73,7 @@ LOG_DEFINE_CATEGORY(File);\n * before performing I/O operations.\n */\n File::File(const std::string &name)\n-\t: name_(name), fd_(-1), mode_(NotOpen), error_(0)\n+\t: name_(name), fd_(-1), mode_(NotOpen), pos_(0), error_(0)\n {\n }\n \n@@ -84,7 +84,7 @@ File::File(const std::string &name)\n * setFileName().\n */\n File::File()\n-\t: fd_(-1), mode_(NotOpen), error_(0)\n+\t: fd_(-1), mode_(NotOpen), pos_(0), error_(0)\n {\n }\n \n@@ -202,6 +202,7 @@ void File::close()\n \t::close(fd_);\n \tfd_ = -1;\n \tmode_ = NotOpen;\n+\tpos_ = 0;\n }\n \n /**\n@@ -237,6 +238,112 @@ ssize_t File::size() const\n \treturn st.st_size;\n }\n \n+/**\n+ * \\fn off_t File::pos() const\n+ * \\brief Return current read or write position\n+ *\n+ * If the file is closed, this function returns 0.\n+ *\n+ * \\return The current read or write position\n+ */\n+\n+/**\n+ * \\brief Set the read or write position\n+ * \\param[in] pos The desired position\n+ * \\return The resulting offset from the beginning of the file on success, or a\n+ * negative error code otherwise\n+ */\n+off_t File::seek(off_t pos)\n+{\n+\tif (!isOpen())\n+\t\treturn -EINVAL;\n+\n+\toff_t ret = lseek(fd_, pos, SEEK_SET);\n+\tif (ret < 0)\n+\t\treturn -errno;\n+\n+\tpos_ = ret;\n+\treturn ret;\n+}\n+\n+/**\n+ * \\brief Read data from the file\n+ * \\param[in] data Memory to read data into\n+ *\n+ * Read at most \\a data.size() bytes from the file into \\a data.data(), and\n+ * return the number of bytes read. If less data than requested is available,\n+ * the returned byte count may be smaller than the requested size. If no more\n+ * data is available, 0 is returned.\n+ *\n+ * The position of the file as returned by pos() is advanced by the number of\n+ * bytes read. If an error occurs, the position isn't modified.\n+ *\n+ * \\return The number of bytes read on success, or a negative error code\n+ * otherwise\n+ */\n+ssize_t File::read(const Span<uint8_t> &data)\n+{\n+\tif (!isOpen())\n+\t\treturn -EINVAL;\n+\n+\tsize_t readBytes = 0;\n+\tssize_t ret = 0;\n+\n+\t/* Retry in case of interrupted system calls. */\n+\twhile (readBytes < data.size()) {\n+\t\tret = ::read(fd_, data.data() + readBytes,\n+\t\t\t data.size() - readBytes);\n+\t\tif (ret <= 0)\n+\t\t\tbreak;\n+\n+\t\treadBytes += ret;\n+\t}\n+\n+\tif (ret < 0 && !readBytes)\n+\t\treturn -errno;\n+\n+\tpos_ += readBytes;\n+\treturn readBytes;\n+}\n+\n+/**\n+ * \\brief Write data to the file\n+ * \\param[in] data Memory containing data to be written\n+ *\n+ * Write at most \\a data.size() bytes from \\a data.data() to the file, and\n+ * return the number of bytes written. If the file system doesn't have enough\n+ * space for the data, the returned byte count may be less than requested.\n+ *\n+ * The position of the file as returned by pos() is advanced by the number of\n+ * bytes written. If an error occurs, the position isn't modified.\n+ *\n+ * \\return The number of bytes written on success, or a negative error code\n+ * otherwise\n+ */\n+ssize_t File::write(const Span<const uint8_t> &data)\n+{\n+\tif (!isOpen())\n+\t\treturn -EINVAL;\n+\n+\tsize_t writtenBytes = 0;\n+\n+\t/* Retry in case of interrupted system calls. */\n+\twhile (writtenBytes < data.size()) {\n+\t\tssize_t ret = ::write(fd_, data.data() + writtenBytes,\n+\t\t\t\t data.size() - writtenBytes);\n+\t\tif (ret <= 0)\n+\t\t\tbreak;\n+\n+\t\twrittenBytes += ret;\n+\t}\n+\n+\tif (data.size() && !writtenBytes)\n+\t\treturn -errno;\n+\n+\tpos_ += writtenBytes;\n+\treturn writtenBytes;\n+}\n+\n /**\n * \\brief Map a region of the file in the process memory\n * \\param[in] offset The region offset within the file\n", "prefixes": [ "libcamera-devel", "1/4" ] }