From patchwork Thu Jun 10 07:50:20 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hirokazu Honda X-Patchwork-Id: 12536 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 EEAFCBD78E for ; Thu, 10 Jun 2021 07:50:42 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id B134A6892F; Thu, 10 Jun 2021 09:50:42 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="Zu9rqh6W"; dkim-atps=neutral Received: from mail-pl1-x630.google.com (mail-pl1-x630.google.com [IPv6:2607:f8b0:4864:20::630]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id D1A3F6892C for ; Thu, 10 Jun 2021 09:50:39 +0200 (CEST) Received: by mail-pl1-x630.google.com with SMTP id c13so564121plz.0 for ; Thu, 10 Jun 2021 00:50:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=wGjYAnenK+/K4jZT2CK5UkjRDJMREEYxA5ZPmQtkY20=; b=Zu9rqh6WFfme1hAlMKrD+318RVhKT0nP4VwbXpcvmXFGa6SGAs62ZEfz+3Sfajx+7G cUt11G7B+ImEHenbAUdPdVpDbDG41DC9ilJX8UjwWVOXo6zAw13C4wfGvRV6iFWpjJOr +9EhQjAJ3DRzlgSJxbLXc8Mj4wokjpSIUfwa0= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=wGjYAnenK+/K4jZT2CK5UkjRDJMREEYxA5ZPmQtkY20=; b=agVrhdMb0A9ZvxvvSVSJxZMR8hH65Qu0IZ+JYyOewiDSxBNSj7UtSybyhAghZFvjmX x5DqCq0CWwbkQTqPVWWtcjdns1b/tIaOL7jIMRGyQUcbgXSY03AxyvIW/3oMtaTsOLoG XZFQmv/UfQ71LhAw0Z7CBmgI8iRLUMbmAYpjtZQ6UF8qKPxv8ZvRhpY1CC5kGRMWJ9gV PXCnzDF/JWYaa9gnWG24Vf2tSRuHyY0FpK3xZB9v9xgS5WaEfQgd5FGNRh+4TH0eVksv ch/69lAZJaTV2Q60nN8/Bv7yunBrx+Nmj8vmix3pPY1rtcZpci74ODVxwY9qNJNDI0Nh VEvA== X-Gm-Message-State: AOAM532bi5mmTVGLw6Ic6YcGvIUjKcQtMwuX5LX07ajmOS63cTkUUea/ tuvluELdsCCrY1Qk6vsArpch+LpiPg5wAw== X-Google-Smtp-Source: ABdhPJw61zk/sI7UWmq3rgO1tmNTWUb1X0UPYNgHH2Uh3rfADsGnSmuJcLEJ5zgA2g61kJ2Vg6SNVA== X-Received: by 2002:a17:90a:1141:: with SMTP id d1mr2093636pje.56.1623311438241; Thu, 10 Jun 2021 00:50:38 -0700 (PDT) Received: from hiroh2.tok.corp.google.com ([2401:fa00:8f:203:98e0:b356:1c8a:25d4]) by smtp.gmail.com with ESMTPSA id d66sm1565161pfa.32.2021.06.10.00.50.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Jun 2021 00:50:37 -0700 (PDT) From: Hirokazu Honda 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 Subject: [libcamera-devel] [PATCH v2 03/10] libcamera: File: Manage fd by ScopedFD 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" Manages the file descriptor owned by File by ScopedFD. Signed-off-by: Hirokazu Honda Reviewed-by: Laurent Pinchart --- include/libcamera/internal/file.h | 5 +++-- src/libcamera/file.cpp | 25 ++++++++++++------------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/include/libcamera/internal/file.h b/include/libcamera/internal/file.h index f0b313a5..eff5da55 100644 --- a/include/libcamera/internal/file.h +++ b/include/libcamera/internal/file.h @@ -12,6 +12,7 @@ #include #include +#include #include namespace libcamera { @@ -40,7 +41,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(); @@ -65,7 +66,7 @@ private: void unmapAll(); std::string name_; - int fd_; + ScopedFD fd_; OpenMode mode_; int error_; diff --git a/src/libcamera/file.cpp b/src/libcamera/file.cpp index bce2b613..cc9b2a8e 100644 --- a/src/libcamera/file.cpp +++ b/src/libcamera/file.cpp @@ -73,7 +73,7 @@ LOG_DEFINE_CATEGORY(File) * before performing I/O operations. */ File::File(const std::string &name) - : name_(name), fd_(-1), mode_(NotOpen), error_(0) + : name_(name), mode_(NotOpen), error_(0) { } @@ -84,7 +84,7 @@ File::File(const std::string &name) * setFileName(). */ File::File() - : fd_(-1), mode_(NotOpen), error_(0) + : mode_(NotOpen), error_(0) { } @@ -167,8 +167,8 @@ bool File::open(File::OpenMode mode) if (mode & WriteOnly) flags |= O_CREAT; - fd_ = ::open(name_.c_str(), flags, 0666); - if (fd_ < 0) { + fd_ = ScopedFD(::open(name_.c_str(), flags, 0666)); + if (!fd_.isValid()) { error_ = -errno; return false; } @@ -199,11 +199,10 @@ bool File::open(File::OpenMode mode) */ void File::close() { - if (fd_ == -1) + if (!fd_.isValid()) return; - ::close(fd_); - fd_ = -1; + fd_.reset(); mode_ = NotOpen; } @@ -233,7 +232,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; @@ -252,7 +251,7 @@ off_t File::pos() const if (!isOpen()) return 0; - return lseek(fd_, 0, SEEK_CUR); + return lseek(fd_.get(), 0, SEEK_CUR); } /** @@ -266,7 +265,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; @@ -298,7 +297,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; @@ -335,7 +334,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; @@ -398,7 +397,7 @@ Span File::map(off_t offset, ssize_t size, enum File::MapFlag flags) if (flags & MapPrivate) 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 {};