VirtualBox

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

Last change on this file since 71963 was 70442, checked in by vboxsync, 7 years ago

RTCp.cpp: scm says max one trailing newline.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 13.4 KB
Line 
1/* $Id: RTCp.cpp 70442 2018-01-03 04:27:32Z 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 /** Number of sources. */
65 size_t cSources;
66 /** Source files/dirs. */
67 const char **papszSources;
68 /** Destination dir/file. */
69 const char *pszDestination;
70} RTCMDCPOPTS;
71/** Pointer to const CAT options. */
72typedef RTCMDCPOPTS const *PCRTCMDCPOPTS;
73
74
75
76/**
77 * Does the copying, source by source.
78 *
79 * @returns exit code.
80 * @param pOpts Options.
81 */
82static RTEXITCODE rtCmdCpDoIt(PCRTCMDCPOPTS pOpts)
83{
84 /*
85 * Check out what the destination is.
86 */
87/** @todo need to cache + share VFS chain elements here! */
88 RTERRINFOSTATIC ErrInfo;
89 uint32_t offError;
90 RTFSOBJINFO DstObjInfo;
91 int rc = RTVfsChainQueryInfo(pOpts->pszDestination, &DstObjInfo, RTFSOBJATTRADD_UNIX, RTPATH_F_FOLLOW_LINK,
92 &offError, RTErrInfoInitStatic(&ErrInfo));
93 if (RT_SUCCESS(rc))
94 {
95 if (pOpts->cSources > 1 && !RTFS_IS_DIRECTORY(DstObjInfo.Attr.fMode))
96 return RTMsgErrorExitFailure("Mutiple files to copy and destination is not a directory!");
97 }
98 else if (rc != VERR_FILE_NOT_FOUND)
99 return RTVfsChainMsgErrorExitFailure("RTVfsChainQueryInfo", pOpts->pszDestination, rc, offError, &ErrInfo.Core);
100 else
101 RT_ZERO(DstObjInfo);
102 AssertCompile(!RTFS_IS_DIRECTORY(0));
103
104 /*
105 * Process the sources.
106 */
107 RTEXITCODE rcExit = RTEXITCODE_SUCCESS;
108 for (size_t iSrc = 0; iSrc < pOpts->cSources; iSrc++)
109 {
110 const char *pszSrc = pOpts->papszSources[iSrc];
111 RTFSOBJINFO SrcObjInfo;
112 RT_ZERO(SrcObjInfo);
113 rc = RTVfsChainQueryInfo(pszSrc, &SrcObjInfo, RTFSOBJATTRADD_UNIX,
114 pOpts->fFollowCommandLineSymlinks ? RTPATH_F_FOLLOW_LINK : RTPATH_F_ON_LINK,
115 &offError, RTErrInfoInitStatic(&ErrInfo));
116 if (RT_FAILURE(rc))
117 {
118 rcExit = RTVfsChainMsgErrorExitFailure("RTVfsChainQueryInfo", pszSrc, rc, offError, &ErrInfo.Core);
119 continue;
120 }
121
122 /*
123 * Regular file.
124 */
125 if (RTFS_IS_FILE(SrcObjInfo.Attr.fMode))
126 {
127 /* Open source. */
128 RTVFSFILE hVfsSrc;
129 rc = RTVfsChainOpenFile(pszSrc, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE,
130 &hVfsSrc, &offError, RTErrInfoInitStatic(&ErrInfo));
131 if (RT_SUCCESS(rc))
132 {
133 /* Make destination name if necessary and open destination.
134 Note! RTFILE_O_READ needed for VFS chains. */
135 char szDstPath[RTPATH_MAX];
136 const char *pszDst = pOpts->pszDestination;
137 if (RTFS_IS_DIRECTORY(DstObjInfo.Attr.fMode))
138 {
139 rc = RTPathJoin(szDstPath, sizeof(szDstPath), pszDst, RTPathFilename(pszSrc));
140 pszDst = szDstPath;
141 }
142 if (RT_SUCCESS(rc))
143 {
144 RTVFSFILE hVfsDst;
145 rc = RTVfsChainOpenFile(pszDst, RTFILE_O_READWRITE | RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_WRITE,
146 &hVfsDst, &offError, RTErrInfoInitStatic(&ErrInfo));
147 if (RT_SUCCESS(rc))
148 {
149 /* Copy the bytes. */
150 RTVFSIOSTREAM hVfsIosSrc = RTVfsFileToIoStream(hVfsSrc);
151 RTVFSIOSTREAM hVfsIosDst = RTVfsFileToIoStream(hVfsDst);
152
153 rc = RTVfsUtilPumpIoStreams(hVfsIosSrc, hVfsIosDst, 0);
154
155 if (RT_SUCCESS(rc))
156 {
157 if (pOpts->fVerbose)
158 RTPrintf("'%s' -> '%s'\n", pszSrc, pszDst);
159 }
160 else
161 rcExit = RTMsgErrorExitFailure("RTVfsUtilPumpIoStreams failed for '%s' -> '%s': %Rrc",
162 pszSrc, pszDst, rc);
163 RTVfsIoStrmRelease(hVfsIosSrc);
164 RTVfsIoStrmRelease(hVfsIosDst);
165 RTVfsFileRelease(hVfsDst);
166 }
167 else
168 rcExit = RTVfsChainMsgErrorExitFailure("RTVfsChainOpenFile", pszDst, rc, offError, &ErrInfo.Core);
169 }
170 else
171 rcExit = RTMsgErrorExitFailure("Destination path too long for source #%u (%Rrc): %s", iSrc, pszSrc, rc);
172 RTVfsFileRelease(hVfsSrc);
173 }
174 else
175 rcExit = RTVfsChainMsgErrorExitFailure("RTVfsChainOpenFile", pszSrc, rc, offError, &ErrInfo.Core);
176 }
177 /*
178 * Copying a directory requires the -R option to be active.
179 */
180 else if (RTFS_IS_DIRECTORY(SrcObjInfo.Attr.fMode))
181 {
182 if (pOpts->fRecursive)
183 {
184 /** @todo recursive copy */
185 rcExit = RTMsgErrorExitFailure("Recursion not implemented yet!");
186 }
187 else
188 rcExit = RTMsgErrorExitFailure("Source #%u is a directory: %s", iSrc + 1, pszSrc);
189 }
190 /*
191 * We currently don't support copying any other file types.
192 */
193 else
194 rcExit = RTMsgErrorExitFailure("Source #%u neither a file nor a directory: %s", iSrc + 1, pszSrc);
195 }
196 return rcExit;
197}
198
199
200/**
201 * A /bin/cp clone.
202 *
203 * @returns Program exit code.
204 *
205 * @param cArgs The number of arguments.
206 * @param papszArgs The argument vector. (Note that this may be
207 * reordered, so the memory must be writable.)
208 */
209RTEXITCODE RTCmdCp(unsigned cArgs, char **papszArgs)
210{
211
212 /*
213 * Parse the command line.
214 */
215 static const RTGETOPTDEF s_aOptions[] =
216 {
217 { "--archive", 'a', RTGETOPT_REQ_NOTHING },
218 { "--backup", 'B', RTGETOPT_REQ_STRING },
219 { "", 'b', RTGETOPT_REQ_NOTHING },
220 { "--copy-contents", 1024, RTGETOPT_REQ_NOTHING },
221 { "", 'd', RTGETOPT_REQ_NOTHING },
222 { "--no-dereference", 'P', RTGETOPT_REQ_NOTHING },
223 { "--force", 'f', RTGETOPT_REQ_NOTHING },
224 { "", 'H', RTGETOPT_REQ_NOTHING },
225 { "--link", 'l', RTGETOPT_REQ_NOTHING },
226 { "--dereference", 'L', RTGETOPT_REQ_NOTHING },
227 { "", 'p', RTGETOPT_REQ_NOTHING },
228 { "--preserve", 1026, RTGETOPT_REQ_STRING },
229 { "--no-preserve", 1027, RTGETOPT_REQ_STRING },
230 { "--recursive", 'R', RTGETOPT_REQ_NOTHING },
231 { "--remove-destination", 1028, RTGETOPT_REQ_NOTHING },
232 { "--reply", 1029, RTGETOPT_REQ_STRING },
233 { "--sparse", 1030, RTGETOPT_REQ_STRING },
234 { "--strip-trailing-slashes", 1031, RTGETOPT_REQ_NOTHING },
235 { "--symbolic-links", 's', RTGETOPT_REQ_NOTHING },
236 { "--suffix", 'S', RTGETOPT_REQ_STRING },
237 { "--target-directory", 't', RTGETOPT_REQ_STRING },
238 { "--no-target-directory", 'T', RTGETOPT_REQ_NOTHING },
239 { "--update", 'u', RTGETOPT_REQ_NOTHING },
240 { "--verbose", 'v', RTGETOPT_REQ_NOTHING },
241 { "--one-file-system", 'x', RTGETOPT_REQ_NOTHING },
242 };
243
244 RTCMDCPOPTS Opts;
245 Opts.fVerbose = false;
246 Opts.fFollowCommandLineSymlinks = false;
247 Opts.fRecursive = false;
248 Opts.fOneFileSystem = false;
249 Opts.pszDestination = NULL;
250 Opts.cSources = 0;
251 Opts.papszSources = (const char **)RTMemAllocZ(sizeof(const char *) * (cArgs + 2));
252 AssertReturn(Opts.papszSources, RTEXITCODE_FAILURE);
253
254 RTEXITCODE rcExit = RTEXITCODE_SUCCESS;
255
256 RTGETOPTSTATE GetState;
257 int rc = RTGetOptInit(&GetState, cArgs, papszArgs, s_aOptions, RT_ELEMENTS(s_aOptions), 1,
258 RTGETOPTINIT_FLAGS_OPTS_FIRST);
259 if (RT_SUCCESS(rc))
260 {
261 bool fContinue = true;
262 do
263 {
264 RTGETOPTUNION ValueUnion;
265 int chOpt = RTGetOpt(&GetState, &ValueUnion);
266 switch (chOpt)
267 {
268 case 0:
269 if (Opts.pszDestination == NULL && Opts.cSources == 0)
270 rcExit = RTMsgErrorExit(RTEXITCODE_SYNTAX, "Missing source and destination");
271 else if (Opts.pszDestination == NULL && Opts.cSources == 1)
272 rcExit = RTMsgErrorExit(RTEXITCODE_SYNTAX, "Missing destination");
273 else if (Opts.pszDestination != NULL && Opts.cSources == 0)
274 rcExit = RTMsgErrorExit(RTEXITCODE_SYNTAX, "Missing source");
275 else
276 {
277 if (Opts.pszDestination == NULL && Opts.cSources > 0)
278 Opts.pszDestination = Opts.papszSources[--Opts.cSources];
279 Assert(Opts.cSources > 0);
280 rcExit = rtCmdCpDoIt(&Opts);
281 }
282 fContinue = false;
283 break;
284
285 case VINF_GETOPT_NOT_OPTION:
286 Assert(Opts.cSources < cArgs);
287 Opts.papszSources[Opts.cSources++] = ValueUnion.psz;
288 break;
289
290 case 'H':
291 Opts.fFollowCommandLineSymlinks = true;
292 break;
293
294 case 'R':
295 Opts.fRecursive = true;
296 break;
297 case 'x':
298 Opts.fOneFileSystem = true;
299 break;
300
301 case 'v':
302 Opts.fVerbose = true;
303 break;
304
305 case 'h':
306 RTPrintf("Usage: to be written\nOption dump:\n");
307 for (unsigned i = 0; i < RT_ELEMENTS(s_aOptions); i++)
308 RTPrintf(" -%c,%s\n", s_aOptions[i].iShort, s_aOptions[i].pszLong);
309 fContinue = false;
310 break;
311
312 case 'V':
313 RTPrintf("%sr%d\n", RTBldCfgVersion(), RTBldCfgRevision());
314 fContinue = false;
315 break;
316
317 default:
318 rcExit = RTGetOptPrintError(chOpt, &ValueUnion);
319 fContinue = false;
320 break;
321 }
322 } while (fContinue);
323 }
324 else
325 rcExit = RTMsgErrorExit(RTEXITCODE_SYNTAX, "RTGetOptInit: %Rrc", rc);
326 RTMemFree(Opts.papszSources);
327 return rcExit;
328}
329
330
331int main(int argc, char **argv)
332{
333 int rc = RTR3InitExe(argc, &argv, 0);
334 if (RT_FAILURE(rc))
335 return RTMsgInitFailure(rc);
336 return RTCmdCp(argc, argv);
337}
338
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