1 | /* $Id: VBoxServiceToolBox.cpp 106061 2024-09-16 14:03:52Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxServiceToolbox - Internal (BusyBox-like) toolbox.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2012-2024 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 | rc2 = vgsvcToolboxPrintFsInfo(pszPath, strlen(pszPath), fOutputFlags, NULL, &IdCache, &objInfo);
|
---|
1001 | else if (RTFS_IS_DIRECTORY(objInfo.Attr.fMode))
|
---|
1002 | rc2 = vgsvcToolboxLsHandleDir(pszPath, fFlags, fOutputFlags, &IdCache);
|
---|
1003 | if (RT_SUCCESS(rc)) /* Keep initial failing rc. */
|
---|
1004 | rc = rc2;
|
---|
1005 | }
|
---|
1006 | else
|
---|
1007 | {
|
---|
1008 | if (!(fOutputFlags & VBOXSERVICETOOLBOXOUTPUTFLAG_PARSEABLE))
|
---|
1009 | RTMsgError("Cannot access '%s': No such file or directory\n", pszPath);
|
---|
1010 | if (RT_SUCCESS(rc))
|
---|
1011 | rc = VERR_FILE_NOT_FOUND;
|
---|
1012 | /* Do not break here -- process every element in the list
|
---|
1013 | * and keep failing rc. */
|
---|
1014 | }
|
---|
1015 |
|
---|
1016 | } while ((ch = RTGetOpt(&GetState, &ValueUnion)) != 0);
|
---|
1017 |
|
---|
1018 | if (fOutputFlags & VBOXSERVICETOOLBOXOUTPUTFLAG_PARSEABLE) /* Output termination. */
|
---|
1019 | vgsvcToolboxPrintStrmTermination();
|
---|
1020 |
|
---|
1021 | return RT_SUCCESS(rc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
|
---|
1022 | }
|
---|
1023 |
|
---|
1024 |
|
---|
1025 | /* Try using RTPathRmCmd. */
|
---|
1026 | static RTEXITCODE vgsvcToolboxRm(int argc, char **argv)
|
---|
1027 | {
|
---|
1028 | return RTPathRmCmd(argc, argv);
|
---|
1029 | }
|
---|
1030 |
|
---|
1031 |
|
---|
1032 | static char g_paszMkTempHelp[] =
|
---|
1033 | " VBoxService [--use-toolbox] vbox_mktemp [<general options>] [<options>]\n"
|
---|
1034 | " <template>\n\n"
|
---|
1035 | "Create a temporary directory based on the template supplied. The first string\n"
|
---|
1036 | "of consecutive 'X' characters in the template will be replaced to form a unique\n"
|
---|
1037 | "name for the directory. The template may not contain a path. The default\n"
|
---|
1038 | "creation mode is 0600 for files and 0700 for directories. If no path is\n"
|
---|
1039 | "specified the default temporary directory will be used.\n"
|
---|
1040 | "Options:\n\n"
|
---|
1041 | " [--directory|-d] Create a directory instead of a file.\n"
|
---|
1042 | " [--mode|-m <mode>] Create the object with mode <mode>.\n"
|
---|
1043 | " [--secure|-s] Fail if the object cannot be created securely.\n"
|
---|
1044 | " [--tmpdir|-t <path>] Create the object with the absolute path <path>.\n"
|
---|
1045 | "\n";
|
---|
1046 |
|
---|
1047 |
|
---|
1048 | /**
|
---|
1049 | * Report the result of a vbox_mktemp operation.
|
---|
1050 | *
|
---|
1051 | * Either errors to stderr (not machine-readable) or everything to stdout as
|
---|
1052 | * {name}\0{rc}\0 (machine- readable format). The message may optionally
|
---|
1053 | * contain a '%s' for the file name and an %Rrc for the result code in that
|
---|
1054 | * order. In future a "verbose" flag may be added, without which nothing will
|
---|
1055 | * be output in non-machine- readable mode. Sets prc if rc is a non-success
|
---|
1056 | * code.
|
---|
1057 | */
|
---|
1058 | static void toolboxMkTempReport(const char *pcszMessage, const char *pcszFile,
|
---|
1059 | bool fActive, int rc, uint32_t fOutputFlags, int *prc)
|
---|
1060 | {
|
---|
1061 | if (!fActive)
|
---|
1062 | return;
|
---|
1063 | if (!(fOutputFlags & VBOXSERVICETOOLBOXOUTPUTFLAG_PARSEABLE))
|
---|
1064 | if (RT_SUCCESS(rc))
|
---|
1065 | RTPrintf(pcszMessage, pcszFile, rc);
|
---|
1066 | else
|
---|
1067 | RTMsgError(pcszMessage, pcszFile, rc);
|
---|
1068 | else
|
---|
1069 | RTPrintf("name=%s%crc=%d%c", pcszFile, 0, rc, 0);
|
---|
1070 | if (prc && RT_FAILURE(rc))
|
---|
1071 | *prc = rc;
|
---|
1072 | }
|
---|
1073 |
|
---|
1074 |
|
---|
1075 | /**
|
---|
1076 | * Main function for tool "vbox_mktemp".
|
---|
1077 | *
|
---|
1078 | * @return RTEXITCODE.
|
---|
1079 | * @param argc Number of arguments.
|
---|
1080 | * @param argv Pointer to argument array.
|
---|
1081 | */
|
---|
1082 | static RTEXITCODE vgsvcToolboxMkTemp(int argc, char **argv)
|
---|
1083 | {
|
---|
1084 | static const RTGETOPTDEF s_aOptions[] =
|
---|
1085 | {
|
---|
1086 | { "--machinereadable", VBOXSERVICETOOLBOXOPT_MACHINE_READABLE,
|
---|
1087 | RTGETOPT_REQ_NOTHING },
|
---|
1088 | { "--directory", 'd', RTGETOPT_REQ_NOTHING },
|
---|
1089 | { "--mode", 'm', RTGETOPT_REQ_STRING },
|
---|
1090 | { "--secure", 's', RTGETOPT_REQ_NOTHING },
|
---|
1091 | { "--tmpdir", 't', RTGETOPT_REQ_STRING },
|
---|
1092 | };
|
---|
1093 |
|
---|
1094 | enum
|
---|
1095 | {
|
---|
1096 | /* Isn't that a bit long? s/VBOXSERVICETOOLBOX/VSTB/ ? */
|
---|
1097 | /** Create a temporary directory instead of a temporary file. */
|
---|
1098 | VBOXSERVICETOOLBOXMKTEMPFLAG_DIRECTORY = RT_BIT_32(0),
|
---|
1099 | /** Only create the temporary object if the operation is expected
|
---|
1100 | * to be secure. Not guaranteed to be supported on a particular
|
---|
1101 | * set-up. */
|
---|
1102 | VBOXSERVICETOOLBOXMKTEMPFLAG_SECURE = RT_BIT_32(1)
|
---|
1103 | };
|
---|
1104 |
|
---|
1105 | int ch, rc;
|
---|
1106 | RTGETOPTUNION ValueUnion;
|
---|
1107 | RTGETOPTSTATE GetState;
|
---|
1108 | rc = RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 1 /*iFirst*/, RTGETOPTINIT_FLAGS_OPTS_FIRST);
|
---|
1109 | AssertRCReturn(rc, RTEXITCODE_INIT);
|
---|
1110 |
|
---|
1111 | uint32_t fFlags = 0;
|
---|
1112 | uint32_t fOutputFlags = 0;
|
---|
1113 | int cNonOptions = 0;
|
---|
1114 | RTFMODE fMode = 0700;
|
---|
1115 | bool fModeSet = false;
|
---|
1116 | const char *pcszPath = NULL;
|
---|
1117 | const char *pcszTemplate;
|
---|
1118 | char szTemplateWithPath[RTPATH_MAX] = "";
|
---|
1119 |
|
---|
1120 | while ( (ch = RTGetOpt(&GetState, &ValueUnion))
|
---|
1121 | && RT_SUCCESS(rc))
|
---|
1122 | {
|
---|
1123 | /* For options that require an argument, ValueUnion has received the value. */
|
---|
1124 | switch (ch)
|
---|
1125 | {
|
---|
1126 | case 'h':
|
---|
1127 | vgsvcToolboxShowUsageHeader();
|
---|
1128 | RTPrintf("%s", g_paszMkTempHelp);
|
---|
1129 | return RTEXITCODE_SUCCESS;
|
---|
1130 |
|
---|
1131 | case 'V':
|
---|
1132 | vgsvcToolboxShowVersion();
|
---|
1133 | return RTEXITCODE_SUCCESS;
|
---|
1134 |
|
---|
1135 | case VBOXSERVICETOOLBOXOPT_MACHINE_READABLE:
|
---|
1136 | fOutputFlags |= VBOXSERVICETOOLBOXOUTPUTFLAG_PARSEABLE;
|
---|
1137 | break;
|
---|
1138 |
|
---|
1139 | case 'd':
|
---|
1140 | fFlags |= VBOXSERVICETOOLBOXMKTEMPFLAG_DIRECTORY;
|
---|
1141 | break;
|
---|
1142 |
|
---|
1143 | case 'm':
|
---|
1144 | rc = vgsvcToolboxParseMode(ValueUnion.psz, &fMode);
|
---|
1145 | if (RT_FAILURE(rc))
|
---|
1146 | return RTEXITCODE_SYNTAX;
|
---|
1147 | fModeSet = true;
|
---|
1148 | #ifndef RT_OS_WINDOWS
|
---|
1149 | umask(0); /* RTDirCreate workaround */
|
---|
1150 | #endif
|
---|
1151 | break;
|
---|
1152 | case 's':
|
---|
1153 | fFlags |= VBOXSERVICETOOLBOXMKTEMPFLAG_SECURE;
|
---|
1154 | break;
|
---|
1155 |
|
---|
1156 | case 't':
|
---|
1157 | pcszPath = ValueUnion.psz;
|
---|
1158 | break;
|
---|
1159 |
|
---|
1160 | case VINF_GETOPT_NOT_OPTION:
|
---|
1161 | /* RTGetOpt will sort these to the end of the argv vector so
|
---|
1162 | * that we will deal with them afterwards. */
|
---|
1163 | ++cNonOptions;
|
---|
1164 | break;
|
---|
1165 |
|
---|
1166 | default:
|
---|
1167 | return RTGetOptPrintError(ch, &ValueUnion);
|
---|
1168 | }
|
---|
1169 | }
|
---|
1170 |
|
---|
1171 | /* Print magic/version. */
|
---|
1172 | if (fOutputFlags & VBOXSERVICETOOLBOXOUTPUTFLAG_PARSEABLE)
|
---|
1173 | {
|
---|
1174 | rc = vgsvcToolboxStrmInit();
|
---|
1175 | if (RT_FAILURE(rc))
|
---|
1176 | RTMsgError("Error while initializing parseable streams, rc=%Rrc\n", rc);
|
---|
1177 | vgsvcToolboxPrintStrmHeader("vbt_mktemp", 1 /* Stream version */);
|
---|
1178 | }
|
---|
1179 |
|
---|
1180 | if (fFlags & VBOXSERVICETOOLBOXMKTEMPFLAG_SECURE && fModeSet)
|
---|
1181 | {
|
---|
1182 | toolboxMkTempReport("'-s' and '-m' parameters cannot be used together.\n", "",
|
---|
1183 | true, VERR_INVALID_PARAMETER, fOutputFlags, &rc);
|
---|
1184 | return RTEXITCODE_SYNTAX;
|
---|
1185 | }
|
---|
1186 |
|
---|
1187 | /* We need exactly one template, containing at least one 'X'. */
|
---|
1188 | if (cNonOptions != 1)
|
---|
1189 | {
|
---|
1190 | toolboxMkTempReport("Please specify exactly one template.\n", "", true, VERR_INVALID_PARAMETER, fOutputFlags, &rc);
|
---|
1191 | return RTEXITCODE_SYNTAX;
|
---|
1192 | }
|
---|
1193 | pcszTemplate = argv[argc - 1];
|
---|
1194 |
|
---|
1195 | /* Validate that the template is as IPRT requires (asserted by IPRT). */
|
---|
1196 | if ( RTPathHasPath(pcszTemplate)
|
---|
1197 | || ( !strstr(pcszTemplate, "XXX")
|
---|
1198 | && pcszTemplate[strlen(pcszTemplate) - 1] != 'X'))
|
---|
1199 | {
|
---|
1200 | toolboxMkTempReport("Template '%s' should contain a file name with no path and at least three consecutive 'X' characters or ending in 'X'.\n",
|
---|
1201 | pcszTemplate, true, VERR_INVALID_PARAMETER, fOutputFlags, &rc);
|
---|
1202 | return RTEXITCODE_FAILURE;
|
---|
1203 | }
|
---|
1204 | if (pcszPath && !RTPathStartsWithRoot(pcszPath))
|
---|
1205 | {
|
---|
1206 | toolboxMkTempReport("Path '%s' should be absolute.\n", pcszPath, true, VERR_INVALID_PARAMETER, fOutputFlags, &rc);
|
---|
1207 | return RTEXITCODE_FAILURE;
|
---|
1208 | }
|
---|
1209 | if (pcszPath)
|
---|
1210 | {
|
---|
1211 | rc = RTStrCopy(szTemplateWithPath, sizeof(szTemplateWithPath), pcszPath);
|
---|
1212 | if (RT_FAILURE(rc))
|
---|
1213 | {
|
---|
1214 | toolboxMkTempReport("Path '%s' too long.\n", pcszPath, true, VERR_INVALID_PARAMETER, fOutputFlags, &rc);
|
---|
1215 | return RTEXITCODE_FAILURE;
|
---|
1216 | }
|
---|
1217 | }
|
---|
1218 | else
|
---|
1219 | {
|
---|
1220 | rc = RTPathTemp(szTemplateWithPath, sizeof(szTemplateWithPath));
|
---|
1221 | if (RT_FAILURE(rc))
|
---|
1222 | {
|
---|
1223 | toolboxMkTempReport("Failed to get the temporary directory.\n", "", true, VERR_INVALID_PARAMETER, fOutputFlags, &rc);
|
---|
1224 | return RTEXITCODE_FAILURE;
|
---|
1225 | }
|
---|
1226 | }
|
---|
1227 | rc = RTPathAppend(szTemplateWithPath, sizeof(szTemplateWithPath), pcszTemplate);
|
---|
1228 | if (RT_FAILURE(rc))
|
---|
1229 | {
|
---|
1230 | toolboxMkTempReport("Template '%s' too long for path.\n", pcszTemplate, true, VERR_INVALID_PARAMETER, fOutputFlags, &rc);
|
---|
1231 | return RTEXITCODE_FAILURE;
|
---|
1232 | }
|
---|
1233 |
|
---|
1234 | if (fFlags & VBOXSERVICETOOLBOXMKTEMPFLAG_DIRECTORY)
|
---|
1235 | {
|
---|
1236 | rc = fFlags & VBOXSERVICETOOLBOXMKTEMPFLAG_SECURE
|
---|
1237 | ? RTDirCreateTempSecure(szTemplateWithPath)
|
---|
1238 | : RTDirCreateTemp(szTemplateWithPath, fMode);
|
---|
1239 | toolboxMkTempReport("Created temporary directory '%s'.\n",
|
---|
1240 | szTemplateWithPath, RT_SUCCESS(rc), rc,
|
---|
1241 | fOutputFlags, NULL);
|
---|
1242 | /* RTDirCreateTemp[Secure] sets the template to "" on failure. */
|
---|
1243 | toolboxMkTempReport("The following error occurred while creating a temporary directory from template '%s': %Rrc.\n",
|
---|
1244 | pcszTemplate, RT_FAILURE(rc), rc, fOutputFlags, NULL /*prc*/);
|
---|
1245 | }
|
---|
1246 | else
|
---|
1247 | {
|
---|
1248 | rc = fFlags & VBOXSERVICETOOLBOXMKTEMPFLAG_SECURE
|
---|
1249 | ? RTFileCreateTempSecure(szTemplateWithPath)
|
---|
1250 | : RTFileCreateTemp(szTemplateWithPath, fMode);
|
---|
1251 | toolboxMkTempReport("Created temporary file '%s'.\n",
|
---|
1252 | szTemplateWithPath, RT_SUCCESS(rc), rc,
|
---|
1253 | fOutputFlags, NULL);
|
---|
1254 | /* RTFileCreateTemp[Secure] sets the template to "" on failure. */
|
---|
1255 | toolboxMkTempReport("The following error occurred while creating a temporary file from template '%s': %Rrc.\n",
|
---|
1256 | pcszTemplate, RT_FAILURE(rc), rc, fOutputFlags, NULL /*prc*/);
|
---|
1257 | }
|
---|
1258 | if (fOutputFlags & VBOXSERVICETOOLBOXOUTPUTFLAG_PARSEABLE) /* Output termination. */
|
---|
1259 | vgsvcToolboxPrintStrmTermination();
|
---|
1260 | return RT_SUCCESS(rc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
|
---|
1261 | }
|
---|
1262 |
|
---|
1263 |
|
---|
1264 | /** @todo Document options! */
|
---|
1265 | static char g_paszMkDirHelp[] =
|
---|
1266 | " VBoxService [--use-toolbox] vbox_mkdir [<general options>] [<options>]\n"
|
---|
1267 | " <directory>...\n\n"
|
---|
1268 | "Options:\n\n"
|
---|
1269 | " [--mode|-m <mode>] The file mode to set (chmod) on the created\n"
|
---|
1270 | " directories. Default: a=rwx & umask.\n"
|
---|
1271 | " [--parents|-p] Create parent directories as needed, no\n"
|
---|
1272 | " error if the directory already exists.\n"
|
---|
1273 | " [--verbose|-v] Display a message for each created directory.\n"
|
---|
1274 | "\n";
|
---|
1275 |
|
---|
1276 |
|
---|
1277 | /**
|
---|
1278 | * Main function for tool "vbox_mkdir".
|
---|
1279 | *
|
---|
1280 | * @return RTEXITCODE.
|
---|
1281 | * @param argc Number of arguments.
|
---|
1282 | * @param argv Pointer to argument array.
|
---|
1283 | */
|
---|
1284 | static RTEXITCODE vgsvcToolboxMkDir(int argc, char **argv)
|
---|
1285 | {
|
---|
1286 | static const RTGETOPTDEF s_aOptions[] =
|
---|
1287 | {
|
---|
1288 | { "--mode", 'm', RTGETOPT_REQ_STRING },
|
---|
1289 | { "--parents", 'p', RTGETOPT_REQ_NOTHING},
|
---|
1290 | { "--verbose", 'v', RTGETOPT_REQ_NOTHING}
|
---|
1291 | };
|
---|
1292 |
|
---|
1293 | int ch;
|
---|
1294 | RTGETOPTUNION ValueUnion;
|
---|
1295 | RTGETOPTSTATE GetState;
|
---|
1296 | int rc = RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions),
|
---|
1297 | 1 /*iFirst*/, RTGETOPTINIT_FLAGS_OPTS_FIRST);
|
---|
1298 | AssertRCReturn(rc, RTEXITCODE_INIT);
|
---|
1299 |
|
---|
1300 | bool fMakeParentDirs = false;
|
---|
1301 | bool fVerbose = false;
|
---|
1302 | RTFMODE fDirMode = RTFS_UNIX_IRWXU | RTFS_UNIX_IRWXG | RTFS_UNIX_IRWXO;
|
---|
1303 | int cDirsCreated = 0;
|
---|
1304 |
|
---|
1305 | while ((ch = RTGetOpt(&GetState, &ValueUnion)))
|
---|
1306 | {
|
---|
1307 | /* For options that require an argument, ValueUnion has received the value. */
|
---|
1308 | switch (ch)
|
---|
1309 | {
|
---|
1310 | case 'p':
|
---|
1311 | fMakeParentDirs = true;
|
---|
1312 | break;
|
---|
1313 |
|
---|
1314 | case 'm':
|
---|
1315 | rc = vgsvcToolboxParseMode(ValueUnion.psz, &fDirMode);
|
---|
1316 | if (RT_FAILURE(rc))
|
---|
1317 | return RTEXITCODE_SYNTAX;
|
---|
1318 | #ifndef RT_OS_WINDOWS
|
---|
1319 | umask(0); /* RTDirCreate workaround */
|
---|
1320 | #endif
|
---|
1321 | break;
|
---|
1322 |
|
---|
1323 | case 'v':
|
---|
1324 | fVerbose = true;
|
---|
1325 | break;
|
---|
1326 |
|
---|
1327 | case 'h':
|
---|
1328 | vgsvcToolboxShowUsageHeader();
|
---|
1329 | RTPrintf("%s", g_paszMkDirHelp);
|
---|
1330 | return RTEXITCODE_SUCCESS;
|
---|
1331 |
|
---|
1332 | case 'V':
|
---|
1333 | vgsvcToolboxShowVersion();
|
---|
1334 | return RTEXITCODE_SUCCESS;
|
---|
1335 |
|
---|
1336 | case VINF_GETOPT_NOT_OPTION:
|
---|
1337 | if (fMakeParentDirs)
|
---|
1338 | /** @todo r=bird: If fVerbose is set, we should also show
|
---|
1339 | * which directories that get created, parents as well as
|
---|
1340 | * omitting existing final dirs. Annoying, but check any
|
---|
1341 | * mkdir implementation (try "mkdir -pv asdf/1/2/3/4"
|
---|
1342 | * twice). */
|
---|
1343 | rc = RTDirCreateFullPath(ValueUnion.psz, fDirMode);
|
---|
1344 | else
|
---|
1345 | rc = RTDirCreate(ValueUnion.psz, fDirMode, 0);
|
---|
1346 | if (RT_FAILURE(rc))
|
---|
1347 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "Could not create directory '%s': %Rra\n",
|
---|
1348 | ValueUnion.psz, rc);
|
---|
1349 | if (fVerbose)
|
---|
1350 | RTMsgInfo("Created directory '%s', mode %#RTfmode\n", ValueUnion.psz, fDirMode);
|
---|
1351 | cDirsCreated++;
|
---|
1352 | break;
|
---|
1353 |
|
---|
1354 | default:
|
---|
1355 | return RTGetOptPrintError(ch, &ValueUnion);
|
---|
1356 | }
|
---|
1357 | }
|
---|
1358 | AssertRC(rc);
|
---|
1359 |
|
---|
1360 | if (cDirsCreated == 0)
|
---|
1361 | return RTMsgErrorExit(RTEXITCODE_SYNTAX, "No directory argument.");
|
---|
1362 |
|
---|
1363 | return RTEXITCODE_SUCCESS;
|
---|
1364 | }
|
---|
1365 |
|
---|
1366 |
|
---|
1367 | /** @todo Document options! */
|
---|
1368 | static char g_paszStatHelp[] =
|
---|
1369 | " VBoxService [--use-toolbox] vbox_stat [<general options>] [<options>]\n"
|
---|
1370 | " <file>...\n\n"
|
---|
1371 | "Display file or file system status.\n\n"
|
---|
1372 | "Options:\n\n"
|
---|
1373 | " [--file-system|-f]\n"
|
---|
1374 | " [--dereference|-L]\n"
|
---|
1375 | " [--terse|-t]\n"
|
---|
1376 | " [--verbose|-v]\n"
|
---|
1377 | "\n";
|
---|
1378 |
|
---|
1379 |
|
---|
1380 | /**
|
---|
1381 | * Main function for tool "vbox_stat".
|
---|
1382 | *
|
---|
1383 | * @return RTEXITCODE.
|
---|
1384 | * @param argc Number of arguments.
|
---|
1385 | * @param argv Pointer to argument array.
|
---|
1386 | */
|
---|
1387 | static RTEXITCODE vgsvcToolboxStat(int argc, char **argv)
|
---|
1388 | {
|
---|
1389 | static const RTGETOPTDEF s_aOptions[] =
|
---|
1390 | {
|
---|
1391 | { "--file-system", 'f', RTGETOPT_REQ_NOTHING },
|
---|
1392 | { "--dereference", 'L', RTGETOPT_REQ_NOTHING },
|
---|
1393 | { "--machinereadable", VBOXSERVICETOOLBOXOPT_MACHINE_READABLE, RTGETOPT_REQ_NOTHING },
|
---|
1394 | { "--terse", 't', RTGETOPT_REQ_NOTHING },
|
---|
1395 | { "--verbose", 'v', RTGETOPT_REQ_NOTHING }
|
---|
1396 | };
|
---|
1397 |
|
---|
1398 | int ch;
|
---|
1399 | RTGETOPTUNION ValueUnion;
|
---|
1400 | RTGETOPTSTATE GetState;
|
---|
1401 | RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 1 /*iFirst*/, RTGETOPTINIT_FLAGS_OPTS_FIRST);
|
---|
1402 |
|
---|
1403 | int rc = VINF_SUCCESS;
|
---|
1404 | uint32_t fOutputFlags = VBOXSERVICETOOLBOXOUTPUTFLAG_LONG; /* Use long mode by default. */
|
---|
1405 | uint32_t fQueryInfoFlags = RTPATH_F_ON_LINK;
|
---|
1406 |
|
---|
1407 | while ( (ch = RTGetOpt(&GetState, &ValueUnion))
|
---|
1408 | && RT_SUCCESS(rc))
|
---|
1409 | {
|
---|
1410 | /* For options that require an argument, ValueUnion has received the value. */
|
---|
1411 | switch (ch)
|
---|
1412 | {
|
---|
1413 | case 'f':
|
---|
1414 | RTMsgError("Sorry, option '%s' is not implemented yet!\n", ValueUnion.pDef->pszLong);
|
---|
1415 | rc = VERR_INVALID_PARAMETER;
|
---|
1416 | break;
|
---|
1417 |
|
---|
1418 | case 'L':
|
---|
1419 | fQueryInfoFlags &= ~RTPATH_F_ON_LINK;
|
---|
1420 | fQueryInfoFlags |= RTPATH_F_FOLLOW_LINK;
|
---|
1421 | break;
|
---|
1422 |
|
---|
1423 | case VBOXSERVICETOOLBOXOPT_MACHINE_READABLE:
|
---|
1424 | fOutputFlags |= VBOXSERVICETOOLBOXOUTPUTFLAG_PARSEABLE;
|
---|
1425 | break;
|
---|
1426 |
|
---|
1427 | case 'h':
|
---|
1428 | vgsvcToolboxShowUsageHeader();
|
---|
1429 | RTPrintf("%s", g_paszStatHelp);
|
---|
1430 | return RTEXITCODE_SUCCESS;
|
---|
1431 |
|
---|
1432 | case 'V':
|
---|
1433 | vgsvcToolboxShowVersion();
|
---|
1434 | return RTEXITCODE_SUCCESS;
|
---|
1435 |
|
---|
1436 | case VINF_GETOPT_NOT_OPTION:
|
---|
1437 | {
|
---|
1438 | Assert(GetState.iNext);
|
---|
1439 | GetState.iNext--;
|
---|
1440 | break;
|
---|
1441 | }
|
---|
1442 |
|
---|
1443 | default:
|
---|
1444 | return RTGetOptPrintError(ch, &ValueUnion);
|
---|
1445 | }
|
---|
1446 |
|
---|
1447 | /* All flags / options processed? Bail out here.
|
---|
1448 | * Processing the file / directory list comes down below. */
|
---|
1449 | if (ch == VINF_GETOPT_NOT_OPTION)
|
---|
1450 | break;
|
---|
1451 | }
|
---|
1452 |
|
---|
1453 | if (RT_SUCCESS(rc))
|
---|
1454 | {
|
---|
1455 | if (fOutputFlags & VBOXSERVICETOOLBOXOUTPUTFLAG_PARSEABLE) /* Output termination. */
|
---|
1456 | {
|
---|
1457 | rc = vgsvcToolboxStrmInit();
|
---|
1458 | if (RT_FAILURE(rc))
|
---|
1459 | RTMsgError("Error while initializing parseable streams, rc=%Rrc\n", rc);
|
---|
1460 | vgsvcToolboxPrintStrmHeader("vbt_stat", 1 /* Stream version */);
|
---|
1461 | }
|
---|
1462 |
|
---|
1463 | VGSVCIDCACHE IdCache;
|
---|
1464 | RT_ZERO(IdCache);
|
---|
1465 |
|
---|
1466 | while ((ch = RTGetOpt(&GetState, &ValueUnion)))
|
---|
1467 | {
|
---|
1468 | RTFSOBJINFO objInfo;
|
---|
1469 | int rc2 = RTPathQueryInfoEx(ValueUnion.psz, &objInfo, RTFSOBJATTRADD_UNIX, fQueryInfoFlags);
|
---|
1470 | if (RT_FAILURE(rc2))
|
---|
1471 | {
|
---|
1472 | if (!(fOutputFlags & VBOXSERVICETOOLBOXOUTPUTFLAG_PARSEABLE))
|
---|
1473 | RTMsgError("Cannot stat for '%s': %Rrc\n", ValueUnion.psz, rc2);
|
---|
1474 | }
|
---|
1475 | else
|
---|
1476 | rc2 = vgsvcToolboxPrintFsInfo(ValueUnion.psz, strlen(ValueUnion.psz), fOutputFlags, NULL, &IdCache, &objInfo);
|
---|
1477 |
|
---|
1478 | if (RT_SUCCESS(rc))
|
---|
1479 | rc = rc2;
|
---|
1480 | /* Do not break here -- process every element in the list
|
---|
1481 | * and keep (initial) failing rc. */
|
---|
1482 | }
|
---|
1483 |
|
---|
1484 | if (fOutputFlags & VBOXSERVICETOOLBOXOUTPUTFLAG_PARSEABLE) /* Output termination. */
|
---|
1485 | vgsvcToolboxPrintStrmTermination();
|
---|
1486 |
|
---|
1487 | /* At this point the overall result (success/failure) should be in rc. */
|
---|
1488 | }
|
---|
1489 | else
|
---|
1490 | RTMsgError("Failed with rc=%Rrc\n", rc);
|
---|
1491 |
|
---|
1492 | if (RT_FAILURE(rc))
|
---|
1493 | {
|
---|
1494 | switch (rc)
|
---|
1495 | {
|
---|
1496 | case VERR_ACCESS_DENIED:
|
---|
1497 | return (RTEXITCODE)VBOXSERVICETOOLBOX_STAT_EXITCODE_ACCESS_DENIED;
|
---|
1498 |
|
---|
1499 | case VERR_FILE_NOT_FOUND:
|
---|
1500 | return (RTEXITCODE)VBOXSERVICETOOLBOX_STAT_EXITCODE_FILE_NOT_FOUND;
|
---|
1501 |
|
---|
1502 | case VERR_PATH_NOT_FOUND:
|
---|
1503 | return (RTEXITCODE)VBOXSERVICETOOLBOX_STAT_EXITCODE_PATH_NOT_FOUND;
|
---|
1504 |
|
---|
1505 | case VERR_NET_PATH_NOT_FOUND:
|
---|
1506 | return (RTEXITCODE)VBOXSERVICETOOLBOX_STAT_EXITCODE_NET_PATH_NOT_FOUND;
|
---|
1507 |
|
---|
1508 | case VERR_INVALID_NAME:
|
---|
1509 | return (RTEXITCODE)VBOXSERVICETOOLBOX_STAT_EXITCODE_INVALID_NAME;
|
---|
1510 |
|
---|
1511 | default:
|
---|
1512 | #ifdef DEBUG_andy
|
---|
1513 | AssertMsgFailed(("Exit code for %Rrc not implemented\n", rc));
|
---|
1514 | #endif
|
---|
1515 | break;
|
---|
1516 | }
|
---|
1517 |
|
---|
1518 | return RTEXITCODE_FAILURE;
|
---|
1519 | }
|
---|
1520 |
|
---|
1521 | return RTEXITCODE_SUCCESS;
|
---|
1522 | }
|
---|
1523 |
|
---|
1524 |
|
---|
1525 | /**
|
---|
1526 | * Looks up the tool definition entry for the tool give by @a pszTool.
|
---|
1527 | *
|
---|
1528 | * @returns Pointer to the tool definition. NULL if not found.
|
---|
1529 | * @param pszTool The name of the tool.
|
---|
1530 | */
|
---|
1531 | static PCVBOXSERVICETOOLBOXTOOL vgsvcToolboxLookUp(const char *pszTool)
|
---|
1532 | {
|
---|
1533 | AssertPtrReturn(pszTool, NULL);
|
---|
1534 |
|
---|
1535 | /* Do a linear search, since we don't have that much stuff in the table. */
|
---|
1536 | for (unsigned i = 0; i < RT_ELEMENTS(g_aTools); i++)
|
---|
1537 | if (!strcmp(g_aTools[i].pszName, pszTool))
|
---|
1538 | return &g_aTools[i];
|
---|
1539 |
|
---|
1540 | return NULL;
|
---|
1541 | }
|
---|
1542 |
|
---|
1543 |
|
---|
1544 | /**
|
---|
1545 | * Converts a tool's exit code back to an IPRT error code.
|
---|
1546 | *
|
---|
1547 | * @return Converted IPRT status code.
|
---|
1548 | * @param pszTool Name of the toolbox tool to convert exit code for.
|
---|
1549 | * @param rcExit The tool's exit code to convert.
|
---|
1550 | */
|
---|
1551 | int VGSvcToolboxExitCodeConvertToRc(const char *pszTool, RTEXITCODE rcExit)
|
---|
1552 | {
|
---|
1553 | AssertPtrReturn(pszTool, VERR_INVALID_POINTER);
|
---|
1554 |
|
---|
1555 | PCVBOXSERVICETOOLBOXTOOL pTool = vgsvcToolboxLookUp(pszTool);
|
---|
1556 | if (pTool)
|
---|
1557 | return pTool->pfnExitCodeConvertToRc(rcExit);
|
---|
1558 |
|
---|
1559 | AssertMsgFailed(("Tool '%s' not found\n", pszTool));
|
---|
1560 | return VERR_GENERAL_FAILURE; /* Lookup failed, should not happen. */
|
---|
1561 | }
|
---|
1562 |
|
---|
1563 |
|
---|
1564 | /**
|
---|
1565 | * Entry point for internal toolbox.
|
---|
1566 | *
|
---|
1567 | * @return True if an internal tool was handled, false if not.
|
---|
1568 | * @param argc Number of arguments.
|
---|
1569 | * @param argv Pointer to argument array.
|
---|
1570 | * @param prcExit Where to store the exit code when an
|
---|
1571 | * internal toolbox command was handled.
|
---|
1572 | */
|
---|
1573 | bool VGSvcToolboxMain(int argc, char **argv, RTEXITCODE *prcExit)
|
---|
1574 | {
|
---|
1575 |
|
---|
1576 | /*
|
---|
1577 | * Check if the file named in argv[0] is one of the toolbox programs.
|
---|
1578 | */
|
---|
1579 | AssertReturn(argc > 0, false);
|
---|
1580 | const char *pszTool = RTPathFilename(argv[0]);
|
---|
1581 | PCVBOXSERVICETOOLBOXTOOL pTool = vgsvcToolboxLookUp(pszTool);
|
---|
1582 | if (!pTool)
|
---|
1583 | {
|
---|
1584 | /*
|
---|
1585 | * For debugging and testing purposes we also allow toolbox program access
|
---|
1586 | * when the first VBoxService argument is --use-toolbox.
|
---|
1587 | */
|
---|
1588 | if (argc < 2 || strcmp(argv[1], "--use-toolbox"))
|
---|
1589 | {
|
---|
1590 | /* We must match vgsvcGstCtrlProcessCreateProcess here and claim
|
---|
1591 | everything starting with "vbox_". */
|
---|
1592 | if (!RTStrStartsWith(pszTool, "vbox_"))
|
---|
1593 | return false;
|
---|
1594 | RTMsgError("Unknown tool: %s\n", pszTool);
|
---|
1595 | *prcExit = RTEXITCODE_SYNTAX;
|
---|
1596 | return true;
|
---|
1597 | }
|
---|
1598 |
|
---|
1599 | /* No tool specified? Show toolbox help. */
|
---|
1600 | if (argc < 3)
|
---|
1601 | {
|
---|
1602 | RTMsgError("No tool following --use-toolbox\n");
|
---|
1603 | *prcExit = RTEXITCODE_SYNTAX;
|
---|
1604 | return true;
|
---|
1605 | }
|
---|
1606 |
|
---|
1607 | argc -= 2;
|
---|
1608 | argv += 2;
|
---|
1609 | pszTool = argv[0];
|
---|
1610 | pTool = vgsvcToolboxLookUp(pszTool);
|
---|
1611 | if (!pTool)
|
---|
1612 | {
|
---|
1613 | *prcExit = RTEXITCODE_SUCCESS;
|
---|
1614 | if ( !strcmp(pszTool, "-V")
|
---|
1615 | || !strcmp(pszTool, "version"))
|
---|
1616 | vgsvcToolboxShowVersion();
|
---|
1617 | else if ( !strcmp(pszTool, "help")
|
---|
1618 | || !strcmp(pszTool, "--help")
|
---|
1619 | || !strcmp(pszTool, "-h"))
|
---|
1620 | vgsvcToolboxShowUsage();
|
---|
1621 | else
|
---|
1622 | {
|
---|
1623 | RTMsgError("Unknown tool: %s\n", pszTool);
|
---|
1624 | *prcExit = RTEXITCODE_SYNTAX;
|
---|
1625 | }
|
---|
1626 | return true;
|
---|
1627 | }
|
---|
1628 | }
|
---|
1629 |
|
---|
1630 | /*
|
---|
1631 | * Invoke the handler.
|
---|
1632 | */
|
---|
1633 | RTMsgSetProgName("VBoxService/%s", pszTool);
|
---|
1634 | AssertPtr(pTool);
|
---|
1635 | *prcExit = pTool->pfnHandler(argc, argv);
|
---|
1636 |
|
---|
1637 | return true;
|
---|
1638 | }
|
---|
1639 |
|
---|