ReactOS  0.4.14-dev-368-gfa26425
copymove.c
Go to the documentation of this file.
1 /*
2  * CopyFile, MoveFile and related routines test
3  */
4 
5 #include <ctype.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <tchar.h>
9 #include <windows.h>
10 
11 static TCHAR
13 {
14  DWORD drives = GetLogicalDrives();
15  BOOL found = FALSE;
16  TCHAR drive;
17  TCHAR rootdir[] = _T( "?:\\" );
18  TCHAR currentdir[MAX_PATH + 1];
19 
20  if (0 != GetCurrentDirectory(MAX_PATH + 1, currentdir)) {
21  for (drive = _T('A'); ! found && drive <= _T('Z'); drive++) {
22  if (0 != (drives & (1 << (drive - _T('A'))))&&
23  drive != _totupper(currentdir[0])) {
24  rootdir[0] = drive;
25  found = (DRIVE_FIXED == GetDriveType(rootdir));
26  }
27  }
28  }
29 
30  return found ? drive - 1 : _T( ' ' );
31 }
32 
33 static void
35 {
38 }
39 
40 static void
42 {
43  HANDLE file;
44  char buffer[4096];
45  DWORD wrote;
46  int c;
47 
51  0,
52  NULL,
54  0,
55  0);
56 
57  if (INVALID_HANDLE_VALUE == file) {
58  fprintf(stderr, "CreateFile failed with code %lu\n", GetLastError());
59  exit(1);
60  }
61  for(c = 0; c < sizeof(buffer); c++) {
62  buffer[c] = (char) c;
63  }
64  if (! WriteFile(file, buffer, sizeof(buffer), &wrote, NULL)) {
65  fprintf(stderr, "WriteFile failed with code %lu\n", GetLastError());
66  exit(1);
67  }
69 
70  if (! SetFileAttributes(filename, attributes)) {
71  fprintf(stderr, "SetFileAttributes failed with code %lu\n", GetLastError());
72  exit(1);
73  }
74 }
75 
76 static void
78 {
80 }
81 
82 static void
84 {
85  if (! CreateDirectory(dirname, NULL)) {
86  fprintf(stderr, "CreateDirectory failed with code %lu\n", GetLastError());
87  exit(1);
88  }
89 }
90 
91 static void
93 {
94  HANDLE file;
95  char buffer[4096];
96  DWORD read;
97  int c;
98  DWORD diskattr;
99 
101  GENERIC_READ,
102  0,
103  NULL,
105  0,
106  0);
107 
108  if (INVALID_HANDLE_VALUE == file) {
109  fprintf(stderr, "CreateFile failed with code %lu\n", GetLastError());
110  exit(1);
111  }
112 
113  if (! ReadFile(file, buffer, sizeof(buffer), &read, NULL)) {
114  fprintf(stderr, "ReadFile failed with code %lu\n", GetLastError());
115  exit(1);
116  }
117  if (read != sizeof(buffer)) {
118  fprintf(stderr, "Trying to read %u bytes but got %lu bytes\n", sizeof(buffer), read);
119  exit(1);
120  }
121  for(c = 0; c < sizeof(buffer); c++) {
122  if (buffer[c] != (char) c) {
123  fprintf(stderr, "File contents changed at position %u\n", c);
124  exit(1);
125  }
126  }
127 
128  CloseHandle(file);
129 
130  diskattr = GetFileAttributes(filename);
131  if (INVALID_FILE_ATTRIBUTES == diskattr) {
132  fprintf(stderr, "GetFileAttributes failed with code %lu\n", GetLastError());
133  exit(1);
134  }
135  if (diskattr != attributes) {
136  fprintf(stderr, "Attribute mismatch, expected 0x%08lx found 0x%08lx\n", attributes, diskattr);
137  exit(1);
138  }
139 }
140 
141 int
142 main(int argc, char *argv[])
143 {
144  TCHAR otherdrive;
145  TCHAR otherfile[ ] = _T("?:\\other.dat");
146 
147  otherdrive = FindOtherDrive();
148 
149  printf("Testing simple move\n");
151  DeleteTestFile(_T("end.dat"));
152  if (! MoveFile(_T("begin.dat"), _T("end.dat"))) {
153  fprintf(stderr, "MoveFile failed with code %lu\n", GetLastError());
154  exit(1);
155  }
157  DeleteTestFile(_T("end.dat"));
158 
159  printf("Testing move of non-existing file\n");
160  DeleteTestFile(_T("begin.dat"));
161  DeleteTestFile(_T("end.dat"));
162  if (MoveFile(_T("begin.dat"), _T("end.dat"))) {
163  fprintf(stderr, "MoveFile succeeded but shouldn't have\n");
164  exit(1);
165  } else if (ERROR_FILE_NOT_FOUND != GetLastError()) {
166  fprintf(stderr, "MoveFile failed with unexpected code %lu\n", GetLastError());
167  exit(1);
168  }
169  DeleteTestFile(_T("end.dat"));
170 
171 /* Not correctly implemented in ros, destination file is kept open after this */
172 #if 0
173  printf("Testing move to existing file\n");
176  if (MoveFile(_T("begin.dat"), _T("end.dat"))) {
177  fprintf(stderr, "MoveFile succeeded but shouldn't have\n");
178  exit(1);
179  } else if (ERROR_ALREADY_EXISTS != GetLastError()) {
180  fprintf(stderr, "MoveFile failed with unexpected code %lu\n", GetLastError());
181  exit(1);
182  }
183  DeleteTestFile(_T("begin.dat"));
184  DeleteTestFile(_T("end.dat"));
185 #endif
186 
187 /* Not implemented yet in ros */
188 #if 0
189  printf("Testing directory move\n");
190  CreateTestDir(_T("begin"));
191  CreateTestFile(_T("begin\\file.dat"), FILE_ATTRIBUTE_NORMAL);
192  DeleteTestDir(_T("end"));
193  if (! MoveFile(_T("begin"), _T("end"))) {
194  fprintf(stderr, "MoveFile failed with code %lu\n", GetLastError());
195  exit(1);
196  }
197  CheckTestFile(_T("end\\file.dat"), FILE_ATTRIBUTE_NORMAL);
198  DeleteTestFile(_T("end\\file.dat"));
199  DeleteTestDir(_T("end"));
200 #endif
201 
202  printf("Testing file move to different directory\n");
204  CreateTestDir(_T("end"));
205  if (! MoveFile(_T("file.dat"), _T("end\\file.dat"))) {
206  fprintf(stderr, "MoveFile failed with code %lu\n", GetLastError());
207  exit(1);
208  }
209  CheckTestFile(_T("end\\file.dat"), FILE_ATTRIBUTE_ARCHIVE);
210  DeleteTestFile(_T("end\\file.dat"));
211  DeleteTestDir(_T("end"));
212 
213  printf("Testing move of read-only file\n");
215  DeleteTestFile(_T("end.dat"));
216  if (! MoveFile(_T("begin.dat"), _T("end.dat"))) {
217  fprintf(stderr, "MoveFile failed with code %lu\n", GetLastError());
218  exit(1);
219  }
221  DeleteTestFile(_T("end.dat"));
222 
223  printf("Testing move to different drive\n");
224  if (_T(' ') != otherdrive) {
225  otherfile[0] = otherdrive;
227  DeleteTestFile(otherfile);
228  if (! MoveFile(_T("begin.dat"), otherfile)) {
229  fprintf(stderr, "MoveFile failed with code %lu\n", GetLastError());
230  exit(1);
231  }
233  DeleteTestFile(otherfile);
234  } else {
235  printf(" Test skipped, no other drive available\n");
236  }
237 
238  printf("Testing move/overwrite of existing file\n");
241  if (! MoveFileEx(_T("begin.dat"), _T("end.dat"), MOVEFILE_REPLACE_EXISTING)) {
242  fprintf(stderr, "MoveFileEx failed with code %lu\n", GetLastError());
243  exit(1);
244  }
245  DeleteTestFile(_T("begin.dat"));
246  DeleteTestFile(_T("end.dat"));
247 
248 /* Not (correctly) implemented in ros yet */
249 #if 0
250  printf("Testing move/overwrite of existing readonly file\n");
253  if (MoveFileEx(_T("begin.dat"), _T("end.dat"), MOVEFILE_REPLACE_EXISTING)) {
254  fprintf(stderr, "MoveFileEx succeeded but shouldn't have\n");
255  exit(1);
256  } else if (ERROR_ALREADY_EXISTS != GetLastError() &&
258  fprintf(stderr, "MoveFileEx failed with unexpected code %lu\n", GetLastError());
259  exit(1);
260  }
261  DeleteTestFile(_T("begin.dat"));
262  DeleteTestFile(_T("end.dat"));
263 #endif
264 
265 /* Not implemented in ros yet */
266 #if 0
267  printf("Testing move to different drive without COPY_ALLOWED\n");
268  if (_T(' ') != otherdrive) {
269  otherfile[0] = otherdrive;
271  DeleteTestFile(otherfile);
272  if (MoveFileEx(_T("begin.dat"), otherfile, 0)) {
273  fprintf(stderr, "MoveFileEx succeeded but shouldn't have\n");
274  exit(1);
275  } else if (ERROR_NOT_SAME_DEVICE != GetLastError()) {
276  fprintf(stderr, "MoveFileEx failed with unexpected code %lu\n", GetLastError());
277  exit(1);
278  }
279  DeleteTestFile(otherfile);
280  } else {
281  printf(" Test skipped, no other drive available\n");
282  }
283 #endif
284 
285  printf("Testing move to different drive with COPY_ALLOWED\n");
286  if (_T(' ') != otherdrive) {
287  otherfile[0] = otherdrive;
289  DeleteTestFile(otherfile);
290  if (! MoveFileEx(_T("begin.dat"), otherfile, MOVEFILE_COPY_ALLOWED)) {
291  fprintf(stderr, "MoveFileEx failed with code %lu\n", GetLastError());
292  exit(1);
293  }
295  DeleteTestFile(otherfile);
296  } else {
297  printf(" Test skipped, no other drive available\n");
298  }
299 
300  printf("All tests successfully completed\n");
301 
302  return 0;
303 }
BOOL WINAPI WriteFile(IN HANDLE hFile, IN LPCVOID lpBuffer, IN DWORD nNumberOfBytesToWrite OPTIONAL, OUT LPDWORD lpNumberOfBytesWritten, IN LPOVERLAPPED lpOverlapped OPTIONAL)
Definition: rw.c:24
static int argc
Definition: ServiceArgs.c:12
const CHAR * LPCTSTR
Definition: xmlstorage.h:193
#define CloseHandle
Definition: compat.h:406
int main(int argc, char *argv[])
Definition: copymove.c:142
#define MOVEFILE_REPLACE_EXISTING
Definition: filesup.h:28
#define GetDriveType
Definition: winbase.h:3636
#define MOVEFILE_COPY_ALLOWED
Definition: filesup.h:29
static void CheckTestFile(LPCTSTR filename, DWORD attributes)
Definition: copymove.c:92
#define RemoveDirectory
Definition: winbase.h:3719
__cdecl __MINGW_NOTHROW char * dirname(char *)
#define INVALID_HANDLE_VALUE
Definition: compat.h:399
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1059
GLuint buffer
Definition: glext.h:5915
#define MoveFileEx
Definition: winbase.h:3702
#define _totupper
Definition: tchar.h:1509
#define argv
Definition: mplay32.c:18
#define DeleteFile
Definition: winbase.h:3588
const char * filename
Definition: ioapi.h:135
#define ERROR_ACCESS_DENIED
Definition: compat.h:87
unsigned int BOOL
Definition: ntddk_ex.h:94
_Check_return_opt_ _CRTIMP int __cdecl fprintf(_Inout_ FILE *_File, _In_z_ _Printf_format_string_ const char *_Format,...)
#define GENERIC_WRITE
Definition: nt_native.h:90
#define GetCurrentDirectory
Definition: winbase.h:3629
smooth NULL
Definition: ftsmooth.c:416
unsigned char
Definition: typeof.h:29
#define ERROR_FILE_NOT_FOUND
Definition: disk.h:79
#define OPEN_EXISTING
Definition: compat.h:434
char TCHAR
Definition: xmlstorage.h:189
#define _T(x)
Definition: vfdio.h:22
static void DeleteTestDir(LPCTSTR dirname)
Definition: copymove.c:77
#define SetFileAttributes
Definition: winbase.h:3733
#define MAX_PATH
Definition: compat.h:26
const char file[]
Definition: icontest.c:11
const GLubyte * c
Definition: glext.h:8905
#define FILE_ATTRIBUTE_READONLY
Definition: nt_native.h:702
static void CreateTestFile(LPCTSTR filename, DWORD attributes)
Definition: copymove.c:41
unsigned long DWORD
Definition: ntddk_ex.h:95
#define INVALID_FILE_ATTRIBUTES
Definition: vfdcmd.c:23
#define FILE_ATTRIBUTE_ARCHIVE
Definition: nt_native.h:706
#define FILE_ATTRIBUTE_NORMAL
Definition: compat.h:126
#define GENERIC_READ
Definition: compat.h:124
#define GetFileAttributes
Definition: winbase.h:3639
#define DRIVE_FIXED
Definition: winbase.h:249
DWORD WINAPI GetLogicalDrives(VOID)
Definition: disk.c:110
#define MoveFile
Definition: winbase.h:3701
#define CREATE_ALWAYS
Definition: disk.h:72
static char drive[2]
Definition: batch.c:28
IN OUT PVCB OUT PDIRENT OUT PBCB IN BOOLEAN CreateFile
Definition: fatprocs.h:904
static void CreateTestDir(LPCTSTR dirname)
Definition: copymove.c:83
static TCHAR FindOtherDrive()
Definition: copymove.c:12
#define ERROR_NOT_SAME_DEVICE
Definition: winerror.h:120
static void DeleteTestFile(LPCTSTR filename)
Definition: copymove.c:34
#define c
Definition: ke_i.h:80
FILE * stderr
#define ERROR_ALREADY_EXISTS
Definition: disk.h:80
void exit(int exitcode)
Definition: _exit.c:33
BOOL WINAPI ReadFile(IN HANDLE hFile, IN LPVOID lpBuffer, IN DWORD nNumberOfBytesToRead, OUT LPDWORD lpNumberOfBytesRead OPTIONAL, IN LPOVERLAPPED lpOverlapped OPTIONAL)
Definition: rw.c:123
_CRTIMP int __cdecl read(_In_ int _FileHandle, _Out_writes_bytes_(_MaxCharCount) void *_DstBuf, _In_ unsigned int _MaxCharCount)
#define printf
Definition: config.h:203
Definition: fci.c:126