]>
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 | |
bcdcc416 | 51 | \r |
b9ec9330 QH |
52 | //\r |
53 | // If the file is the root dir, then don't delete it\r | |
54 | //\r | |
55 | if (OFile->Parent == NULL) {\r | |
56 | Status = EFI_ACCESS_DENIED;\r | |
57 | goto Done;\r | |
58 | }\r | |
bcdcc416 | 59 | \r |
b9ec9330 | 60 | //\r |
db62b65c | 61 | // If the file has a permanent error, skip the delete\r |
b9ec9330 QH |
62 | //\r |
63 | Status = OFile->Error;\r | |
64 | if (!EFI_ERROR (Status)) {\r | |
65 | //\r | |
66 | // If this is a directory, make sure it's empty before\r | |
67 | // allowing it to be deleted\r | |
68 | //\r | |
69 | if (OFile->ODir != NULL) {\r | |
70 | //\r | |
71 | // We do not allow to delete nonempty directory\r | |
72 | //\r | |
73 | FatResetODirCursor (OFile);\r | |
74 | for (Round = 0; Round < 3; Round++) {\r | |
75 | Status = FatGetNextDirEnt (OFile, &DirEnt);\r | |
76 | if ((EFI_ERROR (Status)) ||\r | |
bcdcc416 | 77 | ((Round < 2) && ((DirEnt == NULL) || !FatIsDotDirEnt (DirEnt))) ||\r |
b9ec9330 | 78 | ((Round == 2) && (DirEnt != NULL))\r |
bcdcc416 MK |
79 | )\r |
80 | {\r | |
b9ec9330 QH |
81 | Status = EFI_ACCESS_DENIED;\r |
82 | goto Done;\r | |
83 | }\r | |
84 | }\r | |
85 | }\r | |
bcdcc416 | 86 | \r |
b9ec9330 QH |
87 | //\r |
88 | // Return the file's space by setting its size to 0\r | |
89 | //\r | |
90 | FatTruncateOFile (OFile, 0);\r | |
91 | //\r | |
92 | // Free the directory entry for this file\r | |
93 | //\r | |
94 | Status = FatRemoveDirEnt (OFile->Parent, OFile->DirEnt);\r | |
95 | if (EFI_ERROR (Status)) {\r | |
96 | goto Done;\r | |
97 | }\r | |
bcdcc416 | 98 | \r |
b9ec9330 QH |
99 | //\r |
100 | // Set a permanent error for this OFile in case there\r | |
101 | // are still opened IFiles attached\r | |
102 | //\r | |
103 | OFile->Error = EFI_NOT_FOUND;\r | |
104 | } else if (OFile->Error == EFI_NOT_FOUND) {\r | |
105 | Status = EFI_SUCCESS;\r | |
106 | }\r | |
107 | \r | |
108 | Done:\r | |
109 | //\r | |
110 | // Always close the handle\r | |
111 | //\r | |
112 | FatIFileClose (IFile);\r | |
113 | //\r | |
114 | // Done\r | |
115 | //\r | |
149d6335 | 116 | Status = FatCleanupVolume (OFile->Volume, NULL, Status, NULL);\r |
b9ec9330 QH |
117 | FatReleaseLock ();\r |
118 | \r | |
119 | if (EFI_ERROR (Status)) {\r | |
120 | Status = EFI_WARN_DELETE_FAILURE;\r | |
121 | }\r | |
122 | \r | |
123 | return Status;\r | |
124 | }\r |