| 63 | #define SIZEOF_BUFFER ((const unsigned int)512) |
| 64 | |
| 65 | unsigned int ntfs_remove_used_space(disk_t *disk_car,const partition_t *partition, alloc_data_t *list_search_space) |
| 66 | { |
| 67 | dir_data_t dir_data; |
| 68 | switch(dir_partition_ntfs_init(disk_car, partition, &dir_data, 0, 0)) |
| 69 | { |
| 70 | case DIR_PART_ENOIMP: |
| 71 | case DIR_PART_ENOSYS: |
| 72 | return 0; |
| 73 | case DIR_PART_EIO: |
| 74 | log_partition(disk_car,partition); |
| 75 | log_error("Can't open filesystem. Filesystem seems damaged.\n"); |
| 76 | return 0; |
| 77 | case DIR_PART_OK: |
| 78 | break; |
| 79 | } |
| 80 | { |
| 81 | struct ntfs_dir_struct *ls=(struct ntfs_dir_struct *)dir_data.private_dir_data; |
| 82 | unsigned char *buffer; |
| 83 | uint64_t start_free=0; |
| 84 | uint64_t end_free=0; |
| 85 | unsigned long int lcn; |
| 86 | unsigned long int no_of_cluster; |
| 87 | unsigned int cluster_size; /* size in bytes */ |
| 88 | /* Which bit of $Bitmap is in the buffer */ |
| 89 | long long int bmplcn = - (SIZEOF_BUFFER << 3); |
| 90 | log_trace("ntfs_remove_used_space\n"); |
| 91 | buffer=(unsigned char *)MALLOC(SIZEOF_BUFFER); |
| 92 | { |
| 93 | const struct ntfs_boot_sector*ntfs_header=(const struct ntfs_boot_sector*)buffer; |
| 94 | if(disk_car->pread(disk_car, buffer, 512, partition->part_offset) != 512) |
| 95 | { |
| 96 | free(buffer); |
| 97 | dir_data.close(&dir_data); |
| 98 | return 0; |
| 99 | } |
| 100 | cluster_size=ntfs_header->sectors_per_cluster*ntfs_sector_size(ntfs_header); |
| 101 | if(cluster_size==0) |
| 102 | { |
| 103 | free(buffer); |
| 104 | dir_data.close(&dir_data); |
| 105 | return 0; |
| 106 | } |
| 107 | no_of_cluster=(le64(ntfs_header->sectors_nbr) < partition->part_size ? le64(ntfs_header->sectors_nbr) : partition->part_size); |
| 108 | no_of_cluster/=ntfs_header->sectors_per_cluster; |
| 109 | } |
| 110 | for(lcn=0;lcn<no_of_cluster;lcn++) |
| 111 | { |
| 112 | int byte, bit; |
| 113 | if ((bmplcn < 0) || (lcn < (unsigned)bmplcn) || (lcn >= ((unsigned)bmplcn + (SIZEOF_BUFFER << 3)))) |
| 114 | { |
| 115 | ntfs_attr *attr; |
| 116 | /* Mark the buffer as not in use, in case the read is shorter. */ |
| 117 | memset(buffer, 0x00, SIZEOF_BUFFER); |
| 118 | bmplcn = lcn & (~((SIZEOF_BUFFER << 3) - 1)); |
| 119 | attr = ntfs_attr_open(ls->vol->lcnbmp_ni, AT_DATA, AT_UNNAMED, 0); |
| 120 | if(attr==NULL) |
| 121 | { |
| 122 | log_error("Couldn't open $Bitmap\n"); |
no test coverage detected