Changeset 42763 in vbox
- Timestamp:
- Aug 10, 2012 7:51:17 PM (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Additions/common/VBoxService/VBoxServiceToolBox.cpp
r42747 r42763 136 136 " [--verbose|-v] [<file>...]\n" 137 137 " rm <general options> [-r|-R] <file>...\n" 138 " mktemp <general options> [--directory|-d] [--secure|-s]\n" 139 " [--mode|-m <mode>] <template>\n" 138 " mktemp <general options> [--directory|-d] [--mode|-m <mode>]\n" 139 " [--secure|-s] [--tmpdir|-t <path>]\n" 140 " <template>\n" 140 141 " mkdir <general options> [--mode|-m <mode>] [--parents|-p]\n" 141 142 " [--verbose|-v] <directory>...\n" … … 1135 1136 "Create a temporary directory based on the template supplied. The first string\n" 1136 1137 "of consecutive 'X' characters in the template will be replaced to form a unique\n" 1137 "name for the directory. The template must contain an absolute path. The\n" 1138 "default creation mode is 0600 for files and 0700 for directories.\n" 1138 "name for the directory. The template may not contain a path. The default\n" 1139 "creation mode is 0600 for files and 0700 for directories. If no path is\n" 1140 "specified the default temporary directory will be used.\n" 1139 1141 "Options:\n\n" 1140 1142 " [--directory|-d] Create a directory instead of a file.\n" 1141 1143 " [--mode|-m <mode>] Create the object with mode <mode>.\n" 1142 1144 " [--secure|-s] Fail if the object cannot be created securely.\n" 1145 " [--tmpdir|-t <path>] Create the object with the absolute path <path>.\n" 1143 1146 "\n"; 1144 1147 … … 1186 1189 { "--mode", 'm', RTGETOPT_REQ_STRING }, 1187 1190 { "--secure", 's', RTGETOPT_REQ_NOTHING }, 1191 { "--tmpdir", 't', RTGETOPT_REQ_STRING }, 1188 1192 }; 1189 1193 … … 1207 1211 AssertRCReturn(rc, RTEXITCODE_INIT); 1208 1212 1209 bool fVerbose = false; 1210 uint32_t fFlags = 0; 1211 uint32_t fOutputFlags = 0; 1212 int cNonOptions = 0; 1213 RTFMODE fMode = 0700; 1214 bool fModeSet = false; 1215 char *pszTemplate, *pszFilename; 1213 bool fVerbose = false; 1214 uint32_t fFlags = 0; 1215 uint32_t fOutputFlags = 0; 1216 int cNonOptions = 0; 1217 RTFMODE fMode = 0700; 1218 bool fModeSet = false; 1219 const char *pcszPath = NULL; 1220 const char *pcszTemplate; 1221 char szTemplateWithPath[RTPATH_MAX] = ""; 1216 1222 1217 1223 while ( (ch = RTGetOpt(&GetState, &ValueUnion)) … … 1251 1257 break; 1252 1258 1259 case 't': 1260 pcszPath = ValueUnion.psz; 1261 break; 1262 1253 1263 case VINF_GETOPT_NOT_OPTION: 1254 1264 /* RTGetOpt will sort these to the end of the argv vector so … … 1261 1271 } 1262 1272 } 1263 if (RT_SUCCESS(rc)) 1264 { 1265 /* Print magic/version. */ 1266 if (fOutputFlags & VBOXSERVICETOOLBOXOUTPUTFLAG_PARSEABLE) 1267 { 1268 rc = VBoxServiceToolboxStrmInit(); 1269 if (RT_FAILURE(rc)) 1270 RTMsgError("Error while initializing parseable streams, rc=%Rrc\n", rc); 1271 VBoxServiceToolboxPrintStrmHeader("vbt_mktemp", 1 /* Stream version */); 1272 } 1273 /* Print magic/version. */ 1274 if (fOutputFlags & VBOXSERVICETOOLBOXOUTPUTFLAG_PARSEABLE) 1275 { 1276 rc = VBoxServiceToolboxStrmInit(); 1277 if (RT_FAILURE(rc)) 1278 RTMsgError("Error while initializing parseable streams, rc=%Rrc\n", rc); 1279 VBoxServiceToolboxPrintStrmHeader("vbt_mktemp", 1 /* Stream version */); 1273 1280 } 1274 1281 … … 1280 1287 } 1281 1288 /* We need exactly one template, containing at least one 'X'. */ 1282 if ( RT_SUCCESS(rc) &&cNonOptions != 1)1289 if (cNonOptions != 1) 1283 1290 { 1284 1291 toolboxMkTempReport("Please specify exactly one template.\n", "", … … 1286 1293 return RTEXITCODE_SYNTAX; 1287 1294 } 1288 pszTemplate = argv[argc - 1]; 1289 pszFilename = RTPathFilename(pszTemplate); 1290 if ( RT_SUCCESS(rc) 1291 && (!pszFilename || !strchr(pszFilename, 'X'))) /* IPRT asserts this. */ 1292 { 1293 toolboxMkTempReport("Template '%s' should contain a file name with at least one 'X' character.\n", 1294 pszTemplate, true, VERR_INVALID_PARAMETER, 1295 pcszTemplate = argv[argc - 1]; 1296 /* Validate that the template is as IPRT requires (asserted by IPRT). */ 1297 if (RTPathHasPath(pcszTemplate) || !strchr(pcszTemplate, 'X')) 1298 { 1299 toolboxMkTempReport("Template '%s' should contain a file name with no path and at least one 'X' character.\n", 1300 pcszTemplate, true, VERR_INVALID_PARAMETER, 1295 1301 fOutputFlags, &rc); 1296 1302 return RTEXITCODE_FAILURE; 1297 1303 } 1298 /* For now require an absolute path - perhaps later if we support a 1299 * current directory concept we can change this. */ 1300 if (!RTPathStartsWithRoot(pszTemplate)) 1301 { 1302 toolboxMkTempReport("Template '%s' should contain an absolute path.\n", 1303 pszTemplate, true, VERR_INVALID_PARAMETER, 1304 if (pcszPath && !RTPathStartsWithRoot(pcszPath)) 1305 { 1306 toolboxMkTempReport("Path '%s' should be absolute.\n", 1307 pcszPath, true, VERR_INVALID_PARAMETER, 1304 1308 fOutputFlags, &rc); 1305 1309 return RTEXITCODE_FAILURE; 1306 1310 } 1307 1308 if (RT_SUCCESS(rc)) 1309 { 1310 if (fFlags & VBOXSERVICETOOLBOXMKTEMPFLAG_DIRECTORY) 1311 { 1312 rc = fFlags & VBOXSERVICETOOLBOXMKTEMPFLAG_SECURE 1313 ? RTDirCreateTempSecure(pszTemplate) 1314 : RTDirCreateTemp(pszTemplate, fMode); 1315 toolboxMkTempReport("Created temporary directory '%s'.\n", 1316 pszTemplate, RT_SUCCESS(rc), rc, 1317 fOutputFlags, NULL); 1318 /* RTDirCreateTemp[Secure] sets the template to "" on failure. */ 1319 toolboxMkTempReport("The following error occurred while creating the temporary directory%s: %Rrc.\n", 1320 pszTemplate, RT_FAILURE(rc), rc, 1321 fOutputFlags, NULL); 1322 } 1323 else 1324 { 1325 rc = fFlags & VBOXSERVICETOOLBOXMKTEMPFLAG_SECURE 1326 ? RTFileCreateTempSecure(pszTemplate) 1327 : RTFileCreateTemp(pszTemplate, fMode); 1328 toolboxMkTempReport("Created temporary file '%s'.\n", 1329 pszTemplate, RT_SUCCESS(rc), rc, 1330 fOutputFlags, NULL); 1331 /* RTFileCreateTemp[Secure] sets the template to "" on failure. */ 1332 toolboxMkTempReport("The following error occurred while creating the temporary file%s: %Rrc.\n", 1333 pszTemplate, RT_FAILURE(rc), rc, 1334 fOutputFlags, NULL); 1335 } 1336 if (fOutputFlags & VBOXSERVICETOOLBOXOUTPUTFLAG_PARSEABLE) /* Output termination. */ 1337 VBoxServiceToolboxPrintStrmTermination(); 1338 } 1311 if (pcszPath) 1312 { 1313 rc = RTStrCopy(szTemplateWithPath, sizeof(szTemplateWithPath), 1314 pcszPath); 1315 if (RT_FAILURE(rc)) 1316 { 1317 toolboxMkTempReport("Path '%s' too long.\n", pcszPath, true, 1318 VERR_INVALID_PARAMETER, fOutputFlags, &rc); 1319 return RTEXITCODE_FAILURE; 1320 } 1321 } 1322 else 1323 { 1324 rc = RTPathTemp(szTemplateWithPath, sizeof(szTemplateWithPath)); 1325 if (RT_FAILURE(rc)) 1326 { 1327 toolboxMkTempReport("Failed to get the temporary directory.\n", 1328 "", true, VERR_INVALID_PARAMETER, 1329 fOutputFlags, &rc); 1330 return RTEXITCODE_FAILURE; 1331 } 1332 } 1333 rc = RTPathAppend(szTemplateWithPath, sizeof(szTemplateWithPath), 1334 pcszTemplate); 1335 if (RT_FAILURE(rc)) 1336 { 1337 toolboxMkTempReport("Template '%s' too long for path.\n", 1338 pcszTemplate, true, VERR_INVALID_PARAMETER, 1339 fOutputFlags, &rc); 1340 return RTEXITCODE_FAILURE; 1341 } 1342 1343 if (fFlags & VBOXSERVICETOOLBOXMKTEMPFLAG_DIRECTORY) 1344 { 1345 rc = fFlags & VBOXSERVICETOOLBOXMKTEMPFLAG_SECURE 1346 ? RTDirCreateTempSecure(szTemplateWithPath) 1347 : RTDirCreateTemp(szTemplateWithPath, fMode); 1348 toolboxMkTempReport("Created temporary directory '%s'.\n", 1349 szTemplateWithPath, RT_SUCCESS(rc), rc, 1350 fOutputFlags, NULL); 1351 /* RTDirCreateTemp[Secure] sets the template to "" on failure. */ 1352 toolboxMkTempReport("The following error occurred while creating a temporary directory from template '%s': %Rrc.\n", 1353 pcszTemplate, RT_FAILURE(rc), rc, fOutputFlags, 1354 NULL); 1355 } 1356 else 1357 { 1358 rc = fFlags & VBOXSERVICETOOLBOXMKTEMPFLAG_SECURE 1359 ? RTFileCreateTempSecure(szTemplateWithPath) 1360 : RTFileCreateTemp(szTemplateWithPath, fMode); 1361 toolboxMkTempReport("Created temporary file '%s'.\n", 1362 szTemplateWithPath, RT_SUCCESS(rc), rc, 1363 fOutputFlags, NULL); 1364 /* RTFileCreateTemp[Secure] sets the template to "" on failure. */ 1365 toolboxMkTempReport("The following error occurred while creating a temporary file from template '%s': %Rrc.\n", 1366 pcszTemplate, RT_FAILURE(rc), rc, fOutputFlags, 1367 NULL); 1368 } 1369 if (fOutputFlags & VBOXSERVICETOOLBOXOUTPUTFLAG_PARSEABLE) /* Output termination. */ 1370 VBoxServiceToolboxPrintStrmTermination(); 1339 1371 return RT_SUCCESS(rc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE; 1340 1372 }
Note:
See TracChangeset
for help on using the changeset viewer.