]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lsfd: add 'D' flag for representing deleted files to XMODE column
authorMasatake YAMATO <yamato@redhat.com>
Sun, 11 Jun 2023 07:36:03 +0000 (16:36 +0900)
committerMasatake YAMATO <yamato@redhat.com>
Thu, 22 Jun 2023 00:01:13 +0000 (09:01 +0900)
Now NAME column doesn't print "(deleted)" markers at the end of
file name.

Signed-off-by: Masatake YAMATO <yamato@redhat.com>
misc-utils/lsfd-file.c
misc-utils/lsfd.1.adoc
tests/expected/lsfd/column-xmode-XMODE-D-bit [new file with mode: 0644]
tests/expected/lsfd/column-xmode-XMODE-r-bit
tests/expected/lsfd/column-xmode-XMODE-w-bit
tests/expected/lsfd/column-xmode-XMODE-x-bit
tests/ts/lsfd/column-xmode

index 0a4af458c0fd50dba52e1597a2db5748ee2a7b17..029cc81fc880a1058f4586cf4d453019a001d37f 100644 (file)
@@ -146,6 +146,20 @@ void decode_source(char *buf, size_t bufsize,
                         dev_minor);
 }
 
+static char *strnrstr(const char *haystack, const char *needle, size_t needle_len)
+{
+       char *last = strstr(haystack, needle);
+       if (last == NULL)
+               return NULL;
+
+       do {
+               char *current = strstr(last + needle_len, needle);
+               if (current == NULL)
+                       return last;
+               last = current;
+       } while (1);
+}
+
 static bool file_fill_column(struct proc *proc,
                             struct file *file,
                             struct libscols_line *ln,
@@ -162,8 +176,22 @@ static bool file_fill_column(struct proc *proc,
                    && scols_line_set_data(ln, column_index, proc->command))
                        err(EXIT_FAILURE, _("failed to add output data"));
                return true;
-       case COL_KNAME:
        case COL_NAME:
+               if (file->name && file->stat.st_nlink == 0) {
+                       char *d = strnrstr(file->name, "(deleted)",
+                                          sizeof("(deleted)") - 1);
+                       if (d) {
+                               int r;
+                               *d = '\0';
+                               r = scols_line_set_data(ln, column_index, file->name);
+                               *d = '(';
+                               if (r)
+                                       err(EXIT_FAILURE, _("failed to add output data"));
+                               return true;
+                       }
+               }
+               /* FALL THROUGH */
+       case COL_KNAME:
                if (file->name
                    && scols_line_set_data(ln, column_index, file->name))
                        err(EXIT_FAILURE, _("failed to add output data"));
@@ -270,6 +298,7 @@ static bool file_fill_column(struct proc *proc,
                break;
        case COL_XMODE: {
                char r, w, x;
+               char D = file->stat.st_nlink == 0? 'D': '-';
                if (does_file_has_fdinfo_alike(file)) {
                        r = file->mode & S_IRUSR? 'r': '-';
                        w = file->mode & S_IWUSR? 'w': '-';
@@ -277,7 +306,7 @@ static bool file_fill_column(struct proc *proc,
                             && file->mode & S_IXUSR)? 'x': '-';
                } else
                        r = w = x = '-';
-               xasprintf(&str, "%c%c%c", r, w, x);
+               xasprintf(&str, "%c%c%c%c", r, w, x, D);
                break;
        }
        case COL_POS:
index a72eae5626cfbaeec115ff8c045bd0bac0591cc9..76e606f28733b56c25ed888b56eff4d798435bac 100644 (file)
@@ -235,6 +235,13 @@ Access mode (rwx).
 
 NAME <``string``>::
 Cooked version of KNAME. It is mostly same as KNAME.
+
+____
+Note that `(deleted)` markers are removed from this column.
+Refer to _KNAME_, _DELETED_, or _XMODE_ to know the
+readability of the file from the file system.
+____
+
 +
 Some files have special formats and information sources:
 +
@@ -465,11 +472,21 @@ USER <``string``>::
 User of the process.
 
 XMODE <``string``>::
-Extended version of _MODE_.
-Currently this column shows the same items as _MODE_.
-However, the column may grow; new letters may be added to
-_XMODE_ when *lsfd* supports a new state of file descriptors
+Extended version of _MODE_. This column may grow; new letters may be
+added to _XMODE_ when *lsfd* supports a new state of file descriptors
 and/or memory mappings.
++
+[-r]:::
+opened of mapped for reading. This is also in _MODE_.
++
+[-w]:::
+opened of mapped for writing. This is also in _MODE_.
++
+[-x]:::
+mapped for executing the code. This is also in _MODE_.
++
+[-D]:::
+opened file is deleted from  the file system. See also _DELETED._.
 
 == FILTER EXPRESSION
 
diff --git a/tests/expected/lsfd/column-xmode-XMODE-D-bit b/tests/expected/lsfd/column-xmode-XMODE-D-bit
new file mode 100644 (file)
index 0000000..906370d
--- /dev/null
@@ -0,0 +1,2 @@
+ -w-D
+XMODE(D-bit):  0
index b0e191e31f2adf8df9fd54405a0eb26671ec576a..dfbdf4b5db3fb934a37094b134f2a1fb6ea3a97a 100644 (file)
@@ -1,2 +1,2 @@
 r--
r---
 XMODE(r-bit):  0
index 4318488fda011f38a5c5c43c39617dc1c5ee606a..90c2665b76aa6b9d905920239e7e542eb3b70eac 100644 (file)
@@ -1,2 +1,2 @@
 -w-
-w--
 XMODE(w-bit):  0
index 1e9751574efea242e83418ea4723ff5f057eec29..f8560693304c713f2ca1f53cd3bdb75ef0c708f2 100644 (file)
@@ -1,2 +1,2 @@
-  r-x
+ r-x-
 XMODE(x-bit):  0
index 8e0bc67a5f2c6c328e5a45bd45b6632d779bf12f..d9edd59849e1eb58de232830fb787b112989bffe 100755 (executable)
@@ -115,4 +115,16 @@ else
     ts_finalize_subtest
 fi
 
+EXPR='(FD == '"$FD"')'
+ts_init_subtest "XMODE-D-bit"
+{
+    coproc MKFDS { "$TS_HELPER_MKFDS" make-regular-file $FD delete=1; }
+    if read -r -u "${MKFDS[0]}" PID; then
+       "${TS_CMD_LSFD}" -n -o XMODE -p "${PID}" -Q "${EXPR}"
+       echo "XMODE(D-bit): " $?
+       kill -CONT "${PID}"
+    fi
+} > "$TS_OUTPUT" 2>&1
+ts_finalize_subtest
+
 ts_finalize