system/drivers/fatfs/cluster.c
/*
 * FIX  Should return an error???????  Or is that only possible with buggy code
 *
 * Usually used during Lookup to find the sector/offset of a Dirent so it
 * assumes the file length-cluster is valid at that point.
 */


Usually used during Lookup to find the sector/offset of a Dirent so it
assumes the file length-cluster is valid at that point.
Usually used during Lookup to find the sector / offset of a Dirent so it assumes the file length - cluster is valid at that point .
void FileOffsetToSectorOffset(struct FatNode *node, off_t file_offset,
                              uint32_t *r_sector, uint32_t *r_sec_offset) {
  uint32_t cluster;
  uint32_t base_sector;
  uint32_t cluster_offset;
  uint32_t sector_in_cluster;
  if (node == &fsb.root_node &&
      (fsb.fat_type == TYPE_FAT12 || fsb.fat_type == TYPE_FAT16)) {
    *r_sector = fsb.bpb.reserved_sectors_cnt +
                (fsb.bpb.fat_cnt * fsb.sectors_per_fat) + (file_offset / 512);
    *r_sec_offset = file_offset % 512;
  } else if (FindCluster(node, file_offset, &cluster) == 0) {
    base_sector = ClusterToSector(cluster);
    cluster_offset = file_offset % (fsb.bpb.sectors_per_cluster * 512);
    sector_in_cluster = cluster_offset / 512;
    *r_sector = base_sector + sector_in_cluster;
    *r_sec_offset = file_offset % 512;
  } else {
    KLog("File Offset to Sector Offset FAILURE, shouldn't happen");
    exit(-1);
  }
}
void FileOffsetToSectorOffset ( struct FatNode * node , off_t file_offset , uint32_t * r_sector , uint32_t * r_sec_offset ) { uint32_t cluster ; uint32_t base_sector ; uint32_t cluster_offset ; uint32_t sector_in_cluster ; if ( node == & fsb . root_node && ( fsb . fat_type == TYPE_FAT12 || fsb . fat_type == TYPE_FAT16 ) ) { * r_sector = fsb . bpb . reserved_sectors_cnt + ( fsb . bpb . fat_cnt * fsb . sectors_per_fat ) + ( file_offset / 512 ) ; * r_sec_offset = file_offset % 512 ; } else if ( FindCluster ( node , file_offset , & cluster ) == 0 ) { base_sector = ClusterToSector ( cluster ) ; cluster_offset = file_offset % ( fsb . bpb . sectors_per_cluster * 512 ) ; sector_in_cluster = cluster_offset / 512 ; * r_sector = base_sector + sector_in_cluster ; * r_sec_offset = file_offset % 512 ; } else { KLog ( " " ) ; exit ( -1 ) ; } }
FIX  Should return an error???????
FIX Should return an error???????