Skip to content

Commit

Permalink
maint: Init arrays with { 0 }; cleanup check_pathname_state()
Browse files Browse the repository at this point in the history
  • Loading branch information
andy5995 committed Nov 13, 2024
1 parent 1d1c704 commit ddd6e42
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 84 deletions.
10 changes: 5 additions & 5 deletions src/config_rmw.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions src/config_rmw.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;


Expand Down
19 changes: 9 additions & 10 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) +
Expand Down Expand Up @@ -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);
Expand All @@ -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];
Expand All @@ -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)
Expand All @@ -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];
Expand Down Expand Up @@ -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)
{
Expand Down
3 changes: 1 addition & 2 deletions src/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
};

9 changes: 4 additions & 5 deletions src/purging.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 */
Expand Down
15 changes: 6 additions & 9 deletions src/restore.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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__);
Expand All @@ -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))
{
Expand All @@ -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;
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/time_rmw.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
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;
Expand Down
63 changes: 22 additions & 41 deletions src/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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;
Expand Down
7 changes: 0 additions & 7 deletions src/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.

#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")
Expand Down

0 comments on commit ddd6e42

Please sign in to comment.