8502773207230763deba3e622495f2099398dcb0vboxsync * IPRT - TAR Command.
8502773207230763deba3e622495f2099398dcb0vboxsync * Copyright (C) 2013 Oracle Corporation
8502773207230763deba3e622495f2099398dcb0vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
8502773207230763deba3e622495f2099398dcb0vboxsync * available from http://www.virtualbox.org. This file is free software;
8502773207230763deba3e622495f2099398dcb0vboxsync * you can redistribute it and/or modify it under the terms of the GNU
8502773207230763deba3e622495f2099398dcb0vboxsync * General Public License (GPL) as published by the Free Software
8502773207230763deba3e622495f2099398dcb0vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
8502773207230763deba3e622495f2099398dcb0vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
8502773207230763deba3e622495f2099398dcb0vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
8502773207230763deba3e622495f2099398dcb0vboxsync * The contents of this file may alternatively be used under the terms
8502773207230763deba3e622495f2099398dcb0vboxsync * of the Common Development and Distribution License Version 1.0
8502773207230763deba3e622495f2099398dcb0vboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
8502773207230763deba3e622495f2099398dcb0vboxsync * VirtualBox OSE distribution, in which case the provisions of the
8502773207230763deba3e622495f2099398dcb0vboxsync * CDDL are applicable instead of those of the GPL.
8502773207230763deba3e622495f2099398dcb0vboxsync * You may elect to license modified versions of this file under the
8502773207230763deba3e622495f2099398dcb0vboxsync * terms and conditions of either the GPL or the CDDL or both.
8502773207230763deba3e622495f2099398dcb0vboxsync/*******************************************************************************
8502773207230763deba3e622495f2099398dcb0vboxsync* Header Files *
8502773207230763deba3e622495f2099398dcb0vboxsync*******************************************************************************/
8502773207230763deba3e622495f2099398dcb0vboxsync/*******************************************************************************
8502773207230763deba3e622495f2099398dcb0vboxsync* Defined Constants And Macros *
8502773207230763deba3e622495f2099398dcb0vboxsync*******************************************************************************/
8502773207230763deba3e622495f2099398dcb0vboxsync/** The max directory entry size. */
5743e1ce4fd37e68921b721e8d265d918b10d0c3vboxsync#define RTPATHRM_DIR_MAX_ENTRY_SIZE (sizeof(RTDIRENTRYEX) + 4096)
8502773207230763deba3e622495f2099398dcb0vboxsync/*******************************************************************************
8502773207230763deba3e622495f2099398dcb0vboxsync* Structures and Typedefs *
8502773207230763deba3e622495f2099398dcb0vboxsync*******************************************************************************/
8502773207230763deba3e622495f2099398dcb0vboxsync/** Interactive option. */
8502773207230763deba3e622495f2099398dcb0vboxsynctypedef enum
8502773207230763deba3e622495f2099398dcb0vboxsync /** @todo possible that we should by default prompt if removing read-only
8502773207230763deba3e622495f2099398dcb0vboxsync * files or files owned by someone else. We currently don't. */
8502773207230763deba3e622495f2099398dcb0vboxsync * IPRT rm option structure.
8502773207230763deba3e622495f2099398dcb0vboxsync /** Whether to delete recursively. */
8502773207230763deba3e622495f2099398dcb0vboxsync /** Whether to delete directories as well as other kinds of files. */
8502773207230763deba3e622495f2099398dcb0vboxsync /** Whether to remove files without prompting and ignoring non-existing
8502773207230763deba3e622495f2099398dcb0vboxsync * files. */
8502773207230763deba3e622495f2099398dcb0vboxsync /** Machine readable output. */
8502773207230763deba3e622495f2099398dcb0vboxsync /** Don't try remove root ('/') if set, otherwise don't treat root specially. */
8502773207230763deba3e622495f2099398dcb0vboxsync /** Whether to keep to one file system. */
8502773207230763deba3e622495f2099398dcb0vboxsync /** Whether to safely delete files (overwrite 3x before unlinking). */
8502773207230763deba3e622495f2099398dcb0vboxsync /** Whether to be verbose about the operation. */
8502773207230763deba3e622495f2099398dcb0vboxsync /** The interactive setting. */
8502773207230763deba3e622495f2099398dcb0vboxsync/** Pointer to the IPRT rm options. */
8502773207230763deba3e622495f2099398dcb0vboxsync/*******************************************************************************
8502773207230763deba3e622495f2099398dcb0vboxsync* Global Variables *
8502773207230763deba3e622495f2099398dcb0vboxsync*******************************************************************************/
8502773207230763deba3e622495f2099398dcb0vboxsync/** A bunch of zeros. */
8502773207230763deba3e622495f2099398dcb0vboxsync/** A bunch of 0xFF bytes. (lazy init) */
8502773207230763deba3e622495f2099398dcb0vboxsyncstatic void rtPathRmVerbose(PRTPATHRMCMDOPTS pOpts, const char *pszPath)
8502773207230763deba3e622495f2099398dcb0vboxsyncstatic int rtPathRmError(PRTPATHRMCMDOPTS pOpts, const char *pszPath, int rc,
8502773207230763deba3e622495f2099398dcb0vboxsync const char *pszFormat, ...)
8502773207230763deba3e622495f2099398dcb0vboxsync * Worker that removes a symbolic link.
8502773207230763deba3e622495f2099398dcb0vboxsync * @returns IPRT status code, errors go via rtPathRmError.
8502773207230763deba3e622495f2099398dcb0vboxsync * @param pOpts The RM options.
8502773207230763deba3e622495f2099398dcb0vboxsync * @param pszPath The path to the symbolic link.
8502773207230763deba3e622495f2099398dcb0vboxsyncstatic int rtPathRmOneSymlink(PRTPATHRMCMDOPTS pOpts, const char *pszPath)
8502773207230763deba3e622495f2099398dcb0vboxsync return rtPathRmError(pOpts, pszPath, rc, "Error removing symbolic link '%s': %Rrc\n", pszPath, rc);
8502773207230763deba3e622495f2099398dcb0vboxsync * Worker that removes a file.
8502773207230763deba3e622495f2099398dcb0vboxsync * Currently used to delete both regular and special files.
8502773207230763deba3e622495f2099398dcb0vboxsync * @returns IPRT status code, errors go via rtPathRmError.
8502773207230763deba3e622495f2099398dcb0vboxsync * @param pOpts The RM options.
8502773207230763deba3e622495f2099398dcb0vboxsync * @param pszPath The path to the file.
8502773207230763deba3e622495f2099398dcb0vboxsync * @param pObjInfo The FS object info for the file.
8502773207230763deba3e622495f2099398dcb0vboxsyncstatic int rtPathRmOneFile(PRTPATHRMCMDOPTS pOpts, const char *pszPath, PRTFSOBJINFO pObjInfo)
8502773207230763deba3e622495f2099398dcb0vboxsync * Wipe the file if requested and possible.
8502773207230763deba3e622495f2099398dcb0vboxsync if (pOpts->fSafeDelete && RTFS_IS_FILE(pObjInfo->Attr.fMode))
8502773207230763deba3e622495f2099398dcb0vboxsync /* Lazy init of the 0xff buffer. */
8502773207230763deba3e622495f2099398dcb0vboxsync if (g_ab0xFF[0] != 0xff || g_ab0xFF[sizeof(g_ab0xFF) - 1] != 0xff)
8502773207230763deba3e622495f2099398dcb0vboxsync return rtPathRmError(pOpts, pszPath, rc, "Opening '%s' for overwriting: %Rrc\n", pszPath, rc);
8502773207230763deba3e622495f2099398dcb0vboxsync uint8_t const *pabFiller = iPass == 1 ? g_abZeros : g_ab0xFF;
8502773207230763deba3e622495f2099398dcb0vboxsync size_t const cbFiller = iPass == 1 ? sizeof(g_abZeros) : sizeof(g_ab0xFF);
8502773207230763deba3e622495f2099398dcb0vboxsync rc = RTFileSeek(hFile, 0, RTFILE_SEEK_BEGIN, NULL);
8502773207230763deba3e622495f2099398dcb0vboxsync rc = rtPathRmError(pOpts, pszPath, rc, "Error seeking to start of '%s': %Rrc\n", pszPath, rc);
8502773207230763deba3e622495f2099398dcb0vboxsync for (RTFOFF cbLeft = pObjInfo->cbObject; cbLeft > 0; cbLeft -= cbFiller)
8502773207230763deba3e622495f2099398dcb0vboxsync rc = RTFileWrite(hFile, pabFiller, cbToWrite, NULL);
8502773207230763deba3e622495f2099398dcb0vboxsync rc = rtPathRmError(pOpts, pszPath, rc, "Error writing to '%s': %Rrc\n", pszPath, rc);
8502773207230763deba3e622495f2099398dcb0vboxsync return rtPathRmError(pOpts, pszPath, rc2, "Closing '%s' failed: %Rrc\n", pszPath, rc);
8502773207230763deba3e622495f2099398dcb0vboxsync * Remove the file.
8502773207230763deba3e622495f2099398dcb0vboxsync ? "Error removing regular file '%s': %Rrc\n"
8502773207230763deba3e622495f2099398dcb0vboxsync : "Error removing special file '%s': %Rrc\n",
8502773207230763deba3e622495f2099398dcb0vboxsync * Deletes one directory (if it's empty).
8502773207230763deba3e622495f2099398dcb0vboxsync * @returns IPRT status code, errors go via rtPathRmError.
8502773207230763deba3e622495f2099398dcb0vboxsync * @param pOpts The RM options.
8502773207230763deba3e622495f2099398dcb0vboxsync * @param pszPath The path to the directory.
8502773207230763deba3e622495f2099398dcb0vboxsyncstatic int rtPathRmOneDir(PRTPATHRMCMDOPTS pOpts, const char *pszPath)
8502773207230763deba3e622495f2099398dcb0vboxsync return rtPathRmError(pOpts, pszPath, rc, "Error removing directory '%s': %Rrc", pszPath, rc);
8502773207230763deba3e622495f2099398dcb0vboxsync * Recursively delete a directory.
8502773207230763deba3e622495f2099398dcb0vboxsync * @returns IPRT status code, errors go via rtPathRmError.
8502773207230763deba3e622495f2099398dcb0vboxsync * @param pOpts The RM options.
8502773207230763deba3e622495f2099398dcb0vboxsync * @param pszPath Pointer to a writable buffer holding the path to
8502773207230763deba3e622495f2099398dcb0vboxsync * the directory.
8502773207230763deba3e622495f2099398dcb0vboxsync * @param cchPath The length of the path (avoid strlen).
8502773207230763deba3e622495f2099398dcb0vboxsync * @param pDirEntry Pointer to a directory entry buffer that is
8502773207230763deba3e622495f2099398dcb0vboxsync * RTPATHRM_DIR_MAX_ENTRY_SIZE bytes big.
5743e1ce4fd37e68921b721e8d265d918b10d0c3vboxsyncstatic int rtPathRmRecursive(PRTPATHRMCMDOPTS pOpts, char *pszPath, size_t cchPath, PRTDIRENTRYEX pDirEntry)
8502773207230763deba3e622495f2099398dcb0vboxsync * Make sure the path ends with a slash.
8502773207230763deba3e622495f2099398dcb0vboxsync if (!cchPath || !RTPATH_IS_SLASH(pszPath[cchPath - 1]))
8502773207230763deba3e622495f2099398dcb0vboxsync return rtPathRmError(pOpts, pszPath, VERR_BUFFER_OVERFLOW, "Buffer overflow fixing up '%s'.\n", pszPath);
8502773207230763deba3e622495f2099398dcb0vboxsync * Traverse the directory.
8502773207230763deba3e622495f2099398dcb0vboxsync return rtPathRmError(pOpts, pszPath, rc, "Error opening directory '%s': %Rrc", pszPath, rc);
8502773207230763deba3e622495f2099398dcb0vboxsync * Read the next entry, constructing an full path for it.
5743e1ce4fd37e68921b721e8d265d918b10d0c3vboxsync rc = RTDirReadEx(hDir, pDirEntry, &cbEntry, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK);
5743e1ce4fd37e68921b721e8d265d918b10d0c3vboxsync * Reached the end of the directory.
8502773207230763deba3e622495f2099398dcb0vboxsync return rtPathRmError(pOpts, pszPath, rc, "Error closing directory '%s': %Rrc", pszPath, rc);
8502773207230763deba3e622495f2099398dcb0vboxsync /* Delete the directory. */
8502773207230763deba3e622495f2099398dcb0vboxsync rc = rtPathRmError(pOpts, pszPath, rc, "Error reading directory '%s': %Rrc", pszPath, rc);
349757c26ef83d0ad9a94976edfda73ae8a7ba6cvboxsync /* Skip '.' and '..'. */
349757c26ef83d0ad9a94976edfda73ae8a7ba6cvboxsync /* Construct full path. */
8502773207230763deba3e622495f2099398dcb0vboxsync rc = rtPathRmError(pOpts, pszPath, VERR_BUFFER_OVERFLOW, "Path buffer overflow in directory '%s'.", pszPath);
8502773207230763deba3e622495f2099398dcb0vboxsync memcpy(pszPath + cchPath, pDirEntry->szName, pDirEntry->cbName + 1);
8502773207230763deba3e622495f2099398dcb0vboxsync * Take action according to the type.
5743e1ce4fd37e68921b721e8d265d918b10d0c3vboxsync switch (pDirEntry->Info.Attr.fMode & RTFS_TYPE_MASK)
5743e1ce4fd37e68921b721e8d265d918b10d0c3vboxsync rc = rtPathRmOneFile(pOpts, pszPath, &pDirEntry->Info);
5743e1ce4fd37e68921b721e8d265d918b10d0c3vboxsync rc = rtPathRmRecursive(pOpts, pszPath, cchPath + pDirEntry->cbName, pDirEntry);
5743e1ce4fd37e68921b721e8d265d918b10d0c3vboxsync rc = rtPathRmOneFile(pOpts, pszPath, &pDirEntry->Info);
8502773207230763deba3e622495f2099398dcb0vboxsync rc = rtPathRmError(pOpts, pszPath, VERR_UNEXPECTED_FS_OBJ_TYPE,
5743e1ce4fd37e68921b721e8d265d918b10d0c3vboxsync "Object '%s' has an unknown file type: %o\n",
5743e1ce4fd37e68921b721e8d265d918b10d0c3vboxsync pszPath, pDirEntry->Info.Attr.fMode & RTFS_TYPE_MASK);
5743e1ce4fd37e68921b721e8d265d918b10d0c3vboxsync * Some error occured, close and return.
5743e1ce4fd37e68921b721e8d265d918b10d0c3vboxsync * Validates the specified file or directory.
5743e1ce4fd37e68921b721e8d265d918b10d0c3vboxsync * @returns IPRT status code, errors go via rtPathRmError.
5743e1ce4fd37e68921b721e8d265d918b10d0c3vboxsync * @param pOpts The RM options.
5743e1ce4fd37e68921b721e8d265d918b10d0c3vboxsync * @param pszPath The path to the file, directory, whatever.
5743e1ce4fd37e68921b721e8d265d918b10d0c3vboxsyncstatic int rtPathRmOneValidate(PRTPATHRMCMDOPTS pOpts, const char *pszPath)
5743e1ce4fd37e68921b721e8d265d918b10d0c3vboxsync * RTPathFilename doesn't do the trailing slash thing the way we need it to.
5743e1ce4fd37e68921b721e8d265d918b10d0c3vboxsync * E.g. both '..' and '../' should be rejected.
5743e1ce4fd37e68921b721e8d265d918b10d0c3vboxsync while (cchPath > 0 && RTPATH_IS_SLASH(pszPath[cchPath - 1]))
5743e1ce4fd37e68921b721e8d265d918b10d0c3vboxsync || 0 /** @todo drive letter + UNC crap */)
5743e1ce4fd37e68921b721e8d265d918b10d0c3vboxsync return rtPathRmError(pOpts, pszPath, VERR_CANT_DELETE_DIRECTORY, "Cannot remove root directory ('%s').\n", pszPath);
5743e1ce4fd37e68921b721e8d265d918b10d0c3vboxsync while (offLast > 0 && !RTPATH_IS_SEP(pszPath[offLast - 1]))
5743e1ce4fd37e68921b721e8d265d918b10d0c3vboxsync return rtPathRmError(pOpts, pszPath, VERR_CANT_DELETE_DIRECTORY, "Cannot remove special directory '%s'.\n", pszPath);
8502773207230763deba3e622495f2099398dcb0vboxsync * Remove one user specified file or directory.
8502773207230763deba3e622495f2099398dcb0vboxsync * @returns IPRT status code, errors go via rtPathRmError.
8502773207230763deba3e622495f2099398dcb0vboxsync * @param pOpts The RM options.
8502773207230763deba3e622495f2099398dcb0vboxsync * @param pszPath The path to the file, directory, whatever.
8502773207230763deba3e622495f2099398dcb0vboxsyncstatic int rtPathRmOne(PRTPATHRMCMDOPTS pOpts, const char *pszPath)
5743e1ce4fd37e68921b721e8d265d918b10d0c3vboxsync * RM refuses to delete some directories.
8502773207230763deba3e622495f2099398dcb0vboxsync * Query file system object info.
5743e1ce4fd37e68921b721e8d265d918b10d0c3vboxsync rc = RTPathQueryInfoEx(pszPath, &ObjInfo, RTFSOBJATTRADD_UNIX, RTPATH_F_ON_LINK);
8502773207230763deba3e622495f2099398dcb0vboxsync if (pOpts->fForce && (rc == VERR_FILE_NOT_FOUND || rc == VERR_PATH_NOT_FOUND))
8502773207230763deba3e622495f2099398dcb0vboxsync return rtPathRmError(pOpts, pszPath, rc, "Error deleting '%s': %Rrc", pszPath, rc);
8502773207230763deba3e622495f2099398dcb0vboxsync * Take type specific action.
8502773207230763deba3e622495f2099398dcb0vboxsync return rtPathRmError(pOpts, pszPath, rc, "RTPathAbs failed on '%s': %Rrc\n", pszPath, rc);
5743e1ce4fd37e68921b721e8d265d918b10d0c3vboxsync return rtPathRmRecursive(pOpts, szPath, strlen(szPath), &DirEntry.Core);
8502773207230763deba3e622495f2099398dcb0vboxsync return rtPathRmError(pOpts, pszPath, VERR_IS_A_DIRECTORY, "Cannot remove '%s': %Rrc\n", pszPath, VERR_IS_A_DIRECTORY);
8502773207230763deba3e622495f2099398dcb0vboxsync return rtPathRmError(pOpts, pszPath, VERR_UNEXPECTED_FS_OBJ_TYPE,
8502773207230763deba3e622495f2099398dcb0vboxsync "Object '%s' has an unknown file type: %o\n", pszPath, ObjInfo.Attr.fMode & RTFS_TYPE_MASK);
8502773207230763deba3e622495f2099398dcb0vboxsyncRTDECL(RTEXITCODE) RTPathRmCmd(unsigned cArgs, char **papszArgs)
8502773207230763deba3e622495f2099398dcb0vboxsync * Parse the command line.
8502773207230763deba3e622495f2099398dcb0vboxsync /* operations */
8502773207230763deba3e622495f2099398dcb0vboxsync { "--interactive", RTPATHRMCMD_OPT_INTERACTIVE, RTGETOPT_REQ_STRING },
8502773207230763deba3e622495f2099398dcb0vboxsync { "--one-file-system", RTPATHRMCMD_OPT_ONE_FILE_SYSTEM, RTGETOPT_REQ_NOTHING },
8502773207230763deba3e622495f2099398dcb0vboxsync { "--preserve-root", RTPATHRMCMD_OPT_PRESERVE_ROOT, RTGETOPT_REQ_NOTHING },
8502773207230763deba3e622495f2099398dcb0vboxsync { "--no-preserve-root", RTPATHRMCMD_OPT_NO_PRESERVE_ROOT, RTGETOPT_REQ_NOTHING },
8502773207230763deba3e622495f2099398dcb0vboxsync /* IPRT extensions */
8502773207230763deba3e622495f2099398dcb0vboxsync { "--machine-readable", RTPATHRMCMD_OPT_MACHINE_READABLE, RTGETOPT_REQ_NOTHING },
8502773207230763deba3e622495f2099398dcb0vboxsync { "--machinereadable", RTPATHRMCMD_OPT_MACHINE_READABLE, RTGETOPT_REQ_NOTHING }, /* bad long option style */
8502773207230763deba3e622495f2099398dcb0vboxsync int rc = RTGetOptInit(&GetState, cArgs, papszArgs, s_aOptions, RT_ELEMENTS(s_aOptions), 1,
8502773207230763deba3e622495f2099398dcb0vboxsync return RTMsgErrorExit(RTEXITCODE_FAILURE, "RTGetOpt failed: %Rrc", rc);
8502773207230763deba3e622495f2099398dcb0vboxsync while ( (rc = RTGetOpt(&GetState, &ValueUnion)) != 0
8502773207230763deba3e622495f2099398dcb0vboxsync return RTMsgErrorExit(RTEXITCODE_SYNTAX, "Unknown --interactive option value: '%s'\n", ValueUnion.psz);
8502773207230763deba3e622495f2099398dcb0vboxsync for (unsigned i = 0; i < RT_ELEMENTS(s_aOptions); i++)
8502773207230763deba3e622495f2099398dcb0vboxsync RTPrintf(" -%c,%s\n", s_aOptions[i].iShort, s_aOptions[i].pszLong);
8502773207230763deba3e622495f2099398dcb0vboxsync RTPrintf("%sr%d\n", RTBldCfgVersion(), RTBldCfgRevision());
8502773207230763deba3e622495f2099398dcb0vboxsync * Options we don't support.
8502773207230763deba3e622495f2099398dcb0vboxsync return RTMsgErrorExit(RTEXITCODE_FAILURE, "The --one-file-system option is not yet implemented.\n");
8502773207230763deba3e622495f2099398dcb0vboxsync if (Opts.enmInteractive != RTPATHRMCMDINTERACTIVE_NONE)
8502773207230763deba3e622495f2099398dcb0vboxsync return RTMsgErrorExit(RTEXITCODE_FAILURE, "The -i, -I and --interactive options are not implemented yet.\n");
8502773207230763deba3e622495f2099398dcb0vboxsync * No files means error.
8502773207230763deba3e622495f2099398dcb0vboxsync return RTMsgErrorExit(RTEXITCODE_FAILURE, "No files or directories specified.\n");
8502773207230763deba3e622495f2099398dcb0vboxsync * Machine readable init + header.
8502773207230763deba3e622495f2099398dcb0vboxsync rc = RTStrmSetMode(g_pStdOut, true /*fBinary*/, false /*fCurrentCodeSet*/);
8502773207230763deba3e622495f2099398dcb0vboxsync return RTMsgErrorExit(RTEXITCODE_FAILURE, "RTStrmSetMode failed: %Rrc.\n", rc);
8502773207230763deba3e622495f2099398dcb0vboxsync static const char s_achHeader[] = "hdr_id=rm\0hdr_ver=1";
8502773207230763deba3e622495f2099398dcb0vboxsync RTStrmWrite(g_pStdOut, s_achHeader, sizeof(s_achHeader));
8502773207230763deba3e622495f2099398dcb0vboxsync * Delete the specified files/dirs/whatever.
8502773207230763deba3e622495f2099398dcb0vboxsync * Terminate the machine readable stuff.