{"id":12536,"url":"https://patchwork.libcamera.org/api/1.1/patches/12536/?format=json","web_url":"https://patchwork.libcamera.org/patch/12536/","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":"<20210610075027.523672-4-hiroh@chromium.org>","date":"2021-06-10T07:50:20","name":"[libcamera-devel,v2,03/10] libcamera: File: Manage fd by ScopedFD","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"9b1c95bd88dbbf414d21d454dca0bca732c00a8a","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/12536/mbox/","series":[{"id":2115,"url":"https://patchwork.libcamera.org/api/1.1/series/2115/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=2115","date":"2021-06-10T07:50:17","name":"Introduce ScopedFD","version":2,"mbox":"https://patchwork.libcamera.org/series/2115/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/12536/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/12536/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 EEAFCBD78E\n\tfor <parsemail@patchwork.libcamera.org>;\n\tThu, 10 Jun 2021 07:50:42 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id B134A6892F;\n\tThu, 10 Jun 2021 09:50:42 +0200 (CEST)","from mail-pl1-x630.google.com (mail-pl1-x630.google.com\n\t[IPv6:2607:f8b0:4864:20::630])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id D1A3F6892C\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 10 Jun 2021 09:50:39 +0200 (CEST)","by mail-pl1-x630.google.com with SMTP id c13so564121plz.0\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 10 Jun 2021 00:50:39 -0700 (PDT)","from hiroh2.tok.corp.google.com\n\t([2401:fa00:8f:203:98e0:b356:1c8a:25d4])\n\tby smtp.gmail.com with ESMTPSA id\n\td66sm1565161pfa.32.2021.06.10.00.50.36\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tThu, 10 Jun 2021 00:50:37 -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=\"Zu9rqh6W\"; 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=wGjYAnenK+/K4jZT2CK5UkjRDJMREEYxA5ZPmQtkY20=;\n\tb=Zu9rqh6WFfme1hAlMKrD+318RVhKT0nP4VwbXpcvmXFGa6SGAs62ZEfz+3Sfajx+7G\n\tcUt11G7B+ImEHenbAUdPdVpDbDG41DC9ilJX8UjwWVOXo6zAw13C4wfGvRV6iFWpjJOr\n\t+9EhQjAJ3DRzlgSJxbLXc8Mj4wokjpSIUfwa0=","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=wGjYAnenK+/K4jZT2CK5UkjRDJMREEYxA5ZPmQtkY20=;\n\tb=agVrhdMb0A9ZvxvvSVSJxZMR8hH65Qu0IZ+JYyOewiDSxBNSj7UtSybyhAghZFvjmX\n\tx5DqCq0CWwbkQTqPVWWtcjdns1b/tIaOL7jIMRGyQUcbgXSY03AxyvIW/3oMtaTsOLoG\n\tXZFQmv/UfQ71LhAw0Z7CBmgI8iRLUMbmAYpjtZQ6UF8qKPxv8ZvRhpY1CC5kGRMWJ9gV\n\tPXCnzDF/JWYaa9gnWG24Vf2tSRuHyY0FpK3xZB9v9xgS5WaEfQgd5FGNRh+4TH0eVksv\n\tch/69lAZJaTV2Q60nN8/Bv7yunBrx+Nmj8vmix3pPY1rtcZpci74ODVxwY9qNJNDI0Nh\n\tVEvA==","X-Gm-Message-State":"AOAM532bi5mmTVGLw6Ic6YcGvIUjKcQtMwuX5LX07ajmOS63cTkUUea/\n\ttuvluELdsCCrY1Qk6vsArpch+LpiPg5wAw==","X-Google-Smtp-Source":"ABdhPJw61zk/sI7UWmq3rgO1tmNTWUb1X0UPYNgHH2Uh3rfADsGnSmuJcLEJ5zgA2g61kJ2Vg6SNVA==","X-Received":"by 2002:a17:90a:1141:: with SMTP id\n\td1mr2093636pje.56.1623311438241; \n\tThu, 10 Jun 2021 00:50:38 -0700 (PDT)","From":"Hirokazu Honda <hiroh@chromium.org>","To":"libcamera-devel@lists.libcamera.org","Date":"Thu, 10 Jun 2021 16:50:20 +0900","Message-Id":"<20210610075027.523672-4-hiroh@chromium.org>","X-Mailer":"git-send-email 2.32.0.rc1.229.g3e70b5a671-goog","In-Reply-To":"<20210610075027.523672-1-hiroh@chromium.org>","References":"<20210610075027.523672-1-hiroh@chromium.org>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH v2 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>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"Manages the file descriptor owned by File by ScopedFD.\n\nSigned-off-by: Hirokazu Honda <hiroh@chromium.org>\nReviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n---\n include/libcamera/internal/file.h |  5 +++--\n src/libcamera/file.cpp            | 25 ++++++++++++-------------\n 2 files changed, 15 insertions(+), 15 deletions(-)","diff":"diff --git a/include/libcamera/internal/file.h b/include/libcamera/internal/file.h\nindex f0b313a5..eff5da55 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_fd.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..cc9b2a8e 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,8 +167,8 @@ 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+\tfd_ = ScopedFD(::open(name_.c_str(), flags, 0666));\n+\tif (!fd_.isValid()) {\n \t\terror_ = -errno;\n \t\treturn false;\n \t}\n@@ -199,11 +199,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 +232,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 +251,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 +265,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 +297,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 +334,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 +397,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","v2","03/10"]}