1 | /* $Id: VBoxServiceToolBox.cpp 69753 2017-11-19 14:27:58Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxServiceToolbox - Internal (BusyBox-like) toolbox.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2012-2017 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 | default:
|
---|
545 | #ifdef DEBUG_andy
|
---|
546 | AssertMsgFailed(("Exit code for %Rrc not implemented\n", rc));
|
---|
547 | #endif
|
---|
548 | break;
|
---|
549 | }
|
---|
550 |
|
---|
551 | return RTEXITCODE_FAILURE;
|
---|
552 | }
|
---|
553 |
|
---|
554 | return RTEXITCODE_SUCCESS;
|
---|
555 | }
|
---|
556 |
|
---|
557 | /**
|
---|
558 | * Prints information (based on given flags) of a file system object (file/directory/...)
|
---|
559 | * to stdout.
|
---|
560 | *
|
---|
561 | * @return IPRT status code.
|
---|
562 | * @param pszName Object name.
|
---|
563 | * @param cbName Size of object name.
|
---|
564 | * @param fOutputFlags Output / handling flags of type
|
---|
565 | * VBOXSERVICETOOLBOXOUTPUTFLAG.
|
---|
566 | * @param pObjInfo Pointer to object information.
|
---|
567 | */
|
---|
568 | static int vgsvcToolboxPrintFsInfo(const char *pszName, size_t cbName, uint32_t fOutputFlags, PRTFSOBJINFO pObjInfo)
|
---|
569 | {
|
---|
570 | AssertPtrReturn(pszName, VERR_INVALID_POINTER);
|
---|
571 | AssertReturn(cbName, VERR_INVALID_PARAMETER);
|
---|
572 | AssertPtrReturn(pObjInfo, VERR_INVALID_POINTER);
|
---|
573 |
|
---|
574 | RTFMODE fMode = pObjInfo->Attr.fMode;
|
---|
575 | char chFileType;
|
---|
576 | switch (fMode & RTFS_TYPE_MASK)
|
---|
577 | {
|
---|
578 | case RTFS_TYPE_FIFO: chFileType = 'f'; break;
|
---|
579 | case RTFS_TYPE_DEV_CHAR: chFileType = 'c'; break;
|
---|
580 | case RTFS_TYPE_DIRECTORY: chFileType = 'd'; break;
|
---|
581 | case RTFS_TYPE_DEV_BLOCK: chFileType = 'b'; break;
|
---|
582 | case RTFS_TYPE_FILE: chFileType = '-'; break;
|
---|
583 | case RTFS_TYPE_SYMLINK: chFileType = 'l'; break;
|
---|
584 | case RTFS_TYPE_SOCKET: chFileType = 's'; break;
|
---|
585 | case RTFS_TYPE_WHITEOUT: chFileType = 'w'; break;
|
---|
586 | default: chFileType = '?'; break;
|
---|
587 | }
|
---|
588 | /** @todo sticy bits++ */
|
---|
589 |
|
---|
590 | if (!(fOutputFlags & VBOXSERVICETOOLBOXOUTPUTFLAG_LONG))
|
---|
591 | {
|
---|
592 | if (fOutputFlags & VBOXSERVICETOOLBOXOUTPUTFLAG_PARSEABLE)
|
---|
593 | {
|
---|
594 | /** @todo Skip node_id if not present/available! */
|
---|
595 | RTPrintf("ftype=%c%cnode_id=%RU64%cname_len=%zu%cname=%s%c",
|
---|
596 | chFileType, 0, (uint64_t)pObjInfo->Attr.u.Unix.INodeId, 0,
|
---|
597 | cbName, 0, pszName, 0);
|
---|
598 | }
|
---|
599 | else
|
---|
600 | RTPrintf("%c %#18llx %3zu %s\n",
|
---|
601 | chFileType, (uint64_t)pObjInfo->Attr.u.Unix.INodeId, cbName, pszName);
|
---|
602 |
|
---|
603 | if (fOutputFlags & VBOXSERVICETOOLBOXOUTPUTFLAG_PARSEABLE) /* End of data block. */
|
---|
604 | RTPrintf("%c%c", 0, 0);
|
---|
605 | }
|
---|
606 | else
|
---|
607 | {
|
---|
608 | if (fOutputFlags & VBOXSERVICETOOLBOXOUTPUTFLAG_PARSEABLE)
|
---|
609 | {
|
---|
610 | RTPrintf("ftype=%c%c", chFileType, 0);
|
---|
611 | /** @todo Skip node_id if not present/available! */
|
---|
612 | RTPrintf("cnode_id=%RU64%c", (uint64_t)pObjInfo->Attr.u.Unix.INodeId, 0);
|
---|
613 | RTPrintf("owner_mask=%c%c%c%c",
|
---|
614 | fMode & RTFS_UNIX_IRUSR ? 'r' : '-',
|
---|
615 | fMode & RTFS_UNIX_IWUSR ? 'w' : '-',
|
---|
616 | fMode & RTFS_UNIX_IXUSR ? 'x' : '-', 0);
|
---|
617 | RTPrintf("group_mask=%c%c%c%c",
|
---|
618 | fMode & RTFS_UNIX_IRGRP ? 'r' : '-',
|
---|
619 | fMode & RTFS_UNIX_IWGRP ? 'w' : '-',
|
---|
620 | fMode & RTFS_UNIX_IXGRP ? 'x' : '-', 0);
|
---|
621 | RTPrintf("other_mask=%c%c%c%c",
|
---|
622 | fMode & RTFS_UNIX_IROTH ? 'r' : '-',
|
---|
623 | fMode & RTFS_UNIX_IWOTH ? 'w' : '-',
|
---|
624 | fMode & RTFS_UNIX_IXOTH ? 'x' : '-', 0);
|
---|
625 | RTPrintf("dos_mask=%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c",
|
---|
626 | fMode & RTFS_DOS_READONLY ? 'R' : '-',
|
---|
627 | fMode & RTFS_DOS_HIDDEN ? 'H' : '-',
|
---|
628 | fMode & RTFS_DOS_SYSTEM ? 'S' : '-',
|
---|
629 | fMode & RTFS_DOS_DIRECTORY ? 'D' : '-',
|
---|
630 | fMode & RTFS_DOS_ARCHIVED ? 'A' : '-',
|
---|
631 | fMode & RTFS_DOS_NT_DEVICE ? 'd' : '-',
|
---|
632 | fMode & RTFS_DOS_NT_NORMAL ? 'N' : '-',
|
---|
633 | fMode & RTFS_DOS_NT_TEMPORARY ? 'T' : '-',
|
---|
634 | fMode & RTFS_DOS_NT_SPARSE_FILE ? 'P' : '-',
|
---|
635 | fMode & RTFS_DOS_NT_REPARSE_POINT ? 'J' : '-',
|
---|
636 | fMode & RTFS_DOS_NT_COMPRESSED ? 'C' : '-',
|
---|
637 | fMode & RTFS_DOS_NT_OFFLINE ? 'O' : '-',
|
---|
638 | fMode & RTFS_DOS_NT_NOT_CONTENT_INDEXED ? 'I' : '-',
|
---|
639 | fMode & RTFS_DOS_NT_ENCRYPTED ? 'E' : '-', 0);
|
---|
640 |
|
---|
641 | char szTimeBirth[256];
|
---|
642 | RTTimeSpecToString(&pObjInfo->BirthTime, szTimeBirth, sizeof(szTimeBirth));
|
---|
643 | char szTimeChange[256];
|
---|
644 | RTTimeSpecToString(&pObjInfo->ChangeTime, szTimeChange, sizeof(szTimeChange));
|
---|
645 | char szTimeModification[256];
|
---|
646 | RTTimeSpecToString(&pObjInfo->ModificationTime, szTimeModification, sizeof(szTimeModification));
|
---|
647 | char szTimeAccess[256];
|
---|
648 | RTTimeSpecToString(&pObjInfo->AccessTime, szTimeAccess, sizeof(szTimeAccess));
|
---|
649 |
|
---|
650 | RTPrintf("hlinks=%RU32%cuid=%RU32%cgid=%RU32%cst_size=%RI64%calloc=%RI64%c"
|
---|
651 | "st_birthtime=%s%cst_ctime=%s%cst_mtime=%s%cst_atime=%s%c",
|
---|
652 | pObjInfo->Attr.u.Unix.cHardlinks, 0,
|
---|
653 | pObjInfo->Attr.u.Unix.uid, 0,
|
---|
654 | pObjInfo->Attr.u.Unix.gid, 0,
|
---|
655 | pObjInfo->cbObject, 0,
|
---|
656 | pObjInfo->cbAllocated, 0,
|
---|
657 | szTimeBirth, 0,
|
---|
658 | szTimeChange, 0,
|
---|
659 | szTimeModification, 0,
|
---|
660 | szTimeAccess, 0);
|
---|
661 | RTPrintf("cname_len=%zu%cname=%s%c",
|
---|
662 | cbName, 0, pszName, 0);
|
---|
663 |
|
---|
664 | /* End of data block. */
|
---|
665 | RTPrintf("%c%c", 0, 0);
|
---|
666 | }
|
---|
667 | else
|
---|
668 | {
|
---|
669 | RTPrintf("%c", chFileType);
|
---|
670 | RTPrintf("%c%c%c",
|
---|
671 | fMode & RTFS_UNIX_IRUSR ? 'r' : '-',
|
---|
672 | fMode & RTFS_UNIX_IWUSR ? 'w' : '-',
|
---|
673 | fMode & RTFS_UNIX_IXUSR ? 'x' : '-');
|
---|
674 | RTPrintf("%c%c%c",
|
---|
675 | fMode & RTFS_UNIX_IRGRP ? 'r' : '-',
|
---|
676 | fMode & RTFS_UNIX_IWGRP ? 'w' : '-',
|
---|
677 | fMode & RTFS_UNIX_IXGRP ? 'x' : '-');
|
---|
678 | RTPrintf("%c%c%c",
|
---|
679 | fMode & RTFS_UNIX_IROTH ? 'r' : '-',
|
---|
680 | fMode & RTFS_UNIX_IWOTH ? 'w' : '-',
|
---|
681 | fMode & RTFS_UNIX_IXOTH ? 'x' : '-');
|
---|
682 | RTPrintf(" %c%c%c%c%c%c%c%c%c%c%c%c%c%c",
|
---|
683 | fMode & RTFS_DOS_READONLY ? 'R' : '-',
|
---|
684 | fMode & RTFS_DOS_HIDDEN ? 'H' : '-',
|
---|
685 | fMode & RTFS_DOS_SYSTEM ? 'S' : '-',
|
---|
686 | fMode & RTFS_DOS_DIRECTORY ? 'D' : '-',
|
---|
687 | fMode & RTFS_DOS_ARCHIVED ? 'A' : '-',
|
---|
688 | fMode & RTFS_DOS_NT_DEVICE ? 'd' : '-',
|
---|
689 | fMode & RTFS_DOS_NT_NORMAL ? 'N' : '-',
|
---|
690 | fMode & RTFS_DOS_NT_TEMPORARY ? 'T' : '-',
|
---|
691 | fMode & RTFS_DOS_NT_SPARSE_FILE ? 'P' : '-',
|
---|
692 | fMode & RTFS_DOS_NT_REPARSE_POINT ? 'J' : '-',
|
---|
693 | fMode & RTFS_DOS_NT_COMPRESSED ? 'C' : '-',
|
---|
694 | fMode & RTFS_DOS_NT_OFFLINE ? 'O' : '-',
|
---|
695 | fMode & RTFS_DOS_NT_NOT_CONTENT_INDEXED ? 'I' : '-',
|
---|
696 | fMode & RTFS_DOS_NT_ENCRYPTED ? 'E' : '-');
|
---|
697 | RTPrintf(" %d %4d %4d %10lld %10lld %#llx %#llx %#llx %#llx",
|
---|
698 | pObjInfo->Attr.u.Unix.cHardlinks,
|
---|
699 | pObjInfo->Attr.u.Unix.uid,
|
---|
700 | pObjInfo->Attr.u.Unix.gid,
|
---|
701 | pObjInfo->cbObject,
|
---|
702 | pObjInfo->cbAllocated,
|
---|
703 | RTTimeSpecGetNano(&pObjInfo->BirthTime), /** @todo really ns? */
|
---|
704 | RTTimeSpecGetNano(&pObjInfo->ChangeTime), /** @todo really ns? */
|
---|
705 | RTTimeSpecGetNano(&pObjInfo->ModificationTime), /** @todo really ns? */
|
---|
706 | RTTimeSpecGetNano(&pObjInfo->AccessTime)); /** @todo really ns? */
|
---|
707 | RTPrintf(" %2zu %s\n", cbName, pszName);
|
---|
708 | }
|
---|
709 | }
|
---|
710 |
|
---|
711 | return VINF_SUCCESS;
|
---|
712 | }
|
---|
713 |
|
---|
714 |
|
---|
715 | /**
|
---|
716 | * Helper routine for ls tool doing the actual parsing and output of
|
---|
717 | * a specified directory.
|
---|
718 | *
|
---|
719 | * @return IPRT status code.
|
---|
720 | * @param pszDir Directory (path) to ouptut.
|
---|
721 | * @param fFlags Flags of type VBOXSERVICETOOLBOXLSFLAG.
|
---|
722 | * @param fOutputFlags Flags of type VBOXSERVICETOOLBOXOUTPUTFLAG.
|
---|
723 | */
|
---|
724 | static int vgsvcToolboxLsHandleDir(const char *pszDir, uint32_t fFlags, uint32_t fOutputFlags)
|
---|
725 | {
|
---|
726 | AssertPtrReturn(pszDir, VERR_INVALID_PARAMETER);
|
---|
727 |
|
---|
728 | if (fFlags & VBOXSERVICETOOLBOXOUTPUTFLAG_PARSEABLE)
|
---|
729 | RTPrintf("dname=%s%c", pszDir, 0);
|
---|
730 | else if (fFlags & VBOXSERVICETOOLBOXLSFLAG_RECURSIVE)
|
---|
731 | RTPrintf("%s:\n", pszDir);
|
---|
732 |
|
---|
733 | char szPathAbs[RTPATH_MAX + 1];
|
---|
734 | int rc = RTPathAbs(pszDir, szPathAbs, sizeof(szPathAbs));
|
---|
735 | if (RT_FAILURE(rc))
|
---|
736 | {
|
---|
737 | if (!(fOutputFlags & VBOXSERVICETOOLBOXOUTPUTFLAG_PARSEABLE))
|
---|
738 | RTMsgError("Failed to retrieve absolute path of '%s', rc=%Rrc\n", pszDir, rc);
|
---|
739 | return rc;
|
---|
740 | }
|
---|
741 |
|
---|
742 | RTDIR hDir;
|
---|
743 | rc = RTDirOpen(&hDir, szPathAbs);
|
---|
744 | if (RT_FAILURE(rc))
|
---|
745 | {
|
---|
746 | if (!(fOutputFlags & VBOXSERVICETOOLBOXOUTPUTFLAG_PARSEABLE))
|
---|
747 | RTMsgError("Failed to open directory '%s', rc=%Rrc\n", szPathAbs, rc);
|
---|
748 | return rc;
|
---|
749 | }
|
---|
750 |
|
---|
751 | RTLISTANCHOR dirList;
|
---|
752 | RTListInit(&dirList);
|
---|
753 |
|
---|
754 | /* To prevent races we need to read in the directory entries once
|
---|
755 | * and process them afterwards: First loop is displaying the current
|
---|
756 | * directory's content and second loop is diving deeper into
|
---|
757 | * sub directories (if wanted). */
|
---|
758 | for (;RT_SUCCESS(rc);)
|
---|
759 | {
|
---|
760 | RTDIRENTRYEX DirEntry;
|
---|
761 | rc = RTDirReadEx(hDir, &DirEntry, NULL, RTFSOBJATTRADD_UNIX, RTPATH_F_ON_LINK);
|
---|
762 | if (RT_SUCCESS(rc))
|
---|
763 | {
|
---|
764 | PVBOXSERVICETOOLBOXDIRENTRY pNode = (PVBOXSERVICETOOLBOXDIRENTRY)RTMemAlloc(sizeof(VBOXSERVICETOOLBOXDIRENTRY));
|
---|
765 | if (pNode)
|
---|
766 | {
|
---|
767 | memcpy(&pNode->dirEntry, &DirEntry, sizeof(RTDIRENTRYEX));
|
---|
768 | RTListAppend(&dirList, &pNode->Node);
|
---|
769 | }
|
---|
770 | else
|
---|
771 | rc = VERR_NO_MEMORY;
|
---|
772 | }
|
---|
773 | }
|
---|
774 |
|
---|
775 | if (rc == VERR_NO_MORE_FILES)
|
---|
776 | rc = VINF_SUCCESS;
|
---|
777 |
|
---|
778 | int rc2 = RTDirClose(hDir);
|
---|
779 | if (RT_FAILURE(rc2))
|
---|
780 | {
|
---|
781 | if (!(fOutputFlags & VBOXSERVICETOOLBOXOUTPUTFLAG_PARSEABLE))
|
---|
782 | RTMsgError("Failed to close dir '%s', rc=%Rrc\n", pszDir, rc2);
|
---|
783 | if (RT_SUCCESS(rc))
|
---|
784 | rc = rc2;
|
---|
785 | }
|
---|
786 |
|
---|
787 | if (RT_SUCCESS(rc))
|
---|
788 | {
|
---|
789 | PVBOXSERVICETOOLBOXDIRENTRY pNodeIt;
|
---|
790 | RTListForEach(&dirList, pNodeIt, VBOXSERVICETOOLBOXDIRENTRY, Node)
|
---|
791 | {
|
---|
792 | rc = vgsvcToolboxPrintFsInfo(pNodeIt->dirEntry.szName, pNodeIt->dirEntry.cbName,
|
---|
793 | fOutputFlags, &pNodeIt->dirEntry.Info);
|
---|
794 | if (RT_FAILURE(rc))
|
---|
795 | break;
|
---|
796 | }
|
---|
797 |
|
---|
798 | /* If everything went fine we do the second run (if needed) ... */
|
---|
799 | if ( RT_SUCCESS(rc)
|
---|
800 | && (fFlags & VBOXSERVICETOOLBOXLSFLAG_RECURSIVE))
|
---|
801 | {
|
---|
802 | /* Process all sub-directories. */
|
---|
803 | RTListForEach(&dirList, pNodeIt, VBOXSERVICETOOLBOXDIRENTRY, Node)
|
---|
804 | {
|
---|
805 | RTFMODE fMode = pNodeIt->dirEntry.Info.Attr.fMode;
|
---|
806 | switch (fMode & RTFS_TYPE_MASK)
|
---|
807 | {
|
---|
808 | case RTFS_TYPE_SYMLINK:
|
---|
809 | if (!(fFlags & VBOXSERVICETOOLBOXLSFLAG_SYMLINKS))
|
---|
810 | break;
|
---|
811 | RT_FALL_THRU();
|
---|
812 | case RTFS_TYPE_DIRECTORY:
|
---|
813 | {
|
---|
814 | const char *pszName = pNodeIt->dirEntry.szName;
|
---|
815 | if ( !RTStrICmp(pszName, ".")
|
---|
816 | || !RTStrICmp(pszName, ".."))
|
---|
817 | {
|
---|
818 | /* Skip dot directories. */
|
---|
819 | continue;
|
---|
820 | }
|
---|
821 |
|
---|
822 | char szPath[RTPATH_MAX];
|
---|
823 | rc = RTPathJoin(szPath, sizeof(szPath), pszDir, pNodeIt->dirEntry.szName);
|
---|
824 | if (RT_SUCCESS(rc))
|
---|
825 | rc = vgsvcToolboxLsHandleDir(szPath, fFlags, fOutputFlags);
|
---|
826 | break;
|
---|
827 | }
|
---|
828 |
|
---|
829 | default: /* Ignore the rest. */
|
---|
830 | break;
|
---|
831 | }
|
---|
832 | if (RT_FAILURE(rc))
|
---|
833 | break;
|
---|
834 | }
|
---|
835 | }
|
---|
836 | }
|
---|
837 |
|
---|
838 | /* Clean up the mess. */
|
---|
839 | PVBOXSERVICETOOLBOXDIRENTRY pNode, pSafe;
|
---|
840 | RTListForEachSafe(&dirList, pNode, pSafe, VBOXSERVICETOOLBOXDIRENTRY, Node)
|
---|
841 | {
|
---|
842 | RTListNodeRemove(&pNode->Node);
|
---|
843 | RTMemFree(pNode);
|
---|
844 | }
|
---|
845 | return rc;
|
---|
846 | }
|
---|
847 |
|
---|
848 |
|
---|
849 | /** @todo Document options! */
|
---|
850 | static char g_paszLsHelp[] =
|
---|
851 | " VBoxService [--use-toolbox] vbox_ls [<general options>] [option]...\n"
|
---|
852 | " [<file>...]\n\n"
|
---|
853 | "List information about files (the current directory by default).\n\n"
|
---|
854 | "Options:\n\n"
|
---|
855 | " [--dereference|-L]\n"
|
---|
856 | " [-l][-R]\n"
|
---|
857 | " [--verbose|-v]\n"
|
---|
858 | " [<file>...]\n"
|
---|
859 | "\n";
|
---|
860 |
|
---|
861 |
|
---|
862 | /**
|
---|
863 | * Main function for tool "vbox_ls".
|
---|
864 | *
|
---|
865 | * @return RTEXITCODE.
|
---|
866 | * @param argc Number of arguments.
|
---|
867 | * @param argv Pointer to argument array.
|
---|
868 | */
|
---|
869 | static RTEXITCODE vgsvcToolboxLs(int argc, char **argv)
|
---|
870 | {
|
---|
871 | static const RTGETOPTDEF s_aOptions[] =
|
---|
872 | {
|
---|
873 | { "--machinereadable", VBOXSERVICETOOLBOXOPT_MACHINE_READABLE, RTGETOPT_REQ_NOTHING },
|
---|
874 | { "--dereference", 'L', RTGETOPT_REQ_NOTHING },
|
---|
875 | { NULL, 'l', RTGETOPT_REQ_NOTHING },
|
---|
876 | { NULL, 'R', RTGETOPT_REQ_NOTHING },
|
---|
877 | { "--verbose", VBOXSERVICETOOLBOXOPT_VERBOSE, RTGETOPT_REQ_NOTHING}
|
---|
878 | };
|
---|
879 |
|
---|
880 | int ch;
|
---|
881 | RTGETOPTUNION ValueUnion;
|
---|
882 | RTGETOPTSTATE GetState;
|
---|
883 | int rc = RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions),
|
---|
884 | 1 /*iFirst*/, RTGETOPTINIT_FLAGS_OPTS_FIRST);
|
---|
885 | AssertRCReturn(rc, RTEXITCODE_INIT);
|
---|
886 |
|
---|
887 | bool fVerbose = false;
|
---|
888 | uint32_t fFlags = VBOXSERVICETOOLBOXLSFLAG_NONE;
|
---|
889 | uint32_t fOutputFlags = VBOXSERVICETOOLBOXOUTPUTFLAG_NONE;
|
---|
890 |
|
---|
891 | /* Init file list. */
|
---|
892 | RTLISTANCHOR fileList;
|
---|
893 | RTListInit(&fileList);
|
---|
894 |
|
---|
895 | while ( (ch = RTGetOpt(&GetState, &ValueUnion))
|
---|
896 | && RT_SUCCESS(rc))
|
---|
897 | {
|
---|
898 | /* For options that require an argument, ValueUnion has received the value. */
|
---|
899 | switch (ch)
|
---|
900 | {
|
---|
901 | case 'h':
|
---|
902 | vgsvcToolboxShowUsageHeader();
|
---|
903 | RTPrintf("%s", g_paszLsHelp);
|
---|
904 | return RTEXITCODE_SUCCESS;
|
---|
905 |
|
---|
906 | case 'L': /* Dereference symlinks. */
|
---|
907 | fFlags |= VBOXSERVICETOOLBOXLSFLAG_SYMLINKS;
|
---|
908 | break;
|
---|
909 |
|
---|
910 | case 'l': /* Print long format. */
|
---|
911 | fOutputFlags |= VBOXSERVICETOOLBOXOUTPUTFLAG_LONG;
|
---|
912 | break;
|
---|
913 |
|
---|
914 | case VBOXSERVICETOOLBOXOPT_MACHINE_READABLE:
|
---|
915 | fOutputFlags |= VBOXSERVICETOOLBOXOUTPUTFLAG_PARSEABLE;
|
---|
916 | break;
|
---|
917 |
|
---|
918 | case 'R': /* Recursive processing. */
|
---|
919 | fFlags |= VBOXSERVICETOOLBOXLSFLAG_RECURSIVE;
|
---|
920 | break;
|
---|
921 |
|
---|
922 | case VBOXSERVICETOOLBOXOPT_VERBOSE:
|
---|
923 | fVerbose = true;
|
---|
924 | break;
|
---|
925 |
|
---|
926 | case 'V':
|
---|
927 | vgsvcToolboxShowVersion();
|
---|
928 | return RTEXITCODE_SUCCESS;
|
---|
929 |
|
---|
930 | case VINF_GETOPT_NOT_OPTION:
|
---|
931 | /* Add file(s) to buffer. This enables processing multiple files
|
---|
932 | * at once.
|
---|
933 | *
|
---|
934 | * Since the non-options (RTGETOPTINIT_FLAGS_OPTS_FIRST) come last when
|
---|
935 | * processing this loop it's safe to immediately exit on syntax errors
|
---|
936 | * or showing the help text (see above). */
|
---|
937 | rc = vgsvcToolboxPathBufAddPathEntry(&fileList, ValueUnion.psz);
|
---|
938 | /** @todo r=bird: Nit: creating a list here is not really
|
---|
939 | * necessary since you've got one in argv that's
|
---|
940 | * accessible via RTGetOpt. */
|
---|
941 | break;
|
---|
942 |
|
---|
943 | default:
|
---|
944 | return RTGetOptPrintError(ch, &ValueUnion);
|
---|
945 | }
|
---|
946 | }
|
---|
947 |
|
---|
948 | if (RT_SUCCESS(rc))
|
---|
949 | {
|
---|
950 | /* If not files given add current directory to list. */
|
---|
951 | if (RTListIsEmpty(&fileList))
|
---|
952 | {
|
---|
953 | char szDirCur[RTPATH_MAX + 1];
|
---|
954 | rc = RTPathGetCurrent(szDirCur, sizeof(szDirCur));
|
---|
955 | if (RT_SUCCESS(rc))
|
---|
956 | {
|
---|
957 | rc = vgsvcToolboxPathBufAddPathEntry(&fileList, szDirCur);
|
---|
958 | if (RT_FAILURE(rc))
|
---|
959 | RTMsgError("Adding current directory failed, rc=%Rrc\n", rc);
|
---|
960 | }
|
---|
961 | else
|
---|
962 | RTMsgError("Getting current directory failed, rc=%Rrc\n", rc);
|
---|
963 | }
|
---|
964 |
|
---|
965 | /* Print magic/version. */
|
---|
966 | if (fOutputFlags & VBOXSERVICETOOLBOXOUTPUTFLAG_PARSEABLE)
|
---|
967 | {
|
---|
968 | rc = vgsvcToolboxStrmInit();
|
---|
969 | if (RT_FAILURE(rc))
|
---|
970 | RTMsgError("Error while initializing parseable streams, rc=%Rrc\n", rc);
|
---|
971 | vgsvcToolboxPrintStrmHeader("vbt_ls", 1 /* Stream version */);
|
---|
972 | }
|
---|
973 |
|
---|
974 | PVBOXSERVICETOOLBOXPATHENTRY pNodeIt;
|
---|
975 | RTListForEach(&fileList, pNodeIt, VBOXSERVICETOOLBOXPATHENTRY, Node)
|
---|
976 | {
|
---|
977 | if (RTFileExists(pNodeIt->pszName))
|
---|
978 | {
|
---|
979 | RTFSOBJINFO objInfo;
|
---|
980 | int rc2 = RTPathQueryInfoEx(pNodeIt->pszName, &objInfo,
|
---|
981 | RTFSOBJATTRADD_UNIX, RTPATH_F_ON_LINK /** @todo Follow link? */);
|
---|
982 | if (RT_FAILURE(rc2))
|
---|
983 | {
|
---|
984 | if (!(fOutputFlags & VBOXSERVICETOOLBOXOUTPUTFLAG_PARSEABLE))
|
---|
985 | RTMsgError("Cannot access '%s': No such file or directory\n", pNodeIt->pszName);
|
---|
986 | rc = VERR_FILE_NOT_FOUND;
|
---|
987 | /* Do not break here -- process every element in the list
|
---|
988 | * and keep failing rc. */
|
---|
989 | }
|
---|
990 | else
|
---|
991 | {
|
---|
992 | rc2 = vgsvcToolboxPrintFsInfo(pNodeIt->pszName,
|
---|
993 | strlen(pNodeIt->pszName) /* cbName */,
|
---|
994 | fOutputFlags,
|
---|
995 | &objInfo);
|
---|
996 | if (RT_FAILURE(rc2))
|
---|
997 | rc = rc2;
|
---|
998 | }
|
---|
999 | }
|
---|
1000 | else
|
---|
1001 | {
|
---|
1002 | int rc2 = vgsvcToolboxLsHandleDir(pNodeIt->pszName, fFlags, fOutputFlags);
|
---|
1003 | if (RT_FAILURE(rc2))
|
---|
1004 | rc = rc2;
|
---|
1005 | }
|
---|
1006 | }
|
---|
1007 |
|
---|
1008 | if (fOutputFlags & VBOXSERVICETOOLBOXOUTPUTFLAG_PARSEABLE) /* Output termination. */
|
---|
1009 | vgsvcToolboxPrintStrmTermination();
|
---|
1010 | }
|
---|
1011 | else if (fVerbose)
|
---|
1012 | RTMsgError("Failed with rc=%Rrc\n", rc);
|
---|
1013 |
|
---|
1014 | vgsvcToolboxPathBufDestroy(&fileList);
|
---|
1015 | return RT_SUCCESS(rc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
|
---|
1016 | }
|
---|
1017 |
|
---|
1018 |
|
---|
1019 | /* Try using RTPathRmCmd. */
|
---|
1020 | static RTEXITCODE vgsvcToolboxRm(int argc, char **argv)
|
---|
1021 | {
|
---|
1022 | return RTPathRmCmd(argc, argv);
|
---|
1023 | }
|
---|
1024 |
|
---|
1025 |
|
---|
1026 | static char g_paszMkTempHelp[] =
|
---|
1027 | " VBoxService [--use-toolbox] vbox_mktemp [<general options>] [<options>]\n"
|
---|
1028 | " <template>\n\n"
|
---|
1029 | "Create a temporary directory based on the template supplied. The first string\n"
|
---|
1030 | "of consecutive 'X' characters in the template will be replaced to form a unique\n"
|
---|
1031 | "name for the directory. The template may not contain a path. The default\n"
|
---|
1032 | "creation mode is 0600 for files and 0700 for directories. If no path is\n"
|
---|
1033 | "specified the default temporary directory will be used.\n"
|
---|
1034 | "Options:\n\n"
|
---|
1035 | " [--directory|-d] Create a directory instead of a file.\n"
|
---|
1036 | " [--mode|-m <mode>] Create the object with mode <mode>.\n"
|
---|
1037 | " [--secure|-s] Fail if the object cannot be created securely.\n"
|
---|
1038 | " [--tmpdir|-t <path>] Create the object with the absolute path <path>.\n"
|
---|
1039 | "\n";
|
---|
1040 |
|
---|
1041 |
|
---|
1042 | /**
|
---|
1043 | * Report the result of a vbox_mktemp operation.
|
---|
1044 | *
|
---|
1045 | * Either errors to stderr (not machine-readable) or everything to stdout as
|
---|
1046 | * {name}\0{rc}\0 (machine- readable format). The message may optionally
|
---|
1047 | * contain a '%s' for the file name and an %Rrc for the result code in that
|
---|
1048 | * order. In future a "verbose" flag may be added, without which nothing will
|
---|
1049 | * be output in non-machine- readable mode. Sets prc if rc is a non-success
|
---|
1050 | * code.
|
---|
1051 | */
|
---|
1052 | static void toolboxMkTempReport(const char *pcszMessage, const char *pcszFile,
|
---|
1053 | bool fActive, int rc, uint32_t fOutputFlags, int *prc)
|
---|
1054 | {
|
---|
1055 | if (!fActive)
|
---|
1056 | return;
|
---|
1057 | if (!(fOutputFlags & VBOXSERVICETOOLBOXOUTPUTFLAG_PARSEABLE))
|
---|
1058 | if (RT_SUCCESS(rc))
|
---|
1059 | RTPrintf(pcszMessage, pcszFile, rc);
|
---|
1060 | else
|
---|
1061 | RTMsgError(pcszMessage, pcszFile, rc);
|
---|
1062 | else
|
---|
1063 | RTPrintf("name=%s%crc=%d%c", pcszFile, 0, rc, 0);
|
---|
1064 | if (prc && RT_FAILURE(rc))
|
---|
1065 | *prc = rc;
|
---|
1066 | }
|
---|
1067 |
|
---|
1068 |
|
---|
1069 | /**
|
---|
1070 | * Main function for tool "vbox_mktemp".
|
---|
1071 | *
|
---|
1072 | * @return RTEXITCODE.
|
---|
1073 | * @param argc Number of arguments.
|
---|
1074 | * @param argv Pointer to argument array.
|
---|
1075 | */
|
---|
1076 | static RTEXITCODE vgsvcToolboxMkTemp(int argc, char **argv)
|
---|
1077 | {
|
---|
1078 | static const RTGETOPTDEF s_aOptions[] =
|
---|
1079 | {
|
---|
1080 | { "--machinereadable", VBOXSERVICETOOLBOXOPT_MACHINE_READABLE,
|
---|
1081 | RTGETOPT_REQ_NOTHING },
|
---|
1082 | { "--directory", 'd', RTGETOPT_REQ_NOTHING },
|
---|
1083 | { "--mode", 'm', RTGETOPT_REQ_STRING },
|
---|
1084 | { "--secure", 's', RTGETOPT_REQ_NOTHING },
|
---|
1085 | { "--tmpdir", 't', RTGETOPT_REQ_STRING },
|
---|
1086 | };
|
---|
1087 |
|
---|
1088 | enum
|
---|
1089 | {
|
---|
1090 | /* Isn't that a bit long? s/VBOXSERVICETOOLBOX/VSTB/ ? */
|
---|
1091 | /** Create a temporary directory instead of a temporary file. */
|
---|
1092 | VBOXSERVICETOOLBOXMKTEMPFLAG_DIRECTORY = RT_BIT_32(0),
|
---|
1093 | /** Only create the temporary object if the operation is expected
|
---|
1094 | * to be secure. Not guaranteed to be supported on a particular
|
---|
1095 | * set-up. */
|
---|
1096 | VBOXSERVICETOOLBOXMKTEMPFLAG_SECURE = RT_BIT_32(1)
|
---|
1097 | };
|
---|
1098 |
|
---|
1099 | int ch, rc;
|
---|
1100 | RTGETOPTUNION ValueUnion;
|
---|
1101 | RTGETOPTSTATE GetState;
|
---|
1102 | rc = RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 1 /*iFirst*/, RTGETOPTINIT_FLAGS_OPTS_FIRST);
|
---|
1103 | AssertRCReturn(rc, RTEXITCODE_INIT);
|
---|
1104 |
|
---|
1105 | uint32_t fFlags = 0;
|
---|
1106 | uint32_t fOutputFlags = 0;
|
---|
1107 | int cNonOptions = 0;
|
---|
1108 | RTFMODE fMode = 0700;
|
---|
1109 | bool fModeSet = false;
|
---|
1110 | const char *pcszPath = NULL;
|
---|
1111 | const char *pcszTemplate;
|
---|
1112 | char szTemplateWithPath[RTPATH_MAX] = "";
|
---|
1113 |
|
---|
1114 | while ( (ch = RTGetOpt(&GetState, &ValueUnion))
|
---|
1115 | && RT_SUCCESS(rc))
|
---|
1116 | {
|
---|
1117 | /* For options that require an argument, ValueUnion has received the value. */
|
---|
1118 | switch (ch)
|
---|
1119 | {
|
---|
1120 | case 'h':
|
---|
1121 | vgsvcToolboxShowUsageHeader();
|
---|
1122 | RTPrintf("%s", g_paszMkTempHelp);
|
---|
1123 | return RTEXITCODE_SUCCESS;
|
---|
1124 |
|
---|
1125 | case 'V':
|
---|
1126 | vgsvcToolboxShowVersion();
|
---|
1127 | return RTEXITCODE_SUCCESS;
|
---|
1128 |
|
---|
1129 | case VBOXSERVICETOOLBOXOPT_MACHINE_READABLE:
|
---|
1130 | fOutputFlags |= VBOXSERVICETOOLBOXOUTPUTFLAG_PARSEABLE;
|
---|
1131 | break;
|
---|
1132 |
|
---|
1133 | case 'd':
|
---|
1134 | fFlags |= VBOXSERVICETOOLBOXMKTEMPFLAG_DIRECTORY;
|
---|
1135 | break;
|
---|
1136 |
|
---|
1137 | case 'm':
|
---|
1138 | rc = vgsvcToolboxParseMode(ValueUnion.psz, &fMode);
|
---|
1139 | if (RT_FAILURE(rc))
|
---|
1140 | return RTEXITCODE_SYNTAX;
|
---|
1141 | fModeSet = true;
|
---|
1142 | #ifndef RT_OS_WINDOWS
|
---|
1143 | umask(0); /* RTDirCreate workaround */
|
---|
1144 | #endif
|
---|
1145 | break;
|
---|
1146 | case 's':
|
---|
1147 | fFlags |= VBOXSERVICETOOLBOXMKTEMPFLAG_SECURE;
|
---|
1148 | break;
|
---|
1149 |
|
---|
1150 | case 't':
|
---|
1151 | pcszPath = ValueUnion.psz;
|
---|
1152 | break;
|
---|
1153 |
|
---|
1154 | case VINF_GETOPT_NOT_OPTION:
|
---|
1155 | /* RTGetOpt will sort these to the end of the argv vector so
|
---|
1156 | * that we will deal with them afterwards. */
|
---|
1157 | ++cNonOptions;
|
---|
1158 | break;
|
---|
1159 |
|
---|
1160 | default:
|
---|
1161 | return RTGetOptPrintError(ch, &ValueUnion);
|
---|
1162 | }
|
---|
1163 | }
|
---|
1164 |
|
---|
1165 | /* Print magic/version. */
|
---|
1166 | if (fOutputFlags & VBOXSERVICETOOLBOXOUTPUTFLAG_PARSEABLE)
|
---|
1167 | {
|
---|
1168 | rc = vgsvcToolboxStrmInit();
|
---|
1169 | if (RT_FAILURE(rc))
|
---|
1170 | RTMsgError("Error while initializing parseable streams, rc=%Rrc\n", rc);
|
---|
1171 | vgsvcToolboxPrintStrmHeader("vbt_mktemp", 1 /* Stream version */);
|
---|
1172 | }
|
---|
1173 |
|
---|
1174 | if (fFlags & VBOXSERVICETOOLBOXMKTEMPFLAG_SECURE && fModeSet)
|
---|
1175 | {
|
---|
1176 | toolboxMkTempReport("'-s' and '-m' parameters cannot be used together.\n", "",
|
---|
1177 | true, VERR_INVALID_PARAMETER, fOutputFlags, &rc);
|
---|
1178 | return RTEXITCODE_SYNTAX;
|
---|
1179 | }
|
---|
1180 |
|
---|
1181 | /* We need exactly one template, containing at least one 'X'. */
|
---|
1182 | if (cNonOptions != 1)
|
---|
1183 | {
|
---|
1184 | toolboxMkTempReport("Please specify exactly one template.\n", "", true, VERR_INVALID_PARAMETER, fOutputFlags, &rc);
|
---|
1185 | return RTEXITCODE_SYNTAX;
|
---|
1186 | }
|
---|
1187 | pcszTemplate = argv[argc - 1];
|
---|
1188 |
|
---|
1189 | /* Validate that the template is as IPRT requires (asserted by IPRT). */
|
---|
1190 | if ( RTPathHasPath(pcszTemplate)
|
---|
1191 | || ( !strstr(pcszTemplate, "XXX")
|
---|
1192 | && pcszTemplate[strlen(pcszTemplate) - 1] != 'X'))
|
---|
1193 | {
|
---|
1194 | toolboxMkTempReport("Template '%s' should contain a file name with no path and at least three consecutive 'X' characters or ending in 'X'.\n",
|
---|
1195 | pcszTemplate, true, VERR_INVALID_PARAMETER, fOutputFlags, &rc);
|
---|
1196 | return RTEXITCODE_FAILURE;
|
---|
1197 | }
|
---|
1198 | if (pcszPath && !RTPathStartsWithRoot(pcszPath))
|
---|
1199 | {
|
---|
1200 | toolboxMkTempReport("Path '%s' should be absolute.\n", pcszPath, true, VERR_INVALID_PARAMETER, fOutputFlags, &rc);
|
---|
1201 | return RTEXITCODE_FAILURE;
|
---|
1202 | }
|
---|
1203 | if (pcszPath)
|
---|
1204 | {
|
---|
1205 | rc = RTStrCopy(szTemplateWithPath, sizeof(szTemplateWithPath), pcszPath);
|
---|
1206 | if (RT_FAILURE(rc))
|
---|
1207 | {
|
---|
1208 | toolboxMkTempReport("Path '%s' too long.\n", pcszPath, true, VERR_INVALID_PARAMETER, fOutputFlags, &rc);
|
---|
1209 | return RTEXITCODE_FAILURE;
|
---|
1210 | }
|
---|
1211 | }
|
---|
1212 | else
|
---|
1213 | {
|
---|
1214 | rc = RTPathTemp(szTemplateWithPath, sizeof(szTemplateWithPath));
|
---|
1215 | if (RT_FAILURE(rc))
|
---|
1216 | {
|
---|
1217 | toolboxMkTempReport("Failed to get the temporary directory.\n", "", true, VERR_INVALID_PARAMETER, fOutputFlags, &rc);
|
---|
1218 | return RTEXITCODE_FAILURE;
|
---|
1219 | }
|
---|
1220 | }
|
---|
1221 | rc = RTPathAppend(szTemplateWithPath, sizeof(szTemplateWithPath), pcszTemplate);
|
---|
1222 | if (RT_FAILURE(rc))
|
---|
1223 | {
|
---|
1224 | toolboxMkTempReport("Template '%s' too long for path.\n", pcszTemplate, true, VERR_INVALID_PARAMETER, fOutputFlags, &rc);
|
---|
1225 | return RTEXITCODE_FAILURE;
|
---|
1226 | }
|
---|
1227 |
|
---|
1228 | if (fFlags & VBOXSERVICETOOLBOXMKTEMPFLAG_DIRECTORY)
|
---|
1229 | {
|
---|
1230 | rc = fFlags & VBOXSERVICETOOLBOXMKTEMPFLAG_SECURE
|
---|
1231 | ? RTDirCreateTempSecure(szTemplateWithPath)
|
---|
1232 | : RTDirCreateTemp(szTemplateWithPath, fMode);
|
---|
1233 | toolboxMkTempReport("Created temporary directory '%s'.\n",
|
---|
1234 | szTemplateWithPath, RT_SUCCESS(rc), rc,
|
---|
1235 | fOutputFlags, NULL);
|
---|
1236 | /* RTDirCreateTemp[Secure] sets the template to "" on failure. */
|
---|
1237 | toolboxMkTempReport("The following error occurred while creating a temporary directory from template '%s': %Rrc.\n",
|
---|
1238 | pcszTemplate, RT_FAILURE(rc), rc, fOutputFlags, NULL /*prc*/);
|
---|
1239 | }
|
---|
1240 | else
|
---|
1241 | {
|
---|
1242 | rc = fFlags & VBOXSERVICETOOLBOXMKTEMPFLAG_SECURE
|
---|
1243 | ? RTFileCreateTempSecure(szTemplateWithPath)
|
---|
1244 | : RTFileCreateTemp(szTemplateWithPath, fMode);
|
---|
1245 | toolboxMkTempReport("Created temporary file '%s'.\n",
|
---|
1246 | szTemplateWithPath, RT_SUCCESS(rc), rc,
|
---|
1247 | fOutputFlags, NULL);
|
---|
1248 | /* RTFileCreateTemp[Secure] sets the template to "" on failure. */
|
---|
1249 | toolboxMkTempReport("The following error occurred while creating a temporary file from template '%s': %Rrc.\n",
|
---|
1250 | pcszTemplate, RT_FAILURE(rc), rc, fOutputFlags, NULL /*prc*/);
|
---|
1251 | }
|
---|
1252 | if (fOutputFlags & VBOXSERVICETOOLBOXOUTPUTFLAG_PARSEABLE) /* Output termination. */
|
---|
1253 | vgsvcToolboxPrintStrmTermination();
|
---|
1254 | return RT_SUCCESS(rc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
|
---|
1255 | }
|
---|
1256 |
|
---|
1257 |
|
---|
1258 | /** @todo Document options! */
|
---|
1259 | static char g_paszMkDirHelp[] =
|
---|
1260 | " VBoxService [--use-toolbox] vbox_mkdir [<general options>] [<options>]\n"
|
---|
1261 | " <directory>...\n\n"
|
---|
1262 | "Options:\n\n"
|
---|
1263 | " [--mode|-m <mode>] The file mode to set (chmod) on the created\n"
|
---|
1264 | " directories. Default: a=rwx & umask.\n"
|
---|
1265 | " [--parents|-p] Create parent directories as needed, no\n"
|
---|
1266 | " error if the directory already exists.\n"
|
---|
1267 | " [--verbose|-v] Display a message for each created directory.\n"
|
---|
1268 | "\n";
|
---|
1269 |
|
---|
1270 |
|
---|
1271 | /**
|
---|
1272 | * Main function for tool "vbox_mkdir".
|
---|
1273 | *
|
---|
1274 | * @return RTEXITCODE.
|
---|
1275 | * @param argc Number of arguments.
|
---|
1276 | * @param argv Pointer to argument array.
|
---|
1277 | */
|
---|
1278 | static RTEXITCODE vgsvcToolboxMkDir(int argc, char **argv)
|
---|
1279 | {
|
---|
1280 | static const RTGETOPTDEF s_aOptions[] =
|
---|
1281 | {
|
---|
1282 | { "--mode", 'm', RTGETOPT_REQ_STRING },
|
---|
1283 | { "--parents", 'p', RTGETOPT_REQ_NOTHING},
|
---|
1284 | { "--verbose", 'v', RTGETOPT_REQ_NOTHING}
|
---|
1285 | };
|
---|
1286 |
|
---|
1287 | int ch;
|
---|
1288 | RTGETOPTUNION ValueUnion;
|
---|
1289 | RTGETOPTSTATE GetState;
|
---|
1290 | int rc = RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions),
|
---|
1291 | 1 /*iFirst*/, RTGETOPTINIT_FLAGS_OPTS_FIRST);
|
---|
1292 | AssertRCReturn(rc, RTEXITCODE_INIT);
|
---|
1293 |
|
---|
1294 | bool fMakeParentDirs = false;
|
---|
1295 | bool fVerbose = false;
|
---|
1296 | RTFMODE fDirMode = RTFS_UNIX_IRWXU | RTFS_UNIX_IRWXG | RTFS_UNIX_IRWXO;
|
---|
1297 | int cDirsCreated = 0;
|
---|
1298 |
|
---|
1299 | while ((ch = RTGetOpt(&GetState, &ValueUnion)))
|
---|
1300 | {
|
---|
1301 | /* For options that require an argument, ValueUnion has received the value. */
|
---|
1302 | switch (ch)
|
---|
1303 | {
|
---|
1304 | case 'p':
|
---|
1305 | fMakeParentDirs = true;
|
---|
1306 | break;
|
---|
1307 |
|
---|
1308 | case 'm':
|
---|
1309 | rc = vgsvcToolboxParseMode(ValueUnion.psz, &fDirMode);
|
---|
1310 | if (RT_FAILURE(rc))
|
---|
1311 | return RTEXITCODE_SYNTAX;
|
---|
1312 | #ifndef RT_OS_WINDOWS
|
---|
1313 | umask(0); /* RTDirCreate workaround */
|
---|
1314 | #endif
|
---|
1315 | break;
|
---|
1316 |
|
---|
1317 | case 'v':
|
---|
1318 | fVerbose = true;
|
---|
1319 | break;
|
---|
1320 |
|
---|
1321 | case 'h':
|
---|
1322 | vgsvcToolboxShowUsageHeader();
|
---|
1323 | RTPrintf("%s", g_paszMkDirHelp);
|
---|
1324 | return RTEXITCODE_SUCCESS;
|
---|
1325 |
|
---|
1326 | case 'V':
|
---|
1327 | vgsvcToolboxShowVersion();
|
---|
1328 | return RTEXITCODE_SUCCESS;
|
---|
1329 |
|
---|
1330 | case VINF_GETOPT_NOT_OPTION:
|
---|
1331 | if (fMakeParentDirs)
|
---|
1332 | /** @todo r=bird: If fVerbose is set, we should also show
|
---|
1333 | * which directories that get created, parents as well as
|
---|
1334 | * omitting existing final dirs. Annoying, but check any
|
---|
1335 | * mkdir implementation (try "mkdir -pv asdf/1/2/3/4"
|
---|
1336 | * twice). */
|
---|
1337 | rc = RTDirCreateFullPath(ValueUnion.psz, fDirMode);
|
---|
1338 | else
|
---|
1339 | rc = RTDirCreate(ValueUnion.psz, fDirMode, 0);
|
---|
1340 | if (RT_FAILURE(rc))
|
---|
1341 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "Could not create directory '%s': %Rra\n",
|
---|
1342 | ValueUnion.psz, rc);
|
---|
1343 | if (fVerbose)
|
---|
1344 | RTMsgInfo("Created directory '%s', mode %#RTfmode\n", ValueUnion.psz, fDirMode);
|
---|
1345 | cDirsCreated++;
|
---|
1346 | break;
|
---|
1347 |
|
---|
1348 | default:
|
---|
1349 | return RTGetOptPrintError(ch, &ValueUnion);
|
---|
1350 | }
|
---|
1351 | }
|
---|
1352 | AssertRC(rc);
|
---|
1353 |
|
---|
1354 | if (cDirsCreated == 0)
|
---|
1355 | return RTMsgErrorExit(RTEXITCODE_SYNTAX, "No directory argument.");
|
---|
1356 |
|
---|
1357 | return RTEXITCODE_SUCCESS;
|
---|
1358 | }
|
---|
1359 |
|
---|
1360 |
|
---|
1361 | /** @todo Document options! */
|
---|
1362 | static char g_paszStatHelp[] =
|
---|
1363 | " VBoxService [--use-toolbox] vbox_stat [<general options>] [<options>]\n"
|
---|
1364 | " <file>...\n\n"
|
---|
1365 | "Display file or file system status.\n\n"
|
---|
1366 | "Options:\n\n"
|
---|
1367 | " [--file-system|-f]\n"
|
---|
1368 | " [--dereference|-L]\n"
|
---|
1369 | " [--terse|-t]\n"
|
---|
1370 | " [--verbose|-v]\n"
|
---|
1371 | "\n";
|
---|
1372 |
|
---|
1373 |
|
---|
1374 | /**
|
---|
1375 | * Main function for tool "vbox_stat".
|
---|
1376 | *
|
---|
1377 | * @return RTEXITCODE.
|
---|
1378 | * @param argc Number of arguments.
|
---|
1379 | * @param argv Pointer to argument array.
|
---|
1380 | */
|
---|
1381 | static RTEXITCODE vgsvcToolboxStat(int argc, char **argv)
|
---|
1382 | {
|
---|
1383 | static const RTGETOPTDEF s_aOptions[] =
|
---|
1384 | {
|
---|
1385 | { "--file-system", 'f', RTGETOPT_REQ_NOTHING },
|
---|
1386 | { "--dereference", 'L', RTGETOPT_REQ_NOTHING },
|
---|
1387 | { "--machinereadable", VBOXSERVICETOOLBOXOPT_MACHINE_READABLE, RTGETOPT_REQ_NOTHING },
|
---|
1388 | { "--terse", 't', RTGETOPT_REQ_NOTHING },
|
---|
1389 | { "--verbose", 'v', RTGETOPT_REQ_NOTHING }
|
---|
1390 | };
|
---|
1391 |
|
---|
1392 | int ch;
|
---|
1393 | RTGETOPTUNION ValueUnion;
|
---|
1394 | RTGETOPTSTATE GetState;
|
---|
1395 | RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 1 /*iFirst*/, RTGETOPTINIT_FLAGS_OPTS_FIRST);
|
---|
1396 |
|
---|
1397 | int rc = VINF_SUCCESS;
|
---|
1398 | bool fVerbose = false;
|
---|
1399 | uint32_t fOutputFlags = VBOXSERVICETOOLBOXOUTPUTFLAG_LONG; /* Use long mode by default. */
|
---|
1400 | uint32_t fQueryInfoFlags = RTPATH_F_ON_LINK;
|
---|
1401 |
|
---|
1402 | /* Init file list. */
|
---|
1403 | RTLISTANCHOR fileList;
|
---|
1404 | RTListInit(&fileList);
|
---|
1405 |
|
---|
1406 | while ( (ch = RTGetOpt(&GetState, &ValueUnion))
|
---|
1407 | && RT_SUCCESS(rc))
|
---|
1408 | {
|
---|
1409 | /* For options that require an argument, ValueUnion has received the value. */
|
---|
1410 | switch (ch)
|
---|
1411 | {
|
---|
1412 | case 'f':
|
---|
1413 | RTMsgError("Sorry, option '%s' is not implemented yet!\n", ValueUnion.pDef->pszLong);
|
---|
1414 | rc = VERR_INVALID_PARAMETER;
|
---|
1415 | break;
|
---|
1416 |
|
---|
1417 | case 'L':
|
---|
1418 | fQueryInfoFlags &= ~RTPATH_F_ON_LINK;
|
---|
1419 | fQueryInfoFlags |= RTPATH_F_FOLLOW_LINK;
|
---|
1420 | break;
|
---|
1421 |
|
---|
1422 | case VBOXSERVICETOOLBOXOPT_MACHINE_READABLE:
|
---|
1423 | fOutputFlags |= VBOXSERVICETOOLBOXOUTPUTFLAG_PARSEABLE;
|
---|
1424 | break;
|
---|
1425 |
|
---|
1426 | case 'v': /** @todo r=bird: There is no verbose option for stat. */
|
---|
1427 | fVerbose = true;
|
---|
1428 | break;
|
---|
1429 |
|
---|
1430 | case 'h':
|
---|
1431 | vgsvcToolboxShowUsageHeader();
|
---|
1432 | RTPrintf("%s", g_paszStatHelp);
|
---|
1433 | return RTEXITCODE_SUCCESS;
|
---|
1434 |
|
---|
1435 | case 'V':
|
---|
1436 | vgsvcToolboxShowVersion();
|
---|
1437 | return RTEXITCODE_SUCCESS;
|
---|
1438 |
|
---|
1439 | case VINF_GETOPT_NOT_OPTION:
|
---|
1440 | {
|
---|
1441 | /** @todo r=bird: The whole fileList is unecessary because you're using
|
---|
1442 | * RTGETOPTINIT_FLAGS_OPTS_FIRST. You can obviously do the processing right
|
---|
1443 | * here, but you could also just drop down and rewind GetState.iNext by one and
|
---|
1444 | * continue there. */
|
---|
1445 |
|
---|
1446 | /* Add file(s) to buffer. This enables processing multiple files
|
---|
1447 | * at once.
|
---|
1448 | *
|
---|
1449 | * Since the non-options (RTGETOPTINIT_FLAGS_OPTS_FIRST) come last when
|
---|
1450 | * processing this loop it's safe to immediately exit on syntax errors
|
---|
1451 | * or showing the help text (see above). */
|
---|
1452 | rc = vgsvcToolboxPathBufAddPathEntry(&fileList, ValueUnion.psz);
|
---|
1453 | break;
|
---|
1454 | }
|
---|
1455 |
|
---|
1456 | default:
|
---|
1457 | return RTGetOptPrintError(ch, &ValueUnion);
|
---|
1458 | }
|
---|
1459 | }
|
---|
1460 |
|
---|
1461 | if (RT_SUCCESS(rc))
|
---|
1462 | {
|
---|
1463 | if (fOutputFlags & VBOXSERVICETOOLBOXOUTPUTFLAG_PARSEABLE) /* Output termination. */
|
---|
1464 | {
|
---|
1465 | rc = vgsvcToolboxStrmInit();
|
---|
1466 | if (RT_FAILURE(rc))
|
---|
1467 | RTMsgError("Error while initializing parseable streams, rc=%Rrc\n", rc);
|
---|
1468 | vgsvcToolboxPrintStrmHeader("vbt_stat", 1 /* Stream version */);
|
---|
1469 | }
|
---|
1470 |
|
---|
1471 | PVBOXSERVICETOOLBOXPATHENTRY pNodeIt;
|
---|
1472 | RTListForEach(&fileList, pNodeIt, VBOXSERVICETOOLBOXPATHENTRY, Node)
|
---|
1473 | {
|
---|
1474 | RTFSOBJINFO objInfo;
|
---|
1475 | int rc2 = RTPathQueryInfoEx(pNodeIt->pszName, &objInfo, RTFSOBJATTRADD_UNIX, fQueryInfoFlags);
|
---|
1476 | if (RT_FAILURE(rc2))
|
---|
1477 | {
|
---|
1478 | if (!(fOutputFlags & VBOXSERVICETOOLBOXOUTPUTFLAG_PARSEABLE))
|
---|
1479 | RTMsgError("Cannot stat for '%s': %Rrc\n", pNodeIt->pszName, rc2);
|
---|
1480 | }
|
---|
1481 | else
|
---|
1482 | {
|
---|
1483 | rc2 = vgsvcToolboxPrintFsInfo(pNodeIt->pszName,
|
---|
1484 | strlen(pNodeIt->pszName) /* cbName */,
|
---|
1485 | fOutputFlags,
|
---|
1486 | &objInfo);
|
---|
1487 | }
|
---|
1488 |
|
---|
1489 | if (RT_SUCCESS(rc))
|
---|
1490 | rc = rc2;
|
---|
1491 | /* Do not break here -- process every element in the list
|
---|
1492 | * and keep (initial) failing rc. */
|
---|
1493 | }
|
---|
1494 |
|
---|
1495 | if (fOutputFlags & VBOXSERVICETOOLBOXOUTPUTFLAG_PARSEABLE) /* Output termination. */
|
---|
1496 | vgsvcToolboxPrintStrmTermination();
|
---|
1497 |
|
---|
1498 | /* At this point the overall result (success/failure) should be in rc. */
|
---|
1499 |
|
---|
1500 | if (RTListIsEmpty(&fileList))
|
---|
1501 | RTMsgError("Missing operand\n");
|
---|
1502 | }
|
---|
1503 | else if (fVerbose)
|
---|
1504 | RTMsgError("Failed with rc=%Rrc\n", rc);
|
---|
1505 |
|
---|
1506 | vgsvcToolboxPathBufDestroy(&fileList);
|
---|
1507 |
|
---|
1508 | if (RT_FAILURE(rc))
|
---|
1509 | {
|
---|
1510 | switch (rc)
|
---|
1511 | {
|
---|
1512 | case VERR_ACCESS_DENIED:
|
---|
1513 | return (RTEXITCODE)VBOXSERVICETOOLBOX_STAT_EXITCODE_ACCESS_DENIED;
|
---|
1514 |
|
---|
1515 | case VERR_FILE_NOT_FOUND:
|
---|
1516 | return (RTEXITCODE)VBOXSERVICETOOLBOX_STAT_EXITCODE_FILE_NOT_FOUND;
|
---|
1517 |
|
---|
1518 | case VERR_PATH_NOT_FOUND:
|
---|
1519 | return (RTEXITCODE)VBOXSERVICETOOLBOX_STAT_EXITCODE_PATH_NOT_FOUND;
|
---|
1520 |
|
---|
1521 | default:
|
---|
1522 | #ifdef DEBUG_andy
|
---|
1523 | AssertMsgFailed(("Exit code for %Rrc not implemented\n", rc));
|
---|
1524 | #endif
|
---|
1525 | break;
|
---|
1526 | }
|
---|
1527 |
|
---|
1528 | return RTEXITCODE_FAILURE;
|
---|
1529 | }
|
---|
1530 |
|
---|
1531 | return RTEXITCODE_SUCCESS;
|
---|
1532 | }
|
---|
1533 |
|
---|
1534 |
|
---|
1535 | /**
|
---|
1536 | * Looks up the tool definition entry for the tool give by @a pszTool.
|
---|
1537 | *
|
---|
1538 | * @returns Pointer to the tool definition. NULL if not found.
|
---|
1539 | * @param pszTool The name of the tool.
|
---|
1540 | */
|
---|
1541 | static PCVBOXSERVICETOOLBOXTOOL vgsvcToolboxLookUp(const char *pszTool)
|
---|
1542 | {
|
---|
1543 | AssertPtrReturn(pszTool, NULL);
|
---|
1544 |
|
---|
1545 | /* Do a linear search, since we don't have that much stuff in the table. */
|
---|
1546 | for (unsigned i = 0; i < RT_ELEMENTS(g_aTools); i++)
|
---|
1547 | if (!strcmp(g_aTools[i].pszName, pszTool))
|
---|
1548 | return &g_aTools[i];
|
---|
1549 |
|
---|
1550 | return NULL;
|
---|
1551 | }
|
---|
1552 |
|
---|
1553 |
|
---|
1554 | /**
|
---|
1555 | * Converts a tool's exit code back to an IPRT error code.
|
---|
1556 | *
|
---|
1557 | * @return Converted IPRT status code.
|
---|
1558 | * @param pszTool Name of the toolbox tool to convert exit code for.
|
---|
1559 | * @param rcExit The tool's exit code to convert.
|
---|
1560 | */
|
---|
1561 | int VGSvcToolboxExitCodeConvertToRc(const char *pszTool, RTEXITCODE rcExit)
|
---|
1562 | {
|
---|
1563 | AssertPtrReturn(pszTool, VERR_INVALID_POINTER);
|
---|
1564 |
|
---|
1565 | PCVBOXSERVICETOOLBOXTOOL pTool = vgsvcToolboxLookUp(pszTool);
|
---|
1566 | if (pTool)
|
---|
1567 | return pTool->pfnExitCodeConvertToRc(rcExit);
|
---|
1568 |
|
---|
1569 | AssertMsgFailed(("Tool '%s' not found\n", pszTool));
|
---|
1570 | return VERR_GENERAL_FAILURE; /* Lookup failed, should not happen. */
|
---|
1571 | }
|
---|
1572 |
|
---|
1573 |
|
---|
1574 | /**
|
---|
1575 | * Entry point for internal toolbox.
|
---|
1576 | *
|
---|
1577 | * @return True if an internal tool was handled, false if not.
|
---|
1578 | * @param argc Number of arguments.
|
---|
1579 | * @param argv Pointer to argument array.
|
---|
1580 | * @param prcExit Where to store the exit code when an
|
---|
1581 | * internal toolbox command was handled.
|
---|
1582 | */
|
---|
1583 | bool VGSvcToolboxMain(int argc, char **argv, RTEXITCODE *prcExit)
|
---|
1584 | {
|
---|
1585 |
|
---|
1586 | /*
|
---|
1587 | * Check if the file named in argv[0] is one of the toolbox programs.
|
---|
1588 | */
|
---|
1589 | AssertReturn(argc > 0, false);
|
---|
1590 | const char *pszTool = RTPathFilename(argv[0]);
|
---|
1591 | PCVBOXSERVICETOOLBOXTOOL pTool = vgsvcToolboxLookUp(pszTool);
|
---|
1592 | if (!pTool)
|
---|
1593 | {
|
---|
1594 | /*
|
---|
1595 | * For debugging and testing purposes we also allow toolbox program access
|
---|
1596 | * when the first VBoxService argument is --use-toolbox.
|
---|
1597 | */
|
---|
1598 | if (argc < 3 || strcmp(argv[1], "--use-toolbox"))
|
---|
1599 | return false;
|
---|
1600 | argc -= 2;
|
---|
1601 | argv += 2;
|
---|
1602 | pszTool = argv[0];
|
---|
1603 | pTool = vgsvcToolboxLookUp(pszTool);
|
---|
1604 | if (!pTool)
|
---|
1605 | {
|
---|
1606 | *prcExit = RTEXITCODE_SUCCESS;
|
---|
1607 | if (!strcmp(pszTool, "-V"))
|
---|
1608 | {
|
---|
1609 | vgsvcToolboxShowVersion();
|
---|
1610 | return true;
|
---|
1611 | }
|
---|
1612 | if ( strcmp(pszTool, "help")
|
---|
1613 | && strcmp(pszTool, "--help")
|
---|
1614 | && strcmp(pszTool, "-h"))
|
---|
1615 | *prcExit = RTEXITCODE_SYNTAX;
|
---|
1616 | vgsvcToolboxShowUsage();
|
---|
1617 | return true;
|
---|
1618 | }
|
---|
1619 | }
|
---|
1620 |
|
---|
1621 | /*
|
---|
1622 | * Invoke the handler.
|
---|
1623 | */
|
---|
1624 | RTMsgSetProgName("VBoxService/%s", pszTool);
|
---|
1625 | AssertPtr(pTool);
|
---|
1626 | *prcExit = pTool->pfnHandler(argc, argv);
|
---|
1627 |
|
---|
1628 | return true;
|
---|
1629 | }
|
---|
1630 |
|
---|