3 exFAT file system implementation library.
5 Free exFAT implementation.
6 Copyright (C) 2010-2017 Andrew Nayenko
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 #define EXFAT_ENTRY_NONE (-1)
30 struct exfat_node* exfat_get_node(struct exfat_node* node)
32 /* if we switch to multi-threaded mode we will need atomic
33 increment here and atomic decrement in exfat_put_node() */
38 void exfat_put_node(struct exfat* ef, struct exfat_node* node)
40 char buffer[EXFAT_UTF8_NAME_BUFFER_MAX];
43 if (node->references < 0)
45 exfat_get_name(node, buffer);
46 exfat_bug("reference counter of '%s' is below zero", buffer);
48 else if (node->references == 0 && node != ef->root)
52 exfat_get_name(node, buffer);
53 exfat_warn("dirty node '%s' with zero references", buffer);
59 * This function must be called on rmdir and unlink (after the last
60 * exfat_put_node()) to free clusters.
62 int exfat_cleanup_node(struct exfat* ef, struct exfat_node* node)
66 if (node->references != 0)
67 exfat_bug("unable to cleanup a node with %d references",
70 if (node->is_unlinked)
72 /* free all clusters and node structure itself */
73 rc = exfat_truncate(ef, node, 0, true);
74 /* free the node even in case of error or its memory will be lost */
80 static int read_entries(struct exfat* ef, struct exfat_node* dir,
81 struct exfat_entry* entries, int n, off_t offset)
85 if (!(dir->attrib & EXFAT_ATTRIB_DIR))
86 exfat_bug("attempted to read entries from a file");
88 size = exfat_generic_pread(ef, dir, entries,
89 sizeof(struct exfat_entry[n]), offset);
90 if (size == sizeof(struct exfat_entry[n]))
91 return 0; /* success */
96 exfat_error("read %zd bytes instead of %zu bytes", size,
97 sizeof(struct exfat_entry[n]));
101 static int write_entries(struct exfat* ef, struct exfat_node* dir,
102 const struct exfat_entry* entries, int n, off_t offset)
106 if (!(dir->attrib & EXFAT_ATTRIB_DIR))
107 exfat_bug("attempted to write entries into a file");
109 size = exfat_generic_pwrite(ef, dir, entries,
110 sizeof(struct exfat_entry[n]), offset);
111 if (size == sizeof(struct exfat_entry[n]))
112 return 0; /* success */
115 exfat_error("wrote %zd bytes instead of %zu bytes", size,
116 sizeof(struct exfat_entry[n]));
120 static struct exfat_node* allocate_node(void)
122 struct exfat_node* node = malloc(sizeof(struct exfat_node));
125 exfat_error("failed to allocate node");
128 memset(node, 0, sizeof(struct exfat_node));
132 static void init_node_meta1(struct exfat_node* node,
133 const struct exfat_entry_meta1* meta1)
135 node->attrib = le16_to_cpu(meta1->attrib);
136 node->continuations = meta1->continuations;
137 node->mtime = exfat_exfat2unix(meta1->mdate, meta1->mtime,
139 /* there is no centiseconds field for atime */
140 node->atime = exfat_exfat2unix(meta1->adate, meta1->atime, 0);
143 static void init_node_meta2(struct exfat_node* node,
144 const struct exfat_entry_meta2* meta2)
146 node->size = le64_to_cpu(meta2->size);
147 node->start_cluster = le32_to_cpu(meta2->start_cluster);
148 node->fptr_cluster = node->start_cluster;
149 node->is_contiguous = ((meta2->flags & EXFAT_FLAG_CONTIGUOUS) != 0);
152 static void init_node_name(struct exfat_node* node,
153 const struct exfat_entry* entries, int n)
157 for (i = 0; i < n; i++)
158 memcpy(node->name + i * EXFAT_ENAME_MAX,
159 ((const struct exfat_entry_name*) &entries[i])->name,
160 EXFAT_ENAME_MAX * sizeof(le16_t));
163 static bool check_entries(const struct exfat_entry* entry, int n)
165 int previous = EXFAT_ENTRY_NONE;
169 /* check transitions between entries types */
170 for (i = 0; i < n + 1; previous = current, i++)
174 current = (i < n) ? entry[i].type : EXFAT_ENTRY_NONE;
177 case EXFAT_ENTRY_NONE:
178 valid = (current == EXFAT_ENTRY_FILE);
180 case EXFAT_ENTRY_FILE:
181 valid = (current == EXFAT_ENTRY_FILE_INFO);
183 case EXFAT_ENTRY_FILE_INFO:
184 valid = (current == EXFAT_ENTRY_FILE_NAME);
186 case EXFAT_ENTRY_FILE_NAME:
187 valid = (current == EXFAT_ENTRY_FILE_NAME ||
188 current == EXFAT_ENTRY_NONE ||
189 current >= EXFAT_ENTRY_FILE_TAIL);
191 case EXFAT_ENTRY_FILE_TAIL ... 0xff:
192 valid = (current >= EXFAT_ENTRY_FILE_TAIL ||
193 current == EXFAT_ENTRY_NONE);
199 exfat_error("unexpected entry type %#x after %#x at %d/%d",
200 current, previous, i, n);
207 static bool check_node(const struct exfat_node* node, le16_t actual_checksum,
208 int cluster_size, const struct exfat_entry_meta1* meta1,
209 const struct exfat_entry_meta2* meta2)
211 char buffer[EXFAT_UTF8_NAME_BUFFER_MAX];
215 Validate checksum first. If it's invalid all other fields probably
216 contain just garbage.
218 if (le16_to_cpu(actual_checksum) != le16_to_cpu(meta1->checksum))
220 exfat_get_name(node, buffer);
221 exfat_error("'%s' has invalid checksum (%#hx != %#hx)", buffer,
222 le16_to_cpu(actual_checksum), le16_to_cpu(meta1->checksum));
227 exFAT does not support sparse files but allows files with uninitialized
228 clusters. For such files valid_size means initialized data size and
229 cannot be greater than file size. See SetFileValidData() function
232 if (le64_to_cpu(meta2->valid_size) > node->size)
234 exfat_get_name(node, buffer);
235 exfat_error("'%s' has valid size (%"PRIu64") greater than size "
236 "(%"PRIu64")", buffer, le64_to_cpu(meta2->valid_size),
242 Empty file must have zero start cluster. Non-empty file must start
243 with a valid cluster. Directories cannot be empty (i.e. must always
244 have a valid start cluster), but we will check this later while
245 reading that directory to give user a chance to read this directory.
247 if (node->size == 0 && node->start_cluster != EXFAT_CLUSTER_FREE)
249 exfat_get_name(node, buffer);
250 exfat_error("'%s' is empty but start cluster is %#x", buffer,
251 node->start_cluster);
254 if (node->size > 0 && CLUSTER_INVALID(node->start_cluster))
256 exfat_get_name(node, buffer);
257 exfat_error("'%s' points to invalid cluster %#x", buffer,
258 node->start_cluster);
262 /* Empty file or directory must be marked as non-contiguous. */
263 if (node->size == 0 && node->is_contiguous)
265 exfat_get_name(node, buffer);
266 exfat_error("'%s' is empty but marked as contiguous (%#hx)", buffer,
271 /* Directory size must be aligned on at cluster boundary. */
272 if ((node->attrib & EXFAT_ATTRIB_DIR) && node->size % cluster_size != 0)
274 exfat_get_name(node, buffer);
275 exfat_error("'%s' directory size %"PRIu64" is not divisible by %d", buffer,
276 node->size, cluster_size);
283 static int parse_file_entries(struct exfat* ef, struct exfat_node* parent,
284 struct exfat_node* node, const struct exfat_entry* entries, int n)
286 const struct exfat_entry_meta1* meta1;
287 const struct exfat_entry_meta2* meta2;
288 int mandatory_entries;
290 if (!check_entries(entries, n))
293 meta1 = (const struct exfat_entry_meta1*) &entries[0];
294 if (meta1->continuations < 2)
296 exfat_error("too few continuations (%hhu)", meta1->continuations);
299 meta2 = (const struct exfat_entry_meta2*) &entries[1];
300 if (meta2->flags & ~(EXFAT_FLAG_ALWAYS1 | EXFAT_FLAG_CONTIGUOUS))
302 exfat_error("unknown flags in meta2 (%#hhx)", meta2->flags);
305 mandatory_entries = 2 + DIV_ROUND_UP(meta2->name_length, EXFAT_ENAME_MAX);
306 if (meta1->continuations < mandatory_entries - 1)
308 exfat_error("too few continuations (%hhu < %d)",
309 meta1->continuations, mandatory_entries - 1);
313 init_node_meta1(node, meta1);
314 init_node_meta2(node, meta2);
315 init_node_name(node, entries + 2, mandatory_entries - 2);
317 if (!check_node(node, exfat_calc_checksum(entries, n),
318 CLUSTER_SIZE(*ef->sb), meta1, meta2))
324 static int parse_file_entry(struct exfat* ef, struct exfat_node* parent,
325 struct exfat_node** node, off_t* offset, int n)
327 struct exfat_entry entries[n];
330 rc = read_entries(ef, parent, entries, n, *offset);
334 /* a new node has zero references */
335 *node = allocate_node();
338 (*node)->entry_offset = *offset;
340 rc = parse_file_entries(ef, parent, *node, entries, n);
347 *offset += sizeof(struct exfat_entry[n]);
351 static void decompress_upcase(uint16_t* output, const le16_t* source,
357 for (oi = 0; oi < EXFAT_UPCASE_CHARS; oi++)
360 for (si = 0, oi = 0; si < size && oi < EXFAT_UPCASE_CHARS; si++)
362 uint16_t ch = le16_to_cpu(source[si]);
364 if (ch == 0xffff && si + 1 < size) /* indicates a run */
365 oi += le16_to_cpu(source[++si]);
372 * Read one entry in a directory at offset position and build a new node
375 static int readdir(struct exfat* ef, struct exfat_node* parent,
376 struct exfat_node** node, off_t* offset)
379 struct exfat_entry entry;
380 const struct exfat_entry_meta1* meta1;
381 const struct exfat_entry_upcase* upcase;
382 const struct exfat_entry_bitmap* bitmap;
383 const struct exfat_entry_label* label;
384 uint64_t upcase_size = 0;
385 le16_t* upcase_comp = NULL;
389 rc = read_entries(ef, parent, &entry, 1, *offset);
395 case EXFAT_ENTRY_FILE:
396 meta1 = (const struct exfat_entry_meta1*) &entry;
397 return parse_file_entry(ef, parent, node, offset,
398 1 + meta1->continuations);
400 case EXFAT_ENTRY_UPCASE:
401 if (ef->upcase != NULL)
403 upcase = (const struct exfat_entry_upcase*) &entry;
404 if (CLUSTER_INVALID(le32_to_cpu(upcase->start_cluster)))
406 exfat_error("invalid cluster 0x%x in upcase table",
407 le32_to_cpu(upcase->start_cluster));
410 upcase_size = le64_to_cpu(upcase->size);
411 if (upcase_size == 0 ||
412 upcase_size > EXFAT_UPCASE_CHARS * sizeof(uint16_t) ||
413 upcase_size % sizeof(uint16_t) != 0)
415 exfat_error("bad upcase table size (%"PRIu64" bytes)",
419 upcase_comp = malloc(upcase_size);
420 if (upcase_comp == NULL)
422 exfat_error("failed to allocate upcase table (%"PRIu64" bytes)",
427 /* read compressed upcase table */
428 if (exfat_pread(ef->dev, upcase_comp, upcase_size,
429 exfat_c2o(ef, le32_to_cpu(upcase->start_cluster))) < 0)
432 exfat_error("failed to read upper case table "
433 "(%"PRIu64" bytes starting at cluster %#x)",
435 le32_to_cpu(upcase->start_cluster));
439 /* decompress upcase table */
440 ef->upcase = calloc(EXFAT_UPCASE_CHARS, sizeof(uint16_t));
441 if (ef->upcase == NULL)
444 exfat_error("failed to allocate decompressed upcase table");
447 decompress_upcase(ef->upcase, upcase_comp,
448 upcase_size / sizeof(uint16_t));
452 case EXFAT_ENTRY_BITMAP:
453 bitmap = (const struct exfat_entry_bitmap*) &entry;
454 ef->cmap.start_cluster = le32_to_cpu(bitmap->start_cluster);
455 if (CLUSTER_INVALID(ef->cmap.start_cluster))
457 exfat_error("invalid cluster 0x%x in clusters bitmap",
458 ef->cmap.start_cluster);
461 ef->cmap.size = le32_to_cpu(ef->sb->cluster_count) -
462 EXFAT_FIRST_DATA_CLUSTER;
463 if (le64_to_cpu(bitmap->size) < DIV_ROUND_UP(ef->cmap.size, 8))
465 exfat_error("invalid clusters bitmap size: %"PRIu64
466 " (expected at least %u)",
467 le64_to_cpu(bitmap->size),
468 DIV_ROUND_UP(ef->cmap.size, 8));
471 /* FIXME bitmap can be rather big, up to 512 MB */
472 ef->cmap.chunk_size = ef->cmap.size;
473 ef->cmap.chunk = malloc(BMAP_SIZE(ef->cmap.chunk_size));
474 if (ef->cmap.chunk == NULL)
476 exfat_error("failed to allocate clusters bitmap chunk "
477 "(%"PRIu64" bytes)", le64_to_cpu(bitmap->size));
481 if (exfat_pread(ef->dev, ef->cmap.chunk,
482 BMAP_SIZE(ef->cmap.chunk_size),
483 exfat_c2o(ef, ef->cmap.start_cluster)) < 0)
485 exfat_error("failed to read clusters bitmap "
486 "(%"PRIu64" bytes starting at cluster %#x)",
487 le64_to_cpu(bitmap->size), ef->cmap.start_cluster);
492 case EXFAT_ENTRY_LABEL:
493 label = (const struct exfat_entry_label*) &entry;
494 if (label->length > EXFAT_ENAME_MAX)
496 exfat_error("too long label (%hhu chars)", label->length);
499 if (utf16_to_utf8(ef->label, label->name,
500 sizeof(ef->label), EXFAT_ENAME_MAX) != 0)
505 if (!(entry.type & EXFAT_ENTRY_VALID))
506 break; /* deleted entry, ignore it */
508 exfat_error("unknown entry type %#hhx", entry.type);
511 *offset += sizeof(entry);
513 /* we never reach here */
516 int exfat_cache_directory(struct exfat* ef, struct exfat_node* dir)
520 struct exfat_node* node;
521 struct exfat_node* current = NULL;
524 return 0; /* already cached */
526 while ((rc = readdir(ef, dir, &node, &offset)) == 0)
531 current->next = node;
532 node->prev = current;
543 for (current = dir->child; current; current = node)
545 node = current->next;
552 dir->is_cached = true;
556 static void tree_attach(struct exfat_node* dir, struct exfat_node* node)
561 dir->child->prev = node;
562 node->next = dir->child;
567 static void tree_detach(struct exfat_node* node)
570 node->prev->next = node->next;
571 else /* this is the first node in the list */
572 node->parent->child = node->next;
574 node->next->prev = node->prev;
580 static void reset_cache(struct exfat* ef, struct exfat_node* node)
582 char buffer[EXFAT_UTF8_NAME_BUFFER_MAX];
586 struct exfat_node* p = node->child;
591 node->is_cached = false;
592 if (node->references != 0)
594 exfat_get_name(node, buffer);
595 exfat_warn("non-zero reference counter (%d) for '%s'",
596 node->references, buffer);
598 if (node != ef->root && node->is_dirty)
600 exfat_get_name(node, buffer);
601 exfat_bug("node '%s' is dirty", buffer);
603 while (node->references)
604 exfat_put_node(ef, node);
607 void exfat_reset_cache(struct exfat* ef)
609 reset_cache(ef, ef->root);
612 int exfat_flush_node(struct exfat* ef, struct exfat_node* node)
614 struct exfat_entry entries[1 + node->continuations];
615 struct exfat_entry_meta1* meta1 = (struct exfat_entry_meta1*) &entries[0];
616 struct exfat_entry_meta2* meta2 = (struct exfat_entry_meta2*) &entries[1];
620 return 0; /* no need to flush */
623 exfat_bug("unable to flush node to read-only FS");
625 if (node->parent == NULL)
626 return 0; /* do not flush unlinked node */
628 rc = read_entries(ef, node->parent, entries, 1 + node->continuations,
632 if (!check_entries(entries, 1 + node->continuations))
635 meta1->attrib = cpu_to_le16(node->attrib);
636 exfat_unix2exfat(node->mtime, &meta1->mdate, &meta1->mtime,
638 exfat_unix2exfat(node->atime, &meta1->adate, &meta1->atime, NULL);
639 meta2->size = meta2->valid_size = cpu_to_le64(node->size);
640 meta2->start_cluster = cpu_to_le32(node->start_cluster);
641 meta2->flags = EXFAT_FLAG_ALWAYS1;
642 /* empty files must not be marked as contiguous */
643 if (node->size != 0 && node->is_contiguous)
644 meta2->flags |= EXFAT_FLAG_CONTIGUOUS;
645 /* name hash remains unchanged, no need to recalculate it */
647 meta1->checksum = exfat_calc_checksum(entries, 1 + node->continuations);
648 rc = write_entries(ef, node->parent, entries, 1 + node->continuations,
653 node->is_dirty = false;
654 return exfat_flush(ef);
657 static int erase_entries(struct exfat* ef, struct exfat_node* dir, int n,
660 struct exfat_entry entries[n];
664 rc = read_entries(ef, dir, entries, n, offset);
667 for (i = 0; i < n; i++)
668 entries[i].type &= ~EXFAT_ENTRY_VALID;
669 return write_entries(ef, dir, entries, n, offset);
672 static int erase_node(struct exfat* ef, struct exfat_node* node)
676 exfat_get_node(node->parent);
677 rc = erase_entries(ef, node->parent, 1 + node->continuations,
681 exfat_put_node(ef, node->parent);
684 rc = exfat_flush_node(ef, node->parent);
685 exfat_put_node(ef, node->parent);
689 static int shrink_directory(struct exfat* ef, struct exfat_node* dir,
690 off_t deleted_offset)
692 const struct exfat_node* node;
693 const struct exfat_node* last_node;
694 uint64_t entries = 0;
697 if (!(dir->attrib & EXFAT_ATTRIB_DIR))
698 exfat_bug("attempted to shrink a file");
700 exfat_bug("attempted to shrink uncached directory");
702 for (last_node = node = dir->child; node; node = node->next)
704 if (deleted_offset < node->entry_offset)
706 /* there are other entries after the removed one, no way to shrink
710 if (last_node->entry_offset < node->entry_offset)
716 /* offset of the last entry */
717 entries += last_node->entry_offset / sizeof(struct exfat_entry);
718 /* two subentries with meta info */
720 /* subentries with file name */
721 entries += DIV_ROUND_UP(utf16_length(last_node->name),
725 new_size = DIV_ROUND_UP(entries * sizeof(struct exfat_entry),
726 CLUSTER_SIZE(*ef->sb)) * CLUSTER_SIZE(*ef->sb);
727 if (new_size == 0) /* directory always has at least 1 cluster */
728 new_size = CLUSTER_SIZE(*ef->sb);
729 if (new_size == dir->size)
731 return exfat_truncate(ef, dir, new_size, true);
734 static int delete(struct exfat* ef, struct exfat_node* node)
736 struct exfat_node* parent = node->parent;
737 off_t deleted_offset = node->entry_offset;
740 exfat_get_node(parent);
741 rc = erase_node(ef, node);
744 exfat_put_node(ef, parent);
748 rc = shrink_directory(ef, parent, deleted_offset);
749 node->is_unlinked = true;
752 exfat_flush_node(ef, parent);
753 exfat_put_node(ef, parent);
756 exfat_update_mtime(parent);
757 rc = exfat_flush_node(ef, parent);
758 exfat_put_node(ef, parent);
762 int exfat_unlink(struct exfat* ef, struct exfat_node* node)
764 if (node->attrib & EXFAT_ATTRIB_DIR)
766 return delete(ef, node);
769 int exfat_rmdir(struct exfat* ef, struct exfat_node* node)
773 if (!(node->attrib & EXFAT_ATTRIB_DIR))
775 /* check that directory is empty */
776 rc = exfat_cache_directory(ef, node);
781 return delete(ef, node);
784 static int check_slot(struct exfat* ef, struct exfat_node* dir, off_t offset,
787 struct exfat_entry entries[n];
791 /* Root directory contains entries, that don't have any nodes associated
792 with them (clusters bitmap, upper case table, label). We need to be
793 careful not to overwrite them. */
797 rc = read_entries(ef, dir, entries, n, offset);
800 for (i = 0; i < n; i++)
801 if (entries[i].type & EXFAT_ENTRY_VALID)
806 static int find_slot(struct exfat* ef, struct exfat_node* dir,
807 off_t* offset, int n)
810 struct exfat_node* p;
815 exfat_bug("directory is not cached");
817 /* build a bitmap of valid entries in the directory */
818 dmap = calloc(BMAP_SIZE(dir->size / sizeof(struct exfat_entry)),
822 exfat_error("failed to allocate directory bitmap (%"PRIu64")",
823 dir->size / sizeof(struct exfat_entry));
826 for (p = dir->child; p != NULL; p = p->next)
827 for (i = 0; i < 1 + p->continuations; i++)
828 BMAP_SET(dmap, p->entry_offset / sizeof(struct exfat_entry) + i);
830 /* find a slot in the directory entries bitmap */
831 for (i = 0; i < dir->size / sizeof(struct exfat_entry); i++)
833 if (BMAP_GET(dmap, i) == 0)
835 if (contiguous++ == 0)
836 *offset = (off_t) i * sizeof(struct exfat_entry);
838 /* suitable slot is found, check that it's not occupied */
839 switch (check_slot(ef, dir, *offset, n))
848 /* slot is occupied, continue searching */
858 /* no suitable slots found, extend the directory */
861 return exfat_truncate(ef, dir,
862 ROUND_UP(dir->size + sizeof(struct exfat_entry[n - contiguous]),
863 CLUSTER_SIZE(*ef->sb)),
867 static int commit_entry(struct exfat* ef, struct exfat_node* dir,
868 const le16_t* name, cluster_t cluster, off_t offset, uint16_t attrib)
870 struct exfat_node* node;
871 const size_t name_length = utf16_length(name);
872 const int name_entries = DIV_ROUND_UP(name_length, EXFAT_ENAME_MAX);
873 struct exfat_entry entries[2 + name_entries];
874 struct exfat_entry_meta1* meta1 = (struct exfat_entry_meta1*) &entries[0];
875 struct exfat_entry_meta2* meta2 = (struct exfat_entry_meta2*) &entries[1];
879 memset(entries, 0, sizeof(struct exfat_entry[2]));
881 meta1->type = EXFAT_ENTRY_FILE;
882 meta1->continuations = 1 + name_entries;
883 meta1->attrib = cpu_to_le16(attrib);
884 exfat_unix2exfat(time(NULL), &meta1->crdate, &meta1->crtime,
886 meta1->adate = meta1->mdate = meta1->crdate;
887 meta1->atime = meta1->mtime = meta1->crtime;
888 meta1->mtime_cs = meta1->crtime_cs; /* there is no atime_cs */
890 meta2->type = EXFAT_ENTRY_FILE_INFO;
891 meta2->flags = EXFAT_FLAG_ALWAYS1;
892 meta2->name_length = name_length;
893 meta2->name_hash = exfat_calc_name_hash(ef, name, name_length);
894 meta2->start_cluster = cpu_to_le32(EXFAT_CLUSTER_FREE);
896 for (i = 0; i < name_entries; i++)
898 struct exfat_entry_name* name_entry;
900 name_entry = (struct exfat_entry_name*) &entries[2 + i];
901 name_entry->type = EXFAT_ENTRY_FILE_NAME;
902 name_entry->__unknown = 0;
903 memcpy(name_entry->name, name + i * EXFAT_ENAME_MAX,
904 EXFAT_ENAME_MAX * sizeof(le16_t));
907 meta1->checksum = exfat_calc_checksum(entries, 2 + name_entries);
908 rc = write_entries(ef, dir, entries, 2 + name_entries, offset);
912 node = allocate_node();
915 node->entry_offset = offset;
916 memcpy(node->name, name, name_length * sizeof(le16_t));
917 init_node_meta1(node, meta1);
918 init_node_meta2(node, meta2);
920 tree_attach(dir, node);
924 static int create(struct exfat* ef, const char* path, uint16_t attrib)
926 struct exfat_node* dir;
927 struct exfat_node* existing;
928 cluster_t cluster = EXFAT_CLUSTER_BAD;
930 le16_t name[EXFAT_NAME_MAX + 1];
933 rc = exfat_split(ef, &dir, &existing, name, path);
936 if (existing != NULL)
938 exfat_put_node(ef, existing);
939 exfat_put_node(ef, dir);
943 rc = find_slot(ef, dir, &offset,
944 2 + DIV_ROUND_UP(utf16_length(name), EXFAT_ENAME_MAX));
947 exfat_put_node(ef, dir);
950 rc = commit_entry(ef, dir, name, cluster, offset, attrib);
953 exfat_put_node(ef, dir);
956 exfat_update_mtime(dir);
957 rc = exfat_flush_node(ef, dir);
958 exfat_put_node(ef, dir);
962 int exfat_mknod(struct exfat* ef, const char* path)
964 return create(ef, path, EXFAT_ATTRIB_ARCH);
967 int exfat_mkdir(struct exfat* ef, const char* path)
970 struct exfat_node* node;
972 rc = create(ef, path, EXFAT_ATTRIB_DIR);
975 rc = exfat_lookup(ef, &node, path);
978 /* directories always have at least one cluster */
979 rc = exfat_truncate(ef, node, CLUSTER_SIZE(*ef->sb), true);
983 exfat_put_node(ef, node);
986 rc = exfat_flush_node(ef, node);
990 exfat_put_node(ef, node);
993 exfat_put_node(ef, node);
997 static int rename_entry(struct exfat* ef, struct exfat_node* dir,
998 struct exfat_node* node, const le16_t* name, cluster_t new_cluster,
1001 const size_t name_length = utf16_length(name);
1002 const int name_entries = DIV_ROUND_UP(name_length, EXFAT_ENAME_MAX);
1003 struct exfat_entry entries[2 + name_entries];
1004 struct exfat_entry_meta1* meta1 = (struct exfat_entry_meta1*) &entries[0];
1005 struct exfat_entry_meta2* meta2 = (struct exfat_entry_meta2*) &entries[1];
1009 rc = read_entries(ef, node->parent, entries, 2, node->entry_offset);
1013 meta1->continuations = 1 + name_entries;
1014 meta2->name_length = name_length;
1015 meta2->name_hash = exfat_calc_name_hash(ef, name, name_length);
1017 rc = erase_node(ef, node);
1021 node->entry_offset = new_offset;
1022 node->continuations = 1 + name_entries;
1024 for (i = 0; i < name_entries; i++)
1026 struct exfat_entry_name* name_entry;
1028 name_entry = (struct exfat_entry_name*) &entries[2 + i];
1029 name_entry->type = EXFAT_ENTRY_FILE_NAME;
1030 name_entry->__unknown = 0;
1031 memcpy(name_entry->name, name + i * EXFAT_ENAME_MAX,
1032 EXFAT_ENAME_MAX * sizeof(le16_t));
1035 meta1->checksum = exfat_calc_checksum(entries, 2 + name_entries);
1036 rc = write_entries(ef, dir, entries, 2 + name_entries, new_offset);
1040 memcpy(node->name, name, (EXFAT_NAME_MAX + 1) * sizeof(le16_t));
1042 tree_attach(dir, node);
1046 int exfat_rename(struct exfat* ef, const char* old_path, const char* new_path)
1048 struct exfat_node* node;
1049 struct exfat_node* existing;
1050 struct exfat_node* dir;
1051 cluster_t cluster = EXFAT_CLUSTER_BAD;
1053 le16_t name[EXFAT_NAME_MAX + 1];
1056 rc = exfat_lookup(ef, &node, old_path);
1060 rc = exfat_split(ef, &dir, &existing, name, new_path);
1063 exfat_put_node(ef, node);
1067 /* check that target is not a subdirectory of the source */
1068 if (node->attrib & EXFAT_ATTRIB_DIR)
1070 struct exfat_node* p;
1072 for (p = dir; p; p = p->parent)
1075 if (existing != NULL)
1076 exfat_put_node(ef, existing);
1077 exfat_put_node(ef, dir);
1078 exfat_put_node(ef, node);
1083 if (existing != NULL)
1085 /* remove target if it's not the same node as source */
1086 if (existing != node)
1088 if (existing->attrib & EXFAT_ATTRIB_DIR)
1090 if (node->attrib & EXFAT_ATTRIB_DIR)
1091 rc = exfat_rmdir(ef, existing);
1097 if (!(node->attrib & EXFAT_ATTRIB_DIR))
1098 rc = exfat_unlink(ef, existing);
1102 exfat_put_node(ef, existing);
1105 /* free clusters even if something went wrong; overwise they
1106 will be just lost */
1107 exfat_cleanup_node(ef, existing);
1108 exfat_put_node(ef, dir);
1109 exfat_put_node(ef, node);
1112 rc = exfat_cleanup_node(ef, existing);
1115 exfat_put_node(ef, dir);
1116 exfat_put_node(ef, node);
1121 exfat_put_node(ef, existing);
1124 rc = find_slot(ef, dir, &offset,
1125 2 + DIV_ROUND_UP(utf16_length(name), EXFAT_ENAME_MAX));
1128 exfat_put_node(ef, dir);
1129 exfat_put_node(ef, node);
1132 rc = rename_entry(ef, dir, node, name, cluster, offset);
1135 exfat_put_node(ef, dir);
1136 exfat_put_node(ef, node);
1139 rc = exfat_flush_node(ef, dir);
1140 exfat_put_node(ef, dir);
1141 exfat_put_node(ef, node);
1142 /* node itself is not marked as dirty, no need to flush it */
1146 void exfat_utimes(struct exfat_node* node, const struct timespec tv[2])
1148 node->atime = tv[0].tv_sec;
1149 node->mtime = tv[1].tv_sec;
1150 node->is_dirty = true;
1153 void exfat_update_atime(struct exfat_node* node)
1155 node->atime = time(NULL);
1156 node->is_dirty = true;
1159 void exfat_update_mtime(struct exfat_node* node)
1161 node->mtime = time(NULL);
1162 node->is_dirty = true;
1165 const char* exfat_get_label(struct exfat* ef)
1170 static int find_label(struct exfat* ef, off_t* offset)
1172 struct exfat_entry entry;
1175 for (*offset = 0; ; *offset += sizeof(entry))
1177 rc = read_entries(ef, ef->root, &entry, 1, *offset);
1181 if (entry.type == EXFAT_ENTRY_LABEL)
1186 int exfat_set_label(struct exfat* ef, const char* label)
1188 le16_t label_utf16[EXFAT_ENAME_MAX + 1];
1191 struct exfat_entry_label entry;
1193 memset(label_utf16, 0, sizeof(label_utf16));
1194 rc = utf8_to_utf16(label_utf16, label, EXFAT_ENAME_MAX + 1, strlen(label));
1198 rc = find_label(ef, &offset);
1200 rc = find_slot(ef, ef->root, &offset, 1);
1204 entry.type = EXFAT_ENTRY_LABEL;
1205 entry.length = utf16_length(label_utf16);
1206 memcpy(entry.name, label_utf16, sizeof(entry.name));
1207 if (entry.length == 0)
1208 entry.type ^= EXFAT_ENTRY_VALID;
1210 rc = write_entries(ef, ef->root, (struct exfat_entry*) &entry, 1, offset);
1214 strcpy(ef->label, label);