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