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.

120 lines
3.4 KiB

  1. //-----------------------------------------------------------------------------
  2. // Util.cpp
  3. //-----------------------------------------------------------------------------
  4. #include "util.h"
  5. #include "assert.h"
  6. #define ASSERT assert
  7. //-----------------------------------------------------------------------------
  8. // Takes a const TCHAR * adds a BS if necessary and converts
  9. // it to a TSTRING
  10. //
  11. // 1/11/2000 paolora added to new util.cpp
  12. //-----------------------------------------------------------------------------
  13. TSTRING StrAddBS( const TCHAR *szDirIn )
  14. {
  15. ASSERT( szDirIn );
  16. if (!szDirIn || !_tcslen( szDirIn ))
  17. return _T("");
  18. TSTRING str = szDirIn;
  19. // Do another MBCS ANSI safe comparison
  20. const TCHAR *szTemp = szDirIn;
  21. const UINT iSize = _tcsclen( szDirIn ) - 1;
  22. for( UINT ui = 0; ui < iSize; ui++ )
  23. szTemp = CharNext( szTemp );
  24. if (_tcsncmp( szTemp, _T("\\"), 1))
  25. str += _T("\\");
  26. return str;
  27. }
  28. //-----------------------------------------------------------------------------
  29. // Takes a const TSTRING and adds a BS if necessary
  30. //
  31. // 1/13/2000 paolora added to new util.cpp
  32. //-----------------------------------------------------------------------------
  33. void AddBS( TSTRING *strDir )
  34. {
  35. ASSERT( strDir );
  36. if (!strDir || !strDir->length())
  37. return;
  38. *strDir = StrAddBS( strDir->c_str() );
  39. return;
  40. }
  41. //-----------------------------------------------------------------------------
  42. // Takes a const TCHAR * and deletes all the dirs and files below and
  43. // including the input directory
  44. //
  45. // 12/11/2000 paolora added to new util.cpp
  46. //-----------------------------------------------------------------------------
  47. BOOL BDeleteDirTree( const TCHAR *szDir, BOOL bDeleteInputDir /*=TRUE*/ )
  48. {
  49. ASSERT( szDir );
  50. if (!szDir || !_tcslen( szDir ))
  51. return FALSE;
  52. // Create the findfirstfile path
  53. TSTRING strDir = szDir;
  54. AddBS( &strDir );
  55. strDir += (TSTRING)_T("*");
  56. // Find the first file
  57. BOOL bFileFound;
  58. TSTRING strItem = szDir;
  59. WIN32_FIND_DATA ffd;
  60. HANDLE hItem = FindFirstFile( strDir.c_str(), &ffd );
  61. if(hItem && (INVALID_HANDLE_VALUE != hItem))
  62. bFileFound = TRUE;
  63. // While files and dirs exist
  64. while( bFileFound )
  65. {
  66. if (_tcscmp( ffd.cFileName, _T(".")) && _tcscmp( ffd.cFileName, _T("..") ))
  67. {
  68. // Create item name
  69. strItem = szDir;
  70. AddBS( &strItem );
  71. strItem += (TSTRING)ffd.cFileName;
  72. // If a Dir, recurse
  73. if (FILE_ATTRIBUTE_DIRECTORY & ffd.dwFileAttributes)
  74. {
  75. if (!BDeleteDirTree( strItem.c_str(), TRUE ))
  76. {
  77. FindClose( hItem );
  78. return FALSE;
  79. }
  80. }
  81. // Then a file, delete it
  82. else if (!DeleteFile( strItem.c_str() ))
  83. {
  84. FindClose( hItem );
  85. return FALSE;
  86. }
  87. }
  88. bFileFound = FindNextFile( hItem, &ffd );
  89. }
  90. // Close the find handle
  91. if(hItem && (INVALID_HANDLE_VALUE != hItem))
  92. FindClose( hItem );
  93. // Remove the present directory
  94. if (bDeleteInputDir)
  95. {
  96. if (!RemoveDirectory( szDir ))
  97. return FALSE;
  98. }
  99. return TRUE;
  100. }