Delete.c 3.0 KB

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