summaryrefslogtreecommitdiff
path: root/corebinutils/rm/rm.c
blob: d337229e3ee7c51a4f297b6ccc23615ee4ae132e (plain)
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
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
/*-
 * SPDX-License-Identifier: BSD-3-Clause
 *
 * Copyright (c) 1990, 1993, 1994
 *	The Regents of the University of California.  All rights reserved.
 * Copyright (c) 2026
 *	Project Tick. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <locale.h>
#include <stdbool.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

typedef struct {
	const char *progname;
	bool allow_directories;
	bool force;
	bool interactive;
	bool interactive_once;
	bool recursive;
	bool verbose;
	bool one_file_system;
	bool stdin_is_tty;
	int exit_status;
} options_t;

static void usage_rm(void);
static void usage_unlink(void);
static void set_error(options_t *options, const char *path, const char *message);
static void set_errno_error(options_t *options, const char *path);
static const char *program_basename(const char *path);
static bool is_root_operand(const char *path);
static bool is_dot_operand(const char *path);
static bool should_skip_operand(options_t *options, const char *path);
static bool prompt_yesno(const char *fmt, ...);
static bool prompt_once(options_t *options, char **paths);
static bool path_is_writable(const char *path);
static bool prompt_for_removal(const options_t *options, const char *display,
    const struct stat *st, bool is_directory);
static bool prompt_for_directory_descent(const options_t *options,
    const char *display);
static void print_removed(const options_t *options, const char *path);
static char *join_path(const char *base, const char *name);
static int remove_path_at(options_t *options, int parentfd, const char *name,
    const char *display, const struct stat *known_st, dev_t root_dev,
    bool top_level);
static int remove_path(options_t *options, const char *path);
static int remove_simple_path(options_t *options, const char *path);
static int run_unlink_mode(const char *path);

static void
usage_rm(void)
{
	fprintf(stderr, "%s\n", "usage: rm [-f | -i] [-dIPRrvWx] file ...");
	exit(2);
}

static void
usage_unlink(void)
{
	fprintf(stderr, "%s\n", "usage: unlink [--] file");
	exit(2);
}

static void
set_error(options_t *options, const char *path, const char *message)
{
	fprintf(stderr, "%s: %s: %s\n", options->progname, path, message);
	options->exit_status = 1;
}

static void
set_errno_error(options_t *options, const char *path)
{
	set_error(options, path, strerror(errno));
}

static const char *
program_basename(const char *path)
{
	const char *slash;

	slash = strrchr(path, '/');
	return slash == NULL ? path : slash + 1;
}

static bool
is_root_operand(const char *path)
{
	size_t i;

	if (path[0] == '\0') {
		return false;
	}

	for (i = 0; path[i] != '\0'; ++i) {
		if (path[i] != '/') {
			return false;
		}
	}

	return true;
}

static bool
is_dot_operand(const char *path)
{
	size_t len;
	size_t end;
	size_t start;

	len = strlen(path);
	if (len == 0) {
		return false;
	}

	end = len;
	while (end > 1 && path[end - 1] == '/') {
		--end;
	}
	start = end;
	while (start > 0 && path[start - 1] != '/') {
		--start;
	}

	if (end - start == 1 && path[start] == '.') {
		return true;
	}
	if (end - start == 2 && path[start] == '.' && path[start + 1] == '.') {
		return true;
	}

	return false;
}

static bool
should_skip_operand(options_t *options, const char *path)
{
	if (is_root_operand(path)) {
		fprintf(stderr, "%s: \"/\" may not be removed\n", options->progname);
		options->exit_status = 1;
		return true;
	}

	if (is_dot_operand(path)) {
		fprintf(stderr, "%s: \".\" and \"..\" may not be removed\n",
		    options->progname);
		options->exit_status = 1;
		return true;
	}

	return false;
}

static bool
prompt_yesno(const char *fmt, ...)
{
	char buffer[64];
	va_list ap;

	va_start(ap, fmt);
	vfprintf(stderr, fmt, ap);
	va_end(ap);
	fflush(stderr);

	if (fgets(buffer, sizeof(buffer), stdin) == NULL) {
		clearerr(stdin);
		return false;
	}

	return buffer[0] == 'y' || buffer[0] == 'Y';
}

static bool
prompt_once(options_t *options, char **paths)
{
	int existing_count;
	int directory_count;
	int file_count;
	int i;
	struct stat st;
	const char *single_directory;

	existing_count = 0;
	directory_count = 0;
	file_count = 0;
	single_directory = NULL;

	for (i = 0; paths[i] != NULL; ++i) {
		if (should_skip_operand(options, paths[i])) {
			continue;
		}
		if (lstat(paths[i], &st) != 0) {
			continue;
		}
		++existing_count;
		if (S_ISDIR(st.st_mode)) {
			++directory_count;
			single_directory = paths[i];
		} else {
			++file_count;
		}
	}

	if (directory_count > 0 && options->recursive) {
		if (directory_count == 1 && file_count == 0) {
			return prompt_yesno("recursively remove %s? ", single_directory);
		}
		if (directory_count == 1) {
			return prompt_yesno("recursively remove %s and %d file%s? ",
			    single_directory, file_count, file_count == 1 ? "" : "s");
		}
		return prompt_yesno("recursively remove %d dir%s and %d file%s? ",
		    directory_count, directory_count == 1 ? "" : "s", file_count,
		    file_count == 1 ? "" : "s");
	}

	if (existing_count > 3) {
		return prompt_yesno("remove %d files? ", existing_count);
	}

	return true;
}

static bool
path_is_writable(const char *path)
{
	return access(path, W_OK) == 0;
}

static bool
prompt_for_removal(const options_t *options, const char *display,
    const struct stat *st, bool is_directory)
{
	if (options->force) {
		return true;
	}

	if (options->interactive) {
		if (is_directory) {
			return prompt_yesno("remove directory %s? ", display);
		}
		return prompt_yesno("remove %s? ", display);
	}

	if (!options->stdin_is_tty || S_ISLNK(st->st_mode) || path_is_writable(display)) {
		return true;
	}

	if (is_directory) {
		return prompt_yesno("override write protection for directory %s? ",
		    display);
	}
	return prompt_yesno("override write protection for %s? ", display);
}

static bool
prompt_for_directory_descent(const options_t *options, const char *display)
{
	if (options->force) {
		return true;
	}

	if (options->interactive) {
		return prompt_yesno("descend into directory %s? ", display);
	}

	if (!options->stdin_is_tty || path_is_writable(display)) {
		return true;
	}

	return prompt_yesno("override write protection for directory %s? ",
	    display);
}

static void
print_removed(const options_t *options, const char *path)
{
	if (!options->verbose) {
		return;
	}
	printf("%s\n", path);
}

static char *
join_path(const char *base, const char *name)
{
	size_t base_len;
	size_t name_len;
	bool add_slash;
	char *result;

	base_len = strlen(base);
	while (base_len > 1 && base[base_len - 1] == '/') {
		--base_len;
	}
	name_len = strlen(name);
	add_slash = base_len > 0 && base[base_len - 1] != '/';

	result = malloc(base_len + (add_slash ? 1U : 0U) + name_len + 1U);
	if (result == NULL) {
		perror("rm: malloc");
		exit(1);
	}

	memcpy(result, base, base_len);
	if (add_slash) {
		result[base_len] = '/';
		memcpy(result + base_len + 1, name, name_len + 1);
	} else {
		memcpy(result + base_len, name, name_len + 1);
	}

	return result;
}

static int
remove_path_at(options_t *options, int parentfd, const char *name,
    const char *display, const struct stat *known_st, dev_t root_dev,
    bool top_level)
{
	struct stat st;
	const struct stat *stp;
	int dirfd;
	DIR *dir;
	struct dirent *entry;
	int child_status;

	if (known_st != NULL) {
		st = *known_st;
		stp = &st;
	} else {
		if (fstatat(parentfd, name, &st, AT_SYMLINK_NOFOLLOW) != 0) {
			if (options->force && errno == ENOENT) {
				return 0;
			}
			set_errno_error(options, display);
			return -1;
		}
		stp = &st;
	}

	if (S_ISDIR(stp->st_mode)) {
		if (!options->recursive) {
			if (!options->allow_directories) {
				set_error(options, display, "is a directory");
				return -1;
			}
			if (!prompt_for_removal(options, display, stp, true)) {
				return 0;
			}
			if (unlinkat(parentfd, name, AT_REMOVEDIR) != 0) {
				if (!(options->force && errno == ENOENT)) {
					set_errno_error(options, display);
					return -1;
				}
			} else {
				print_removed(options, display);
			}
			return 0;
		}

		if (!top_level && options->one_file_system && stp->st_dev != root_dev) {
			return 0;
		}

		if (!prompt_for_directory_descent(options, display)) {
			return 0;
		}

		dirfd = openat(parentfd, name, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
		if (dirfd >= 0) {
			dir = fdopendir(dirfd);
			if (dir == NULL) {
				close(dirfd);
				set_errno_error(options, display);
				return -1;
			}

			errno = 0;
			while ((entry = readdir(dir)) != NULL) {
				char *child_display;
				struct stat child_st;

				if (strcmp(entry->d_name, ".") == 0 ||
				    strcmp(entry->d_name, "..") == 0) {
					continue;
				}

				child_display = join_path(display, entry->d_name);
				if (fstatat(dirfd, entry->d_name, &child_st,
				    AT_SYMLINK_NOFOLLOW) != 0) {
					if (!(options->force && errno == ENOENT)) {
						set_errno_error(options, child_display);
					}
					free(child_display);
					continue;
				}
				child_status = remove_path_at(options, dirfd, entry->d_name,
				    child_display, &child_st, root_dev, false);
				(void)child_status;
				free(child_display);
			}

			if (errno != 0) {
				set_errno_error(options, display);
			}
			closedir(dir);
		}

		if (options->interactive &&
		    !prompt_for_removal(options, display, stp, true)) {
			return 0;
		}

		if (unlinkat(parentfd, name, AT_REMOVEDIR) != 0) {
			if (options->force && errno == ENOENT) {
				return 0;
			}
			set_errno_error(options, display);
			return -1;
		}
		print_removed(options, display);
		return 0;
	}

	if (!prompt_for_removal(options, display, stp, false)) {
		return 0;
	}

	if (unlinkat(parentfd, name, 0) != 0) {
		if (!(options->force && errno == ENOENT)) {
			set_errno_error(options, display);
			return -1;
		}
		return 0;
	}

	print_removed(options, display);
	return 0;
}

static int
remove_path(options_t *options, const char *path)
{
	struct stat st;

	if (lstat(path, &st) != 0) {
		if (options->force && errno == ENOENT) {
			return 0;
		}
		set_errno_error(options, path);
		return -1;
	}

	return remove_path_at(options, AT_FDCWD, path, path, &st, st.st_dev, true);
}

static int
remove_simple_path(options_t *options, const char *path)
{
	struct stat st;

	if (lstat(path, &st) != 0) {
		if (options->force && errno == ENOENT) {
			return 0;
		}
		set_errno_error(options, path);
		return -1;
	}

	if (S_ISDIR(st.st_mode) && !options->allow_directories) {
		set_error(options, path, "is a directory");
		return -1;
	}

	if (!prompt_for_removal(options, path, &st, S_ISDIR(st.st_mode))) {
		return 0;
	}

	if (S_ISDIR(st.st_mode)) {
		if (unlinkat(AT_FDCWD, path, AT_REMOVEDIR) != 0) {
			if (!(options->force && errno == ENOENT)) {
				set_errno_error(options, path);
				return -1;
			}
		} else {
			print_removed(options, path);
		}
		return 0;
	}

	if (unlinkat(AT_FDCWD, path, 0) != 0) {
		if (!(options->force && errno == ENOENT)) {
			set_errno_error(options, path);
			return -1;
		}
		return 0;
	}

	print_removed(options, path);
	return 0;
}

static int
run_unlink_mode(const char *path)
{
	struct stat st;

	if (lstat(path, &st) != 0) {
		fprintf(stderr, "unlink: %s: %s\n", path, strerror(errno));
		return 1;
	}
	if (S_ISDIR(st.st_mode)) {
		fprintf(stderr, "unlink: %s: is a directory\n", path);
		return 1;
	}
	if (unlink(path) != 0) {
		fprintf(stderr, "unlink: %s: %s\n", path, strerror(errno));
		return 1;
	}
	return 0;
}

int
main(int argc, char *argv[])
{
	options_t options;
	int ch;
	int i;

	memset(&options, 0, sizeof(options));
	options.progname = program_basename(argv[0]);

	(void)setlocale(LC_ALL, "");

	if (strcmp(options.progname, "unlink") == 0) {
		if (argc == 2) {
			return run_unlink_mode(argv[1]);
		}
		if (argc == 3 && strcmp(argv[1], "--") == 0) {
			return run_unlink_mode(argv[2]);
		}
		usage_unlink();
	}

	while ((ch = getopt(argc, argv, "dfiIPRrvWx")) != -1) {
		switch (ch) {
		case 'd':
			options.allow_directories = true;
			break;
		case 'f':
			options.force = true;
			options.interactive = false;
			break;
		case 'i':
			options.interactive = true;
			options.force = false;
			break;
		case 'I':
			options.interactive_once = true;
			break;
		case 'P':
			break;
		case 'R':
		case 'r':
			options.recursive = true;
			options.allow_directories = true;
			break;
		case 'v':
			options.verbose = true;
			break;
		case 'W':
			fprintf(stderr, "%s: -W is unsupported on Linux: whiteout undelete is unavailable\n",
			    options.progname);
			return 1;
		case 'x':
			options.one_file_system = true;
			break;
		default:
			usage_rm();
		}
	}

	argc -= optind;
	argv += optind;

	if (argc < 1) {
		if (options.force) {
			return 0;
		}
		usage_rm();
	}

	options.stdin_is_tty = isatty(STDIN_FILENO) == 1;

	if (options.interactive_once && !prompt_once(&options, argv)) {
		return 1;
	}

	for (i = 0; argv[i] != NULL; ++i) {
		if (should_skip_operand(&options, argv[i])) {
			continue;
		}
		if (options.recursive) {
			(void)remove_path(&options, argv[i]);
		} else {
			(void)remove_simple_path(&options, argv[i]);
		}
	}

	return options.exit_status;
}