From patchwork Mon Sep 6 22:56:08 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 13668 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 0A34EBE175 for ; Mon, 6 Sep 2021 22:57:04 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 842CB69174; Tue, 7 Sep 2021 00:57:01 +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="evgzIiaW"; 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 7A23769167 for ; Tue, 7 Sep 2021 00:56:59 +0200 (CEST) Received: from pendragon.lan (62-78-145-57.bb.dnainternet.fi [62.78.145.57]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id EEA60993; Tue, 7 Sep 2021 00:56:58 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1630969019; bh=uLkB9Y9I1Q8pO5WttL2JG13L9GNHJ16SgH6o+Osv5LE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=evgzIiaWssesMIWVcdrDz1b5LFnoWuf/w6l5g7QX/RLas70oMQI1SrX8LSSM2Q9v/ sGmFK6FW1iy/1wF4J21MwhViWxxfljj4GUEapBD1WxD9ehjsLfBjIEguwH4qBxcSN7 CqCEbARZHDG9GWpdwulFAG4kBf/Hc0JMKdioTPSA= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Date: Tue, 7 Sep 2021 01:56:08 +0300 Message-Id: <20210906225636.14683-2-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210906225420.13275-1-laurent.pinchart@ideasonboard.com> References: <20210906225420.13275-1-laurent.pinchart@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v3 02/30] libcamera: file_descriptor: Add a function to retrieve the inode 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" The inode is useful to check if two file descriptors refer to the same file. Add a function to retrieve it. Signed-off-by: Laurent Pinchart Reviewed-by: Kieran Bingham Reviewed-by: Jean-Michel Hautbois Reviewed-by: Paul Elder Reviewed-by: Hirokazu Honda --- Changes since v1: - Use isValid() instead of open-coding it - Print a message on error --- include/libcamera/file_descriptor.h | 3 +++ src/libcamera/file_descriptor.cpp | 26 ++++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/include/libcamera/file_descriptor.h b/include/libcamera/file_descriptor.h index d514aac7697b..988f9b7a3d25 100644 --- a/include/libcamera/file_descriptor.h +++ b/include/libcamera/file_descriptor.h @@ -8,6 +8,7 @@ #define __LIBCAMERA_FILE_DESCRIPTOR_H__ #include +#include namespace libcamera { @@ -27,6 +28,8 @@ public: int fd() const { return fd_ ? fd_->fd() : -1; } FileDescriptor dup() const; + ino_t inode() const; + private: class Descriptor { diff --git a/src/libcamera/file_descriptor.cpp b/src/libcamera/file_descriptor.cpp index 9f9ebc81f738..0409c3e1758c 100644 --- a/src/libcamera/file_descriptor.cpp +++ b/src/libcamera/file_descriptor.cpp @@ -8,6 +8,8 @@ #include #include +#include +#include #include #include @@ -221,6 +223,30 @@ FileDescriptor FileDescriptor::dup() const return FileDescriptor(fd()); } +/** + * \brief Retrieve the file descriptor inode + * + * \todo Should this move to the File class ? + * + * \return The file descriptor inode on success, or 0 on error + */ +ino_t FileDescriptor::inode() const +{ + if (!isValid()) + return 0; + + struct stat st; + int ret = fstat(fd_->fd(), &st); + if (ret < 0) { + ret = -errno; + LOG(FileDescriptor, Fatal) + << "Failed to fstat() fd: " << strerror(-ret); + return 0; + } + + return st.st_ino; +} + FileDescriptor::Descriptor::Descriptor(int fd, bool duplicate) { if (!duplicate) {