From patchwork Tue Nov 30 03:38:07 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 14865 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 162F6C324F for ; Tue, 30 Nov 2021 03:39:09 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id ADCC0605CB; Tue, 30 Nov 2021 04:39:08 +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="sTNNBZOP"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 635E36059E for ; Tue, 30 Nov 2021 04:38:52 +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 E133811C5; Tue, 30 Nov 2021 04:38:51 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1638243532; bh=vElHEE1F82SJUGoJvqv0YsLh86mBBcXrpD58wJKYuCY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=sTNNBZOPEhFT4hM2d/t17RCi9iM35VlRpuh1xzhdvE8WcvFzrTI/bKtHuAUW6nzE9 PkVJ+mD2T/lP2p6HOTqgRFb2mXcIufw3qRJLEmANTSr/dJcXl8/++0FjT88KSZfsqU WNANz1qZmM8GUdGO+7taBvKRcBl8X+iFEwdh+nk4= From: Laurent Pinchart 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 Subject: [libcamera-devel] [PATCH v4 09/22] 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 9b62871b8230..dfb8dd0f19af 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(); @@ -75,7 +76,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 a04dfacdc102..cab6e714fb07 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 {};