-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathconfig_rmw.c
More file actions
461 lines (401 loc) · 11.7 KB
/
config_rmw.c
File metadata and controls
461 lines (401 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
/*
This file is part of rmw<https://theimpossibleastronaut.github.io/rmw-website/>
Copyright (C) 2012-2023 Andy Alt (arch_stanton5995@proton.me)
Other authors: https://github.com/theimpossibleastronaut/rmw/blob/master/AUTHORS.md
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <unistd.h>
#include "ficlone.h"
#include "config_rmw.h"
#include "utils.h"
#include "main.h"
// #include <sys/sysmacros.h> // for major() and minor()
static const int DEFAULT_EXPIRE_AGE = 0;
const char *expire_age_str = "expire_age";
/*!
* Prints a copy of the default config file to the specified stream.
*
* If any changes are made to rmwrc.example, the text here will need to be updated
* to match
*/
void
print_config(FILE *restrict stream)
{
fputs(_("\
# rmw default waste directory, separate from the desktop trash\n"), stream);
fputs("\
WASTE = $HOME/.local/share/Waste\n", stream);
fputs(_("\n\
# The directory used by the FreeDesktop.org Trash spec\n\
# Note to macOS and Windows users: moving files to 'Desktop' trash\n\
# doesn't work yet\n"), stream);
fputs("\
# WASTE=$HOME/.local/share/Trash\n", stream);
fputs("\n", stream);
fputs(_("\
# A folder can use the $UID variable.\n"), stream);
fputs(_("\
# See the README or man page for details about using the 'removable' attribute\n"), stream);
fputs("\
# WASTE=/mnt/flash/.Trash-$UID, removable\n", stream);
fputs(_("\n\
# How many days should items be allowed to stay in the waste\n\
# directories before they are permanently deleted\n\
#\n\
# use '0' to disable purging (can be overridden by using --purge=N_DAYS)\n\
#\n"), stream);
fprintf(stream, "\
%s = %d\n", expire_age_str, DEFAULT_EXPIRE_AGE);
fputs(_("\n\
# purge is allowed to run without the '-f' option. If you'd rather\n\
# require the use of '-f', you may uncomment the line below.\n\
#\n"), stream);
fputs("\
# force_required\n", stream);
}
/*
* replace part of a string, adapted from code by Gazl
* https://www.linuxquestions.org/questions/showthread.php?&p=5794938#post5794938
*/
static char *
strrepl(char *src, const char *str, char *repl)
{
// The replacement text may make the returned string shorter or
// longer than src, so just add the length of all three for the
// mallocation.
size_t req_len = strlen(src) + strlen(str) + strlen(repl) + 1;
char *dest = malloc(req_len);
if (dest == NULL)
return NULL;
char *s, *d, *p;
s = strstr(src, str);
if (s && *str != '\0')
{
d = dest;
for (p = src; p < s; p++, d++)
*d = *p;
for (p = repl; *p != '\0'; p++, d++)
*d = *p;
for (p = s + strlen(str); *p != '\0'; p++, d++)
*d = *p;
*d = '\0';
}
else
strcpy(dest, src);
dest = realloc(dest, strlen(dest) + 1);
if (dest == NULL)
return NULL;
return dest;
}
// looks for '$HOME', '$UID', or '~' in a string and replace it with its
// corresponding literal value
//
// TODO: make it compatible with Windows systems.
static unsigned short
realize_str(char *str, const char *homedir, const char *uid)
{
struct st_vars_to_check
{
const char *name;
const char *value;
} st_var[] = {
{"~", homedir},
{"$HOME", homedir},
{"$UID", uid},
{NULL, NULL}
};
int i = 0;
while (st_var[i].name != NULL)
{
if (strstr(str, st_var[i].name) != NULL)
{
char *dest = strrepl(str, st_var[i].name, (char *) st_var[i].value);
if (dest == NULL)
return -1;
if (snprintf(str, PATH_MAX, "%s", dest) >= PATH_MAX)
{
free(dest);
return -1;
}
free(dest);
/* check the string again, in case str contains something like
* $HOME/Trash-$UID (which would be rare, if ever, but... */
i--;
}
i++;
}
return 0;
}
/*!
* This function is called when the "WASTE" option is encountered in the
* config file. The line is parsed and added to the linked list of WASTE
* folders.
*/
static st_waste *
parse_line_waste(st_waste *waste_curr, struct Canfigger *node,
const rmw_options *cli_user_options,
const char *homedir, const char *uid)
{
bool removable = 0;
char *attr = NULL;
canfigger_free_current_attr_str_advance(node->attributes, &attr);
if (attr)
{
if (strcmp("removable", attr) == 0)
removable = 1;
else
{
print_msg_warn();
printf("ignoring invalid attribute: '%s'\n", node->attributes->str);
}
}
bufchk_len(strlen(node->value) + 1, PATH_MAX, __func__, __LINE__);
char tmp_waste_parent_folder[PATH_MAX];
trim_char('/', node->value);
sn_check(snprintf(tmp_waste_parent_folder, sizeof tmp_waste_parent_folder,
"%s", node->value), sizeof tmp_waste_parent_folder);
if (realize_str(tmp_waste_parent_folder, homedir, uid) != 0)
{
print_msg_error();
fprintf(stderr, "truncated: %s\n", tmp_waste_parent_folder);
}
bool is_attached =
(check_pathname_state(tmp_waste_parent_folder) == EEXIST);
if (removable && !is_attached)
{
if (cli_user_options->list)
show_folder_line(tmp_waste_parent_folder, removable, is_attached);
return NULL;
}
st_waste *temp_node = malloc(sizeof(st_waste));
if (!temp_node)
fatal_malloc();
if (waste_curr != NULL)
{
waste_curr->next_node = temp_node;
temp_node->prev_node = waste_curr;
waste_curr = waste_curr->next_node;
}
else
{
waste_curr = temp_node;
waste_curr->prev_node = NULL;
}
waste_curr->next_node = NULL;
waste_curr->removable = removable ? true : false;
/* make the parent... */
waste_curr->parent = malloc(strlen(tmp_waste_parent_folder) + 1);
if (!waste_curr->parent)
fatal_malloc();
strcpy(waste_curr->parent, tmp_waste_parent_folder);
if (is_symlink(waste_curr->parent))
{
print_msg_warn();
printf(_("symbolic link: %s\n\
:In the future, rmw will not allow using symbolic links\n\
:as waste parent folders.\n"), waste_curr->parent);
}
/* and the files... */
waste_curr->files = join_paths(waste_curr->parent, "files");
waste_curr->len_files = strlen(waste_curr->files);
int p_state = check_pathname_state(waste_curr->files);
if (p_state == ENOENT)
{
if (!rmw_mkdir(waste_curr->files))
msg_success_mkdir(waste_curr->files);
else
{
msg_err_mkdir(waste_curr->files, __func__);
exit(errno);
}
}
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)) == ENOENT)
{
if (!rmw_mkdir(waste_curr->info))
msg_success_mkdir(waste_curr->info);
else
{
msg_err_mkdir(waste_curr->info, __func__);
exit(errno);
}
}
else if (p_state == -1)
exit(p_state);
waste_curr->is_ficlone_fs = is_ficlone_fs(waste_curr->parent);
// get device number to use later for rename
struct stat st, mp_st;
if (!lstat(waste_curr->parent, &st))
{
waste_curr->dev_num = st.st_dev;
// printf("actual: %ld |major: %d | minor: %d\n", st.st_dev, major(st.st_dev), minor(st.st_dev));
char tmp[PATH_MAX];
strcpy(tmp, waste_curr->parent);
char *media_root_ptr = rmw_dirname(tmp);
if (!media_root_ptr)
{
fputs("Error getting media root pointer.\n\
char *media_root_ptr = rmw_dirname(tmp)\n", stderr);
exit(EXIT_FAILURE);
}
if (!(waste_curr->media_root = malloc(strlen(media_root_ptr) + 1)))
fatal_malloc();
strcpy(waste_curr->media_root, media_root_ptr);
sn_check(snprintf(tmp, sizeof tmp, "%s", waste_curr->media_root),
sizeof tmp);
if (!lstat(rmw_dirname(tmp), &mp_st))
{
if (mp_st.st_dev == waste_curr->dev_num)
{
free(waste_curr->media_root);
waste_curr->media_root = NULL;
}
}
else
msg_err_lstat(waste_curr->parent, __func__, __LINE__);
}
else
msg_err_lstat(waste_curr->parent, __func__, __LINE__);
return waste_curr;
}
/*!
* Get configuration data (parse the config file)
*/
void
parse_config_file(const rmw_options *cli_user_options,
st_config *st_config_data, const st_loc *st_location)
{
struct Canfigger *cfg_node =
canfigger_parse_file(st_location->config_file, ',');
if (cfg_node == NULL)
{
perror(__func__);
exit(errno);
}
typedef enum
{
EXPIRE_AGE,
WASTE,
FORCE_REQUIRED,
PURGE_AFTER,
INVALID_OPTION
} cfg_opt_id;
struct opt
{
const char *opt;
cfg_opt_id id;
};
struct opt st_opt[] = {
{"WASTE", WASTE},
{expire_age_str, EXPIRE_AGE},
{"force_required", FORCE_REQUIRED},
{"purge_after", PURGE_AFTER},
{NULL, INVALID_OPTION}
};
st_waste *waste_curr = st_config_data->st_waste_folder_props_head;
while (cfg_node != NULL)
{
int i = 0;
while (st_opt[i].opt != NULL)
{
if (strcasecmp(st_opt[i].opt, cfg_node->key) == 0)
break;
i++;
}
switch (st_opt[i].id)
{
case EXPIRE_AGE:
case PURGE_AFTER:
if (cli_user_options->want_purge <= 0)
{
st_config_data->expire_age = atoi(cfg_node->value);
if (i == PURGE_AFTER)
{
putchar('\n');
print_msg_warn();
printf("\
The configuration option 'purge_after' will be deprecated.\n\
Please replace it with '%s' instead.\n\n", expire_age_str);
}
}
else
{
st_config_data->expire_age = cli_user_options->want_purge;
}
break;
case FORCE_REQUIRED:
st_config_data->force_required = 1;
break;
case WASTE:
{
st_waste *st_new_waste_ptr =
parse_line_waste(waste_curr, cfg_node, cli_user_options,
st_location->home_dir, st_config_data->uid);
if (st_new_waste_ptr != NULL)
{
waste_curr = st_new_waste_ptr;
if (st_config_data->st_waste_folder_props_head == NULL)
st_config_data->st_waste_folder_props_head = waste_curr;
}
}
break;
default:
print_msg_warn();
printf(_("Unknown or invalid option: '%s'\n"), cfg_node->key);
}
canfigger_free_current_key_node_advance(&cfg_node);
}
if (waste_curr == NULL)
{
printf(_("no usable WASTE folder could be found\n\
Please check your configuration file and permissions\n\
If you need further help, or to report a possible bug,\n\
visit the rmw web site at\n"));
printf(" " PACKAGE_URL "\n");
printf("Unable to continue. Exiting...\n");
exit(EXIT_FAILURE);
}
return;
}
void
init_config_data(st_config *x)
{
x->st_waste_folder_props_head = NULL;
/*
* The default value is only used as a last resort,
* if for some reason unspecified in the config file.
*/
x->expire_age = DEFAULT_EXPIRE_AGE;
x->force_required = 0;
// get the UID
sn_check(snprintf(x->uid, sizeof x->uid, "%d", getuid()), sizeof x->uid);
}
void
show_folder_line(const char *folder, const bool is_r, const bool is_attached)
{
printf("%s", folder);
if (is_r && verbose)
{
/*
* These lines are separated to ease translation
*
*/
printf(" (");
printf(_("removable, "));
/* TRANSLATORS: context - "a mounted device or filesystem is presently attached or mounted" */
printf("%s)", is_attached == true ? _("attached") : _("detached"));
}
putchar('\n');
}