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