From patchwork Tue Jul 14 09:09:33 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 8771 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 02C8ABD790 for ; Tue, 14 Jul 2020 09:09:49 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 6D34C607BD; Tue, 14 Jul 2020 11:09:48 +0200 (CEST) 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="ieu+jSUb"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 2F8BF60483 for ; Tue, 14 Jul 2020 11:09:47 +0200 (CEST) Received: from pendragon.bb.dnainternet.fi (81-175-216-236.bb.dnainternet.fi [81.175.216.236]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id AD788564 for ; Tue, 14 Jul 2020 11:09:46 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1594717786; bh=/QsgrHKm13Gr5qOL7fK0z3kLpCEXUBZJipLsV/Rpo6E=; h=From:To:Subject:Date:From; b=ieu+jSUblAbujoE7Xi4WtivR5KMUFEEWOj8Pi1UjQBTZWek+OliQFrrAJPg/ZtigK jUGSghQ/I76ngp8p/la4+LpZRvIb/Dd5Ax501tAztl4f0CyggWYUDgVqRlRR56Na28 YfKNUIebSw8edd/Sha2RVo7OqFZnjKwUnAVA99LE= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Date: Tue, 14 Jul 2020 12:09:33 +0300 Message-Id: <20200714090936.9562-1-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.27.0 MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 1/4] libcamera: file: Add read/write support 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" Add basic support to read and write data from/to a file, along with retrieving and setting the current read/write position. Signed-off-by: Laurent Pinchart Reviewed-by: Kieran Bingham --- Changes since v1: - Don't cache pos(), use lseek() --- include/libcamera/internal/file.h | 6 ++ src/libcamera/file.cpp | 109 ++++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+) diff --git a/include/libcamera/internal/file.h b/include/libcamera/internal/file.h index e3e72132e336..9b6d011f5e9d 100644 --- a/include/libcamera/internal/file.h +++ b/include/libcamera/internal/file.h @@ -49,6 +49,12 @@ public: int error() const { return error_; } ssize_t size() const; + off_t pos() const; + off_t seek(off_t pos); + + ssize_t read(const Span &data); + ssize_t write(const Span &data); + Span map(off_t offset = 0, ssize_t size = -1, MapFlag flags = MapNoOption); bool unmap(uint8_t *addr); diff --git a/src/libcamera/file.cpp b/src/libcamera/file.cpp index c471bde3fc68..d99a1806e1c8 100644 --- a/src/libcamera/file.cpp +++ b/src/libcamera/file.cpp @@ -237,6 +237,115 @@ ssize_t File::size() const return st.st_size; } +/** + * \brief Return current read or write position + * + * If the file is closed, this function returns 0. + * + * \return The current read or write position + */ +off_t File::pos() const +{ + if (!isOpen()) + return 0; + + return lseek(fd_, 0, SEEK_CUR); +} + +/** + * \brief Set the read or write position + * \param[in] pos The desired position + * \return The resulting offset from the beginning of the file on success, or a + * negative error code otherwise + */ +off_t File::seek(off_t pos) +{ + if (!isOpen()) + return -EINVAL; + + off_t ret = lseek(fd_, pos, SEEK_SET); + if (ret < 0) + return -errno; + + return ret; +} + +/** + * \brief Read data from the file + * \param[in] data Memory to read data into + * + * Read at most \a data.size() bytes from the file into \a data.data(), and + * return the number of bytes read. If less data than requested is available, + * the returned byte count may be smaller than the requested size. If no more + * data is available, 0 is returned. + * + * The position of the file as returned by pos() is advanced by the number of + * bytes read. If an error occurs, the position isn't modified. + * + * \return The number of bytes read on success, or a negative error code + * otherwise + */ +ssize_t File::read(const Span &data) +{ + if (!isOpen()) + return -EINVAL; + + size_t readBytes = 0; + ssize_t ret = 0; + + /* Retry in case of interrupted system calls. */ + while (readBytes < data.size()) { + ret = ::read(fd_, data.data() + readBytes, + data.size() - readBytes); + if (ret <= 0) + break; + + readBytes += ret; + } + + if (ret < 0 && !readBytes) + return -errno; + + return readBytes; +} + +/** + * \brief Write data to the file + * \param[in] data Memory containing data to be written + * + * Write at most \a data.size() bytes from \a data.data() to the file, and + * return the number of bytes written. If the file system doesn't have enough + * space for the data, the returned byte count may be less than requested. + * + * The position of the file as returned by pos() is advanced by the number of + * bytes written. If an error occurs, the position isn't modified. + * + * \return The number of bytes written on success, or a negative error code + * otherwise + */ +ssize_t File::write(const Span &data) +{ + if (!isOpen()) + return -EINVAL; + + size_t writtenBytes = 0; + + /* Retry in case of interrupted system calls. */ + while (writtenBytes < data.size()) { + ssize_t ret = ::write(fd_, data.data() + writtenBytes, + data.size() - writtenBytes); + if (ret <= 0) + break; + + writtenBytes += ret; + } + + if (data.size() && !writtenBytes) + return -errno; + + return writtenBytes; +} + /** * \brief Map a region of the file in the process memory * \param[in] offset The region offset within the file From patchwork Tue Jul 14 09:09:34 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 8772 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 BB5C6BD790 for ; Tue, 14 Jul 2020 09:09:49 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id EEA4B60738; Tue, 14 Jul 2020 11:09:48 +0200 (CEST) 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="t5JpDZxQ"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 5D80960484 for ; Tue, 14 Jul 2020 11:09:47 +0200 (CEST) Received: from pendragon.bb.dnainternet.fi (81-175-216-236.bb.dnainternet.fi [81.175.216.236]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 015E971D for ; Tue, 14 Jul 2020 11:09:46 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1594717787; bh=1r1YaBcFysKsRXmZAoZrhf3gFg1lsLvxu624Xa8W7gA=; h=From:To:Subject:Date:In-Reply-To:References:From; b=t5JpDZxQfH5BEtzq6hXxUlcA6aWiORJ3ynhJw3n0cBOBOmSGXxYWRtd2hyd6VlJBy g6YZGfaIuCmIde0S447UavdkxfcnmzHtqk9UJAligotnvtY0/uC/xkMU9Cm7Kng1v8 a7/2eaKiriCHfU6ZQFO1eobj2a5t5P2z0kjaScjc= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Date: Tue, 14 Jul 2020 12:09:34 +0300 Message-Id: <20200714090936.9562-2-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20200714090936.9562-1-laurent.pinchart@ideasonboard.com> References: <20200714090936.9562-1-laurent.pinchart@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 2/4] test: file: Add read/write tests 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" Add tests for the File::read() and File::write() functions. Signed-off-by: Laurent Pinchart Reviewed-by: Kieran Bingham Reviewed-by: Niklas Söderlund --- test/file.cpp | 65 ++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 62 insertions(+), 3 deletions(-) diff --git a/test/file.cpp b/test/file.cpp index 287831f3ef37..7688a9dc224a 100644 --- a/test/file.cpp +++ b/test/file.cpp @@ -30,11 +30,9 @@ protected: if (fd == -1) return TestFail; - ssize_t ret = write(fd, "libcamera", 9); - close(fd); - return ret == 9 ? TestPass : TestFail; + return TestPass; } int run() @@ -191,7 +189,68 @@ protected: return TestFail; } + file.close(); + + /* Test read and write. */ + std::array buffer = { 0 }; + + strncpy(reinterpret_cast(buffer.data()), "libcamera", + buffer.size()); + + file.setFileName(fileName_); + + if (file.read(buffer) >= 0) { + cerr << "Read succeeded on closed file" << endl; + return TestFail; + } + + if (file.write(buffer) >= 0) { + cerr << "Write succeeded on closed file" << endl; + return TestFail; + } + + file.open(File::ReadOnly); + + if (file.write(buffer) >= 0) { + cerr << "Write succeeded on read-only file" << endl; + return TestFail; + } + + file.close(); + + file.open(File::ReadWrite); + + if (file.write({ buffer.data(), 9 }) != 9) { + cerr << "Write test failed" << endl; + return TestFail; + } + + if (file.read(buffer) != 0) { + cerr << "Read at end of file test failed" << endl; + return TestFail; + } + + if (file.seek(0) != 0) { + cerr << "Seek test failed" << endl; + return TestFail; + } + + if (file.read(buffer) != 9) { + cerr << "Read test failed" << endl; + return TestFail; + } + + if (file.pos() != 9) { + cerr << "Position test failed" << endl; + return TestFail; + } + + file.close(); + /* Test mapping and unmapping. */ + file.setFileName("/proc/self/exe"); + file.open(File::ReadOnly); + Span data = file.map(); if (data.empty()) { cerr << "Mapping of complete file failed" << endl; From patchwork Tue Jul 14 09:09:35 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 8773 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 8C6A3BD790 for ; Tue, 14 Jul 2020 09:09:50 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 3C0AC60839; Tue, 14 Jul 2020 11:09:49 +0200 (CEST) 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="v4oUBEeM"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id A1B6160486 for ; Tue, 14 Jul 2020 11:09:47 +0200 (CEST) Received: from pendragon.bb.dnainternet.fi (81-175-216-236.bb.dnainternet.fi [81.175.216.236]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 4B363564 for ; Tue, 14 Jul 2020 11:09:47 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1594717787; bh=4Q6mWaJxSH6gDRcVDdSTfMaXLzYh/nkWlR/I+hAhK6E=; h=From:To:Subject:Date:In-Reply-To:References:From; b=v4oUBEeMhhjBItvYzlCD9T+HzzwdRk88CNXbHsOL+LMNEARLwdco7Jpk27Xw6saVd ABWI8duSDOUmOM1sLOJ82UL/wabMn6c6MbrBIm9F4/qLqyjEvxhc2J3wpsmCtNtF+4 sqO/F1HT1avUZAb+m7qMzq8Ifhi0w+ieHHUvRBOc= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Date: Tue, 14 Jul 2020 12:09:35 +0300 Message-Id: <20200714090936.9562-3-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20200714090936.9562-1-laurent.pinchart@ideasonboard.com> References: <20200714090936.9562-1-laurent.pinchart@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 3/4] test: file: Add file creation test 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" Add a test to verify file creation with File::open(). The test is expected to fail as the File::open() implementation is not correct. Signed-off-by: Laurent Pinchart Reviewed-by: Kieran Bingham Reviewed-by: Niklas Söderlund --- test/file.cpp | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/test/file.cpp b/test/file.cpp index 7688a9dc224a..f458f355ccad 100644 --- a/test/file.cpp +++ b/test/file.cpp @@ -31,6 +31,7 @@ protected: return TestFail; close(fd); + unlink(fileName_.c_str()); return TestPass; } @@ -191,14 +192,37 @@ protected: file.close(); + /* Test file creation. */ + file.setFileName(fileName_); + + if (file.exists()) { + cerr << "Temporary file already exists" << endl; + return TestFail; + } + + if (file.open(File::ReadOnly)) { + cerr << "Read-only open succeeded on nonexistent file" << endl; + return TestFail; + } + + if (!file.open(File::WriteOnly)) { + cerr << "Write-only open failed on nonexistent file" << endl; + return TestFail; + } + + if (!file.exists()) { + cerr << "Write-only open failed to create file" << endl; + return TestFail; + } + + file.close(); + /* Test read and write. */ std::array buffer = { 0 }; strncpy(reinterpret_cast(buffer.data()), "libcamera", buffer.size()); - file.setFileName(fileName_); - if (file.read(buffer) >= 0) { cerr << "Read succeeded on closed file" << endl; return TestFail; From patchwork Tue Jul 14 09:09:36 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 8774 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 E73EFBDB1C for ; Tue, 14 Jul 2020 09:09:50 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id AAB696081A; Tue, 14 Jul 2020 11:09:49 +0200 (CEST) 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="Kq8j6yyH"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id E6C30607BD for ; Tue, 14 Jul 2020 11:09:47 +0200 (CEST) Received: from pendragon.bb.dnainternet.fi (81-175-216-236.bb.dnainternet.fi [81.175.216.236]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 93DF671D for ; Tue, 14 Jul 2020 11:09:47 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1594717787; bh=HvbYpu1YRVVxXvEgooF8Tm0ef3WHzKwJTs2u3t7qwUI=; h=From:To:Subject:Date:In-Reply-To:References:From; b=Kq8j6yyHzDWwFCWj5lb1rlGVOTx+1VsXe+Np/ZOyGNmRSS26XZ7qC53GyoDwYnQfj eszIeKZ1RXqAjGX0j1Ti+o1kmpgTrD7CfnNbk5s+nngsEiCXy+IqsWtLoJgGy53m/h /5lkzqfCQ2CuEHIItHFb6YFRspImlqfnGMCePGVw= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Date: Tue, 14 Jul 2020 12:09:36 +0300 Message-Id: <20200714090936.9562-4-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20200714090936.9562-1-laurent.pinchart@ideasonboard.com> References: <20200714090936.9562-1-laurent.pinchart@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 4/4] libcamera: file: Create the file on open() if it doesn't exist 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" When a file is opened in WriteOnly or ReadWrite mode, create it if it doesn't exist. Signed-off-by: Laurent Pinchart Reviewed-by: Kieran Bingham Reviewed-by: Niklas Söderlund --- src/libcamera/file.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/libcamera/file.cpp b/src/libcamera/file.cpp index d99a1806e1c8..04b0cb61a1bf 100644 --- a/src/libcamera/file.cpp +++ b/src/libcamera/file.cpp @@ -148,8 +148,9 @@ bool File::exists() const * \param[in] mode The open mode * * This function opens the file specified by fileName() in \a mode. If the file - * doesn't exist and the mode is WriteOnly or ReadWrite, this - * function will attempt to create the file. + * doesn't exist and the mode is WriteOnly or ReadWrite, this function will + * attempt to create the file with initial permissions set to 0666 (modified by + * the process' umask). * * The error() status is updated. * @@ -163,8 +164,10 @@ bool File::open(File::OpenMode mode) } int flags = (mode & ReadWrite) - 1; + if (mode & WriteOnly) + flags |= O_CREAT; - fd_ = ::open(name_.c_str(), flags); + fd_ = ::open(name_.c_str(), flags, 0666); if (fd_ < 0) { error_ = -errno; return false;