]>
Commit | Line | Data |
---|---|---|
cae7420b DB |
1 | /** @file\r |
2 | Function that deletes a file.\r | |
b9ec9330 | 3 | \r |
149d6335 | 4 | Copyright (c) 2005 - 2013, Intel Corporation. All rights reserved.<BR>\r |
eb6cb4ce | 5 | SPDX-License-Identifier: BSD-2-Clause-Patent\r |
b9ec9330 QH |
6 | \r |
7 | \r | |
cae7420b | 8 | **/\r |
b9ec9330 | 9 | \r |
cae7420b | 10 | #include "Fat.h"\r |
b9ec9330 | 11 | \r |
cae7420b | 12 | /**\r |
b9ec9330 | 13 | \r |
cae7420b | 14 | Deletes the file & Closes the file handle.\r |
b9ec9330 | 15 | \r |
cae7420b | 16 | @param FHand - Handle to the file to delete.\r |
b9ec9330 | 17 | \r |
cae7420b DB |
18 | @retval EFI_SUCCESS - Delete the file successfully.\r |
19 | @retval EFI_WARN_DELETE_FAILURE - Fail to delete the file.\r | |
b9ec9330 | 20 | \r |
cae7420b | 21 | **/\r |
b9ec9330 QH |
22 | EFI_STATUS\r |
23 | EFIAPI\r | |
24 | FatDelete (\r | |
dba03ba1 | 25 | IN EFI_FILE_PROTOCOL *FHand\r |
b9ec9330 | 26 | )\r |
b9ec9330 QH |
27 | {\r |
28 | FAT_IFILE *IFile;\r | |
29 | FAT_OFILE *OFile;\r | |
30 | FAT_DIRENT *DirEnt;\r | |
31 | EFI_STATUS Status;\r | |
32 | UINTN Round;\r | |
33 | \r | |
34 | IFile = IFILE_FROM_FHAND (FHand);\r | |
35 | OFile = IFile->OFile;\r | |
36 | \r | |
149d6335 RN |
37 | FatWaitNonblockingTask (IFile);\r |
38 | \r | |
b9ec9330 QH |
39 | //\r |
40 | // Lock the volume\r | |
41 | //\r | |
42 | FatAcquireLock ();\r | |
43 | \r | |
44 | //\r | |
45 | // If the file is read-only, then don't delete it\r | |
46 | //\r | |
47 | if (IFile->ReadOnly) {\r | |
48 | Status = EFI_WRITE_PROTECTED;\r | |
49 | goto Done;\r | |
50 | }\r | |
51 | //\r | |
52 | // If the file is the root dir, then don't delete it\r | |
53 | //\r | |
54 | if (OFile->Parent == NULL) {\r | |
55 | Status = EFI_ACCESS_DENIED;\r | |
56 | goto Done;\r | |
57 | }\r | |
58 | //\r | |
db62b65c | 59 | // If the file has a permanent error, skip the delete\r |
b9ec9330 QH |
60 | //\r |
61 | Status = OFile->Error;\r | |
62 | if (!EFI_ERROR (Status)) {\r | |
63 | //\r | |
64 | // If this is a directory, make sure it's empty before\r | |
65 | // allowing it to be deleted\r | |
66 | //\r | |
67 | if (OFile->ODir != NULL) {\r | |
68 | //\r | |
69 | // We do not allow to delete nonempty directory\r | |
70 | //\r | |
71 | FatResetODirCursor (OFile);\r | |
72 | for (Round = 0; Round < 3; Round++) {\r | |
73 | Status = FatGetNextDirEnt (OFile, &DirEnt);\r | |
74 | if ((EFI_ERROR (Status)) ||\r | |
75 | ((Round < 2) && (DirEnt == NULL || !FatIsDotDirEnt (DirEnt))) ||\r | |
76 | ((Round == 2) && (DirEnt != NULL))\r | |
77 | ) {\r | |
78 | Status = EFI_ACCESS_DENIED;\r | |
79 | goto Done;\r | |
80 | }\r | |
81 | }\r | |
82 | }\r | |
83 | //\r | |
84 | // Return the file's space by setting its size to 0\r | |
85 | //\r | |
86 | FatTruncateOFile (OFile, 0);\r | |
87 | //\r | |
88 | // Free the directory entry for this file\r | |
89 | //\r | |
90 | Status = FatRemoveDirEnt (OFile->Parent, OFile->DirEnt);\r | |
91 | if (EFI_ERROR (Status)) {\r | |
92 | goto Done;\r | |
93 | }\r | |
94 | //\r | |
95 | // Set a permanent error for this OFile in case there\r | |
96 | // are still opened IFiles attached\r | |
97 | //\r | |
98 | OFile->Error = EFI_NOT_FOUND;\r | |
99 | } else if (OFile->Error == EFI_NOT_FOUND) {\r | |
100 | Status = EFI_SUCCESS;\r | |
101 | }\r | |
102 | \r | |
103 | Done:\r | |
104 | //\r | |
105 | // Always close the handle\r | |
106 | //\r | |
107 | FatIFileClose (IFile);\r | |
108 | //\r | |
109 | // Done\r | |
110 | //\r | |
149d6335 | 111 | Status = FatCleanupVolume (OFile->Volume, NULL, Status, NULL);\r |
b9ec9330 QH |
112 | FatReleaseLock ();\r |
113 | \r | |
114 | if (EFI_ERROR (Status)) {\r | |
115 | Status = EFI_WARN_DELETE_FAILURE;\r | |
116 | }\r | |
117 | \r | |
118 | return Status;\r | |
119 | }\r |