summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLaurent Pinchart <laurent.pinchart@ideasonboard.com>2021-09-02 02:49:16 +0300
committerLaurent Pinchart <laurent.pinchart@ideasonboard.com>2021-09-07 19:17:38 +0300
commit8e18f8d45fb61977926a3395b1963028bab258f9 (patch)
tree973dedb2b5bdce20bd21d475ec3535f236655009
parentf9c1a40e21bff9739c9910436d59b25a69538cdf (diff)
libcamera: file_descriptor: Add a function to retrieve the inode
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 <laurent.pinchart@ideasonboard.com> Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com> Reviewed-by: Jean-Michel Hautbois <jeanmichel.hautbois@ideasonboard.com> Reviewed-by: Paul Elder <paul.elder@ideasonboard.com> Reviewed-by: Hirokazu Honda <hiroh@chromium.org>
-rw-r--r--include/libcamera/file_descriptor.h3
-rw-r--r--src/libcamera/file_descriptor.cpp26
2 files changed, 29 insertions, 0 deletions
diff --git a/include/libcamera/file_descriptor.h b/include/libcamera/file_descriptor.h
index d514aac7..988f9b7a 100644
--- a/include/libcamera/file_descriptor.h
+++ b/include/libcamera/file_descriptor.h
@@ -8,6 +8,7 @@
#define __LIBCAMERA_FILE_DESCRIPTOR_H__
#include <memory>
+#include <sys/types.h>
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 9f9ebc81..0409c3e1 100644
--- a/src/libcamera/file_descriptor.cpp
+++ b/src/libcamera/file_descriptor.cpp
@@ -8,6 +8,8 @@
#include <libcamera/file_descriptor.h>
#include <string.h>
+#include <sys/stat.h>
+#include <sys/types.h>
#include <unistd.h>
#include <utility>
@@ -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) {