diff --git a/src/config_rmw.c b/src/config_rmw.c index 26b67075..c5b80349 100644 --- a/src/config_rmw.c +++ b/src/config_rmw.c @@ -200,7 +200,7 @@ parse_line_waste(st_waste *waste_curr, struct Canfigger *node, } bool is_attached = - check_pathname_state(tmp_waste_parent_folder) == P_STATE_EXISTS; + (check_pathname_state(tmp_waste_parent_folder) == EEXIST); if (removable && !is_attached) { if (cli_user_options->list) @@ -247,7 +247,7 @@ parse_line_waste(st_waste *waste_curr, struct Canfigger *node, waste_curr->len_files = strlen(waste_curr->files); int p_state = check_pathname_state(waste_curr->files); - if (p_state == P_STATE_ENOENT) + if (p_state == ENOENT) { if (!rmw_mkdir(waste_curr->files)) msg_success_mkdir(waste_curr->files); @@ -257,13 +257,13 @@ parse_line_waste(st_waste *waste_curr, struct Canfigger *node, exit(errno); } } - else if (p_state == P_STATE_ERR) + else if (p_state == -1) exit(p_state); waste_curr->info = join_paths(waste_curr->parent, lit_info); waste_curr->len_info = strlen(waste_curr->info); - if ((p_state = check_pathname_state(waste_curr->info)) == P_STATE_ENOENT) + if ((p_state = check_pathname_state(waste_curr->info)) == ENOENT) { if (!rmw_mkdir(waste_curr->info)) msg_success_mkdir(waste_curr->info); @@ -273,7 +273,7 @@ parse_line_waste(st_waste *waste_curr, struct Canfigger *node, exit(errno); } } - else if (p_state == P_STATE_ERR) + else if (p_state == -1) exit(p_state); waste_curr->is_btrfs = is_btrfs(waste_curr->parent); diff --git a/src/config_rmw.h b/src/config_rmw.h index 4c8ad049..63ceb775 100644 --- a/src/config_rmw.h +++ b/src/config_rmw.h @@ -34,10 +34,10 @@ typedef struct { st_waste *st_waste_folder_props_head; // Pointer with high alignment requirements - int expire_age; // 4-byte alignment, placed next + int expire_age; // 4-byte alignment, placed next - bool force_required; // Minimal alignment, placed after the int - char uid[10]; // Character array, lower alignment, placed last + bool force_required; // Minimal alignment, placed after the int + char uid[10]; // Character array, lower alignment, placed last } st_config; diff --git a/src/main.c b/src/main.c index 2906d55b..2c055936 100644 --- a/src/main.c +++ b/src/main.c @@ -311,9 +311,9 @@ damage of 5000 hp. You feel satisfied.\n")); } int p_state = check_pathname_state(argv[file_arg]); - if (p_state != P_STATE_EXISTS) + if (p_state != EEXIST) { - if (p_state == P_STATE_ENOENT) + if (p_state == ENOENT) msg_warn_file_not_found(argv[file_arg]); continue; @@ -397,8 +397,7 @@ damage of 5000 hp. You feel satisfied.\n")); /* If a duplicate file exists */ if ((st_target.is_duplicate = - check_pathname_state(st_target.waste_dest_name)) == - P_STATE_EXISTS) + (check_pathname_state(st_target.waste_dest_name)) == EEXIST)) { // append a time string bufchk_len(strlen(st_target.waste_dest_name) + @@ -584,7 +583,7 @@ get_locations(const char *alt_config_file) } int p_state = check_pathname_state(x.config_dir); - if (p_state == P_STATE_ENOENT) + if (p_state == ENOENT) { if (!rmw_mkdir(x.config_dir)) msg_success_mkdir(x.config_dir); @@ -594,7 +593,7 @@ get_locations(const char *alt_config_file) exit(errno); } } - else if (p_state == P_STATE_ERR) + else if (p_state == -1) exit(p_state); static char s_config_file[PATH_MAX]; @@ -614,7 +613,7 @@ get_locations(const char *alt_config_file) if (verbose) printf("config_file: %s\n", x.config_file); - if ((p_state = check_pathname_state(x.config_file)) == P_STATE_ENOENT) + if ((p_state = check_pathname_state(x.config_file)) == ENOENT) { FILE *fp = fopen(x.config_file, "w"); if (fp) @@ -632,7 +631,7 @@ get_locations(const char *alt_config_file) exit(errno); } } - else if (p_state == P_STATE_ERR) + else if (p_state == -1) exit(p_state); static char s_mrl_file[PATH_MAX]; @@ -702,10 +701,10 @@ main(const int argc, char *const argv[]) } int p_state = 0; - if ((p_state = check_pathname_state(st_location->data_dir)) == P_STATE_ERR) + if ((p_state = check_pathname_state(st_location->data_dir)) == -1) exit(p_state); - bool init_data_dir = p_state == P_STATE_ENOENT; + bool init_data_dir = (p_state == ENOENT); if (init_data_dir) { diff --git a/src/main.h b/src/main.h index 7fa8e4ae..eb1d22bf 100644 --- a/src/main.h +++ b/src/main.h @@ -51,6 +51,5 @@ struct st_loc const char *purge_time_file; const char *mrl_file; - const st_dir *st_directory; // Pointer to another struct, placed at the end for clarity + const st_dir *st_directory; // Pointer to another struct, placed at the end for clarity }; - diff --git a/src/purging.c b/src/purging.c index 612e4338..e99726d8 100644 --- a/src/purging.c +++ b/src/purging.c @@ -128,7 +128,7 @@ do_file_purge(char *purge_target, const rmw_options *cli_user_options, const char *pt_basename, int *ctr) { int p_state = check_pathname_state(purge_target); - if (p_state == P_STATE_ENOENT) + if (p_state == ENOENT) { if (cli_user_options->want_orphan_chk && cli_user_options->force >= 2) { @@ -151,7 +151,7 @@ do_file_purge(char *purge_target, const rmw_options *cli_user_options, msg_warn_file_not_found(purge_target); } } - else if (p_state == P_STATE_ERR) + else if (p_state == -1) exit(p_state); bool is_dir = is_dir_f(purge_target); @@ -307,8 +307,7 @@ purge(st_config *st_config_data, || cli_user_options->want_empty_trash; if (want_purge || verbose >= 2) { - char purge_target[PATH_MAX]; - *purge_target = '\0'; + char purge_target[PATH_MAX] = { 0 }; get_purge_target(purge_target, st_trashinfo_dir_entry->d_name, waste_curr->files); char *pt_basename = get_pt_basename(purge_target); @@ -380,7 +379,7 @@ orphan_maint(st_waste *waste_head, st_time *st_time_var, int *orphan_ctr) free(tmp_str); - if (check_pathname_state(path_to_trashinfo) == P_STATE_EXISTS) + if (check_pathname_state(path_to_trashinfo) == EEXIST) continue; /* destination if restored */ diff --git a/src/restore.c b/src/restore.c index 7a87efc1..3efd91f9 100644 --- a/src/restore.c +++ b/src/restore.c @@ -69,11 +69,10 @@ restore(const char *src, st_time *st_time_var, const rmw_options *cli_user_options, st_waste *waste_head) { int p_state = check_pathname_state(src); - if (p_state == P_STATE_EXISTS) + if (p_state == EEXIST) { bufchk_len(strlen(src) + 1, PATH_MAX, __func__, __LINE__); - char waste_parent[PATH_MAX]; - *waste_parent = '\0'; + char waste_parent[PATH_MAX] = { 0 }; get_waste_parent(waste_parent, src); st_waste *waste_curr = waste_head; @@ -134,7 +133,7 @@ restore(const char *src, st_time *st_time_var, /* Check for duplicate filename */ - if (check_pathname_state(dest) == P_STATE_EXISTS) + if (check_pathname_state(dest) == EEXIST) { bufchk_len(strlen(dest) + LEN_MAX_TIME_STR_SUFFIX, PATH_MAX, __func__, __LINE__); @@ -154,7 +153,7 @@ Duplicate filename at destination - appending time string...\n")); if (cli_user_options->want_dry_run == false) { int p_state_parent = check_pathname_state(parent_dir); - if (p_state_parent == P_STATE_ENOENT) + if (p_state_parent == ENOENT) { if (!rmw_mkdir(parent_dir)) { @@ -168,7 +167,7 @@ Duplicate filename at destination - appending time string...\n")); msg_err_mkdir(waste_curr->files, __func__); } } - else if (p_state_parent == P_STATE_ERR) + else if (p_state_parent == -1) return p_state_parent; } @@ -205,7 +204,7 @@ Duplicate filename at destination - appending time string...\n")); } else { - if (p_state == P_STATE_ENOENT) + if (p_state == ENOENT) { print_msg_warn(); msg_warn_file_not_found(src); @@ -565,8 +564,6 @@ test_create_file_details_str(void) int i = 0; while (i < data_size) { - //char file_details[LEN_MAX_FILE_DETAILS]; - //*file_details = '\0'; char *file_details = create_file_details_str(data[i].size, data[i].mode); fprintf(stderr, "file_details: %s\nExpected: %s\n\n", file_details, data[i].out); diff --git a/src/time_rmw.h b/src/time_rmw.h index 9de4fc4b..bf512ffd 100644 --- a/src/time_rmw.h +++ b/src/time_rmw.h @@ -42,8 +42,8 @@ along with this program. If not, see . */ typedef struct { - time_t now; // Largest member, placed first to avoid alignment padding issues. - char deletion_date[LEN_MAX_DELETION_DATE]; // Grouped char arrays together. + time_t now; // Largest member, placed first to avoid alignment padding issues. + char deletion_date[LEN_MAX_DELETION_DATE]; // Grouped char arrays together. char t_fmt[LEN_MAX_DELETION_DATE]; char suffix_added_dup_exists[LEN_MAX_TIME_STR_SUFFIX]; } st_time; diff --git a/src/utils.c b/src/utils.c index 0872e14f..0167b327 100644 --- a/src/utils.c +++ b/src/utils.c @@ -111,11 +111,8 @@ rmw_mkdir(const char *dir) { if (!dir) return -1; - if (check_pathname_state(dir) == P_STATE_EXISTS) - { - errno = EEXIST; + if (check_pathname_state(dir) == EEXIST) return -1; - } int res = 0; @@ -125,9 +122,9 @@ rmw_mkdir(const char *dir) if (!parent) return -1; int p_state = check_pathname_state(parent); - if (p_state == P_STATE_ENOENT) + if (p_state == ENOENT) res = rmw_mkdir(parent); - else if (p_state == P_STATE_ERR) + else if (p_state == -1) exit(p_state); if (res) @@ -144,38 +141,22 @@ int check_pathname_state(const char *pathname) { if (!pathname) - return false; + return -1; - int fd = open(pathname, O_RDONLY); + int fd = open(pathname, O_RDONLY | O_NOFOLLOW); if (fd != -1) { - if (close(fd) == -1) - { - perror("close"); - errno = 0; - } - return P_STATE_EXISTS; + close(fd); + return EEXIST; } - // open() returns ENOENT in the case of dangling symbolic links - // check if it's a link or not: - if (errno == ENOENT) - { - static char buf[1]; - ssize_t f = readlink(pathname, buf, 1); - *buf = '\0'; - if (f != -1) - return P_STATE_EXISTS; - else - { - errno = 0; - return P_STATE_ENOENT; - } - } + if (errno == ELOOP) + return EEXIST; + else if (errno == ENOENT) + return ENOENT; - printf("open %s: %s\n", pathname, strerror(errno)); - errno = 0; - return P_STATE_ERR; + fprintf(stderr, "open %s: %s\n", pathname, strerror(errno)); + return -1; } void @@ -533,12 +514,12 @@ test_rmw_mkdir(const char *h) { const char *subdirs = "foo/bar/21/42"; char *dir = join_paths(h, subdirs); - if (check_pathname_state(dir) == P_STATE_EXISTS) + if (check_pathname_state(dir) == EEXIST) assert(bsdutils_rm(dir, verbose) == 0); assert(rmw_mkdir(dir) == 0); assert(dir); printf("%s\n", dir); - assert(check_pathname_state(dir) == P_STATE_EXISTS); + assert(check_pathname_state(dir) == EEXIST); assert(bsdutils_rm(dir, verbose) == 0); free(dir); @@ -691,30 +672,30 @@ test_check_pathname_state(const char *const homedir) FILE *fp = fopen(foobar, "w"); assert(fp != NULL); assert(fclose(fp) != EOF); - assert(check_pathname_state(foobar) == P_STATE_EXISTS); + assert(check_pathname_state(foobar) == EEXIST); char *snafu = join_paths(homedir, "snafu"); assert(symlink(foobar, snafu) == 0); - assert(check_pathname_state(snafu) == P_STATE_EXISTS); + assert(check_pathname_state(snafu) == EEXIST); assert(remove(foobar) == 0); free(foobar); assert(remove(snafu) == 0); free(snafu); char *home_link = join_paths(homedir, "home_1234"); - assert(check_pathname_state(homedir) == P_STATE_EXISTS); - if (check_pathname_state(home_link) == P_STATE_EXISTS) + assert(check_pathname_state(homedir) == EEXIST); + if (check_pathname_state(home_link) == EEXIST) assert(remove(home_link) == 0); assert(symlink(homedir, home_link) == 0); - assert(check_pathname_state(home_link) == P_STATE_EXISTS); + assert(check_pathname_state(home_link) == EEXIST); assert(remove(home_link) == 0); free(home_link); const char *dlink = "dangling_link"; - if (check_pathname_state(dlink) == P_STATE_EXISTS) + if (check_pathname_state(dlink) == EEXIST) assert(remove(dlink) == 0); assert(symlink("dangler", dlink) == 0); - assert(check_pathname_state(dlink) == P_STATE_EXISTS); + assert(check_pathname_state(dlink) == EEXIST); assert(remove(dlink) == 0); return; diff --git a/src/utils.h b/src/utils.h index ce557b80..947a725a 100644 --- a/src/utils.h +++ b/src/utils.h @@ -27,13 +27,6 @@ along with this program. If not, see . #include "trashinfo.h" -enum -{ - P_STATE_ERR = -1, - P_STATE_ENOENT, - P_STATE_EXISTS -}; - #define join_paths(...) real_join_paths(__VA_ARGS__, NULL) #define LEN_MAX_HUMAN_READABLE_SIZE (sizeof "xxxx.y GiB")