OSDN Git Service

Pass struct exfat to exfat_put_node() function.
[android-x86/external-exfat.git] / fsck / main.c
index 9d21049..2bb6978 100644 (file)
@@ -17,6 +17,8 @@
 
 #define MB (1024 * 1024)
 
+#define BMAP_GET(bitmap, index) ((bitmap)[(index) / 8] & (1u << ((index) % 8)))
+
 uint64_t files_count, directories_count;
 
 static uint64_t bytes2mb(uint64_t bytes)
@@ -50,12 +52,35 @@ static void sbck(const struct exfat* ef)
        printf("Block size            %8u bytes\n", block_size);
        printf("Cluster size          %8u bytes\n", cluster_size);
        printf("Total space           %8"PRIu64" MB\n", bytes2mb(total));
-       printf("Used space            %8"PRIu64" MB (%hhu%%)\n",
-                       bytes2mb(total * ef->sb->allocated_percent / 100),
-                       ef->sb->allocated_percent);
-       printf("Free space            %8"PRIu64" MB (%hhu%%)\n",
-                       bytes2mb(total * (100 - ef->sb->allocated_percent) / 100),
-                       100 - ef->sb->allocated_percent);
+       printf("Used space            %8hhu%%\n", ef->sb->allocated_percent);
+}
+
+static void nodeck(struct exfat* ef, struct exfat_node* node)
+{
+       const cluster_t cluster_size = CLUSTER_SIZE(*ef->sb);
+       cluster_t clusters = (node->size + cluster_size - 1) / cluster_size;
+       cluster_t c = node->start_cluster;
+       
+       while (clusters--)
+       {
+               if (CLUSTER_INVALID(c))
+               {
+                       char name[EXFAT_NAME_MAX + 1];
+
+                       exfat_get_name(node, name, EXFAT_NAME_MAX);
+                       exfat_error("file `%s' has invalid cluster", name);
+                       return;
+               }
+               if (BMAP_GET(ef->cmap.chunk, c - EXFAT_FIRST_DATA_CLUSTER) == 0)
+               {
+                       char name[EXFAT_NAME_MAX + 1];
+
+                       exfat_get_name(node, name, EXFAT_NAME_MAX);
+                       exfat_error("cluster 0x%x of file `%s' already allocated "
+                                       "to another file", c, name);
+               }
+               c = exfat_next_cluster(ef, node, c);
+       }
 }
 
 static void dirck(struct exfat* ef, const char* path)
@@ -74,7 +99,7 @@ static void dirck(struct exfat* ef, const char* path)
        rc = exfat_opendir(ef, parent, &it);
        if (rc != 0)
        {
-               exfat_put_node(parent);
+               exfat_put_node(ef, parent);
                exfat_error("failed to open directory `%s'", path);
                return;
        }
@@ -94,10 +119,11 @@ static void dirck(struct exfat* ef, const char* path)
                }
                else
                        files_count++;
-               exfat_put_node(node);
+               nodeck(ef, node);
+               exfat_put_node(ef, node);
        }
-       exfat_closedir(&it);
-       exfat_put_node(parent);
+       exfat_closedir(ef, &it);
+       exfat_put_node(ef, parent);
 }
 
 static void fsck(struct exfat* ef)