{"id":14865,"url":"https://patchwork.libcamera.org/api/1.1/patches/14865/?format=json","web_url":"https://patchwork.libcamera.org/patch/14865/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/1.1/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":"<20211130033820.18235-10-laurent.pinchart@ideasonboard.com>","date":"2021-11-30T03:38:07","name":"[libcamera-devel,v4,09/22] libcamera: file: Manage fd by UniqueFD","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"9940c5beb914ed53704916273d4a75d16179c014","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/1.1/people/2/?format=json","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/14865/mbox/","series":[{"id":2776,"url":"https://patchwork.libcamera.org/api/1.1/series/2776/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=2776","date":"2021-11-30T03:37:58","name":"libcamera: Introduce UniqueFD","version":4,"mbox":"https://patchwork.libcamera.org/series/2776/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/14865/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/14865/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 162F6C324F\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue, 30 Nov 2021 03:39:09 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id ADCC0605CB;\n\tTue, 30 Nov 2021 04:39:08 +0100 (CET)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 635E36059E\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 30 Nov 2021 04:38:52 +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 E133811C5; \n\tTue, 30 Nov 2021 04:38:51 +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=\"sTNNBZOP\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1638243532;\n\tbh=vElHEE1F82SJUGoJvqv0YsLh86mBBcXrpD58wJKYuCY=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=sTNNBZOPEhFT4hM2d/t17RCi9iM35VlRpuh1xzhdvE8WcvFzrTI/bKtHuAUW6nzE9\n\tPkVJ+mD2T/lP2p6HOTqgRFb2mXcIufw3qRJLEmANTSr/dJcXl8/++0FjT88KSZfsqU\n\tWNANz1qZmM8GUdGO+7taBvKRcBl8X+iFEwdh+nk4=","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Date":"Tue, 30 Nov 2021 05:38:07 +0200","Message-Id":"<20211130033820.18235-10-laurent.pinchart@ideasonboard.com>","X-Mailer":"git-send-email 2.32.0","In-Reply-To":"<20211130033820.18235-1-laurent.pinchart@ideasonboard.com>","References":"<20211130033820.18235-1-laurent.pinchart@ideasonboard.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH v4 09/22] 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>\nReviewed-by: Jacopo Mondi <jacopo@jmondi.org>\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 9b62871b8230..dfb8dd0f19af 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@@ -75,7 +76,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 a04dfacdc102..cab6e714fb07 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","v4","09/22"]}