3 exFAT file system implementation library.
5 Free exFAT implementation.
6 Copyright (C) 2010-2013 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 */
37 struct exfat_node* exfat_get_node(struct exfat_node* node)
39 /* if we switch to multi-threaded mode we will need atomic
40 increment here and atomic decrement in exfat_put_node() */
45 void exfat_put_node(struct exfat* ef, struct exfat_node* node)
47 if (--node->references < 0)
49 char buffer[UTF8_BYTES(EXFAT_NAME_MAX) + 1];
50 exfat_get_name(node, buffer, sizeof(buffer) - 1);
51 exfat_bug("reference counter of `%s' is below zero", buffer);
54 if (node->references == 0)
56 /* FIXME handle I/O error */
57 if (exfat_flush_node(ef, node) != 0)
58 exfat_bug("node flush failed");
59 if (node->flags & EXFAT_ATTRIB_UNLINKED)
61 /* free all clusters and node structure itself */
62 exfat_truncate(ef, node, 0, true);
65 /* FIXME handle I/O error */
66 if (exfat_flush(ef) != 0)
67 exfat_bug("flush failed");
72 * Cluster + offset from the beginning of the directory to absolute offset.
74 static off_t co2o(struct exfat* ef, cluster_t cluster, off_t offset)
76 return exfat_c2o(ef, cluster) + offset % CLUSTER_SIZE(*ef->sb);
79 static int opendir(struct exfat* ef, const struct exfat_node* dir,
82 if (!(dir->flags & EXFAT_ATTRIB_DIR))
83 exfat_bug("not a directory");
84 it->cluster = dir->start_cluster;
86 it->contiguous = IS_CONTIGUOUS(*dir);
87 it->chunk = malloc(CLUSTER_SIZE(*ef->sb));
88 if (it->chunk == NULL)
90 exfat_error("out of memory");
93 if (exfat_pread(ef->dev, it->chunk, CLUSTER_SIZE(*ef->sb),
94 exfat_c2o(ef, it->cluster)) < 0)
96 exfat_error("failed to read directory cluster %#x", it->cluster);
102 static void closedir(struct iterator* it)
111 static int fetch_next_entry(struct exfat* ef, const struct exfat_node* parent,
114 /* move iterator to the next entry in the directory */
115 it->offset += sizeof(struct exfat_entry);
116 /* fetch the next cluster if needed */
117 if ((it->offset & (CLUSTER_SIZE(*ef->sb) - 1)) == 0)
119 /* reached the end of directory; the caller should check this
121 if (it->offset >= parent->size)
123 it->cluster = exfat_next_cluster(ef, parent, it->cluster);
124 if (CLUSTER_INVALID(it->cluster))
126 exfat_error("invalid cluster 0x%x while reading directory",
130 if (exfat_pread(ef->dev, it->chunk, CLUSTER_SIZE(*ef->sb),
131 exfat_c2o(ef, it->cluster)) < 0)
133 exfat_error("failed to read the next directory cluster %#x",
141 static struct exfat_node* allocate_node(void)
143 struct exfat_node* node = malloc(sizeof(struct exfat_node));
146 exfat_error("failed to allocate node");
149 memset(node, 0, sizeof(struct exfat_node));
153 static void init_node_meta1(struct exfat_node* node,
154 const struct exfat_entry_meta1* meta1)
156 node->flags = le16_to_cpu(meta1->attrib);
157 node->mtime = exfat_exfat2unix(meta1->mdate, meta1->mtime,
159 /* there is no centiseconds field for atime */
160 node->atime = exfat_exfat2unix(meta1->adate, meta1->atime, 0);
163 static void init_node_meta2(struct exfat_node* node,
164 const struct exfat_entry_meta2* meta2)
166 node->size = le64_to_cpu(meta2->size);
167 node->start_cluster = le32_to_cpu(meta2->start_cluster);
168 node->fptr_cluster = node->start_cluster;
169 if (meta2->flags & EXFAT_FLAG_CONTIGUOUS)
170 node->flags |= EXFAT_ATTRIB_CONTIGUOUS;
173 static const struct exfat_entry* get_entry_ptr(const struct exfat* ef,
174 const struct iterator* it)
176 return (const struct exfat_entry*)
177 (it->chunk + it->offset % CLUSTER_SIZE(*ef->sb));
181 * Reads one entry in directory at position pointed by iterator and fills
184 static int readdir(struct exfat* ef, const struct exfat_node* parent,
185 struct exfat_node** node, struct iterator* it)
188 const struct exfat_entry* entry;
189 const struct exfat_entry_meta1* meta1;
190 const struct exfat_entry_meta2* meta2;
191 const struct exfat_entry_name* file_name;
192 const struct exfat_entry_upcase* upcase;
193 const struct exfat_entry_bitmap* bitmap;
194 const struct exfat_entry_label* label;
195 uint8_t continuations = 0;
196 le16_t* namep = NULL;
197 uint16_t reference_checksum = 0;
198 uint16_t actual_checksum = 0;
199 uint64_t real_size = 0;
205 if (it->offset >= parent->size)
207 if (continuations != 0)
209 exfat_error("expected %hhu continuations", continuations);
212 return -ENOENT; /* that's OK, means end of directory */
215 entry = get_entry_ptr(ef, it);
218 case EXFAT_ENTRY_FILE:
219 if (continuations != 0)
221 exfat_error("expected %hhu continuations before new entry",
225 meta1 = (const struct exfat_entry_meta1*) entry;
226 continuations = meta1->continuations;
227 /* each file entry must have at least 2 continuations:
229 if (continuations < 2)
231 exfat_error("too few continuations (%hhu)", continuations);
234 if (continuations > 1 +
235 DIV_ROUND_UP(EXFAT_NAME_MAX, EXFAT_ENAME_MAX))
237 exfat_error("too many continuations (%hhu)", continuations);
240 reference_checksum = le16_to_cpu(meta1->checksum);
241 actual_checksum = exfat_start_checksum(meta1);
242 *node = allocate_node();
248 /* new node has zero reference counter */
249 (*node)->entry_cluster = it->cluster;
250 (*node)->entry_offset = it->offset;
251 init_node_meta1(*node, meta1);
252 namep = (*node)->name;
255 case EXFAT_ENTRY_FILE_INFO:
256 if (continuations < 2)
258 exfat_error("unexpected continuation (%hhu)",
262 meta2 = (const struct exfat_entry_meta2*) entry;
263 if (meta2->flags & ~(EXFAT_FLAG_ALWAYS1 | EXFAT_FLAG_CONTIGUOUS))
265 exfat_error("unknown flags in meta2 (0x%hhx)", meta2->flags);
268 init_node_meta2(*node, meta2);
269 actual_checksum = exfat_add_checksum(entry, actual_checksum);
270 real_size = le64_to_cpu(meta2->real_size);
271 /* empty files must be marked as non-contiguous */
272 if ((*node)->size == 0 && (meta2->flags & EXFAT_FLAG_CONTIGUOUS))
274 exfat_error("empty file marked as contiguous (0x%hhx)",
278 /* directories must be aligned on at cluster boundary */
279 if (((*node)->flags & EXFAT_ATTRIB_DIR) &&
280 (*node)->size % CLUSTER_SIZE(*ef->sb) != 0)
282 exfat_error("directory has invalid size %"PRIu64" bytes",
289 case EXFAT_ENTRY_FILE_NAME:
290 if (continuations == 0)
292 exfat_error("unexpected continuation");
295 file_name = (const struct exfat_entry_name*) entry;
296 actual_checksum = exfat_add_checksum(entry, actual_checksum);
298 memcpy(namep, file_name->name,
300 ((*node)->name + EXFAT_NAME_MAX - namep)) *
302 namep += EXFAT_ENAME_MAX;
303 if (--continuations == 0)
306 There are two fields that contain file size. Maybe they
307 plan to add compression support in the future and one of
308 those fields is visible (uncompressed) size and the other
309 is real (compressed) size. Anyway, currently it looks like
310 exFAT does not support compression and both fields must be
313 There is an exception though: pagefile.sys (its real_size
316 if (real_size != (*node)->size)
318 char buffer[UTF8_BYTES(EXFAT_NAME_MAX) + 1];
320 exfat_get_name(*node, buffer, sizeof(buffer) - 1);
321 exfat_error("`%s' real size does not equal to size "
322 "(%"PRIu64" != %"PRIu64")", buffer,
323 real_size, (*node)->size);
326 if (actual_checksum != reference_checksum)
328 char buffer[UTF8_BYTES(EXFAT_NAME_MAX) + 1];
330 exfat_get_name(*node, buffer, sizeof(buffer) - 1);
331 exfat_error("`%s' has invalid checksum (0x%hx != 0x%hx)",
332 buffer, actual_checksum, reference_checksum);
335 if (fetch_next_entry(ef, parent, it) != 0)
337 return 0; /* entry completed */
341 case EXFAT_ENTRY_UPCASE:
342 if (ef->upcase != NULL)
344 upcase = (const struct exfat_entry_upcase*) entry;
345 if (CLUSTER_INVALID(le32_to_cpu(upcase->start_cluster)))
347 exfat_error("invalid cluster 0x%x in upcase table",
348 le32_to_cpu(upcase->start_cluster));
351 if (le64_to_cpu(upcase->size) == 0 ||
352 le64_to_cpu(upcase->size) > 0xffff * sizeof(uint16_t) ||
353 le64_to_cpu(upcase->size) % sizeof(uint16_t) != 0)
355 exfat_error("bad upcase table size (%"PRIu64" bytes)",
356 le64_to_cpu(upcase->size));
359 ef->upcase = malloc(le64_to_cpu(upcase->size));
360 if (ef->upcase == NULL)
362 exfat_error("failed to allocate upcase table (%"PRIu64" bytes)",
363 le64_to_cpu(upcase->size));
367 ef->upcase_chars = le64_to_cpu(upcase->size) / sizeof(le16_t);
369 if (exfat_pread(ef->dev, ef->upcase, le64_to_cpu(upcase->size),
370 exfat_c2o(ef, le32_to_cpu(upcase->start_cluster))) < 0)
372 exfat_error("failed to read upper case table "
373 "(%"PRIu64" bytes starting at cluster %#x)",
374 le64_to_cpu(upcase->size),
375 le32_to_cpu(upcase->start_cluster));
380 case EXFAT_ENTRY_BITMAP:
381 bitmap = (const struct exfat_entry_bitmap*) entry;
382 ef->cmap.start_cluster = le32_to_cpu(bitmap->start_cluster);
383 if (CLUSTER_INVALID(ef->cmap.start_cluster))
385 exfat_error("invalid cluster 0x%x in clusters bitmap",
386 ef->cmap.start_cluster);
389 ef->cmap.size = le32_to_cpu(ef->sb->cluster_count) -
390 EXFAT_FIRST_DATA_CLUSTER;
391 if (le64_to_cpu(bitmap->size) < DIV_ROUND_UP(ef->cmap.size, 8))
393 exfat_error("invalid clusters bitmap size: %"PRIu64
394 " (expected at least %u)",
395 le64_to_cpu(bitmap->size),
396 DIV_ROUND_UP(ef->cmap.size, 8));
399 /* FIXME bitmap can be rather big, up to 512 MB */
400 ef->cmap.chunk_size = ef->cmap.size;
401 ef->cmap.chunk = malloc(BMAP_SIZE(ef->cmap.chunk_size));
402 if (ef->cmap.chunk == NULL)
404 exfat_error("failed to allocate clusters bitmap chunk "
405 "(%"PRIu64" bytes)", le64_to_cpu(bitmap->size));
410 if (exfat_pread(ef->dev, ef->cmap.chunk,
411 BMAP_SIZE(ef->cmap.chunk_size),
412 exfat_c2o(ef, ef->cmap.start_cluster)) < 0)
414 exfat_error("failed to read clusters bitmap "
415 "(%"PRIu64" bytes starting at cluster %#x)",
416 le64_to_cpu(bitmap->size), ef->cmap.start_cluster);
421 case EXFAT_ENTRY_LABEL:
422 label = (const struct exfat_entry_label*) entry;
423 if (label->length > EXFAT_ENAME_MAX)
425 exfat_error("too long label (%hhu chars)", label->length);
428 if (utf16_to_utf8(ef->label, label->name,
429 sizeof(ef->label) - 1, EXFAT_ENAME_MAX) != 0)
434 if (entry->type & EXFAT_ENTRY_VALID)
436 exfat_error("unknown entry type 0x%hhx", entry->type);
442 if (fetch_next_entry(ef, parent, it) != 0)
445 /* we never reach here */
453 int exfat_cache_directory(struct exfat* ef, struct exfat_node* dir)
457 struct exfat_node* node;
458 struct exfat_node* current = NULL;
460 if (dir->flags & EXFAT_ATTRIB_CACHED)
461 return 0; /* already cached */
463 rc = opendir(ef, dir, &it);
466 while ((rc = readdir(ef, dir, &node, &it)) == 0)
471 current->next = node;
472 node->prev = current;
484 for (current = dir->child; current; current = node)
486 node = current->next;
493 dir->flags |= EXFAT_ATTRIB_CACHED;
497 static void tree_attach(struct exfat_node* dir, struct exfat_node* node)
502 dir->child->prev = node;
503 node->next = dir->child;
508 static void tree_detach(struct exfat_node* node)
511 node->prev->next = node->next;
512 else /* this is the first node in the list */
513 node->parent->child = node->next;
515 node->next->prev = node->prev;
521 static void reset_cache(struct exfat* ef, struct exfat_node* node)
525 struct exfat_node* p = node->child;
530 node->flags &= ~EXFAT_ATTRIB_CACHED;
531 if (node->references != 0)
533 char buffer[UTF8_BYTES(EXFAT_NAME_MAX) + 1];
534 exfat_get_name(node, buffer, sizeof(buffer) - 1);
535 exfat_warn("non-zero reference counter (%d) for `%s'",
536 node->references, buffer);
538 while (node->references)
539 exfat_put_node(ef, node);
542 void exfat_reset_cache(struct exfat* ef)
544 reset_cache(ef, ef->root);
547 static void next_entry(struct exfat* ef, const struct exfat_node* parent,
548 cluster_t* cluster, off_t* offset)
550 *offset += sizeof(struct exfat_entry);
551 if (*offset % CLUSTER_SIZE(*ef->sb) == 0)
552 /* next cluster cannot be invalid */
553 *cluster = exfat_next_cluster(ef, parent, *cluster);
556 int exfat_flush_node(struct exfat* ef, struct exfat_node* node)
560 off_t meta1_offset, meta2_offset;
561 struct exfat_entry_meta1 meta1;
562 struct exfat_entry_meta2 meta2;
564 if (!(node->flags & EXFAT_ATTRIB_DIRTY))
565 return 0; /* no need to flush */
568 exfat_bug("unable to flush node to read-only FS");
570 if (node->parent == NULL)
571 return 0; /* do not flush unlinked node */
573 cluster = node->entry_cluster;
574 offset = node->entry_offset;
575 meta1_offset = co2o(ef, cluster, offset);
576 next_entry(ef, node->parent, &cluster, &offset);
577 meta2_offset = co2o(ef, cluster, offset);
579 if (exfat_pread(ef->dev, &meta1, sizeof(meta1), meta1_offset) < 0)
581 exfat_error("failed to read meta1 entry on flush");
584 if (meta1.type != EXFAT_ENTRY_FILE)
585 exfat_bug("invalid type of meta1: 0x%hhx", meta1.type);
586 meta1.attrib = cpu_to_le16(node->flags);
587 exfat_unix2exfat(node->mtime, &meta1.mdate, &meta1.mtime, &meta1.mtime_cs);
588 exfat_unix2exfat(node->atime, &meta1.adate, &meta1.atime, NULL);
590 if (exfat_pread(ef->dev, &meta2, sizeof(meta2), meta2_offset) < 0)
592 exfat_error("failed to read meta2 entry on flush");
595 if (meta2.type != EXFAT_ENTRY_FILE_INFO)
596 exfat_bug("invalid type of meta2: 0x%hhx", meta2.type);
597 meta2.size = meta2.real_size = cpu_to_le64(node->size);
598 meta2.start_cluster = cpu_to_le32(node->start_cluster);
599 meta2.flags = EXFAT_FLAG_ALWAYS1;
600 /* empty files must not be marked as contiguous */
601 if (node->size != 0 && IS_CONTIGUOUS(*node))
602 meta2.flags |= EXFAT_FLAG_CONTIGUOUS;
603 /* name hash remains unchanged, no need to recalculate it */
605 meta1.checksum = exfat_calc_checksum(&meta1, &meta2, node->name);
607 if (exfat_pwrite(ef->dev, &meta1, sizeof(meta1), meta1_offset) < 0)
609 exfat_error("failed to write meta1 entry on flush");
612 if (exfat_pwrite(ef->dev, &meta2, sizeof(meta2), meta2_offset) < 0)
614 exfat_error("failed to write meta2 entry on flush");
618 node->flags &= ~EXFAT_ATTRIB_DIRTY;
622 static bool erase_entry(struct exfat* ef, struct exfat_node* node)
624 cluster_t cluster = node->entry_cluster;
625 off_t offset = node->entry_offset;
626 int name_entries = DIV_ROUND_UP(utf16_length(node->name), EXFAT_ENAME_MAX);
629 entry_type = EXFAT_ENTRY_FILE & ~EXFAT_ENTRY_VALID;
630 if (exfat_pwrite(ef->dev, &entry_type, 1, co2o(ef, cluster, offset)) < 0)
632 exfat_error("failed to erase meta1 entry");
636 next_entry(ef, node->parent, &cluster, &offset);
637 entry_type = EXFAT_ENTRY_FILE_INFO & ~EXFAT_ENTRY_VALID;
638 if (exfat_pwrite(ef->dev, &entry_type, 1, co2o(ef, cluster, offset)) < 0)
640 exfat_error("failed to erase meta2 entry");
644 while (name_entries--)
646 next_entry(ef, node->parent, &cluster, &offset);
647 entry_type = EXFAT_ENTRY_FILE_NAME & ~EXFAT_ENTRY_VALID;
648 if (exfat_pwrite(ef->dev, &entry_type, 1,
649 co2o(ef, cluster, offset)) < 0)
651 exfat_error("failed to erase name entry");
658 static int shrink_directory(struct exfat* ef, struct exfat_node* dir,
659 off_t deleted_offset)
661 const struct exfat_node* node;
662 const struct exfat_node* last_node;
663 uint64_t entries = 0;
667 if (!(dir->flags & EXFAT_ATTRIB_DIR))
668 exfat_bug("attempted to shrink a file");
669 if (!(dir->flags & EXFAT_ATTRIB_CACHED))
670 exfat_bug("attempted to shrink uncached directory");
672 for (last_node = node = dir->child; node; node = node->next)
674 if (deleted_offset < node->entry_offset)
676 /* there are other entries after the removed one, no way to shrink
680 if (last_node->entry_offset < node->entry_offset)
686 /* offset of the last entry */
687 entries += last_node->entry_offset / sizeof(struct exfat_entry);
688 /* two subentries with meta info */
690 /* subentries with file name */
691 entries += DIV_ROUND_UP(utf16_length(last_node->name),
695 new_size = DIV_ROUND_UP(entries * sizeof(struct exfat_entry),
696 CLUSTER_SIZE(*ef->sb)) * CLUSTER_SIZE(*ef->sb);
697 if (new_size == 0) /* directory always has at least 1 cluster */
698 new_size = CLUSTER_SIZE(*ef->sb);
699 if (new_size == dir->size)
701 rc = exfat_truncate(ef, dir, new_size, true);
707 static int delete(struct exfat* ef, struct exfat_node* node)
709 struct exfat_node* parent = node->parent;
710 off_t deleted_offset = node->entry_offset;
713 exfat_get_node(parent);
714 if (!erase_entry(ef, node))
716 exfat_put_node(ef, parent);
719 exfat_update_mtime(parent);
721 rc = shrink_directory(ef, parent, deleted_offset);
722 exfat_put_node(ef, parent);
723 /* file clusters will be freed when node reference counter becomes 0 */
724 node->flags |= EXFAT_ATTRIB_UNLINKED;
728 int exfat_unlink(struct exfat* ef, struct exfat_node* node)
730 if (node->flags & EXFAT_ATTRIB_DIR)
732 return delete(ef, node);
735 int exfat_rmdir(struct exfat* ef, struct exfat_node* node)
737 if (!(node->flags & EXFAT_ATTRIB_DIR))
739 /* check that directory is empty */
740 exfat_cache_directory(ef, node);
743 return delete(ef, node);
746 static int grow_directory(struct exfat* ef, struct exfat_node* dir,
747 uint64_t asize, uint32_t difference)
749 return exfat_truncate(ef, dir,
750 DIV_ROUND_UP(asize + difference, CLUSTER_SIZE(*ef->sb))
751 * CLUSTER_SIZE(*ef->sb), true);
754 static int find_slot(struct exfat* ef, struct exfat_node* dir,
755 cluster_t* cluster, off_t* offset, int subentries)
759 const struct exfat_entry* entry;
762 rc = opendir(ef, dir, &it);
769 *cluster = it.cluster;
772 entry = get_entry_ptr(ef, &it);
773 if (entry->type & EXFAT_ENTRY_VALID)
777 if (contiguous == subentries)
778 break; /* suitable slot is found */
779 if (it.offset + sizeof(struct exfat_entry) >= dir->size)
781 rc = grow_directory(ef, dir, dir->size,
782 (subentries - contiguous) * sizeof(struct exfat_entry));
789 if (fetch_next_entry(ef, dir, &it) != 0)
799 static int write_entry(struct exfat* ef, struct exfat_node* dir,
800 const le16_t* name, cluster_t cluster, off_t offset, uint16_t attrib)
802 struct exfat_node* node;
803 struct exfat_entry_meta1 meta1;
804 struct exfat_entry_meta2 meta2;
805 const size_t name_length = utf16_length(name);
806 const int name_entries = DIV_ROUND_UP(name_length, EXFAT_ENAME_MAX);
809 node = allocate_node();
812 node->entry_cluster = cluster;
813 node->entry_offset = offset;
814 memcpy(node->name, name, name_length * sizeof(le16_t));
816 memset(&meta1, 0, sizeof(meta1));
817 meta1.type = EXFAT_ENTRY_FILE;
818 meta1.continuations = 1 + name_entries;
819 meta1.attrib = cpu_to_le16(attrib);
820 exfat_unix2exfat(time(NULL), &meta1.crdate, &meta1.crtime,
822 meta1.adate = meta1.mdate = meta1.crdate;
823 meta1.atime = meta1.mtime = meta1.crtime;
824 meta1.mtime_cs = meta1.crtime_cs; /* there is no atime_cs */
826 memset(&meta2, 0, sizeof(meta2));
827 meta2.type = EXFAT_ENTRY_FILE_INFO;
828 meta2.flags = EXFAT_FLAG_ALWAYS1;
829 meta2.name_length = name_length;
830 meta2.name_hash = exfat_calc_name_hash(ef, node->name);
831 meta2.start_cluster = cpu_to_le32(EXFAT_CLUSTER_FREE);
833 meta1.checksum = exfat_calc_checksum(&meta1, &meta2, node->name);
835 if (exfat_pwrite(ef->dev, &meta1, sizeof(meta1),
836 co2o(ef, cluster, offset)) < 0)
838 exfat_error("failed to write meta1 entry");
841 next_entry(ef, dir, &cluster, &offset);
842 if (exfat_pwrite(ef->dev, &meta2, sizeof(meta2),
843 co2o(ef, cluster, offset)) < 0)
845 exfat_error("failed to write meta2 entry");
848 for (i = 0; i < name_entries; i++)
850 struct exfat_entry_name name_entry = {EXFAT_ENTRY_FILE_NAME, 0};
851 memcpy(name_entry.name, node->name + i * EXFAT_ENAME_MAX,
852 MIN(EXFAT_ENAME_MAX, EXFAT_NAME_MAX - i * EXFAT_ENAME_MAX) *
854 next_entry(ef, dir, &cluster, &offset);
855 if (exfat_pwrite(ef->dev, &name_entry, sizeof(name_entry),
856 co2o(ef, cluster, offset)) < 0)
858 exfat_error("failed to write name entry");
863 init_node_meta1(node, &meta1);
864 init_node_meta2(node, &meta2);
866 tree_attach(dir, node);
867 exfat_update_mtime(dir);
871 static int create(struct exfat* ef, const char* path, uint16_t attrib)
873 struct exfat_node* dir;
874 struct exfat_node* existing;
875 cluster_t cluster = EXFAT_CLUSTER_BAD;
877 le16_t name[EXFAT_NAME_MAX + 1];
880 rc = exfat_split(ef, &dir, &existing, name, path);
883 if (existing != NULL)
885 exfat_put_node(ef, existing);
886 exfat_put_node(ef, dir);
890 rc = find_slot(ef, dir, &cluster, &offset,
891 2 + DIV_ROUND_UP(utf16_length(name), EXFAT_ENAME_MAX));
894 exfat_put_node(ef, dir);
897 rc = write_entry(ef, dir, name, cluster, offset, attrib);
898 exfat_put_node(ef, dir);
902 int exfat_mknod(struct exfat* ef, const char* path)
904 return create(ef, path, EXFAT_ATTRIB_ARCH);
907 int exfat_mkdir(struct exfat* ef, const char* path)
910 struct exfat_node* node;
912 rc = create(ef, path, EXFAT_ATTRIB_ARCH | EXFAT_ATTRIB_DIR);
915 rc = exfat_lookup(ef, &node, path);
918 /* directories always have at least one cluster */
919 rc = exfat_truncate(ef, node, CLUSTER_SIZE(*ef->sb), true);
923 exfat_put_node(ef, node);
926 exfat_put_node(ef, node);
930 static int rename_entry(struct exfat* ef, struct exfat_node* dir,
931 struct exfat_node* node, const le16_t* name, cluster_t new_cluster,
934 struct exfat_entry_meta1 meta1;
935 struct exfat_entry_meta2 meta2;
936 cluster_t old_cluster = node->entry_cluster;
937 off_t old_offset = node->entry_offset;
938 const size_t name_length = utf16_length(name);
939 const int name_entries = DIV_ROUND_UP(name_length, EXFAT_ENAME_MAX);
942 if (exfat_pread(ef->dev, &meta1, sizeof(meta1),
943 co2o(ef, old_cluster, old_offset)) < 0)
945 exfat_error("failed to read meta1 entry on rename");
948 next_entry(ef, node->parent, &old_cluster, &old_offset);
949 if (exfat_pread(ef->dev, &meta2, sizeof(meta2),
950 co2o(ef, old_cluster, old_offset)) < 0)
952 exfat_error("failed to read meta2 entry on rename");
955 meta1.continuations = 1 + name_entries;
956 meta2.name_hash = exfat_calc_name_hash(ef, name);
957 meta2.name_length = name_length;
958 meta1.checksum = exfat_calc_checksum(&meta1, &meta2, name);
960 if (!erase_entry(ef, node))
963 node->entry_cluster = new_cluster;
964 node->entry_offset = new_offset;
966 if (exfat_pwrite(ef->dev, &meta1, sizeof(meta1),
967 co2o(ef, new_cluster, new_offset)) < 0)
969 exfat_error("failed to write meta1 entry on rename");
972 next_entry(ef, dir, &new_cluster, &new_offset);
973 if (exfat_pwrite(ef->dev, &meta2, sizeof(meta2),
974 co2o(ef, new_cluster, new_offset)) < 0)
976 exfat_error("failed to write meta2 entry on rename");
980 for (i = 0; i < name_entries; i++)
982 struct exfat_entry_name name_entry = {EXFAT_ENTRY_FILE_NAME, 0};
983 memcpy(name_entry.name, name + i * EXFAT_ENAME_MAX,
984 EXFAT_ENAME_MAX * sizeof(le16_t));
985 next_entry(ef, dir, &new_cluster, &new_offset);
986 if (exfat_pwrite(ef->dev, &name_entry, sizeof(name_entry),
987 co2o(ef, new_cluster, new_offset)) < 0)
989 exfat_error("failed to write name entry on rename");
994 memcpy(node->name, name, (EXFAT_NAME_MAX + 1) * sizeof(le16_t));
996 tree_attach(dir, node);
1000 int exfat_rename(struct exfat* ef, const char* old_path, const char* new_path)
1002 struct exfat_node* node;
1003 struct exfat_node* existing;
1004 struct exfat_node* dir;
1005 cluster_t cluster = EXFAT_CLUSTER_BAD;
1007 le16_t name[EXFAT_NAME_MAX + 1];
1010 rc = exfat_lookup(ef, &node, old_path);
1014 rc = exfat_split(ef, &dir, &existing, name, new_path);
1017 exfat_put_node(ef, node);
1021 /* check that target is not a subdirectory of the source */
1022 if (node->flags & EXFAT_ATTRIB_DIR)
1024 struct exfat_node* p;
1026 for (p = dir; p; p = p->parent)
1029 if (existing != NULL)
1030 exfat_put_node(ef, existing);
1031 exfat_put_node(ef, dir);
1032 exfat_put_node(ef, node);
1037 if (existing != NULL)
1039 /* remove target if it's not the same node as source */
1040 if (existing != node)
1042 if (existing->flags & EXFAT_ATTRIB_DIR)
1044 if (node->flags & EXFAT_ATTRIB_DIR)
1045 rc = exfat_rmdir(ef, existing);
1051 if (!(node->flags & EXFAT_ATTRIB_DIR))
1052 rc = exfat_unlink(ef, existing);
1056 exfat_put_node(ef, existing);
1059 exfat_put_node(ef, dir);
1060 exfat_put_node(ef, node);
1065 exfat_put_node(ef, existing);
1068 rc = find_slot(ef, dir, &cluster, &offset,
1069 2 + DIV_ROUND_UP(utf16_length(name), EXFAT_ENAME_MAX));
1072 exfat_put_node(ef, dir);
1073 exfat_put_node(ef, node);
1076 rc = rename_entry(ef, dir, node, name, cluster, offset);
1077 exfat_put_node(ef, dir);
1078 exfat_put_node(ef, node);
1082 void exfat_utimes(struct exfat_node* node, const struct timespec tv[2])
1084 node->atime = tv[0].tv_sec;
1085 node->mtime = tv[1].tv_sec;
1086 node->flags |= EXFAT_ATTRIB_DIRTY;
1089 void exfat_update_atime(struct exfat_node* node)
1091 node->atime = time(NULL);
1092 node->flags |= EXFAT_ATTRIB_DIRTY;
1095 void exfat_update_mtime(struct exfat_node* node)
1097 node->mtime = time(NULL);
1098 node->flags |= EXFAT_ATTRIB_DIRTY;
1101 const char* exfat_get_label(struct exfat* ef)
1106 static int find_label(struct exfat* ef, cluster_t* cluster, off_t* offset)
1111 rc = opendir(ef, ef->root, &it);
1117 if (it.offset >= ef->root->size)
1123 if (get_entry_ptr(ef, &it)->type == EXFAT_ENTRY_LABEL)
1125 *cluster = it.cluster;
1126 *offset = it.offset;
1131 if (fetch_next_entry(ef, ef->root, &it) != 0)
1139 int exfat_set_label(struct exfat* ef, const char* label)
1141 le16_t label_utf16[EXFAT_ENAME_MAX + 1];
1145 struct exfat_entry_label entry;
1147 memset(label_utf16, 0, sizeof(label_utf16));
1148 rc = utf8_to_utf16(label_utf16, label, EXFAT_ENAME_MAX, strlen(label));
1152 rc = find_label(ef, &cluster, &offset);
1154 rc = find_slot(ef, ef->root, &cluster, &offset, 1);
1158 entry.type = EXFAT_ENTRY_LABEL;
1159 entry.length = utf16_length(label_utf16);
1160 memcpy(entry.name, label_utf16, sizeof(entry.name));
1161 if (entry.length == 0)
1162 entry.type ^= EXFAT_ENTRY_VALID;
1164 if (exfat_pwrite(ef->dev, &entry, sizeof(struct exfat_entry_label),
1165 co2o(ef, cluster, offset)) < 0)
1167 exfat_error("failed to write label entry");
1170 strcpy(ef->label, label);