From 00fbf739ff8e497128ea6664e501293fa13eb7a3 Mon Sep 17 00:00:00 2001 From: Masatake YAMATO Date: Sun, 12 Oct 2025 15:58:59 +0900 Subject: [PATCH] lsfd: (refactor) add a helper function making error file objects 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 --- lsfd-cmd/file.c | 12 ++++++------ lsfd-cmd/lsfd.c | 37 +++++++++++++++++-------------------- 2 files changed, 23 insertions(+), 26 deletions(-) diff --git a/lsfd-cmd/file.c b/lsfd-cmd/file.c index 34d902979..a28be3f37 100644 --- a/lsfd-cmd/file.c +++ b/lsfd-cmd/file.c @@ -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, }; /* diff --git a/lsfd-cmd/lsfd.c b/lsfd-cmd/lsfd.c index 7c2fa8dee..bf631ee9b 100644 --- a/lsfd-cmd/lsfd.c +++ b/lsfd-cmd/lsfd.c @@ -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; } -- 2.47.3