{"id":14824,"url":"https://patchwork.libcamera.org/api/patches/14824/?format=json","web_url":"https://patchwork.libcamera.org/patch/14824/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20211128235752.10836-10-laurent.pinchart@ideasonboard.com>","date":"2021-11-28T23:57:44","name":"[libcamera-devel,v3,09/17] libcamera: file: Manage fd by UniqueFD","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"9940c5beb914ed53704916273d4a75d16179c014","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/?format=json","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/14824/mbox/","series":[{"id":2768,"url":"https://patchwork.libcamera.org/api/series/2768/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=2768","date":"2021-11-28T23:57:35","name":"libcamera: Introduce UniqueFD","version":3,"mbox":"https://patchwork.libcamera.org/series/2768/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/14824/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/14824/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 33742C3250\n\tfor <parsemail@patchwork.libcamera.org>;\n\tSun, 28 Nov 2021 23:58:34 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id B4C3860596;\n\tMon, 29 Nov 2021 00:58:33 +0100 (CET)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 46ECF6011D\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 29 Nov 2021 00:58:25 +0100 (CET)","from pendragon.lan (62-78-145-57.bb.dnainternet.fi [62.78.145.57])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id C9E11730;\n\tMon, 29 Nov 2021 00:58:24 +0100 (CET)"],"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=\"YbgM20Pc\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1638143905;\n\tbh=zxxSb4XXJYUx3mNsv1S+NucKWSFlybKznq1uDPLBFFw=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=YbgM20PcObY7CfH02aq5LsHJh2Bx8HaWguhpdKvg25eiCqqMe85MY/T04UHQEgUUJ\n\tz0Oe/0Rsn7yOKunpEAxTAWJiIT7ZHi9QSBxO4eDXL28qUyHP4GPHznQ25nFfqLLi5x\n\t05OqPDZ0vQCIqML+ffdUQ0TbznFz8Btv0/IelozA=","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Date":"Mon, 29 Nov 2021 01:57:44 +0200","Message-Id":"<20211128235752.10836-10-laurent.pinchart@ideasonboard.com>","X-Mailer":"git-send-email 2.32.0","In-Reply-To":"<20211128235752.10836-1-laurent.pinchart@ideasonboard.com>","References":"<20211128235752.10836-1-laurent.pinchart@ideasonboard.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH v3 09/17] libcamera: file: Manage fd by\n\tUniqueFD","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>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"From: Hirokazu Honda <hiroh@chromium.org>\n\nManages the file descriptor owned by File by UniqueFD.\n\nSigned-off-by: Hirokazu Honda <hiroh@chromium.org>\nReviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n---\n include/libcamera/base/file.h |  5 +++--\n src/libcamera/base/file.cpp   | 25 ++++++++++++-------------\n 2 files changed, 15 insertions(+), 15 deletions(-)","diff":"diff --git a/include/libcamera/base/file.h b/include/libcamera/base/file.h\nindex 996751a7ab72..47769da7abc2 100644\n--- a/include/libcamera/base/file.h\n+++ b/include/libcamera/base/file.h\n@@ -17,6 +17,7 @@\n #include <libcamera/base/class.h>\n #include <libcamera/base/flags.h>\n #include <libcamera/base/span.h>\n+#include <libcamera/base/unique_fd.h>\n \n namespace libcamera {\n \n@@ -50,7 +51,7 @@ public:\n \tbool exists() const;\n \n \tbool open(OpenMode mode);\n-\tbool isOpen() const { return fd_ != -1; }\n+\tbool isOpen() const { return fd_.isValid(); }\n \tOpenMode openMode() const { return mode_; }\n \tvoid close();\n \n@@ -76,7 +77,7 @@ private:\n \tvoid unmapAll();\n \n \tstd::string name_;\n-\tint fd_;\n+\tUniqueFD fd_;\n \tOpenMode mode_;\n \n \tint error_;\ndiff --git a/src/libcamera/base/file.cpp b/src/libcamera/base/file.cpp\nindex 7043f9461cf7..66c73c406198 100644\n--- a/src/libcamera/base/file.cpp\n+++ b/src/libcamera/base/file.cpp\n@@ -84,7 +84,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_(OpenModeFlag::NotOpen), error_(0)\n+\t: name_(name), mode_(OpenModeFlag::NotOpen), error_(0)\n {\n }\n \n@@ -95,7 +95,7 @@ File::File(const std::string &name)\n  * setFileName().\n  */\n File::File()\n-\t: fd_(-1), mode_(OpenModeFlag::NotOpen), error_(0)\n+\t: mode_(OpenModeFlag::NotOpen), error_(0)\n {\n }\n \n@@ -178,8 +178,8 @@ bool File::open(File::OpenMode mode)\n \tif (mode & OpenModeFlag::WriteOnly)\n \t\tflags |= O_CREAT;\n \n-\tfd_ = ::open(name_.c_str(), flags, 0666);\n-\tif (fd_ < 0) {\n+\tfd_ = UniqueFD(::open(name_.c_str(), flags, 0666));\n+\tif (!fd_.isValid()) {\n \t\terror_ = -errno;\n \t\treturn false;\n \t}\n@@ -210,11 +210,10 @@ bool File::open(File::OpenMode mode)\n  */\n void File::close()\n {\n-\tif (fd_ == -1)\n+\tif (!fd_.isValid())\n \t\treturn;\n \n-\t::close(fd_);\n-\tfd_ = -1;\n+\tfd_.reset();\n \tmode_ = OpenModeFlag::NotOpen;\n }\n \n@@ -244,7 +243,7 @@ ssize_t File::size() const\n \t\treturn -EINVAL;\n \n \tstruct stat st;\n-\tint ret = fstat(fd_, &st);\n+\tint ret = fstat(fd_.get(), &st);\n \tif (ret < 0)\n \t\treturn -errno;\n \n@@ -263,7 +262,7 @@ off_t File::pos() const\n \tif (!isOpen())\n \t\treturn 0;\n \n-\treturn lseek(fd_, 0, SEEK_CUR);\n+\treturn lseek(fd_.get(), 0, SEEK_CUR);\n }\n \n /**\n@@ -277,7 +276,7 @@ off_t File::seek(off_t pos)\n \tif (!isOpen())\n \t\treturn -EINVAL;\n \n-\toff_t ret = lseek(fd_, pos, SEEK_SET);\n+\toff_t ret = lseek(fd_.get(), pos, SEEK_SET);\n \tif (ret < 0)\n \t\treturn -errno;\n \n@@ -309,7 +308,7 @@ ssize_t File::read(const Span<uint8_t> &data)\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\tret = ::read(fd_.get(), data.data() + readBytes,\n \t\t\t     data.size() - readBytes);\n \t\tif (ret <= 0)\n \t\t\tbreak;\n@@ -346,7 +345,7 @@ ssize_t File::write(const Span<const uint8_t> &data)\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\tssize_t ret = ::write(fd_.get(), data.data() + writtenBytes,\n \t\t\t\t      data.size() - writtenBytes);\n \t\tif (ret <= 0)\n \t\t\tbreak;\n@@ -409,7 +408,7 @@ Span<uint8_t> File::map(off_t offset, ssize_t size, File::MapFlags flags)\n \tif (flags & MapFlag::Private)\n \t\tprot |= PROT_WRITE;\n \n-\tvoid *map = mmap(NULL, size, prot, mmapFlags, fd_, offset);\n+\tvoid *map = mmap(NULL, size, prot, mmapFlags, fd_.get(), offset);\n \tif (map == MAP_FAILED) {\n \t\terror_ = -errno;\n \t\treturn {};\n","prefixes":["libcamera-devel","v3","09/17"]}