Skip to content

Commit 70afe64

Browse files
authored
maint: Refactor trashinfo; cleanup (#492)
* maint: Cleanup trashinfo * more cleanup, obliterate union * Add error-checking to fprintf when writing trashinfo file * combine enums * Move some vars into globals.c * rename function * narrow scope of LEN_DELETION_DATE_KEY_WITH_VALUE
1 parent 487357a commit 70afe64

11 files changed

+126
-152
lines changed

src/config_rmw.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
2828
// #include <sys/sysmacros.h> // for major() and minor()
2929

3030
static const int DEFAULT_EXPIRE_AGE = 0;
31-
static const char *lit_files = "files";
3231

3332
const char *expire_age_str = "expire_age";
3433

@@ -243,7 +242,7 @@ parse_line_waste(st_waste *waste_curr, struct Canfigger *node,
243242
}
244243

245244
/* and the files... */
246-
waste_curr->files = join_paths(waste_curr->parent, lit_files);
245+
waste_curr->files = join_paths(waste_curr->parent, "files");
247246
waste_curr->len_files = strlen(waste_curr->files);
248247

249248
int p_state = check_pathname_state(waste_curr->files);

src/globals.c

+3
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
2121
#include "globals.h"
2222

2323
int verbose = 0;
24+
const char *lit_info = "info";
25+
const char trashinfo_ext[] = ".trashinfo";
26+
const int len_trashinfo_ext = sizeof trashinfo_ext - 1; /* Subtract 1 for the terminating NULL */

src/globals.h

+12-5
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,19 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
4747
#define VERSION "unversioned"
4848
#endif
4949

50-
#ifndef PATH_MAX
51-
#define PATH_MAX 4096
52-
#endif
53-
54-
#define LEN_MAX_ESCAPED_PATH (((PATH_MAX - 1) * 3) + 1)
50+
#define LEN_MAX_ESCAPED_PATH (PATH_MAX * 3)
5551

5652
#define EBUF 11
5753

54+
typedef enum
55+
{
56+
TI_HEADER,
57+
PATH_KEY,
58+
DELETIONDATE_KEY,
59+
TI_LINE_MAX
60+
} ti_key;
61+
5862
extern int verbose;
63+
extern const char *lit_info;
64+
extern const char trashinfo_ext[];
65+
extern const int len_trashinfo_ext;

src/main.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -680,7 +680,7 @@ main(const int argc, char *const argv[])
680680
parse_cli_options(argc, argv, &cli_user_options);
681681

682682
if (verbose > 1)
683-
printf("PATH_MAX = %d\n", PATH_MAX - 1);
683+
printf("PATH_MAX = %d\n", PATH_MAX);
684684

685685
if (verbose > 0)
686686
#ifdef HAVE_LINUX_BTRFS

src/messages.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,7 @@ display_dot_trashinfo_error(const char *dti)
9494
* "format" refers to the layout of the file
9595
* contents
9696
*/
97-
printf(_("format of .trashinfo `file %s` is incorrect"), dti);
98-
putchar('\n');
97+
printf(_("format of .trashinfo file '%s' is incorrect\n"), dti);
9998
return;
10099
}
101100

src/purging.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ static time_t
9595
get_then_time(const char *tinfo_file)
9696
{
9797
char *raw_deletion_date =
98-
parse_trashinfo_file(tinfo_file, deletion_date_key);
98+
validate_and_get_value(tinfo_file, DELETIONDATE_KEY);
9999
if (raw_deletion_date != NULL)
100100
{
101101
struct tm tm_then;

src/restore.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ restore(const char *src, st_time *st_time_var,
112112
tmp_str = NULL;
113113
sn_check(r, PATH_MAX);
114114

115-
char *_dest = parse_trashinfo_file(src_tinfo, path_key);
115+
char *_dest = validate_and_get_value(src_tinfo, PATH_KEY);
116116
if (_dest == NULL)
117117
return -1;
118118

src/trashinfo.c

+93-106
Original file line numberDiff line numberDiff line change
@@ -18,37 +18,17 @@ You should have received a copy of the GNU General Public License
1818
along with this program. If not, see <http://www.gnu.org/licenses/>.
1919
*/
2020

21+
#include <stdint.h>
22+
2123
#include "trashinfo.h"
2224
#include "utils.h"
2325
#include "messages.h"
2426

25-
#define TI_LINE_COUNT 3
26-
27-
enum
28-
{
29-
TI_HEADER,
30-
TI_PATH_LINE,
31-
TI_DATE_LINE
32-
};
33-
34-
static const char ti_header[] = "[Trash Info]";
35-
static const char ti_path[] = "Path=";
36-
static const char ti_date[] = "DeletionDate=";
27+
const size_t LEN_MAX_TRASHINFO_PATH_LINE =
28+
sizeof "Path=" + LEN_MAX_ESCAPED_PATH - 1;
3729

38-
static const struct st__trashinfo st_trashinfo_template[] = {
39-
{ti_header, sizeof ti_header - 1},
40-
{ti_path, sizeof ti_path - 1},
41-
{ti_date, sizeof ti_date - 1},
42-
};
43-
44-
const char trashinfo_ext[] = ".trashinfo";
45-
const int len_trashinfo_ext = sizeof trashinfo_ext - 1; /* Subtract 1 for the terminating NULL */
46-
const int LEN_MAX_TRASHINFO_PATH_LINE =
47-
(sizeof ti_path - 1) + LEN_MAX_ESCAPED_PATH;
48-
49-
const char *lit_info = "info";
50-
const char *path_key = "Path";
51-
const char *deletion_date_key = "DeletionDate";
30+
const struct trashinfo_template trashinfo_template =
31+
{ "[Trash Info]", "Path=", "DeletionDate=" };
5232

5333

5434
int
@@ -105,13 +85,32 @@ create_trashinfo(rmw_target *st_f_props, st_waste *waste_curr,
10585
}
10686
}
10787

108-
fprintf(fp, "%s\n", st_trashinfo_template[TI_HEADER].str);
109-
fprintf(fp, "%s%s\n", st_trashinfo_template[TI_PATH_LINE].str,
110-
escaped_path_ptr);
88+
ssize_t want_size = strlen(trashinfo_template.header) + 1 +
89+
strlen(trashinfo_template.path_key) +
90+
strlen(escaped_path_ptr) + 1 +
91+
strlen(trashinfo_template.deletion_date_key) +
92+
strlen(st_time_var->deletion_date) + 1;
93+
94+
int n = fprintf(fp, "%s\n%s%s\n%s%s\n", trashinfo_template.header,
95+
trashinfo_template.path_key, escaped_path_ptr,
96+
trashinfo_template.deletion_date_key,
97+
st_time_var->deletion_date);
98+
11199
free(escaped_path);
112-
fprintf(fp, "%s%s\n", st_trashinfo_template[TI_DATE_LINE].str,
113-
st_time_var->deletion_date);
114100

101+
if (n < 0)
102+
{
103+
print_msg_error();
104+
fprintf(stderr, "fprintf() failed due to an error writing to %s\n",
105+
final_info_dest);
106+
}
107+
else if (n != want_size)
108+
{
109+
print_msg_error();
110+
fprintf(stderr,
111+
"Expected to write %zu bytes, but wrote %d bytes to %s\n",
112+
want_size, n, final_info_dest);
113+
}
115114
return close_file(&fp, final_info_dest, __func__);
116115
}
117116
else
@@ -122,108 +121,96 @@ create_trashinfo(rmw_target *st_f_props, st_waste *waste_curr,
122121
}
123122

124123

125-
/*
126-
* name: parse_trashinfo_file
127-
*
128-
* Checks the integrity of a trashinfo file and returns req_value for
129-
* either the Path or DeletionDate key
130-
*
131-
*/
132124
char *
133-
parse_trashinfo_file(const char *file, const char *req_value)
125+
validate_and_get_value(const char *file, ti_key key)
134126
{
135-
struct trashinfo_field trashinfo_field;
136-
if (strcmp(req_value, path_key) != 0
137-
&& strcmp(req_value, deletion_date_key) != 0)
127+
const uint8_t LEN_DELETION_DATE_KEY_WITH_VALUE = 32;
128+
struct
138129
{
139-
print_msg_error();
140-
fprintf(stderr, "Required arg for %s can be either \"%s\" or \"%s\".",
141-
__func__, path_key, deletion_date_key);
142-
return NULL;
143-
}
130+
bool header_ok;
131+
bool path_ok;
132+
bool date_ok;
133+
} ti_status = { false, false, false };
144134

145-
int line_no = 0;
146135
FILE *fp = fopen(file, "r");
147136
if (fp != NULL)
148137
{
149-
trashinfo_field.value = NULL;
150-
bool res = true;
138+
char *key_value = NULL;
139+
uint8_t line_n = 0;
140+
151141
char fp_line[LEN_MAX_TRASHINFO_PATH_LINE];
152142
while (fgets(fp_line, LEN_MAX_TRASHINFO_PATH_LINE, fp) != NULL
153-
&& res == true)
143+
&& line_n < TI_LINE_MAX)
154144
{
155145
trim_whitespace(fp_line);
156-
157-
switch (line_no)
146+
char *val_ptr;
147+
switch (line_n)
158148
{
159149
case TI_HEADER:
160-
res =
161-
strncmp(fp_line, st_trashinfo_template[TI_HEADER].str,
162-
st_trashinfo_template[TI_HEADER].len) == 0;
150+
ti_status.header_ok =
151+
(strcmp(fp_line, trashinfo_template.header) == 0);
163152
break;
164-
case TI_PATH_LINE:
165-
res =
166-
strncmp(fp_line, st_trashinfo_template[TI_PATH_LINE].str,
167-
st_trashinfo_template[TI_PATH_LINE].len) == 0;
168-
if (res && strcmp(req_value, path_key) == 0)
153+
case PATH_KEY:
154+
ti_status.path_ok =
155+
(strncmp
156+
(fp_line, trashinfo_template.path_key,
157+
strlen(trashinfo_template.path_key)) == 0);
158+
if (ti_status.path_ok && key == PATH_KEY)
169159
{
170-
trashinfo_field.f.path_ptr = strchr(fp_line, '=');
171-
trashinfo_field.f.path_ptr++; /* move past the '=' sign */
172-
char *unescaped_path = unescape_url(trashinfo_field.f.path_ptr);
173-
trashinfo_field.value = unescaped_path;
160+
val_ptr = strchr(fp_line, '=');
161+
if (val_ptr)
162+
{
163+
val_ptr++; /* move past the '=' sign */
164+
char *unescaped_path = unescape_url(val_ptr);
165+
if (!unescaped_path)
166+
fatal_malloc();
167+
key_value = unescaped_path;
168+
}
174169
}
175170
break;
176-
case TI_DATE_LINE:
177-
res =
178-
strncmp(fp_line, st_trashinfo_template[TI_DATE_LINE].str,
179-
st_trashinfo_template[TI_DATE_LINE].len) == 0
180-
&& strlen(fp_line) == 32;
181-
182-
if (res && strcmp(req_value, deletion_date_key) == 0)
171+
case DELETIONDATE_KEY:
172+
ti_status.date_ok =
173+
(strncmp(fp_line, trashinfo_template.deletion_date_key,
174+
strlen(trashinfo_template.deletion_date_key)) == 0)
175+
&& strlen(fp_line) == LEN_DELETION_DATE_KEY_WITH_VALUE;
176+
if (ti_status.date_ok && key == DELETIONDATE_KEY)
183177
{
184-
trashinfo_field.f.date_str_ptr = strchr(fp_line, '=');
185-
trashinfo_field.f.date_str_ptr++;
186-
trashinfo_field.value = strdup(trashinfo_field.f.date_str_ptr);
187-
if (!trashinfo_field.value)
188-
fatal_malloc();
178+
val_ptr = strchr(fp_line, '=');
179+
if (val_ptr)
180+
{
181+
val_ptr++;
182+
key_value = strdup(val_ptr);
183+
if (!key_value)
184+
fatal_malloc();
185+
}
189186
}
190187
break;
191-
default:
192-
res = false;
193-
break;
194188
}
195-
line_no++;
189+
line_n++;
196190
}
197191
close_file(&fp, file, __func__);
198192

199-
if (res && line_no == TI_LINE_COUNT)
200-
return trashinfo_field.value;
193+
if (ti_status.header_ok && ti_status.path_ok && ti_status.date_ok
194+
&& key_value)
195+
return key_value;
201196

202-
if (trashinfo_field.value != NULL)
203-
free(trashinfo_field.value);
197+
if (key_value != NULL)
198+
free(key_value);
204199
display_dot_trashinfo_error(file);
205200
return NULL;
206201
}
207-
else
208-
{
209-
open_err(file, __func__);
210-
return NULL;
211-
}
202+
open_err(file, __func__);
203+
return NULL;
212204
}
213205

214-
///////////////////////////////////////////////////////////////////////
215-
#ifdef TEST_LIB
216-
217-
#include "test.h"
218-
219-
int
220-
main()
221-
{
222-
assert(strcmp(st_trashinfo_template[TI_HEADER].str, "[Trash Info]") == 0);
223-
assert(strcmp(st_trashinfo_template[TI_PATH_LINE].str, "Path=") == 0);
224-
assert(strcmp(st_trashinfo_template[TI_DATE_LINE].str, "DeletionDate=") ==
225-
0);
226-
227-
return 0;
228-
}
229-
#endif
206+
//const char *ti_key_to_string(ti_key key)
207+
//{
208+
//switch (key)
209+
//{
210+
//case TI_HEADER: return "TI_HEADER";
211+
//case PATH_KEY: return "PATH_KEY";
212+
//case DELETIONDATE_KEY: return "DELETIONDATE_KEY";
213+
//case TI_LINE_MAX: return "TI_LINE_MAX";
214+
//default: return "UNKNOWN_KEY";
215+
//}
216+
//}

src/trashinfo.h

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

2626
#include "time_rmw.h"
2727

28-
extern const char trashinfo_ext[];
29-
extern const int len_trashinfo_ext;
30-
31-
extern const int TI_LINE_COUNT;
32-
33-
extern const int LEN_MAX_TRASHINFO_PATH_LINE;
34-
3528
/** Each waste directory is added to a linked list and has the data
3629
* from this structure associated with it.
3730
*/
@@ -103,30 +96,16 @@ typedef struct
10396
bool is_duplicate;
10497
} rmw_target;
10598

106-
107-
extern const char *lit_info;
108-
extern const char *path_key;
109-
extern const char *deletion_date_key;
110-
111-
struct st__trashinfo
99+
extern const struct trashinfo_template
112100
{
113-
const char *str;
114-
const unsigned short int len;
115-
};
116-
101+
const char *header;
102+
const char *path_key;
103+
const char *deletion_date_key;
104+
} trashinfo_template;
117105

118-
struct trashinfo_field
119-
{
120-
char *value;
121-
union
122-
{
123-
char *path_ptr;
124-
char *date_str_ptr;
125-
} f;
126-
};
127106

128107
int
129108
create_trashinfo(rmw_target * st_f_props, st_waste * waste_curr,
130109
st_time * st_time_var);
131110

132-
char *parse_trashinfo_file(const char *file, const char *req_value);
111+
char *validate_and_get_value(const char *file, ti_key key);

0 commit comments

Comments
 (0)