-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathutils.c
More file actions
532 lines (444 loc) · 11.4 KB
/
utils.c
File metadata and controls
532 lines (444 loc) · 11.4 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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
/*
This file is part of rmw<https://theimpossibleastronaut.github.io/rmw-website/>
Copyright (C) 2012-2025 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/>.
*/
#ifndef INC_GLOBALS_H
#define INC_GLOBALS_H
#include "globals.h"
#endif
#include <gio/gio.h>
#include "ficlone.h"
#include "utils.h"
#include "messages.h"
bool
is_symlink(const char *path)
{
int fd = open(path, O_RDONLY | O_NOFOLLOW);
if (fd != -1)
{
close(fd);
return false;
}
else if (errno == ELOOP)
return true;
return false;
}
/**
* rmw_mkdir()
*
* Check for the existence of a dir, and create it if not found.
* Also creates parent directories.
*/
int
rmw_mkdir(const char *dir)
{
if (!dir)
return -1;
if (check_pathname_state(dir) == EEXIST)
return -1;
return g_mkdir_with_parents(dir, 0777);
}
/*!
* Determine whether a file or directory exists, and is accessible.
*/
int
check_pathname_state(const char *pathname)
{
if (!pathname)
return -1;
int fd = open(pathname, O_RDONLY | O_NOFOLLOW);
if (fd != -1)
{
if (close(fd) != 0)
fprintf(stderr, "close: %s\n%s", pathname, strerror(errno));
return EEXIST;
}
/* FreeBSD sets errno to EMLINK instead of ELOOP as specified by POSIX
when O_NOFOLLOW is set in flags and the final component of pathname is
a symbolic link to distinguish it from the case of too many symbolic
link traversals in one of its non-final components.
https://man.freebsd.org/cgi/man.cgi?query=open
*/
if (errno == ELOOP || errno == EMLINK)
return EEXIST;
else if (errno == ENOENT)
return ENOENT;
fprintf(stderr, "open %s: %s\n", pathname, strerror(errno));
return -1;
}
void
dispose_waste(st_waste *node)
{
if (node != NULL)
{
dispose_waste(node->next_node);
free(node->parent);
free(node->files);
free(node->info);
if (node->media_root != NULL)
free(node->media_root);
free(node);
}
return;
}
void
make_size_human_readable(const off_t size, char *buf)
{
/* Store only the first letter; we add "iB" later during snprintf(). */
const char prefix[] = { 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y' };
short power = -1;
off_t remainder = 0;
off_t hr_size = size;
while (hr_size >= 1024)
{
remainder = hr_size % 1024;
hr_size /= 1024;
power++;
}
// Doing some casting here because after the division above, 'hr_size'
// and 'remainder' should not be any more than 4 digits
if (power >= 0)
sn_check(snprintf
(buf, LEN_MAX_HUMAN_READABLE_SIZE, "%d.%d %ciB",
(short) hr_size, (short) remainder * 10 / 1024,
prefix[power]), LEN_MAX_HUMAN_READABLE_SIZE);
else
sn_check(snprintf
(buf, LEN_MAX_HUMAN_READABLE_SIZE, "%d B",
(short) hr_size), LEN_MAX_HUMAN_READABLE_SIZE);
return;
}
/*!
* Prompt user for confirmation.
* @return true if 'y' or 'Y' was entered, false otherwise"
*/
bool
user_verify(void)
{
fputs(_("Continue? (y/n): "), stdout);
int answer = getchar();
bool want_continue = (answer == (int) 'Y') || (answer == (int) 'y');
int char_count = 0;
/* Check if there's any more chars */
while (answer != '\n' && answer != EOF)
{
char_count++;
if (char_count > 1024)
{
fputs("Too many chars in stdin!!\n", stderr);
exit(EXIT_FAILURE);
}
answer = getchar();
}
return (want_continue && (char_count <= 1));
}
/*!
* Convert str into a URL valid string, escaping when necessary.
* "/" is treated as unreserved per RFC2396 for this application.
* returns an allocated string which must be freed later
*/
char *
escape_url(const char *str)
{
char *result = (char *) g_uri_escape_string(str, "/", FALSE);
if (!result)
fatal_malloc();
return result;
}
/**
* Convert a URL valid string into a regular string, unescaping any '%'s
* that appear.
* returns an allocated string which must be freed later
*/
char *
unescape_url(const char *str)
{
char *result = (char *) g_uri_unescape_string(str, NULL);
if (!result)
fatal_malloc();
return result;
}
/*
* name: isdotdir
* Checks for . and .. directories
*/
bool
isdotdir(const char *dir)
{
return (strcmp(dir, ".") == 0 || strcmp(dir, "..") == 0);
}
/*
* name: resolve_path()
*
* Gets the absolute path + filename
*
* realpath() by itself doesn't give the desired result if basename is
* a dangling * symlink; this function is designed to do that.
*
*/
char *
resolve_path(const char *file, const char *b)
{
int req_len = strlen(file) + 1;
bufchk_len(req_len, PATH_MAX, __func__, __LINE__);
char tmp[req_len];
strcpy(tmp, file);
/* g_path_get_dirname("foo/") returns "foo", not "." — strip trailing slash
so it behaves like POSIX dirname */
size_t tlen = strlen(tmp);
if (tlen > 1 && tmp[tlen - 1] == '/')
tmp[tlen - 1] = '\0';
gchar *dir = g_path_get_dirname(tmp);
char *orig_dirname = realpath(dir, NULL);
g_free(dir);
if (orig_dirname == NULL)
{
print_msg_error();
perror("realpath()");
return NULL;
}
char *abspath = join_paths(orig_dirname, b);
free(orig_dirname);
return abspath;
}
/*!
* Trim a trailing character of a string
* @param[in] c The character to erase
* @param[out] str The string to alter
* @return void
*/
void
trim_char(const int c, char *str)
{
char *p = str;
trim_whitespace(p);
if (*p == '\0')
return;
while (*p != '\0')
p++;
p--;
while (*p == c)
{
*p = '\0';
if (p == str)
return;
p--;
}
}
bool
is_dir_f(const char *pathname)
{
// O_FOLLOW is needed, otherwise the a symlink to a directory
// would be detected as a directory, which we don't want
int fd = open(pathname, O_RDONLY | O_DIRECTORY | O_NOFOLLOW);
if (fd != -1)
{
if (close(fd) != 0)
perror("close:");
return true;
}
return false;
}
int
count_chars(const char c, const char *str)
{
int i = 0, n = 0;
while (str[i])
{
if (str[i++] == c)
n++;
}
return n;
}
///////////////////////////////////////////////////////////////////////
#ifdef TEST_LIB
#include "test.h"
#include "purging.h"
static void
test_isdotdir(void)
{
assert(isdotdir(".") == true);
assert(isdotdir("..") == true);
assert(isdotdir(".t") == false);
assert(isdotdir("...") == false);
assert(isdotdir("t.") == false);
assert(isdotdir(".. ") == false);
return;
}
static void
test_rmw_mkdir(const char *h)
{
const char *subdirs = "foo/bar/21/42";
char *dir = join_paths(h, subdirs);
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) == EEXIST);
assert(bsdutils_rm(dir, verbose) == 0);
free(dir);
return;
}
static void
test_make_size_human_readable(void)
{
struct expected
{
const off_t file_size;
const char *out;
} const data[] = {
{256, "256 B"},
{1024, "1.0 KiB"},
{12000, "11.7 KiB"},
{32000000000, "29.8 GiB"},
{62000000000000, "56.3 TiB"},
{82000300000000000, "72.8 PiB"},
};
int data_size = ARRAY_SIZE(data);
int i = 0;
while (i < data_size)
{
char hr[LEN_MAX_HUMAN_READABLE_SIZE];
make_size_human_readable(data[i].file_size, hr);
fprintf(stderr, "hr_size: %s\nExpected: %s\n\n", hr, data[i].out);
assert(strcmp(hr, data[i].out) == 0);
i++;
}
assert(i == data_size);
return;
}
void
test_join_paths(void)
{
char *path = join_paths("home", "foo//", "bar");
assert(path != NULL);
assert(strcmp(path, "home/foo/bar") == 0);
free(path);
path = join_paths("/home/foo", "bar", "world");
assert(path != NULL);
assert(strcmp(path, "/home/foo/bar/world") == 0);
free(path);
return;
}
void
test_trim_char(void)
{
char foo[] = "Hello Worldd ";
trim_char('d', foo);
assert(strcmp(foo, "Hello Worl") == 0);
*foo = '\0';
trim_char('/', foo);
assert(*foo == '\0');
strcpy(foo, "ccc");
trim_char('c', foo);
assert(*foo == '\0');
return;
}
static void
test_is_dir_f(const char *const homedir)
{
assert(is_dir_f("."));
char *foobar = join_paths(homedir, "foobar");
FILE *fp = fopen(foobar, "w");
assert(fp != NULL);
assert(fclose(fp) != EOF);
assert(!is_dir_f(foobar));
char *snafu = join_paths(homedir, "snafu");
assert(symlink(foobar, snafu) == 0);
assert(!is_dir_f(snafu));
assert(bsdutils_rm(foobar, false) == 0);
free(foobar);
assert(bsdutils_rm(snafu, false) == 0);
free(snafu);
char *home_link = join_paths(homedir, "home_link");
assert(symlink(homedir, home_link) == 0);
assert(is_dir_f(home_link) == false);
assert(bsdutils_rm(home_link, false) == 0);
free(home_link);
return;
}
static void
test_check_pathname_state(const char *const homedir)
{
char *foobar = join_paths(homedir, "foobar");
FILE *fp = fopen(foobar, "w");
assert(fp != NULL);
assert(fclose(fp) != EOF);
assert(check_pathname_state(foobar) == EEXIST);
char *snafu = join_paths(homedir, "snafu");
assert(symlink(foobar, snafu) == 0);
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) == 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) == EEXIST);
assert(remove(home_link) == 0);
free(home_link);
const char *dlink = "dangling_link";
if (check_pathname_state(dlink) == EEXIST)
assert(remove(dlink) == 0);
assert(symlink("dangler", dlink) == 0);
assert(check_pathname_state(dlink) == EEXIST);
assert(remove(dlink) == 0);
return;
}
static void
test_count_chars(void)
{
int n = 0;
char *greet = "/hello/world";
n = count_chars('/', greet);
assert(n == 2);
n = count_chars('l', greet);
assert(n == 3);
n = count_chars(' ', greet);
assert(n == 0);
return;
}
int
main()
{
char tmp[PATH_MAX];
int r = snprintf(tmp, PATH_MAX, "%s/%s", RMW_FAKE_HOME, "test_utils_dir");
assert(r < PATH_MAX);
const char *HOMEDIR = tmp;
test_isdotdir();
test_rmw_mkdir(HOMEDIR);
test_make_size_human_readable();
test_join_paths();
test_trim_char();
test_check_pathname_state(HOMEDIR);
test_is_dir_f(HOMEDIR);
char *str = "reserved = ; | / | ? | : | @ | & | = | + | $ \n\t\v \f\r";
char *escaped_path = escape_url(str);
fprintf(stderr, "'%s'\n", escaped_path);
assert(!strcmp
(escaped_path,
"reserved%20%20%20%20%3D%20%3B%20%7C%20/%20%7C%20%3F%20%7C%20%3A%20%7C%20%40%20%7C%20%26%20%7C%20%3D%20%7C%20%2B%20%7C%20%24%20%0A%09%0B%20%20%0C%0D"));
char *unescaped_path = unescape_url(escaped_path);
fprintf(stderr, "'%s'\n", unescaped_path);
assert(!strcmp(unescaped_path, str));
free(unescaped_path);
free(escaped_path);
test_count_chars();
return 0;
}
#endif