1 | /* $Id: RTPathRmCmd.cpp 82968 2020-02-04 10:35:17Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - RM Command.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2013-2020 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*********************************************************************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *********************************************************************************************************************************/
|
---|
31 | #include <iprt/path.h>
|
---|
32 |
|
---|
33 | #include <iprt/buildconfig.h>
|
---|
34 | #include <iprt/ctype.h>
|
---|
35 | #include <iprt/err.h>
|
---|
36 | #include <iprt/file.h>
|
---|
37 | #include <iprt/dir.h>
|
---|
38 | #include <iprt/getopt.h>
|
---|
39 | #include <iprt/initterm.h>
|
---|
40 | #include <iprt/message.h>
|
---|
41 | #include <iprt/stream.h>
|
---|
42 | #include <iprt/string.h>
|
---|
43 | #include <iprt/symlink.h>
|
---|
44 |
|
---|
45 |
|
---|
46 | /*********************************************************************************************************************************
|
---|
47 | * Defined Constants And Macros *
|
---|
48 | *********************************************************************************************************************************/
|
---|
49 | #define RTPATHRMCMD_OPT_INTERACTIVE 1000
|
---|
50 | #define RTPATHRMCMD_OPT_ONE_FILE_SYSTEM 1001
|
---|
51 | #define RTPATHRMCMD_OPT_PRESERVE_ROOT 1002
|
---|
52 | #define RTPATHRMCMD_OPT_NO_PRESERVE_ROOT 1003
|
---|
53 | #define RTPATHRMCMD_OPT_MACHINE_READABLE 1004
|
---|
54 |
|
---|
55 | /** The max directory entry size. */
|
---|
56 | #define RTPATHRM_DIR_MAX_ENTRY_SIZE (sizeof(RTDIRENTRYEX) + 4096)
|
---|
57 |
|
---|
58 |
|
---|
59 | /*********************************************************************************************************************************
|
---|
60 | * Structures and Typedefs *
|
---|
61 | *********************************************************************************************************************************/
|
---|
62 | /** Interactive option. */
|
---|
63 | typedef enum
|
---|
64 | {
|
---|
65 | RTPATHRMCMDINTERACTIVE_NONE = 1,
|
---|
66 | RTPATHRMCMDINTERACTIVE_ALL,
|
---|
67 | RTPATHRMCMDINTERACTIVE_ONCE
|
---|
68 | /** @todo possible that we should by default prompt if removing read-only
|
---|
69 | * files or files owned by someone else. We currently don't. */
|
---|
70 | } RTPATHRMCMDINTERACTIVE;
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * IPRT rm option structure.
|
---|
74 | */
|
---|
75 | typedef struct RTPATHRMCMDOPTS
|
---|
76 | {
|
---|
77 | /** Whether to delete recursively. */
|
---|
78 | bool fRecursive;
|
---|
79 | /** Whether to delete directories as well as other kinds of files. */
|
---|
80 | bool fDirsAndOther;
|
---|
81 | /** Whether to remove files without prompting and ignoring non-existing
|
---|
82 | * files. */
|
---|
83 | bool fForce;
|
---|
84 | /** Machine readable output. */
|
---|
85 | bool fMachineReadable;
|
---|
86 | /** Don't try remove root ('/') if set, otherwise don't treat root specially. */
|
---|
87 | bool fPreserveRoot;
|
---|
88 | /** Whether to keep to one file system. */
|
---|
89 | bool fOneFileSystem;
|
---|
90 | /** Whether to safely delete files (overwrite 3x before unlinking). */
|
---|
91 | bool fSafeDelete;
|
---|
92 | /** Whether to be verbose about the operation. */
|
---|
93 | bool fVerbose;
|
---|
94 | /** The interactive setting. */
|
---|
95 | RTPATHRMCMDINTERACTIVE enmInteractive;
|
---|
96 | } RTPATHRMCMDOPTS;
|
---|
97 | /** Pointer to the IPRT rm options. */
|
---|
98 | typedef RTPATHRMCMDOPTS *PRTPATHRMCMDOPTS;
|
---|
99 |
|
---|
100 |
|
---|
101 | /*********************************************************************************************************************************
|
---|
102 | * Global Variables *
|
---|
103 | *********************************************************************************************************************************/
|
---|
104 | /** A bunch of zeros. */
|
---|
105 | static uint8_t const g_abZeros[16384] = { 0 };
|
---|
106 | /** A bunch of 0xFF bytes. (lazy init) */
|
---|
107 | static uint8_t g_ab0xFF[16384];
|
---|
108 |
|
---|
109 |
|
---|
110 | static void rtPathRmVerbose(PRTPATHRMCMDOPTS pOpts, const char *pszPath)
|
---|
111 | {
|
---|
112 | if (!pOpts->fMachineReadable)
|
---|
113 | RTPrintf("%s\n", pszPath);
|
---|
114 | }
|
---|
115 |
|
---|
116 |
|
---|
117 | static int rtPathRmError(PRTPATHRMCMDOPTS pOpts, const char *pszPath, int rc,
|
---|
118 | const char *pszFormat, ...)
|
---|
119 | {
|
---|
120 | if (pOpts->fMachineReadable)
|
---|
121 | RTPrintf("fname=%s%crc=%d%c", pszPath, 0, rc, 0);
|
---|
122 | else
|
---|
123 | {
|
---|
124 | va_list va;
|
---|
125 | va_start(va, pszFormat);
|
---|
126 | RTMsgErrorV(pszFormat, va);
|
---|
127 | va_end(va);
|
---|
128 | }
|
---|
129 | return rc;
|
---|
130 | }
|
---|
131 |
|
---|
132 |
|
---|
133 | /**
|
---|
134 | * Worker that removes a symbolic link.
|
---|
135 | *
|
---|
136 | * @returns IPRT status code, errors go via rtPathRmError.
|
---|
137 | * @param pOpts The RM options.
|
---|
138 | * @param pszPath The path to the symbolic link.
|
---|
139 | */
|
---|
140 | static int rtPathRmOneSymlink(PRTPATHRMCMDOPTS pOpts, const char *pszPath)
|
---|
141 | {
|
---|
142 | if (pOpts->fVerbose)
|
---|
143 | rtPathRmVerbose(pOpts, pszPath);
|
---|
144 | int rc = RTSymlinkDelete(pszPath, 0);
|
---|
145 | if (RT_FAILURE(rc))
|
---|
146 | return rtPathRmError(pOpts, pszPath, rc, "Error removing symbolic link '%s': %Rrc\n", pszPath, rc);
|
---|
147 | return rc;
|
---|
148 | }
|
---|
149 |
|
---|
150 |
|
---|
151 | /**
|
---|
152 | * Worker that removes a file.
|
---|
153 | *
|
---|
154 | * Currently used to delete both regular and special files.
|
---|
155 | *
|
---|
156 | * @returns IPRT status code, errors go via rtPathRmError.
|
---|
157 | * @param pOpts The RM options.
|
---|
158 | * @param pszPath The path to the file.
|
---|
159 | * @param pObjInfo The FS object info for the file.
|
---|
160 | */
|
---|
161 | static int rtPathRmOneFile(PRTPATHRMCMDOPTS pOpts, const char *pszPath, PRTFSOBJINFO pObjInfo)
|
---|
162 | {
|
---|
163 | int rc;
|
---|
164 | if (pOpts->fVerbose)
|
---|
165 | rtPathRmVerbose(pOpts, pszPath);
|
---|
166 |
|
---|
167 | /*
|
---|
168 | * Wipe the file if requested and possible.
|
---|
169 | */
|
---|
170 | if (pOpts->fSafeDelete && RTFS_IS_FILE(pObjInfo->Attr.fMode))
|
---|
171 | {
|
---|
172 | /* Lazy init of the 0xff buffer. */
|
---|
173 | if (g_ab0xFF[0] != 0xff || g_ab0xFF[sizeof(g_ab0xFF) - 1] != 0xff)
|
---|
174 | memset(g_ab0xFF, 0xff, sizeof(g_ab0xFF));
|
---|
175 |
|
---|
176 | RTFILE hFile;
|
---|
177 | rc = RTFileOpen(&hFile, pszPath, RTFILE_O_WRITE);
|
---|
178 | if (RT_FAILURE(rc))
|
---|
179 | return rtPathRmError(pOpts, pszPath, rc, "Opening '%s' for overwriting: %Rrc\n", pszPath, rc);
|
---|
180 |
|
---|
181 | for (unsigned iPass = 0; iPass < 3; iPass++)
|
---|
182 | {
|
---|
183 | uint8_t const *pabFiller = iPass == 1 ? g_abZeros : g_ab0xFF;
|
---|
184 | size_t const cbFiller = iPass == 1 ? sizeof(g_abZeros) : sizeof(g_ab0xFF);
|
---|
185 |
|
---|
186 | rc = RTFileSeek(hFile, 0, RTFILE_SEEK_BEGIN, NULL);
|
---|
187 | if (RT_FAILURE(rc))
|
---|
188 | {
|
---|
189 | rc = rtPathRmError(pOpts, pszPath, rc, "Error seeking to start of '%s': %Rrc\n", pszPath, rc);
|
---|
190 | break;
|
---|
191 | }
|
---|
192 | for (RTFOFF cbLeft = pObjInfo->cbObject; cbLeft > 0; cbLeft -= cbFiller)
|
---|
193 | {
|
---|
194 | size_t cbToWrite = cbFiller;
|
---|
195 | if (cbLeft < (RTFOFF)cbToWrite)
|
---|
196 | cbToWrite = (size_t)cbLeft;
|
---|
197 | rc = RTFileWrite(hFile, pabFiller, cbToWrite, NULL);
|
---|
198 | if (RT_FAILURE(rc))
|
---|
199 | {
|
---|
200 | rc = rtPathRmError(pOpts, pszPath, rc, "Error writing to '%s': %Rrc\n", pszPath, rc);
|
---|
201 | break;
|
---|
202 | }
|
---|
203 | }
|
---|
204 | }
|
---|
205 |
|
---|
206 | int rc2 = RTFileClose(hFile);
|
---|
207 | if (RT_FAILURE(rc2) && RT_SUCCESS(rc))
|
---|
208 | return rtPathRmError(pOpts, pszPath, rc2, "Closing '%s' failed: %Rrc\n", pszPath, rc);
|
---|
209 | if (RT_FAILURE(rc))
|
---|
210 | return rc;
|
---|
211 | }
|
---|
212 |
|
---|
213 | /*
|
---|
214 | * Remove the file.
|
---|
215 | */
|
---|
216 | rc = RTFileDelete(pszPath);
|
---|
217 | if (RT_FAILURE(rc))
|
---|
218 | return rtPathRmError(pOpts, pszPath, rc,
|
---|
219 | RTFS_IS_FILE(pObjInfo->Attr.fMode)
|
---|
220 | ? "Error removing regular file '%s': %Rrc\n"
|
---|
221 | : "Error removing special file '%s': %Rrc\n",
|
---|
222 | pszPath, rc);
|
---|
223 | return rc;
|
---|
224 | }
|
---|
225 |
|
---|
226 |
|
---|
227 | /**
|
---|
228 | * Deletes one directory (if it's empty).
|
---|
229 | *
|
---|
230 | * @returns IPRT status code, errors go via rtPathRmError.
|
---|
231 | * @param pOpts The RM options.
|
---|
232 | * @param pszPath The path to the directory.
|
---|
233 | */
|
---|
234 | static int rtPathRmOneDir(PRTPATHRMCMDOPTS pOpts, const char *pszPath)
|
---|
235 | {
|
---|
236 | if (pOpts->fVerbose)
|
---|
237 | rtPathRmVerbose(pOpts, pszPath);
|
---|
238 |
|
---|
239 | int rc = RTDirRemove(pszPath);
|
---|
240 | if (RT_FAILURE(rc))
|
---|
241 | return rtPathRmError(pOpts, pszPath, rc, "Error removing directory '%s': %Rrc", pszPath, rc);
|
---|
242 | return rc;
|
---|
243 | }
|
---|
244 |
|
---|
245 |
|
---|
246 | /**
|
---|
247 | * Recursively delete a directory.
|
---|
248 | *
|
---|
249 | * @returns IPRT status code, errors go via rtPathRmError.
|
---|
250 | * @param pOpts The RM options.
|
---|
251 | * @param pszPath Pointer to a writable buffer holding the path to
|
---|
252 | * the directory.
|
---|
253 | * @param cchPath The length of the path (avoid strlen).
|
---|
254 | * @param pDirEntry Pointer to a directory entry buffer that is
|
---|
255 | * RTPATHRM_DIR_MAX_ENTRY_SIZE bytes big.
|
---|
256 | */
|
---|
257 | static int rtPathRmRecursive(PRTPATHRMCMDOPTS pOpts, char *pszPath, size_t cchPath, PRTDIRENTRYEX pDirEntry)
|
---|
258 | {
|
---|
259 | /*
|
---|
260 | * Make sure the path ends with a slash.
|
---|
261 | */
|
---|
262 | if (!cchPath || !RTPATH_IS_SLASH(pszPath[cchPath - 1]))
|
---|
263 | {
|
---|
264 | if (cchPath + 1 >= RTPATH_MAX)
|
---|
265 | return rtPathRmError(pOpts, pszPath, VERR_BUFFER_OVERFLOW, "Buffer overflow fixing up '%s'.\n", pszPath);
|
---|
266 | pszPath[cchPath++] = RTPATH_SLASH;
|
---|
267 | pszPath[cchPath] = '\0';
|
---|
268 | }
|
---|
269 |
|
---|
270 | /*
|
---|
271 | * Traverse the directory.
|
---|
272 | */
|
---|
273 | RTDIR hDir;
|
---|
274 | int rc = RTDirOpen(&hDir, pszPath);
|
---|
275 | if (RT_FAILURE(rc))
|
---|
276 | return rtPathRmError(pOpts, pszPath, rc, "Error opening directory '%s': %Rrc", pszPath, rc);
|
---|
277 | int rcRet = VINF_SUCCESS;
|
---|
278 | for (;;)
|
---|
279 | {
|
---|
280 | /*
|
---|
281 | * Read the next entry, constructing an full path for it.
|
---|
282 | */
|
---|
283 | size_t cbEntry = RTPATHRM_DIR_MAX_ENTRY_SIZE;
|
---|
284 | rc = RTDirReadEx(hDir, pDirEntry, &cbEntry, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK);
|
---|
285 | if (rc == VERR_NO_MORE_FILES)
|
---|
286 | {
|
---|
287 | /*
|
---|
288 | * Reached the end of the directory.
|
---|
289 | */
|
---|
290 | pszPath[cchPath] = '\0';
|
---|
291 | rc = RTDirClose(hDir);
|
---|
292 | if (RT_FAILURE(rc))
|
---|
293 | return rtPathRmError(pOpts, pszPath, rc, "Error closing directory '%s': %Rrc", pszPath, rc);
|
---|
294 |
|
---|
295 | /* Delete the directory. */
|
---|
296 | int rc2 = rtPathRmOneDir(pOpts, pszPath);
|
---|
297 | if (RT_FAILURE(rc2) && RT_SUCCESS(rcRet))
|
---|
298 | return rc2;
|
---|
299 | return rcRet;
|
---|
300 | }
|
---|
301 |
|
---|
302 | if (RT_FAILURE(rc))
|
---|
303 | {
|
---|
304 | rc = rtPathRmError(pOpts, pszPath, rc, "Error reading directory '%s': %Rrc", pszPath, rc);
|
---|
305 | break;
|
---|
306 | }
|
---|
307 |
|
---|
308 | /* Skip '.' and '..'. */
|
---|
309 | if ( pDirEntry->szName[0] == '.'
|
---|
310 | && ( pDirEntry->cbName == 1
|
---|
311 | || ( pDirEntry->cbName == 2
|
---|
312 | && pDirEntry->szName[1] == '.')))
|
---|
313 | continue;
|
---|
314 |
|
---|
315 | /* Construct full path. */
|
---|
316 | if (cchPath + pDirEntry->cbName >= RTPATH_MAX)
|
---|
317 | {
|
---|
318 | pszPath[cchPath] = '\0';
|
---|
319 | rc = rtPathRmError(pOpts, pszPath, VERR_BUFFER_OVERFLOW, "Path buffer overflow in directory '%s'.", pszPath);
|
---|
320 | break;
|
---|
321 | }
|
---|
322 | memcpy(pszPath + cchPath, pDirEntry->szName, pDirEntry->cbName + 1);
|
---|
323 |
|
---|
324 | /*
|
---|
325 | * Take action according to the type.
|
---|
326 | */
|
---|
327 | switch (pDirEntry->Info.Attr.fMode & RTFS_TYPE_MASK)
|
---|
328 | {
|
---|
329 | case RTFS_TYPE_FILE:
|
---|
330 | rc = rtPathRmOneFile(pOpts, pszPath, &pDirEntry->Info);
|
---|
331 | break;
|
---|
332 |
|
---|
333 | case RTFS_TYPE_DIRECTORY:
|
---|
334 | rc = rtPathRmRecursive(pOpts, pszPath, cchPath + pDirEntry->cbName, pDirEntry);
|
---|
335 | break;
|
---|
336 |
|
---|
337 | case RTFS_TYPE_SYMLINK:
|
---|
338 | rc = rtPathRmOneSymlink(pOpts, pszPath);
|
---|
339 | break;
|
---|
340 |
|
---|
341 | case RTFS_TYPE_FIFO:
|
---|
342 | case RTFS_TYPE_DEV_CHAR:
|
---|
343 | case RTFS_TYPE_DEV_BLOCK:
|
---|
344 | case RTFS_TYPE_SOCKET:
|
---|
345 | rc = rtPathRmOneFile(pOpts, pszPath, &pDirEntry->Info);
|
---|
346 | break;
|
---|
347 |
|
---|
348 | case RTFS_TYPE_WHITEOUT:
|
---|
349 | default:
|
---|
350 | rc = rtPathRmError(pOpts, pszPath, VERR_UNEXPECTED_FS_OBJ_TYPE,
|
---|
351 | "Object '%s' has an unknown file type: %o\n",
|
---|
352 | pszPath, pDirEntry->Info.Attr.fMode & RTFS_TYPE_MASK);
|
---|
353 | break;
|
---|
354 | }
|
---|
355 | if (RT_FAILURE(rc) && RT_SUCCESS(rcRet))
|
---|
356 | rcRet = rc;
|
---|
357 | }
|
---|
358 |
|
---|
359 | /*
|
---|
360 | * Some error occured, close and return.
|
---|
361 | */
|
---|
362 | RTDirClose(hDir);
|
---|
363 | return rc;
|
---|
364 | }
|
---|
365 |
|
---|
366 | /**
|
---|
367 | * Validates the specified file or directory.
|
---|
368 | *
|
---|
369 | * @returns IPRT status code, errors go via rtPathRmError.
|
---|
370 | * @param pOpts The RM options.
|
---|
371 | * @param pszPath The path to the file, directory, whatever.
|
---|
372 | */
|
---|
373 | static int rtPathRmOneValidate(PRTPATHRMCMDOPTS pOpts, const char *pszPath)
|
---|
374 | {
|
---|
375 | /*
|
---|
376 | * RTPathFilename doesn't do the trailing slash thing the way we need it to.
|
---|
377 | * E.g. both '..' and '../' should be rejected.
|
---|
378 | */
|
---|
379 | size_t cchPath = strlen(pszPath);
|
---|
380 | while (cchPath > 0 && RTPATH_IS_SLASH(pszPath[cchPath - 1]))
|
---|
381 | cchPath--;
|
---|
382 |
|
---|
383 | if ( ( cchPath == 0
|
---|
384 | || 0 /** @todo drive letter + UNC crap */)
|
---|
385 | && pOpts->fPreserveRoot)
|
---|
386 | return rtPathRmError(pOpts, pszPath, VERR_CANT_DELETE_DIRECTORY, "Cannot remove root directory ('%s').\n", pszPath);
|
---|
387 |
|
---|
388 | size_t offLast = cchPath - 1;
|
---|
389 | while (offLast > 0 && !RTPATH_IS_SEP(pszPath[offLast - 1]))
|
---|
390 | offLast--;
|
---|
391 |
|
---|
392 | size_t cchLast = cchPath - offLast;
|
---|
393 | if ( pszPath[offLast] == '.'
|
---|
394 | && ( cchLast == 1
|
---|
395 | || (cchLast == 2 && pszPath[offLast + 1] == '.')))
|
---|
396 | return rtPathRmError(pOpts, pszPath, VERR_CANT_DELETE_DIRECTORY, "Cannot remove special directory '%s'.\n", pszPath);
|
---|
397 |
|
---|
398 | return VINF_SUCCESS;
|
---|
399 | }
|
---|
400 |
|
---|
401 |
|
---|
402 | /**
|
---|
403 | * Remove one user specified file or directory.
|
---|
404 | *
|
---|
405 | * @returns IPRT status code, errors go via rtPathRmError.
|
---|
406 | * @param pOpts The RM options.
|
---|
407 | * @param pszPath The path to the file, directory, whatever.
|
---|
408 | */
|
---|
409 | static int rtPathRmOne(PRTPATHRMCMDOPTS pOpts, const char *pszPath)
|
---|
410 | {
|
---|
411 | /*
|
---|
412 | * RM refuses to delete some directories.
|
---|
413 | */
|
---|
414 | int rc = rtPathRmOneValidate(pOpts, pszPath);
|
---|
415 | if (RT_FAILURE(rc))
|
---|
416 | return rc;
|
---|
417 |
|
---|
418 | /*
|
---|
419 | * Query file system object info.
|
---|
420 | */
|
---|
421 | RTFSOBJINFO ObjInfo;
|
---|
422 | rc = RTPathQueryInfoEx(pszPath, &ObjInfo, RTFSOBJATTRADD_UNIX, RTPATH_F_ON_LINK);
|
---|
423 | if (RT_FAILURE(rc))
|
---|
424 | {
|
---|
425 | if (pOpts->fForce && (rc == VERR_FILE_NOT_FOUND || rc == VERR_PATH_NOT_FOUND))
|
---|
426 | return VINF_SUCCESS;
|
---|
427 | return rtPathRmError(pOpts, pszPath, rc, "Error deleting '%s': %Rrc", pszPath, rc);
|
---|
428 | }
|
---|
429 |
|
---|
430 | /*
|
---|
431 | * Take type specific action.
|
---|
432 | */
|
---|
433 | switch (ObjInfo.Attr.fMode & RTFS_TYPE_MASK)
|
---|
434 | {
|
---|
435 | case RTFS_TYPE_FILE:
|
---|
436 | return rtPathRmOneFile(pOpts, pszPath, &ObjInfo);
|
---|
437 |
|
---|
438 | case RTFS_TYPE_DIRECTORY:
|
---|
439 | if (pOpts->fRecursive)
|
---|
440 | {
|
---|
441 | char szPath[RTPATH_MAX];
|
---|
442 | rc = RTPathAbs(pszPath, szPath, sizeof(szPath));
|
---|
443 | if (RT_FAILURE(rc))
|
---|
444 | return rtPathRmError(pOpts, pszPath, rc, "RTPathAbs failed on '%s': %Rrc\n", pszPath, rc);
|
---|
445 |
|
---|
446 | union
|
---|
447 | {
|
---|
448 | RTDIRENTRYEX Core;
|
---|
449 | uint8_t abPadding[RTPATHRM_DIR_MAX_ENTRY_SIZE];
|
---|
450 | } DirEntry;
|
---|
451 |
|
---|
452 | return rtPathRmRecursive(pOpts, szPath, strlen(szPath), &DirEntry.Core);
|
---|
453 | }
|
---|
454 | if (pOpts->fDirsAndOther)
|
---|
455 | return rtPathRmOneDir(pOpts, pszPath);
|
---|
456 | return rtPathRmError(pOpts, pszPath, VERR_IS_A_DIRECTORY, "Cannot remove '%s': %Rrc\n", pszPath, VERR_IS_A_DIRECTORY);
|
---|
457 |
|
---|
458 | case RTFS_TYPE_SYMLINK:
|
---|
459 | return rtPathRmOneSymlink(pOpts, pszPath);
|
---|
460 |
|
---|
461 | case RTFS_TYPE_FIFO:
|
---|
462 | case RTFS_TYPE_DEV_CHAR:
|
---|
463 | case RTFS_TYPE_DEV_BLOCK:
|
---|
464 | case RTFS_TYPE_SOCKET:
|
---|
465 | return rtPathRmOneFile(pOpts, pszPath, &ObjInfo);
|
---|
466 |
|
---|
467 | case RTFS_TYPE_WHITEOUT:
|
---|
468 | default:
|
---|
469 | return rtPathRmError(pOpts, pszPath, VERR_UNEXPECTED_FS_OBJ_TYPE,
|
---|
470 | "Object '%s' has an unknown file type: %o\n", pszPath, ObjInfo.Attr.fMode & RTFS_TYPE_MASK);
|
---|
471 |
|
---|
472 | }
|
---|
473 | }
|
---|
474 |
|
---|
475 |
|
---|
476 | RTDECL(RTEXITCODE) RTPathRmCmd(unsigned cArgs, char **papszArgs)
|
---|
477 | {
|
---|
478 | /*
|
---|
479 | * Parse the command line.
|
---|
480 | */
|
---|
481 | static const RTGETOPTDEF s_aOptions[] =
|
---|
482 | {
|
---|
483 | /* operations */
|
---|
484 | { "--dirs-and-more", 'd', RTGETOPT_REQ_NOTHING },
|
---|
485 | { "--force", 'f', RTGETOPT_REQ_NOTHING },
|
---|
486 | { "--prompt", 'i', RTGETOPT_REQ_NOTHING },
|
---|
487 | { "--prompt-once", 'I', RTGETOPT_REQ_NOTHING },
|
---|
488 | { "--interactive", RTPATHRMCMD_OPT_INTERACTIVE, RTGETOPT_REQ_STRING },
|
---|
489 | { "--one-file-system", RTPATHRMCMD_OPT_ONE_FILE_SYSTEM, RTGETOPT_REQ_NOTHING },
|
---|
490 | { "--preserve-root", RTPATHRMCMD_OPT_PRESERVE_ROOT, RTGETOPT_REQ_NOTHING },
|
---|
491 | { "--no-preserve-root", RTPATHRMCMD_OPT_NO_PRESERVE_ROOT, RTGETOPT_REQ_NOTHING },
|
---|
492 | { "--recursive", 'R', RTGETOPT_REQ_NOTHING },
|
---|
493 | { "--recursive", 'r', RTGETOPT_REQ_NOTHING },
|
---|
494 | { "--safe-delete", 'P', RTGETOPT_REQ_NOTHING },
|
---|
495 | { "--verbose", 'v', RTGETOPT_REQ_NOTHING },
|
---|
496 |
|
---|
497 | /* IPRT extensions */
|
---|
498 | { "--machine-readable", RTPATHRMCMD_OPT_MACHINE_READABLE, RTGETOPT_REQ_NOTHING },
|
---|
499 | { "--machinereadable", RTPATHRMCMD_OPT_MACHINE_READABLE, RTGETOPT_REQ_NOTHING }, /* bad long option style */
|
---|
500 | };
|
---|
501 |
|
---|
502 | RTGETOPTSTATE GetState;
|
---|
503 | int rc = RTGetOptInit(&GetState, cArgs, papszArgs, s_aOptions, RT_ELEMENTS(s_aOptions), 1,
|
---|
504 | RTGETOPTINIT_FLAGS_OPTS_FIRST);
|
---|
505 | if (RT_FAILURE(rc))
|
---|
506 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "RTGetOpt failed: %Rrc", rc);
|
---|
507 |
|
---|
508 | RTPATHRMCMDOPTS Opts;
|
---|
509 | RT_ZERO(Opts);
|
---|
510 | Opts.fPreserveRoot = true;
|
---|
511 | Opts.enmInteractive = RTPATHRMCMDINTERACTIVE_NONE;
|
---|
512 |
|
---|
513 | RTGETOPTUNION ValueUnion;
|
---|
514 | while ( (rc = RTGetOpt(&GetState, &ValueUnion)) != 0
|
---|
515 | && rc != VINF_GETOPT_NOT_OPTION)
|
---|
516 | {
|
---|
517 | switch (rc)
|
---|
518 | {
|
---|
519 | case 'd':
|
---|
520 | Opts.fDirsAndOther = true;
|
---|
521 | break;
|
---|
522 |
|
---|
523 | case 'f':
|
---|
524 | Opts.fForce = true;
|
---|
525 | Opts.enmInteractive = RTPATHRMCMDINTERACTIVE_NONE;
|
---|
526 | break;
|
---|
527 |
|
---|
528 | case 'i':
|
---|
529 | Opts.enmInteractive = RTPATHRMCMDINTERACTIVE_ALL;
|
---|
530 | break;
|
---|
531 |
|
---|
532 | case 'I':
|
---|
533 | Opts.enmInteractive = RTPATHRMCMDINTERACTIVE_ONCE;
|
---|
534 | break;
|
---|
535 |
|
---|
536 | case RTPATHRMCMD_OPT_INTERACTIVE:
|
---|
537 | if (!strcmp(ValueUnion.psz, "always"))
|
---|
538 | Opts.enmInteractive = RTPATHRMCMDINTERACTIVE_ALL;
|
---|
539 | else if (!strcmp(ValueUnion.psz, "once"))
|
---|
540 | Opts.enmInteractive = RTPATHRMCMDINTERACTIVE_ONCE;
|
---|
541 | else
|
---|
542 | return RTMsgErrorExit(RTEXITCODE_SYNTAX, "Unknown --interactive option value: '%s'\n", ValueUnion.psz);
|
---|
543 | break;
|
---|
544 |
|
---|
545 | case RTPATHRMCMD_OPT_ONE_FILE_SYSTEM:
|
---|
546 | Opts.fOneFileSystem = true;
|
---|
547 | break;
|
---|
548 |
|
---|
549 | case RTPATHRMCMD_OPT_PRESERVE_ROOT:
|
---|
550 | Opts.fPreserveRoot = true;
|
---|
551 | break;
|
---|
552 |
|
---|
553 | case RTPATHRMCMD_OPT_NO_PRESERVE_ROOT:
|
---|
554 | Opts.fPreserveRoot = false;
|
---|
555 | break;
|
---|
556 |
|
---|
557 | case 'R':
|
---|
558 | case 'r':
|
---|
559 | Opts.fRecursive = true;
|
---|
560 | Opts.fDirsAndOther = true;
|
---|
561 | break;
|
---|
562 |
|
---|
563 | case 'P':
|
---|
564 | Opts.fSafeDelete = true;
|
---|
565 | break;
|
---|
566 |
|
---|
567 | case 'v':
|
---|
568 | Opts.fVerbose = true;
|
---|
569 | break;
|
---|
570 |
|
---|
571 | case RTPATHRMCMD_OPT_MACHINE_READABLE:
|
---|
572 | Opts.fMachineReadable = true;
|
---|
573 | break;
|
---|
574 |
|
---|
575 | case 'h':
|
---|
576 | RTPrintf("Usage: to be written\nOption dump:\n");
|
---|
577 | for (unsigned i = 0; i < RT_ELEMENTS(s_aOptions); i++)
|
---|
578 | if (RT_C_IS_PRINT(s_aOptions[i].iShort))
|
---|
579 | RTPrintf(" -%c,%s\n", s_aOptions[i].iShort, s_aOptions[i].pszLong);
|
---|
580 | else
|
---|
581 | RTPrintf(" %s\n", s_aOptions[i].pszLong);
|
---|
582 | return RTEXITCODE_SUCCESS;
|
---|
583 |
|
---|
584 | case 'V':
|
---|
585 | RTPrintf("%sr%d\n", RTBldCfgVersion(), RTBldCfgRevision());
|
---|
586 | return RTEXITCODE_SUCCESS;
|
---|
587 |
|
---|
588 | default:
|
---|
589 | return RTGetOptPrintError(rc, &ValueUnion);
|
---|
590 | }
|
---|
591 | }
|
---|
592 |
|
---|
593 | /*
|
---|
594 | * Options we don't support.
|
---|
595 | */
|
---|
596 | if (Opts.fOneFileSystem)
|
---|
597 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "The --one-file-system option is not yet implemented.\n");
|
---|
598 | if (Opts.enmInteractive != RTPATHRMCMDINTERACTIVE_NONE)
|
---|
599 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "The -i, -I and --interactive options are not implemented yet.\n");
|
---|
600 |
|
---|
601 | /*
|
---|
602 | * No files means error.
|
---|
603 | */
|
---|
604 | if (rc != VINF_GETOPT_NOT_OPTION && !Opts.fForce)
|
---|
605 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "No files or directories specified.\n");
|
---|
606 |
|
---|
607 | /*
|
---|
608 | * Machine readable init + header.
|
---|
609 | */
|
---|
610 | if (Opts.fMachineReadable)
|
---|
611 | {
|
---|
612 | int rc2 = RTStrmSetMode(g_pStdOut, true /*fBinary*/, false /*fCurrentCodeSet*/);
|
---|
613 | if (RT_FAILURE(rc2))
|
---|
614 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "RTStrmSetMode failed: %Rrc.\n", rc2);
|
---|
615 | static const char s_achHeader[] = "hdr_id=rm\0hdr_ver=1";
|
---|
616 | RTStrmWrite(g_pStdOut, s_achHeader, sizeof(s_achHeader));
|
---|
617 | }
|
---|
618 |
|
---|
619 | /*
|
---|
620 | * Delete the specified files/dirs/whatever.
|
---|
621 | */
|
---|
622 | RTEXITCODE rcExit = RTEXITCODE_SUCCESS;
|
---|
623 | while (rc == VINF_GETOPT_NOT_OPTION)
|
---|
624 | {
|
---|
625 | rc = rtPathRmOne(&Opts, ValueUnion.psz);
|
---|
626 | if (RT_FAILURE(rc))
|
---|
627 | rcExit = RTEXITCODE_FAILURE;
|
---|
628 |
|
---|
629 | /* next */
|
---|
630 | rc = RTGetOpt(&GetState, &ValueUnion);
|
---|
631 | }
|
---|
632 | if (rc != 0)
|
---|
633 | rcExit = RTGetOptPrintError(rc, &ValueUnion);
|
---|
634 |
|
---|
635 | /*
|
---|
636 | * Terminate the machine readable stuff.
|
---|
637 | */
|
---|
638 | if (Opts.fMachineReadable)
|
---|
639 | {
|
---|
640 | RTStrmWrite(g_pStdOut, "\0\0\0", 4);
|
---|
641 | rc = RTStrmFlush(g_pStdOut);
|
---|
642 | if (RT_FAILURE(rc) && rcExit == RTEXITCODE_SUCCESS)
|
---|
643 | rcExit = RTEXITCODE_FAILURE;
|
---|
644 | }
|
---|
645 |
|
---|
646 | return rcExit;
|
---|
647 | }
|
---|
648 |
|
---|