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