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