]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - FatPkg/EnhancedFatDxe/Delete.c
MdePkg/BaseLib: Move CHAR_NULL definition to Base.h in BaseLib
[mirror_edk2.git] / FatPkg / EnhancedFatDxe / Delete.c
... / ...
CommitLineData
1/*++\r
2\r
3Copyright (c) 2005 - 2013, Intel Corporation. All rights reserved.<BR>\r
4This program and the accompanying materials are licensed and made available\r
5under the terms and conditions of the BSD License which accompanies this\r
6distribution. The full text of the license may be found at\r
7http://opensource.org/licenses/bsd-license.php\r
8\r
9THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
10WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
11\r
12\r
13Module Name:\r
14\r
15 delete.c\r
16\r
17Abstract:\r
18\r
19 Function that deletes a file\r
20\r
21Revision History\r
22\r
23--*/\r
24\r
25#include "Fat.h"\r
26\r
27EFI_STATUS\r
28EFIAPI\r
29FatDelete (\r
30 IN EFI_FILE_PROTOCOL *FHand\r
31 )\r
32/*++\r
33\r
34Routine Description:\r
35\r
36 Deletes the file & Closes the file handle.\r
37\r
38Arguments:\r
39\r
40 FHand - Handle to the file to delete.\r
41\r
42Returns:\r
43\r
44 EFI_SUCCESS - Delete the file successfully.\r
45 EFI_WARN_DELETE_FAILURE - Fail to delete the file.\r
46\r
47--*/\r
48{\r
49 FAT_IFILE *IFile;\r
50 FAT_OFILE *OFile;\r
51 FAT_DIRENT *DirEnt;\r
52 EFI_STATUS Status;\r
53 UINTN Round;\r
54\r
55 IFile = IFILE_FROM_FHAND (FHand);\r
56 OFile = IFile->OFile;\r
57\r
58 FatWaitNonblockingTask (IFile);\r
59\r
60 //\r
61 // Lock the volume\r
62 //\r
63 FatAcquireLock ();\r
64\r
65 //\r
66 // If the file is read-only, then don't delete it\r
67 //\r
68 if (IFile->ReadOnly) {\r
69 Status = EFI_WRITE_PROTECTED;\r
70 goto Done;\r
71 }\r
72 //\r
73 // If the file is the root dir, then don't delete it\r
74 //\r
75 if (OFile->Parent == NULL) {\r
76 Status = EFI_ACCESS_DENIED;\r
77 goto Done;\r
78 }\r
79 //\r
80 // If the file has a permanant error, skip the delete\r
81 //\r
82 Status = OFile->Error;\r
83 if (!EFI_ERROR (Status)) {\r
84 //\r
85 // If this is a directory, make sure it's empty before\r
86 // allowing it to be deleted\r
87 //\r
88 if (OFile->ODir != NULL) {\r
89 //\r
90 // We do not allow to delete nonempty directory\r
91 //\r
92 FatResetODirCursor (OFile);\r
93 for (Round = 0; Round < 3; Round++) {\r
94 Status = FatGetNextDirEnt (OFile, &DirEnt);\r
95 if ((EFI_ERROR (Status)) ||\r
96 ((Round < 2) && (DirEnt == NULL || !FatIsDotDirEnt (DirEnt))) ||\r
97 ((Round == 2) && (DirEnt != NULL))\r
98 ) {\r
99 Status = EFI_ACCESS_DENIED;\r
100 goto Done;\r
101 }\r
102 }\r
103 }\r
104 //\r
105 // Return the file's space by setting its size to 0\r
106 //\r
107 FatTruncateOFile (OFile, 0);\r
108 //\r
109 // Free the directory entry for this file\r
110 //\r
111 Status = FatRemoveDirEnt (OFile->Parent, OFile->DirEnt);\r
112 if (EFI_ERROR (Status)) {\r
113 goto Done;\r
114 }\r
115 //\r
116 // Set a permanent error for this OFile in case there\r
117 // are still opened IFiles attached\r
118 //\r
119 OFile->Error = EFI_NOT_FOUND;\r
120 } else if (OFile->Error == EFI_NOT_FOUND) {\r
121 Status = EFI_SUCCESS;\r
122 }\r
123\r
124Done:\r
125 //\r
126 // Always close the handle\r
127 //\r
128 FatIFileClose (IFile);\r
129 //\r
130 // Done\r
131 //\r
132 Status = FatCleanupVolume (OFile->Volume, NULL, Status, NULL);\r
133 FatReleaseLock ();\r
134\r
135 if (EFI_ERROR (Status)) {\r
136 Status = EFI_WARN_DELETE_FAILURE;\r
137 }\r
138\r
139 return Status;\r
140}\r