VirtualBox

source: vbox/trunk/src/VBox/Runtime/tools/RTCp.cpp@ 93217

Last change on this file since 93217 was 93115, checked in by vboxsync, 3 years ago

scm --update-copyright-year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.0 KB
Line 
1/* $Id: RTCp.cpp 93115 2022-01-01 11:31:46Z vboxsync $ */
2/** @file
3 * IPRT - cp like utility.
4 */
5
6/*
7 * Copyright (C) 2017-2022 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#include <iprt/vfs.h>
32
33#include <iprt/buildconfig.h>
34#include <iprt/file.h>
35#include <iprt/fs.h>
36#include <iprt/err.h>
37#include <iprt/getopt.h>
38#include <iprt/initterm.h>
39#include <iprt/message.h>
40#include <iprt/mem.h>
41#include <iprt/path.h>
42#include <iprt/stream.h>
43#include <iprt/string.h>
44
45
46/*********************************************************************************************************************************
47* Structures and Typedefs *
48*********************************************************************************************************************************/
49/**
50 * CAT command options.
51 */
52typedef struct RTCMDCPOPTS
53{
54 /** -v, --verbose. */
55 bool fVerbose;
56
57 /** -H */
58 bool fFollowCommandLineSymlinks;
59
60 /** Set if recursive copy. */
61 bool fRecursive;
62 /** -x, --one-filesystem. */
63 bool fOneFileSystem;
64
65 /** Special --no-replace-nor-trucate hack for basic NTFS write support. */
66 bool fNoReplaceNorTruncate;
67
68 /** Number of sources. */
69 size_t cSources;
70 /** Source files/dirs. */
71 const char **papszSources;
72 /** Destination dir/file. */
73 const char *pszDestination;
74} RTCMDCPOPTS;
75/** Pointer to const CAT options. */
76typedef RTCMDCPOPTS const *PCRTCMDCPOPTS;
77
78
79
80/**
81 * Does the copying, source by source.
82 *
83 * @returns exit code.
84 * @param pOpts Options.
85 */
86static RTEXITCODE rtCmdCpDoIt(PCRTCMDCPOPTS pOpts)
87{
88 /*
89 * Check out what the destination is.
90 */
91/** @todo need to cache + share VFS chain elements here! */
92 RTERRINFOSTATIC ErrInfo;
93 uint32_t offError;
94 RTFSOBJINFO DstObjInfo;
95 int rc = RTVfsChainQueryInfo(pOpts->pszDestination, &DstObjInfo, RTFSOBJATTRADD_UNIX, RTPATH_F_FOLLOW_LINK,
96 &offError, RTErrInfoInitStatic(&ErrInfo));
97 if (RT_SUCCESS(rc))
98 {
99 if (pOpts->cSources > 1 && !RTFS_IS_DIRECTORY(DstObjInfo.Attr.fMode))
100 return RTMsgErrorExitFailure("Mutiple files to copy and destination is not a directory!");
101 }
102 else if (rc != VERR_FILE_NOT_FOUND)
103 return RTVfsChainMsgErrorExitFailure("RTVfsChainQueryInfo", pOpts->pszDestination, rc, offError, &ErrInfo.Core);
104 else
105 RT_ZERO(DstObjInfo);
106#if !RT_GNUC_PREREQ(8,2) || RT_GNUC_PREREQ(8,3) /* GCC 8.2 produces a tautological compare warning/error here. */
107 AssertCompile(!RTFS_IS_DIRECTORY(0));
108#endif
109
110 /*
111 * Process the sources.
112 */
113 RTEXITCODE rcExit = RTEXITCODE_SUCCESS;
114 for (size_t iSrc = 0; iSrc < pOpts->cSources; iSrc++)
115 {
116 const char *pszSrc = pOpts->papszSources[iSrc];
117 RTFSOBJINFO SrcObjInfo;
118 RT_ZERO(SrcObjInfo);
119 rc = RTVfsChainQueryInfo(pszSrc, &SrcObjInfo, RTFSOBJATTRADD_UNIX,
120 pOpts->fFollowCommandLineSymlinks ? RTPATH_F_FOLLOW_LINK : RTPATH_F_ON_LINK,
121 &offError, RTErrInfoInitStatic(&ErrInfo));
122 if (RT_FAILURE(rc))
123 {
124 rcExit = RTVfsChainMsgErrorExitFailure("RTVfsChainQueryInfo", pszSrc, rc, offError, &ErrInfo.Core);
125 continue;
126 }
127
128 /*
129 * Regular file.
130 */
131 if (RTFS_IS_FILE(SrcObjInfo.Attr.fMode))
132 {
133 /* Open source. */
134 RTVFSFILE hVfsSrc;
135 rc = RTVfsChainOpenFile(pszSrc, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE,
136 &hVfsSrc, &offError, RTErrInfoInitStatic(&ErrInfo));
137 if (RT_SUCCESS(rc))
138 {
139 /* Make destination name if necessary and open destination.
140 Note! RTFILE_O_READ needed for VFS chains. */
141 char szDstPath[RTPATH_MAX];
142 const char *pszDst = pOpts->pszDestination;
143 if (RTFS_IS_DIRECTORY(DstObjInfo.Attr.fMode))
144 {
145 rc = RTPathJoin(szDstPath, sizeof(szDstPath), pszDst, RTPathFilename(pszSrc));
146 pszDst = szDstPath;
147 }
148 if (RT_SUCCESS(rc))
149 {
150 RTVFSFILE hVfsDst;
151 uint64_t fDstFlags = (pOpts->fNoReplaceNorTruncate ? RTFILE_O_OPEN_CREATE : RTFILE_O_CREATE_REPLACE)
152 | RTFILE_O_READWRITE | RTFILE_O_DENY_WRITE | (0666 << RTFILE_O_CREATE_MODE_SHIFT);
153 rc = RTVfsChainOpenFile(pszDst, fDstFlags, &hVfsDst, &offError, RTErrInfoInitStatic(&ErrInfo));
154 if (RT_SUCCESS(rc))
155 {
156 /* Copy the bytes. */
157 RTVFSIOSTREAM hVfsIosSrc = RTVfsFileToIoStream(hVfsSrc);
158 RTVFSIOSTREAM hVfsIosDst = RTVfsFileToIoStream(hVfsDst);
159
160 rc = RTVfsUtilPumpIoStreams(hVfsIosSrc, hVfsIosDst, 0);
161
162 if (RT_SUCCESS(rc))
163 {
164 if (pOpts->fVerbose)
165 RTPrintf("'%s' -> '%s'\n", pszSrc, pszDst);
166 }
167 else
168 rcExit = RTMsgErrorExitFailure("RTVfsUtilPumpIoStreams failed for '%s' -> '%s': %Rrc",
169 pszSrc, pszDst, rc);
170 RTVfsIoStrmRelease(hVfsIosSrc);
171 RTVfsIoStrmRelease(hVfsIosDst);
172 RTVfsFileRelease(hVfsDst);
173 }
174 else
175 rcExit = RTVfsChainMsgErrorExitFailure("RTVfsChainOpenFile", pszDst, rc, offError, &ErrInfo.Core);
176 }
177 else
178 rcExit = RTMsgErrorExitFailure("Destination path too long for source #%u (%Rrc): %s", iSrc, pszSrc, rc);
179 RTVfsFileRelease(hVfsSrc);
180 }
181 else
182 rcExit = RTVfsChainMsgErrorExitFailure("RTVfsChainOpenFile", pszSrc, rc, offError, &ErrInfo.Core);
183 }
184 /*
185 * Copying a directory requires the -R option to be active.
186 */
187 else if (RTFS_IS_DIRECTORY(SrcObjInfo.Attr.fMode))
188 {
189 if (pOpts->fRecursive)
190 {
191 /** @todo recursive copy */
192 rcExit = RTMsgErrorExitFailure("Recursion not implemented yet!");
193 }
194 else
195 rcExit = RTMsgErrorExitFailure("Source #%u is a directory: %s", iSrc + 1, pszSrc);
196 }
197 /*
198 * We currently don't support copying any other file types.
199 */
200 else
201 rcExit = RTMsgErrorExitFailure("Source #%u neither a file nor a directory: %s", iSrc + 1, pszSrc);
202 }
203 return rcExit;
204}
205
206
207/**
208 * A /bin/cp clone.
209 *
210 * @returns Program exit code.
211 *
212 * @param cArgs The number of arguments.
213 * @param papszArgs The argument vector. (Note that this may be
214 * reordered, so the memory must be writable.)
215 */
216RTEXITCODE RTCmdCp(unsigned cArgs, char **papszArgs)
217{
218
219 /*
220 * Parse the command line.
221 */
222 static const RTGETOPTDEF s_aOptions[] =
223 {
224 { "--archive", 'a', RTGETOPT_REQ_NOTHING },
225 { "--backup", 'B', RTGETOPT_REQ_STRING },
226 { "", 'b', RTGETOPT_REQ_NOTHING },
227 { "--copy-contents", 1024, RTGETOPT_REQ_NOTHING },
228 { "", 'd', RTGETOPT_REQ_NOTHING },
229 { "--no-dereference", 'P', RTGETOPT_REQ_NOTHING },
230 { "--force", 'f', RTGETOPT_REQ_NOTHING },
231 { "", 'H', RTGETOPT_REQ_NOTHING },
232 { "--link", 'l', RTGETOPT_REQ_NOTHING },
233 { "--dereference", 'L', RTGETOPT_REQ_NOTHING },
234 { "", 'p', RTGETOPT_REQ_NOTHING },
235 { "--preserve", 1026, RTGETOPT_REQ_STRING },
236 { "--no-preserve", 1027, RTGETOPT_REQ_STRING },
237 { "--recursive", 'R', RTGETOPT_REQ_NOTHING },
238 { "--remove-destination", 1028, RTGETOPT_REQ_NOTHING },
239 { "--reply", 1029, RTGETOPT_REQ_STRING },
240 { "--sparse", 1030, RTGETOPT_REQ_STRING },
241 { "--strip-trailing-slashes", 1031, RTGETOPT_REQ_NOTHING },
242 { "--symbolic-links", 's', RTGETOPT_REQ_NOTHING },
243 { "--suffix", 'S', RTGETOPT_REQ_STRING },
244 { "--target-directory", 't', RTGETOPT_REQ_STRING },
245 { "--no-target-directory", 'T', RTGETOPT_REQ_NOTHING },
246 { "--update", 'u', RTGETOPT_REQ_NOTHING },
247 { "--verbose", 'v', RTGETOPT_REQ_NOTHING },
248 { "--one-file-system", 'x', RTGETOPT_REQ_NOTHING },
249 { "--no-replace-nor-trucate", 1032, RTGETOPT_REQ_NOTHING },
250 };
251
252 RTCMDCPOPTS Opts;
253 Opts.fVerbose = false;
254 Opts.fFollowCommandLineSymlinks = false;
255 Opts.fRecursive = false;
256 Opts.fOneFileSystem = false;
257 Opts.fNoReplaceNorTruncate = false;
258 Opts.pszDestination = NULL;
259 Opts.cSources = 0;
260 Opts.papszSources = (const char **)RTMemAllocZ(sizeof(const char *) * (cArgs + 2));
261 AssertReturn(Opts.papszSources, RTEXITCODE_FAILURE);
262
263 RTEXITCODE rcExit = RTEXITCODE_SUCCESS;
264
265 RTGETOPTSTATE GetState;
266 int rc = RTGetOptInit(&GetState, cArgs, papszArgs, s_aOptions, RT_ELEMENTS(s_aOptions), 1,
267 RTGETOPTINIT_FLAGS_OPTS_FIRST);
268 if (RT_SUCCESS(rc))
269 {
270 bool fContinue = true;
271 do
272 {
273 RTGETOPTUNION ValueUnion;
274 int chOpt = RTGetOpt(&GetState, &ValueUnion);
275 switch (chOpt)
276 {
277 case 0:
278 if (Opts.pszDestination == NULL && Opts.cSources == 0)
279 rcExit = RTMsgErrorExit(RTEXITCODE_SYNTAX, "Missing source and destination");
280 else if (Opts.pszDestination == NULL && Opts.cSources == 1)
281 rcExit = RTMsgErrorExit(RTEXITCODE_SYNTAX, "Missing destination");
282 else if (Opts.pszDestination != NULL && Opts.cSources == 0)
283 rcExit = RTMsgErrorExit(RTEXITCODE_SYNTAX, "Missing source");
284 else
285 {
286 if (Opts.pszDestination == NULL && Opts.cSources > 0)
287 Opts.pszDestination = Opts.papszSources[--Opts.cSources];
288 Assert(Opts.cSources > 0);
289 rcExit = rtCmdCpDoIt(&Opts);
290 }
291 fContinue = false;
292 break;
293
294 case VINF_GETOPT_NOT_OPTION:
295 Assert(Opts.cSources < cArgs);
296 Opts.papszSources[Opts.cSources++] = ValueUnion.psz;
297 break;
298
299 case 'H':
300 Opts.fFollowCommandLineSymlinks = true;
301 break;
302
303 case 'R':
304 Opts.fRecursive = true;
305 break;
306 case 'x':
307 Opts.fOneFileSystem = true;
308 break;
309
310 case 'v':
311 Opts.fVerbose = true;
312 break;
313
314 case 1032:
315 Opts.fNoReplaceNorTruncate = true;
316 break;
317
318 case 'h':
319 RTPrintf("Usage: to be written\nOption dump:\n");
320 for (unsigned i = 0; i < RT_ELEMENTS(s_aOptions); i++)
321 RTPrintf(" -%c,%s\n", s_aOptions[i].iShort, s_aOptions[i].pszLong);
322 fContinue = false;
323 break;
324
325 case 'V':
326 RTPrintf("%sr%d\n", RTBldCfgVersion(), RTBldCfgRevision());
327 fContinue = false;
328 break;
329
330 default:
331 rcExit = RTGetOptPrintError(chOpt, &ValueUnion);
332 fContinue = false;
333 break;
334 }
335 } while (fContinue);
336 }
337 else
338 rcExit = RTMsgErrorExit(RTEXITCODE_SYNTAX, "RTGetOptInit: %Rrc", rc);
339 RTMemFree(Opts.papszSources);
340 return rcExit;
341}
342
343
344int main(int argc, char **argv)
345{
346 int rc = RTR3InitExe(argc, &argv, 0);
347 if (RT_FAILURE(rc))
348 return RTMsgInitFailure(rc);
349 return RTCmdCp(argc, argv);
350}
351
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