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.

212 lines
5.4 KiB

  1. /*++
  2. Copyright (c) 2000-2001 Microsoft Corporation
  3. Module Name:
  4. direntrs.cpp
  5. Abstract:
  6. Implementation of the directory entries class. Given a path to a directory,
  7. creates two linked lists, one a list of all sub-directories (including
  8. mountpoints) and another a list of non-directories.
  9. Author:
  10. Stefan R. Steiner [ssteiner] 02-21-2000
  11. Revision History:
  12. --*/
  13. #include "stdafx.h"
  14. #include "direntrs.h"
  15. /*++
  16. Routine Description:
  17. Constructor for CDirectoryEntries.
  18. Arguments:
  19. pcDumpParameters - The command-line dump parameters block
  20. cwsDirPath - The path to the directory or file to get the directory
  21. entries for.
  22. Return Value:
  23. Can throw an exception. DWORD Win32 error only.
  24. --*/
  25. CDirectoryEntries::CDirectoryEntries(
  26. IN CDumpParameters *pcDumpParameters,
  27. IN const CBsString& cwsDirPath
  28. ) : m_pcParams( pcDumpParameters ),
  29. m_cwsDirPath( cwsDirPath )
  30. {
  31. DWORD dwRet;
  32. dwRet = GetDirectoryEntries();
  33. if ( dwRet != ERROR_SUCCESS )
  34. throw( dwRet );
  35. }
  36. /*++
  37. Routine Description:
  38. Destructor for the CDirectoryEntries class
  39. Arguments:
  40. NONE
  41. Return Value:
  42. NONE
  43. --*/
  44. CDirectoryEntries::~CDirectoryEntries()
  45. {
  46. SDirectoryEntry *pDE;
  47. //
  48. // Iterate through the sub-directory list and delete each element
  49. //
  50. CVssDLListIterator< SDirectoryEntry * > cDirListIter( m_cDirList );
  51. while( cDirListIter.GetNext( pDE ) )
  52. delete pDE;
  53. //
  54. // Iterate through the file list and delete each element
  55. //
  56. CVssDLListIterator< SDirectoryEntry * > cFileListIter( m_cFileList );
  57. while( cFileListIter.GetNext( pDE ) )
  58. delete pDE;
  59. }
  60. /*++
  61. Routine Description:
  62. Performs the actual retrieval of directory entries.
  63. Arguments:
  64. NONE
  65. Return Value:
  66. Any DWORD WIN32 error
  67. --*/
  68. DWORD
  69. CDirectoryEntries::GetDirectoryEntries()
  70. {
  71. DWORD dwRet = ERROR_SUCCESS;
  72. HANDLE hFind;
  73. try
  74. {
  75. WIN32_FIND_DATAW sFindData;
  76. //
  77. // Now enumerate the directory list
  78. //
  79. hFind = ::FindFirstFileEx(
  80. m_cwsDirPath,
  81. FindExInfoStandard,
  82. &sFindData,
  83. FindExSearchNameMatch,
  84. NULL,
  85. 0 );
  86. if ( hFind == INVALID_HANDLE_VALUE )
  87. {
  88. dwRet = ::GetLastError();
  89. if ( dwRet == ERROR_NO_MORE_FILES || dwRet == ERROR_FILE_NOT_FOUND )
  90. return 0;
  91. else
  92. {
  93. // Calling code will print out an error message if necessary
  94. return dwRet;
  95. }
  96. }
  97. //
  98. // Now run through the directory
  99. //
  100. do
  101. {
  102. // Check and make sure the file such as "." and ".." are not considered
  103. if( ::wcscmp( sFindData.cFileName, L".") != 0 &&
  104. ::wcscmp( sFindData.cFileName, L"..") != 0 )
  105. {
  106. SDirectoryEntry *psDirEntry;
  107. psDirEntry = new SDirectoryEntry;
  108. if ( psDirEntry == NULL )
  109. {
  110. dwRet = ::GetLastError();
  111. m_pcParams->ErrPrint( L"GetDirectoryEntries: dirPath: '%s', new() returned dwRet: %d", m_cwsDirPath.c_str(), dwRet );
  112. ::FindClose( hFind );
  113. return dwRet;
  114. }
  115. //
  116. // NOTE!! The following cast makes the assumption that WIN32_FILE_ATTRIBUTE_DATA
  117. // is a subset of WIN32_FIND_DATAW
  118. //
  119. psDirEntry->m_sFindData = *( WIN32_FILE_ATTRIBUTE_DATA * )&sFindData;
  120. psDirEntry->m_cwsFileName = sFindData.cFileName;
  121. //
  122. // Short name is empty if the file name is a conformant 8.3 name.
  123. //
  124. if ( sFindData.cAlternateFileName[0] != L'\0' )
  125. psDirEntry->m_cwsShortName = sFindData.cAlternateFileName;
  126. if ( psDirEntry->m_sFindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
  127. {
  128. //
  129. // Add to directory list
  130. //
  131. m_cDirList.AddTail( psDirEntry );
  132. }
  133. else
  134. {
  135. //
  136. // Add to file list
  137. //
  138. m_cFileList.AddTail( psDirEntry );
  139. }
  140. }
  141. } while ( ::FindNextFile( hFind, &sFindData ) );
  142. dwRet = ::GetLastError();
  143. if ( dwRet == ERROR_NO_MORE_FILES )
  144. dwRet = ERROR_SUCCESS;
  145. else
  146. m_pcParams->ErrPrint( L"GetDirectoryEntries: Got an unexpected error, FindNextFile('%s'), dwRet: %d", m_cwsDirPath.c_str(), dwRet );
  147. }
  148. catch ( DWORD dwRetThrown )
  149. {
  150. dwRet = dwRetThrown;
  151. m_pcParams->ErrPrint( L"GetDirectoryEntries: Caught an exception, dirPath: '%s', dwRet: %d", m_cwsDirPath.c_str(), dwRet );
  152. }
  153. catch ( ... )
  154. {
  155. dwRet = ::GetLastError();
  156. m_pcParams->ErrPrint( L"GetDirectoryEntries: Caught an unknown exception, dirPath: '%s'", m_cwsDirPath.c_str() );
  157. }
  158. ::FindClose( hFind );
  159. return dwRet;
  160. }