1 | /* $Id: tstMove.cpp 4071 2007-08-07 17:07:59Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * innotek Portable Runtime - RTFileMove & RTDirMove test program.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 innotek GmbH
|
---|
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 as published by the Free Software Foundation,
|
---|
13 | * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
|
---|
14 | * distribution. VirtualBox OSE is distributed in the hope that it will
|
---|
15 | * be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 | /*******************************************************************************
|
---|
19 | * Header Files *
|
---|
20 | *******************************************************************************/
|
---|
21 | #include <iprt/file.h>
|
---|
22 | #include <iprt/path.h>
|
---|
23 | #include <iprt/dir.h>
|
---|
24 | #include <iprt/err.h>
|
---|
25 | #include <iprt/runtime.h>
|
---|
26 | #include <iprt/stream.h>
|
---|
27 |
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * Checks if there is one of the typical help options in the argument list.
|
---|
31 | */
|
---|
32 | static bool HasHelpOption(int argc, char **argv)
|
---|
33 | {
|
---|
34 | for (int argi = 1; argi < argc; argi++)
|
---|
35 | if ( argv[argi][0] == '-'
|
---|
36 | && ( argv[argi][1] == 'h'
|
---|
37 | || argv[argi][1] == 'H'
|
---|
38 | || argv[argi][1] == '?'
|
---|
39 | || argv[argi][1] == '-')
|
---|
40 | )
|
---|
41 | return true;
|
---|
42 | return false;
|
---|
43 | }
|
---|
44 |
|
---|
45 |
|
---|
46 | int main(int argc, char **argv)
|
---|
47 | {
|
---|
48 | RTR3Init();
|
---|
49 |
|
---|
50 | /*
|
---|
51 | * Arguments or any -? or --help?
|
---|
52 | */
|
---|
53 | if (argc <= 1 || HasHelpOption(argc, argv))
|
---|
54 | {
|
---|
55 | RTPrintf("usage: tstMove [-efdr] <src> <dst>\n"
|
---|
56 | "\n"
|
---|
57 | " -f File only.\n"
|
---|
58 | " -d Directory only.\n"
|
---|
59 | " -m Use move operation instead of rename. (implies -f)\n"
|
---|
60 | " -r Replace existing destination.\n"
|
---|
61 | );
|
---|
62 | return 1;
|
---|
63 | }
|
---|
64 |
|
---|
65 | /*
|
---|
66 | * Parse args.
|
---|
67 | */
|
---|
68 | const char *pszNew = NULL;
|
---|
69 | const char *pszOld = NULL;
|
---|
70 | bool fDir = false;
|
---|
71 | bool fFile = false;
|
---|
72 | bool fReplace = false;
|
---|
73 | bool fMoveFile = false;
|
---|
74 | for (int argi = 1; argi < argc; argi++)
|
---|
75 | {
|
---|
76 | if (argv[argi][0] == '-')
|
---|
77 | {
|
---|
78 | const char *psz = &argv[argi][1];
|
---|
79 | do
|
---|
80 | {
|
---|
81 | switch (*psz)
|
---|
82 | {
|
---|
83 | case 'd':
|
---|
84 | fDir = true;
|
---|
85 | fMoveFile = false;
|
---|
86 | break;
|
---|
87 | case 'f':
|
---|
88 | fFile = true;
|
---|
89 | break;
|
---|
90 | case 'm':
|
---|
91 | fMoveFile = true;
|
---|
92 | fDir = false;
|
---|
93 | fFile = true;
|
---|
94 | break;
|
---|
95 | case 'r':
|
---|
96 | fReplace = true;
|
---|
97 | break;
|
---|
98 | default:
|
---|
99 | RTPrintf("tstRTFileMove: syntax error: Unknown option '%c' in '%s'!\n", *psz, argv[argi]);
|
---|
100 | return 1;
|
---|
101 | }
|
---|
102 | } while (*++psz);
|
---|
103 | }
|
---|
104 | else if (!pszOld)
|
---|
105 | pszOld = argv[argi];
|
---|
106 | else if (!pszNew)
|
---|
107 | pszNew = argv[argi];
|
---|
108 | else
|
---|
109 | {
|
---|
110 | RTPrintf("tstRTFileMove: syntax error: too many filenames!\n");
|
---|
111 | return 1;
|
---|
112 | }
|
---|
113 | }
|
---|
114 | if (!pszNew || !pszOld)
|
---|
115 | {
|
---|
116 | RTPrintf("tstRTFileMove: syntax error: too few filenames!\n");
|
---|
117 | return 1;
|
---|
118 | }
|
---|
119 |
|
---|
120 | /*
|
---|
121 | * Do the operation.
|
---|
122 | */
|
---|
123 | int rc;
|
---|
124 | if (!fDir && !fFile)
|
---|
125 | rc = RTPathRename(pszOld, pszNew, fReplace ? RTPATHRENAME_FLAGS_REPLACE : 0);
|
---|
126 | else if (fDir)
|
---|
127 | rc = RTDirRename( pszOld, pszNew, fReplace ? RTPATHRENAME_FLAGS_REPLACE : 0);
|
---|
128 | else if (!fMoveFile)
|
---|
129 | rc = RTFileRename(pszOld, pszNew, fReplace ? RTPATHRENAME_FLAGS_REPLACE : 0);
|
---|
130 | else
|
---|
131 | rc = RTFileMove( pszOld, pszNew, fReplace ? RTFILEMOVE_FLAGS_REPLACE : 0);
|
---|
132 |
|
---|
133 | RTPrintf("The API returned %Rrc\n", rc);
|
---|
134 | return !RT_SUCCESS(rc);
|
---|
135 | }
|
---|
136 |
|
---|