1 | /* $Id: VBoxServiceToolBox.cpp 75929 2018-12-03 23:09:03Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxServiceToolbox - Internal (BusyBox-like) toolbox.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2012-2018 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 |
|
---|
18 |
|
---|
19 | /*********************************************************************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *********************************************************************************************************************************/
|
---|
22 | #include <stdio.h>
|
---|
23 |
|
---|
24 | #include <iprt/assert.h>
|
---|
25 | #include <iprt/buildconfig.h>
|
---|
26 | #include <iprt/dir.h>
|
---|
27 | #include <iprt/file.h>
|
---|
28 | #include <iprt/getopt.h>
|
---|
29 | #include <iprt/list.h>
|
---|
30 | #include <iprt/mem.h>
|
---|
31 | #include <iprt/message.h>
|
---|
32 | #include <iprt/path.h>
|
---|
33 | #include <iprt/string.h>
|
---|
34 | #include <iprt/stream.h>
|
---|
35 | #include <iprt/symlink.h>
|
---|
36 |
|
---|
37 | #ifndef RT_OS_WINDOWS
|
---|
38 | # include <sys/stat.h> /* need umask */
|
---|
39 | #endif
|
---|
40 |
|
---|
41 | #include <VBox/VBoxGuestLib.h>
|
---|
42 | #include <VBox/version.h>
|
---|
43 |
|
---|
44 | #include <VBox/GuestHost/GuestControl.h>
|
---|
45 |
|
---|
46 | #include "VBoxServiceInternal.h"
|
---|
47 | #include "VBoxServiceToolBox.h"
|
---|
48 | #include "VBoxServiceUtils.h"
|
---|
49 |
|
---|
50 | using namespace guestControl;
|
---|
51 |
|
---|
52 |
|
---|
53 | /*********************************************************************************************************************************
|
---|
54 | * Defined Constants And Macros *
|
---|
55 | *********************************************************************************************************************************/
|
---|
56 |
|
---|
57 | /** Generic option indices for commands. */
|
---|
58 | enum
|
---|
59 | {
|
---|
60 | VBOXSERVICETOOLBOXOPT_MACHINE_READABLE = 1000,
|
---|
61 | VBOXSERVICETOOLBOXOPT_VERBOSE
|
---|
62 | };
|
---|
63 |
|
---|
64 | /** Options indices for "vbox_cat". */
|
---|
65 | typedef enum VBOXSERVICETOOLBOXCATOPT
|
---|
66 | {
|
---|
67 | VBOXSERVICETOOLBOXCATOPT_NO_CONTENT_INDEXED = 1000
|
---|
68 | } VBOXSERVICETOOLBOXCATOPT;
|
---|
69 |
|
---|
70 | /** Flags for "vbox_ls". */
|
---|
71 | typedef enum VBOXSERVICETOOLBOXLSFLAG
|
---|
72 | {
|
---|
73 | VBOXSERVICETOOLBOXLSFLAG_NONE,
|
---|
74 | VBOXSERVICETOOLBOXLSFLAG_RECURSIVE,
|
---|
75 | VBOXSERVICETOOLBOXLSFLAG_SYMLINKS
|
---|
76 | } VBOXSERVICETOOLBOXLSFLAG;
|
---|
77 |
|
---|
78 | /** Flags for fs object output. */
|
---|
79 | typedef enum VBOXSERVICETOOLBOXOUTPUTFLAG
|
---|
80 | {
|
---|
81 | VBOXSERVICETOOLBOXOUTPUTFLAG_NONE,
|
---|
82 | VBOXSERVICETOOLBOXOUTPUTFLAG_LONG,
|
---|
83 | VBOXSERVICETOOLBOXOUTPUTFLAG_PARSEABLE
|
---|
84 | } VBOXSERVICETOOLBOXOUTPUTFLAG;
|
---|
85 |
|
---|
86 |
|
---|
87 | /*********************************************************************************************************************************
|
---|
88 | * Structures and Typedefs *
|
---|
89 | *********************************************************************************************************************************/
|
---|
90 | /** Pointer to a tool handler function. */
|
---|
91 | typedef RTEXITCODE (*PFNHANDLER)(int , char **);
|
---|
92 |
|
---|
93 | /** Definition for a specific toolbox tool. */
|
---|
94 | typedef struct VBOXSERVICETOOLBOXTOOL
|
---|
95 | {
|
---|
96 | /** Friendly name of the tool. */
|
---|
97 | const char *pszName;
|
---|
98 | /** Main handler to be invoked to use the tool. */
|
---|
99 | RTEXITCODE (*pfnHandler)(int argc, char **argv);
|
---|
100 | /** Conversion routine to convert the tool's exit code back to an IPRT rc. Optional.
|
---|
101 | *
|
---|
102 | * @todo r=bird: You better revert this, i.e. having pfnHandler return a VBox
|
---|
103 | * status code and have a routine for converting it to RTEXITCODE.
|
---|
104 | * Unless, what you really want to do here is to get a cached status, in
|
---|
105 | * which case you better call it what it is.
|
---|
106 | */
|
---|
107 | int (*pfnExitCodeConvertToRc)(RTEXITCODE rcExit);
|
---|
108 | } VBOXSERVICETOOLBOXTOOL;
|
---|
109 | /** Pointer to a const tool definition. */
|
---|
110 | typedef VBOXSERVICETOOLBOXTOOL const *PCVBOXSERVICETOOLBOXTOOL;
|
---|
111 |
|
---|
112 | /**
|
---|
113 | * An file/directory entry. Used to cache
|
---|
114 | * file names/paths for later processing.
|
---|
115 | */
|
---|
116 | typedef struct VBOXSERVICETOOLBOXPATHENTRY
|
---|
117 | {
|
---|
118 | /** Our node. */
|
---|
119 | RTLISTNODE Node;
|
---|
120 | /** Name of the entry. */
|
---|
121 | char *pszName;
|
---|
122 | } VBOXSERVICETOOLBOXPATHENTRY, *PVBOXSERVICETOOLBOXPATHENTRY;
|
---|
123 |
|
---|
124 | typedef struct VBOXSERVICETOOLBOXDIRENTRY
|
---|
125 | {
|
---|
126 | /** Our node. */
|
---|
127 | RTLISTNODE Node;
|
---|
128 | /** The actual entry. */
|
---|
129 | RTDIRENTRYEX dirEntry;
|
---|
130 | } VBOXSERVICETOOLBOXDIRENTRY, *PVBOXSERVICETOOLBOXDIRENTRY;
|
---|
131 |
|
---|
132 | /** ID cache entry. */
|
---|
133 | typedef struct VGSVCTOOLBOXUIDENTRY
|
---|
134 | {
|
---|
135 | /** The identifier name. */
|
---|
136 | uint32_t id;
|
---|
137 | /** Set if UID, clear if GID. */
|
---|
138 | bool fIsUid;
|
---|
139 | /** The name. */
|
---|
140 | char szName[128 - 4 - 1];
|
---|
141 | } VGSVCTOOLBOXUIDENTRY;
|
---|
142 | typedef VGSVCTOOLBOXUIDENTRY *PVGSVCTOOLBOXUIDENTRY;
|
---|
143 |
|
---|
144 |
|
---|
145 | /** ID cache. */
|
---|
146 | typedef struct VGSVCTOOLBOXIDCACHE
|
---|
147 | {
|
---|
148 | /** Number of valid cache entries. */
|
---|
149 | uint32_t cEntries;
|
---|
150 | /** The next entry to replace. */
|
---|
151 | uint32_t iNextReplace;
|
---|
152 | /** The cache entries. */
|
---|
153 | VGSVCTOOLBOXUIDENTRY aEntries[16];
|
---|
154 | } VGSVCTOOLBOXIDCACHE;
|
---|
155 | typedef VGSVCTOOLBOXIDCACHE *PVGSVCTOOLBOXIDCACHE;
|
---|
156 |
|
---|
157 |
|
---|
158 | /*********************************************************************************************************************************
|
---|
159 | * Internal Functions *
|
---|
160 | *********************************************************************************************************************************/
|
---|
161 | static RTEXITCODE vgsvcToolboxCat(int argc, char **argv);
|
---|
162 | static RTEXITCODE vgsvcToolboxLs(int argc, char **argv);
|
---|
163 | static RTEXITCODE vgsvcToolboxRm(int argc, char **argv);
|
---|
164 | static RTEXITCODE vgsvcToolboxMkTemp(int argc, char **argv);
|
---|
165 | static RTEXITCODE vgsvcToolboxMkDir(int argc, char **argv);
|
---|
166 | static RTEXITCODE vgsvcToolboxStat(int argc, char **argv);
|
---|
167 |
|
---|
168 |
|
---|
169 | /*********************************************************************************************************************************
|
---|
170 | * Global Variables *
|
---|
171 | *********************************************************************************************************************************/
|
---|
172 | /** Tool definitions. */
|
---|
173 | static VBOXSERVICETOOLBOXTOOL const g_aTools[] =
|
---|
174 | {
|
---|
175 | { VBOXSERVICE_TOOL_CAT, vgsvcToolboxCat , NULL },
|
---|
176 | { VBOXSERVICE_TOOL_LS, vgsvcToolboxLs , NULL },
|
---|
177 | { VBOXSERVICE_TOOL_RM, vgsvcToolboxRm , NULL },
|
---|
178 | { VBOXSERVICE_TOOL_MKTEMP, vgsvcToolboxMkTemp, NULL },
|
---|
179 | { VBOXSERVICE_TOOL_MKDIR, vgsvcToolboxMkDir , NULL },
|
---|
180 | { VBOXSERVICE_TOOL_STAT, vgsvcToolboxStat , NULL }
|
---|
181 | };
|
---|
182 |
|
---|
183 |
|
---|
184 |
|
---|
185 |
|
---|
186 | /**
|
---|
187 | * Displays a common header for all help text to stdout.
|
---|
188 | */
|
---|
189 | static void vgsvcToolboxShowUsageHeader(void)
|
---|
190 | {
|
---|
191 | RTPrintf(VBOX_PRODUCT " Guest Toolbox Version "
|
---|
192 | VBOX_VERSION_STRING "\n"
|
---|
193 | "(C) " VBOX_C_YEAR " " VBOX_VENDOR "\n"
|
---|
194 | "All rights reserved.\n"
|
---|
195 | "\n");
|
---|
196 | RTPrintf("Usage:\n\n");
|
---|
197 | }
|
---|
198 |
|
---|
199 |
|
---|
200 | /**
|
---|
201 | * Displays a help text to stdout.
|
---|
202 | */
|
---|
203 | static void vgsvcToolboxShowUsage(void)
|
---|
204 | {
|
---|
205 | vgsvcToolboxShowUsageHeader();
|
---|
206 | RTPrintf(" VBoxService [--use-toolbox] vbox_<command> [<general options>] <parameters>\n\n"
|
---|
207 | "General options:\n\n"
|
---|
208 | " --machinereadable produce all output in machine-readable form\n"
|
---|
209 | " -V print version number and exit\n"
|
---|
210 | "\n"
|
---|
211 | "Commands:\n\n"
|
---|
212 | " vbox_cat [<general options>] <file>...\n"
|
---|
213 | " vbox_ls [<general options>] [--dereference|-L] [-l] [-R]\n"
|
---|
214 | " [--verbose|-v] [<file>...]\n"
|
---|
215 | " vbox_rm [<general options>] [-r|-R] <file>...\n"
|
---|
216 | " vbox_mktemp [<general options>] [--directory|-d] [--mode|-m <mode>]\n"
|
---|
217 | " [--secure|-s] [--tmpdir|-t <path>] <template>\n"
|
---|
218 | " vbox_mkdir [<general options>] [--mode|-m <mode>] [--parents|-p]\n"
|
---|
219 | " [--verbose|-v] <directory>...\n"
|
---|
220 | " vbox_stat [<general options>] [--file-system|-f]\n"
|
---|
221 | " [--dereference|-L] [--terse|-t] [--verbose|-v] <file>...\n"
|
---|
222 | "\n");
|
---|
223 | }
|
---|
224 |
|
---|
225 |
|
---|
226 | /**
|
---|
227 | * Displays the program's version number.
|
---|
228 | */
|
---|
229 | static void vgsvcToolboxShowVersion(void)
|
---|
230 | {
|
---|
231 | RTPrintf("%sr%d\n", VBOX_VERSION_STRING, RTBldCfgRevision());
|
---|
232 | }
|
---|
233 |
|
---|
234 |
|
---|
235 | /**
|
---|
236 | * Initializes the parseable stream(s).
|
---|
237 | *
|
---|
238 | * @return IPRT status code.
|
---|
239 | */
|
---|
240 | static int vgsvcToolboxStrmInit(void)
|
---|
241 | {
|
---|
242 | /* Set stdout's mode to binary. This is required for outputting all the machine-readable
|
---|
243 | * data correctly. */
|
---|
244 | int rc = RTStrmSetMode(g_pStdOut, 1 /* Binary mode */, -1 /* Current code set, not changed */);
|
---|
245 | if (RT_FAILURE(rc))
|
---|
246 | RTMsgError("Unable to set stdout to binary mode, rc=%Rrc\n", rc);
|
---|
247 |
|
---|
248 | return rc;
|
---|
249 | }
|
---|
250 |
|
---|
251 |
|
---|
252 | /**
|
---|
253 | * Prints a parseable stream header which contains the actual tool
|
---|
254 | * which was called/used along with its stream version.
|
---|
255 | *
|
---|
256 | * @param pszToolName Name of the tool being used, e.g. "vbt_ls".
|
---|
257 | * @param uVersion Stream version name. Handy for distinguishing
|
---|
258 | * different stream versions later.
|
---|
259 | */
|
---|
260 | static void vgsvcToolboxPrintStrmHeader(const char *pszToolName, uint32_t uVersion)
|
---|
261 | {
|
---|
262 | AssertPtrReturnVoid(pszToolName);
|
---|
263 | RTPrintf("hdr_id=%s%chdr_ver=%u%c", pszToolName, 0, uVersion, 0);
|
---|
264 | }
|
---|
265 |
|
---|
266 |
|
---|
267 | /**
|
---|
268 | * Prints a standardized termination sequence indicating that the
|
---|
269 | * parseable stream just ended.
|
---|
270 | *
|
---|
271 | */
|
---|
272 | static void vgsvcToolboxPrintStrmTermination()
|
---|
273 | {
|
---|
274 | RTPrintf("%c%c%c%c", 0, 0, 0, 0);
|
---|
275 | }
|
---|
276 |
|
---|
277 |
|
---|
278 | /**
|
---|
279 | * Parse a file mode string from the command line (currently octal only)
|
---|
280 | * and print an error message and return an error if necessary.
|
---|
281 | */
|
---|
282 | static int vgsvcToolboxParseMode(const char *pcszMode, RTFMODE *pfMode)
|
---|
283 | {
|
---|
284 | int rc = RTStrToUInt32Ex(pcszMode, NULL, 8 /* Base */, pfMode);
|
---|
285 | if (RT_FAILURE(rc)) /* Only octet based values supported right now! */
|
---|
286 | RTMsgError("Mode flag strings not implemented yet! Use octal numbers instead. (%s)\n", pcszMode);
|
---|
287 | return rc;
|
---|
288 | }
|
---|
289 |
|
---|
290 |
|
---|
291 | /**
|
---|
292 | * Destroys a path buffer list.
|
---|
293 | *
|
---|
294 | * @return IPRT status code.
|
---|
295 | * @param pList Pointer to list to destroy.
|
---|
296 | */
|
---|
297 | static void vgsvcToolboxPathBufDestroy(PRTLISTNODE pList)
|
---|
298 | {
|
---|
299 | AssertPtr(pList);
|
---|
300 | /** @todo use RTListForEachSafe */
|
---|
301 | PVBOXSERVICETOOLBOXPATHENTRY pNode = RTListGetFirst(pList, VBOXSERVICETOOLBOXPATHENTRY, Node);
|
---|
302 | while (pNode)
|
---|
303 | {
|
---|
304 | PVBOXSERVICETOOLBOXPATHENTRY pNext = RTListNodeIsLast(pList, &pNode->Node)
|
---|
305 | ? NULL
|
---|
306 | : RTListNodeGetNext(&pNode->Node, VBOXSERVICETOOLBOXPATHENTRY, Node);
|
---|
307 | RTListNodeRemove(&pNode->Node);
|
---|
308 |
|
---|
309 | RTStrFree(pNode->pszName);
|
---|
310 |
|
---|
311 | RTMemFree(pNode);
|
---|
312 | pNode = pNext;
|
---|
313 | }
|
---|
314 | }
|
---|
315 |
|
---|
316 |
|
---|
317 | /**
|
---|
318 | * Adds a path entry (file/directory/whatever) to a given path buffer list.
|
---|
319 | *
|
---|
320 | * @return IPRT status code.
|
---|
321 | * @param pList Pointer to list to add entry to.
|
---|
322 | * @param pszName Name of entry to add.
|
---|
323 | */
|
---|
324 | static int vgsvcToolboxPathBufAddPathEntry(PRTLISTNODE pList, const char *pszName)
|
---|
325 | {
|
---|
326 | AssertPtrReturn(pList, VERR_INVALID_PARAMETER);
|
---|
327 |
|
---|
328 | int rc = VINF_SUCCESS;
|
---|
329 | PVBOXSERVICETOOLBOXPATHENTRY pNode = (PVBOXSERVICETOOLBOXPATHENTRY)RTMemAlloc(sizeof(VBOXSERVICETOOLBOXPATHENTRY));
|
---|
330 | if (pNode)
|
---|
331 | {
|
---|
332 | pNode->pszName = RTStrDup(pszName);
|
---|
333 | AssertPtr(pNode->pszName);
|
---|
334 |
|
---|
335 | RTListAppend(pList, &pNode->Node);
|
---|
336 | }
|
---|
337 | else
|
---|
338 | rc = VERR_NO_MEMORY;
|
---|
339 | return rc;
|
---|
340 | }
|
---|
341 |
|
---|
342 |
|
---|
343 | /**
|
---|
344 | * Performs the actual output operation of "vbox_cat".
|
---|
345 | *
|
---|
346 | * @return IPRT status code.
|
---|
347 | * @param hInput Handle of input file (if any) to use;
|
---|
348 | * else stdin will be used.
|
---|
349 | * @param hOutput Handle of output file (if any) to use;
|
---|
350 | * else stdout will be used.
|
---|
351 | */
|
---|
352 | static int vgsvcToolboxCatOutput(RTFILE hInput, RTFILE hOutput)
|
---|
353 | {
|
---|
354 | int rc = VINF_SUCCESS;
|
---|
355 | if (hInput == NIL_RTFILE)
|
---|
356 | {
|
---|
357 | rc = RTFileFromNative(&hInput, RTFILE_NATIVE_STDIN);
|
---|
358 | if (RT_FAILURE(rc))
|
---|
359 | RTMsgError("Could not translate input file to native handle, rc=%Rrc\n", rc);
|
---|
360 | }
|
---|
361 |
|
---|
362 | if (hOutput == NIL_RTFILE)
|
---|
363 | {
|
---|
364 | rc = RTFileFromNative(&hOutput, RTFILE_NATIVE_STDOUT);
|
---|
365 | if (RT_FAILURE(rc))
|
---|
366 | RTMsgError("Could not translate output file to native handle, rc=%Rrc\n", rc);
|
---|
367 | }
|
---|
368 |
|
---|
369 | if (RT_SUCCESS(rc))
|
---|
370 | {
|
---|
371 | uint8_t abBuf[_64K];
|
---|
372 | size_t cbRead;
|
---|
373 | for (;;)
|
---|
374 | {
|
---|
375 | rc = RTFileRead(hInput, abBuf, sizeof(abBuf), &cbRead);
|
---|
376 | if (RT_SUCCESS(rc) && cbRead > 0)
|
---|
377 | {
|
---|
378 | rc = RTFileWrite(hOutput, abBuf, cbRead, NULL /* Try to write all at once! */);
|
---|
379 | if (RT_FAILURE(rc))
|
---|
380 | {
|
---|
381 | RTMsgError("Error while writing output, rc=%Rrc\n", rc);
|
---|
382 | break;
|
---|
383 | }
|
---|
384 | }
|
---|
385 | else
|
---|
386 | {
|
---|
387 | if (rc == VERR_BROKEN_PIPE)
|
---|
388 | rc = VINF_SUCCESS;
|
---|
389 | else if (RT_FAILURE(rc))
|
---|
390 | RTMsgError("Error while reading input, rc=%Rrc\n", rc);
|
---|
391 | break;
|
---|
392 | }
|
---|
393 | }
|
---|
394 | }
|
---|
395 | return rc;
|
---|
396 | }
|
---|
397 |
|
---|
398 |
|
---|
399 | /** @todo Document options! */
|
---|
400 | static char g_paszCatHelp[] =
|
---|
401 | " VBoxService [--use-toolbox] vbox_cat [<general options>] <file>...\n\n"
|
---|
402 | "Concatenate files, or standard input, to standard output.\n"
|
---|
403 | "\n";
|
---|
404 |
|
---|
405 |
|
---|
406 | /**
|
---|
407 | * Main function for tool "vbox_cat".
|
---|
408 | *
|
---|
409 | * @return RTEXITCODE.
|
---|
410 | * @param argc Number of arguments.
|
---|
411 | * @param argv Pointer to argument array.
|
---|
412 | */
|
---|
413 | static RTEXITCODE vgsvcToolboxCat(int argc, char **argv)
|
---|
414 | {
|
---|
415 | static const RTGETOPTDEF s_aOptions[] =
|
---|
416 | {
|
---|
417 | /* Sorted by short ops. */
|
---|
418 | { "--show-all", 'a', RTGETOPT_REQ_NOTHING },
|
---|
419 | { "--number-nonblank", 'b', RTGETOPT_REQ_NOTHING},
|
---|
420 | { NULL, 'e', RTGETOPT_REQ_NOTHING},
|
---|
421 | { NULL, 'E', RTGETOPT_REQ_NOTHING},
|
---|
422 | { "--flags", 'f', RTGETOPT_REQ_STRING},
|
---|
423 | { "--no-content-indexed", VBOXSERVICETOOLBOXCATOPT_NO_CONTENT_INDEXED, RTGETOPT_REQ_NOTHING},
|
---|
424 | { "--number", 'n', RTGETOPT_REQ_NOTHING},
|
---|
425 | { "--output", 'o', RTGETOPT_REQ_STRING},
|
---|
426 | { "--squeeze-blank", 's', RTGETOPT_REQ_NOTHING},
|
---|
427 | { NULL, 't', RTGETOPT_REQ_NOTHING},
|
---|
428 | { "--show-tabs", 'T', RTGETOPT_REQ_NOTHING},
|
---|
429 | { NULL, 'u', RTGETOPT_REQ_NOTHING},
|
---|
430 | { "--show-noneprinting", 'v', RTGETOPT_REQ_NOTHING}
|
---|
431 | };
|
---|
432 |
|
---|
433 | int ch;
|
---|
434 | RTGETOPTUNION ValueUnion;
|
---|
435 | RTGETOPTSTATE GetState;
|
---|
436 |
|
---|
437 | RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 1 /*iFirst*/, 0 /*fFlags*/);
|
---|
438 |
|
---|
439 | int rc = VINF_SUCCESS;
|
---|
440 |
|
---|
441 | const char *pszOutput = NULL;
|
---|
442 | RTFILE hOutput = NIL_RTFILE;
|
---|
443 | uint32_t fFlags = RTFILE_O_CREATE_REPLACE /* Output file flags. */
|
---|
444 | | RTFILE_O_WRITE
|
---|
445 | | RTFILE_O_DENY_WRITE;
|
---|
446 |
|
---|
447 | /* Init directory list. */
|
---|
448 | RTLISTANCHOR inputList;
|
---|
449 | RTListInit(&inputList);
|
---|
450 |
|
---|
451 | while ( (ch = RTGetOpt(&GetState, &ValueUnion))
|
---|
452 | && RT_SUCCESS(rc))
|
---|
453 | {
|
---|
454 | /* For options that require an argument, ValueUnion has received the value. */
|
---|
455 | switch (ch)
|
---|
456 | {
|
---|
457 | case 'a':
|
---|
458 | case 'b':
|
---|
459 | case 'e':
|
---|
460 | case 'E':
|
---|
461 | case 'n':
|
---|
462 | case 's':
|
---|
463 | case 't':
|
---|
464 | case 'T':
|
---|
465 | case 'v':
|
---|
466 | RTMsgError("Sorry, option '%s' is not implemented yet!\n",
|
---|
467 | ValueUnion.pDef->pszLong);
|
---|
468 | rc = VERR_INVALID_PARAMETER;
|
---|
469 | break;
|
---|
470 |
|
---|
471 | case 'h':
|
---|
472 | vgsvcToolboxShowUsageHeader();
|
---|
473 | RTPrintf("%s", g_paszCatHelp);
|
---|
474 | return RTEXITCODE_SUCCESS;
|
---|
475 |
|
---|
476 | case 'o':
|
---|
477 | pszOutput = ValueUnion.psz;
|
---|
478 | break;
|
---|
479 |
|
---|
480 | case 'u':
|
---|
481 | /* Ignored. */
|
---|
482 | break;
|
---|
483 |
|
---|
484 | case 'V':
|
---|
485 | vgsvcToolboxShowVersion();
|
---|
486 | return RTEXITCODE_SUCCESS;
|
---|
487 |
|
---|
488 | case VBOXSERVICETOOLBOXCATOPT_NO_CONTENT_INDEXED:
|
---|
489 | fFlags |= RTFILE_O_NOT_CONTENT_INDEXED;
|
---|
490 | break;
|
---|
491 |
|
---|
492 | case VINF_GETOPT_NOT_OPTION:
|
---|
493 | /* Add file(s) to buffer. This enables processing multiple paths
|
---|
494 | * at once.
|
---|
495 | *
|
---|
496 | * Since the non-options (RTGETOPTINIT_FLAGS_OPTS_FIRST) come last when
|
---|
497 | * processing this loop it's safe to immediately exit on syntax errors
|
---|
498 | * or showing the help text (see above). */
|
---|
499 | rc = vgsvcToolboxPathBufAddPathEntry(&inputList, ValueUnion.psz);
|
---|
500 | break;
|
---|
501 |
|
---|
502 | default:
|
---|
503 | return RTGetOptPrintError(ch, &ValueUnion);
|
---|
504 | }
|
---|
505 | }
|
---|
506 |
|
---|
507 | if (RT_SUCCESS(rc))
|
---|
508 | {
|
---|
509 | if (pszOutput)
|
---|
510 | {
|
---|
511 | rc = RTFileOpen(&hOutput, pszOutput, fFlags);
|
---|
512 | if (RT_FAILURE(rc))
|
---|
513 | RTMsgError("Could not create output file '%s', rc=%Rrc\n", pszOutput, rc);
|
---|
514 | }
|
---|
515 |
|
---|
516 | if (RT_SUCCESS(rc))
|
---|
517 | {
|
---|
518 | /* Process each input file. */
|
---|
519 | RTFILE hInput = NIL_RTFILE;
|
---|
520 | PVBOXSERVICETOOLBOXPATHENTRY pNodeIt;
|
---|
521 | RTListForEach(&inputList, pNodeIt, VBOXSERVICETOOLBOXPATHENTRY, Node)
|
---|
522 | {
|
---|
523 | rc = RTFileOpen(&hInput, pNodeIt->pszName,
|
---|
524 | RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_WRITE);
|
---|
525 | if (RT_SUCCESS(rc))
|
---|
526 | {
|
---|
527 | rc = vgsvcToolboxCatOutput(hInput, hOutput);
|
---|
528 | RTFileClose(hInput);
|
---|
529 | }
|
---|
530 | else
|
---|
531 | {
|
---|
532 | PCRTSTATUSMSG pMsg = RTErrGet(rc);
|
---|
533 | if (pMsg)
|
---|
534 | RTMsgError("Could not open input file '%s': %s\n", pNodeIt->pszName, pMsg->pszMsgFull);
|
---|
535 | else
|
---|
536 | RTMsgError("Could not open input file '%s', rc=%Rrc\n", pNodeIt->pszName, rc);
|
---|
537 | }
|
---|
538 |
|
---|
539 | if (RT_FAILURE(rc))
|
---|
540 | break;
|
---|
541 | }
|
---|
542 |
|
---|
543 | /* If no input files were defined, process stdin. */
|
---|
544 | if (RTListNodeIsFirst(&inputList, &inputList))
|
---|
545 | rc = vgsvcToolboxCatOutput(hInput, hOutput);
|
---|
546 | }
|
---|
547 | }
|
---|
548 |
|
---|
549 | if (hOutput != NIL_RTFILE)
|
---|
550 | RTFileClose(hOutput);
|
---|
551 | vgsvcToolboxPathBufDestroy(&inputList);
|
---|
552 |
|
---|
553 | if (RT_FAILURE(rc))
|
---|
554 | {
|
---|
555 | switch (rc)
|
---|
556 | {
|
---|
557 | case VERR_ACCESS_DENIED:
|
---|
558 | return (RTEXITCODE)VBOXSERVICETOOLBOX_CAT_EXITCODE_ACCESS_DENIED;
|
---|
559 |
|
---|
560 | case VERR_FILE_NOT_FOUND:
|
---|
561 | return (RTEXITCODE)VBOXSERVICETOOLBOX_CAT_EXITCODE_FILE_NOT_FOUND;
|
---|
562 |
|
---|
563 | case VERR_PATH_NOT_FOUND:
|
---|
564 | return (RTEXITCODE)VBOXSERVICETOOLBOX_CAT_EXITCODE_PATH_NOT_FOUND;
|
---|
565 |
|
---|
566 | case VERR_SHARING_VIOLATION:
|
---|
567 | return (RTEXITCODE)VBOXSERVICETOOLBOX_CAT_EXITCODE_SHARING_VIOLATION;
|
---|
568 |
|
---|
569 | case VERR_IS_A_DIRECTORY:
|
---|
570 | return (RTEXITCODE)VBOXSERVICETOOLBOX_CAT_EXITCODE_IS_A_DIRECTORY;
|
---|
571 |
|
---|
572 | default:
|
---|
573 | #ifdef DEBUG_andy
|
---|
574 | AssertMsgFailed(("Exit code for %Rrc not implemented\n", rc));
|
---|
575 | #endif
|
---|
576 | break;
|
---|
577 | }
|
---|
578 |
|
---|
579 | return RTEXITCODE_FAILURE;
|
---|
580 | }
|
---|
581 |
|
---|
582 | return RTEXITCODE_SUCCESS;
|
---|
583 | }
|
---|
584 |
|
---|
585 |
|
---|
586 | /**
|
---|
587 | * Resolves the UID to a name as best as we can.
|
---|
588 | *
|
---|
589 | * @returns Read-only name string. Only valid till the next cache call.
|
---|
590 | * @param pIdCache The ID cache.
|
---|
591 | * @param uid The UID to resolve.
|
---|
592 | * @param pszEntry The filename of the UID.
|
---|
593 | * @param pszRelativeTo What @a pszEntry is relative to, NULL if absolute.
|
---|
594 | */
|
---|
595 | static const char *vgsvcToolboxIdCacheGetUidName(PVGSVCTOOLBOXIDCACHE pIdCache, RTUID uid,
|
---|
596 | const char *pszEntry, const char *pszRelativeTo)
|
---|
597 | {
|
---|
598 | /* Check cached entries. */
|
---|
599 | for (uint32_t i = 0; i < pIdCache->cEntries; i++)
|
---|
600 | if ( pIdCache->aEntries[i].id == uid
|
---|
601 | && pIdCache->aEntries[i].fIsUid)
|
---|
602 | return pIdCache->aEntries[i].szName;
|
---|
603 |
|
---|
604 | /* Miss. */
|
---|
605 | RTFSOBJINFO ObjInfo;
|
---|
606 | RT_ZERO(ObjInfo); /* shut up msc */
|
---|
607 | int rc;
|
---|
608 | if (!pszRelativeTo)
|
---|
609 | rc = RTPathQueryInfoEx(pszEntry, &ObjInfo, RTFSOBJATTRADD_UNIX_OWNER, RTPATH_F_ON_LINK);
|
---|
610 | else
|
---|
611 | {
|
---|
612 | char szPath[RTPATH_MAX];
|
---|
613 | rc = RTPathJoin(szPath, sizeof(szPath), pszRelativeTo, pszEntry);
|
---|
614 | if (RT_SUCCESS(rc))
|
---|
615 | rc = RTPathQueryInfoEx(szPath, &ObjInfo, RTFSOBJATTRADD_UNIX_OWNER, RTPATH_F_ON_LINK);
|
---|
616 | }
|
---|
617 |
|
---|
618 | if ( RT_SUCCESS(rc)
|
---|
619 | && ObjInfo.Attr.u.UnixOwner.uid == uid)
|
---|
620 | {
|
---|
621 | uint32_t i = pIdCache->cEntries;
|
---|
622 | if (i < RT_ELEMENTS(pIdCache->aEntries))
|
---|
623 | pIdCache->cEntries = i + 1;
|
---|
624 | else
|
---|
625 | i = pIdCache->iNextReplace++ % RT_ELEMENTS(pIdCache->aEntries);
|
---|
626 | pIdCache->aEntries[i].id = uid;
|
---|
627 | pIdCache->aEntries[i].fIsUid = true;
|
---|
628 | RTStrCopy(pIdCache->aEntries[i].szName, sizeof(pIdCache->aEntries[i].szName), ObjInfo.Attr.u.UnixOwner.szName);
|
---|
629 | return pIdCache->aEntries[i].szName;
|
---|
630 | }
|
---|
631 | return "";
|
---|
632 | }
|
---|
633 |
|
---|
634 |
|
---|
635 | /**
|
---|
636 | * Resolves the GID to a name as best as we can.
|
---|
637 | *
|
---|
638 | * @returns Read-only name string. Only valid till the next cache call.
|
---|
639 | * @param pIdCache The ID cache.
|
---|
640 | * @param gid The GID to resolve.
|
---|
641 | * @param pszEntry The filename of the GID.
|
---|
642 | * @param pszRelativeTo What @a pszEntry is relative to, NULL if absolute.
|
---|
643 | */
|
---|
644 | static const char *vgsvcToolboxIdCacheGetGidName(PVGSVCTOOLBOXIDCACHE pIdCache, RTGID gid,
|
---|
645 | const char *pszEntry, const char *pszRelativeTo)
|
---|
646 | {
|
---|
647 | /* Check cached entries. */
|
---|
648 | for (uint32_t i = 0; i < pIdCache->cEntries; i++)
|
---|
649 | if ( pIdCache->aEntries[i].id == gid
|
---|
650 | && !pIdCache->aEntries[i].fIsUid)
|
---|
651 | return pIdCache->aEntries[i].szName;
|
---|
652 |
|
---|
653 | /* Miss. */
|
---|
654 | RTFSOBJINFO ObjInfo;
|
---|
655 | RT_ZERO(ObjInfo); /* shut up msc */
|
---|
656 | int rc;
|
---|
657 | if (!pszRelativeTo)
|
---|
658 | rc = RTPathQueryInfoEx(pszEntry, &ObjInfo, RTFSOBJATTRADD_UNIX_GROUP, RTPATH_F_ON_LINK);
|
---|
659 | else
|
---|
660 | {
|
---|
661 | char szPath[RTPATH_MAX];
|
---|
662 | rc = RTPathJoin(szPath, sizeof(szPath), pszRelativeTo, pszEntry);
|
---|
663 | if (RT_SUCCESS(rc))
|
---|
664 | rc = RTPathQueryInfoEx(szPath, &ObjInfo, RTFSOBJATTRADD_UNIX_GROUP, RTPATH_F_ON_LINK);
|
---|
665 | }
|
---|
666 |
|
---|
667 | if ( RT_SUCCESS(rc)
|
---|
668 | && ObjInfo.Attr.u.UnixGroup.gid == gid)
|
---|
669 | {
|
---|
670 | uint32_t i = pIdCache->cEntries;
|
---|
671 | if (i < RT_ELEMENTS(pIdCache->aEntries))
|
---|
672 | pIdCache->cEntries = i + 1;
|
---|
673 | else
|
---|
674 | i = pIdCache->iNextReplace++ % RT_ELEMENTS(pIdCache->aEntries);
|
---|
675 | pIdCache->aEntries[i].id = gid;
|
---|
676 | pIdCache->aEntries[i].fIsUid = false;
|
---|
677 | RTStrCopy(pIdCache->aEntries[i].szName, sizeof(pIdCache->aEntries[i].szName), ObjInfo.Attr.u.UnixGroup.szName);
|
---|
678 | return pIdCache->aEntries[i].szName;
|
---|
679 | }
|
---|
680 | return "";
|
---|
681 | }
|
---|
682 |
|
---|
683 |
|
---|
684 | /**
|
---|
685 | * Prints information (based on given flags) of a file system object (file/directory/...)
|
---|
686 | * to stdout.
|
---|
687 | *
|
---|
688 | * @return IPRT status code.
|
---|
689 | * @param pszName Object name.
|
---|
690 | * @param cchName Length of pszName.
|
---|
691 | * @param fOutputFlags Output / handling flags of type
|
---|
692 | * VBOXSERVICETOOLBOXOUTPUTFLAG.
|
---|
693 | * @param pszRelativeTo What pszName is relative to.
|
---|
694 | * @param pIdCache The ID cache.
|
---|
695 | * @param pObjInfo Pointer to object information.
|
---|
696 | */
|
---|
697 | static int vgsvcToolboxPrintFsInfo(const char *pszName, size_t cchName, uint32_t fOutputFlags, const char *pszRelativeTo,
|
---|
698 | PVGSVCTOOLBOXIDCACHE pIdCache, PRTFSOBJINFO pObjInfo)
|
---|
699 | {
|
---|
700 | AssertPtrReturn(pszName, VERR_INVALID_POINTER);
|
---|
701 | AssertReturn(cchName, VERR_INVALID_PARAMETER);
|
---|
702 | AssertPtrReturn(pObjInfo, VERR_INVALID_POINTER);
|
---|
703 |
|
---|
704 | RTFMODE fMode = pObjInfo->Attr.fMode;
|
---|
705 | char chFileType;
|
---|
706 | switch (fMode & RTFS_TYPE_MASK)
|
---|
707 | {
|
---|
708 | case RTFS_TYPE_FIFO: chFileType = 'f'; break;
|
---|
709 | case RTFS_TYPE_DEV_CHAR: chFileType = 'c'; break;
|
---|
710 | case RTFS_TYPE_DIRECTORY: chFileType = 'd'; break;
|
---|
711 | case RTFS_TYPE_DEV_BLOCK: chFileType = 'b'; break;
|
---|
712 | case RTFS_TYPE_FILE: chFileType = '-'; break;
|
---|
713 | case RTFS_TYPE_SYMLINK: chFileType = 'l'; break;
|
---|
714 | case RTFS_TYPE_SOCKET: chFileType = 's'; break;
|
---|
715 | case RTFS_TYPE_WHITEOUT: chFileType = 'w'; break;
|
---|
716 | default: chFileType = '?'; break;
|
---|
717 | }
|
---|
718 | /** @todo sticy bits++ */
|
---|
719 |
|
---|
720 | /** @todo r=bird: turns out the host doesn't use or need cname_len, so perhaps we could drop it? */
|
---|
721 | if (!(fOutputFlags & VBOXSERVICETOOLBOXOUTPUTFLAG_LONG))
|
---|
722 | {
|
---|
723 | if (fOutputFlags & VBOXSERVICETOOLBOXOUTPUTFLAG_PARSEABLE)
|
---|
724 | {
|
---|
725 | RTPrintf("ftype=%c%cnode_id=%RU64%inode_dev=%RU32%ccname_len=%zu%cname=%s%c",
|
---|
726 | chFileType, 0, (uint64_t)pObjInfo->Attr.u.Unix.INodeId, 0,
|
---|
727 | (uint32_t)pObjInfo->Attr.u.Unix.INodeIdDevice, 0, cchName, 0, pszName, 0);
|
---|
728 | RTPrintf("%c%c", 0, 0);
|
---|
729 | }
|
---|
730 | else
|
---|
731 | RTPrintf("%c %#18llx %3zu %s\n", chFileType, (uint64_t)pObjInfo->Attr.u.Unix.INodeId, cchName, pszName);
|
---|
732 | }
|
---|
733 | else
|
---|
734 | {
|
---|
735 | char szTimeBirth[RTTIME_STR_LEN];
|
---|
736 | char szTimeChange[RTTIME_STR_LEN];
|
---|
737 | char szTimeModification[RTTIME_STR_LEN];
|
---|
738 | char szTimeAccess[RTTIME_STR_LEN];
|
---|
739 |
|
---|
740 | if (fOutputFlags & VBOXSERVICETOOLBOXOUTPUTFLAG_PARSEABLE)
|
---|
741 | {
|
---|
742 | RTPrintf("ftype=%c%c", chFileType, 0);
|
---|
743 | if (pObjInfo->Attr.u.Unix.INodeId || pObjInfo->Attr.u.Unix.INodeIdDevice)
|
---|
744 | RTPrintf("node_id=%RU64%cinode_dev=%RU32%c", (uint64_t)pObjInfo->Attr.u.Unix.INodeId, 0,
|
---|
745 | (uint32_t)pObjInfo->Attr.u.Unix.INodeIdDevice, 0);
|
---|
746 | RTPrintf("owner_mask=%c%c%c%c",
|
---|
747 | fMode & RTFS_UNIX_IRUSR ? 'r' : '-',
|
---|
748 | fMode & RTFS_UNIX_IWUSR ? 'w' : '-',
|
---|
749 | fMode & RTFS_UNIX_IXUSR ? 'x' : '-', 0);
|
---|
750 | RTPrintf("group_mask=%c%c%c%c",
|
---|
751 | fMode & RTFS_UNIX_IRGRP ? 'r' : '-',
|
---|
752 | fMode & RTFS_UNIX_IWGRP ? 'w' : '-',
|
---|
753 | fMode & RTFS_UNIX_IXGRP ? 'x' : '-', 0);
|
---|
754 | RTPrintf("other_mask=%c%c%c%c",
|
---|
755 | fMode & RTFS_UNIX_IROTH ? 'r' : '-',
|
---|
756 | fMode & RTFS_UNIX_IWOTH ? 'w' : '-',
|
---|
757 | fMode & RTFS_UNIX_IXOTH ? 'x' : '-', 0);
|
---|
758 | /** @todo sticky bits. */
|
---|
759 | RTPrintf("dos_mask=%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c",
|
---|
760 | fMode & RTFS_DOS_READONLY ? 'R' : '-',
|
---|
761 | fMode & RTFS_DOS_HIDDEN ? 'H' : '-',
|
---|
762 | fMode & RTFS_DOS_SYSTEM ? 'S' : '-',
|
---|
763 | fMode & RTFS_DOS_DIRECTORY ? 'D' : '-',
|
---|
764 | fMode & RTFS_DOS_ARCHIVED ? 'A' : '-',
|
---|
765 | fMode & RTFS_DOS_NT_DEVICE ? 'd' : '-',
|
---|
766 | fMode & RTFS_DOS_NT_NORMAL ? 'N' : '-',
|
---|
767 | fMode & RTFS_DOS_NT_TEMPORARY ? 'T' : '-',
|
---|
768 | fMode & RTFS_DOS_NT_SPARSE_FILE ? 'P' : '-',
|
---|
769 | fMode & RTFS_DOS_NT_REPARSE_POINT ? 'J' : '-',
|
---|
770 | fMode & RTFS_DOS_NT_COMPRESSED ? 'C' : '-',
|
---|
771 | fMode & RTFS_DOS_NT_OFFLINE ? 'O' : '-',
|
---|
772 | fMode & RTFS_DOS_NT_NOT_CONTENT_INDEXED ? 'I' : '-',
|
---|
773 | fMode & RTFS_DOS_NT_ENCRYPTED ? 'E' : '-', 0);
|
---|
774 | RTPrintf("hlinks=%RU32%cst_size=%RI64%calloc=%RI64%c",
|
---|
775 | pObjInfo->Attr.u.Unix.cHardlinks, 0,
|
---|
776 | pObjInfo->cbObject, 0,
|
---|
777 | pObjInfo->cbAllocated, 0);
|
---|
778 | RTPrintf("st_birthtime=%s%cst_ctime=%s%cst_mtime=%s%cst_atime=%s%c",
|
---|
779 | RTTimeSpecToString(&pObjInfo->BirthTime, szTimeBirth, sizeof(szTimeBirth)), 0,
|
---|
780 | RTTimeSpecToString(&pObjInfo->ChangeTime, szTimeChange, sizeof(szTimeChange)), 0,
|
---|
781 | RTTimeSpecToString(&pObjInfo->ModificationTime, szTimeModification, sizeof(szTimeModification)), 0,
|
---|
782 | RTTimeSpecToString(&pObjInfo->AccessTime, szTimeAccess, sizeof(szTimeAccess)), 0);
|
---|
783 | if (pObjInfo->Attr.u.Unix.uid != NIL_RTUID)
|
---|
784 | RTPrintf("uid=%RU32%cusername=%s%c", pObjInfo->Attr.u.Unix.uid, 0,
|
---|
785 | vgsvcToolboxIdCacheGetUidName(pIdCache, pObjInfo->Attr.u.Unix.uid, pszName, pszRelativeTo), 0);
|
---|
786 | if (pObjInfo->Attr.u.Unix.gid != NIL_RTGID)
|
---|
787 | RTPrintf("gid=%RU32%cgroupname=%s%c", pObjInfo->Attr.u.Unix.gid, 0,
|
---|
788 | vgsvcToolboxIdCacheGetGidName(pIdCache, pObjInfo->Attr.u.Unix.gid, pszName, pszRelativeTo), 0);
|
---|
789 | if ( (RTFS_IS_DEV_BLOCK(pObjInfo->Attr.fMode) || RTFS_IS_DEV_CHAR(pObjInfo->Attr.fMode))
|
---|
790 | && pObjInfo->Attr.u.Unix.Device)
|
---|
791 | RTPrintf("st_rdev=%RU32%c", pObjInfo->Attr.u.Unix.Device, 0);
|
---|
792 | if (pObjInfo->Attr.u.Unix.GenerationId)
|
---|
793 | RTPrintf("st_gen=%RU32%c", pObjInfo->Attr.u.Unix.GenerationId, 0);
|
---|
794 | if (pObjInfo->Attr.u.Unix.fFlags)
|
---|
795 | RTPrintf("st_flags=%RU32%c", pObjInfo->Attr.u.Unix.fFlags, 0);
|
---|
796 | RTPrintf("cname_len=%zu%cname=%s%c", cchName, 0, pszName, 0);
|
---|
797 | RTPrintf("%c%c", 0, 0); /* End of data block. */
|
---|
798 | }
|
---|
799 | else
|
---|
800 | {
|
---|
801 | RTPrintf("%c", chFileType);
|
---|
802 | RTPrintf("%c%c%c",
|
---|
803 | fMode & RTFS_UNIX_IRUSR ? 'r' : '-',
|
---|
804 | fMode & RTFS_UNIX_IWUSR ? 'w' : '-',
|
---|
805 | fMode & RTFS_UNIX_IXUSR ? 'x' : '-');
|
---|
806 | RTPrintf("%c%c%c",
|
---|
807 | fMode & RTFS_UNIX_IRGRP ? 'r' : '-',
|
---|
808 | fMode & RTFS_UNIX_IWGRP ? 'w' : '-',
|
---|
809 | fMode & RTFS_UNIX_IXGRP ? 'x' : '-');
|
---|
810 | RTPrintf("%c%c%c",
|
---|
811 | fMode & RTFS_UNIX_IROTH ? 'r' : '-',
|
---|
812 | fMode & RTFS_UNIX_IWOTH ? 'w' : '-',
|
---|
813 | fMode & RTFS_UNIX_IXOTH ? 'x' : '-');
|
---|
814 | RTPrintf(" %c%c%c%c%c%c%c%c%c%c%c%c%c%c",
|
---|
815 | fMode & RTFS_DOS_READONLY ? 'R' : '-',
|
---|
816 | fMode & RTFS_DOS_HIDDEN ? 'H' : '-',
|
---|
817 | fMode & RTFS_DOS_SYSTEM ? 'S' : '-',
|
---|
818 | fMode & RTFS_DOS_DIRECTORY ? 'D' : '-',
|
---|
819 | fMode & RTFS_DOS_ARCHIVED ? 'A' : '-',
|
---|
820 | fMode & RTFS_DOS_NT_DEVICE ? 'd' : '-',
|
---|
821 | fMode & RTFS_DOS_NT_NORMAL ? 'N' : '-',
|
---|
822 | fMode & RTFS_DOS_NT_TEMPORARY ? 'T' : '-',
|
---|
823 | fMode & RTFS_DOS_NT_SPARSE_FILE ? 'P' : '-',
|
---|
824 | fMode & RTFS_DOS_NT_REPARSE_POINT ? 'J' : '-',
|
---|
825 | fMode & RTFS_DOS_NT_COMPRESSED ? 'C' : '-',
|
---|
826 | fMode & RTFS_DOS_NT_OFFLINE ? 'O' : '-',
|
---|
827 | fMode & RTFS_DOS_NT_NOT_CONTENT_INDEXED ? 'I' : '-',
|
---|
828 | fMode & RTFS_DOS_NT_ENCRYPTED ? 'E' : '-');
|
---|
829 | RTPrintf(" %d %4d %4d %10lld %10lld",
|
---|
830 | pObjInfo->Attr.u.Unix.cHardlinks,
|
---|
831 | pObjInfo->Attr.u.Unix.uid,
|
---|
832 | pObjInfo->Attr.u.Unix.gid,
|
---|
833 | pObjInfo->cbObject,
|
---|
834 | pObjInfo->cbAllocated);
|
---|
835 | RTPrintf(" %s %s %s %s",
|
---|
836 | RTTimeSpecToString(&pObjInfo->BirthTime, szTimeBirth, sizeof(szTimeBirth)),
|
---|
837 | RTTimeSpecToString(&pObjInfo->ChangeTime, szTimeChange, sizeof(szTimeChange)),
|
---|
838 | RTTimeSpecToString(&pObjInfo->ModificationTime, szTimeModification, sizeof(szTimeModification)),
|
---|
839 | RTTimeSpecToString(&pObjInfo->AccessTime, szTimeAccess, sizeof(szTimeAccess)) );
|
---|
840 | RTPrintf(" %2zu %s\n", cchName, pszName);
|
---|
841 | }
|
---|
842 | }
|
---|
843 |
|
---|
844 | return VINF_SUCCESS;
|
---|
845 | }
|
---|
846 |
|
---|
847 |
|
---|
848 | /**
|
---|
849 | * Helper routine for ls tool doing the actual parsing and output of
|
---|
850 | * a specified directory.
|
---|
851 | *
|
---|
852 | * @return IPRT status code.
|
---|
853 | * @param pszDir Directory (path) to ouptut.
|
---|
854 | * @param fFlags Flags of type VBOXSERVICETOOLBOXLSFLAG.
|
---|
855 | * @param fOutputFlags Flags of type VBOXSERVICETOOLBOXOUTPUTFLAG.
|
---|
856 | */
|
---|
857 | static int vgsvcToolboxLsHandleDir(const char *pszDir, uint32_t fFlags, uint32_t fOutputFlags, PVGSVCTOOLBOXIDCACHE pIdCache)
|
---|
858 | {
|
---|
859 | AssertPtrReturn(pszDir, VERR_INVALID_PARAMETER);
|
---|
860 |
|
---|
861 | if (fFlags & VBOXSERVICETOOLBOXOUTPUTFLAG_PARSEABLE)
|
---|
862 | RTPrintf("dname=%s%c", pszDir, 0);
|
---|
863 | else if (fFlags & VBOXSERVICETOOLBOXLSFLAG_RECURSIVE)
|
---|
864 | RTPrintf("%s:\n", pszDir);
|
---|
865 |
|
---|
866 | char szPathAbs[RTPATH_MAX + 1];
|
---|
867 | int rc = RTPathAbs(pszDir, szPathAbs, sizeof(szPathAbs));
|
---|
868 | if (RT_FAILURE(rc))
|
---|
869 | {
|
---|
870 | if (!(fOutputFlags & VBOXSERVICETOOLBOXOUTPUTFLAG_PARSEABLE))
|
---|
871 | RTMsgError("Failed to retrieve absolute path of '%s', rc=%Rrc\n", pszDir, rc);
|
---|
872 | return rc;
|
---|
873 | }
|
---|
874 |
|
---|
875 | RTDIR hDir;
|
---|
876 | rc = RTDirOpen(&hDir, szPathAbs);
|
---|
877 | if (RT_FAILURE(rc))
|
---|
878 | {
|
---|
879 | if (!(fOutputFlags & VBOXSERVICETOOLBOXOUTPUTFLAG_PARSEABLE))
|
---|
880 | RTMsgError("Failed to open directory '%s', rc=%Rrc\n", szPathAbs, rc);
|
---|
881 | return rc;
|
---|
882 | }
|
---|
883 |
|
---|
884 | RTLISTANCHOR dirList;
|
---|
885 | RTListInit(&dirList);
|
---|
886 |
|
---|
887 | /* To prevent races we need to read in the directory entries once
|
---|
888 | * and process them afterwards: First loop is displaying the current
|
---|
889 | * directory's content and second loop is diving deeper into
|
---|
890 | * sub directories (if wanted). */
|
---|
891 | /** @todo r=bird: Which races are these exactly??? Please, do considering that directory with half a
|
---|
892 | * million files in it, because this isn't going to fly well there (especially not in the recursive case)...
|
---|
893 | * So, this needs to be rewritten unless there is an actual race you're avoiding by doing this! */
|
---|
894 | do
|
---|
895 | {
|
---|
896 | RTDIRENTRYEX DirEntry;
|
---|
897 | rc = RTDirReadEx(hDir, &DirEntry, NULL, RTFSOBJATTRADD_UNIX, RTPATH_F_ON_LINK);
|
---|
898 | if (RT_SUCCESS(rc))
|
---|
899 | {
|
---|
900 | PVBOXSERVICETOOLBOXDIRENTRY pNode = (PVBOXSERVICETOOLBOXDIRENTRY)RTMemAlloc(sizeof(VBOXSERVICETOOLBOXDIRENTRY));
|
---|
901 | if (pNode)
|
---|
902 | {
|
---|
903 | memcpy(&pNode->dirEntry, &DirEntry, sizeof(RTDIRENTRYEX));
|
---|
904 | RTListAppend(&dirList, &pNode->Node);
|
---|
905 | }
|
---|
906 | else
|
---|
907 | rc = VERR_NO_MEMORY;
|
---|
908 | }
|
---|
909 | /** @todo r=bird: missing DirEntry overflow handling. */
|
---|
910 | } while (RT_SUCCESS(rc));
|
---|
911 |
|
---|
912 | if (rc == VERR_NO_MORE_FILES)
|
---|
913 | rc = VINF_SUCCESS;
|
---|
914 |
|
---|
915 | int rc2 = RTDirClose(hDir);
|
---|
916 | if (RT_FAILURE(rc2))
|
---|
917 | {
|
---|
918 | if (!(fOutputFlags & VBOXSERVICETOOLBOXOUTPUTFLAG_PARSEABLE))
|
---|
919 | RTMsgError("Failed to close dir '%s', rc=%Rrc\n", pszDir, rc2);
|
---|
920 | if (RT_SUCCESS(rc))
|
---|
921 | rc = rc2;
|
---|
922 | }
|
---|
923 |
|
---|
924 | if (RT_SUCCESS(rc))
|
---|
925 | {
|
---|
926 | PVBOXSERVICETOOLBOXDIRENTRY pNodeIt;
|
---|
927 | RTListForEach(&dirList, pNodeIt, VBOXSERVICETOOLBOXDIRENTRY, Node)
|
---|
928 | {
|
---|
929 | rc = vgsvcToolboxPrintFsInfo(pNodeIt->dirEntry.szName, pNodeIt->dirEntry.cbName, fOutputFlags,
|
---|
930 | szPathAbs, pIdCache, &pNodeIt->dirEntry.Info);
|
---|
931 | if (RT_FAILURE(rc))
|
---|
932 | break;
|
---|
933 | }
|
---|
934 |
|
---|
935 | /* If everything went fine we do the second run (if needed) ... */
|
---|
936 | if ( RT_SUCCESS(rc)
|
---|
937 | && (fFlags & VBOXSERVICETOOLBOXLSFLAG_RECURSIVE))
|
---|
938 | {
|
---|
939 | /* Process all sub-directories. */
|
---|
940 | RTListForEach(&dirList, pNodeIt, VBOXSERVICETOOLBOXDIRENTRY, Node)
|
---|
941 | {
|
---|
942 | RTFMODE fMode = pNodeIt->dirEntry.Info.Attr.fMode;
|
---|
943 | switch (fMode & RTFS_TYPE_MASK)
|
---|
944 | {
|
---|
945 | case RTFS_TYPE_SYMLINK:
|
---|
946 | if (!(fFlags & VBOXSERVICETOOLBOXLSFLAG_SYMLINKS))
|
---|
947 | break;
|
---|
948 | RT_FALL_THRU();
|
---|
949 | case RTFS_TYPE_DIRECTORY:
|
---|
950 | {
|
---|
951 | const char *pszName = pNodeIt->dirEntry.szName;
|
---|
952 | if ( !RTStrICmp(pszName, ".") /** @todo r=bird: Please do explain what the upper/lower casing of '.' is! I'm really curious. */
|
---|
953 | || !RTStrICmp(pszName, "..")) /** @todo r=bird: There is a RTDir API for checking these. Use it! */
|
---|
954 | {
|
---|
955 | /* Skip dot directories. */
|
---|
956 | continue;
|
---|
957 | }
|
---|
958 |
|
---|
959 | char szPath[RTPATH_MAX]; /** @todo r=bird: This is going to kill your stack pretty quickly if deep
|
---|
960 | * directory nesting. There is another buffer further up the function too.
|
---|
961 | * You need to share the path buffer between recursions! There should be
|
---|
962 | * several examples of how to efficiently traverse a tree. */
|
---|
963 | rc = RTPathJoin(szPath, sizeof(szPath), pszDir, pNodeIt->dirEntry.szName);
|
---|
964 | if (RT_SUCCESS(rc))
|
---|
965 | rc = vgsvcToolboxLsHandleDir(szPath, fFlags, fOutputFlags, pIdCache);
|
---|
966 | break;
|
---|
967 | }
|
---|
968 |
|
---|
969 | default: /* Ignore the rest. */
|
---|
970 | break;
|
---|
971 | }
|
---|
972 | if (RT_FAILURE(rc))
|
---|
973 | break;
|
---|
974 | }
|
---|
975 | }
|
---|
976 | }
|
---|
977 |
|
---|
978 | /* Clean up the mess. */
|
---|
979 | PVBOXSERVICETOOLBOXDIRENTRY pNode, pSafe;
|
---|
980 | RTListForEachSafe(&dirList, pNode, pSafe, VBOXSERVICETOOLBOXDIRENTRY, Node)
|
---|
981 | {
|
---|
982 | RTListNodeRemove(&pNode->Node);
|
---|
983 | RTMemFree(pNode);
|
---|
984 | }
|
---|
985 | return rc;
|
---|
986 | }
|
---|
987 |
|
---|
988 |
|
---|
989 | /** @todo Document options! */
|
---|
990 | static char g_paszLsHelp[] =
|
---|
991 | " VBoxService [--use-toolbox] vbox_ls [<general options>] [option]...\n"
|
---|
992 | " [<file>...]\n\n"
|
---|
993 | "List information about files (the current directory by default).\n\n"
|
---|
994 | "Options:\n\n"
|
---|
995 | " [--dereference|-L]\n"
|
---|
996 | " [-l][-R]\n"
|
---|
997 | " [--verbose|-v]\n"
|
---|
998 | " [<file>...]\n"
|
---|
999 | "\n";
|
---|
1000 |
|
---|
1001 |
|
---|
1002 | /**
|
---|
1003 | * Main function for tool "vbox_ls".
|
---|
1004 | *
|
---|
1005 | * @return RTEXITCODE.
|
---|
1006 | * @param argc Number of arguments.
|
---|
1007 | * @param argv Pointer to argument array.
|
---|
1008 | */
|
---|
1009 | static RTEXITCODE vgsvcToolboxLs(int argc, char **argv)
|
---|
1010 | {
|
---|
1011 | static const RTGETOPTDEF s_aOptions[] =
|
---|
1012 | {
|
---|
1013 | { "--machinereadable", VBOXSERVICETOOLBOXOPT_MACHINE_READABLE, RTGETOPT_REQ_NOTHING },
|
---|
1014 | { "--dereference", 'L', RTGETOPT_REQ_NOTHING },
|
---|
1015 | { NULL, 'l', RTGETOPT_REQ_NOTHING },
|
---|
1016 | { NULL, 'R', RTGETOPT_REQ_NOTHING },
|
---|
1017 | { "--verbose", VBOXSERVICETOOLBOXOPT_VERBOSE, RTGETOPT_REQ_NOTHING}
|
---|
1018 | };
|
---|
1019 |
|
---|
1020 | int ch;
|
---|
1021 | RTGETOPTUNION ValueUnion;
|
---|
1022 | RTGETOPTSTATE GetState;
|
---|
1023 | int rc = RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions),
|
---|
1024 | 1 /*iFirst*/, RTGETOPTINIT_FLAGS_OPTS_FIRST);
|
---|
1025 | AssertRCReturn(rc, RTEXITCODE_INIT);
|
---|
1026 |
|
---|
1027 | bool fVerbose = false;
|
---|
1028 | uint32_t fFlags = VBOXSERVICETOOLBOXLSFLAG_NONE;
|
---|
1029 | uint32_t fOutputFlags = VBOXSERVICETOOLBOXOUTPUTFLAG_NONE;
|
---|
1030 |
|
---|
1031 | while ( (ch = RTGetOpt(&GetState, &ValueUnion))
|
---|
1032 | && RT_SUCCESS(rc) /** @todo r=bird: WTF is this doing here? rc isn't set in the loop!! And there is an AssertRCReturn after the previous place it was set. */)
|
---|
1033 | {
|
---|
1034 | /* For options that require an argument, ValueUnion has received the value. */
|
---|
1035 | switch (ch)
|
---|
1036 | {
|
---|
1037 | case 'h':
|
---|
1038 | vgsvcToolboxShowUsageHeader();
|
---|
1039 | RTPrintf("%s", g_paszLsHelp);
|
---|
1040 | return RTEXITCODE_SUCCESS;
|
---|
1041 |
|
---|
1042 | case 'L': /* Dereference symlinks. */
|
---|
1043 | fFlags |= VBOXSERVICETOOLBOXLSFLAG_SYMLINKS;
|
---|
1044 | break;
|
---|
1045 |
|
---|
1046 | case 'l': /* Print long format. */
|
---|
1047 | fOutputFlags |= VBOXSERVICETOOLBOXOUTPUTFLAG_LONG;
|
---|
1048 | break;
|
---|
1049 |
|
---|
1050 | case VBOXSERVICETOOLBOXOPT_MACHINE_READABLE:
|
---|
1051 | fOutputFlags |= VBOXSERVICETOOLBOXOUTPUTFLAG_PARSEABLE;
|
---|
1052 | break;
|
---|
1053 |
|
---|
1054 | case 'R': /* Recursive processing. */
|
---|
1055 | fFlags |= VBOXSERVICETOOLBOXLSFLAG_RECURSIVE;
|
---|
1056 | break;
|
---|
1057 |
|
---|
1058 | case VBOXSERVICETOOLBOXOPT_VERBOSE:
|
---|
1059 | fVerbose = true;
|
---|
1060 | break;
|
---|
1061 |
|
---|
1062 | case 'V':
|
---|
1063 | vgsvcToolboxShowVersion();
|
---|
1064 | return RTEXITCODE_SUCCESS;
|
---|
1065 |
|
---|
1066 | case VINF_GETOPT_NOT_OPTION:
|
---|
1067 | Assert(GetState.iNext);
|
---|
1068 | GetState.iNext--;
|
---|
1069 | break;
|
---|
1070 |
|
---|
1071 | default:
|
---|
1072 | return RTGetOptPrintError(ch, &ValueUnion);
|
---|
1073 | }
|
---|
1074 |
|
---|
1075 | /* All flags / options processed? Bail out here.
|
---|
1076 | * Processing the file / directory list comes down below. */
|
---|
1077 | if (ch == VINF_GETOPT_NOT_OPTION)
|
---|
1078 | break;
|
---|
1079 | }
|
---|
1080 |
|
---|
1081 | if (RT_SUCCESS(rc)) /** @todo r=bird: WTF?!? The state handling here is certifiably insane. Crap like this drives me CRAZY!! */
|
---|
1082 | {
|
---|
1083 | /* Print magic/version. */
|
---|
1084 | if (fOutputFlags & VBOXSERVICETOOLBOXOUTPUTFLAG_PARSEABLE)
|
---|
1085 | {
|
---|
1086 | rc = vgsvcToolboxStrmInit();
|
---|
1087 | if (RT_FAILURE(rc))
|
---|
1088 | RTMsgError("Error while initializing parseable streams, rc=%Rrc\n", rc);
|
---|
1089 | vgsvcToolboxPrintStrmHeader("vbt_ls", 1 /* Stream version */);
|
---|
1090 | }
|
---|
1091 |
|
---|
1092 | VGSVCTOOLBOXIDCACHE IdCache;
|
---|
1093 | RT_ZERO(IdCache);
|
---|
1094 |
|
---|
1095 | ch = RTGetOpt(&GetState, &ValueUnion);
|
---|
1096 | do
|
---|
1097 | {
|
---|
1098 | char *pszEntry = NULL; /** @todo r=bird: Bad name choice. pszEntry sounds like RTDIRENTRY::szName, i.e. no path. */
|
---|
1099 |
|
---|
1100 | if (ch == 0) /* Use current directory if no element specified. */
|
---|
1101 | {
|
---|
1102 | char szDirCur[RTPATH_MAX + 1]; /** @todo r=bird: Just put this outside the if(ch==0) and make pszEntry point to it. There is no need to duplicate any strings here! */
|
---|
1103 | rc = RTPathGetCurrent(szDirCur, sizeof(szDirCur));
|
---|
1104 | if (RT_FAILURE(rc))
|
---|
1105 | RTMsgError("Getting current directory failed, rc=%Rrc\n", rc);
|
---|
1106 |
|
---|
1107 | pszEntry = RTStrDup(szDirCur);
|
---|
1108 | if (!pszEntry)
|
---|
1109 | RTMsgError("Allocating current directory failed\n");
|
---|
1110 | }
|
---|
1111 | else
|
---|
1112 | {
|
---|
1113 | pszEntry = RTStrDup(ValueUnion.psz);
|
---|
1114 | if (!pszEntry)
|
---|
1115 | RTMsgError("Allocating directory '%s' failed\n", ValueUnion.psz);
|
---|
1116 | }
|
---|
1117 |
|
---|
1118 | /** @todo r=bird: RTFileExists == RTPathQueryInfo, so just do
|
---|
1119 | * RTPathQueryInfoEx here! Also, you _need_ to figure out whether or
|
---|
1120 | * not to follow "commandline" links! */
|
---|
1121 | if (RTFileExists(pszEntry))
|
---|
1122 | {
|
---|
1123 | RTFSOBJINFO objInfo;
|
---|
1124 | int rc2 = RTPathQueryInfoEx(pszEntry, &objInfo,
|
---|
1125 | RTFSOBJATTRADD_UNIX, RTPATH_F_ON_LINK /** @todo Follow link? */);
|
---|
1126 | if (RT_FAILURE(rc2))
|
---|
1127 | {
|
---|
1128 | if (!(fOutputFlags & VBOXSERVICETOOLBOXOUTPUTFLAG_PARSEABLE))
|
---|
1129 | RTMsgError("Cannot access '%s': No such file or directory\n", pszEntry);
|
---|
1130 | rc = VERR_FILE_NOT_FOUND;
|
---|
1131 | /* Do not break here -- process every element in the list
|
---|
1132 | * and keep failing rc. */
|
---|
1133 | }
|
---|
1134 | else
|
---|
1135 | {
|
---|
1136 | rc2 = vgsvcToolboxPrintFsInfo(pszEntry, strlen(pszEntry), fOutputFlags, NULL, &IdCache, &objInfo);
|
---|
1137 | if (RT_FAILURE(rc2))
|
---|
1138 | rc = rc2;
|
---|
1139 | }
|
---|
1140 | }
|
---|
1141 | else
|
---|
1142 | {
|
---|
1143 | int rc2 = vgsvcToolboxLsHandleDir(pszEntry, fFlags, fOutputFlags, &IdCache);
|
---|
1144 | if (RT_FAILURE(rc2))
|
---|
1145 | rc = rc2;
|
---|
1146 | }
|
---|
1147 |
|
---|
1148 | RTStrFree(pszEntry);
|
---|
1149 | } while ((ch = RTGetOpt(&GetState, &ValueUnion)) != 0);
|
---|
1150 |
|
---|
1151 | if (fOutputFlags & VBOXSERVICETOOLBOXOUTPUTFLAG_PARSEABLE) /* Output termination. */
|
---|
1152 | vgsvcToolboxPrintStrmTermination();
|
---|
1153 | }
|
---|
1154 | else if (fVerbose)
|
---|
1155 | RTMsgError("Failed with rc=%Rrc\n", rc);
|
---|
1156 |
|
---|
1157 | return RT_SUCCESS(rc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
|
---|
1158 | }
|
---|
1159 |
|
---|
1160 |
|
---|
1161 | /* Try using RTPathRmCmd. */
|
---|
1162 | static RTEXITCODE vgsvcToolboxRm(int argc, char **argv)
|
---|
1163 | {
|
---|
1164 | return RTPathRmCmd(argc, argv);
|
---|
1165 | }
|
---|
1166 |
|
---|
1167 |
|
---|
1168 | static char g_paszMkTempHelp[] =
|
---|
1169 | " VBoxService [--use-toolbox] vbox_mktemp [<general options>] [<options>]\n"
|
---|
1170 | " <template>\n\n"
|
---|
1171 | "Create a temporary directory based on the template supplied. The first string\n"
|
---|
1172 | "of consecutive 'X' characters in the template will be replaced to form a unique\n"
|
---|
1173 | "name for the directory. The template may not contain a path. The default\n"
|
---|
1174 | "creation mode is 0600 for files and 0700 for directories. If no path is\n"
|
---|
1175 | "specified the default temporary directory will be used.\n"
|
---|
1176 | "Options:\n\n"
|
---|
1177 | " [--directory|-d] Create a directory instead of a file.\n"
|
---|
1178 | " [--mode|-m <mode>] Create the object with mode <mode>.\n"
|
---|
1179 | " [--secure|-s] Fail if the object cannot be created securely.\n"
|
---|
1180 | " [--tmpdir|-t <path>] Create the object with the absolute path <path>.\n"
|
---|
1181 | "\n";
|
---|
1182 |
|
---|
1183 |
|
---|
1184 | /**
|
---|
1185 | * Report the result of a vbox_mktemp operation.
|
---|
1186 | *
|
---|
1187 | * Either errors to stderr (not machine-readable) or everything to stdout as
|
---|
1188 | * {name}\0{rc}\0 (machine- readable format). The message may optionally
|
---|
1189 | * contain a '%s' for the file name and an %Rrc for the result code in that
|
---|
1190 | * order. In future a "verbose" flag may be added, without which nothing will
|
---|
1191 | * be output in non-machine- readable mode. Sets prc if rc is a non-success
|
---|
1192 | * code.
|
---|
1193 | */
|
---|
1194 | static void toolboxMkTempReport(const char *pcszMessage, const char *pcszFile,
|
---|
1195 | bool fActive, int rc, uint32_t fOutputFlags, int *prc)
|
---|
1196 | {
|
---|
1197 | if (!fActive)
|
---|
1198 | return;
|
---|
1199 | if (!(fOutputFlags & VBOXSERVICETOOLBOXOUTPUTFLAG_PARSEABLE))
|
---|
1200 | if (RT_SUCCESS(rc))
|
---|
1201 | RTPrintf(pcszMessage, pcszFile, rc);
|
---|
1202 | else
|
---|
1203 | RTMsgError(pcszMessage, pcszFile, rc);
|
---|
1204 | else
|
---|
1205 | RTPrintf("name=%s%crc=%d%c", pcszFile, 0, rc, 0);
|
---|
1206 | if (prc && RT_FAILURE(rc))
|
---|
1207 | *prc = rc;
|
---|
1208 | }
|
---|
1209 |
|
---|
1210 |
|
---|
1211 | /**
|
---|
1212 | * Main function for tool "vbox_mktemp".
|
---|
1213 | *
|
---|
1214 | * @return RTEXITCODE.
|
---|
1215 | * @param argc Number of arguments.
|
---|
1216 | * @param argv Pointer to argument array.
|
---|
1217 | */
|
---|
1218 | static RTEXITCODE vgsvcToolboxMkTemp(int argc, char **argv)
|
---|
1219 | {
|
---|
1220 | static const RTGETOPTDEF s_aOptions[] =
|
---|
1221 | {
|
---|
1222 | { "--machinereadable", VBOXSERVICETOOLBOXOPT_MACHINE_READABLE,
|
---|
1223 | RTGETOPT_REQ_NOTHING },
|
---|
1224 | { "--directory", 'd', RTGETOPT_REQ_NOTHING },
|
---|
1225 | { "--mode", 'm', RTGETOPT_REQ_STRING },
|
---|
1226 | { "--secure", 's', RTGETOPT_REQ_NOTHING },
|
---|
1227 | { "--tmpdir", 't', RTGETOPT_REQ_STRING },
|
---|
1228 | };
|
---|
1229 |
|
---|
1230 | enum
|
---|
1231 | {
|
---|
1232 | /* Isn't that a bit long? s/VBOXSERVICETOOLBOX/VSTB/ ? */
|
---|
1233 | /** Create a temporary directory instead of a temporary file. */
|
---|
1234 | VBOXSERVICETOOLBOXMKTEMPFLAG_DIRECTORY = RT_BIT_32(0),
|
---|
1235 | /** Only create the temporary object if the operation is expected
|
---|
1236 | * to be secure. Not guaranteed to be supported on a particular
|
---|
1237 | * set-up. */
|
---|
1238 | VBOXSERVICETOOLBOXMKTEMPFLAG_SECURE = RT_BIT_32(1)
|
---|
1239 | };
|
---|
1240 |
|
---|
1241 | int ch, rc;
|
---|
1242 | RTGETOPTUNION ValueUnion;
|
---|
1243 | RTGETOPTSTATE GetState;
|
---|
1244 | rc = RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 1 /*iFirst*/, RTGETOPTINIT_FLAGS_OPTS_FIRST);
|
---|
1245 | AssertRCReturn(rc, RTEXITCODE_INIT);
|
---|
1246 |
|
---|
1247 | uint32_t fFlags = 0;
|
---|
1248 | uint32_t fOutputFlags = 0;
|
---|
1249 | int cNonOptions = 0;
|
---|
1250 | RTFMODE fMode = 0700;
|
---|
1251 | bool fModeSet = false;
|
---|
1252 | const char *pcszPath = NULL;
|
---|
1253 | const char *pcszTemplate;
|
---|
1254 | char szTemplateWithPath[RTPATH_MAX] = "";
|
---|
1255 |
|
---|
1256 | while ( (ch = RTGetOpt(&GetState, &ValueUnion))
|
---|
1257 | && RT_SUCCESS(rc))
|
---|
1258 | {
|
---|
1259 | /* For options that require an argument, ValueUnion has received the value. */
|
---|
1260 | switch (ch)
|
---|
1261 | {
|
---|
1262 | case 'h':
|
---|
1263 | vgsvcToolboxShowUsageHeader();
|
---|
1264 | RTPrintf("%s", g_paszMkTempHelp);
|
---|
1265 | return RTEXITCODE_SUCCESS;
|
---|
1266 |
|
---|
1267 | case 'V':
|
---|
1268 | vgsvcToolboxShowVersion();
|
---|
1269 | return RTEXITCODE_SUCCESS;
|
---|
1270 |
|
---|
1271 | case VBOXSERVICETOOLBOXOPT_MACHINE_READABLE:
|
---|
1272 | fOutputFlags |= VBOXSERVICETOOLBOXOUTPUTFLAG_PARSEABLE;
|
---|
1273 | break;
|
---|
1274 |
|
---|
1275 | case 'd':
|
---|
1276 | fFlags |= VBOXSERVICETOOLBOXMKTEMPFLAG_DIRECTORY;
|
---|
1277 | break;
|
---|
1278 |
|
---|
1279 | case 'm':
|
---|
1280 | rc = vgsvcToolboxParseMode(ValueUnion.psz, &fMode);
|
---|
1281 | if (RT_FAILURE(rc))
|
---|
1282 | return RTEXITCODE_SYNTAX;
|
---|
1283 | fModeSet = true;
|
---|
1284 | #ifndef RT_OS_WINDOWS
|
---|
1285 | umask(0); /* RTDirCreate workaround */
|
---|
1286 | #endif
|
---|
1287 | break;
|
---|
1288 | case 's':
|
---|
1289 | fFlags |= VBOXSERVICETOOLBOXMKTEMPFLAG_SECURE;
|
---|
1290 | break;
|
---|
1291 |
|
---|
1292 | case 't':
|
---|
1293 | pcszPath = ValueUnion.psz;
|
---|
1294 | break;
|
---|
1295 |
|
---|
1296 | case VINF_GETOPT_NOT_OPTION:
|
---|
1297 | /* RTGetOpt will sort these to the end of the argv vector so
|
---|
1298 | * that we will deal with them afterwards. */
|
---|
1299 | ++cNonOptions;
|
---|
1300 | break;
|
---|
1301 |
|
---|
1302 | default:
|
---|
1303 | return RTGetOptPrintError(ch, &ValueUnion);
|
---|
1304 | }
|
---|
1305 | }
|
---|
1306 |
|
---|
1307 | /* Print magic/version. */
|
---|
1308 | if (fOutputFlags & VBOXSERVICETOOLBOXOUTPUTFLAG_PARSEABLE)
|
---|
1309 | {
|
---|
1310 | rc = vgsvcToolboxStrmInit();
|
---|
1311 | if (RT_FAILURE(rc))
|
---|
1312 | RTMsgError("Error while initializing parseable streams, rc=%Rrc\n", rc);
|
---|
1313 | vgsvcToolboxPrintStrmHeader("vbt_mktemp", 1 /* Stream version */);
|
---|
1314 | }
|
---|
1315 |
|
---|
1316 | if (fFlags & VBOXSERVICETOOLBOXMKTEMPFLAG_SECURE && fModeSet)
|
---|
1317 | {
|
---|
1318 | toolboxMkTempReport("'-s' and '-m' parameters cannot be used together.\n", "",
|
---|
1319 | true, VERR_INVALID_PARAMETER, fOutputFlags, &rc);
|
---|
1320 | return RTEXITCODE_SYNTAX;
|
---|
1321 | }
|
---|
1322 |
|
---|
1323 | /* We need exactly one template, containing at least one 'X'. */
|
---|
1324 | if (cNonOptions != 1)
|
---|
1325 | {
|
---|
1326 | toolboxMkTempReport("Please specify exactly one template.\n", "", true, VERR_INVALID_PARAMETER, fOutputFlags, &rc);
|
---|
1327 | return RTEXITCODE_SYNTAX;
|
---|
1328 | }
|
---|
1329 | pcszTemplate = argv[argc - 1];
|
---|
1330 |
|
---|
1331 | /* Validate that the template is as IPRT requires (asserted by IPRT). */
|
---|
1332 | if ( RTPathHasPath(pcszTemplate)
|
---|
1333 | || ( !strstr(pcszTemplate, "XXX")
|
---|
1334 | && pcszTemplate[strlen(pcszTemplate) - 1] != 'X'))
|
---|
1335 | {
|
---|
1336 | toolboxMkTempReport("Template '%s' should contain a file name with no path and at least three consecutive 'X' characters or ending in 'X'.\n",
|
---|
1337 | pcszTemplate, true, VERR_INVALID_PARAMETER, fOutputFlags, &rc);
|
---|
1338 | return RTEXITCODE_FAILURE;
|
---|
1339 | }
|
---|
1340 | if (pcszPath && !RTPathStartsWithRoot(pcszPath))
|
---|
1341 | {
|
---|
1342 | toolboxMkTempReport("Path '%s' should be absolute.\n", pcszPath, true, VERR_INVALID_PARAMETER, fOutputFlags, &rc);
|
---|
1343 | return RTEXITCODE_FAILURE;
|
---|
1344 | }
|
---|
1345 | if (pcszPath)
|
---|
1346 | {
|
---|
1347 | rc = RTStrCopy(szTemplateWithPath, sizeof(szTemplateWithPath), pcszPath);
|
---|
1348 | if (RT_FAILURE(rc))
|
---|
1349 | {
|
---|
1350 | toolboxMkTempReport("Path '%s' too long.\n", pcszPath, true, VERR_INVALID_PARAMETER, fOutputFlags, &rc);
|
---|
1351 | return RTEXITCODE_FAILURE;
|
---|
1352 | }
|
---|
1353 | }
|
---|
1354 | else
|
---|
1355 | {
|
---|
1356 | rc = RTPathTemp(szTemplateWithPath, sizeof(szTemplateWithPath));
|
---|
1357 | if (RT_FAILURE(rc))
|
---|
1358 | {
|
---|
1359 | toolboxMkTempReport("Failed to get the temporary directory.\n", "", true, VERR_INVALID_PARAMETER, fOutputFlags, &rc);
|
---|
1360 | return RTEXITCODE_FAILURE;
|
---|
1361 | }
|
---|
1362 | }
|
---|
1363 | rc = RTPathAppend(szTemplateWithPath, sizeof(szTemplateWithPath), pcszTemplate);
|
---|
1364 | if (RT_FAILURE(rc))
|
---|
1365 | {
|
---|
1366 | toolboxMkTempReport("Template '%s' too long for path.\n", pcszTemplate, true, VERR_INVALID_PARAMETER, fOutputFlags, &rc);
|
---|
1367 | return RTEXITCODE_FAILURE;
|
---|
1368 | }
|
---|
1369 |
|
---|
1370 | if (fFlags & VBOXSERVICETOOLBOXMKTEMPFLAG_DIRECTORY)
|
---|
1371 | {
|
---|
1372 | rc = fFlags & VBOXSERVICETOOLBOXMKTEMPFLAG_SECURE
|
---|
1373 | ? RTDirCreateTempSecure(szTemplateWithPath)
|
---|
1374 | : RTDirCreateTemp(szTemplateWithPath, fMode);
|
---|
1375 | toolboxMkTempReport("Created temporary directory '%s'.\n",
|
---|
1376 | szTemplateWithPath, RT_SUCCESS(rc), rc,
|
---|
1377 | fOutputFlags, NULL);
|
---|
1378 | /* RTDirCreateTemp[Secure] sets the template to "" on failure. */
|
---|
1379 | toolboxMkTempReport("The following error occurred while creating a temporary directory from template '%s': %Rrc.\n",
|
---|
1380 | pcszTemplate, RT_FAILURE(rc), rc, fOutputFlags, NULL /*prc*/);
|
---|
1381 | }
|
---|
1382 | else
|
---|
1383 | {
|
---|
1384 | rc = fFlags & VBOXSERVICETOOLBOXMKTEMPFLAG_SECURE
|
---|
1385 | ? RTFileCreateTempSecure(szTemplateWithPath)
|
---|
1386 | : RTFileCreateTemp(szTemplateWithPath, fMode);
|
---|
1387 | toolboxMkTempReport("Created temporary file '%s'.\n",
|
---|
1388 | szTemplateWithPath, RT_SUCCESS(rc), rc,
|
---|
1389 | fOutputFlags, NULL);
|
---|
1390 | /* RTFileCreateTemp[Secure] sets the template to "" on failure. */
|
---|
1391 | toolboxMkTempReport("The following error occurred while creating a temporary file from template '%s': %Rrc.\n",
|
---|
1392 | pcszTemplate, RT_FAILURE(rc), rc, fOutputFlags, NULL /*prc*/);
|
---|
1393 | }
|
---|
1394 | if (fOutputFlags & VBOXSERVICETOOLBOXOUTPUTFLAG_PARSEABLE) /* Output termination. */
|
---|
1395 | vgsvcToolboxPrintStrmTermination();
|
---|
1396 | return RT_SUCCESS(rc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
|
---|
1397 | }
|
---|
1398 |
|
---|
1399 |
|
---|
1400 | /** @todo Document options! */
|
---|
1401 | static char g_paszMkDirHelp[] =
|
---|
1402 | " VBoxService [--use-toolbox] vbox_mkdir [<general options>] [<options>]\n"
|
---|
1403 | " <directory>...\n\n"
|
---|
1404 | "Options:\n\n"
|
---|
1405 | " [--mode|-m <mode>] The file mode to set (chmod) on the created\n"
|
---|
1406 | " directories. Default: a=rwx & umask.\n"
|
---|
1407 | " [--parents|-p] Create parent directories as needed, no\n"
|
---|
1408 | " error if the directory already exists.\n"
|
---|
1409 | " [--verbose|-v] Display a message for each created directory.\n"
|
---|
1410 | "\n";
|
---|
1411 |
|
---|
1412 |
|
---|
1413 | /**
|
---|
1414 | * Main function for tool "vbox_mkdir".
|
---|
1415 | *
|
---|
1416 | * @return RTEXITCODE.
|
---|
1417 | * @param argc Number of arguments.
|
---|
1418 | * @param argv Pointer to argument array.
|
---|
1419 | */
|
---|
1420 | static RTEXITCODE vgsvcToolboxMkDir(int argc, char **argv)
|
---|
1421 | {
|
---|
1422 | static const RTGETOPTDEF s_aOptions[] =
|
---|
1423 | {
|
---|
1424 | { "--mode", 'm', RTGETOPT_REQ_STRING },
|
---|
1425 | { "--parents", 'p', RTGETOPT_REQ_NOTHING},
|
---|
1426 | { "--verbose", 'v', RTGETOPT_REQ_NOTHING}
|
---|
1427 | };
|
---|
1428 |
|
---|
1429 | int ch;
|
---|
1430 | RTGETOPTUNION ValueUnion;
|
---|
1431 | RTGETOPTSTATE GetState;
|
---|
1432 | int rc = RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions),
|
---|
1433 | 1 /*iFirst*/, RTGETOPTINIT_FLAGS_OPTS_FIRST);
|
---|
1434 | AssertRCReturn(rc, RTEXITCODE_INIT);
|
---|
1435 |
|
---|
1436 | bool fMakeParentDirs = false;
|
---|
1437 | bool fVerbose = false;
|
---|
1438 | RTFMODE fDirMode = RTFS_UNIX_IRWXU | RTFS_UNIX_IRWXG | RTFS_UNIX_IRWXO;
|
---|
1439 | int cDirsCreated = 0;
|
---|
1440 |
|
---|
1441 | while ((ch = RTGetOpt(&GetState, &ValueUnion)))
|
---|
1442 | {
|
---|
1443 | /* For options that require an argument, ValueUnion has received the value. */
|
---|
1444 | switch (ch)
|
---|
1445 | {
|
---|
1446 | case 'p':
|
---|
1447 | fMakeParentDirs = true;
|
---|
1448 | break;
|
---|
1449 |
|
---|
1450 | case 'm':
|
---|
1451 | rc = vgsvcToolboxParseMode(ValueUnion.psz, &fDirMode);
|
---|
1452 | if (RT_FAILURE(rc))
|
---|
1453 | return RTEXITCODE_SYNTAX;
|
---|
1454 | #ifndef RT_OS_WINDOWS
|
---|
1455 | umask(0); /* RTDirCreate workaround */
|
---|
1456 | #endif
|
---|
1457 | break;
|
---|
1458 |
|
---|
1459 | case 'v':
|
---|
1460 | fVerbose = true;
|
---|
1461 | break;
|
---|
1462 |
|
---|
1463 | case 'h':
|
---|
1464 | vgsvcToolboxShowUsageHeader();
|
---|
1465 | RTPrintf("%s", g_paszMkDirHelp);
|
---|
1466 | return RTEXITCODE_SUCCESS;
|
---|
1467 |
|
---|
1468 | case 'V':
|
---|
1469 | vgsvcToolboxShowVersion();
|
---|
1470 | return RTEXITCODE_SUCCESS;
|
---|
1471 |
|
---|
1472 | case VINF_GETOPT_NOT_OPTION:
|
---|
1473 | if (fMakeParentDirs)
|
---|
1474 | /** @todo r=bird: If fVerbose is set, we should also show
|
---|
1475 | * which directories that get created, parents as well as
|
---|
1476 | * omitting existing final dirs. Annoying, but check any
|
---|
1477 | * mkdir implementation (try "mkdir -pv asdf/1/2/3/4"
|
---|
1478 | * twice). */
|
---|
1479 | rc = RTDirCreateFullPath(ValueUnion.psz, fDirMode);
|
---|
1480 | else
|
---|
1481 | rc = RTDirCreate(ValueUnion.psz, fDirMode, 0);
|
---|
1482 | if (RT_FAILURE(rc))
|
---|
1483 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "Could not create directory '%s': %Rra\n",
|
---|
1484 | ValueUnion.psz, rc);
|
---|
1485 | if (fVerbose)
|
---|
1486 | RTMsgInfo("Created directory '%s', mode %#RTfmode\n", ValueUnion.psz, fDirMode);
|
---|
1487 | cDirsCreated++;
|
---|
1488 | break;
|
---|
1489 |
|
---|
1490 | default:
|
---|
1491 | return RTGetOptPrintError(ch, &ValueUnion);
|
---|
1492 | }
|
---|
1493 | }
|
---|
1494 | AssertRC(rc);
|
---|
1495 |
|
---|
1496 | if (cDirsCreated == 0)
|
---|
1497 | return RTMsgErrorExit(RTEXITCODE_SYNTAX, "No directory argument.");
|
---|
1498 |
|
---|
1499 | return RTEXITCODE_SUCCESS;
|
---|
1500 | }
|
---|
1501 |
|
---|
1502 |
|
---|
1503 | /** @todo Document options! */
|
---|
1504 | static char g_paszStatHelp[] =
|
---|
1505 | " VBoxService [--use-toolbox] vbox_stat [<general options>] [<options>]\n"
|
---|
1506 | " <file>...\n\n"
|
---|
1507 | "Display file or file system status.\n\n"
|
---|
1508 | "Options:\n\n"
|
---|
1509 | " [--file-system|-f]\n"
|
---|
1510 | " [--dereference|-L]\n"
|
---|
1511 | " [--terse|-t]\n"
|
---|
1512 | " [--verbose|-v]\n"
|
---|
1513 | "\n";
|
---|
1514 |
|
---|
1515 |
|
---|
1516 | /**
|
---|
1517 | * Main function for tool "vbox_stat".
|
---|
1518 | *
|
---|
1519 | * @return RTEXITCODE.
|
---|
1520 | * @param argc Number of arguments.
|
---|
1521 | * @param argv Pointer to argument array.
|
---|
1522 | */
|
---|
1523 | static RTEXITCODE vgsvcToolboxStat(int argc, char **argv)
|
---|
1524 | {
|
---|
1525 | static const RTGETOPTDEF s_aOptions[] =
|
---|
1526 | {
|
---|
1527 | { "--file-system", 'f', RTGETOPT_REQ_NOTHING },
|
---|
1528 | { "--dereference", 'L', RTGETOPT_REQ_NOTHING },
|
---|
1529 | { "--machinereadable", VBOXSERVICETOOLBOXOPT_MACHINE_READABLE, RTGETOPT_REQ_NOTHING },
|
---|
1530 | { "--terse", 't', RTGETOPT_REQ_NOTHING },
|
---|
1531 | { "--verbose", 'v', RTGETOPT_REQ_NOTHING }
|
---|
1532 | };
|
---|
1533 |
|
---|
1534 | int ch;
|
---|
1535 | RTGETOPTUNION ValueUnion;
|
---|
1536 | RTGETOPTSTATE GetState;
|
---|
1537 | RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 1 /*iFirst*/, RTGETOPTINIT_FLAGS_OPTS_FIRST);
|
---|
1538 |
|
---|
1539 | int rc = VINF_SUCCESS;
|
---|
1540 | uint32_t fOutputFlags = VBOXSERVICETOOLBOXOUTPUTFLAG_LONG; /* Use long mode by default. */
|
---|
1541 | uint32_t fQueryInfoFlags = RTPATH_F_ON_LINK;
|
---|
1542 |
|
---|
1543 | while ( (ch = RTGetOpt(&GetState, &ValueUnion))
|
---|
1544 | && RT_SUCCESS(rc))
|
---|
1545 | {
|
---|
1546 | /* For options that require an argument, ValueUnion has received the value. */
|
---|
1547 | switch (ch)
|
---|
1548 | {
|
---|
1549 | case 'f':
|
---|
1550 | RTMsgError("Sorry, option '%s' is not implemented yet!\n", ValueUnion.pDef->pszLong);
|
---|
1551 | rc = VERR_INVALID_PARAMETER;
|
---|
1552 | break;
|
---|
1553 |
|
---|
1554 | case 'L':
|
---|
1555 | fQueryInfoFlags &= ~RTPATH_F_ON_LINK;
|
---|
1556 | fQueryInfoFlags |= RTPATH_F_FOLLOW_LINK;
|
---|
1557 | break;
|
---|
1558 |
|
---|
1559 | case VBOXSERVICETOOLBOXOPT_MACHINE_READABLE:
|
---|
1560 | fOutputFlags |= VBOXSERVICETOOLBOXOUTPUTFLAG_PARSEABLE;
|
---|
1561 | break;
|
---|
1562 |
|
---|
1563 | case 'h':
|
---|
1564 | vgsvcToolboxShowUsageHeader();
|
---|
1565 | RTPrintf("%s", g_paszStatHelp);
|
---|
1566 | return RTEXITCODE_SUCCESS;
|
---|
1567 |
|
---|
1568 | case 'V':
|
---|
1569 | vgsvcToolboxShowVersion();
|
---|
1570 | return RTEXITCODE_SUCCESS;
|
---|
1571 |
|
---|
1572 | case VINF_GETOPT_NOT_OPTION:
|
---|
1573 | {
|
---|
1574 | Assert(GetState.iNext);
|
---|
1575 | GetState.iNext--;
|
---|
1576 | break;
|
---|
1577 | }
|
---|
1578 |
|
---|
1579 | default:
|
---|
1580 | return RTGetOptPrintError(ch, &ValueUnion);
|
---|
1581 | }
|
---|
1582 |
|
---|
1583 | /* All flags / options processed? Bail out here.
|
---|
1584 | * Processing the file / directory list comes down below. */
|
---|
1585 | if (ch == VINF_GETOPT_NOT_OPTION)
|
---|
1586 | break;
|
---|
1587 | }
|
---|
1588 |
|
---|
1589 | if (RT_SUCCESS(rc))
|
---|
1590 | {
|
---|
1591 | if (fOutputFlags & VBOXSERVICETOOLBOXOUTPUTFLAG_PARSEABLE) /* Output termination. */
|
---|
1592 | {
|
---|
1593 | rc = vgsvcToolboxStrmInit();
|
---|
1594 | if (RT_FAILURE(rc))
|
---|
1595 | RTMsgError("Error while initializing parseable streams, rc=%Rrc\n", rc);
|
---|
1596 | vgsvcToolboxPrintStrmHeader("vbt_stat", 1 /* Stream version */);
|
---|
1597 | }
|
---|
1598 |
|
---|
1599 | VGSVCTOOLBOXIDCACHE IdCache;
|
---|
1600 | RT_ZERO(IdCache);
|
---|
1601 |
|
---|
1602 | while ((ch = RTGetOpt(&GetState, &ValueUnion)))
|
---|
1603 | {
|
---|
1604 | RTFSOBJINFO objInfo;
|
---|
1605 | int rc2 = RTPathQueryInfoEx(ValueUnion.psz, &objInfo, RTFSOBJATTRADD_UNIX, fQueryInfoFlags);
|
---|
1606 | if (RT_FAILURE(rc2))
|
---|
1607 | {
|
---|
1608 | if (!(fOutputFlags & VBOXSERVICETOOLBOXOUTPUTFLAG_PARSEABLE))
|
---|
1609 | RTMsgError("Cannot stat for '%s': %Rrc\n", ValueUnion.psz, rc2);
|
---|
1610 | }
|
---|
1611 | else
|
---|
1612 | rc2 = vgsvcToolboxPrintFsInfo(ValueUnion.psz, strlen(ValueUnion.psz), fOutputFlags, NULL, &IdCache, &objInfo);
|
---|
1613 | /** @todo r=bird: You're checking rc not rc2 here... */
|
---|
1614 | if (RT_SUCCESS(rc))
|
---|
1615 | rc = rc2;
|
---|
1616 | /* Do not break here -- process every element in the list
|
---|
1617 | * and keep (initial) failing rc. */
|
---|
1618 | }
|
---|
1619 |
|
---|
1620 | if (fOutputFlags & VBOXSERVICETOOLBOXOUTPUTFLAG_PARSEABLE) /* Output termination. */
|
---|
1621 | vgsvcToolboxPrintStrmTermination();
|
---|
1622 |
|
---|
1623 | /* At this point the overall result (success/failure) should be in rc. */
|
---|
1624 | }
|
---|
1625 | else
|
---|
1626 | RTMsgError("Failed with rc=%Rrc\n", rc);
|
---|
1627 |
|
---|
1628 | if (RT_FAILURE(rc))
|
---|
1629 | {
|
---|
1630 | switch (rc)
|
---|
1631 | {
|
---|
1632 | case VERR_ACCESS_DENIED:
|
---|
1633 | return (RTEXITCODE)VBOXSERVICETOOLBOX_STAT_EXITCODE_ACCESS_DENIED;
|
---|
1634 |
|
---|
1635 | case VERR_FILE_NOT_FOUND:
|
---|
1636 | return (RTEXITCODE)VBOXSERVICETOOLBOX_STAT_EXITCODE_FILE_NOT_FOUND;
|
---|
1637 |
|
---|
1638 | case VERR_PATH_NOT_FOUND:
|
---|
1639 | return (RTEXITCODE)VBOXSERVICETOOLBOX_STAT_EXITCODE_PATH_NOT_FOUND;
|
---|
1640 |
|
---|
1641 | case VERR_NET_PATH_NOT_FOUND:
|
---|
1642 | return (RTEXITCODE)VBOXSERVICETOOLBOX_STAT_EXITCODE_NET_PATH_NOT_FOUND;
|
---|
1643 |
|
---|
1644 | default:
|
---|
1645 | #ifdef DEBUG_andy
|
---|
1646 | AssertMsgFailed(("Exit code for %Rrc not implemented\n", rc));
|
---|
1647 | #endif
|
---|
1648 | break;
|
---|
1649 | }
|
---|
1650 |
|
---|
1651 | return RTEXITCODE_FAILURE;
|
---|
1652 | }
|
---|
1653 |
|
---|
1654 | return RTEXITCODE_SUCCESS;
|
---|
1655 | }
|
---|
1656 |
|
---|
1657 |
|
---|
1658 | /**
|
---|
1659 | * Looks up the tool definition entry for the tool give by @a pszTool.
|
---|
1660 | *
|
---|
1661 | * @returns Pointer to the tool definition. NULL if not found.
|
---|
1662 | * @param pszTool The name of the tool.
|
---|
1663 | */
|
---|
1664 | static PCVBOXSERVICETOOLBOXTOOL vgsvcToolboxLookUp(const char *pszTool)
|
---|
1665 | {
|
---|
1666 | AssertPtrReturn(pszTool, NULL);
|
---|
1667 |
|
---|
1668 | /* Do a linear search, since we don't have that much stuff in the table. */
|
---|
1669 | for (unsigned i = 0; i < RT_ELEMENTS(g_aTools); i++)
|
---|
1670 | if (!strcmp(g_aTools[i].pszName, pszTool))
|
---|
1671 | return &g_aTools[i];
|
---|
1672 |
|
---|
1673 | return NULL;
|
---|
1674 | }
|
---|
1675 |
|
---|
1676 |
|
---|
1677 | /**
|
---|
1678 | * Converts a tool's exit code back to an IPRT error code.
|
---|
1679 | *
|
---|
1680 | * @return Converted IPRT status code.
|
---|
1681 | * @param pszTool Name of the toolbox tool to convert exit code for.
|
---|
1682 | * @param rcExit The tool's exit code to convert.
|
---|
1683 | */
|
---|
1684 | int VGSvcToolboxExitCodeConvertToRc(const char *pszTool, RTEXITCODE rcExit)
|
---|
1685 | {
|
---|
1686 | AssertPtrReturn(pszTool, VERR_INVALID_POINTER);
|
---|
1687 |
|
---|
1688 | PCVBOXSERVICETOOLBOXTOOL pTool = vgsvcToolboxLookUp(pszTool);
|
---|
1689 | if (pTool)
|
---|
1690 | return pTool->pfnExitCodeConvertToRc(rcExit);
|
---|
1691 |
|
---|
1692 | AssertMsgFailed(("Tool '%s' not found\n", pszTool));
|
---|
1693 | return VERR_GENERAL_FAILURE; /* Lookup failed, should not happen. */
|
---|
1694 | }
|
---|
1695 |
|
---|
1696 |
|
---|
1697 | /**
|
---|
1698 | * Entry point for internal toolbox.
|
---|
1699 | *
|
---|
1700 | * @return True if an internal tool was handled, false if not.
|
---|
1701 | * @param argc Number of arguments.
|
---|
1702 | * @param argv Pointer to argument array.
|
---|
1703 | * @param prcExit Where to store the exit code when an
|
---|
1704 | * internal toolbox command was handled.
|
---|
1705 | */
|
---|
1706 | bool VGSvcToolboxMain(int argc, char **argv, RTEXITCODE *prcExit)
|
---|
1707 | {
|
---|
1708 |
|
---|
1709 | /*
|
---|
1710 | * Check if the file named in argv[0] is one of the toolbox programs.
|
---|
1711 | */
|
---|
1712 | AssertReturn(argc > 0, false);
|
---|
1713 | const char *pszTool = RTPathFilename(argv[0]);
|
---|
1714 | PCVBOXSERVICETOOLBOXTOOL pTool = vgsvcToolboxLookUp(pszTool);
|
---|
1715 | if (!pTool)
|
---|
1716 | {
|
---|
1717 | /*
|
---|
1718 | * For debugging and testing purposes we also allow toolbox program access
|
---|
1719 | * when the first VBoxService argument is --use-toolbox.
|
---|
1720 | */
|
---|
1721 | if (argc < 2 || strcmp(argv[1], "--use-toolbox"))
|
---|
1722 | return false;
|
---|
1723 |
|
---|
1724 | /* No tool specified? Show toolbox help. */
|
---|
1725 | if (argc < 3)
|
---|
1726 | {
|
---|
1727 | vgsvcToolboxShowUsage();
|
---|
1728 | *prcExit = RTEXITCODE_SYNTAX;
|
---|
1729 | return true;
|
---|
1730 | }
|
---|
1731 |
|
---|
1732 | argc -= 2;
|
---|
1733 | argv += 2;
|
---|
1734 | pszTool = argv[0];
|
---|
1735 | pTool = vgsvcToolboxLookUp(pszTool);
|
---|
1736 | if (!pTool)
|
---|
1737 | {
|
---|
1738 | *prcExit = RTEXITCODE_SUCCESS;
|
---|
1739 | if (!strcmp(pszTool, "-V"))
|
---|
1740 | {
|
---|
1741 | vgsvcToolboxShowVersion();
|
---|
1742 | return true;
|
---|
1743 | }
|
---|
1744 | if ( strcmp(pszTool, "help")
|
---|
1745 | && strcmp(pszTool, "--help")
|
---|
1746 | && strcmp(pszTool, "-h"))
|
---|
1747 | *prcExit = RTEXITCODE_SYNTAX;
|
---|
1748 | vgsvcToolboxShowUsage();
|
---|
1749 | return true;
|
---|
1750 | }
|
---|
1751 | }
|
---|
1752 |
|
---|
1753 | /*
|
---|
1754 | * Invoke the handler.
|
---|
1755 | */
|
---|
1756 | RTMsgSetProgName("VBoxService/%s", pszTool);
|
---|
1757 | AssertPtr(pTool);
|
---|
1758 | *prcExit = pTool->pfnHandler(argc, argv);
|
---|
1759 |
|
---|
1760 | return true;
|
---|
1761 | }
|
---|
1762 |
|
---|