From: Masatake YAMATO Date: Sun, 11 Jun 2023 07:36:03 +0000 (+0900) Subject: lsfd: add 'D' flag for representing deleted files to XMODE column X-Git-Tag: v2.40-rc1~351^2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=30ac13fa20e7433f290b76b1e5f384eac0b6a86d;p=thirdparty%2Futil-linux.git lsfd: add 'D' flag for representing deleted files to XMODE column Now NAME column doesn't print "(deleted)" markers at the end of file name. Signed-off-by: Masatake YAMATO --- diff --git a/misc-utils/lsfd-file.c b/misc-utils/lsfd-file.c index 0a4af458c0..029cc81fc8 100644 --- a/misc-utils/lsfd-file.c +++ b/misc-utils/lsfd-file.c @@ -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: diff --git a/misc-utils/lsfd.1.adoc b/misc-utils/lsfd.1.adoc index a72eae5626..76e606f287 100644 --- a/misc-utils/lsfd.1.adoc +++ b/misc-utils/lsfd.1.adoc @@ -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 index 0000000000..906370d0cf --- /dev/null +++ b/tests/expected/lsfd/column-xmode-XMODE-D-bit @@ -0,0 +1,2 @@ + -w-D +XMODE(D-bit): 0 diff --git a/tests/expected/lsfd/column-xmode-XMODE-r-bit b/tests/expected/lsfd/column-xmode-XMODE-r-bit index b0e191e31f..dfbdf4b5db 100644 --- a/tests/expected/lsfd/column-xmode-XMODE-r-bit +++ b/tests/expected/lsfd/column-xmode-XMODE-r-bit @@ -1,2 +1,2 @@ - r-- + r--- XMODE(r-bit): 0 diff --git a/tests/expected/lsfd/column-xmode-XMODE-w-bit b/tests/expected/lsfd/column-xmode-XMODE-w-bit index 4318488fda..90c2665b76 100644 --- a/tests/expected/lsfd/column-xmode-XMODE-w-bit +++ b/tests/expected/lsfd/column-xmode-XMODE-w-bit @@ -1,2 +1,2 @@ - -w- + -w-- XMODE(w-bit): 0 diff --git a/tests/expected/lsfd/column-xmode-XMODE-x-bit b/tests/expected/lsfd/column-xmode-XMODE-x-bit index 1e9751574e..f856069330 100644 --- a/tests/expected/lsfd/column-xmode-XMODE-x-bit +++ b/tests/expected/lsfd/column-xmode-XMODE-x-bit @@ -1,2 +1,2 @@ - r-x + r-x- XMODE(x-bit): 0 diff --git a/tests/ts/lsfd/column-xmode b/tests/ts/lsfd/column-xmode index 8e0bc67a5f..d9edd59849 100755 --- a/tests/ts/lsfd/column-xmode +++ b/tests/ts/lsfd/column-xmode @@ -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