{"id":11939,"url":"https://patchwork.libcamera.org/api/1.1/patches/11939/?format=json","web_url":"https://patchwork.libcamera.org/patch/11939/","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":"<20210415083843.3399502-3-hiroh@chromium.org>","date":"2021-04-15T08:38:36","name":"[libcamera-devel,RFC,03/10] libcamera: File: Manage fd by ScopedFD","commit_ref":null,"pull_url":null,"state":"changes-requested","archived":false,"hash":"4256f6a4bd5ae9819704398b67f77dafb8230a09","submitter":{"id":63,"url":"https://patchwork.libcamera.org/api/1.1/people/63/?format=json","name":"Hirokazu Honda","email":"hiroh@chromium.org"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/11939/mbox/","series":[{"id":1933,"url":"https://patchwork.libcamera.org/api/1.1/series/1933/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=1933","date":"2021-04-15T08:38:35","name":null,"version":1,"mbox":"https://patchwork.libcamera.org/series/1933/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/11939/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/11939/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 8D850BD224\n\tfor <parsemail@patchwork.libcamera.org>;\n\tThu, 15 Apr 2021 08:38:55 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id C41A568813;\n\tThu, 15 Apr 2021 10:38:54 +0200 (CEST)","from mail-pf1-x42b.google.com (mail-pf1-x42b.google.com\n\t[IPv6:2607:f8b0:4864:20::42b])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 742766880B\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 15 Apr 2021 10:38:53 +0200 (CEST)","by mail-pf1-x42b.google.com with SMTP id w6so1059942pfc.8\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 15 Apr 2021 01:38:53 -0700 (PDT)","from hiroh2.tok.corp.google.com\n\t([2401:fa00:8f:2:84f5:7981:dfbe:8f02])\n\tby smtp.gmail.com with ESMTPSA id\n\t205sm1520258pfc.201.2021.04.15.01.38.50\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tThu, 15 Apr 2021 01:38:51 -0700 (PDT)"],"Authentication-Results":"lancelot.ideasonboard.com;\n\tdkim=fail reason=\"signature verification failed\" (1024-bit key;\n\tunprotected) header.d=chromium.org header.i=@chromium.org\n\theader.b=\"FHptjf/b\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org;\n\ts=google; \n\th=from:to:cc:subject:date:message-id:in-reply-to:references\n\t:mime-version:content-transfer-encoding;\n\tbh=DYcKT7iH73omu+2Qdx1U5wAzjkExITB+CoFUzylVeWM=;\n\tb=FHptjf/b49SClU9j3PfvlIF5TnGtF2l1MvAf+HjTgUbTDmC3CtomMFljyxzTNKTSrg\n\tEXfbAmQR9i+O/ji6mh61P3tzA3XcgUNUVIHOcGLKuC3yLYETipVJ42jAjjJw5TzQNWkS\n\tKwUiG2ouG7fDUZjisMX9DETuhRwcx2XeXBnT0=","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20161025;\n\th=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to\n\t:references:mime-version:content-transfer-encoding;\n\tbh=DYcKT7iH73omu+2Qdx1U5wAzjkExITB+CoFUzylVeWM=;\n\tb=XtUJ5b50E3Q0qreMe3HhIMD+tupf1uTIEYOrVr8w9wYPDtSdhjpuzy1eL3U7Ffv4qJ\n\tZNH3YB3mTNoiXVCpzqbCX6qL9IUtDk65MEJa7Fqiu8MGrXAlAjoPHoT0v01Tx8rRvkTW\n\tz9L587Lp5o2AkSgVLPhicFC4fhoM+0I2db6cBtELE+aDUION9ELJYFTzrtQ3bwS3nref\n\t70Ze4PSMVyMa80MjNwuHfzWuEo6JxJQUmrbrosxem6YKSXyQc/An/bF8GYOyXVgIHYIx\n\tP835nNhz1OpSSXqLkdUSQ2iv15+u4LlgL+ZBHSjJDx5CPcQOAo60b9lrRwy+y5PeQ0WR\n\tjTcA==","X-Gm-Message-State":"AOAM531jzYiypXRiAy+peCsPZblnhHRD6SFiouE0D/s9BIlEpe/XU6Wk\n\tx6g+WC/A665EBm2dQCTdWtWESAxFPSUSdw==","X-Google-Smtp-Source":"ABdhPJz5F5JNTLAXeaYyluRk3HFp2mRvEyRSLLWKVLbWZ2naAYUHnFu/vMuJQ7ewqNXGtu1RgB7Z7g==","X-Received":"by 2002:a63:9dc5:: with SMTP id\n\ti188mr2501704pgd.191.1618475931862; \n\tThu, 15 Apr 2021 01:38:51 -0700 (PDT)","From":"Hirokazu Honda <hiroh@chromium.org>","To":"libcamera-devel@lists.libcamera.org","Date":"Thu, 15 Apr 2021 17:38:36 +0900","Message-Id":"<20210415083843.3399502-3-hiroh@chromium.org>","X-Mailer":"git-send-email 2.31.1.368.gbe11c130af-goog","In-Reply-To":"<20210415083843.3399502-1-hiroh@chromium.org>","References":"<20210415083843.3399502-1-hiroh@chromium.org>","MIME-Version":"1.0","Subject":"[libcamera-devel] [RFC PATCH 03/10] libcamera: File: Manage fd by\n\tScopedFD","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":"File owns the file descriptor for a file. It should be managed\nby ScopedFD to avoid the leakage.\n\nSigned-off-by: Hirokazu Honda <hiroh@chromium.org>\n---\n include/libcamera/internal/file.h |  5 +++--\n src/libcamera/file.cpp            | 26 +++++++++++++-------------\n 2 files changed, 16 insertions(+), 15 deletions(-)","diff":"diff --git a/include/libcamera/internal/file.h b/include/libcamera/internal/file.h\nindex f0b313a5..e6eb7f04 100644\n--- a/include/libcamera/internal/file.h\n+++ b/include/libcamera/internal/file.h\n@@ -12,6 +12,7 @@\n #include <sys/types.h>\n \n #include <libcamera/class.h>\n+#include <libcamera/scoped_file_descriptor.h>\n #include <libcamera/span.h>\n \n namespace libcamera {\n@@ -40,7 +41,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@@ -65,7 +66,7 @@ private:\n \tvoid unmapAll();\n \n \tstd::string name_;\n-\tint fd_;\n+\tScopedFD fd_;\n \tOpenMode mode_;\n \n \tint error_;\ndiff --git a/src/libcamera/file.cpp b/src/libcamera/file.cpp\nindex bce2b613..4db9a8d3 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), mode_(NotOpen), 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: mode_(NotOpen), error_(0)\n {\n }\n \n@@ -167,12 +167,13 @@ bool File::open(File::OpenMode mode)\n \tif (mode & WriteOnly)\n \t\tflags |= O_CREAT;\n \n-\tfd_ = ::open(name_.c_str(), flags, 0666);\n-\tif (fd_ < 0) {\n+\tint fd = ::open(name_.c_str(), flags, 0666);\n+\tif (fd < 0) {\n \t\terror_ = -errno;\n \t\treturn false;\n \t}\n \n+\tfd_ = ScopedFD(fd);\n \tmode_ = mode;\n \terror_ = 0;\n \treturn true;\n@@ -199,11 +200,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_ = NotOpen;\n }\n \n@@ -233,7 +233,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@@ -252,7 +252,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@@ -266,7 +266,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@@ -298,7 +298,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@@ -335,7 +335,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@@ -398,7 +398,7 @@ Span<uint8_t> File::map(off_t offset, ssize_t size, enum File::MapFlag flags)\n \tif (flags & MapPrivate)\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","RFC","03/10"]}