static int grow_file(struct exfat* ef, struct exfat_node* node, uint32_t current, uint32_t difference) { cluster_t previous; cluster_t next; uint32_t allocated = 0; if (difference == 0) exfat_bug("zero clusters count passed"); if (node->start_cluster != EXFAT_CLUSTER_FREE) { /* get the last cluster of the file */ previous = exfat_advance_cluster(ef, node, current - 1); if (CLUSTER_INVALID(previous)) { exfat_error("invalid cluster 0x%x while growing", previous); return -EIO; } } else { if (node->fptr_index != 0) exfat_bug("non-zero pointer index (%u)", node->fptr_index); /* file does not have clusters (i.e. is empty), allocate the first one for it */ previous = allocate_cluster(ef, 0); if (CLUSTER_INVALID(previous)) return -ENOSPC; node->fptr_cluster = node->start_cluster = previous; allocated = 1; /* file consists of only one cluster, so it's contiguous */ node->flags |= EXFAT_ATTRIB_CONTIGUOUS; } while (allocated < difference) { next = allocate_cluster(ef, previous + 1); if (CLUSTER_INVALID(next)) { if (allocated != 0) shrink_file(ef, node, current + allocated, allocated); return -ENOSPC; } if (next != previous - 1 && IS_CONTIGUOUS(*node)) { /* it's a pity, but we are not able to keep the file contiguous anymore */ make_noncontiguous(ef, node->start_cluster, previous); node->flags &= ~EXFAT_ATTRIB_CONTIGUOUS; node->flags |= EXFAT_ATTRIB_DIRTY; } set_next_cluster(ef, IS_CONTIGUOUS(*node), previous, next); previous = next; allocated++; } set_next_cluster(ef, IS_CONTIGUOUS(*node), previous, EXFAT_CLUSTER_END); return 0; }
static int shrink_file(struct exfat* ef, struct exfat_node* node, uint32_t current, uint32_t difference) { cluster_t previous; cluster_t next; if (difference == 0) exfat_bug("zero difference passed"); if (node->start_cluster == EXFAT_CLUSTER_FREE) exfat_bug("unable to shrink empty file (%u clusters)", current); if (current < difference) exfat_bug("file underflow (%u < %u)", current, difference); /* crop the file */ if (current > difference) { cluster_t last = exfat_advance_cluster(ef, node, current - difference - 1); if (CLUSTER_INVALID(last)) { exfat_error("invalid cluster 0x%x while shrinking", last); return -EIO; } previous = exfat_next_cluster(ef, node, last); if (!set_next_cluster(ef, IS_CONTIGUOUS(*node), last, EXFAT_CLUSTER_END)) return -EIO; } else { previous = node->start_cluster; node->start_cluster = EXFAT_CLUSTER_FREE; } node->fptr_index = 0; node->fptr_cluster = node->start_cluster; /* free remaining clusters */ while (difference--) { if (CLUSTER_INVALID(previous)) { exfat_error("invalid cluster 0x%x while freeing after shrink", previous); return -EIO; } next = exfat_next_cluster(ef, node, previous); if (!set_next_cluster(ef, IS_CONTIGUOUS(*node), previous, EXFAT_CLUSTER_FREE)) return -EIO; free_cluster(ef, previous); previous = next; } return 0; }
static void make_noncontiguous(const struct exfat* ef, cluster_t first, cluster_t last) { cluster_t c; for (c = first; c < last; c++) set_next_cluster(ef, 0, c, c + 1); }
static bool make_noncontiguous(const struct exfat* ef, cluster_t first, cluster_t last) { cluster_t c; for (c = first; c < last; c++) if (!set_next_cluster(ef, false, c, c + 1)) return false; return true; }