Moved CloseFile within MoveFileExW.
[reactos.git] / reactos / lib / kernel32 / file / move.c
1 /* $Id: move.c,v 1.5 2002/04/27 19:15:43 hbirr Exp $
2 *
3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS system libraries
5 * FILE: lib/kernel32/file/file.c
6 * PURPOSE: Directory functions
7 * PROGRAMMER: Ariadne ( ariadne@xs4all.nl)
8 * UPDATE HISTORY:
9 * Created 01/11/98
10 */
11
12 /* INCLUDES *****************************************************************/
13
14 #include <ddk/ntddk.h>
15 #include <windows.h>
16 #include <ntos/minmax.h>
17
18 #define NDEBUG
19 #include <kernel32/kernel32.h>
20
21
22 #define FILE_RENAME_SIZE MAX_PATH +sizeof(FILE_RENAME_INFORMATION)
23
24
25 /* FUNCTIONS ****************************************************************/
26
27 WINBOOL
28 STDCALL
29 MoveFileA (
30 LPCSTR lpExistingFileName,
31 LPCSTR lpNewFileName
32 )
33 {
34 return MoveFileExA (lpExistingFileName,
35 lpNewFileName,
36 MOVEFILE_COPY_ALLOWED);
37 }
38
39
40 WINBOOL
41 STDCALL
42 MoveFileExA (
43 LPCSTR lpExistingFileName,
44 LPCSTR lpNewFileName,
45 DWORD dwFlags
46 )
47 {
48 UNICODE_STRING ExistingFileNameU;
49 UNICODE_STRING NewFileNameU;
50 ANSI_STRING ExistingFileName;
51 ANSI_STRING NewFileName;
52 WINBOOL Result;
53
54 RtlInitAnsiString (&ExistingFileName,
55 (LPSTR)lpExistingFileName);
56
57 RtlInitAnsiString (&NewFileName,
58 (LPSTR)lpNewFileName);
59
60 /* convert ansi (or oem) string to unicode */
61 if (bIsFileApiAnsi)
62 {
63 RtlAnsiStringToUnicodeString (&ExistingFileNameU,
64 &ExistingFileName,
65 TRUE);
66 RtlAnsiStringToUnicodeString (&NewFileNameU,
67 &NewFileName,
68 TRUE);
69 }
70 else
71 {
72 RtlOemStringToUnicodeString (&ExistingFileNameU,
73 &ExistingFileName,
74 TRUE);
75 RtlOemStringToUnicodeString (&NewFileNameU,
76 &NewFileName,
77 TRUE);
78 }
79
80 Result = MoveFileExW (ExistingFileNameU.Buffer,
81 NewFileNameU.Buffer,
82 dwFlags);
83
84 RtlFreeHeap (RtlGetProcessHeap (),
85 0,
86 ExistingFileNameU.Buffer);
87 RtlFreeHeap (RtlGetProcessHeap (),
88 0,
89 NewFileNameU.Buffer);
90
91 return Result;
92 }
93
94
95 WINBOOL
96 STDCALL
97 MoveFileW (
98 LPCWSTR lpExistingFileName,
99 LPCWSTR lpNewFileName
100 )
101 {
102 return MoveFileExW (lpExistingFileName,
103 lpNewFileName,
104 MOVEFILE_COPY_ALLOWED);
105 }
106
107
108 WINBOOL
109 STDCALL
110 MoveFileExW (
111 LPCWSTR lpExistingFileName,
112 LPCWSTR lpNewFileName,
113 DWORD dwFlags
114 )
115 {
116 HANDLE hFile = NULL;
117 IO_STATUS_BLOCK IoStatusBlock;
118 FILE_RENAME_INFORMATION *FileRename;
119 USHORT Buffer[FILE_RENAME_SIZE];
120 NTSTATUS errCode;
121
122 hFile = CreateFileW (lpExistingFileName,
123 GENERIC_ALL,
124 FILE_SHARE_WRITE|FILE_SHARE_READ,
125 NULL,
126 OPEN_EXISTING,
127 FILE_ATTRIBUTE_NORMAL,
128 NULL);
129
130 FileRename = (FILE_RENAME_INFORMATION *)Buffer;
131 if ((dwFlags & MOVEFILE_REPLACE_EXISTING) == MOVEFILE_REPLACE_EXISTING)
132 FileRename->Replace = TRUE;
133 else
134 FileRename->Replace = FALSE;
135
136 FileRename->FileNameLength = wcslen (lpNewFileName);
137 memcpy (FileRename->FileName,
138 lpNewFileName,
139 min(FileRename->FileNameLength, MAX_PATH));
140
141 errCode = NtSetInformationFile (hFile,
142 &IoStatusBlock,
143 FileRename,
144 FILE_RENAME_SIZE,
145 FileRenameInformation);
146 CloseHandle(hFile);
147 if (!NT_SUCCESS(errCode))
148 {
149 if (CopyFileW (lpExistingFileName,
150 lpNewFileName,
151 FileRename->Replace))
152 DeleteFileW (lpExistingFileName);
153 }
154 return TRUE;
155 }
156
157 /* EOF */