Rm.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /** @file
  2. Main file for attrib shell level 2 function.
  3. (C) Copyright 2015 Hewlett-Packard Development Company, L.P.<BR>
  4. Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>
  5. SPDX-License-Identifier: BSD-2-Clause-Patent
  6. **/
  7. #include "UefiShellLevel2CommandsLib.h"
  8. STATIC CONST SHELL_PARAM_ITEM ParamList[] = {
  9. {L"-q", TypeFlag},
  10. {NULL, TypeMax}
  11. };
  12. /**
  13. Determine if a directory has no files in it.
  14. @param[in] FileHandle The EFI_HANDLE to the directory.
  15. @retval TRUE The directory has no files (or directories).
  16. @retval FALSE The directory has at least 1 file or directory in it.
  17. **/
  18. BOOLEAN
  19. IsDirectoryEmpty (
  20. IN SHELL_FILE_HANDLE FileHandle
  21. )
  22. {
  23. EFI_STATUS Status;
  24. EFI_FILE_INFO *FileInfo;
  25. BOOLEAN NoFile;
  26. BOOLEAN RetVal;
  27. RetVal = TRUE;
  28. NoFile = FALSE;
  29. FileInfo = NULL;
  30. for (Status = FileHandleFindFirstFile(FileHandle, &FileInfo)
  31. ; !NoFile && !EFI_ERROR (Status)
  32. ; FileHandleFindNextFile(FileHandle, FileInfo, &NoFile)
  33. ){
  34. if (StrStr(FileInfo->FileName, L".") != FileInfo->FileName
  35. &&StrStr(FileInfo->FileName, L"..") != FileInfo->FileName) {
  36. RetVal = FALSE;
  37. }
  38. }
  39. return (RetVal);
  40. }
  41. /**
  42. Delete a node and all nodes under it (including sub directories).
  43. @param[in] Node The node to start deleting with.
  44. @param[in] Quiet TRUE to print no messages.
  45. @retval SHELL_SUCCESS The operation was successful.
  46. @retval SHELL_ACCESS_DENIED A file was read only.
  47. @retval SHELL_ABORTED The abort message was received.
  48. @retval SHELL_DEVICE_ERROR A device error occurred reading this Node.
  49. **/
  50. SHELL_STATUS
  51. CascadeDelete(
  52. IN EFI_SHELL_FILE_INFO *Node,
  53. IN CONST BOOLEAN Quiet
  54. )
  55. {
  56. SHELL_STATUS ShellStatus;
  57. EFI_SHELL_FILE_INFO *List;
  58. EFI_SHELL_FILE_INFO *Node2;
  59. EFI_STATUS Status;
  60. SHELL_PROMPT_RESPONSE *Resp;
  61. CHAR16 *TempName;
  62. UINTN NewSize;
  63. Resp = NULL;
  64. ShellStatus = SHELL_SUCCESS;
  65. List = NULL;
  66. Status = EFI_SUCCESS;
  67. if ((Node->Info->Attribute & EFI_FILE_READ_ONLY) == EFI_FILE_READ_ONLY) {
  68. ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_RM_LOG_DETELE_RO), gShellLevel2HiiHandle, L"rm", Node->FullName);
  69. return (SHELL_ACCESS_DENIED);
  70. }
  71. if ((Node->Info->Attribute & EFI_FILE_DIRECTORY) == EFI_FILE_DIRECTORY) {
  72. if (!IsDirectoryEmpty(Node->Handle)) {
  73. if (!Quiet) {
  74. Status = ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN(STR_RM_LOG_DELETE_CONF), gShellLevel2HiiHandle, Node->FullName);
  75. Status = ShellPromptForResponse(ShellPromptResponseTypeYesNo, NULL, (VOID**)&Resp);
  76. ASSERT(Resp != NULL);
  77. if (EFI_ERROR(Status) || *Resp != ShellPromptResponseYes) {
  78. SHELL_FREE_NON_NULL(Resp);
  79. return (SHELL_ABORTED);
  80. }
  81. SHELL_FREE_NON_NULL(Resp);
  82. }
  83. //
  84. // empty out the directory
  85. //
  86. Status = gEfiShellProtocol->FindFilesInDir(Node->Handle, &List);
  87. if (EFI_ERROR(Status)) {
  88. if (List!=NULL) {
  89. gEfiShellProtocol->FreeFileList(&List);
  90. }
  91. return (SHELL_DEVICE_ERROR);
  92. }
  93. for (Node2 = (EFI_SHELL_FILE_INFO *)GetFirstNode(&List->Link)
  94. ; !IsNull(&List->Link, &Node2->Link)
  95. ; Node2 = (EFI_SHELL_FILE_INFO *)GetNextNode(&List->Link, &Node2->Link)
  96. ){
  97. //
  98. // skip the directory traversing stuff...
  99. //
  100. if (StrCmp(Node2->FileName, L".") == 0 || StrCmp(Node2->FileName, L"..") == 0) {
  101. continue;
  102. }
  103. Node2->Status = gEfiShellProtocol->OpenFileByName (Node2->FullName, &Node2->Handle, EFI_FILE_MODE_READ|EFI_FILE_MODE_WRITE);
  104. if (EFI_ERROR(Node2->Status) && StrStr(Node2->FileName, L":") == NULL) {
  105. //
  106. // Update the node filename to have full path with file system identifier
  107. //
  108. NewSize = StrSize(Node->FullName) + StrSize(Node2->FullName);
  109. TempName = AllocateZeroPool(NewSize);
  110. if (TempName == NULL) {
  111. ShellStatus = SHELL_OUT_OF_RESOURCES;
  112. } else {
  113. StrCpyS(TempName, NewSize/sizeof(CHAR16), Node->FullName);
  114. TempName[StrStr(TempName, L":")+1-TempName] = CHAR_NULL;
  115. StrCatS(TempName, NewSize/sizeof(CHAR16), Node2->FullName);
  116. FreePool((VOID*)Node2->FullName);
  117. Node2->FullName = TempName;
  118. //
  119. // Now try again to open the file
  120. //
  121. Node2->Status = gEfiShellProtocol->OpenFileByName (Node2->FullName, &Node2->Handle, EFI_FILE_MODE_READ|EFI_FILE_MODE_WRITE);
  122. }
  123. }
  124. if (!EFI_ERROR(Node2->Status)) {
  125. ShellStatus = CascadeDelete(Node2, Quiet);
  126. } else if (ShellStatus == SHELL_SUCCESS) {
  127. ShellStatus = (SHELL_STATUS)(Node2->Status&(~0x80000000));
  128. }
  129. if (ShellStatus != SHELL_SUCCESS) {
  130. if (List!=NULL) {
  131. gEfiShellProtocol->FreeFileList(&List);
  132. }
  133. return (ShellStatus);
  134. }
  135. }
  136. if (List!=NULL) {
  137. gEfiShellProtocol->FreeFileList(&List);
  138. }
  139. }
  140. }
  141. if (!(StrCmp(Node->FileName, L".") == 0 || StrCmp(Node->FileName, L"..") == 0)) {
  142. //
  143. // now delete the current node...
  144. //
  145. if (!Quiet) {
  146. ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_RM_LOG_DELETE), gShellLevel2HiiHandle, Node->FullName);
  147. }
  148. Status = gEfiShellProtocol->DeleteFile(Node->Handle);
  149. Node->Handle = NULL;
  150. }
  151. //
  152. // We cant allow for the warning here! (Dont use EFI_ERROR Macro).
  153. //
  154. if (Status != EFI_SUCCESS){
  155. ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_RM_LOG_DELETE_ERR), gShellLevel2HiiHandle, Status);
  156. return (SHELL_ACCESS_DENIED);
  157. } else {
  158. if (!Quiet) {
  159. ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_RM_LOG_DELETE_COMP), gShellLevel2HiiHandle);
  160. }
  161. return (SHELL_SUCCESS);
  162. }
  163. }
  164. /**
  165. Determines if a Node is a valid delete target. Will prevent deleting the root directory.
  166. @param[in] List RESERVED. Not used.
  167. @param[in] Node The node to analyze.
  168. @param[in] Package RESERVED. Not used.
  169. **/
  170. BOOLEAN
  171. IsValidDeleteTarget(
  172. IN CONST EFI_SHELL_FILE_INFO *List,
  173. IN CONST EFI_SHELL_FILE_INFO *Node,
  174. IN CONST LIST_ENTRY *Package
  175. )
  176. {
  177. CONST CHAR16 *TempLocation;
  178. BOOLEAN RetVal;
  179. CHAR16 *SearchString;
  180. CHAR16 *Pattern;
  181. UINTN Size;
  182. if (Node == NULL || Node->FullName == NULL) {
  183. return (FALSE);
  184. }
  185. TempLocation = StrStr(Node->FullName, L":");
  186. if (StrLen(TempLocation) <= 2) {
  187. //
  188. // Deleting the root directory is invalid.
  189. //
  190. return (FALSE);
  191. }
  192. TempLocation = ShellGetCurrentDir(NULL);
  193. if (TempLocation == NULL) {
  194. //
  195. // No working directory is specified so whatever is left is ok.
  196. //
  197. return (TRUE);
  198. }
  199. Pattern = NULL;
  200. SearchString = NULL;
  201. Size = 0;
  202. Pattern = StrnCatGrow(&Pattern, &Size, TempLocation , 0);
  203. Pattern = StrnCatGrow(&Pattern, &Size, L"\\" , 0);
  204. Size = 0;
  205. SearchString = StrnCatGrow(&SearchString, &Size, Node->FullName, 0);
  206. if (!EFI_ERROR(ShellIsDirectory(SearchString))) {
  207. SearchString = StrnCatGrow(&SearchString, &Size, L"\\", 0);
  208. SearchString = StrnCatGrow(&SearchString, &Size, L"*", 0);
  209. }
  210. if (Pattern == NULL || SearchString == NULL) {
  211. RetVal = FALSE;
  212. } else {
  213. RetVal = TRUE;
  214. if (gUnicodeCollation->MetaiMatch(gUnicodeCollation, Pattern, SearchString)) {
  215. RetVal = FALSE;
  216. }
  217. }
  218. SHELL_FREE_NON_NULL(Pattern );
  219. SHELL_FREE_NON_NULL(SearchString);
  220. return (RetVal);
  221. }
  222. /**
  223. Function for 'rm' command.
  224. @param[in] ImageHandle Handle to the Image (NULL if Internal).
  225. @param[in] SystemTable Pointer to the System Table (NULL if Internal).
  226. **/
  227. SHELL_STATUS
  228. EFIAPI
  229. ShellCommandRunRm (
  230. IN EFI_HANDLE ImageHandle,
  231. IN EFI_SYSTEM_TABLE *SystemTable
  232. )
  233. {
  234. EFI_STATUS Status;
  235. LIST_ENTRY *Package;
  236. CHAR16 *ProblemParam;
  237. CONST CHAR16 *Param;
  238. SHELL_STATUS ShellStatus;
  239. UINTN ParamCount;
  240. EFI_SHELL_FILE_INFO *FileList;
  241. EFI_SHELL_FILE_INFO *Node;
  242. ProblemParam = NULL;
  243. ShellStatus = SHELL_SUCCESS;
  244. ParamCount = 0;
  245. FileList = NULL;
  246. //
  247. // initialize the shell lib (we must be in non-auto-init...)
  248. //
  249. Status = ShellInitialize();
  250. ASSERT_EFI_ERROR(Status);
  251. //
  252. // parse the command line
  253. //
  254. Status = ShellCommandLineParse (ParamList, &Package, &ProblemParam, TRUE);
  255. if (EFI_ERROR(Status)) {
  256. if (Status == EFI_VOLUME_CORRUPTED && ProblemParam != NULL) {
  257. ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellLevel2HiiHandle, L"rm", ProblemParam);
  258. FreePool(ProblemParam);
  259. ShellStatus = SHELL_INVALID_PARAMETER;
  260. } else {
  261. ASSERT(FALSE);
  262. }
  263. } else {
  264. //
  265. // check for "-?"
  266. //
  267. if (ShellCommandLineGetFlag(Package, L"-?")) {
  268. ASSERT(FALSE);
  269. }
  270. if (ShellCommandLineGetRawValue(Package, 1) == NULL) {
  271. //
  272. // we insufficient parameters
  273. //
  274. ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellLevel2HiiHandle, L"rm");
  275. ShellStatus = SHELL_INVALID_PARAMETER;
  276. } else {
  277. //
  278. // get a list with each file specified by parameters
  279. // if parameter is a directory then add all the files below it to the list
  280. //
  281. for ( ParamCount = 1, Param = ShellCommandLineGetRawValue(Package, ParamCount)
  282. ; Param != NULL
  283. ; ParamCount++, Param = ShellCommandLineGetRawValue(Package, ParamCount)
  284. ){
  285. Status = ShellOpenFileMetaArg((CHAR16*)Param, EFI_FILE_MODE_WRITE|EFI_FILE_MODE_READ, &FileList);
  286. if (EFI_ERROR(Status) || FileList == NULL || IsListEmpty(&FileList->Link)) {
  287. ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_NF), gShellLevel2HiiHandle, L"rm", (CHAR16*)Param);
  288. ShellStatus = SHELL_NOT_FOUND;
  289. break;
  290. }
  291. }
  292. if (ShellStatus == SHELL_SUCCESS){
  293. //
  294. // loop through the list and make sure we are not aborting...
  295. //
  296. for ( Node = (EFI_SHELL_FILE_INFO*)GetFirstNode(&FileList->Link)
  297. ; !IsNull(&FileList->Link, &Node->Link) && !ShellGetExecutionBreakFlag()
  298. ; Node = (EFI_SHELL_FILE_INFO*)GetNextNode(&FileList->Link, &Node->Link)
  299. ){
  300. //
  301. // skip the directory traversing stuff...
  302. //
  303. if (StrCmp(Node->FileName, L".") == 0 || StrCmp(Node->FileName, L"..") == 0) {
  304. continue;
  305. }
  306. //
  307. // do the deleting of nodes
  308. //
  309. if (EFI_ERROR(Node->Status)){
  310. ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_RM_LOG_DELETE_ERR2), gShellLevel2HiiHandle, Node->Status);
  311. ShellStatus = SHELL_ACCESS_DENIED;
  312. break;
  313. }
  314. if (!IsValidDeleteTarget(FileList, Node, Package)) {
  315. ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_RM_LOG_DELETE_ERR3), gShellLevel2HiiHandle, Node->FullName);
  316. ShellStatus = SHELL_INVALID_PARAMETER;
  317. break;
  318. }
  319. ShellStatus = CascadeDelete(Node, ShellCommandLineGetFlag(Package, L"-q"));
  320. }
  321. }
  322. //
  323. // Free the fileList
  324. //
  325. if (FileList != NULL) {
  326. Status = ShellCloseFileMetaArg(&FileList);
  327. }
  328. FileList = NULL;
  329. }
  330. //
  331. // free the command line package
  332. //
  333. ShellCommandLineFreeVarList (Package);
  334. }
  335. return (ShellStatus);
  336. }