Fixed a memory leakage in DeleteFileW.
[reactos.git] / reactos / lib / kernel32 / file / delete.c
1 /* $Id: delete.c,v 1.10 2002/04/27 19:13:41 hbirr Exp $
2 *
3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS system libraries
5 * FILE: lib/kernel32/file/delete.c
6 * PURPOSE: Deleting files
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 <ntdll/rtl.h>
16 #include <windows.h>
17
18 #define NDEBUG
19 #include <kernel32/kernel32.h>
20 #include <kernel32/error.h>
21
22
23 /* FUNCTIONS ****************************************************************/
24
25 WINBOOL
26 STDCALL
27 DeleteFileA (
28 LPCSTR lpFileName
29 )
30 {
31 UNICODE_STRING FileNameU;
32 ANSI_STRING FileName;
33 WINBOOL Result;
34
35 RtlInitAnsiString (&FileName,
36 (LPSTR)lpFileName);
37
38 /* convert ansi (or oem) string to unicode */
39 if (bIsFileApiAnsi)
40 RtlAnsiStringToUnicodeString (&FileNameU,
41 &FileName,
42 TRUE);
43 else
44 RtlOemStringToUnicodeString (&FileNameU,
45 &FileName,
46 TRUE);
47
48 Result = DeleteFileW (FileNameU.Buffer);
49
50 RtlFreeHeap (RtlGetProcessHeap (),
51 0,
52 FileNameU.Buffer);
53
54 return Result;
55 }
56
57
58 WINBOOL
59 STDCALL
60 DeleteFileW (
61 LPCWSTR lpFileName
62 )
63 {
64 FILE_DISPOSITION_INFORMATION FileDispInfo;
65 OBJECT_ATTRIBUTES ObjectAttributes;
66 IO_STATUS_BLOCK IoStatusBlock;
67 UNICODE_STRING NtPathU;
68 HANDLE FileHandle;
69 NTSTATUS Status;
70
71 DPRINT("DeleteFileW (lpFileName %S)\n",lpFileName);
72
73 if (!RtlDosPathNameToNtPathName_U ((LPWSTR)lpFileName,
74 &NtPathU,
75 NULL,
76 NULL))
77 return FALSE;
78
79 DPRINT("NtPathU \'%wZ\'\n", &NtPathU);
80
81 ObjectAttributes.Length = sizeof(OBJECT_ATTRIBUTES);
82 ObjectAttributes.RootDirectory = NULL;
83 ObjectAttributes.ObjectName = &NtPathU;
84 ObjectAttributes.Attributes = OBJ_CASE_INSENSITIVE| OBJ_INHERIT;
85 ObjectAttributes.SecurityDescriptor = NULL;
86 ObjectAttributes.SecurityQualityOfService = NULL;
87
88 Status = NtCreateFile (&FileHandle,
89 FILE_WRITE_ATTRIBUTES,
90 &ObjectAttributes,
91 &IoStatusBlock,
92 NULL,
93 FILE_ATTRIBUTE_NORMAL,
94 0,
95 FILE_OPEN,
96 FILE_NON_DIRECTORY_FILE,
97 NULL,
98 0);
99
100 RtlFreeUnicodeString(&NtPathU);
101
102 if (!NT_SUCCESS(Status))
103 {
104 CHECKPOINT;
105 SetLastErrorByStatus (Status);
106 return FALSE;
107 }
108
109 FileDispInfo.DoDeleteFile = TRUE;
110
111 Status = NtSetInformationFile (FileHandle,
112 &IoStatusBlock,
113 &FileDispInfo,
114 sizeof(FILE_DISPOSITION_INFORMATION),
115 FileDispositionInformation);
116 if (!NT_SUCCESS(Status))
117 {
118 CHECKPOINT;
119 NtClose (FileHandle);
120 SetLastErrorByStatus (Status);
121 return FALSE;
122 }
123
124 Status = NtClose (FileHandle);
125 if (!NT_SUCCESS (Status))
126 {
127 CHECKPOINT;
128 SetLastErrorByStatus (Status);
129 return FALSE;
130 }
131
132 return TRUE;
133 }
134
135 /* EOF */