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.

139 lines
3.5 KiB

  1. /////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 2000-2002 Microsoft Corporation
  4. //
  5. // Module Name:
  6. // DirectoryUtils.cpp
  7. //
  8. // Description:
  9. // Useful functions for manipulating directies.
  10. //
  11. // Maintained By:
  12. // Galen Barbee (GalenB) 05-DEC-2000
  13. //
  14. // Revision History:
  15. //
  16. // Notes:
  17. //
  18. /////////////////////////////////////////////////////////////////////////////
  19. #include <windows.h>
  20. #include <objbase.h>
  21. #include <shlwapi.h>
  22. #include <ComCat.h>
  23. #include <wchar.h>
  24. #include <Dsgetdc.h>
  25. #include <Lm.h>
  26. #if !defined( THR )
  27. #define THR( _hr ) _hr
  28. #endif
  29. #if !defined( TW32 )
  30. #define TW32( _sc ) _sc
  31. #endif
  32. //////////////////////////////////////////////////////////////////////////////
  33. //++
  34. //
  35. // HRESULT
  36. // HrCreateDirectoryPath(
  37. // LPWSTR pszDirectoryPathInOut
  38. // )
  39. //
  40. // Descriptions:
  41. // Creates the directory tree as required.
  42. //
  43. // Arguments:
  44. // pszDirectoryPathOut
  45. // Must be MAX_PATH big. It will contain the trace log file path to
  46. // create.
  47. //
  48. // Return Values:
  49. // S_OK - Success
  50. // other HRESULTs for failures
  51. //
  52. //--
  53. //////////////////////////////////////////////////////////////////////////////
  54. HRESULT
  55. HrCreateDirectoryPath( LPWSTR pszDirectoryPath )
  56. {
  57. LPTSTR psz;
  58. BOOL fReturn;
  59. DWORD dwAttr;
  60. HRESULT hr = S_OK;
  61. //
  62. // Find the \ that indicates the root directory. There should be at least
  63. // one \, but if there isn't, we just fall through.
  64. //
  65. // skip X:\ part
  66. psz = wcschr( pszDirectoryPath, L'\\' );
  67. // Assert( psz != NULL );
  68. if ( psz != NULL )
  69. {
  70. //
  71. // Find the \ that indicates the end of the first level directory. It's
  72. // probable that there won't be another \, in which case we just fall
  73. // through to creating the entire path.
  74. //
  75. psz = wcschr( psz + 1, L'\\' );
  76. while ( psz != NULL )
  77. {
  78. // Terminate the directory path at the current level.
  79. *psz = 0;
  80. //
  81. // Create a directory at the current level.
  82. //
  83. dwAttr = GetFileAttributes( pszDirectoryPath );
  84. if ( 0xFFFFffff == dwAttr )
  85. {
  86. // DebugMsg( TEXT("DEBUG: Creating %ws"), pszDirectoryPath );
  87. fReturn = CreateDirectory( pszDirectoryPath, NULL );
  88. if ( ! fReturn )
  89. {
  90. hr = HRESULT_FROM_WIN32( TW32( GetLastError() ) );
  91. goto Error;
  92. } // if: creation failed
  93. } // if: directory not found
  94. else if ( ( dwAttr & FILE_ATTRIBUTE_DIRECTORY ) == 0 )
  95. {
  96. hr = THR( HRESULT_FROM_WIN32( ERROR_DIRECTORY ) );
  97. goto Error;
  98. } // else: file found
  99. //
  100. // Restore the \ and find the next one.
  101. //
  102. *psz = L'\\';
  103. psz = wcschr( psz + 1, L'\\' );
  104. } // while: found slash
  105. } // if: found slash
  106. //
  107. // Create the target directory.
  108. //
  109. dwAttr = GetFileAttributes( pszDirectoryPath );
  110. if ( 0xFFFFffff == dwAttr )
  111. {
  112. fReturn = CreateDirectory( pszDirectoryPath, NULL );
  113. if ( ! fReturn )
  114. {
  115. hr = THR( HRESULT_FROM_WIN32( GetLastError( ) ) );
  116. } // if: creation failed
  117. } // if: path not found
  118. Error:
  119. return hr;
  120. } //*** HrCreateDirectoryPath()