From: Karel Zak Date: Thu, 30 Sep 2021 09:11:30 +0000 (+0200) Subject: lsfd: optimize symlinks use X-Git-Tag: v2.38-rc1~144^2~82 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=b813e8ce8f0375ce7f8b3f0deb8f51f8af5d333f;p=thirdparty%2Futil-linux.git lsfd: optimize symlinks use Like the previous commit, this patch tries to reuse /proc/#/{fd,ns,cwd,root} symlinks if possible. On my machine it saves ~2000 stat() calls. Signed-off-by: Karel Zak --- diff --git a/misc-utils/lsfd.c b/misc-utils/lsfd.c index 5c4cb17c4c..f855f637cb 100644 --- a/misc-utils/lsfd.c +++ b/misc-utils/lsfd.c @@ -351,16 +351,27 @@ static struct file *collect_file_symlink(struct path_cxt *pc, { char sym[PATH_MAX] = { '\0' }; struct stat sb; - struct file *f; + struct file *f, *prev; - if (ul_path_stat(pc, &sb, 0, name) < 0) - return NULL; if (ul_path_readlink(pc, sym, sizeof(sym), name) < 0) return NULL; - f = new_file(proc, stat2class(&sb)); + /* The /proc/#/{fd,ns} often contains the same file (e.g. /dev/tty) + * more than once. Let's try to reuse the previous file if the real + * path is the same to save stat() call. + */ + prev = list_last_entry(&proc->files, struct file, files); + if (prev && prev->name && strcmp(prev->name, sym) == 0) { + f = copy_file(prev); + f->association = assoc; + } else { + if (ul_path_stat(pc, &sb, 0, name) < 0) + return NULL; + + f = new_file(proc, stat2class(&sb)); + file_set_path(f, &sb, sym, assoc); + } - file_set_path(f, &sb, sym, assoc); file_init_content(f); if (is_association(f, EXE))