Source code of Windows XP (NT5)
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.

1094 lines
43 KiB

  1. /****************************************************************************************************************
  2. File Name: FatSubs.cpp
  3. COPYRIGHT 2001 Microsoft Corporation and Executive Software International, Inc.
  4. Description: FAT file system interfaces
  5. /***************************************************************************************************************/
  6. #include "stdafx.h"
  7. extern "C"{
  8. #include <stdio.h>
  9. }
  10. #ifdef BOOTIME
  11. #include "Offline.h"
  12. #else
  13. #include <Windows.h>
  14. #endif
  15. #include <winioctl.h>
  16. extern "C"{
  17. #include "SysStruc.h"
  18. }
  19. #include "DfrgCmn.h"
  20. #include "DfrgEngn.h"
  21. #include "DfrgRes.h"
  22. #include "ErrMacro.h"
  23. #include "DasdRead.h"
  24. #include "ErrMsg.h"
  25. #include "Alloc.h"
  26. #include "IntFuncs.h"
  27. #include "FatSubs.h"
  28. #include "DevIo.h"
  29. #include "FsSubs.h"
  30. #include "GetDfrgRes.h"
  31. #include "getreg.h"
  32. extern HWND hWndMain;
  33. #define BOOT_OPTIMIZE_REGISTRY_PATH TEXT("SOFTWARE\\Microsoft\\Dfrg\\BootOptimizeFunction")
  34. #define BOOT_OPTIMIZE_REGISTRY_LCNSTARTLOCATION TEXT("LcnStartLocation")
  35. #define BOOT_OPTIMIZE_REGISTRY_LCNENDLOCATION TEXT("LcnEndLocation")
  36. /*****************************************************************************************************************
  37. COPYRIGHT 2001 Microsoft Corporation and Executive Software International, Inc.
  38. ROUTINE DESCRIPTION:
  39. Gets the parameters of a volume and puts them into the VolData.
  40. INPUT + OUTPUT:
  41. None.
  42. GLOBALS:
  43. IN OUT Various VolData fields.
  44. RETURN:
  45. TRUE - Success.
  46. FALSE - Fatal Error.
  47. */
  48. BOOL
  49. GetFatVolumeStats(
  50. )
  51. {
  52. //TCHAR cVolume[8] = TEXT("\\\\.\\");
  53. DWORD SectorsPerCluster;
  54. DWORD BytesPerSector;
  55. DWORD FreeClusters;
  56. DWORD TotalNumberOfClusters;
  57. NTSTATUS Status;
  58. IO_STATUS_BLOCK IoStatus = {0};
  59. FILE_FS_SIZE_INFORMATION FsSizeBuf = {0};
  60. VolData.hVolume = INVALID_HANDLE_VALUE;
  61. VolData.hFile = INVALID_HANDLE_VALUE;
  62. #ifdef OFFLINEDK
  63. //0.1E00 Get the file system on this drive.
  64. EF(GetFileSystem(VolData.cVolumeName, &VolData.FileSystem));
  65. if(VolData.FileSystem == FS_FAT32){
  66. wprintf(L"\nError - FAT32 is not supported.\n"); // todo Is this still true?
  67. return FALSE;
  68. }
  69. #else
  70. //0.1E00 Get the file system on this drive.
  71. EF(GetFileSystem(VolData.cVolumeName, &VolData.FileSystem, VolData.cVolumeLabel));
  72. #endif
  73. //1.0E00 Check to see if this is anything other than FAT or FAT32, and if so, bail out.
  74. if(VolData.FileSystem != FS_FAT && VolData.FileSystem != FS_FAT32){
  75. return FALSE;
  76. }
  77. //0.0E00 Get handle to volume
  78. VolData.hVolume = CreateFile(
  79. VolData.cVolumeName,
  80. GENERIC_READ,
  81. FILE_SHARE_READ|FILE_SHARE_WRITE,
  82. NULL,
  83. OPEN_EXISTING,
  84. FILE_ATTRIBUTE_NORMAL|FILE_FLAG_NO_BUFFERING,
  85. NULL);
  86. EF_ASSERT(VolData.hVolume != INVALID_HANDLE_VALUE);
  87. //bug #120872 sks
  88. TCHAR tmpVolumeName[GUID_LENGTH+1];
  89. _stprintf(tmpVolumeName,TEXT("%s%s"),VolData.cVolumeName,TEXT("\\"));
  90. if(!GetDiskFreeSpace(
  91. tmpVolumeName,
  92. &SectorsPerCluster,
  93. &BytesPerSector,
  94. &FreeClusters,
  95. &TotalNumberOfClusters
  96. ))
  97. {
  98. EF_ASSERT(FALSE);
  99. }
  100. //0.0E00 Fill volume statistics structure
  101. VolData.BytesPerSector = BytesPerSector;
  102. VolData.SectorsPerCluster = SectorsPerCluster;
  103. VolData.TotalSectors = TotalNumberOfClusters * SectorsPerCluster;
  104. VolData.BytesPerCluster = BytesPerSector * SectorsPerCluster;
  105. VolData.TotalClusters = TotalNumberOfClusters;
  106. VolData.MftZoneStart = 0;
  107. VolData.MftZoneEnd = 0;
  108. VolData.bCompressed = FALSE;
  109. VolData.bFragmented = FALSE;
  110. VolData.UsedClusters = TotalNumberOfClusters - FreeClusters;
  111. VolData.CenterOfDisk = TotalNumberOfClusters / 2;
  112. //0.0E00 Compute number of bits needed for bitmap
  113. //bug 120782 SKS
  114. VolData.BitmapSize = (((VolData.TotalClusters /32) +
  115. ((VolData.TotalClusters % 32) ? 1 : 0)) + 1) * 32;
  116. VolData.BootOptimizeBeginClusterExclude = 0;
  117. VolData.BootOptimizeEndClusterExclude = 0;
  118. if (IsBootVolume(VolData.cDrive)) {
  119. //get the registry value for BootOptimizeBeginClusterExclude
  120. HKEY hValue = NULL;
  121. DWORD dwRegValueSize = 0;
  122. long ret = 0;
  123. TCHAR cRegValue[100];
  124. // get Boot Optimize Begin Cluster Exclude from registry
  125. dwRegValueSize = sizeof(cRegValue);
  126. ret = GetRegValue(
  127. &hValue,
  128. BOOT_OPTIMIZE_REGISTRY_PATH,
  129. BOOT_OPTIMIZE_REGISTRY_LCNSTARTLOCATION,
  130. cRegValue,
  131. &dwRegValueSize);
  132. RegCloseKey(hValue);
  133. //check to see if the key exists, else exit from routine
  134. if (ret == ERROR_SUCCESS) {
  135. VolData.BootOptimizeBeginClusterExclude = _ttoi(cRegValue);
  136. }
  137. // get Boot Optimize End Cluster Exclude from registry
  138. hValue = NULL;
  139. dwRegValueSize = sizeof(cRegValue);
  140. ret = GetRegValue(
  141. &hValue,
  142. BOOT_OPTIMIZE_REGISTRY_PATH,
  143. BOOT_OPTIMIZE_REGISTRY_LCNENDLOCATION,
  144. cRegValue,
  145. &dwRegValueSize);
  146. RegCloseKey(hValue);
  147. //check to see if the key exists, else exit from routine
  148. if (ret == ERROR_SUCCESS) {
  149. VolData.BootOptimizeEndClusterExclude = _ttoi(cRegValue);
  150. }
  151. }
  152. return TRUE;
  153. }
  154. /*****************************************************************************************************************
  155. COPYRIGHT 2001 Microsoft Corporation and Executive Software International, Inc.
  156. ROUTINE DESCRIPTION:
  157. Gets the extent list for a file.
  158. DATA STRUCTURES:
  159. //0.1E00 The following is the format for each retrieval pointer in the retrieval pointer buffer.
  160. //0.1E00 See winioctl.h RETRIEVAL_POINTERS_BUFFER for complete definition.
  161. //struct {
  162. // LARGE_INTEGER NextVcn; //Vcn for *next* extent.
  163. // LARGE_INTEGER Lcn; //Lcn for this extent.
  164. /};
  165. GLOBAL VARIABLES:
  166. INPUT:
  167. TypeCode - Contains a value of $DATA or $INDEX_ALLOCATION depending on whether this file is a data file or a directory.
  168. NOTE - TYPECODE IS NO LONGER USED!!! But it is left in so as to not invalidate
  169. old calls to GetExtentList.
  170. RETURN:
  171. TRUE - Success.
  172. FALSE - Fatal Error.
  173. */
  174. BOOL
  175. GetExtentList(
  176. DWORD dwEnabledStreams,
  177. FILE_RECORD_SEGMENT_HEADER* pFrs
  178. )
  179. {
  180. STARTING_VCN_INPUT_BUFFER StartingVcnInputBuffer;
  181. HANDLE hRetrievalPointersBuffer = NULL;
  182. PRETRIEVAL_POINTERS_BUFFER pRetrievalPointersBuffer = NULL;
  183. PLARGE_INTEGER pRetrievalPointers = NULL;
  184. LONGLONG Extent;
  185. LONGLONG Vcn = 0;
  186. ULONG BytesReturned = 0;
  187. EXTENT_LIST_DATA ExtentData;
  188. ULONG RetrievalPointers = 0x100;
  189. EXTENT_LIST* pExtentList = NULL;
  190. //Set up the Extent pointers structure to fill in the extent list in VolData.
  191. ZeroMemory(&ExtentData, sizeof(EXTENT_LIST_DATA));
  192. ExtentData.hExtents = VolData.hExtentList;
  193. ExtentData.pExtents = VolData.pExtentList;
  194. ExtentData.ExtentListAlloced = (DWORD)VolData.ExtentListAlloced;
  195. ExtentData.ExtentListSize = 0;
  196. ExtentData.dwEnabledStreams = dwEnabledStreams;
  197. ExtentData.BytesRead = 0;
  198. ExtentData.TotalClusters = 0;
  199. //Note that on FAT and FAT32, files may only have either one or no streams.
  200. //Initialize the FILE_EXTENT_HEADER structure.
  201. ExtentData.pFileExtentHeader = (FILE_EXTENT_HEADER*)ExtentData.pExtents;
  202. ExtentData.pFileExtentHeader->FileRecordNumber = 0; //There is no FileRecordNumber on FAT.
  203. ExtentData.pFileExtentHeader->NumberOfStreams = 0; //Contains the number of streams with extents.
  204. ExtentData.pFileExtentHeader->TotalNumberOfStreams = 0; //Contains the total number of streams.
  205. ExtentData.pFileExtentHeader->ExcessExtents = 0;
  206. //Initialize the FILE_STREAM_HEADER structure.
  207. ExtentData.pStreamExtentHeader = (STREAM_EXTENT_HEADER*)((char*)VolData.pExtentList + sizeof(FILE_EXTENT_HEADER));
  208. ExtentData.pStreamExtentHeader->StreamNumber = 0;
  209. ExtentData.pStreamExtentHeader->ExtentCount = 0;
  210. ExtentData.pStreamExtentHeader->ExcessExtents = 0;
  211. ExtentData.pStreamExtentHeader->AllocatedLength = 0;
  212. ExtentData.pStreamExtentHeader->FileSize = 0;
  213. //Initialize the pointer to the extent list itself.
  214. pExtentList = (EXTENT_LIST*)((char*)ExtentData.pStreamExtentHeader + sizeof(STREAM_EXTENT_HEADER));
  215. //Initialize the VolData fields.
  216. //The file size is predetermined by the time this routine is called. Therefore, we
  217. VolData.bFragmented = FALSE;
  218. VolData.bCompressed = FALSE;
  219. VolData.NumberOfClusters = 0;
  220. VolData.NumberOfRealClusters = 0;
  221. VolData.NumberOfFragments = 0;
  222. VolData.FileSize = 0;
  223. __try{
  224. ZeroMemory(&StartingVcnInputBuffer, sizeof(STARTING_VCN_INPUT_BUFFER));
  225. //0.1E00 Read the retrieval pointers into a buffer in memory.
  226. while(TRUE){
  227. //0.0E00 Allocate a RetrievalPointersBuffer.
  228. EF(AllocateMemory(sizeof(RETRIEVAL_POINTERS_BUFFER) + (RetrievalPointers * 2 * sizeof(LARGE_INTEGER)),
  229. &hRetrievalPointersBuffer,
  230. (void**)(PCHAR*)&pRetrievalPointersBuffer));
  231. //0.0E00 Get extent list / determine extent count.
  232. if(ESDeviceIoControl(VolData.hFile,
  233. FSCTL_GET_RETRIEVAL_POINTERS,
  234. &StartingVcnInputBuffer,
  235. sizeof(STARTING_VCN_INPUT_BUFFER),
  236. pRetrievalPointersBuffer,
  237. (DWORD)GlobalSize(hRetrievalPointersBuffer),
  238. &BytesReturned,
  239. NULL)){
  240. break;
  241. }
  242. //This occurs on a zero length file (no clusters allocated).
  243. if(GetLastError() == ERROR_HANDLE_EOF){
  244. //Clean out the extent list.
  245. ZeroMemory(VolData.pExtentList, (DWORD)VolData.ExtentListAlloced);
  246. break;
  247. }
  248. //0.0E00 Check to see if the error is not because the buffer is too small.
  249. EF(GetLastError() == ERROR_MORE_DATA);
  250. //0.1E00 Double the buffer size until it's large enough to hold the file's extent list.
  251. RetrievalPointers *= 2;
  252. }
  253. //0.0E00 Check to ensure that extent list starts at the begining.
  254. if(pRetrievalPointersBuffer->StartingVcn.QuadPart != 0) {
  255. TCHAR cExtentVcn[64];
  256. SetLastError(0x20000001);
  257. wsprintf(cExtentVcn, TEXT("%d"), pRetrievalPointersBuffer->StartingVcn.QuadPart);
  258. Message(TEXT("GetExtentList - pRetrievalPointersBuffer->StartingVcn.QuadPart"),
  259. 0x20000001,
  260. cExtentVcn);
  261. return FALSE;
  262. }
  263. //0.1E00 Store a pointer to the retrieval pointers within the retrieval pointer buffer.
  264. pRetrievalPointers = (PLARGE_INTEGER)&pRetrievalPointersBuffer->Extents;
  265. //If there aren't any extents in this file, then we're done.
  266. if(!pRetrievalPointersBuffer->ExtentCount){
  267. return TRUE;
  268. }
  269. ExtentData.pFileExtentHeader->NumberOfStreams = 1;
  270. ExtentData.pFileExtentHeader->TotalNumberOfStreams =1;
  271. ExtentData.pFileExtentHeader->ExcessExtents = pRetrievalPointersBuffer->ExtentCount - 1;
  272. ExtentData.pStreamExtentHeader->ExtentCount = pRetrievalPointersBuffer->ExtentCount;
  273. ExtentData.pStreamExtentHeader->ExcessExtents = pRetrievalPointersBuffer->ExtentCount - 1;
  274. /**/
  275. // Extend the extent list buffer as necessary to hold that many extents.
  276. if(ExtentData.pStreamExtentHeader->ExtentCount * sizeof(EXTENT_LIST) > (size_t) VolData.ExtentListAlloced) {
  277. // Allocate to the new size.
  278. EF(AllocateMemory((DWORD)(ExtentData.pStreamExtentHeader->ExtentCount * sizeof(EXTENT_LIST)*2),
  279. &VolData.hExtentList,
  280. (void**)&VolData.pExtentList));
  281. // Update our pointers.
  282. ExtentData.hExtents = VolData.hExtentList;
  283. ExtentData.pExtents = VolData.pExtentList;
  284. ExtentData.pFileExtentHeader = (FILE_EXTENT_HEADER*)ExtentData.pExtents;
  285. ExtentData.pStreamExtentHeader = (STREAM_EXTENT_HEADER*)((char*)VolData.pExtentList + sizeof(FILE_EXTENT_HEADER));
  286. pExtentList = (EXTENT_LIST*)((char*)ExtentData.pStreamExtentHeader + sizeof(STREAM_EXTENT_HEADER));
  287. VolData.ExtentListAlloced = ExtentData.pStreamExtentHeader->ExtentCount * sizeof(EXTENT_LIST);
  288. // pExtentList = (EXTENT_LIST*)VolData.pExtentList;
  289. }
  290. /**/
  291. /** /
  292. //0.0E00 Extend the extent list buffer as necessary to hold that many extents.
  293. if(VolData.NumberOfExtents*sizeof(EXTENT_LIST) > VolData.ExtentListAlloced){
  294. //0.1E00 Allocate to the new size.
  295. EF(AllocateMemory((DWORD)(VolData.NumberOfExtents * sizeof(EXTENT_LIST)), &VolData.hExtentList, (void**)&VolData.pExtentList));
  296. VolData.ExtentListAlloced = VolData.NumberOfExtents * sizeof(EXTENT_LIST);
  297. //0.1E00 Update our pointer to the buffer.
  298. pExtentList = VolData.pExtentList;
  299. }
  300. /**/
  301. //0.0E00 Convert retrieval pointers into an extent list
  302. for(Extent = 0; Extent < pRetrievalPointersBuffer->ExtentCount; Extent ++){
  303. //0.1E00 Pull the starting lcn out of the retrieval pointer.
  304. pExtentList[Extent].StartingLcn = pRetrievalPointers[1].QuadPart;
  305. //0.1E00 The cluster count is the starting Vcn of the next retrieval pointer minus the Vcn we're at. That is, if the last extent was at
  306. //Vcn 10, and the next extent starts at Vcn 20, then the extent must be 10 clusters long.
  307. pExtentList[Extent].ClusterCount = pRetrievalPointers[0].QuadPart - Vcn;
  308. //0.1E00 Keep a running total of the number of clusters in this file.
  309. VolData.NumberOfClusters += pExtentList[Extent].ClusterCount;
  310. VolData.NumberOfRealClusters = VolData.NumberOfClusters;
  311. //0.1E00 The Vcn can be read directly out of the retrieval pointer.
  312. Vcn = pRetrievalPointers[0].QuadPart;
  313. //0.1E00 Go to the next retrieval pointer (2 LARGE_INTEGERS per retrieval pointer).
  314. pRetrievalPointers += 2;
  315. }
  316. //0.0E00 Put extent list handle and number of extents into volume's structure.
  317. VolData.NumberOfFragments = pRetrievalPointersBuffer->ExtentCount;
  318. VolData.StartingLcn = pExtentList->StartingLcn;
  319. //Get the file's size.
  320. if(VolData.bDirectory){
  321. //GetFileSize returns zero bytes for directories.
  322. VolData.FileSize = VolData.NumberOfClusters * VolData.BytesPerCluster;
  323. }
  324. else{
  325. EF(OpenFatFile());
  326. EF_ASSERT((VolData.FileSize = GetFileSize(VolData.hFile, NULL)) != 0xFFFFFFFF);
  327. }
  328. ExtentData.pStreamExtentHeader->FileSize = VolData.FileSize;
  329. ExtentData.pStreamExtentHeader->AllocatedLength = VolData.NumberOfClusters * VolData.BytesPerCluster;
  330. //0.1E00 Make sure the extent list doesn't list extents that are actually contiguous, as sometimes happens.
  331. // Also, eliminate extents defining virtual cluster counts for fragmented files.
  332. EF(CollapseExtentList(&ExtentData));
  333. return TRUE;
  334. }
  335. __finally{
  336. if(hRetrievalPointersBuffer){
  337. EH_ASSERT(GlobalUnlock(hRetrievalPointersBuffer) == FALSE);
  338. EH_ASSERT(GlobalFree(hRetrievalPointersBuffer) == NULL);
  339. }
  340. }
  341. return TRUE;
  342. }
  343. /*****************************************************************************************************************
  344. COPYRIGHT 2001 Microsoft Corporation and Executive Software International, Inc.
  345. ROUTINE DESCRIPTION:
  346. Gets the extent list for a file by manually traversing the FAT chain for that file. I load
  347. in the appropriate chunks of the FAT on the fly rather than load the whole FAT into memory, since the
  348. whole FAT could be quite large.
  349. This function doesn't replace GetExtentList() above for FAT since this requires that you know the StartingLcn
  350. of the file. GetExtentList() requires the file name.
  351. INPUT + OUTPUT:
  352. None.
  353. GLOBALS:
  354. IN VolData.StartingLcn - The first LCN of the file.
  355. OUT VolData.NumberOfExtents - The number of extents in the extent list for this file.
  356. OUT VolData.NumberOfFragments - Same as NumberOfExtents on FAT and FAT32 (not on NTFS).
  357. OUT VolData.NumberOfClusters - The number of clusters allocated for this file.
  358. OUT VolData.hExtentList - Handle to the extent list I allocate an fill in here.
  359. OUT VolData.pExtentList - The pointer.
  360. RETURN:
  361. TRUE - Success.
  362. FALSE - Fatal Error.
  363. */
  364. //0.0E00 This may not be the ideal number. This will use a maximum of 32K on even the largest FAT drive
  365. //since that's the largest cluster size. I didn't see any performance gain on my computer by reading
  366. //multiple clusters. So I saved memory instead.
  367. #define CLUSTERS_PER_FAT_CHUNK 1
  368. BOOL
  369. GetExtentListManuallyFat(
  370. )
  371. {
  372. LONGLONG Cluster = 0;
  373. DWORD FatChunkNum = 0;
  374. DWORD LastFatChunkNum = 0;
  375. DWORD EntriesPerChunk = 0;
  376. HANDLE hFat = NULL;
  377. BYTE* pFat = NULL;
  378. EXTENT_LIST* pExtentList = NULL;
  379. DWORD Extent = 0;
  380. USHORT uEntry = 0;
  381. DWORD dwEntry = 0;
  382. LONGLONG llNumOfClusters = 0;
  383. EXTENT_LIST_DATA ExtentData;
  384. DWORD NumberOfExtents = 0;
  385. //Set up the Extent pointers structure to fill in the extent list in VolData.
  386. ZeroMemory(&ExtentData, sizeof(EXTENT_LIST_DATA));
  387. ExtentData.hExtents = VolData.hExtentList;
  388. ExtentData.pExtents = VolData.pExtentList;
  389. ExtentData.ExtentListAlloced = (DWORD)VolData.ExtentListAlloced;
  390. ExtentData.ExtentListSize = 0;
  391. ExtentData.dwEnabledStreams = FALSE;
  392. ExtentData.BytesRead = 0;
  393. ExtentData.TotalClusters = 0;
  394. //Note that on FAT and FAT32, files may only have either one or no streams.
  395. //Initialize the FILE_EXTENT_HEADER structure.
  396. ExtentData.pFileExtentHeader = (FILE_EXTENT_HEADER*)ExtentData.pExtents;
  397. ExtentData.pFileExtentHeader->FileRecordNumber = 0; //There is no FileRecordNumber on FAT.
  398. ExtentData.pFileExtentHeader->NumberOfStreams = 0; //Contains the number of streams with extents.
  399. ExtentData.pFileExtentHeader->TotalNumberOfStreams = 0; //Contains the total number of streams.
  400. ExtentData.pFileExtentHeader->ExcessExtents = 0;
  401. //Initialize the FILE_STREAM_HEADER structure.
  402. ExtentData.pStreamExtentHeader = (STREAM_EXTENT_HEADER*)((char*)VolData.pExtentList + sizeof(FILE_EXTENT_HEADER));
  403. ExtentData.pStreamExtentHeader->StreamNumber = 0;
  404. ExtentData.pStreamExtentHeader->ExtentCount = 0;
  405. ExtentData.pStreamExtentHeader->ExcessExtents = 0;
  406. ExtentData.pStreamExtentHeader->AllocatedLength = 0;
  407. ExtentData.pStreamExtentHeader->FileSize = 0;
  408. //Initialize the pointer to the extent list itself.
  409. pExtentList = (EXTENT_LIST*)((char*)ExtentData.pStreamExtentHeader + sizeof(STREAM_EXTENT_HEADER));
  410. //0.0E00 Initialize output variables.
  411. VolData.bFragmented = FALSE;
  412. VolData.bCompressed = FALSE;
  413. VolData.NumberOfFragments = 0;
  414. VolData.NumberOfClusters = 0;
  415. VolData.NumberOfRealClusters = 0;
  416. //If this file is a small file -- it has an invalid starting Lcn number to indicate there are no
  417. //allocated clusters. Don't scan the FAT for clusters.
  418. if(VolData.StartingLcn == 0xFFFFFFFF){
  419. goto SmallFile;
  420. }
  421. //0.0E00 Note the number of FAT entries in CLUSTERS_PER_FAT_CHUNK clusters of the FAT.
  422. //0.0E00 We will be reading in CLUSTERS_PER_FAT_CHUNK clusters of the FAT at a time.
  423. switch(VolData.FileSystem){
  424. case FS_FAT:
  425. EntriesPerChunk = (DWORD)(CLUSTERS_PER_FAT_CHUNK * VolData.BytesPerCluster / 2); //2 bytes per entry.
  426. break;
  427. case FS_FAT32:
  428. EntriesPerChunk = (DWORD)(CLUSTERS_PER_FAT_CHUNK * VolData.BytesPerCluster / 4); //4 bytes per entry.
  429. break;
  430. default:
  431. EF(FALSE);
  432. }
  433. EF_ASSERT(EntriesPerChunk);
  434. //0.0E00 Begin with the assumption that the number of extents for this file is 1, I will can increment it as I find more.
  435. NumberOfExtents = 1;
  436. //0.0E00 Count the file's extents by counting non-adjacent clusters
  437. //0.0E00 Loop through each Lcn of the file -- ending at the terminating cluster number.
  438. //+2 because Lcn 0 is offset 2 in the FAT.
  439. for(Cluster = (DWORD)VolData.StartingLcn+2; TRUE; ){
  440. //0.0E00 Figure out which chunk of the FAT to read in to look at the next entry.
  441. FatChunkNum = (DWORD)(Cluster / EntriesPerChunk); //Zero based number.
  442. //0.0E00 Check to see if I've already loaded this chunk. If not, or if I haven't yet read in any chunk, read in the chunk.
  443. if((FatChunkNum != LastFatChunkNum) || (!hFat)){
  444. //0.0E00 Free up the previous chunk.
  445. if(hFat){
  446. while(GlobalUnlock(hFat))
  447. ;
  448. EH_ASSERT(GlobalFree(hFat) == NULL);
  449. }
  450. //0.0E00 Read in the new chunk.
  451. EF((hFat = DasdLoadSectors(VolData.hVolume,
  452. ((DWORD)(VolData.FatOffset)/VolData.BytesPerSector) //VolData.FatOffset contains the byte offset
  453. //of the FAT on the drive.
  454. + (FatChunkNum * CLUSTERS_PER_FAT_CHUNK * VolData.SectorsPerCluster), //Which chunk in the FAT to read.
  455. CLUSTERS_PER_FAT_CHUNK * VolData.SectorsPerCluster, //Read one chunk of the FAT.
  456. VolData.BytesPerSector)) != INVALID_HANDLE_VALUE);
  457. EF((pFat = (BYTE*)GlobalLock(hFat)) != NULL);
  458. LastFatChunkNum = FatChunkNum;
  459. }
  460. //0.0E00 Filter out the correct entry from the FAT.
  461. //0.0E00 Compare that entry with the next to see if it is adjacent or not, and if not increment the extent count.
  462. llNumOfClusters++;
  463. if(VolData.FileSystem == FS_FAT){
  464. //0.0E00 First use a USHORT* since this is a 16 bit FAT, then extract the correct entry from the FAT which is
  465. //the modulus of the cluster number (how far into the chunk I look).
  466. uEntry = ((USHORT*)pFat)[Cluster%EntriesPerChunk];
  467. //0.0E00 If that entry is zero, then this is a small file (no allocated clusters
  468. if(uEntry == 0){
  469. NumberOfExtents = 0;
  470. //0.0E00 Can all you programmers ever forgive me for using a goto?
  471. goto SmallFile;
  472. }
  473. //0.0E00 If that entry points to the next contiguous entry, then this is not a new fragment.
  474. if(uEntry == Cluster + 1){
  475. //0.0E00 Go on to the next cluster.
  476. Cluster++;
  477. continue;
  478. }
  479. //0.0E00 Otherwise, check to see if this is the terminator:
  480. if((0xFFF8 <= uEntry) && (uEntry <= 0xFFFF)){
  481. //0.0E00 If so, we're done looking for fragments.
  482. break;
  483. }
  484. //0.0E00 Otherwise, this is a new fragment, so increment the count.
  485. Cluster = uEntry;
  486. NumberOfExtents++;
  487. }
  488. else{
  489. //0.0E00 Same as above only with DWORD for a 32 bit FAT.
  490. //0.0E00 Also, mask out the top four bits per the FAT32 spec -- They are undefined and not part of the number.
  491. dwEntry = ((DWORD*)pFat)[Cluster%EntriesPerChunk] & 0x0FFFFFFF;
  492. if(dwEntry == 0){
  493. NumberOfExtents = 0;
  494. //0.0E00 Can all you programmers ever forgive me for using a goto?
  495. goto SmallFile;
  496. }
  497. if(dwEntry == Cluster + 1){
  498. Cluster++;
  499. continue;
  500. }
  501. //0.0E00 The terminator on FAT32 can be any number in this range.
  502. if((0x0FFFFFF8 <= dwEntry) && (dwEntry <= 0x0FFFFFFF)){
  503. break;
  504. }
  505. Cluster = dwEntry;
  506. NumberOfExtents++;
  507. }
  508. }
  509. // //0.0E00 Extend the extent list buffer as necessary to hold that many extents.
  510. // if(NumberOfExtents*sizeof(EXTENT_LIST) > VolData.ExtentListBytes){
  511. // //0.1E00 Allocate to the new size.
  512. // EF(AllocateMemory((DWORD)(NumberOfExtents * sizeof(EXTENT_LIST)), &VolData.hExtentList, (void**)&VolData.pExtentList));
  513. // VolData.ExtentListBytes = NumberOfExtents * sizeof(EXTENT_LIST);
  514. // }
  515. ExtentData.pFileExtentHeader->NumberOfStreams = 1;
  516. ExtentData.pFileExtentHeader->TotalNumberOfStreams =1;
  517. ExtentData.pFileExtentHeader->ExcessExtents = NumberOfExtents - 1;
  518. ExtentData.pStreamExtentHeader->ExtentCount = NumberOfExtents;
  519. ExtentData.pStreamExtentHeader->ExcessExtents = NumberOfExtents - 1;
  520. Extent = 0;
  521. //0.0E00 Note the starting lcn of the first extent (i.e. beginning of file).
  522. pExtentList->StartingLcn = VolData.StartingLcn;
  523. //0.0E00 Go though the FAT chain again, now filling in the extents. End at the terminating cluster number.
  524. for(Cluster = (DWORD)VolData.StartingLcn+2; TRUE; ){
  525. //0.0E00 Figure out which chunk of the FAT to read in to look at the next entry.
  526. FatChunkNum = (DWORD)(Cluster / EntriesPerChunk); //Zero based number.
  527. //0.0E00 Check to see if I've already loaded this chunk. If not, read in the chunk.
  528. //0.0E00 I don't have to check to see if I haven't loaded any chunk here, because one was loaded in the last loop.
  529. if(FatChunkNum != LastFatChunkNum){
  530. //0.0E00 Free up the previous chunk.
  531. if(hFat){
  532. while (GlobalUnlock(hFat))
  533. ;
  534. EH_ASSERT(GlobalFree(hFat) == NULL);
  535. }
  536. //0.0E00 Read in the new chunk.
  537. EF((hFat = DasdLoadSectors(VolData.hVolume,
  538. ((DWORD)(VolData.FatOffset)/VolData.BytesPerSector) //VolData.FatOffset contains the byte offset
  539. //of the FAT on the drive.
  540. + (FatChunkNum * CLUSTERS_PER_FAT_CHUNK * VolData.SectorsPerCluster), //Which chunk in the FAT to read.
  541. CLUSTERS_PER_FAT_CHUNK * VolData.SectorsPerCluster, //Read one chunk of the FAT.
  542. VolData.BytesPerSector)) != INVALID_HANDLE_VALUE);
  543. EF((pFat = (BYTE*)GlobalLock(hFat)) != NULL);
  544. LastFatChunkNum = FatChunkNum;
  545. }
  546. //0.0E00 Filter out the correct entry from the FAT.
  547. //0.0E00 Compare that entry with the next to see if it is adjacent or not, and if not increment the extent count.
  548. if(VolData.FileSystem == FS_FAT){
  549. //0.0E00 First use a USHORT* since this is a 16 bit FAT, then extract the correct entry from the FAT which is
  550. //0.0E00 the modulus of the cluster number (how far into the chunk I look).
  551. uEntry = ((USHORT*)pFat)[Cluster%EntriesPerChunk];
  552. //0.0E00 If that entry points to the next contiguous entry, then this is not a new fragment.
  553. if(uEntry == Cluster + 1){
  554. //0.0E00 Go on to the next cluster.
  555. Cluster++;
  556. continue;
  557. }
  558. //0.0E00 Otherwise, check to see if this is the terminator:
  559. if((0xFFF8 <= uEntry) && (uEntry <= 0xFFFF)){
  560. //0.0E00 If so, we're done looking for fragments.
  561. break;
  562. }
  563. //0.0E00 Otherwise, this is a new fragment, so...
  564. // 1) Update the clustercount for the last extent.
  565. //The cluster count is the total number of clusters between the last Lcn and the current position.
  566. pExtentList[Extent].ClusterCount = Cluster + 1 - (pExtentList[Extent].StartingLcn+2);
  567. // 2) Set the Lcn for the next extent.
  568. pExtentList[++Extent].StartingLcn = uEntry - 2;
  569. // 3) Update the cluster value to the new starting extent
  570. Cluster = uEntry;
  571. }
  572. else{
  573. //0.0E00 Same as above only with DWORD for a 32 bit FAT.
  574. //0.0E00 Also, mask out the top four bits per the FAT32 spec -- They are undefined and not part of the number.
  575. dwEntry = ((DWORD*)pFat)[Cluster%EntriesPerChunk] & 0x0FFFFFFF;
  576. if(dwEntry == Cluster + 1){
  577. Cluster++;
  578. continue;
  579. }
  580. //0.0E00 The terminator on FAT32 can be any number in this range.
  581. if((0x0FFFFFF8 <= dwEntry) && (dwEntry <= 0x0FFFFFFF)){
  582. break;
  583. }
  584. pExtentList[Extent].ClusterCount = Cluster + 1 - (pExtentList[Extent].StartingLcn+2);
  585. pExtentList[++Extent].StartingLcn = dwEntry - 2;
  586. Cluster = dwEntry;
  587. }
  588. }
  589. //0.0E00 Set the clustercount for the last extent.
  590. pExtentList[Extent].ClusterCount = Cluster + 1 - (pExtentList[Extent].StartingLcn+2);
  591. //If the first FAT loop detects that this is a small file, it will immediately jump here.
  592. SmallFile:
  593. //If there are no extents, then zero out the extent list.
  594. if(NumberOfExtents == 0){
  595. ZeroMemory(VolData.pExtentList, (DWORD)VolData.ExtentListAlloced);
  596. VolData.FileSize = 0;
  597. }
  598. //0.0E00 Record the number of fragments in the file.
  599. VolData.NumberOfFragments = NumberOfExtents;
  600. //Note the number of clusters in the file.
  601. VolData.NumberOfRealClusters = VolData.NumberOfClusters = llNumOfClusters;
  602. //0.0E00 If this is a directory, round it's filesize up to the nearest cluster.
  603. if(VolData.bDirectory){
  604. VolData.FileSize = VolData.NumberOfClusters * VolData.BytesPerCluster;
  605. }
  606. //0.1E00 If there is only one fragments, then the file is contiguous, otherwise it is fragmented.
  607. if (VolData.NumberOfFragments < 2) {
  608. VolData.bFragmented = FALSE;
  609. }
  610. else {
  611. VolData.bFragmented = TRUE;
  612. }
  613. //0.0E00 Free up the FAT chunk.
  614. if(hFat){
  615. while(GlobalUnlock(hFat))
  616. ;
  617. EH_ASSERT(GlobalFree(hFat) == NULL);
  618. }
  619. //Too Cool! :-)
  620. return TRUE;
  621. }
  622. /*****************************************************************************************************************
  623. COPYRIGHT 2001 Microsoft Corporation and Executive Software International, Inc.
  624. ROUTINE DESCRIPTION:
  625. Opens a file on a FAT volume.
  626. INPUT + OUTPUT:
  627. None.
  628. GLOBALS:
  629. IN OUT Various VolData fields.
  630. RETURN:
  631. TRUE - Success.
  632. FALSE - Fatal Error.
  633. */
  634. BOOL
  635. OpenFatFile(
  636. )
  637. {
  638. DWORD_AND_LONGLONG FunkyFileSize;
  639. DWORD Error;
  640. if(VolData.hFile != INVALID_HANDLE_VALUE){
  641. CloseHandle(VolData.hFile);
  642. VolData.hFile = INVALID_HANDLE_VALUE;
  643. }
  644. if (VolData.vFileName.IsEmpty()) {
  645. return FALSE;
  646. }
  647. //0.0E00 Get a transparent handle to the file.
  648. if((VolData.hFile = CreateFile((LPTSTR)VolData.vFileName.GetBuffer(),
  649. FILE_READ_ATTRIBUTES | SYNCHRONIZE, //sks bug #184735
  650. 0,
  651. NULL,
  652. OPEN_EXISTING,
  653. (!VolData.bDirectory) ? FILE_ATTRIBUTE_NORMAL : FILE_FLAG_BACKUP_SEMANTICS,
  654. NULL)) == INVALID_HANDLE_VALUE) {
  655. //0.0E00 Try later.
  656. Error = GetLastError();
  657. Message(TEXT("OpenFatFile - CreateFile"), GetLastError(), ESICompressFilePath(VolData.vFileName.GetBuffer()));
  658. SetLastError(Error);
  659. return FALSE;
  660. }
  661. #ifndef OFFLINEDK
  662. FunkyFileSize.Dword.Low = GetFileSize(VolData.hFile, &FunkyFileSize.Dword.High);
  663. VolData.FileSize = FunkyFileSize.LongLong;
  664. VolData.NumberOfFragments = (VolData.FileSize == 0) ? 0 : 1;
  665. #endif
  666. return TRUE;
  667. }
  668. /*****************************************************************************************************************
  669. COPYRIGHT 2001 Microsoft Corporation and Executive Software International, Inc.
  670. ROUTINE DESCRIPTION:
  671. Gets the next Fat file from the moveable file list.
  672. INPUT:
  673. dwMoveFlags - MOVE_FRAGMENTED - moves fragmented files.
  674. - MOVE_CONTIGUOUS - moves contiguous files.
  675. OUTPUT:
  676. None.
  677. GLOBALS:
  678. IN OUT Various VolData fields.
  679. RETURN:
  680. TRUE - File Found.
  681. FALSE - No file found.
  682. */
  683. BOOL
  684. GetNextFatFile(
  685. DWORD dwMoveFlags
  686. )
  687. {
  688. FILE_LIST_ENTRY* pFileListEntry = NULL;
  689. LONGLONG NextLcn;
  690. LONGLONG LastStartingLcn;
  691. LONGLONG LowestIndex;
  692. LONGLONG TempStartingLcn;
  693. LONGLONG i;
  694. BOOL bMoveContiguous = (dwMoveFlags & MOVE_CONTIGUOUS) ? TRUE : FALSE;
  695. BOOL bMoveFragmented = (dwMoveFlags & MOVE_FRAGMENTED) ? TRUE : FALSE;
  696. // This is here because if a file is deleted, then we
  697. // want to call this function again until we either
  698. // hit the end of the list, or get a file.
  699. Top_Of_GetNextFatFile:
  700. // Here is the function initialization.
  701. pFileListEntry = NULL;
  702. NextLcn = 0xFFFFFFFFFFFFFFFF;
  703. LastStartingLcn = VolData.LastStartingLcn;
  704. LowestIndex = 0xFFFFFFFFFFFFFFFF;
  705. TempStartingLcn = 0;
  706. i = 0;
  707. // The NextLcn specifies which Lcn is the next
  708. // one to write to, and it's location therefore
  709. // depends on which direction this pass is going.
  710. NextLcn = (VolData.ProcessFilesDirection == FORWARD) ? VolData.SourceEndLcn : VolData.SourceStartLcn;
  711. // Get a pointer to the file list. We do
  712. // this for the performance benefit of
  713. // losing the indirection via VolData.
  714. pFileListEntry = VolData.pMoveableFileList;
  715. // Find the next file in the list
  716. for(i = 0; i < VolData.MoveableFileListEntries; i ++, pFileListEntry++) {
  717. // If we've hit the end of filled in data
  718. // in the file list, we're done checking this list.
  719. if(pFileListEntry->FileRecordNumber == 0) {
  720. break;
  721. }
  722. // If this file is set to not be moved - next.
  723. // If we're not supposed to move contiguous files on this pass and this file is contiguous - next.
  724. // If we're not supposed to move fragmented files on this pass and this file is fragmented - next.
  725. if((pFileListEntry->Flags & (FLE_NEXT_PASS | FLE_DISABLED))
  726. || (!bMoveContiguous && !(pFileListEntry->Flags & FLE_FRAGMENTED))
  727. || (!bMoveFragmented && pFileListEntry->Flags & FLE_FRAGMENTED)) {
  728. continue;
  729. }
  730. // Don't move directories on FAT or FAT32 since the hooks will not move the first cluster.
  731. if(pFileListEntry->Flags & FLE_DIRECTORY) {
  732. Message(L"Cannot defrag directories on FAT or FAT32 volumes.", -1, ESICompressFilePath(VolData.vFileName.GetBuffer()));
  733. continue;
  734. }
  735. // Offload the StartingLcn into a memory variable for faster access.
  736. TempStartingLcn = pFileListEntry->StartingLcn;
  737. // See if this file is the next in sequence
  738. if( ( (VolData.ProcessFilesDirection == FORWARD) &&
  739. (TempStartingLcn > LastStartingLcn) &&
  740. (TempStartingLcn < NextLcn) )
  741. ||
  742. ( (VolData.ProcessFilesDirection == BACKWARD) &&
  743. (TempStartingLcn < LastStartingLcn) &&
  744. (TempStartingLcn > NextLcn) ) ){
  745. NextLcn = TempStartingLcn;
  746. LowestIndex = i;
  747. }
  748. }
  749. // If LowestIndex still is -1, then that means we didn't find any files.
  750. if(LowestIndex == 0xFFFFFFFFFFFFFFFF){
  751. return FALSE;
  752. }
  753. // Get a pointer to the file in the list.
  754. pFileListEntry = &VolData.pMoveableFileList[LowestIndex];
  755. // Mark it as moved, so it doesn't get moved again until the next pass.
  756. pFileListEntry->Flags |= FLE_NEXT_PASS;
  757. // Since a file was found in one of these lists, set the return values accordingly.
  758. VolData.LastStartingLcn = VolData.StartingLcn = NextLcn;
  759. // Get the file record number of the file from the list.
  760. VolData.FileRecordNumber = pFileListEntry->FileRecordNumber;
  761. // Note whether this file is a directory or not.
  762. VolData.bDirectory = (pFileListEntry->Flags & FLE_DIRECTORY) ? TRUE : FALSE;
  763. // Note whether this file is fragmented or not.
  764. VolData.bFragmented = (pFileListEntry->Flags & FLE_FRAGMENTED) ? TRUE : FALSE;
  765. // Note this file's index in the list so it can be updated later.
  766. VolData.MoveableFileListIndex = (UINT)LowestIndex;
  767. //Get the file's name.
  768. VolData.vFileName = &VolData.pNameList[pFileListEntry->FileRecordNumber];
  769. // Check to see if the file has been deleted out from under us.
  770. if(!OpenFatFile()) {
  771. //Note not to move this file again.
  772. pFileListEntry->Flags |= FLE_DISABLED;
  773. //Since we didn't find a file, try again. (Use forbidden goto command!)
  774. goto Top_Of_GetNextFatFile;
  775. }
  776. return TRUE;
  777. }
  778. /*****************************************************************************************************************
  779. ROUTINE: AddFileToList
  780. COPYRIGHT 2001 Microsoft Corporation and Executive Software International, Inc.
  781. ROUTINE DESCRIPTION:
  782. Given an extent list it adds it to a file list.
  783. INPUT + OUTPUT:
  784. OUT pList - A pointer to the file list.
  785. IN pListIndex - An index of how far the list has been written into.
  786. IN ListEntries - The number of entries in the extent list.
  787. GLOBALS:
  788. IN VolData.bDirectory - Whether or not this file a directory.
  789. IN VolData.NumberOfExtents - The number of extents in this file.
  790. IN VolData.FileSize - The size of the file.
  791. IN VolData.vFileName - The name of the file.
  792. RETURN:
  793. TRUE = Success
  794. FALSE = Failure
  795. */
  796. BOOL
  797. AddFileToListFat(
  798. OUT FILE_LIST_ENTRY* pList,
  799. IN OUT ULONG* pListIndex,
  800. IN ULONG ListSize,
  801. IN UCHAR* pExtentList
  802. )
  803. {
  804. LONGLONG EarliestStartingLcn = 0xFFFFFFFFFFFFFFFF;
  805. FILE_LIST_ENTRY* pFileListEntry;
  806. FILE_EXTENT_HEADER* pFileExtentHeader;
  807. EF_ASSERT(pList);
  808. EF_ASSERT(pListIndex);
  809. EF_ASSERT(pExtentList);
  810. EF_ASSERT(*pListIndex <= ListSize); //Make sure there is room for another entry in the list.
  811. //We need to start the NameListIndex one byte into the name list so that we can use it's value which will be put
  812. //into FileRecordNumber as an indication that the file list entry contains a valid file. We'll do a test for zero,
  813. //and if the NameListIndex starts at zero then the algorithm will think the first file record is the end of the list.
  814. if(!VolData.NameListIndex){
  815. VolData.NameListIndex = 1;
  816. }
  817. //1.0E00 Check for an overflow of the name list.
  818. EF_ASSERT((VolData.NameListIndex + (ULONG) VolData.vFileName.GetLength() + 1) * sizeof(TCHAR) < VolData.NameListSize);
  819. #ifdef OFFLINEDK
  820. TCHAR cString[200];
  821. //1.0E00 Print out data for the developer.
  822. wsprintf(cString,
  823. TEXT("Adding to %s list; 0x%lX extents, %ld bytes - %s"),
  824. (VolData.bDirectory) ? TEXT("Dir") : TEXT("File"),
  825. (ULONG)VolData.NumberOfExtents,
  826. (ULONG)VolData.FileSize,
  827. (ULONG)ESICompressFilePath(VolData.vFileName));
  828. Message(ESICompressFilePath(VolData.vFileName), -1, cString);
  829. #endif
  830. //Get a pointer to the file extent header.
  831. pFileExtentHeader = (FILE_EXTENT_HEADER*)VolData.pExtentList;
  832. //Get the earliest starting lcn for the file which we'll use in our algorithms to determine which file to move next.
  833. EF(GetLowestStartingLcn(&EarliestStartingLcn, pFileExtentHeader));
  834. //Get a pointer to the end of the file list.
  835. pFileListEntry = &pList[*pListIndex];
  836. //Fill in this file into the file list.
  837. pFileListEntry->FileRecordNumber = (UINT)VolData.NameListIndex;
  838. pFileListEntry->StartingLcn = EarliestStartingLcn;
  839. pFileListEntry->ExcessExtentCount = (UINT)VolData.NumberOfFragments - pFileExtentHeader->NumberOfStreams; //Only count *excess* extents since otherwise files with multiple streams would be "fragmented".
  840. //Set or clear the fragmented flag depending on whether the file is fragmented or not.
  841. if(VolData.bFragmented){
  842. //Set the fragmented flag.
  843. pFileListEntry->Flags |= FLE_FRAGMENTED;
  844. }
  845. else{
  846. //Clear the fragmented flag.
  847. pFileListEntry->Flags &= ~FLE_FRAGMENTED;
  848. }
  849. //Set or clear the directory flag depending on whether the file is a directory or not.
  850. if(VolData.bDirectory){
  851. //Set the directory flag.
  852. pFileListEntry->Flags |= FLE_DIRECTORY;
  853. }
  854. else{
  855. //Clear the directory flag.
  856. pFileListEntry->Flags &= ~FLE_DIRECTORY;
  857. }
  858. if (VolData.vFileName.GetLength() > 0) {
  859. //1.0E00 Put the filename into the file list and update the file list index.
  860. lstrcpy((PTCHAR)(VolData.pNameList + VolData.NameListIndex), VolData.vFileName.GetBuffer());
  861. pFileListEntry->FileRecordNumber = VolData.NameListIndex;
  862. VolData.NameListIndex += VolData.vFileName.GetLength() + 1;
  863. //Set the index to point to the new end of the list.
  864. (*pListIndex)++;
  865. }
  866. return TRUE;
  867. }
  868. BOOL
  869. UpdateInFileList(
  870. )
  871. {
  872. LONGLONG EarliestStartingLcn = 0xFFFFFFFFFFFFFFFF;
  873. FILE_EXTENT_HEADER* pFileExtentHeader;
  874. FILE_LIST_ENTRY* pFileListEntry;
  875. //Sanity check this value. CurrentFileListIndex must fall inside the file list.
  876. EF(VolData.MoveableFileListIndex <= VolData.MoveableFileListEntries);
  877. //Get a pointer to the entry.
  878. pFileListEntry = &VolData.pMoveableFileList[VolData.MoveableFileListIndex];
  879. //Sanity check the file record number. They should be the same.
  880. EF(pFileListEntry->FileRecordNumber == (UINT)VolData.FileRecordNumber);
  881. //Get a pointer to the file extent header.
  882. pFileExtentHeader = (FILE_EXTENT_HEADER*)VolData.pExtentList;
  883. //Get the earliest starting lcn for the file which we'll use in our algorithms to determine which file to move next.
  884. EF(GetLowestStartingLcn(&EarliestStartingLcn, pFileExtentHeader));
  885. //Update the information on this file.
  886. pFileListEntry->StartingLcn = EarliestStartingLcn;
  887. pFileListEntry->ExcessExtentCount = (UINT)VolData.NumberOfFragments - pFileExtentHeader->NumberOfStreams; //Only count *excess* extents since otherwise files with multiple streams would be "fragmented".
  888. //Set or clear the fragmented flag depending on whether the file is fragmented or not.
  889. if(VolData.bFragmented){
  890. //Set the fragmented flag.
  891. pFileListEntry->Flags |= FLE_FRAGMENTED;
  892. }
  893. else{
  894. //Clear the fragmented flag.
  895. pFileListEntry->Flags &= ~FLE_FRAGMENTED;
  896. }
  897. //Set or clear the directory flag depending on whether the file is a directory or not.
  898. if(VolData.bDirectory){
  899. //Set the directory flag.
  900. pFileListEntry->Flags |= FLE_DIRECTORY;
  901. }
  902. else{
  903. //Clear the directory flag.
  904. pFileListEntry->Flags &= ~FLE_DIRECTORY;
  905. }
  906. return TRUE;
  907. }