]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lsfd: (refactor) add a helper function making error file objects
authorMasatake YAMATO <yamato@redhat.com>
Sun, 12 Oct 2025 06:58:59 +0000 (15:58 +0900)
committerMasatake YAMATO <yamato@redhat.com>
Mon, 19 Jan 2026 20:56:08 +0000 (05:56 +0900)
new_error_file_common() is the helper function.  new_stat_error_file()
and new_stat_error_file() use the function.

Signed-off-by: Masatake YAMATO <yamato@redhat.com>
lsfd-cmd/file.c
lsfd-cmd/lsfd.c

index 34d902979a549de29db50224aeb2552575de7961..a28be3f373e7fb00a0cf09a967cb78583e3f2a3b 100644 (file)
@@ -286,9 +286,15 @@ static bool error_fill_column(struct proc *proc __attribute__((__unused__)),
        }
 }
 
+static void error_file_free_content(struct file *file)
+{
+       free(file->name);       /* NULL is acceptable.  */
+}
+
 static const struct file_class error_class = {
        .super = &abst_class,
        .size = sizeof(struct file),
+       .free_content = error_file_free_content,
        .fill_column = error_fill_column,
 };
 
@@ -319,16 +325,10 @@ const struct file_class readlink_error_class = {
        .fill_column = readlink_error_fill_column,
 };
 
-static void stat_error_file_free_content(struct file *file)
-{
-       free(file->name);
-}
-
 const struct file_class stat_error_class = {
        .super = &error_class,
        .size = sizeof(struct file),
        .initialize_content = init_error_content,
-       .free_content = stat_error_file_free_content,
 };
 
 /*
index 7c2fa8deec2ca3f7bbb406db8250e37fc3989670..bf631ee9b2ad20c70c897eec9e1a06567ad31656 100644 (file)
@@ -757,43 +757,40 @@ static struct file *new_file(struct proc *proc, const struct file_class *class,
        return file;
 }
 
-static struct file *new_readlink_error_file(struct proc *proc, int error_no, int association)
+static struct file *new_error_file_common(const struct file_class *error_class, const char *syscall,
+                                         struct proc *proc, int error_no, int association,
+                                         const char *name)
 {
        struct file *file;
 
-       file = xcalloc(1, readlink_error_class.size);
-       file->class = &readlink_error_class;
+       file = xcalloc(1, error_class->size);
+       file->class = error_class;
 
        file->proc = proc;
 
        INIT_LIST_HEAD(&file->files);
        list_add_tail(&file->files, &proc->files);
 
-       file->error.syscall = "readlink";
+       file->error.syscall = syscall;
        file->error.number = error_no;
        file->association = association;
-       file->name = NULL;
+       file->name = name ? xstrdup(name) : NULL;
 
        return file;
 }
 
-static struct file *new_stat_error_file(struct proc *proc, const char *name, int error_no, int association)
+static struct file *new_readlink_error_file(struct proc *proc, int error_no, int association)
 {
-       struct file *file;
-
-       file = xcalloc(1, stat_error_class.size);
-       file->class = &stat_error_class;
-
-       file->proc = proc;
-
-       INIT_LIST_HEAD(&file->files);
-       list_add_tail(&file->files, &proc->files);
-
-       file->error.syscall = "stat";
-       file->error.number = error_no;
-       file->association = association;
-       file->name = xstrdup(name);
+       return new_error_file_common(&readlink_error_class, "readlink",
+                                    proc, error_no, association,
+                                    NULL);
+}
 
+static struct file *new_stat_error_file(struct proc *proc, const char *name, int error_no, int association)
+{
+       struct file *file = new_error_file_common(&stat_error_class, "stat",
+                                                 proc, error_no, association,
+                                                 name);
        return file;
 }