]> git.sven.stormbind.net Git - sven/exfat-utils.git/blobdiff - libexfat/cluster.c
New upstream version 1.2.6
[sven/exfat-utils.git] / libexfat / cluster.c
index 1a4b53ca30102d5a21fa105be6cec7c30e67ec8e..1d3923355534cfb371ed243bc219fdbdd51dc1d4 100644 (file)
@@ -3,7 +3,7 @@
        exFAT file system implementation library.
 
        Free exFAT implementation.
-       Copyright (C) 2010-2014  Andrew Nayenko
+       Copyright (C) 2010-2017  Andrew Nayenko
 
        This program is free software; you can redistribute it and/or modify
        it under the terms of the GNU General Public License as published by
@@ -67,7 +67,7 @@ static cluster_t s2c(const struct exfat* ef, off_t sector)
 static uint32_t bytes2clusters(const struct exfat* ef, uint64_t bytes)
 {
        uint64_t cluster_size = CLUSTER_SIZE(*ef->sb);
-       return (bytes + cluster_size - 1) / cluster_size;
+       return DIV_ROUND_UP(bytes, cluster_size);
 }
 
 cluster_t exfat_next_cluster(const struct exfat* ef,
@@ -79,7 +79,7 @@ cluster_t exfat_next_cluster(const struct exfat* ef,
        if (cluster < EXFAT_FIRST_DATA_CLUSTER)
                exfat_bug("bad cluster 0x%x", cluster);
 
-       if (IS_CONTIGUOUS(*node))
+       if (node->is_contiguous)
                return cluster + 1;
        fat_offset = s2o(ef, le32_to_cpu(ef->sb->fat_sector_start))
                + cluster * sizeof(cluster_t);
@@ -149,10 +149,13 @@ static int flush_nodes(struct exfat* ef, struct exfat_node* node)
        return exfat_flush_node(ef, node);
 }
 
-int exfat_flush(struct exfat* ef)
+int exfat_flush_nodes(struct exfat* ef)
 {
-       int rc = flush_nodes(ef, ef->root);
+       return flush_nodes(ef, ef->root);
+}
 
+int exfat_flush(struct exfat* ef)
+{
        if (ef->cmap.dirty)
        {
                if (exfat_pwrite(ef->dev, ef->cmap.chunk,
@@ -165,7 +168,7 @@ int exfat_flush(struct exfat* ef)
                ef->cmap.dirty = false;
        }
 
-       return rc;
+       return 0;
 }
 
 static bool set_next_cluster(const struct exfat* ef, bool contiguous,
@@ -267,7 +270,7 @@ static int grow_file(struct exfat* ef, struct exfat_node* node,
                node->fptr_cluster = node->start_cluster = previous;
                allocated = 1;
                /* file consists of only one cluster, so it's contiguous */
-               node->flags |= EXFAT_ATTRIB_CONTIGUOUS;
+               node->is_contiguous = true;
        }
 
        while (allocated < difference)
@@ -279,22 +282,22 @@ static int grow_file(struct exfat* ef, struct exfat_node* node,
                                shrink_file(ef, node, current + allocated, allocated);
                        return -ENOSPC;
                }
-               if (next != previous - 1 && IS_CONTIGUOUS(*node))
+               if (next != previous - 1 && node->is_contiguous)
                {
                        /* it's a pity, but we are not able to keep the file contiguous
                           anymore */
                        if (!make_noncontiguous(ef, node->start_cluster, previous))
                                return -EIO;
-                       node->flags &= ~EXFAT_ATTRIB_CONTIGUOUS;
-                       node->flags |= EXFAT_ATTRIB_DIRTY;
+                       node->is_contiguous = false;
+                       node->is_dirty = true;
                }
-               if (!set_next_cluster(ef, IS_CONTIGUOUS(*node), previous, next))
+               if (!set_next_cluster(ef, node->is_contiguous, previous, next))
                        return -EIO;
                previous = next;
                allocated++;
        }
 
-       if (!set_next_cluster(ef, IS_CONTIGUOUS(*node), previous,
+       if (!set_next_cluster(ef, node->is_contiguous, previous,
                        EXFAT_CLUSTER_END))
                return -EIO;
        return 0;
@@ -324,7 +327,7 @@ static int shrink_file(struct exfat* ef, struct exfat_node* node,
                        return -EIO;
                }
                previous = exfat_next_cluster(ef, node, last);
-               if (!set_next_cluster(ef, IS_CONTIGUOUS(*node), last,
+               if (!set_next_cluster(ef, node->is_contiguous, last,
                                EXFAT_CLUSTER_END))
                        return -EIO;
        }
@@ -332,7 +335,7 @@ static int shrink_file(struct exfat* ef, struct exfat_node* node,
        {
                previous = node->start_cluster;
                node->start_cluster = EXFAT_CLUSTER_FREE;
-               node->flags |= EXFAT_ATTRIB_DIRTY;
+               node->is_dirty = true;
        }
        node->fptr_index = 0;
        node->fptr_cluster = node->start_cluster;
@@ -347,7 +350,7 @@ static int shrink_file(struct exfat* ef, struct exfat_node* node,
                        return -EIO;
                }
                next = exfat_next_cluster(ef, node, previous);
-               if (!set_next_cluster(ef, IS_CONTIGUOUS(*node), previous,
+               if (!set_next_cluster(ef, node->is_contiguous, previous,
                                EXFAT_CLUSTER_FREE))
                        return -EIO;
                free_cluster(ef, previous);
@@ -431,7 +434,7 @@ int exfat_truncate(struct exfat* ef, struct exfat_node* node, uint64_t size,
 
        exfat_update_mtime(node);
        node->size = size;
-       node->flags |= EXFAT_ATTRIB_DIRTY;
+       node->is_dirty = true;
        return 0;
 }