1 | /* $Id: tstMove.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - RTFileMove & RTDirMove test program.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-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/file.h>
|
---|
42 | #include <iprt/path.h>
|
---|
43 | #include <iprt/dir.h>
|
---|
44 | #include <iprt/errcore.h>
|
---|
45 | #include <iprt/initterm.h>
|
---|
46 | #include <iprt/stream.h>
|
---|
47 |
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * Checks if there is one of the typical help options in the argument list.
|
---|
51 | */
|
---|
52 | static bool HasHelpOption(int argc, char **argv)
|
---|
53 | {
|
---|
54 | for (int argi = 1; argi < argc; argi++)
|
---|
55 | if ( argv[argi][0] == '-'
|
---|
56 | && ( argv[argi][1] == 'h'
|
---|
57 | || argv[argi][1] == 'H'
|
---|
58 | || argv[argi][1] == '?'
|
---|
59 | || argv[argi][1] == '-')
|
---|
60 | )
|
---|
61 | return true;
|
---|
62 | return false;
|
---|
63 | }
|
---|
64 |
|
---|
65 |
|
---|
66 | int main(int argc, char **argv)
|
---|
67 | {
|
---|
68 | RTR3InitExe(argc, &argv, 0);
|
---|
69 |
|
---|
70 | /*
|
---|
71 | * Arguments or any -? or --help?
|
---|
72 | */
|
---|
73 | if (argc <= 1 || HasHelpOption(argc, argv))
|
---|
74 | {
|
---|
75 | RTPrintf("usage: tstMove [-efdr] <src> <dst>\n"
|
---|
76 | "\n"
|
---|
77 | " -f File only.\n"
|
---|
78 | " -d Directory only.\n"
|
---|
79 | " -m Use move operation instead of rename. (implies -f)\n"
|
---|
80 | " -r Replace existing destination.\n"
|
---|
81 | );
|
---|
82 | return 1;
|
---|
83 | }
|
---|
84 |
|
---|
85 | /*
|
---|
86 | * Parse args.
|
---|
87 | */
|
---|
88 | const char *pszNew = NULL;
|
---|
89 | const char *pszOld = NULL;
|
---|
90 | bool fDir = false;
|
---|
91 | bool fFile = false;
|
---|
92 | bool fReplace = false;
|
---|
93 | bool fMoveFile = false;
|
---|
94 | for (int argi = 1; argi < argc; argi++)
|
---|
95 | {
|
---|
96 | if (argv[argi][0] == '-')
|
---|
97 | {
|
---|
98 | const char *psz = &argv[argi][1];
|
---|
99 | do
|
---|
100 | {
|
---|
101 | switch (*psz)
|
---|
102 | {
|
---|
103 | case 'd':
|
---|
104 | fDir = true;
|
---|
105 | fMoveFile = false;
|
---|
106 | break;
|
---|
107 | case 'f':
|
---|
108 | fFile = true;
|
---|
109 | break;
|
---|
110 | case 'm':
|
---|
111 | fMoveFile = true;
|
---|
112 | fDir = false;
|
---|
113 | fFile = true;
|
---|
114 | break;
|
---|
115 | case 'r':
|
---|
116 | fReplace = true;
|
---|
117 | break;
|
---|
118 | default:
|
---|
119 | RTPrintf("tstRTFileMove: syntax error: Unknown option '%c' in '%s'!\n", *psz, argv[argi]);
|
---|
120 | return 1;
|
---|
121 | }
|
---|
122 | } while (*++psz);
|
---|
123 | }
|
---|
124 | else if (!pszOld)
|
---|
125 | pszOld = argv[argi];
|
---|
126 | else if (!pszNew)
|
---|
127 | pszNew = argv[argi];
|
---|
128 | else
|
---|
129 | {
|
---|
130 | RTPrintf("tstRTFileMove: syntax error: too many filenames!\n");
|
---|
131 | return 1;
|
---|
132 | }
|
---|
133 | }
|
---|
134 | if (!pszNew || !pszOld)
|
---|
135 | {
|
---|
136 | RTPrintf("tstRTFileMove: syntax error: too few filenames!\n");
|
---|
137 | return 1;
|
---|
138 | }
|
---|
139 |
|
---|
140 | /*
|
---|
141 | * Do the operation.
|
---|
142 | */
|
---|
143 | int rc;
|
---|
144 | if (!fDir && !fFile)
|
---|
145 | rc = RTPathRename(pszOld, pszNew, fReplace ? RTPATHRENAME_FLAGS_REPLACE : 0);
|
---|
146 | else if (fDir)
|
---|
147 | rc = RTDirRename( pszOld, pszNew, fReplace ? RTPATHRENAME_FLAGS_REPLACE : 0);
|
---|
148 | else if (!fMoveFile)
|
---|
149 | rc = RTFileRename(pszOld, pszNew, fReplace ? RTPATHRENAME_FLAGS_REPLACE : 0);
|
---|
150 | else
|
---|
151 | rc = RTFileMove( pszOld, pszNew, fReplace ? RTFILEMOVE_FLAGS_REPLACE : 0);
|
---|
152 |
|
---|
153 | RTPrintf("The API returned %Rrc\n", rc);
|
---|
154 | return !RT_SUCCESS(rc);
|
---|
155 | }
|
---|
156 |
|
---|