summaryrefslogtreecommitdiffstats
path: root/FatPkg/EnhancedFatDxe/Delete.c
blob: 448d669f9d8ef654ac0702d2668b39d0cd37fddb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/** @file
  Function that deletes a file.

Copyright (c) 2005 - 2013, Intel Corporation. All rights reserved.<BR>
SPDX-License-Identifier: BSD-2-Clause-Patent


**/

#include "Fat.h"

/**

  Deletes the file & Closes the file handle.

  @param  FHand                    - Handle to the file to delete.

  @retval EFI_SUCCESS              - Delete the file successfully.
  @retval EFI_WARN_DELETE_FAILURE  - Fail to delete the file.

**/
EFI_STATUS
EFIAPI
FatDelete (
  IN EFI_FILE_PROTOCOL  *FHand
  )
{
  FAT_IFILE   *IFile;
  FAT_OFILE   *OFile;
  FAT_DIRENT  *DirEnt;
  EFI_STATUS  Status;
  UINTN       Round;

  IFile = IFILE_FROM_FHAND (FHand);
  OFile = IFile->OFile;

  FatWaitNonblockingTask (IFile);

  //
  // Lock the volume
  //
  FatAcquireLock ();

  //
  // If the file is read-only, then don't delete it
  //
  if (IFile->ReadOnly) {
    Status = EFI_WRITE_PROTECTED;
    goto Done;
  }

  //
  // If the file is the root dir, then don't delete it
  //
  if (OFile->Parent == NULL) {
    Status = EFI_ACCESS_DENIED;
    goto Done;
  }

  //
  // If the file has a permanent error, skip the delete
  //
  Status = OFile->Error;
  if (!EFI_ERROR (Status)) {
    //
    // If this is a directory, make sure it's empty before
    // allowing it to be deleted
    //
    if (OFile->ODir != NULL) {
      //
      // We do not allow to delete nonempty directory
      //
      FatResetODirCursor (OFile);
      for (Round = 0; Round < 3; Round++) {
        Status = FatGetNextDirEnt (OFile, &DirEnt);
        if ((EFI_ERROR (Status)) ||
            ((Round < 2) && ((DirEnt == NULL) || !FatIsDotDirEnt (DirEnt))) ||
            ((Round == 2) && (DirEnt != NULL))
            )
        {
          Status = EFI_ACCESS_DENIED;
          goto Done;
        }
      }
    }

    //
    // Return the file's space by setting its size to 0
    //
    FatTruncateOFile (OFile, 0);
    //
    // Free the directory entry for this file
    //
    Status = FatRemoveDirEnt (OFile->Parent, OFile->DirEnt);
    if (EFI_ERROR (Status)) {
      goto Done;
    }

    //
    // Set a permanent error for this OFile in case there
    // are still opened IFiles attached
    //
    OFile->Error = EFI_NOT_FOUND;
  } else if (OFile->Error == EFI_NOT_FOUND) {
    Status = EFI_SUCCESS;
  }

Done:
  //
  // Always close the handle
  //
  FatIFileClose (IFile);
  //
  // Done
  //
  Status = FatCleanupVolume (OFile->Volume, NULL, Status, NULL);
  FatReleaseLock ();

  if (EFI_ERROR (Status)) {
    Status = EFI_WARN_DELETE_FAILURE;
  }

  return Status;
}