OpenCoverage

rmdir.c

Absolute File Name:/home/opencoverage/opencoverage/guest-scripts/coreutils/src/src/rmdir.c
Source codeSwitch to Preprocessed file
LineSourceCount
1/* rmdir -- remove directories-
2-
3 Copyright (C) 1990-2018 Free Software Foundation, Inc.-
4-
5 This program is free software: you can redistribute it and/or modify-
6 it under the terms of the GNU General Public License as published by-
7 the Free Software Foundation, either version 3 of the License, or-
8 (at your option) any later version.-
9-
10 This program is distributed in the hope that it will be useful,-
11 but WITHOUT ANY WARRANTY; without even the implied warranty of-
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the-
13 GNU General Public License for more details.-
14-
15 You should have received a copy of the GNU General Public License-
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */-
17-
18/* Options:-
19 -p, --parent Remove any parent dirs that are explicitly mentioned-
20 in an argument, if they become empty after the-
21 argument file is removed.-
22-
23 David MacKenzie <djm@ai.mit.edu> */-
24-
25#include <config.h>-
26#include <stdio.h>-
27#include <getopt.h>-
28#include <sys/types.h>-
29-
30#include "system.h"-
31#include "error.h"-
32#include "prog-fprintf.h"-
33-
34/* The official name of this program (e.g., no 'g' prefix). */-
35#define PROGRAM_NAME "rmdir"-
36-
37#define AUTHORS proper_name ("David MacKenzie")-
38-
39/* If true, remove empty parent directories. */-
40static bool remove_empty_parents;-
41-
42/* If true, don't treat failure to remove a nonempty directory-
43 as an error. */-
44static bool ignore_fail_on_non_empty;-
45-
46/* If true, output a diagnostic for every directory processed. */-
47static bool verbose;-
48-
49/* For long options that have no equivalent short option, use a-
50 non-character as a pseudo short option, starting with CHAR_MAX + 1. */-
51enum-
52{-
53 IGNORE_FAIL_ON_NON_EMPTY_OPTION = CHAR_MAX + 1-
54};-
55-
56static struct option const longopts[] =-
57{-
58 /* Don't name this '--force' because it's not close enough in meaning-
59 to e.g. rm's -f option. */-
60 {"ignore-fail-on-non-empty", no_argument, NULL,-
61 IGNORE_FAIL_ON_NON_EMPTY_OPTION},-
62-
63 {"path", no_argument, NULL, 'p'}, /* Deprecated. */-
64 {"parents", no_argument, NULL, 'p'},-
65 {"verbose", no_argument, NULL, 'v'},-
66 {GETOPT_HELP_OPTION_DECL},-
67 {GETOPT_VERSION_OPTION_DECL},-
68 {NULL, 0, NULL, 0}-
69};-
70-
71/* Return true if ERROR_NUMBER is one of the values associated-
72 with a failed rmdir due to non-empty target directory. */-
73static bool-
74errno_rmdir_non_empty (int error_number)-
75{-
76 return error_number == ENOTEMPTY || error_number == EEXIST;
executed 1 time by 1 test: return error_number == 39 || error_number == 17 ;
Executed by:
  • rmdir
1
77}-
78-
79/* Return true if when rmdir fails with errno == ERROR_NUMBER-
80 the directory may be empty. */-
81static bool-
82errno_may_be_empty (int error_number)-
83{-
84 switch (error_number)-
85 {-
86 case EACCES:
never executed: case 13 :
0
87 case EPERM:
never executed: case 1 :
0
88 case EROFS:
never executed: case 30 :
0
89 case EEXIST:
never executed: case 17 :
0
90 case EBUSY:
never executed: case 16 :
0
91 return true;
never executed: return 1 ;
0
92 default:
never executed: default:
0
93 return false;
never executed: return 0 ;
0
94 }-
95}-
96-
97/* Return true if an rmdir failure with errno == error_number-
98 for DIR is ignorable. */-
99static bool-
100ignorable_failure (int error_number, char const *dir)-
101{-
102 return (ignore_fail_on_non_empty
executed 3 times by 1 test: return (ignore_fail_on_non_empty && (errno_rmdir_non_empty (error_number) || (errno_may_be_empty (error_number) && is_empty_dir ( -100 , dir))));
Executed by:
  • rmdir
3
103 && (errno_rmdir_non_empty (error_number)
executed 3 times by 1 test: return (ignore_fail_on_non_empty && (errno_rmdir_non_empty (error_number) || (errno_may_be_empty (error_number) && is_empty_dir ( -100 , dir))));
Executed by:
  • rmdir
3
104 || (errno_may_be_empty (error_number)
executed 3 times by 1 test: return (ignore_fail_on_non_empty && (errno_rmdir_non_empty (error_number) || (errno_may_be_empty (error_number) && is_empty_dir ( -100 , dir))));
Executed by:
  • rmdir
3
105 && is_empty_dir (AT_FDCWD, dir))));
executed 3 times by 1 test: return (ignore_fail_on_non_empty && (errno_rmdir_non_empty (error_number) || (errno_may_be_empty (error_number) && is_empty_dir ( -100 , dir))));
Executed by:
  • rmdir
3
106}-
107-
108/* Remove any empty parent directories of DIR.-
109 If DIR contains slash characters, at least one of them-
110 (beginning with the rightmost) is replaced with a NUL byte.-
111 Return true if successful. */-
112-
113static bool-
114remove_parents (char *dir)-
115{-
116 char *slash;-
117 bool ok = true;-
118-
119 strip_trailing_slashes (dir);-
120 while (1)-
121 {-
122 slash = strrchr (dir, '/');-
123 if (slash == NULL)
slash == ((void *)0)Description
TRUEevaluated 1 time by 1 test
Evaluated by:
  • rmdir
FALSEevaluated 3 times by 1 test
Evaluated by:
  • rmdir
1-3
124 break;
executed 1 time by 1 test: break;
Executed by:
  • rmdir
1
125 /* Remove any characters after the slash, skipping any extra-
126 slashes in a row. */-
127 while (slash > dir && *slash == '/')
slash > dirDescription
TRUEevaluated 6 times by 1 test
Evaluated by:
  • rmdir
FALSEnever evaluated
*slash == '/'Description
TRUEevaluated 3 times by 1 test
Evaluated by:
  • rmdir
FALSEevaluated 3 times by 1 test
Evaluated by:
  • rmdir
0-6
128 --slash;
executed 3 times by 1 test: --slash;
Executed by:
  • rmdir
3
129 slash[1] = 0;-
130-
131 /* Give a diagnostic for each attempted removal if --verbose. */-
132 if (verbose)
verboseDescription
TRUEnever evaluated
FALSEevaluated 3 times by 1 test
Evaluated by:
  • rmdir
0-3
133 prog_fprintf (stdout, _("removing directory, %s"), quoteaf (dir));
never executed: prog_fprintf ( stdout , dcgettext (((void *)0), "removing directory, %s" , 5) , quotearg_style (shell_escape_always_quoting_style, dir));
0
134-
135 ok = (rmdir (dir) == 0);-
136-
137 if (!ok)
!okDescription
TRUEevaluated 2 times by 1 test
Evaluated by:
  • rmdir
FALSEevaluated 1 time by 1 test
Evaluated by:
  • rmdir
1-2
138 {-
139 /* Stop quietly if --ignore-fail-on-non-empty. */-
140 if (ignorable_failure (errno, dir))
ignorable_fail...ion ()) , dir)Description
TRUEevaluated 1 time by 1 test
Evaluated by:
  • rmdir
FALSEevaluated 1 time by 1 test
Evaluated by:
  • rmdir
1
141 {-
142 ok = true;-
143 }
executed 1 time by 1 test: end of block
Executed by:
  • rmdir
1
144 else-
145 {-
146 /* Barring race conditions, DIR is expected to be a directory. */-
147 error (0, errno, _("failed to remove directory %s"),-
148 quoteaf (dir));-
149 }
executed 1 time by 1 test: end of block
Executed by:
  • rmdir
1
150 break;
executed 2 times by 1 test: break;
Executed by:
  • rmdir
2
151 }-
152 }
executed 1 time by 1 test: end of block
Executed by:
  • rmdir
1
153 return ok;
executed 3 times by 1 test: return ok;
Executed by:
  • rmdir
3
154}-
155-
156void-
157usage (int status)-
158{-
159 if (status != EXIT_SUCCESS)
status != 0Description
TRUEevaluated 3 times by 1 test
Evaluated by:
  • rmdir
FALSEevaluated 8 times by 1 test
Evaluated by:
  • rmdir
3-8
160 emit_try_help ();
executed 3 times by 1 test: end of block
Executed by:
  • rmdir
3
161 else-
162 {-
163 printf (_("Usage: %s [OPTION]... DIRECTORY...\n"), program_name);-
164 fputs (_("\-
165Remove the DIRECTORY(ies), if they are empty.\n\-
166\n\-
167 --ignore-fail-on-non-empty\n\-
168 ignore each failure that is solely because a directory\n\-
169 is non-empty\n\-
170"), stdout);-
171 fputs (_("\-
172 -p, --parents remove DIRECTORY and its ancestors; e.g., 'rmdir -p a/b/c' is\-
173\n\-
174 similar to 'rmdir a/b/c a/b a'\n\-
175 -v, --verbose output a diagnostic for every directory processed\n\-
176"), stdout);-
177 fputs (HELP_OPTION_DESCRIPTION, stdout);-
178 fputs (VERSION_OPTION_DESCRIPTION, stdout);-
179 emit_ancillary_info (PROGRAM_NAME);-
180 }
executed 8 times by 1 test: end of block
Executed by:
  • rmdir
8
181 exit (status);
executed 11 times by 1 test: exit (status);
Executed by:
  • rmdir
11
182}-
183-
184int-
185main (int argc, char **argv)-
186{-
187 bool ok = true;-
188 int optc;-
189-
190 initialize_main (&argc, &argv);-
191 set_program_name (argv[0]);-
192 setlocale (LC_ALL, "");-
193 bindtextdomain (PACKAGE, LOCALEDIR);-
194 textdomain (PACKAGE);-
195-
196 atexit (close_stdout);-
197-
198 remove_empty_parents = false;-
199-
200 while ((optc = getopt_long (argc, argv, "pv", longopts, NULL)) != -1)
(optc = getopt... *)0) )) != -1Description
TRUEevaluated 26 times by 1 test
Evaluated by:
  • rmdir
FALSEevaluated 16 times by 1 test
Evaluated by:
  • rmdir
16-26
201 {-
202 switch (optc)-
203 {-
204 case 'p':
executed 5 times by 1 test: case 'p':
Executed by:
  • rmdir
5
205 remove_empty_parents = true;-
206 break;
executed 5 times by 1 test: break;
Executed by:
  • rmdir
5
207 case IGNORE_FAIL_ON_NON_EMPTY_OPTION:
executed 2 times by 1 test: case IGNORE_FAIL_ON_NON_EMPTY_OPTION:
Executed by:
  • rmdir
2
208 ignore_fail_on_non_empty = true;-
209 break;
executed 2 times by 1 test: break;
Executed by:
  • rmdir
2
210 case 'v':
executed 2 times by 1 test: case 'v':
Executed by:
  • rmdir
2
211 verbose = true;-
212 break;
executed 2 times by 1 test: break;
Executed by:
  • rmdir
2
213 case_GETOPT_HELP_CHAR;
never executed: break;
executed 8 times by 1 test: case GETOPT_HELP_CHAR:
Executed by:
  • rmdir
0-8
214 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
executed 6 times by 1 test: exit ( 0 );
Executed by:
  • rmdir
never executed: break;
executed 6 times by 1 test: case GETOPT_VERSION_CHAR:
Executed by:
  • rmdir
0-6
215 default:
executed 3 times by 1 test: default:
Executed by:
  • rmdir
3
216 usage (EXIT_FAILURE);-
217 }
never executed: end of block
0
218 }-
219-
220 if (optind == argc)
optind == argcDescription
TRUEnever evaluated
FALSEevaluated 16 times by 1 test
Evaluated by:
  • rmdir
0-16
221 {-
222 error (0, 0, _("missing operand"));-
223 usage (EXIT_FAILURE);-
224 }
never executed: end of block
0
225-
226 for (; optind < argc; ++optind)
optind < argcDescription
TRUEevaluated 17 times by 1 test
Evaluated by:
  • rmdir
FALSEevaluated 16 times by 1 test
Evaluated by:
  • rmdir
16-17
227 {-
228 char *dir = argv[optind];-
229-
230 /* Give a diagnostic for each attempted removal if --verbose. */-
231 if (verbose)
verboseDescription
TRUEnever evaluated
FALSEevaluated 17 times by 1 test
Evaluated by:
  • rmdir
0-17
232 prog_fprintf (stdout, _("removing directory, %s"), quoteaf (dir));
never executed: prog_fprintf ( stdout , dcgettext (((void *)0), "removing directory, %s" , 5) , quotearg_style (shell_escape_always_quoting_style, dir));
0
233-
234 if (rmdir (dir) != 0)
rmdir (dir) != 0Description
TRUEevaluated 1 time by 1 test
Evaluated by:
  • rmdir
FALSEevaluated 16 times by 1 test
Evaluated by:
  • rmdir
1-16
235 {-
236 if (ignorable_failure (errno, dir))
ignorable_fail...ion ()) , dir)Description
TRUEnever evaluated
FALSEevaluated 1 time by 1 test
Evaluated by:
  • rmdir
0-1
237 continue;
never executed: continue;
0
238-
239 /* Here, the diagnostic is less precise, since we have no idea-
240 whether DIR is a directory. */-
241 error (0, errno, _("failed to remove %s"), quoteaf (dir));-
242 ok = false;-
243 }
executed 1 time by 1 test: end of block
Executed by:
  • rmdir
1
244 else if (remove_empty_parents)
remove_empty_parentsDescription
TRUEevaluated 3 times by 1 test
Evaluated by:
  • rmdir
FALSEevaluated 13 times by 1 test
Evaluated by:
  • rmdir
3-13
245 {-
246 ok &= remove_parents (dir);-
247 }
executed 3 times by 1 test: end of block
Executed by:
  • rmdir
3
248 }
executed 17 times by 1 test: end of block
Executed by:
  • rmdir
17
249-
250 return ok ? EXIT_SUCCESS : EXIT_FAILURE;
executed 16 times by 1 test: return ok ? 0 : 1 ;
Executed by:
  • rmdir
16
251}-
Source codeSwitch to Preprocessed file

Generated by Squish Coco 4.1.2