]> xenbits.xensource.com Git - unikraft/unikraft.git/commitdiff
lib/posix-fd: Add optional name field to ofiles
authorAndrei Tatar <andrei@unikraft.io>
Wed, 25 Sep 2024 14:53:47 +0000 (16:53 +0200)
committerUnikraft Bot <monkey@unikraft.io>
Tue, 25 Feb 2025 07:58:01 +0000 (07:58 +0000)
This change adds an optional, binary-compatible, name field to open file
descriptions, allowing files to be assigned a meaningful name on open.

Signed-off-by: Andrei Tatar <andrei@unikraft.io>
Approved-by: Sergiu Moga <sergiu@unikraft.io>
Reviewed-by: Sergiu Moga <sergiu@unikraft.io>
GitHub-Closes: #1592

lib/posix-fd/include/uk/posix-fd.h

index 6532f78522b9db2d9849b5d60545dc9d1b6d77ef..a2ead23051375358704039b1d5e1da0ff2a2ed2e 100644 (file)
@@ -24,8 +24,15 @@ struct uk_ofile {
        __atomic refcnt;
        size_t pos; /* Current file read/write offset position */
        struct uk_mutex lock; /* Lock for modifying open file state */
+       char name[]; /* Name of open file description; driver dependent */
+       /* Filesystem-backed files should be named after the path they were
+        * opened with. Pseudo-files should be named something descriptive.
+        */
 };
 
+/* Allocation size of a named uk_ofile with a name of length `namelen` */
+#define UKFD_OFILE_SIZE(namelen) (sizeof(struct uk_ofile) + (namelen) + 1)
+
 /**
  * Initialize an open file description with a refcount of 1.
  *
@@ -85,6 +92,24 @@ int uk_ofile_release(struct uk_ofile *of)
 /* File I/O should not use the file locks (e.g. if driver handles them) */
 #define UKFD_O_NOIOLOCK 020
 
+/* INTERNAL. Open file description has .name field allocated */
+#define UKFD_O_NAMED 040
+
+/**
+ * Retrieve the name of open file description `of`, or `fallback` if anonymous.
+ *
+ * @param of Open file description
+ * @param fallback Name to return if `of` is anonymous
+ *
+ * @return
+ *  Name of `of` (NUL-terminated string) or `fallback` if `of` is anonymous
+ */
+static inline
+const char *uk_ofile_name(const struct uk_ofile *of, const char *fallback)
+{
+       return (of->mode & UKFD_O_NAMED) ? of->name : fallback;
+}
+
 /* Event sets */
 #define UKFD_POLL_ALWAYS (EPOLLERR|EPOLLHUP)
 #define UKFD_POLLIN (EPOLLIN|EPOLLRDNORM|EPOLLRDBAND)