Changeset 56745 in vbox for trunk/src/VBox/Additions/common/VBoxService
- Timestamp:
- Jul 2, 2015 10:29:29 AM (10 years ago)
- svn:sync-xref-src-repo-rev:
- 101391
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Additions/common/VBoxService/VBoxServiceToolBox.cpp
r56744 r56745 957 957 958 958 959 #if 1960 961 959 /* Try using RTPathRmCmd. */ 962 960 static RTEXITCODE VBoxServiceToolboxRm(int argc, char **argv) … … 964 962 return RTPathRmCmd(argc, argv); 965 963 } 966 967 #else968 static char g_paszRmHelp[] =969 " VBoxService [--use-toolbox] vbox_rm [<general options>] [<options>] <file>...\n\n"970 "Delete files and optionally directories if the '-R' or '-r' option is specified.\n"971 "If a file or directory cannot be deleted, an error message is printed if the\n"972 "'--machine-readable' option is not specified and the next file will be\n"973 "processed. The root directory is always ignored.\n\n"974 "Options:\n\n"975 " [-R|-r] Recursively delete directories too.\n"976 "\n";977 978 979 /**980 * Report the result of a vbox_rm operation - either errors to stderr (not981 * machine-readable) or everything to stdout as <name>\0<rc>\0 (machine-982 * readable format). The message may optionally contain a '%s' for the file983 * name and an %Rrc for the result code in that order. In future a "verbose"984 * flag may be added, without which nothing will be output in non-machine-985 * readable mode. Sets prc if rc is a non-success code.986 */987 static void toolboxRmReport(const char *pcszMessage, const char *pcszFile,988 bool fActive, int rc, uint32_t fOutputFlags,989 int *prc)990 {991 if (!fActive)992 return;993 if (!(fOutputFlags & VBOXSERVICETOOLBOXOUTPUTFLAG_PARSEABLE))994 {995 if (RT_SUCCESS(rc))996 RTPrintf(pcszMessage, pcszFile, rc);997 else998 RTMsgError(pcszMessage, pcszFile, rc);999 }1000 else1001 RTPrintf("fname=%s%crc=%d%c", pcszFile, 0, rc, 0);1002 if (prc && RT_FAILURE(rc))1003 *prc = rc;1004 }1005 1006 1007 /**1008 * Main function for tool "vbox_rm".1009 *1010 * @return RTEXITCODE.1011 * @param argc Number of arguments.1012 * @param argv Pointer to argument array.1013 */1014 static RTEXITCODE VBoxServiceToolboxRm(int argc, char **argv)1015 {1016 static const RTGETOPTDEF s_aOptions[] =1017 {1018 { "--machinereadable", VBOXSERVICETOOLBOXOPT_MACHINE_READABLE,1019 RTGETOPT_REQ_NOTHING },1020 /* Be like POSIX, which has both 'r' and 'R'. */1021 { NULL, 'r',1022 RTGETOPT_REQ_NOTHING },1023 { NULL, 'R',1024 RTGETOPT_REQ_NOTHING },1025 };1026 1027 enum1028 {1029 VBOXSERVICETOOLBOXRMFLAG_RECURSIVE = RT_BIT_32(0)1030 };1031 1032 int ch, rc;1033 RTGETOPTUNION ValueUnion;1034 RTGETOPTSTATE GetState;1035 rc = RTGetOptInit(&GetState, argc, argv, s_aOptions,1036 RT_ELEMENTS(s_aOptions), 1 /*iFirst*/,1037 RTGETOPTINIT_FLAGS_OPTS_FIRST);1038 AssertRCReturn(rc, RTEXITCODE_INIT);1039 1040 bool fVerbose = false;1041 uint32_t fFlags = 0;1042 uint32_t fOutputFlags = 0;1043 int cNonOptions = 0;1044 1045 while ( (ch = RTGetOpt(&GetState, &ValueUnion))1046 && RT_SUCCESS(rc))1047 {1048 /* For options that require an argument, ValueUnion has received the value. */1049 switch (ch)1050 {1051 case 'h':1052 VBoxServiceToolboxShowUsageHeader();1053 RTPrintf("%s", g_paszRmHelp);1054 return RTEXITCODE_SUCCESS;1055 1056 case 'V':1057 VBoxServiceToolboxShowVersion();1058 return RTEXITCODE_SUCCESS;1059 1060 case VBOXSERVICETOOLBOXOPT_MACHINE_READABLE:1061 fOutputFlags |= VBOXSERVICETOOLBOXOUTPUTFLAG_PARSEABLE;1062 break;1063 1064 case 'r':1065 case 'R': /* Allow directories too. */1066 fFlags |= VBOXSERVICETOOLBOXRMFLAG_RECURSIVE;1067 break;1068 1069 case VINF_GETOPT_NOT_OPTION:1070 /* RTGetOpt will sort these to the end of the argv vector so1071 * that we will deal with them afterwards. */1072 ++cNonOptions;1073 break;1074 1075 default:1076 return RTGetOptPrintError(ch, &ValueUnion);1077 }1078 }1079 if (RT_SUCCESS(rc))1080 {1081 /* Print magic/version. */1082 if (fOutputFlags & VBOXSERVICETOOLBOXOUTPUTFLAG_PARSEABLE)1083 {1084 rc = VBoxServiceToolboxStrmInit();1085 if (RT_FAILURE(rc))1086 RTMsgError("Error while initializing parseable streams, rc=%Rrc\n", rc);1087 VBoxServiceToolboxPrintStrmHeader("vbt_rm", 1 /* Stream version */);1088 }1089 }1090 1091 /* We need at least one file. */1092 if (RT_SUCCESS(rc) && cNonOptions == 0)1093 {1094 toolboxRmReport("No files or directories specified.\n", NULL, true, 0,1095 fOutputFlags, NULL);1096 return RTEXITCODE_FAILURE;1097 }1098 if (RT_SUCCESS(rc))1099 {1100 for (int i = argc - cNonOptions; i < argc; ++i)1101 {1102 RTFSOBJINFO Info;1103 int rc2 = RTPathQueryInfoEx(argv[i], &Info, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK);1104 if (RT_SUCCESS(rc))1105 {1106 if (RTFS_IS_SYMLINK(Info.Attr.fMode))1107 {1108 rc2 = RTSymlinkDelete(argv[i], 0 /*fFlags*/);1109 toolboxRmReport("", argv[i], RT_SUCCESS(rc2), rc2, fOutputFlags, NULL);1110 toolboxRmReport("The following error occurred while removing symlink '%s': %Rrc.\n",1111 argv[i], RT_FAILURE(rc2), rc2, fOutputFlags, &rc);1112 }1113 else if (RTFS_IS_DIRECTORY(Info.Attr.fMode))1114 {1115 if (!(fFlags & VBOXSERVICETOOLBOXRMFLAG_RECURSIVE))1116 toolboxRmReport("Cannot remove directory '%s' as the '-R' option was not specified.\n",1117 argv[i], true, VERR_INVALID_PARAMETER, fOutputFlags, &rc);1118 else1119 {1120 rc2 = RTDirRemoveRecursive(argv[i], RTDIRRMREC_F_CONTENT_AND_DIR);1121 toolboxRmReport("", argv[i], RT_SUCCESS(rc2), rc2, fOutputFlags, NULL);1122 toolboxRmReport("The following error occurred while removing directory '%s': %Rrc.\n",1123 argv[i], RT_FAILURE(rc2), rc2, fOutputFlags, &rc);1124 }1125 }1126 else1127 {1128 rc2 = RTFileDelete(argv[i]);1129 toolboxRmReport("", argv[i], RT_SUCCESS(rc2), rc2, fOutputFlags, NULL);1130 toolboxRmReport("The following error occurred while removing file '%s': %Rrc.\n",1131 argv[i], RT_FAILURE(rc2), rc2, fOutputFlags, &rc);1132 }1133 }1134 else if ( rc2 == VERR_FILE_NOT_FOUND1135 || rc2 == VERR_PATH_NOT_FOUND)1136 toolboxRmReport("File '%s' does not exist (%Rrc).\n", argv[i], true, rc2, fOutputFlags, &rc);1137 else1138 toolboxRmReport("The following error occurred while checkin '%s' before removal: %Rrc.\n",1139 argv[i], true, rc2, fOutputFlags, &rc);1140 }1141 1142 if (fOutputFlags & VBOXSERVICETOOLBOXOUTPUTFLAG_PARSEABLE) /* Output termination. */1143 VBoxServiceToolboxPrintStrmTermination();1144 }1145 return RT_SUCCESS(rc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;1146 }1147 #endif1148 964 1149 965
Note:
See TracChangeset
for help on using the changeset viewer.