static BOOL RelocateFirstCluster(RDWRHandle handle, struct DirectoryPosition* pos, struct DirectoryEntry* entry, CLUSTER destination) { int labelsize; CLUSTER fatpos=0, freecluster, label; BOOL IsInFAT = FALSE; SECTOR srcsector, destsector; unsigned long sectorspercluster; CLUSTER clustervalue; BOOL DOTSprocessed=FALSE; struct FSInfoStruct FSInfo; CLUSTER source; /* Do some preliminary calculations. */ source = GetFirstCluster(entry); srcsector = ConvertToDataSector(handle, source); if (!srcsector) RETURN_FTEERROR(FALSE); destsector = ConvertToDataSector(handle, destination); if (!destsector) RETURN_FTEERROR(FALSE); sectorspercluster = GetSectorsPerCluster(handle); if (!sectorspercluster) RETURN_FTEERROR(FALSE); /* Get the value that is stored at the source position in the FAT */ if (!ReadFatLabel(handle, source, &clustervalue)) RETURN_FTEERROR(FALSE); /* We know the first cluster is refered to in the directory entry given */ /* This is the first cluster of some file. See if it is a directory and if it is, adjust the '.' entry of this directory and all of the '..' entries of all the (direct) subdirectories to point to the new cluster. */ if (entry->attribute & FA_DIREC) { if (!AdaptCurrentAndPreviousDirs(handle, source, destination)) { RETURN_FTEERROR(FALSE); } DOTSprocessed = TRUE; } /* Copy all sectors in this cluster to the new position */ if (!CopySectors(handle, srcsector, destsector, sectorspercluster)) { RETURN_FTEERROR(FALSE); } /* Write the entry in the FAT */ if (!WriteFatLabel(handle, destination, clustervalue)) { RETURN_FTEERROR(FALSE); } SetFirstCluster(destination, entry); if (!WriteDirectory(handle, pos, entry)) { RETURN_FTEERROR(FALSE); } // if (!GetNthCluster(handle, source, &label)) // return FALSE; if (!WriteFatLabel(handle, source, FAT_FREE_LABEL)) { RETURN_FTEERROR(FALSE); } /* Adjust FSInfo on FAT32 */ labelsize = GetFatLabelSize(handle); if (labelsize == FAT32) { if (!GetFreeClusterSearchStart(handle, &freecluster)) RETURN_FTEERROR(FALSE); if (source < freecluster) /* source cluster became available */ { if (!ReadFSInfo(handle, &FSInfo)) RETURN_FTEERROR(FALSE); WriteFreeClusterStart(&FSInfo, source); if (!WriteFSInfo(handle, &FSInfo)) RETURN_FTEERROR(FALSE); } if ((freecluster == destination) && /* We are relocating to the first */ (destination < source)) /* free cluster */ { CLUSTER dummy; if (!FindFirstFreeSpace(handle, &dummy, &dummy)) RETURN_FTEERROR(FALSE); } } return TRUE; }
static BOOL Relocate1Cluster(RDWRHandle handle, CLUSTER source, CLUSTER destination, CLUSTER pSource) { int labelsize; CLUSTER fatpos=0, freecluster, label; //BOOL IsInFAT = FALSE; SECTOR srcsector, destsector; unsigned long sectorspercluster; CLUSTER clustervalue; BOOL DOTSprocessed=FALSE; struct FSInfoStruct FSInfo; /* See wether the destination is actually free. */ if (!GetNthCluster(handle, destination, &label)) RETURN_FTEERROR(FALSE); if (!FAT_FREE(label)) RETURN_FTEERROR(FALSE); /* Do some preliminary calculations. */ srcsector = ConvertToDataSector(handle, source); if (!srcsector) RETURN_FTEERROR(FALSE); destsector = ConvertToDataSector(handle, destination); if (!destsector) RETURN_FTEERROR(FALSE); sectorspercluster = GetSectorsPerCluster(handle); if (!sectorspercluster) RETURN_FTEERROR(FALSE); /* Get the value that is stored at the source position in the FAT */ if (!ReadFatLabel(handle, source, &clustervalue)) RETURN_FTEERROR(FALSE); /* Copy all sectors in this cluster to the new position */ if (!CopySectors(handle, srcsector, destsector, sectorspercluster)) { RETURN_FTEERROR(FALSE); } if (!WriteFatLabel(handle, destination, clustervalue)) { RETURN_FTEERROR(FALSE); } /* Adjust the pointer to the relocated cluster */ if (!WriteFatLabel(handle, pSource, destination)) { RETURN_FTEERROR(FALSE); } if (!WriteFatLabel(handle, source, FAT_FREE_LABEL)) { if (DOTSprocessed) AdaptCurrentAndPreviousDirs(handle, source, source); RETURN_FTEERROR(FALSE); } /* Adjust FSInfo on FAT32 */ labelsize = GetFatLabelSize(handle); if (labelsize == FAT32) { if (!GetFreeClusterSearchStart(handle, &freecluster)) RETURN_FTEERROR(FALSE); if (source < freecluster) /* source cluster became available */ { if (!ReadFSInfo(handle, &FSInfo)) RETURN_FTEERROR(FALSE); WriteFreeClusterStart(&FSInfo, source); if (!WriteFSInfo(handle, &FSInfo)) RETURN_FTEERROR(FALSE); } if ((freecluster == destination) && /* We are relocating to the first */ (destination < source)) /* free cluster */ { CLUSTER dummy; if (!FindFirstFreeSpace(handle, &dummy, &dummy)) RETURN_FTEERROR(FALSE); } } return TRUE; }
/* This function returns FALSE if the destination is not free. */ BOOL RelocateCluster(RDWRHandle handle, CLUSTER source, CLUSTER destination) { int labelsize, value; CLUSTER fatpos=0, freecluster, dircluster, label; struct DirectoryPosition dirpos; struct DirectoryEntry entry; BOOL IsInFAT = FALSE; SECTOR srcsector, destsector; unsigned long sectorspercluster; CLUSTER clustervalue; BOOL found, DOTSprocessed=FALSE; struct FSInfoStruct FSInfo; if (!FatReferedMap) { if (!CreateFatReferedMap(handle)) return FALSE; } /* See wether the destination is actually free. */ if (!GetNthCluster(handle, destination, &label)) RETURN_FTEERROR(FALSE); if (!FAT_FREE(label)) RETURN_FTEERROR(FALSE); /* Do some preliminary calculations. */ srcsector = ConvertToDataSector(handle, source); if (!srcsector) RETURN_FTEERROR(FALSE); destsector = ConvertToDataSector(handle, destination); if (!destsector) RETURN_FTEERROR(FALSE); sectorspercluster = GetSectorsPerCluster(handle); if (!sectorspercluster) RETURN_FTEERROR(FALSE); /* Get the value that is stored at the source position in the FAT */ if (!ReadFatLabel(handle, source, &clustervalue)) RETURN_FTEERROR(FALSE); /* See where the cluster is refered */ if (!GetVFSBitfieldBit(FatReferedMap, source, &value)) return FALSE; if (value) {//CLUSTER fatpos1; if (!FindClusterInFAT(handle, source, &fatpos)) RETURN_FTEERROR(FALSE); /* if (!FindClusterInFAT1(handle, source, &fatpos1)) RETURN_FTEERROR(FALSE); if (fatpos != fatpos1) printf("hola"); */ } if (!fatpos) { if (!FindClusterInDirectories(handle, source, &dirpos, &found)) RETURN_FTEERROR(FALSE); if (!found) { /* Note: on FAT32 this cluster may be pointing to the root directory. We do not support relocating the root cluster at this time. */ RETURN_FTEERROR(FALSE); /* Non valid cluster! */ } else { /* This is the first cluster of some file. See if it is a directory and if it is, adjust the '.' entry of this directory and all of the '..' entries of all the (direct) subdirectories to point to the new cluster. */ if (!GetDirectory(handle, &dirpos, &entry)) RETURN_FTEERROR(FALSE); if (entry.attribute & FA_DIREC) { dircluster = GetFirstCluster(&entry); if (!AdaptCurrentAndPreviousDirs(handle, dircluster, destination)) { RETURN_FTEERROR(FALSE); } DOTSprocessed = TRUE; } } } else { IsInFAT = TRUE; } /* Copy all sectors in this cluster to the new position */ if (!CopySectors(handle, srcsector, destsector, sectorspercluster)) { if (DOTSprocessed) AdaptCurrentAndPreviousDirs(handle, dircluster, source); RETURN_FTEERROR(FALSE); } /* Write the entry in the FAT */ if (!WriteFatReference(handle, destination, clustervalue)) { if (DOTSprocessed) AdaptCurrentAndPreviousDirs(handle, dircluster, source); RETURN_FTEERROR(FALSE); } if (!WriteFatLabel(handle, destination, clustervalue)) { if (DOTSprocessed) AdaptCurrentAndPreviousDirs(handle, dircluster, source); RETURN_FTEERROR(FALSE); } /* Adjust the pointer to the relocated cluster */ if (IsInFAT) { if (!WriteFatReference(handle, fatpos, destination)) { RETURN_FTEERROR(FALSE); } if (!WriteFatLabel(handle, fatpos, destination)) { RETURN_FTEERROR(FALSE); } if (!ClearVFSBitfieldBit(FatReferedMap, source)) return FALSE; if (!SetVFSBitfieldBit(FatReferedMap, destination)) return FALSE; //if (!IndicateFatClusterMoved(fatpos, source, destination)) // return FALSE; } else { CLUSTER label; if (!GetDirectory(handle, &dirpos, &entry)) { if (DOTSprocessed) AdaptCurrentAndPreviousDirs(handle, dircluster, source); RETURN_FTEERROR(FALSE); } SetFirstCluster(destination, &entry); if (!WriteDirectory(handle, &dirpos, &entry)) { if (DOTSprocessed) AdaptCurrentAndPreviousDirs(handle, dircluster, source); RETURN_FTEERROR(FALSE); } if (!IndicateDirEntryMoved(source, destination)) return FALSE; if (!GetNthCluster(handle, source, &label)) return FALSE; //if (!IndicateFatClusterMoved(label, source, destination)) // return FALSE; } if (!WriteFatReference(handle, source, FAT_FREE_LABEL)) { if (DOTSprocessed) AdaptCurrentAndPreviousDirs(handle, dircluster, source); RETURN_FTEERROR(FALSE); } if (!WriteFatLabel(handle, source, FAT_FREE_LABEL)) { if (DOTSprocessed) AdaptCurrentAndPreviousDirs(handle, dircluster, source); RETURN_FTEERROR(FALSE); } if (!IndicateDirClusterMoved(handle, source, destination)) RETURN_FTEERROR(FALSE); /* Adjust FSInfo on FAT32 */ labelsize = GetFatLabelSize(handle); if (labelsize == FAT32) { if (!GetFreeClusterSearchStart(handle, &freecluster)) RETURN_FTEERROR(FALSE); if (source < freecluster) /* source cluster became available */ { if (!ReadFSInfo(handle, &FSInfo)) RETURN_FTEERROR(FALSE); WriteFreeClusterStart(&FSInfo, source); if (!WriteFSInfo(handle, &FSInfo)) RETURN_FTEERROR(FALSE); } if ((freecluster == destination) && /* We are relocating to the first */ (destination < source)) /* free cluster */ { CLUSTER dummy; if (!FindFirstFreeSpace(handle, &dummy, &dummy)) RETURN_FTEERROR(FALSE); } } return TRUE; }