From patchwork Sun Nov 28 23:57:44 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 14824 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 33742C3250 for ; Sun, 28 Nov 2021 23:58:34 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id B4C3860596; Mon, 29 Nov 2021 00:58:33 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="YbgM20Pc"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 46ECF6011D for ; Mon, 29 Nov 2021 00:58:25 +0100 (CET) Received: from pendragon.lan (62-78-145-57.bb.dnainternet.fi [62.78.145.57]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id C9E11730; Mon, 29 Nov 2021 00:58:24 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1638143905; bh=zxxSb4XXJYUx3mNsv1S+NucKWSFlybKznq1uDPLBFFw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=YbgM20PcObY7CfH02aq5LsHJh2Bx8HaWguhpdKvg25eiCqqMe85MY/T04UHQEgUUJ z0Oe/0Rsn7yOKunpEAxTAWJiIT7ZHi9QSBxO4eDXL28qUyHP4GPHznQ25nFfqLLi5x 05OqPDZ0vQCIqML+ffdUQ0TbznFz8Btv0/IelozA= From: Laurent Pinchart 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 Subject: [libcamera-devel] [PATCH v3 09/17] libcamera: file: Manage fd by UniqueFD X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" From: Hirokazu Honda Manages the file descriptor owned by File by UniqueFD. Signed-off-by: Hirokazu Honda Reviewed-by: Laurent Pinchart Reviewed-by: Jacopo Mondi --- include/libcamera/base/file.h | 5 +++-- src/libcamera/base/file.cpp | 25 ++++++++++++------------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/include/libcamera/base/file.h b/include/libcamera/base/file.h index 996751a7ab72..47769da7abc2 100644 --- a/include/libcamera/base/file.h +++ b/include/libcamera/base/file.h @@ -17,6 +17,7 @@ #include #include #include +#include namespace libcamera { @@ -50,7 +51,7 @@ public: bool exists() const; bool open(OpenMode mode); - bool isOpen() const { return fd_ != -1; } + bool isOpen() const { return fd_.isValid(); } OpenMode openMode() const { return mode_; } void close(); @@ -76,7 +77,7 @@ private: void unmapAll(); std::string name_; - int fd_; + UniqueFD fd_; OpenMode mode_; int error_; diff --git a/src/libcamera/base/file.cpp b/src/libcamera/base/file.cpp index 7043f9461cf7..66c73c406198 100644 --- a/src/libcamera/base/file.cpp +++ b/src/libcamera/base/file.cpp @@ -84,7 +84,7 @@ LOG_DEFINE_CATEGORY(File) * before performing I/O operations. */ File::File(const std::string &name) - : name_(name), fd_(-1), mode_(OpenModeFlag::NotOpen), error_(0) + : name_(name), mode_(OpenModeFlag::NotOpen), error_(0) { } @@ -95,7 +95,7 @@ File::File(const std::string &name) * setFileName(). */ File::File() - : fd_(-1), mode_(OpenModeFlag::NotOpen), error_(0) + : mode_(OpenModeFlag::NotOpen), error_(0) { } @@ -178,8 +178,8 @@ bool File::open(File::OpenMode mode) if (mode & OpenModeFlag::WriteOnly) flags |= O_CREAT; - fd_ = ::open(name_.c_str(), flags, 0666); - if (fd_ < 0) { + fd_ = UniqueFD(::open(name_.c_str(), flags, 0666)); + if (!fd_.isValid()) { error_ = -errno; return false; } @@ -210,11 +210,10 @@ bool File::open(File::OpenMode mode) */ void File::close() { - if (fd_ == -1) + if (!fd_.isValid()) return; - ::close(fd_); - fd_ = -1; + fd_.reset(); mode_ = OpenModeFlag::NotOpen; } @@ -244,7 +243,7 @@ ssize_t File::size() const return -EINVAL; struct stat st; - int ret = fstat(fd_, &st); + int ret = fstat(fd_.get(), &st); if (ret < 0) return -errno; @@ -263,7 +262,7 @@ off_t File::pos() const if (!isOpen()) return 0; - return lseek(fd_, 0, SEEK_CUR); + return lseek(fd_.get(), 0, SEEK_CUR); } /** @@ -277,7 +276,7 @@ off_t File::seek(off_t pos) if (!isOpen()) return -EINVAL; - off_t ret = lseek(fd_, pos, SEEK_SET); + off_t ret = lseek(fd_.get(), pos, SEEK_SET); if (ret < 0) return -errno; @@ -309,7 +308,7 @@ ssize_t File::read(const Span &data) /* Retry in case of interrupted system calls. */ while (readBytes < data.size()) { - ret = ::read(fd_, data.data() + readBytes, + ret = ::read(fd_.get(), data.data() + readBytes, data.size() - readBytes); if (ret <= 0) break; @@ -346,7 +345,7 @@ ssize_t File::write(const Span &data) /* Retry in case of interrupted system calls. */ while (writtenBytes < data.size()) { - ssize_t ret = ::write(fd_, data.data() + writtenBytes, + ssize_t ret = ::write(fd_.get(), data.data() + writtenBytes, data.size() - writtenBytes); if (ret <= 0) break; @@ -409,7 +408,7 @@ Span File::map(off_t offset, ssize_t size, File::MapFlags flags) if (flags & MapFlag::Private) prot |= PROT_WRITE; - void *map = mmap(NULL, size, prot, mmapFlags, fd_, offset); + void *map = mmap(NULL, size, prot, mmapFlags, fd_.get(), offset); if (map == MAP_FAILED) { error_ = -errno; return {};