VirtualBox

source: vbox/trunk/src/VBox/Runtime/tools/RTChMod.cpp@ 103914

Last change on this file since 103914 was 98103, checked in by vboxsync, 22 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 15.8 KB
Line 
1/* $Id: RTChMod.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * IPRT - Changes the mode/attributes of a file system object.
4 */
5
6/*
7 * Copyright (C) 2013-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 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#include <iprt/buildconfig.h>
42#include <iprt/errcore.h>
43#include <iprt/file.h>
44#include <iprt/getopt.h>
45#include <iprt/initterm.h>
46#include <iprt/message.h>
47#include <iprt/path.h>
48#include <iprt/stream.h>
49#include <iprt/string.h>
50#include <iprt/vfs.h>
51
52
53/*********************************************************************************************************************************
54* Defined Constants And Macros *
55*********************************************************************************************************************************/
56/** What to clear we all bits are being set. */
57#define RTCHMOD_SET_ALL_MASK (~( RTFS_TYPE_MASK \
58 | RTFS_DOS_NT_ENCRYPTED \
59 | RTFS_DOS_NT_COMPRESSED \
60 | RTFS_DOS_NT_REPARSE_POINT \
61 | RTFS_DOS_NT_SPARSE_FILE \
62 | RTFS_DOS_NT_DEVICE \
63 | RTFS_DOS_DIRECTORY))
64
65
66/*********************************************************************************************************************************
67* Structures and Typedefs *
68*********************************************************************************************************************************/
69typedef enum RTCMDCHMODNOISE
70{
71 kRTCmdChModNoise_Quiet,
72 kRTCmdChModNoise_Default,
73 kRTCmdChModNoise_Changes,
74 kRTCmdChModNoise_Verbose
75} RTCMDCHMODNOISE;
76
77
78typedef struct RTCMDCHMODOPTS
79{
80 /** The noise level. */
81 RTCMDCHMODNOISE enmNoiseLevel;
82 /** -R, --recursive */
83 bool fRecursive;
84 /** --preserve-root / --no-preserve-root (don't allow recursion from root). */
85 bool fPreserveRoot;
86 /** Whether to always use the VFS chain API (for testing). */
87 bool fAlwaysUseChainApi;
88 /** Which mode bits to set. */
89 RTFMODE fModeSet;
90 /** Which mode bits to clear. */
91 RTFMODE fModeClear;
92} RTCMDCHMODOPTS;
93
94
95
96/**
97 * Calculates the new file mode.
98 *
99 * @returns New mode mask.
100 * @param pOpts The chmod options.
101 * @param fMode The current file mode.
102 */
103static RTFMODE rtCmdMkModCalcNewMode(RTCMDCHMODOPTS const *pOpts, RTFMODE fMode)
104{
105 fMode &= ~pOpts->fModeClear;
106 fMode |= pOpts->fModeSet;
107 /** @todo do 'X' */
108 return fMode;
109}
110
111
112/**
113 * Changes the file mode of one file system object.
114 *
115 * @returns exit code
116 * @param pOpts The chmod options.
117 * @param pszPath The path to the file system object to change the
118 * file mode of.
119 */
120static RTEXITCODE rtCmdChModOne(RTCMDCHMODOPTS const *pOpts, const char *pszPath)
121{
122 int rc;
123 RTFSOBJINFO ObjInfo;
124 bool fChanges = false;
125 if (!pOpts->fAlwaysUseChainApi && !RTVfsChainIsSpec(pszPath) )
126 {
127 rc = RTPathQueryInfoEx(pszPath, &ObjInfo, RTFSOBJATTRADD_NOTHING, RTPATH_F_FOLLOW_LINK);
128 if (RT_SUCCESS(rc))
129 {
130 RTFMODE fNewMode = rtCmdMkModCalcNewMode(pOpts, ObjInfo.Attr.fMode);
131 fChanges = fNewMode != ObjInfo.Attr.fMode;
132 if (fChanges)
133 {
134 rc = RTPathSetMode(pszPath, fNewMode);
135 if (RT_FAILURE(rc))
136 RTMsgError("RTPathSetMode failed on '%s' with fNewMode=%#x: %Rrc", pszPath, fNewMode, rc);
137 }
138 }
139 else
140 RTMsgError("RTPathQueryInfoEx failed on '%s': %Rrc", pszPath, rc);
141 }
142 else
143 {
144 RTVFSOBJ hVfsObj;
145 uint32_t offError;
146 RTERRINFOSTATIC ErrInfo;
147 rc = RTVfsChainOpenObj(pszPath, RTFILE_O_ACCESS_ATTR_READWRITE | RTFILE_O_DENY_NONE | RTFILE_O_OPEN,
148 RTVFSOBJ_F_OPEN_ANY | RTVFSOBJ_F_CREATE_NOTHING | RTPATH_F_FOLLOW_LINK,
149 &hVfsObj, &offError, RTErrInfoInitStatic(&ErrInfo));
150 if (RT_SUCCESS(rc))
151 {
152 rc = RTVfsObjQueryInfo(hVfsObj, &ObjInfo, RTFSOBJATTRADD_NOTHING);
153 if (RT_SUCCESS(rc))
154 {
155 RTFMODE fNewMode = rtCmdMkModCalcNewMode(pOpts, ObjInfo.Attr.fMode);
156 fChanges = fNewMode != ObjInfo.Attr.fMode;
157 if (fChanges)
158 {
159 rc = RTVfsObjSetMode(hVfsObj, fNewMode, RTCHMOD_SET_ALL_MASK);
160 if (RT_FAILURE(rc))
161 RTMsgError("RTVfsObjSetMode failed on '%s' with fNewMode=%#x: %Rrc", pszPath, fNewMode, rc);
162 }
163 }
164 else
165 RTVfsChainMsgError("RTVfsObjQueryInfo", pszPath, rc, offError, &ErrInfo.Core);
166 RTVfsObjRelease(hVfsObj);
167 }
168 else
169 RTVfsChainMsgError("RTVfsChainOpenObject", pszPath, rc, offError, &ErrInfo.Core);
170 }
171
172 if (RT_SUCCESS(rc))
173 {
174 if (pOpts->enmNoiseLevel >= (fChanges ? kRTCmdChModNoise_Changes : kRTCmdChModNoise_Verbose))
175 RTPrintf("%s\n", pszPath);
176 return RTEXITCODE_SUCCESS;
177 }
178 return RTEXITCODE_FAILURE;
179}
180
181
182/**
183 * Recursively changes the file mode.
184 *
185 * @returns exit code
186 * @param pOpts The mkdir option.
187 * @param pszPath The path to start changing the mode of.
188 */
189static int rtCmdChModRecursive(RTCMDCHMODOPTS const *pOpts, const char *pszPath)
190{
191 /*
192 * Check if it's a directory first. If not, join the non-recursive code.
193 */
194 int rc;
195 uint32_t offError;
196 RTFSOBJINFO ObjInfo;
197 RTERRINFOSTATIC ErrInfo;
198 bool const fUseChainApi = pOpts->fAlwaysUseChainApi || RTVfsChainIsSpec(pszPath);
199 if (!fUseChainApi)
200 {
201 rc = RTPathQueryInfoEx(pszPath, &ObjInfo, RTFSOBJATTRADD_NOTHING, RTPATH_F_FOLLOW_LINK);
202 if (RT_FAILURE(rc))
203 return RTMsgErrorExitFailure("RTPathQueryInfoEx failed on '%s': %Rrc", pszPath, rc);
204 }
205 else
206 {
207 rc = RTVfsChainQueryInfo(pszPath, &ObjInfo, RTFSOBJATTRADD_NOTHING, RTPATH_F_FOLLOW_LINK,
208 &offError, RTErrInfoInitStatic(&ErrInfo));
209 if (RT_FAILURE(rc))
210 return RTVfsChainMsgErrorExitFailure("RTVfsChainQueryInfo", pszPath, rc, offError, &ErrInfo.Core);
211 }
212
213 if (!RTFS_IS_DIRECTORY(ObjInfo.Attr.fMode))
214 {
215 /*
216 * Don't bother redoing the above work if its not necessary.
217 */
218 RTFMODE fNewMode = rtCmdMkModCalcNewMode(pOpts, ObjInfo.Attr.fMode);
219 if (fNewMode != ObjInfo.Attr.fMode)
220 return rtCmdChModOne(pOpts, pszPath);
221 if (pOpts->enmNoiseLevel >= kRTCmdChModNoise_Verbose)
222 RTPrintf("%s\n", pszPath);
223 return RTEXITCODE_SUCCESS;
224 }
225
226 /*
227 * For recursion we always use the VFS layer.
228 */
229 RTVFSDIR hVfsDir;
230 if (!fUseChainApi)
231 {
232 rc = RTVfsDirOpenNormal(pszPath, 0 /** @todo write attrib flag*/, &hVfsDir);
233 if (RT_FAILURE(rc))
234 return RTMsgErrorExitFailure("RTVfsDirOpenNormal failed on '%s': %Rrc", pszPath, rc);
235 }
236 else
237 {
238 rc = RTVfsChainOpenDir(pszPath, 0 /** @todo write attrib flag*/, &hVfsDir, &offError, RTErrInfoInitStatic(&ErrInfo));
239 if (RT_FAILURE(rc))
240 return RTVfsChainMsgErrorExitFailure("RTVfsChainQueryInfo", pszPath, rc, offError, &ErrInfo.Core);
241 }
242
243 RTMsgError("Recursion is not yet implemented\n");
244 RTVfsDirRelease(hVfsDir);
245 rc = VERR_NOT_IMPLEMENTED;
246
247 return RT_SUCCESS(rc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
248}
249
250
251static RTEXITCODE RTCmdChMod(unsigned cArgs, char **papszArgs)
252{
253 /*
254 * Parse the command line.
255 */
256 static const RTGETOPTDEF s_aOptions[] =
257 {
258 /* operations */
259 { "--recursive", 'R', RTGETOPT_REQ_NOTHING },
260 { "--preserve-root", 'x', RTGETOPT_REQ_NOTHING },
261 { "--no-preserve-root", 'X', RTGETOPT_REQ_NOTHING },
262 { "--changes", 'c', RTGETOPT_REQ_NOTHING },
263 { "--quiet", 'f', RTGETOPT_REQ_NOTHING },
264 { "--silent", 'f', RTGETOPT_REQ_NOTHING },
265 { "--verbose", 'v', RTGETOPT_REQ_NOTHING },
266 { "--reference", 'Z', RTGETOPT_REQ_NOTHING },
267 { "--always-use-vfs-chain-api", 'A', RTGETOPT_REQ_NOTHING },
268
269 };
270
271 RTGETOPTSTATE GetState;
272 int rc = RTGetOptInit(&GetState, cArgs, papszArgs, s_aOptions, RT_ELEMENTS(s_aOptions), 1,
273 RTGETOPTINIT_FLAGS_OPTS_FIRST);
274 if (RT_FAILURE(rc))
275 return RTMsgErrorExit(RTEXITCODE_FAILURE, "RTGetOpt failed: %Rrc", rc);
276
277 RTCMDCHMODOPTS Opts;
278 Opts.enmNoiseLevel = kRTCmdChModNoise_Default;
279 Opts.fPreserveRoot = false;
280 Opts.fRecursive = false;
281 Opts.fAlwaysUseChainApi = false;
282 Opts.fModeClear = 0;
283 Opts.fModeSet = 0;
284
285 RTGETOPTUNION ValueUnion;
286 while ( (rc = RTGetOpt(&GetState, &ValueUnion)) != 0
287 && rc != VINF_GETOPT_NOT_OPTION)
288 {
289 switch (rc)
290 {
291 case 'R':
292 Opts.fRecursive = true;
293 break;
294
295 case 'x':
296 Opts.fPreserveRoot = true;
297 break;
298 case 'X':
299 Opts.fPreserveRoot = false;
300 break;
301
302 case 'f':
303 Opts.enmNoiseLevel = kRTCmdChModNoise_Quiet;
304 break;
305 case 'c':
306 Opts.enmNoiseLevel = kRTCmdChModNoise_Changes;
307 break;
308 case 'v':
309 Opts.enmNoiseLevel = kRTCmdChModNoise_Verbose;
310 break;
311
312 case 'Z':
313 {
314 RTFSOBJINFO ObjInfo;
315 RTERRINFOSTATIC ErrInfo;
316 uint32_t offError;
317 rc = RTVfsChainQueryInfo(ValueUnion.psz, &ObjInfo,RTFSOBJATTRADD_NOTHING, RTPATH_F_FOLLOW_LINK,
318 &offError, RTErrInfoInitStatic(&ErrInfo));
319 if (RT_FAILURE(rc))
320 return RTVfsChainMsgErrorExitFailure("RTVfsChainQueryInfo", ValueUnion.psz, rc, offError, &ErrInfo.Core);
321 Opts.fModeClear = RTCHMOD_SET_ALL_MASK;
322 Opts.fModeSet = ObjInfo.Attr.fMode & RTCHMOD_SET_ALL_MASK;
323 break;
324 }
325
326 case 'A':
327 Opts.fAlwaysUseChainApi = true;
328 break;
329
330 case 'h':
331 RTPrintf("Usage: %s [options] <mode> <file> [..]\n"
332 "\n"
333 "Options:\n"
334 " -f, --silent, --quiet\n"
335 " -c, --changes\n"
336 " -v, --verbose\n"
337 " Noise level selection.\n"
338 " -R, --recursive\n"
339 " Recurse into directories.\n"
340 " --preserve-root, --no-preserve-root\n"
341 " Whether to allow recursion from the root (default: yes).\n"
342 " --reference <file>\n"
343 " Take mode mask to use from <file> instead of <mode>.\n"
344 "\n"
345 "The <mode> part isn't fully implemented, so only numerical octal notation\n"
346 "works. Prefix the number(s) with 0x to use hexadecimal. There are two forms\n"
347 "of the numerical notation: <SET> and <SET>:<CLEAR>\n"
348 , papszArgs[0]);
349 return RTEXITCODE_SUCCESS;
350
351 case 'V':
352 RTPrintf("%sr%d\n", RTBldCfgVersion(), RTBldCfgRevision());
353 return RTEXITCODE_SUCCESS;
354
355 default:
356
357 return RTGetOptPrintError(rc, &ValueUnion);
358 }
359 }
360
361 /*
362 * The MODE.
363 */
364 if ( Opts.fModeClear == 0
365 && Opts.fModeSet == 0)
366 {
367 if (rc != VINF_GETOPT_NOT_OPTION)
368 return RTMsgErrorExit(RTEXITCODE_SYNTAX, "No mode change specified.\n");
369
370 char *pszNext;
371 if ( ValueUnion.psz[0] == '0'
372 && (ValueUnion.psz[1] == 'x' || ValueUnion.psz[1] == 'X'))
373 rc = RTStrToUInt32Ex(ValueUnion.psz, &pszNext, 16, &Opts.fModeSet);
374 else
375 rc = RTStrToUInt32Ex(ValueUnion.psz, &pszNext, 8, &Opts.fModeSet);
376 if ( rc != VINF_SUCCESS
377 && (rc != VWRN_TRAILING_CHARS || *pszNext != ':'))
378 return RTMsgErrorExit(RTEXITCODE_SYNTAX, "Unable to parse mode mask: %s\n", ValueUnion.psz);
379 Opts.fModeSet &= RTCHMOD_SET_ALL_MASK;
380
381 if (rc == VINF_SUCCESS)
382 Opts.fModeClear = RTCHMOD_SET_ALL_MASK;
383 else
384 {
385 pszNext++;
386 if ( pszNext[0] == '0'
387 && (pszNext[1] == 'x' || pszNext[1] == 'X'))
388 rc = RTStrToUInt32Ex(pszNext, &pszNext, 16, &Opts.fModeClear);
389 else
390 rc = RTStrToUInt32Ex(pszNext, &pszNext, 8, &Opts.fModeClear);
391 if (rc != VINF_SUCCESS)
392 return RTMsgErrorExit(RTEXITCODE_SYNTAX, "Unable to parse mode mask: %s\n", ValueUnion.psz);
393 Opts.fModeClear &= RTCHMOD_SET_ALL_MASK;
394 }
395
396 rc = RTGetOpt(&GetState, &ValueUnion);
397 }
398
399 /*
400 * No files means error.
401 */
402 if (rc != VINF_GETOPT_NOT_OPTION)
403 return RTMsgErrorExit(RTEXITCODE_FAILURE, "No directories specified.\n");
404
405 /*
406 * Work thru the specified dirs.
407 */
408 RTEXITCODE rcExit = RTEXITCODE_SUCCESS;
409 while (rc == VINF_GETOPT_NOT_OPTION)
410 {
411 if (Opts.fRecursive)
412 rc = rtCmdChModRecursive(&Opts, ValueUnion.psz);
413 else
414 rc = rtCmdChModOne(&Opts, ValueUnion.psz);
415 if (RT_FAILURE(rc))
416 rcExit = RTEXITCODE_FAILURE;
417
418 /* next */
419 rc = RTGetOpt(&GetState, &ValueUnion);
420 }
421 if (rc != 0)
422 rcExit = RTGetOptPrintError(rc, &ValueUnion);
423
424 return rcExit;
425}
426
427
428int main(int argc, char **argv)
429{
430 int rc = RTR3InitExe(argc, &argv, 0);
431 if (RT_FAILURE(rc))
432 return RTMsgInitFailure(rc);
433 return RTCmdChMod(argc, argv);
434}
435
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette