ReactOS  0.4.15-dev-4857-g47842d7
copymove.c File Reference
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <tchar.h>
#include <windows.h>
Include dependency graph for copymove.c:

Go to the source code of this file.

Functions

static TCHAR FindOtherDrive ()
 
static void DeleteTestFile (LPCTSTR filename)
 
static void CreateTestFile (LPCTSTR filename, DWORD attributes)
 
static void DeleteTestDir (LPCTSTR dirname)
 
static void CreateTestDir (LPCTSTR dirname)
 
static void CheckTestFile (LPCTSTR filename, DWORD attributes)
 
int main (int argc, char *argv[])
 

Function Documentation

◆ CheckTestFile()

static void CheckTestFile ( LPCTSTR  filename,
DWORD  attributes 
)
static

Definition at line 92 of file copymove.c.

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 }
#define CloseHandle
Definition: compat.h:598
#define INVALID_HANDLE_VALUE
Definition: compat.h:590
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1040
GLuint buffer
Definition: glext.h:5915
const char * filename
Definition: ioapi.h:135
_Check_return_opt_ _CRTIMP int __cdecl fprintf(_Inout_ FILE *_File, _In_z_ _Printf_format_string_ const char *_Format,...)
#define OPEN_EXISTING
Definition: compat.h:634
const char file[]
Definition: icontest.c:11
const GLubyte * c
Definition: glext.h:8905
unsigned long DWORD
Definition: ntddk_ex.h:95
#define INVALID_FILE_ATTRIBUTES
Definition: vfdcmd.c:23
#define GENERIC_READ
Definition: compat.h:135
#define GetFileAttributes
Definition: winbase.h:3676
IN OUT PVCB OUT PDIRENT OUT PBCB IN BOOLEAN CreateFile
Definition: fatprocs.h:913
#define ReadFile(a, b, c, d, e)
Definition: compat.h:601
#define NULL
Definition: types.h:112
#define c
Definition: ke_i.h:80
FILE * stderr
void exit(int exitcode)
Definition: _exit.c:33
_CRTIMP int __cdecl read(_In_ int _FileHandle, _Out_writes_bytes_(_MaxCharCount) void *_DstBuf, _In_ unsigned int _MaxCharCount)
Definition: fci.c:126

Referenced by main().

◆ CreateTestDir()

static void CreateTestDir ( LPCTSTR  dirname)
static

Definition at line 83 of file copymove.c.

84 {
85  if (! CreateDirectory(dirname, NULL)) {
86  fprintf(stderr, "CreateDirectory failed with code %lu\n", GetLastError());
87  exit(1);
88  }
89 }
__cdecl __MINGW_NOTHROW char * dirname(char *)
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1040
_Check_return_opt_ _CRTIMP int __cdecl fprintf(_Inout_ FILE *_File, _In_z_ _Printf_format_string_ const char *_Format,...)
#define NULL
Definition: types.h:112
FILE * stderr
void exit(int exitcode)
Definition: _exit.c:33

Referenced by main().

◆ CreateTestFile()

static void CreateTestFile ( LPCTSTR  filename,
DWORD  attributes 
)
static

Definition at line 41 of file copymove.c.

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 }
BOOL WINAPI WriteFile(IN HANDLE hFile, IN LPCVOID lpBuffer, IN DWORD nNumberOfBytesToWrite OPTIONAL, OUT LPDWORD lpNumberOfBytesWritten, IN LPOVERLAPPED lpOverlapped OPTIONAL)
Definition: rw.c:24
#define CloseHandle
Definition: compat.h:598
#define INVALID_HANDLE_VALUE
Definition: compat.h:590
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1040
GLuint buffer
Definition: glext.h:5915
const char * filename
Definition: ioapi.h:135
_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
unsigned char
Definition: typeof.h:29
#define SetFileAttributes
Definition: winbase.h:3770
const char file[]
Definition: icontest.c:11
const GLubyte * c
Definition: glext.h:8905
unsigned long DWORD
Definition: ntddk_ex.h:95
#define GENERIC_READ
Definition: compat.h:135
#define CREATE_ALWAYS
Definition: disk.h:72
IN OUT PVCB OUT PDIRENT OUT PBCB IN BOOLEAN CreateFile
Definition: fatprocs.h:913
#define NULL
Definition: types.h:112
static void DeleteTestFile(LPCTSTR filename)
Definition: copymove.c:34
#define c
Definition: ke_i.h:80
FILE * stderr
void exit(int exitcode)
Definition: _exit.c:33
Definition: fci.c:126

Referenced by main().

◆ DeleteTestDir()

static void DeleteTestDir ( LPCTSTR  dirname)
static

Definition at line 77 of file copymove.c.

78 {
80 }
#define RemoveDirectory
Definition: winbase.h:3756
__cdecl __MINGW_NOTHROW char * dirname(char *)

Referenced by main().

◆ DeleteTestFile()

static void DeleteTestFile ( LPCTSTR  filename)
static

Definition at line 34 of file copymove.c.

35 {
38 }
#define DeleteFile
Definition: winbase.h:3625
const char * filename
Definition: ioapi.h:135
#define SetFileAttributes
Definition: winbase.h:3770
#define FILE_ATTRIBUTE_NORMAL
Definition: compat.h:137

Referenced by CreateTestFile(), and main().

◆ FindOtherDrive()

static TCHAR FindOtherDrive ( )
static

Definition at line 12 of file copymove.c.

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 }
#define GetDriveType
Definition: winbase.h:3673
#define _totupper
Definition: tchar.h:1509
#define FALSE
Definition: types.h:117
unsigned int BOOL
Definition: ntddk_ex.h:94
#define GetCurrentDirectory
Definition: winbase.h:3666
char TCHAR
Definition: xmlstorage.h:189
#define _T(x)
Definition: vfdio.h:22
#define MAX_PATH
Definition: compat.h:34
unsigned long DWORD
Definition: ntddk_ex.h:95
#define DRIVE_FIXED
Definition: winbase.h:249
DWORD WINAPI GetLogicalDrives(VOID)
Definition: disk.c:110
static char drive[2]
Definition: batch.c:28

Referenced by main().

◆ main()

int main ( int argc  ,
char argv[] 
)

hosttype.c Copyright (C) 2002 by Brian Palmer brian.nosp@m.p@sg.nosp@m.inet..nosp@m.com

Definition at line 142 of file copymove.c.

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 }
#define MOVEFILE_REPLACE_EXISTING
Definition: filesup.h:28
#define MOVEFILE_COPY_ALLOWED
Definition: filesup.h:29
static void CheckTestFile(LPCTSTR filename, DWORD attributes)
Definition: copymove.c:92
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1040
#define MoveFileEx
Definition: winbase.h:3739
#define ERROR_ACCESS_DENIED
Definition: compat.h:97
_Check_return_opt_ _CRTIMP int __cdecl fprintf(_Inout_ FILE *_File, _In_z_ _Printf_format_string_ const char *_Format,...)
#define ERROR_FILE_NOT_FOUND
Definition: disk.h:79
char TCHAR
Definition: xmlstorage.h:189
#define _T(x)
Definition: vfdio.h:22
#define printf
Definition: freeldr.h:94
static void DeleteTestDir(LPCTSTR dirname)
Definition: copymove.c:77
#define FILE_ATTRIBUTE_READONLY
Definition: nt_native.h:702
static void CreateTestFile(LPCTSTR filename, DWORD attributes)
Definition: copymove.c:41
#define FILE_ATTRIBUTE_ARCHIVE
Definition: nt_native.h:706
#define FILE_ATTRIBUTE_NORMAL
Definition: compat.h:137
#define MoveFile
Definition: winbase.h:3738
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
FILE * stderr
#define ERROR_ALREADY_EXISTS
Definition: disk.h:80
void exit(int exitcode)
Definition: _exit.c:33