3 exFAT file system implementation library.
5 Free exFAT implementation.
6 Copyright (C) 2010-2016 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 /* on-disk nodes iterator */
36 struct exfat_node* exfat_get_node(struct exfat_node* node)
38 /* if we switch to multi-threaded mode we will need atomic
39 increment here and atomic decrement in exfat_put_node() */
44 void exfat_put_node(struct exfat* ef, struct exfat_node* node)
46 char buffer[EXFAT_UTF8_NAME_BUFFER_MAX];
49 if (node->references < 0)
51 exfat_get_name(node, buffer);
52 exfat_bug("reference counter of '%s' is below zero", buffer);
54 else if (node->references == 0 && node != ef->root)
58 exfat_get_name(node, buffer);
59 exfat_warn("dirty node '%s' with zero references", buffer);
65 * This function must be called on rmdir and unlink (after the last
66 * exfat_put_node()) to free clusters.
68 int exfat_cleanup_node(struct exfat* ef, struct exfat_node* node)
72 if (node->references != 0)
73 exfat_bug("unable to cleanup a node with %d references",
76 if (node->is_unlinked)
78 /* free all clusters and node structure itself */
79 rc = exfat_truncate(ef, node, 0, true);
80 /* free the node even in case of error or its memory will be lost */
86 static int opendir(struct exfat* ef, const struct exfat_node* dir,
89 char buffer[EXFAT_UTF8_NAME_BUFFER_MAX];
91 if (!(dir->attrib & EXFAT_ATTRIB_DIR))
93 exfat_get_name(dir, buffer);
94 exfat_bug("'%s' is not a directory", buffer);
96 if (CLUSTER_INVALID(dir->start_cluster))
98 exfat_get_name(dir, buffer);
99 exfat_error("'%s' directory starts with invalid cluster %#x", buffer,
103 it->cluster = dir->start_cluster;
105 it->chunk = malloc(CLUSTER_SIZE(*ef->sb));
106 if (it->chunk == NULL)
108 exfat_error("failed to allocate memory for directory cluster");
111 if (exfat_pread(ef->dev, it->chunk, CLUSTER_SIZE(*ef->sb),
112 exfat_c2o(ef, it->cluster)) < 0)
115 exfat_get_name(dir, buffer);
116 exfat_error("failed to read '%s' directory cluster %#x", buffer,
123 static void closedir(struct iterator* it)
131 static bool fetch_next_entry(struct exfat* ef, const struct exfat_node* parent,
134 /* move iterator to the next entry in the directory */
135 it->offset += sizeof(struct exfat_entry);
136 /* fetch the next cluster if needed */
137 if ((it->offset & (CLUSTER_SIZE(*ef->sb) - 1)) == 0)
139 /* reached the end of directory; the caller should check this
141 if (it->offset >= parent->size)
143 it->cluster = exfat_next_cluster(ef, parent, it->cluster);
144 if (CLUSTER_INVALID(it->cluster))
146 exfat_error("invalid cluster 0x%x while reading directory",
150 if (exfat_pread(ef->dev, it->chunk, CLUSTER_SIZE(*ef->sb),
151 exfat_c2o(ef, it->cluster)) < 0)
153 exfat_error("failed to read the next directory cluster %#x",
161 static int read_entries(struct exfat* ef, struct exfat_node* dir,
162 struct exfat_entry* entries, int n, off_t offset)
166 if (!(dir->attrib & EXFAT_ATTRIB_DIR))
167 exfat_bug("attempted to read entries from a file");
169 size = exfat_generic_pread(ef, dir, entries,
170 sizeof(struct exfat_entry[n]), offset);
171 if (size == sizeof(struct exfat_entry[n]))
172 return 0; /* success */
177 exfat_error("read %zd bytes instead of %zu bytes", size,
178 sizeof(struct exfat_entry[n]));
182 static int write_entries(struct exfat* ef, struct exfat_node* dir,
183 const struct exfat_entry* entries, int n, off_t offset)
187 if (!(dir->attrib & EXFAT_ATTRIB_DIR))
188 exfat_bug("attempted to write entries into a file");
190 size = exfat_generic_pwrite(ef, dir, entries,
191 sizeof(struct exfat_entry[n]), offset);
192 if (size == sizeof(struct exfat_entry[n]))
193 return 0; /* success */
196 exfat_error("wrote %zd bytes instead of %zu bytes", size,
197 sizeof(struct exfat_entry[n]));
201 static struct exfat_node* allocate_node(void)
203 struct exfat_node* node = malloc(sizeof(struct exfat_node));
206 exfat_error("failed to allocate node");
209 memset(node, 0, sizeof(struct exfat_node));
213 static void init_node_meta1(struct exfat_node* node,
214 const struct exfat_entry_meta1* meta1)
216 node->attrib = le16_to_cpu(meta1->attrib);
217 node->continuations = meta1->continuations;
218 node->mtime = exfat_exfat2unix(meta1->mdate, meta1->mtime,
220 /* there is no centiseconds field for atime */
221 node->atime = exfat_exfat2unix(meta1->adate, meta1->atime, 0);
224 static void init_node_meta2(struct exfat_node* node,
225 const struct exfat_entry_meta2* meta2)
227 node->size = le64_to_cpu(meta2->size);
228 node->start_cluster = le32_to_cpu(meta2->start_cluster);
229 node->fptr_cluster = node->start_cluster;
230 node->is_contiguous = ((meta2->flags & EXFAT_FLAG_CONTIGUOUS) != 0);
233 static const struct exfat_entry* get_entry_ptr(const struct exfat* ef,
234 const struct iterator* it)
236 return (const struct exfat_entry*)
237 (it->chunk + it->offset % CLUSTER_SIZE(*ef->sb));
240 static bool check_node(const struct exfat_node* node, uint16_t actual_checksum,
241 uint16_t reference_checksum, uint64_t valid_size, int cluster_size)
243 char buffer[EXFAT_UTF8_NAME_BUFFER_MAX];
247 Validate checksum first. If it's invalid all other fields probably
248 contain just garbage.
250 if (actual_checksum != reference_checksum)
252 exfat_get_name(node, buffer);
253 exfat_error("'%s' has invalid checksum (%#hx != %#hx)", buffer,
254 actual_checksum, reference_checksum);
259 exFAT does not support sparse files but allows files with uninitialized
260 clusters. For such files valid_size means initialized data size and
261 cannot be greater than file size. See SetFileValidData() function
264 if (valid_size > node->size)
266 exfat_get_name(node, buffer);
267 exfat_error("'%s' has valid size (%"PRIu64") greater than size "
268 "(%"PRIu64")", buffer, valid_size, node->size);
273 Empty file must have zero start cluster. Non-empty file must start
274 with a valid cluster. Directories cannot be empty (i.e. must always
275 have a valid start cluster), but we will check this later in opendir()
276 to give user a chance to read current directory.
278 if (node->size == 0 && node->start_cluster != EXFAT_CLUSTER_FREE)
280 exfat_get_name(node, buffer);
281 exfat_error("'%s' is empty but start cluster is %#x", buffer,
282 node->start_cluster);
285 if (node->size > 0 && CLUSTER_INVALID(node->start_cluster))
287 exfat_get_name(node, buffer);
288 exfat_error("'%s' points to invalid cluster %#x", buffer,
289 node->start_cluster);
293 /* Empty file or directory must be marked as non-contiguous. */
294 if (node->size == 0 && node->is_contiguous)
296 exfat_get_name(node, buffer);
297 exfat_error("'%s' is empty but marked as contiguous (%#hx)", buffer,
302 /* Directory size must be aligned on at cluster boundary. */
303 if ((node->attrib & EXFAT_ATTRIB_DIR) && node->size % cluster_size != 0)
305 exfat_get_name(node, buffer);
306 exfat_error("'%s' directory size %"PRIu64" is not divisible by %d", buffer,
307 node->size, cluster_size);
314 static void decompress_upcase(uint16_t* output, const le16_t* source,
320 for (oi = 0; oi < EXFAT_UPCASE_CHARS; oi++)
323 for (si = 0, oi = 0; si < size && oi < EXFAT_UPCASE_CHARS; si++)
325 uint16_t ch = le16_to_cpu(source[si]);
327 if (ch == 0xffff && si + 1 < size) /* indicates a run */
328 oi += le16_to_cpu(source[++si]);
335 * Reads one entry in directory at position pointed by iterator and fills
338 static int readdir(struct exfat* ef, const struct exfat_node* parent,
339 struct exfat_node** node, struct iterator* it)
342 const struct exfat_entry* entry;
343 const struct exfat_entry_meta1* meta1;
344 const struct exfat_entry_meta2* meta2;
345 const struct exfat_entry_name* file_name;
346 const struct exfat_entry_upcase* upcase;
347 const struct exfat_entry_bitmap* bitmap;
348 const struct exfat_entry_label* label;
349 uint8_t continuations = 0;
350 le16_t* namep = NULL;
351 uint16_t reference_checksum = 0;
352 uint16_t actual_checksum = 0;
353 uint64_t valid_size = 0;
354 uint64_t upcase_size = 0;
355 le16_t* upcase_comp = NULL;
361 if (it->offset >= parent->size)
363 if (continuations != 0)
365 exfat_error("expected %hhu continuations", continuations);
368 return -ENOENT; /* that's OK, means end of directory */
371 entry = get_entry_ptr(ef, it);
374 case EXFAT_ENTRY_FILE:
375 if (continuations != 0)
377 exfat_error("expected %hhu continuations before new entry",
381 meta1 = (const struct exfat_entry_meta1*) entry;
382 continuations = meta1->continuations;
383 /* each file entry must have at least 2 continuations:
385 if (continuations < 2)
387 exfat_error("too few continuations (%hhu)", continuations);
390 if (continuations > 1 +
391 DIV_ROUND_UP(EXFAT_NAME_MAX, EXFAT_ENAME_MAX))
393 exfat_error("too many continuations (%hhu)", continuations);
396 reference_checksum = le16_to_cpu(meta1->checksum);
397 actual_checksum = exfat_start_checksum(meta1);
398 *node = allocate_node();
404 /* new node has zero reference counter */
405 (*node)->entry_cluster = it->cluster;
406 (*node)->entry_offset = it->offset;
407 init_node_meta1(*node, meta1);
408 namep = (*node)->name;
411 case EXFAT_ENTRY_FILE_INFO:
412 if (continuations < 2)
414 exfat_error("unexpected continuation (%hhu)",
418 meta2 = (const struct exfat_entry_meta2*) entry;
419 if (meta2->flags & ~(EXFAT_FLAG_ALWAYS1 | EXFAT_FLAG_CONTIGUOUS))
421 exfat_error("unknown flags in meta2 (0x%hhx)", meta2->flags);
424 init_node_meta2(*node, meta2);
425 actual_checksum = exfat_add_checksum(entry, actual_checksum);
426 valid_size = le64_to_cpu(meta2->valid_size);
430 case EXFAT_ENTRY_FILE_NAME:
431 if (continuations == 0)
433 exfat_error("unexpected continuation");
436 file_name = (const struct exfat_entry_name*) entry;
437 actual_checksum = exfat_add_checksum(entry, actual_checksum);
439 memcpy(namep, file_name->name,
441 ((*node)->name + EXFAT_NAME_MAX - namep)) *
443 namep += EXFAT_ENAME_MAX;
444 if (--continuations == 0)
446 if (!check_node(*node, actual_checksum, reference_checksum,
447 valid_size, CLUSTER_SIZE(*ef->sb)))
449 if (!fetch_next_entry(ef, parent, it))
451 return 0; /* entry completed */
455 case EXFAT_ENTRY_UPCASE:
456 if (ef->upcase != NULL)
458 upcase = (const struct exfat_entry_upcase*) entry;
459 if (CLUSTER_INVALID(le32_to_cpu(upcase->start_cluster)))
461 exfat_error("invalid cluster 0x%x in upcase table",
462 le32_to_cpu(upcase->start_cluster));
465 upcase_size = le64_to_cpu(upcase->size);
466 if (upcase_size == 0 ||
467 upcase_size > EXFAT_UPCASE_CHARS * sizeof(uint16_t) ||
468 upcase_size % sizeof(uint16_t) != 0)
470 exfat_error("bad upcase table size (%"PRIu64" bytes)",
474 upcase_comp = malloc(upcase_size);
475 if (upcase_comp == NULL)
477 exfat_error("failed to allocate upcase table (%"PRIu64" bytes)",
483 /* read compressed upcase table */
484 if (exfat_pread(ef->dev, upcase_comp, upcase_size,
485 exfat_c2o(ef, le32_to_cpu(upcase->start_cluster))) < 0)
488 exfat_error("failed to read upper case table "
489 "(%"PRIu64" bytes starting at cluster %#x)",
491 le32_to_cpu(upcase->start_cluster));
495 /* decompress upcase table */
496 ef->upcase = calloc(EXFAT_UPCASE_CHARS, sizeof(uint16_t));
497 if (ef->upcase == NULL)
500 exfat_error("failed to allocate decompressed upcase table");
504 decompress_upcase(ef->upcase, upcase_comp,
505 upcase_size / sizeof(uint16_t));
509 case EXFAT_ENTRY_BITMAP:
510 bitmap = (const struct exfat_entry_bitmap*) entry;
511 ef->cmap.start_cluster = le32_to_cpu(bitmap->start_cluster);
512 if (CLUSTER_INVALID(ef->cmap.start_cluster))
514 exfat_error("invalid cluster 0x%x in clusters bitmap",
515 ef->cmap.start_cluster);
518 ef->cmap.size = le32_to_cpu(ef->sb->cluster_count) -
519 EXFAT_FIRST_DATA_CLUSTER;
520 if (le64_to_cpu(bitmap->size) < DIV_ROUND_UP(ef->cmap.size, 8))
522 exfat_error("invalid clusters bitmap size: %"PRIu64
523 " (expected at least %u)",
524 le64_to_cpu(bitmap->size),
525 DIV_ROUND_UP(ef->cmap.size, 8));
528 /* FIXME bitmap can be rather big, up to 512 MB */
529 ef->cmap.chunk_size = ef->cmap.size;
530 ef->cmap.chunk = malloc(BMAP_SIZE(ef->cmap.chunk_size));
531 if (ef->cmap.chunk == NULL)
533 exfat_error("failed to allocate clusters bitmap chunk "
534 "(%"PRIu64" bytes)", le64_to_cpu(bitmap->size));
539 if (exfat_pread(ef->dev, ef->cmap.chunk,
540 BMAP_SIZE(ef->cmap.chunk_size),
541 exfat_c2o(ef, ef->cmap.start_cluster)) < 0)
543 exfat_error("failed to read clusters bitmap "
544 "(%"PRIu64" bytes starting at cluster %#x)",
545 le64_to_cpu(bitmap->size), ef->cmap.start_cluster);
550 case EXFAT_ENTRY_LABEL:
551 label = (const struct exfat_entry_label*) entry;
552 if (label->length > EXFAT_ENAME_MAX)
554 exfat_error("too long label (%hhu chars)", label->length);
557 if (utf16_to_utf8(ef->label, label->name,
558 sizeof(ef->label), EXFAT_ENAME_MAX) != 0)
563 if (!(entry->type & EXFAT_ENTRY_VALID))
564 break; /* deleted entry, ignore it */
565 if (!(entry->type & EXFAT_ENTRY_OPTIONAL))
567 exfat_error("unknown entry type %#hhx", entry->type);
570 /* optional entry, warn and skip */
571 exfat_warn("unknown entry type %#hhx", entry->type);
572 if (continuations == 0)
574 exfat_error("unexpected continuation");
581 if (!fetch_next_entry(ef, parent, it))
584 /* we never reach here */
592 int exfat_cache_directory(struct exfat* ef, struct exfat_node* dir)
596 struct exfat_node* node;
597 struct exfat_node* current = NULL;
600 return 0; /* already cached */
602 rc = opendir(ef, dir, &it);
605 while ((rc = readdir(ef, dir, &node, &it)) == 0)
610 current->next = node;
611 node->prev = current;
623 for (current = dir->child; current; current = node)
625 node = current->next;
632 dir->is_cached = true;
636 static void tree_attach(struct exfat_node* dir, struct exfat_node* node)
641 dir->child->prev = node;
642 node->next = dir->child;
647 static void tree_detach(struct exfat_node* node)
650 node->prev->next = node->next;
651 else /* this is the first node in the list */
652 node->parent->child = node->next;
654 node->next->prev = node->prev;
660 static void reset_cache(struct exfat* ef, struct exfat_node* node)
662 char buffer[EXFAT_UTF8_NAME_BUFFER_MAX];
666 struct exfat_node* p = node->child;
671 node->is_cached = false;
672 if (node->references != 0)
674 exfat_get_name(node, buffer);
675 exfat_warn("non-zero reference counter (%d) for '%s'",
676 node->references, buffer);
678 if (node != ef->root && node->is_dirty)
680 exfat_get_name(node, buffer);
681 exfat_bug("node '%s' is dirty", buffer);
683 while (node->references)
684 exfat_put_node(ef, node);
687 void exfat_reset_cache(struct exfat* ef)
689 reset_cache(ef, ef->root);
692 int exfat_flush_node(struct exfat* ef, struct exfat_node* node)
694 struct exfat_entry entries[2];
695 struct exfat_entry_meta1* meta1 = (struct exfat_entry_meta1*) &entries[0];
696 struct exfat_entry_meta2* meta2 = (struct exfat_entry_meta2*) &entries[1];
700 return 0; /* no need to flush */
703 exfat_bug("unable to flush node to read-only FS");
705 if (node->parent == NULL)
706 return 0; /* do not flush unlinked node */
708 rc = read_entries(ef, node->parent, entries, 2, node->entry_offset);
712 if (meta1->type != EXFAT_ENTRY_FILE)
714 exfat_error("invalid type of meta1: %#hhx", meta1->type);
717 meta1->attrib = cpu_to_le16(node->attrib);
718 exfat_unix2exfat(node->mtime, &meta1->mdate, &meta1->mtime,
720 exfat_unix2exfat(node->atime, &meta1->adate, &meta1->atime, NULL);
722 if (meta2->type != EXFAT_ENTRY_FILE_INFO)
724 exfat_error("invalid type of meta2: %#hhx", meta2->type);
727 meta2->size = meta2->valid_size = cpu_to_le64(node->size);
728 meta2->start_cluster = cpu_to_le32(node->start_cluster);
729 meta2->flags = EXFAT_FLAG_ALWAYS1;
730 /* empty files must not be marked as contiguous */
731 if (node->size != 0 && node->is_contiguous)
732 meta2->flags |= EXFAT_FLAG_CONTIGUOUS;
733 /* name hash remains unchanged, no need to recalculate it */
735 meta1->checksum = exfat_calc_checksum(meta1, meta2, node->name);
737 rc = write_entries(ef, node->parent, entries, 2, node->entry_offset);
741 node->is_dirty = false;
742 return exfat_flush(ef);
745 static int erase_entries(struct exfat* ef, struct exfat_node* dir, int n,
748 struct exfat_entry entries[n];
752 rc = read_entries(ef, dir, entries, n, offset);
755 for (i = 0; i < n; i++)
756 entries[i].type &= ~EXFAT_ENTRY_VALID;
757 return write_entries(ef, dir, entries, n, offset);
760 static int erase_node(struct exfat* ef, struct exfat_node* node)
764 exfat_get_node(node->parent);
765 rc = erase_entries(ef, node->parent, 1 + node->continuations,
769 exfat_put_node(ef, node->parent);
772 rc = exfat_flush_node(ef, node->parent);
773 exfat_put_node(ef, node->parent);
777 static int shrink_directory(struct exfat* ef, struct exfat_node* dir,
778 off_t deleted_offset)
780 const struct exfat_node* node;
781 const struct exfat_node* last_node;
782 uint64_t entries = 0;
785 if (!(dir->attrib & EXFAT_ATTRIB_DIR))
786 exfat_bug("attempted to shrink a file");
788 exfat_bug("attempted to shrink uncached directory");
790 for (last_node = node = dir->child; node; node = node->next)
792 if (deleted_offset < node->entry_offset)
794 /* there are other entries after the removed one, no way to shrink
798 if (last_node->entry_offset < node->entry_offset)
804 /* offset of the last entry */
805 entries += last_node->entry_offset / sizeof(struct exfat_entry);
806 /* two subentries with meta info */
808 /* subentries with file name */
809 entries += DIV_ROUND_UP(utf16_length(last_node->name),
813 new_size = DIV_ROUND_UP(entries * sizeof(struct exfat_entry),
814 CLUSTER_SIZE(*ef->sb)) * CLUSTER_SIZE(*ef->sb);
815 if (new_size == 0) /* directory always has at least 1 cluster */
816 new_size = CLUSTER_SIZE(*ef->sb);
817 if (new_size == dir->size)
819 return exfat_truncate(ef, dir, new_size, true);
822 static int delete(struct exfat* ef, struct exfat_node* node)
824 struct exfat_node* parent = node->parent;
825 off_t deleted_offset = node->entry_offset;
828 exfat_get_node(parent);
829 rc = erase_node(ef, node);
832 exfat_put_node(ef, parent);
836 rc = shrink_directory(ef, parent, deleted_offset);
837 node->is_unlinked = true;
840 exfat_flush_node(ef, parent);
841 exfat_put_node(ef, parent);
844 exfat_update_mtime(parent);
845 rc = exfat_flush_node(ef, parent);
846 exfat_put_node(ef, parent);
850 int exfat_unlink(struct exfat* ef, struct exfat_node* node)
852 if (node->attrib & EXFAT_ATTRIB_DIR)
854 return delete(ef, node);
857 int exfat_rmdir(struct exfat* ef, struct exfat_node* node)
861 if (!(node->attrib & EXFAT_ATTRIB_DIR))
863 /* check that directory is empty */
864 rc = exfat_cache_directory(ef, node);
869 return delete(ef, node);
872 static int grow_directory(struct exfat* ef, struct exfat_node* dir,
873 uint64_t asize, uint32_t difference)
875 return exfat_truncate(ef, dir,
876 DIV_ROUND_UP(asize + difference, CLUSTER_SIZE(*ef->sb))
877 * CLUSTER_SIZE(*ef->sb), true);
880 static int find_slot(struct exfat* ef, struct exfat_node* dir,
881 cluster_t* cluster, off_t* offset, int subentries)
885 const struct exfat_entry* entry;
888 rc = opendir(ef, dir, &it);
895 *cluster = it.cluster;
898 entry = get_entry_ptr(ef, &it);
899 if (entry->type & EXFAT_ENTRY_VALID)
903 if (contiguous == subentries)
904 break; /* suitable slot is found */
905 if (it.offset + sizeof(struct exfat_entry) >= dir->size)
907 rc = grow_directory(ef, dir, dir->size,
908 (subentries - contiguous) * sizeof(struct exfat_entry));
915 if (!fetch_next_entry(ef, dir, &it))
925 static int commit_entry(struct exfat* ef, struct exfat_node* dir,
926 const le16_t* name, cluster_t cluster, off_t offset, uint16_t attrib)
928 struct exfat_node* node;
929 const size_t name_length = utf16_length(name);
930 const int name_entries = DIV_ROUND_UP(name_length, EXFAT_ENAME_MAX);
931 struct exfat_entry entries[2 + name_entries];
932 struct exfat_entry_meta1* meta1 = (struct exfat_entry_meta1*) &entries[0];
933 struct exfat_entry_meta2* meta2 = (struct exfat_entry_meta2*) &entries[1];
937 memset(entries, 0, sizeof(struct exfat_entry[2]));
939 meta1->type = EXFAT_ENTRY_FILE;
940 meta1->continuations = 1 + name_entries;
941 meta1->attrib = cpu_to_le16(attrib);
942 exfat_unix2exfat(time(NULL), &meta1->crdate, &meta1->crtime,
944 meta1->adate = meta1->mdate = meta1->crdate;
945 meta1->atime = meta1->mtime = meta1->crtime;
946 meta1->mtime_cs = meta1->crtime_cs; /* there is no atime_cs */
948 meta2->type = EXFAT_ENTRY_FILE_INFO;
949 meta2->flags = EXFAT_FLAG_ALWAYS1;
950 meta2->name_length = name_length;
951 meta2->name_hash = exfat_calc_name_hash(ef, name, name_length);
952 meta2->start_cluster = cpu_to_le32(EXFAT_CLUSTER_FREE);
954 meta1->checksum = exfat_calc_checksum(meta1, meta2, name);
956 for (i = 0; i < name_entries; i++)
958 struct exfat_entry_name* name_entry;
960 name_entry = (struct exfat_entry_name*) &entries[2 + i];
961 name_entry->type = EXFAT_ENTRY_FILE_NAME;
962 name_entry->__unknown = 0;
963 memcpy(name_entry->name, name + i * EXFAT_ENAME_MAX,
964 EXFAT_ENAME_MAX * sizeof(le16_t));
967 rc = write_entries(ef, dir, entries, 2 + name_entries, offset);
971 node = allocate_node();
974 node->entry_cluster = cluster;
975 node->entry_offset = offset;
976 memcpy(node->name, name, name_length * sizeof(le16_t));
977 init_node_meta1(node, meta1);
978 init_node_meta2(node, meta2);
980 tree_attach(dir, node);
984 static int create(struct exfat* ef, const char* path, uint16_t attrib)
986 struct exfat_node* dir;
987 struct exfat_node* existing;
988 cluster_t cluster = EXFAT_CLUSTER_BAD;
990 le16_t name[EXFAT_NAME_MAX + 1];
993 rc = exfat_split(ef, &dir, &existing, name, path);
996 if (existing != NULL)
998 exfat_put_node(ef, existing);
999 exfat_put_node(ef, dir);
1003 rc = find_slot(ef, dir, &cluster, &offset,
1004 2 + DIV_ROUND_UP(utf16_length(name), EXFAT_ENAME_MAX));
1007 exfat_put_node(ef, dir);
1010 rc = commit_entry(ef, dir, name, cluster, offset, attrib);
1013 exfat_put_node(ef, dir);
1016 exfat_update_mtime(dir);
1017 rc = exfat_flush_node(ef, dir);
1018 exfat_put_node(ef, dir);
1022 int exfat_mknod(struct exfat* ef, const char* path)
1024 return create(ef, path, EXFAT_ATTRIB_ARCH);
1027 int exfat_mkdir(struct exfat* ef, const char* path)
1030 struct exfat_node* node;
1032 rc = create(ef, path, EXFAT_ATTRIB_DIR);
1035 rc = exfat_lookup(ef, &node, path);
1038 /* directories always have at least one cluster */
1039 rc = exfat_truncate(ef, node, CLUSTER_SIZE(*ef->sb), true);
1043 exfat_put_node(ef, node);
1046 rc = exfat_flush_node(ef, node);
1050 exfat_put_node(ef, node);
1053 exfat_put_node(ef, node);
1057 static int rename_entry(struct exfat* ef, struct exfat_node* dir,
1058 struct exfat_node* node, const le16_t* name, cluster_t new_cluster,
1061 const size_t name_length = utf16_length(name);
1062 const int name_entries = DIV_ROUND_UP(name_length, EXFAT_ENAME_MAX);
1063 struct exfat_entry entries[2 + name_entries];
1064 struct exfat_entry_meta1* meta1 = (struct exfat_entry_meta1*) &entries[0];
1065 struct exfat_entry_meta2* meta2 = (struct exfat_entry_meta2*) &entries[1];
1069 rc = read_entries(ef, node->parent, entries, 2, node->entry_offset);
1073 meta1->continuations = 1 + name_entries;
1074 meta2->name_length = name_length;
1075 meta2->name_hash = exfat_calc_name_hash(ef, name, name_length);
1076 meta1->checksum = exfat_calc_checksum(meta1, meta2, name);
1078 rc = erase_node(ef, node);
1082 node->entry_cluster = new_cluster;
1083 node->entry_offset = new_offset;
1084 node->continuations = 1 + name_entries;
1086 for (i = 0; i < name_entries; i++)
1088 struct exfat_entry_name* name_entry;
1090 name_entry = (struct exfat_entry_name*) &entries[2 + i];
1091 name_entry->type = EXFAT_ENTRY_FILE_NAME;
1092 name_entry->__unknown = 0;
1093 memcpy(name_entry->name, name + i * EXFAT_ENAME_MAX,
1094 EXFAT_ENAME_MAX * sizeof(le16_t));
1097 rc = write_entries(ef, dir, entries, 2 + name_entries, new_offset);
1101 memcpy(node->name, name, (EXFAT_NAME_MAX + 1) * sizeof(le16_t));
1103 tree_attach(dir, node);
1107 int exfat_rename(struct exfat* ef, const char* old_path, const char* new_path)
1109 struct exfat_node* node;
1110 struct exfat_node* existing;
1111 struct exfat_node* dir;
1112 cluster_t cluster = EXFAT_CLUSTER_BAD;
1114 le16_t name[EXFAT_NAME_MAX + 1];
1117 rc = exfat_lookup(ef, &node, old_path);
1121 rc = exfat_split(ef, &dir, &existing, name, new_path);
1124 exfat_put_node(ef, node);
1128 /* check that target is not a subdirectory of the source */
1129 if (node->attrib & EXFAT_ATTRIB_DIR)
1131 struct exfat_node* p;
1133 for (p = dir; p; p = p->parent)
1136 if (existing != NULL)
1137 exfat_put_node(ef, existing);
1138 exfat_put_node(ef, dir);
1139 exfat_put_node(ef, node);
1144 if (existing != NULL)
1146 /* remove target if it's not the same node as source */
1147 if (existing != node)
1149 if (existing->attrib & EXFAT_ATTRIB_DIR)
1151 if (node->attrib & EXFAT_ATTRIB_DIR)
1152 rc = exfat_rmdir(ef, existing);
1158 if (!(node->attrib & EXFAT_ATTRIB_DIR))
1159 rc = exfat_unlink(ef, existing);
1163 exfat_put_node(ef, existing);
1166 /* free clusters even if something went wrong; overwise they
1167 will be just lost */
1168 exfat_cleanup_node(ef, existing);
1169 exfat_put_node(ef, dir);
1170 exfat_put_node(ef, node);
1173 rc = exfat_cleanup_node(ef, existing);
1176 exfat_put_node(ef, dir);
1177 exfat_put_node(ef, node);
1182 exfat_put_node(ef, existing);
1185 rc = find_slot(ef, dir, &cluster, &offset,
1186 2 + DIV_ROUND_UP(utf16_length(name), EXFAT_ENAME_MAX));
1189 exfat_put_node(ef, dir);
1190 exfat_put_node(ef, node);
1193 rc = rename_entry(ef, dir, node, name, cluster, offset);
1196 exfat_put_node(ef, dir);
1197 exfat_put_node(ef, node);
1200 rc = exfat_flush_node(ef, dir);
1201 exfat_put_node(ef, dir);
1202 exfat_put_node(ef, node);
1203 /* node itself is not marked as dirty, no need to flush it */
1207 void exfat_utimes(struct exfat_node* node, const struct timespec tv[2])
1209 node->atime = tv[0].tv_sec;
1210 node->mtime = tv[1].tv_sec;
1211 node->is_dirty = true;
1214 void exfat_update_atime(struct exfat_node* node)
1216 node->atime = time(NULL);
1217 node->is_dirty = true;
1220 void exfat_update_mtime(struct exfat_node* node)
1222 node->mtime = time(NULL);
1223 node->is_dirty = true;
1226 const char* exfat_get_label(struct exfat* ef)
1231 static int find_label(struct exfat* ef, off_t* offset)
1233 struct exfat_entry entry;
1236 for (*offset = 0; ; *offset += sizeof(entry))
1238 rc = read_entries(ef, ef->root, &entry, 1, *offset);
1242 if (entry.type == EXFAT_ENTRY_LABEL)
1247 int exfat_set_label(struct exfat* ef, const char* label)
1249 le16_t label_utf16[EXFAT_ENAME_MAX + 1];
1253 struct exfat_entry_label entry;
1255 memset(label_utf16, 0, sizeof(label_utf16));
1256 rc = utf8_to_utf16(label_utf16, label, EXFAT_ENAME_MAX + 1, strlen(label));
1260 rc = find_label(ef, &offset);
1262 rc = find_slot(ef, ef->root, &cluster, &offset, 1);
1266 entry.type = EXFAT_ENTRY_LABEL;
1267 entry.length = utf16_length(label_utf16);
1268 memcpy(entry.name, label_utf16, sizeof(entry.name));
1269 if (entry.length == 0)
1270 entry.type ^= EXFAT_ENTRY_VALID;
1272 rc = write_entries(ef, ef->root, (struct exfat_entry*) &entry, 1, offset);
1276 strcpy(ef->label, label);