]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lsfd: (refactor) simplify the step to make a file struct
authorMasatake YAMATO <yamato@redhat.com>
Sun, 14 Jan 2024 21:40:37 +0000 (06:40 +0900)
committerMasatake YAMATO <yamato@redhat.com>
Fri, 2 Feb 2024 19:27:50 +0000 (04:27 +0900)
Signed-off-by: Masatake YAMATO <yamato@redhat.com>
misc-utils/lsfd.c

index 8c74d862b7b26563172a5489ae23c2c29b2442bd..f588022fb16fec0590b3fb3415386bbbe38a4e40 100644 (file)
@@ -683,7 +683,8 @@ static const struct file_class *stat2class(struct stat *sb)
        return &unkn_class;
 }
 
-static struct file *new_file(struct proc *proc, const struct file_class *class)
+static struct file *new_file(struct proc *proc, const struct file_class *class,
+                            struct stat *sb, const char *name, int association)
 {
        struct file *file;
 
@@ -696,6 +697,10 @@ static struct file *new_file(struct proc *proc, const struct file_class *class)
        INIT_LIST_HEAD(&file->files);
        list_add_tail(&file->files, &proc->files);
 
+       file->association = association;
+       file->name = xstrdup(name);
+       file->stat = *sb;
+
        return file;
 }
 
@@ -715,13 +720,6 @@ static struct file *copy_file(struct file *old, int new_association)
        return file;
 }
 
-static void file_set_path(struct file *file, struct stat *sb, const char *name, int association)
-{
-       file->association = association;
-       file->name = xstrdup(name);
-       file->stat = *sb;
-}
-
 static void file_init_content(struct file *file)
 {
        if (file->class && file->class->initialize_content)
@@ -825,8 +823,7 @@ static struct file *collect_file_symlink(struct path_cxt *pc,
                     */
                    && (class != &sock_class) && (class != &nsfs_file_class))
                        return NULL;
-               f = new_file(proc, class);
-               file_set_path(f, &sb, sym, assoc);
+               f = new_file(proc, class, &sb, sym, assoc);
        }
 
        file_init_content(f);
@@ -921,8 +918,7 @@ static void parse_maps_line(struct path_cxt *pc, char *buf, struct proc *proc)
                         * "stat by the file name" may not work. In that case,
                         */
                        goto try_map_files;
-               f = new_file(proc, stat2class(&sb));
-               file_set_path(f, &sb, path, -assoc);
+               f = new_file(proc, stat2class(&sb), &sb, path, -assoc);
        } else {
                /* As used in tcpdump, AF_PACKET socket can be mmap'ed. */
                char map_file[sizeof("map_files/0000000000000000-ffffffffffffffff")];
@@ -934,8 +930,7 @@ static void parse_maps_line(struct path_cxt *pc, char *buf, struct proc *proc)
                        return;
                if (ul_path_readlink(pc, sym, sizeof(sym), map_file) < 0)
                        return;
-               f = new_file(proc, stat2class(&sb));
-               file_set_path(f, &sb, sym, -assoc);
+               f = new_file(proc, stat2class(&sb), &sb, sym, -assoc);
        }
 
        if (modestr[0] == 'r')