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