Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

114 lines
2.9 KiB

  1. /*++
  2. Copyright (c) 1991 Microsoft Corporation
  3. Module Name:
  4. delnode.c
  5. Abstract:
  6. Delnode routine for Setup.
  7. WARNING: the delnode routine in here is not multi-thread safe!
  8. Author:
  9. Ted Miller (tedm) August 1992
  10. --*/
  11. #include "precomp.h"
  12. #pragma hdrstop
  13. //
  14. // Put these out here so we don't consume huge stack space as we recurse.
  15. //
  16. TCHAR DelnodePattern[MAX_PATH];
  17. WIN32_FIND_DATA DelnodeFindData;
  18. VOID
  19. DelnodeRoutine(
  20. VOID
  21. )
  22. {
  23. LPTSTR PatternEnd;
  24. HANDLE FindHandle;
  25. //
  26. // Delete each file in the directory, then remove the directory itself.
  27. // If any directories are encountered along the way recurse to delete
  28. // them as they are encountered.
  29. //
  30. PatternEnd = DelnodePattern+lstrlen(DelnodePattern);
  31. //This is safe, since we accounted for these two chars in the higher level MyDelnode()
  32. lstrcat(DelnodePattern,TEXT("\\*"));
  33. FindHandle = FindFirstFile(DelnodePattern,&DelnodeFindData);
  34. if(FindHandle != INVALID_HANDLE_VALUE) {
  35. do {
  36. //
  37. // Form the full name of the file we just found.
  38. //
  39. if (SUCCEEDED(StringCchCopy(PatternEnd+1,
  40. (DelnodePattern + ARRAYSIZE(DelnodePattern)) - (PatternEnd+1),
  41. DelnodeFindData.cFileName)))
  42. {
  43. if(DelnodeFindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
  44. //
  45. // The current match is a directory. Recurse into it unless
  46. // it's . or ...
  47. //
  48. if(lstrcmp(DelnodeFindData.cFileName,TEXT("." ))
  49. && lstrcmp(DelnodeFindData.cFileName,TEXT("..")))
  50. {
  51. DelnodeRoutine();
  52. }
  53. } else {
  54. //
  55. // The current match is not a directory -- so delete it.
  56. //
  57. SetFileAttributes(DelnodePattern,FILE_ATTRIBUTE_NORMAL);
  58. DeleteFile(DelnodePattern);
  59. }
  60. }
  61. *(PatternEnd+1) = 0;
  62. } while(FindNextFile(FindHandle,&DelnodeFindData));
  63. FindClose(FindHandle);
  64. }
  65. //
  66. // Remove the directory we just emptied out.
  67. //
  68. *PatternEnd = 0;
  69. SetFileAttributes(DelnodePattern,FILE_ATTRIBUTE_NORMAL);
  70. RemoveDirectory(DelnodePattern);
  71. //
  72. // Note that the 'directory' might actually be a file.
  73. // Catch that case here.
  74. //
  75. DeleteFile(DelnodePattern);
  76. }
  77. VOID
  78. MyDelnode(
  79. IN LPCTSTR Directory
  80. )
  81. {
  82. if (SUCCEEDED(StringCchCopy(DelnodePattern, ARRAYSIZE(DelnodePattern) - 2, Directory)))
  83. //Use ArraySize - 2, since we will always append a wack, then a * to DelnodePattern.
  84. {
  85. DelnodeRoutine();
  86. }
  87. }