VirtualBox

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

Last change on this file since 75410 was 75119, checked in by vboxsync, 6 years ago

RTCp.cpp: Build fix for previous GCC 8.2 workaround.

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