From c512e12d79bf2176f77754502644645b718ffa9c Mon Sep 17 00:00:00 2001 From: relan Date: Tue, 27 Dec 2016 08:53:38 +0300 Subject: [PATCH] Replace node flags with bit fields. Bit fields are safer and more readable. --- fsck/main.c | 2 +- fuse/main.c | 2 +- libexfat/cluster.c | 22 +++++++++++----------- libexfat/exfat.h | 9 ++++----- libexfat/node.c | 33 ++++++++++++++++----------------- 5 files changed, 33 insertions(+), 35 deletions(-) diff --git a/fsck/main.c b/fsck/main.c index be9c683..700c83c 100644 --- a/fsck/main.c +++ b/fsck/main.c @@ -102,7 +102,7 @@ static void dirck(struct exfat* ef, const char* path) { exfat_get_name(node, entry_path + path_length + 1); exfat_debug("%s: %s, %"PRIu64" bytes, cluster %u", entry_path, - IS_CONTIGUOUS(*node) ? "contiguous" : "fragmented", + node->is_contiguous ? "contiguous" : "fragmented", node->size, node->start_cluster); if (node->flags & EXFAT_ATTRIB_DIR) { diff --git a/fuse/main.c b/fuse/main.c index 9772fa9..efa5bfa 100644 --- a/fuse/main.c +++ b/fuse/main.c @@ -132,7 +132,7 @@ static int fuse_exfat_readdir(const char* path, void* buffer, { exfat_get_name(node, name); exfat_debug("[%s] %s: %s, %"PRId64" bytes, cluster 0x%x", __func__, - name, IS_CONTIGUOUS(*node) ? "contiguous" : "fragmented", + name, node->is_contiguous ? "contiguous" : "fragmented", node->size, node->start_cluster); filler(buffer, name, NULL, 0); exfat_put_node(&ef, node); diff --git a/libexfat/cluster.c b/libexfat/cluster.c index 34f027c..c8c88eb 100644 --- a/libexfat/cluster.c +++ b/libexfat/cluster.c @@ -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); @@ -270,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) @@ -282,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; @@ -327,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; } @@ -335,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; @@ -350,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); @@ -434,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; } diff --git a/libexfat/exfat.h b/libexfat/exfat.h index a84306e..4b8cf69 100644 --- a/libexfat/exfat.h +++ b/libexfat/exfat.h @@ -41,11 +41,6 @@ #define EXFAT_UTF8_NAME_BUFFER_MAX (EXFAT_NAME_MAX * 3 + 1) #define EXFAT_UTF8_ENAME_BUFFER_MAX (EXFAT_ENAME_MAX * 3 + 1) -#define EXFAT_ATTRIB_CONTIGUOUS 0x10000 -#define EXFAT_ATTRIB_CACHED 0x20000 -#define EXFAT_ATTRIB_DIRTY 0x40000 -#define EXFAT_ATTRIB_UNLINKED 0x80000 -#define IS_CONTIGUOUS(node) (((node).flags & EXFAT_ATTRIB_CONTIGUOUS) != 0) #define SECTOR_SIZE(sb) (1 << (sb).sector_bits) #define CLUSTER_SIZE(sb) (SECTOR_SIZE(sb) << (sb).spc_bits) #define CLUSTER_INVALID(c) \ @@ -84,6 +79,10 @@ struct exfat_node off_t entry_offset; cluster_t start_cluster; int flags; + bool is_contiguous : 1; + bool is_cached : 1; + bool is_dirty : 1; + bool is_unlinked : 1; uint64_t size; time_t mtime, atime; le16_t name[EXFAT_NAME_MAX + 1]; diff --git a/libexfat/node.c b/libexfat/node.c index 5b623be..6c23f14 100644 --- a/libexfat/node.c +++ b/libexfat/node.c @@ -53,7 +53,7 @@ void exfat_put_node(struct exfat* ef, struct exfat_node* node) } else if (node->references == 0 && node != ef->root) { - if (node->flags & EXFAT_ATTRIB_DIRTY) + if (node->is_dirty) { exfat_get_name(node, buffer); exfat_warn("dirty node '%s' with zero references", buffer); @@ -73,7 +73,7 @@ int exfat_cleanup_node(struct exfat* ef, struct exfat_node* node) exfat_bug("unable to cleanup a node with %d references", node->references); - if (node->flags & EXFAT_ATTRIB_UNLINKED) + if (node->is_unlinked) { /* free all clusters and node structure itself */ rc = exfat_truncate(ef, node, 0, true); @@ -194,8 +194,7 @@ static void init_node_meta2(struct exfat_node* node, node->size = le64_to_cpu(meta2->size); node->start_cluster = le32_to_cpu(meta2->start_cluster); node->fptr_cluster = node->start_cluster; - if (meta2->flags & EXFAT_FLAG_CONTIGUOUS) - node->flags |= EXFAT_ATTRIB_CONTIGUOUS; + node->is_contiguous = ((meta2->flags & EXFAT_FLAG_CONTIGUOUS) != 0); } static const struct exfat_entry* get_entry_ptr(const struct exfat* ef, @@ -259,7 +258,7 @@ static bool check_node(const struct exfat_node* node, uint16_t actual_checksum, } /* Empty file or directory must be marked as non-contiguous. */ - if (node->size == 0 && (node->flags & EXFAT_ATTRIB_CONTIGUOUS)) + if (node->size == 0 && node->is_contiguous) { exfat_get_name(node, buffer); exfat_error("'%s' is empty but marked as contiguous (%#x)", buffer, @@ -564,7 +563,7 @@ int exfat_cache_directory(struct exfat* ef, struct exfat_node* dir) struct exfat_node* node; struct exfat_node* current = NULL; - if (dir->flags & EXFAT_ATTRIB_CACHED) + if (dir->is_cached) return 0; /* already cached */ rc = opendir(ef, dir, &it); @@ -597,7 +596,7 @@ int exfat_cache_directory(struct exfat* ef, struct exfat_node* dir) return rc; } - dir->flags |= EXFAT_ATTRIB_CACHED; + dir->is_cached = true; return 0; } @@ -636,14 +635,14 @@ static void reset_cache(struct exfat* ef, struct exfat_node* node) tree_detach(p); free(p); } - node->flags &= ~EXFAT_ATTRIB_CACHED; + node->is_cached = false; if (node->references != 0) { exfat_get_name(node, buffer); exfat_warn("non-zero reference counter (%d) for '%s'", node->references, buffer); } - if (node != ef->root && (node->flags & EXFAT_ATTRIB_DIRTY)) + if (node != ef->root && node->is_dirty) { exfat_get_name(node, buffer); exfat_bug("node '%s' is dirty", buffer); @@ -682,7 +681,7 @@ int exfat_flush_node(struct exfat* ef, struct exfat_node* node) struct exfat_entry_meta1 meta1; struct exfat_entry_meta2 meta2; - if (!(node->flags & EXFAT_ATTRIB_DIRTY)) + if (!node->is_dirty) return 0; /* no need to flush */ if (ef->ro) @@ -720,7 +719,7 @@ int exfat_flush_node(struct exfat* ef, struct exfat_node* node) meta2.start_cluster = cpu_to_le32(node->start_cluster); meta2.flags = EXFAT_FLAG_ALWAYS1; /* empty files must not be marked as contiguous */ - if (node->size != 0 && IS_CONTIGUOUS(*node)) + if (node->size != 0 && node->is_contiguous) meta2.flags |= EXFAT_FLAG_CONTIGUOUS; /* name hash remains unchanged, no need to recalculate it */ @@ -737,7 +736,7 @@ int exfat_flush_node(struct exfat* ef, struct exfat_node* node) return -EIO; } - node->flags &= ~EXFAT_ATTRIB_DIRTY; + node->is_dirty = false; return exfat_flush(ef); } @@ -789,7 +788,7 @@ static int shrink_directory(struct exfat* ef, struct exfat_node* dir, if (!(dir->flags & EXFAT_ATTRIB_DIR)) exfat_bug("attempted to shrink a file"); - if (!(dir->flags & EXFAT_ATTRIB_CACHED)) + if (!dir->is_cached) exfat_bug("attempted to shrink uncached directory"); for (last_node = node = dir->child; node; node = node->next) @@ -839,7 +838,7 @@ static int delete(struct exfat* ef, struct exfat_node* node) exfat_update_mtime(parent); tree_detach(node); rc = shrink_directory(ef, parent, deleted_offset); - node->flags |= EXFAT_ATTRIB_UNLINKED; + node->is_unlinked = true; if (rc != 0) { exfat_flush_node(ef, parent); @@ -1241,19 +1240,19 @@ void exfat_utimes(struct exfat_node* node, const struct timespec tv[2]) { node->atime = tv[0].tv_sec; node->mtime = tv[1].tv_sec; - node->flags |= EXFAT_ATTRIB_DIRTY; + node->is_dirty = true; } void exfat_update_atime(struct exfat_node* node) { node->atime = time(NULL); - node->flags |= EXFAT_ATTRIB_DIRTY; + node->is_dirty = true; } void exfat_update_mtime(struct exfat_node* node) { node->mtime = time(NULL); - node->flags |= EXFAT_ATTRIB_DIRTY; + node->is_dirty = true; } const char* exfat_get_label(struct exfat* ef) -- 2.11.0