Skip to content

Commit e82435c

Browse files
committed
maint: Init arrays with { 0 }; cleanup check_pathname_state()
1 parent 1d1c704 commit e82435c

9 files changed

+59
-84
lines changed

src/config_rmw.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ parse_line_waste(st_waste *waste_curr, struct Canfigger *node,
200200
}
201201

202202
bool is_attached =
203-
check_pathname_state(tmp_waste_parent_folder) == P_STATE_EXISTS;
203+
(check_pathname_state(tmp_waste_parent_folder) == EEXIST);
204204
if (removable && !is_attached)
205205
{
206206
if (cli_user_options->list)
@@ -247,7 +247,7 @@ parse_line_waste(st_waste *waste_curr, struct Canfigger *node,
247247
waste_curr->len_files = strlen(waste_curr->files);
248248

249249
int p_state = check_pathname_state(waste_curr->files);
250-
if (p_state == P_STATE_ENOENT)
250+
if (p_state == ENOENT)
251251
{
252252
if (!rmw_mkdir(waste_curr->files))
253253
msg_success_mkdir(waste_curr->files);
@@ -257,13 +257,13 @@ parse_line_waste(st_waste *waste_curr, struct Canfigger *node,
257257
exit(errno);
258258
}
259259
}
260-
else if (p_state == P_STATE_ERR)
260+
else if (p_state == -1)
261261
exit(p_state);
262262

263263
waste_curr->info = join_paths(waste_curr->parent, lit_info);
264264
waste_curr->len_info = strlen(waste_curr->info);
265265

266-
if ((p_state = check_pathname_state(waste_curr->info)) == P_STATE_ENOENT)
266+
if ((p_state = check_pathname_state(waste_curr->info)) == ENOENT)
267267
{
268268
if (!rmw_mkdir(waste_curr->info))
269269
msg_success_mkdir(waste_curr->info);
@@ -273,7 +273,7 @@ parse_line_waste(st_waste *waste_curr, struct Canfigger *node,
273273
exit(errno);
274274
}
275275
}
276-
else if (p_state == P_STATE_ERR)
276+
else if (p_state == -1)
277277
exit(p_state);
278278

279279
waste_curr->is_btrfs = is_btrfs(waste_curr->parent);

src/config_rmw.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ typedef struct
3434
{
3535
st_waste *st_waste_folder_props_head; // Pointer with high alignment requirements
3636

37-
int expire_age; // 4-byte alignment, placed next
37+
int expire_age; // 4-byte alignment, placed next
3838

39-
bool force_required; // Minimal alignment, placed after the int
40-
char uid[10]; // Character array, lower alignment, placed last
39+
bool force_required; // Minimal alignment, placed after the int
40+
char uid[10]; // Character array, lower alignment, placed last
4141
} st_config;
4242

4343

src/main.c

+9-10
Original file line numberDiff line numberDiff line change
@@ -311,9 +311,9 @@ damage of 5000 hp. You feel satisfied.\n"));
311311
}
312312

313313
int p_state = check_pathname_state(argv[file_arg]);
314-
if (p_state != P_STATE_EXISTS)
314+
if (p_state != EEXIST)
315315
{
316-
if (p_state == P_STATE_ENOENT)
316+
if (p_state == ENOENT)
317317
msg_warn_file_not_found(argv[file_arg]);
318318

319319
continue;
@@ -397,8 +397,7 @@ damage of 5000 hp. You feel satisfied.\n"));
397397
/* If a duplicate file exists
398398
*/
399399
if ((st_target.is_duplicate =
400-
check_pathname_state(st_target.waste_dest_name)) ==
401-
P_STATE_EXISTS)
400+
(check_pathname_state(st_target.waste_dest_name)) == EEXIST))
402401
{
403402
// append a time string
404403
bufchk_len(strlen(st_target.waste_dest_name) +
@@ -584,7 +583,7 @@ get_locations(const char *alt_config_file)
584583
}
585584

586585
int p_state = check_pathname_state(x.config_dir);
587-
if (p_state == P_STATE_ENOENT)
586+
if (p_state == ENOENT)
588587
{
589588
if (!rmw_mkdir(x.config_dir))
590589
msg_success_mkdir(x.config_dir);
@@ -594,7 +593,7 @@ get_locations(const char *alt_config_file)
594593
exit(errno);
595594
}
596595
}
597-
else if (p_state == P_STATE_ERR)
596+
else if (p_state == -1)
598597
exit(p_state);
599598

600599
static char s_config_file[PATH_MAX];
@@ -614,7 +613,7 @@ get_locations(const char *alt_config_file)
614613
if (verbose)
615614
printf("config_file: %s\n", x.config_file);
616615

617-
if ((p_state = check_pathname_state(x.config_file)) == P_STATE_ENOENT)
616+
if ((p_state = check_pathname_state(x.config_file)) == ENOENT)
618617
{
619618
FILE *fp = fopen(x.config_file, "w");
620619
if (fp)
@@ -632,7 +631,7 @@ get_locations(const char *alt_config_file)
632631
exit(errno);
633632
}
634633
}
635-
else if (p_state == P_STATE_ERR)
634+
else if (p_state == -1)
636635
exit(p_state);
637636

638637
static char s_mrl_file[PATH_MAX];
@@ -702,10 +701,10 @@ main(const int argc, char *const argv[])
702701
}
703702

704703
int p_state = 0;
705-
if ((p_state = check_pathname_state(st_location->data_dir)) == P_STATE_ERR)
704+
if ((p_state = check_pathname_state(st_location->data_dir)) == -1)
706705
exit(p_state);
707706

708-
bool init_data_dir = p_state == P_STATE_ENOENT;
707+
bool init_data_dir = (p_state == ENOENT);
709708

710709
if (init_data_dir)
711710
{

src/main.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,5 @@ struct st_loc
5151
const char *purge_time_file;
5252
const char *mrl_file;
5353

54-
const st_dir *st_directory; // Pointer to another struct, placed at the end for clarity
54+
const st_dir *st_directory; // Pointer to another struct, placed at the end for clarity
5555
};
56-

src/purging.c

+4-5
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ do_file_purge(char *purge_target, const rmw_options *cli_user_options,
128128
const char *pt_basename, int *ctr)
129129
{
130130
int p_state = check_pathname_state(purge_target);
131-
if (p_state == P_STATE_ENOENT)
131+
if (p_state == ENOENT)
132132
{
133133
if (cli_user_options->want_orphan_chk && cli_user_options->force >= 2)
134134
{
@@ -151,7 +151,7 @@ do_file_purge(char *purge_target, const rmw_options *cli_user_options,
151151
msg_warn_file_not_found(purge_target);
152152
}
153153
}
154-
else if (p_state == P_STATE_ERR)
154+
else if (p_state == -1)
155155
exit(p_state);
156156

157157
bool is_dir = is_dir_f(purge_target);
@@ -307,8 +307,7 @@ purge(st_config *st_config_data,
307307
|| cli_user_options->want_empty_trash;
308308
if (want_purge || verbose >= 2)
309309
{
310-
char purge_target[PATH_MAX];
311-
*purge_target = '\0';
310+
char purge_target[PATH_MAX] = { 0 };
312311
get_purge_target(purge_target, st_trashinfo_dir_entry->d_name,
313312
waste_curr->files);
314313
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)
380379

381380
free(tmp_str);
382381

383-
if (check_pathname_state(path_to_trashinfo) == P_STATE_EXISTS)
382+
if (check_pathname_state(path_to_trashinfo) == EEXIST)
384383
continue;
385384

386385
/* destination if restored */

src/restore.c

+6-9
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,10 @@ restore(const char *src, st_time *st_time_var,
6969
const rmw_options *cli_user_options, st_waste *waste_head)
7070
{
7171
int p_state = check_pathname_state(src);
72-
if (p_state == P_STATE_EXISTS)
72+
if (p_state == EEXIST)
7373
{
7474
bufchk_len(strlen(src) + 1, PATH_MAX, __func__, __LINE__);
75-
char waste_parent[PATH_MAX];
76-
*waste_parent = '\0';
75+
char waste_parent[PATH_MAX] = { 0 };
7776
get_waste_parent(waste_parent, src);
7877

7978
st_waste *waste_curr = waste_head;
@@ -134,7 +133,7 @@ restore(const char *src, st_time *st_time_var,
134133

135134
/* Check for duplicate filename
136135
*/
137-
if (check_pathname_state(dest) == P_STATE_EXISTS)
136+
if (check_pathname_state(dest) == EEXIST)
138137
{
139138
bufchk_len(strlen(dest) + LEN_MAX_TIME_STR_SUFFIX, PATH_MAX,
140139
__func__, __LINE__);
@@ -154,7 +153,7 @@ Duplicate filename at destination - appending time string...\n"));
154153
if (cli_user_options->want_dry_run == false)
155154
{
156155
int p_state_parent = check_pathname_state(parent_dir);
157-
if (p_state_parent == P_STATE_ENOENT)
156+
if (p_state_parent == ENOENT)
158157
{
159158
if (!rmw_mkdir(parent_dir))
160159
{
@@ -168,7 +167,7 @@ Duplicate filename at destination - appending time string...\n"));
168167
msg_err_mkdir(waste_curr->files, __func__);
169168
}
170169
}
171-
else if (p_state_parent == P_STATE_ERR)
170+
else if (p_state_parent == -1)
172171
return p_state_parent;
173172
}
174173

@@ -205,7 +204,7 @@ Duplicate filename at destination - appending time string...\n"));
205204
}
206205
else
207206
{
208-
if (p_state == P_STATE_ENOENT)
207+
if (p_state == ENOENT)
209208
{
210209
print_msg_warn();
211210
msg_warn_file_not_found(src);
@@ -565,8 +564,6 @@ test_create_file_details_str(void)
565564
int i = 0;
566565
while (i < data_size)
567566
{
568-
//char file_details[LEN_MAX_FILE_DETAILS];
569-
//*file_details = '\0';
570567
char *file_details = create_file_details_str(data[i].size, data[i].mode);
571568
fprintf(stderr, "file_details: %s\nExpected: %s\n\n", file_details,
572569
data[i].out);

src/time_rmw.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
4242
*/
4343
typedef struct
4444
{
45-
time_t now; // Largest member, placed first to avoid alignment padding issues.
46-
char deletion_date[LEN_MAX_DELETION_DATE]; // Grouped char arrays together.
45+
time_t now; // Largest member, placed first to avoid alignment padding issues.
46+
char deletion_date[LEN_MAX_DELETION_DATE]; // Grouped char arrays together.
4747
char t_fmt[LEN_MAX_DELETION_DATE];
4848
char suffix_added_dup_exists[LEN_MAX_TIME_STR_SUFFIX];
4949
} st_time;

src/utils.c

+29-41
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,8 @@ rmw_mkdir(const char *dir)
111111
{
112112
if (!dir)
113113
return -1;
114-
if (check_pathname_state(dir) == P_STATE_EXISTS)
115-
{
116-
errno = EEXIST;
114+
if (check_pathname_state(dir) == EEXIST)
117115
return -1;
118-
}
119116

120117
int res = 0;
121118

@@ -125,9 +122,9 @@ rmw_mkdir(const char *dir)
125122
if (!parent)
126123
return -1;
127124
int p_state = check_pathname_state(parent);
128-
if (p_state == P_STATE_ENOENT)
125+
if (p_state == ENOENT)
129126
res = rmw_mkdir(parent);
130-
else if (p_state == P_STATE_ERR)
127+
else if (p_state == -1)
131128
exit(p_state);
132129

133130
if (res)
@@ -144,38 +141,29 @@ int
144141
check_pathname_state(const char *pathname)
145142
{
146143
if (!pathname)
147-
return false;
144+
return -1;
148145

149-
int fd = open(pathname, O_RDONLY);
146+
int fd = open(pathname, O_RDONLY | O_NOFOLLOW);
150147
if (fd != -1)
151148
{
152-
if (close(fd) == -1)
153-
{
154-
perror("close");
155-
errno = 0;
156-
}
157-
return P_STATE_EXISTS;
149+
close(fd);
150+
return EEXIST;
158151
}
159152

160-
// open() returns ENOENT in the case of dangling symbolic links
161-
// check if it's a link or not:
162-
if (errno == ENOENT)
163-
{
164-
static char buf[1];
165-
ssize_t f = readlink(pathname, buf, 1);
166-
*buf = '\0';
167-
if (f != -1)
168-
return P_STATE_EXISTS;
169-
else
170-
{
171-
errno = 0;
172-
return P_STATE_ENOENT;
173-
}
174-
}
153+
/* FreeBSD sets errno to EMLINK instead of ELOOP as specified by POSIX
154+
when O_NOFOLLOW is set in flags and the final component of pathname is
155+
a symbolic link to distinguish it from the case of too many symbolic
156+
link traversals in one of its non-final components.
157+
https://man.freebsd.org/cgi/man.cgi?query=open
158+
*/
159+
160+
if (errno == ELOOP || errno == EMLINK)
161+
return EEXIST;
162+
else if (errno == ENOENT)
163+
return ENOENT;
175164

176-
printf("open %s: %s\n", pathname, strerror(errno));
177-
errno = 0;
178-
return P_STATE_ERR;
165+
fprintf(stderr, "open %s: %s\n", pathname, strerror(errno));
166+
return -1;
179167
}
180168

181169
void
@@ -533,12 +521,12 @@ test_rmw_mkdir(const char *h)
533521
{
534522
const char *subdirs = "foo/bar/21/42";
535523
char *dir = join_paths(h, subdirs);
536-
if (check_pathname_state(dir) == P_STATE_EXISTS)
524+
if (check_pathname_state(dir) == EEXIST)
537525
assert(bsdutils_rm(dir, verbose) == 0);
538526
assert(rmw_mkdir(dir) == 0);
539527
assert(dir);
540528
printf("%s\n", dir);
541-
assert(check_pathname_state(dir) == P_STATE_EXISTS);
529+
assert(check_pathname_state(dir) == EEXIST);
542530
assert(bsdutils_rm(dir, verbose) == 0);
543531
free(dir);
544532

@@ -691,30 +679,30 @@ test_check_pathname_state(const char *const homedir)
691679
FILE *fp = fopen(foobar, "w");
692680
assert(fp != NULL);
693681
assert(fclose(fp) != EOF);
694-
assert(check_pathname_state(foobar) == P_STATE_EXISTS);
682+
assert(check_pathname_state(foobar) == EEXIST);
695683

696684
char *snafu = join_paths(homedir, "snafu");
697685
assert(symlink(foobar, snafu) == 0);
698-
assert(check_pathname_state(snafu) == P_STATE_EXISTS);
686+
assert(check_pathname_state(snafu) == EEXIST);
699687
assert(remove(foobar) == 0);
700688
free(foobar);
701689
assert(remove(snafu) == 0);
702690
free(snafu);
703691

704692
char *home_link = join_paths(homedir, "home_1234");
705-
assert(check_pathname_state(homedir) == P_STATE_EXISTS);
706-
if (check_pathname_state(home_link) == P_STATE_EXISTS)
693+
assert(check_pathname_state(homedir) == EEXIST);
694+
if (check_pathname_state(home_link) == EEXIST)
707695
assert(remove(home_link) == 0);
708696
assert(symlink(homedir, home_link) == 0);
709-
assert(check_pathname_state(home_link) == P_STATE_EXISTS);
697+
assert(check_pathname_state(home_link) == EEXIST);
710698
assert(remove(home_link) == 0);
711699
free(home_link);
712700

713701
const char *dlink = "dangling_link";
714-
if (check_pathname_state(dlink) == P_STATE_EXISTS)
702+
if (check_pathname_state(dlink) == EEXIST)
715703
assert(remove(dlink) == 0);
716704
assert(symlink("dangler", dlink) == 0);
717-
assert(check_pathname_state(dlink) == P_STATE_EXISTS);
705+
assert(check_pathname_state(dlink) == EEXIST);
718706
assert(remove(dlink) == 0);
719707

720708
return;

src/utils.h

-7
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
2727

2828
#include "trashinfo.h"
2929

30-
enum
31-
{
32-
P_STATE_ERR = -1,
33-
P_STATE_ENOENT,
34-
P_STATE_EXISTS
35-
};
36-
3730
#define join_paths(...) real_join_paths(__VA_ARGS__, NULL)
3831

3932
#define LEN_MAX_HUMAN_READABLE_SIZE (sizeof "xxxx.y GiB")

0 commit comments

Comments
 (0)