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));
180 static bool check_node(const struct exfat_node* node, uint16_t actual_checksum,
181 uint16_t reference_checksum, uint64_t real_size)
183 char buffer[UTF8_BYTES(EXFAT_NAME_MAX) + 1];
186 Validate checksum first. If it's invalid all other fields probably
187 contain just garbage.
189 if (actual_checksum != reference_checksum)
191 exfat_get_name(node, buffer, sizeof(buffer) - 1);
192 exfat_error("`%s' has invalid checksum (%#hx != %#hx)", buffer,
193 actual_checksum, reference_checksum);
198 It's unclear what is real_size field needed for. It usually equals to
199 the size but may contain any value less than size (including 0).
201 if (real_size > node->size)
203 exfat_get_name(node, buffer, sizeof(buffer) - 1);
204 exfat_error("`%s' has real size (%"PRIu64") greater than size "
205 "(%"PRIu64")", buffer, real_size, node->size);
213 * Reads one entry in directory at position pointed by iterator and fills
216 static int readdir(struct exfat* ef, const struct exfat_node* parent,
217 struct exfat_node** node, struct iterator* it)
220 const struct exfat_entry* entry;
221 const struct exfat_entry_meta1* meta1;
222 const struct exfat_entry_meta2* meta2;
223 const struct exfat_entry_name* file_name;
224 const struct exfat_entry_upcase* upcase;
225 const struct exfat_entry_bitmap* bitmap;
226 const struct exfat_entry_label* label;
227 uint8_t continuations = 0;
228 le16_t* namep = NULL;
229 uint16_t reference_checksum = 0;
230 uint16_t actual_checksum = 0;
231 uint64_t real_size = 0;
237 if (it->offset >= parent->size)
239 if (continuations != 0)
241 exfat_error("expected %hhu continuations", continuations);
244 return -ENOENT; /* that's OK, means end of directory */
247 entry = get_entry_ptr(ef, it);
250 case EXFAT_ENTRY_FILE:
251 if (continuations != 0)
253 exfat_error("expected %hhu continuations before new entry",
257 meta1 = (const struct exfat_entry_meta1*) entry;
258 continuations = meta1->continuations;
259 /* each file entry must have at least 2 continuations:
261 if (continuations < 2)
263 exfat_error("too few continuations (%hhu)", continuations);
266 if (continuations > 1 +
267 DIV_ROUND_UP(EXFAT_NAME_MAX, EXFAT_ENAME_MAX))
269 exfat_error("too many continuations (%hhu)", continuations);
272 reference_checksum = le16_to_cpu(meta1->checksum);
273 actual_checksum = exfat_start_checksum(meta1);
274 *node = allocate_node();
280 /* new node has zero reference counter */
281 (*node)->entry_cluster = it->cluster;
282 (*node)->entry_offset = it->offset;
283 init_node_meta1(*node, meta1);
284 namep = (*node)->name;
287 case EXFAT_ENTRY_FILE_INFO:
288 if (continuations < 2)
290 exfat_error("unexpected continuation (%hhu)",
294 meta2 = (const struct exfat_entry_meta2*) entry;
295 if (meta2->flags & ~(EXFAT_FLAG_ALWAYS1 | EXFAT_FLAG_CONTIGUOUS))
297 exfat_error("unknown flags in meta2 (0x%hhx)", meta2->flags);
300 init_node_meta2(*node, meta2);
301 actual_checksum = exfat_add_checksum(entry, actual_checksum);
302 real_size = le64_to_cpu(meta2->real_size);
303 /* empty files must be marked as non-contiguous */
304 if ((*node)->size == 0 && (meta2->flags & EXFAT_FLAG_CONTIGUOUS))
306 exfat_error("empty file marked as contiguous (0x%hhx)",
310 /* directories must be aligned on at cluster boundary */
311 if (((*node)->flags & EXFAT_ATTRIB_DIR) &&
312 (*node)->size % CLUSTER_SIZE(*ef->sb) != 0)
314 exfat_error("directory has invalid size %"PRIu64" bytes",
321 case EXFAT_ENTRY_FILE_NAME:
322 if (continuations == 0)
324 exfat_error("unexpected continuation");
327 file_name = (const struct exfat_entry_name*) entry;
328 actual_checksum = exfat_add_checksum(entry, actual_checksum);
330 memcpy(namep, file_name->name,
332 ((*node)->name + EXFAT_NAME_MAX - namep)) *
334 namep += EXFAT_ENAME_MAX;
335 if (--continuations == 0)
337 if (!check_node(*node, actual_checksum, reference_checksum,
340 if (fetch_next_entry(ef, parent, it) != 0)
342 return 0; /* entry completed */
346 case EXFAT_ENTRY_UPCASE:
347 if (ef->upcase != NULL)
349 upcase = (const struct exfat_entry_upcase*) entry;
350 if (CLUSTER_INVALID(le32_to_cpu(upcase->start_cluster)))
352 exfat_error("invalid cluster 0x%x in upcase table",
353 le32_to_cpu(upcase->start_cluster));
356 if (le64_to_cpu(upcase->size) == 0 ||
357 le64_to_cpu(upcase->size) > 0xffff * sizeof(uint16_t) ||
358 le64_to_cpu(upcase->size) % sizeof(uint16_t) != 0)
360 exfat_error("bad upcase table size (%"PRIu64" bytes)",
361 le64_to_cpu(upcase->size));
364 ef->upcase = malloc(le64_to_cpu(upcase->size));
365 if (ef->upcase == NULL)
367 exfat_error("failed to allocate upcase table (%"PRIu64" bytes)",
368 le64_to_cpu(upcase->size));
372 ef->upcase_chars = le64_to_cpu(upcase->size) / sizeof(le16_t);
374 if (exfat_pread(ef->dev, ef->upcase, le64_to_cpu(upcase->size),
375 exfat_c2o(ef, le32_to_cpu(upcase->start_cluster))) < 0)
377 exfat_error("failed to read upper case table "
378 "(%"PRIu64" bytes starting at cluster %#x)",
379 le64_to_cpu(upcase->size),
380 le32_to_cpu(upcase->start_cluster));
385 case EXFAT_ENTRY_BITMAP:
386 bitmap = (const struct exfat_entry_bitmap*) entry;
387 ef->cmap.start_cluster = le32_to_cpu(bitmap->start_cluster);
388 if (CLUSTER_INVALID(ef->cmap.start_cluster))
390 exfat_error("invalid cluster 0x%x in clusters bitmap",
391 ef->cmap.start_cluster);
394 ef->cmap.size = le32_to_cpu(ef->sb->cluster_count) -
395 EXFAT_FIRST_DATA_CLUSTER;
396 if (le64_to_cpu(bitmap->size) < DIV_ROUND_UP(ef->cmap.size, 8))
398 exfat_error("invalid clusters bitmap size: %"PRIu64
399 " (expected at least %u)",
400 le64_to_cpu(bitmap->size),
401 DIV_ROUND_UP(ef->cmap.size, 8));
404 /* FIXME bitmap can be rather big, up to 512 MB */
405 ef->cmap.chunk_size = ef->cmap.size;
406 ef->cmap.chunk = malloc(BMAP_SIZE(ef->cmap.chunk_size));
407 if (ef->cmap.chunk == NULL)
409 exfat_error("failed to allocate clusters bitmap chunk "
410 "(%"PRIu64" bytes)", le64_to_cpu(bitmap->size));
415 if (exfat_pread(ef->dev, ef->cmap.chunk,
416 BMAP_SIZE(ef->cmap.chunk_size),
417 exfat_c2o(ef, ef->cmap.start_cluster)) < 0)
419 exfat_error("failed to read clusters bitmap "
420 "(%"PRIu64" bytes starting at cluster %#x)",
421 le64_to_cpu(bitmap->size), ef->cmap.start_cluster);
426 case EXFAT_ENTRY_LABEL:
427 label = (const struct exfat_entry_label*) entry;
428 if (label->length > EXFAT_ENAME_MAX)
430 exfat_error("too long label (%hhu chars)", label->length);
433 if (utf16_to_utf8(ef->label, label->name,
434 sizeof(ef->label) - 1, EXFAT_ENAME_MAX) != 0)
439 if (entry->type & EXFAT_ENTRY_VALID)
441 exfat_error("unknown entry type 0x%hhx", entry->type);
447 if (fetch_next_entry(ef, parent, it) != 0)
450 /* we never reach here */
458 int exfat_cache_directory(struct exfat* ef, struct exfat_node* dir)
462 struct exfat_node* node;
463 struct exfat_node* current = NULL;
465 if (dir->flags & EXFAT_ATTRIB_CACHED)
466 return 0; /* already cached */
468 rc = opendir(ef, dir, &it);
471 while ((rc = readdir(ef, dir, &node, &it)) == 0)
476 current->next = node;
477 node->prev = current;
489 for (current = dir->child; current; current = node)
491 node = current->next;
498 dir->flags |= EXFAT_ATTRIB_CACHED;
502 static void tree_attach(struct exfat_node* dir, struct exfat_node* node)
507 dir->child->prev = node;
508 node->next = dir->child;
513 static void tree_detach(struct exfat_node* node)
516 node->prev->next = node->next;
517 else /* this is the first node in the list */
518 node->parent->child = node->next;
520 node->next->prev = node->prev;
526 static void reset_cache(struct exfat* ef, struct exfat_node* node)
530 struct exfat_node* p = node->child;
535 node->flags &= ~EXFAT_ATTRIB_CACHED;
536 if (node->references != 0)
538 char buffer[UTF8_BYTES(EXFAT_NAME_MAX) + 1];
539 exfat_get_name(node, buffer, sizeof(buffer) - 1);
540 exfat_warn("non-zero reference counter (%d) for `%s'",
541 node->references, buffer);
543 while (node->references)
544 exfat_put_node(ef, node);
547 void exfat_reset_cache(struct exfat* ef)
549 reset_cache(ef, ef->root);
552 static bool next_entry(struct exfat* ef, const struct exfat_node* parent,
553 cluster_t* cluster, off_t* offset)
555 *offset += sizeof(struct exfat_entry);
556 if (*offset % CLUSTER_SIZE(*ef->sb) == 0)
558 *cluster = exfat_next_cluster(ef, parent, *cluster);
559 if (CLUSTER_INVALID(*cluster))
561 exfat_error("invalid cluster %#x while getting next entry",
569 int exfat_flush_node(struct exfat* ef, struct exfat_node* node)
573 off_t meta1_offset, meta2_offset;
574 struct exfat_entry_meta1 meta1;
575 struct exfat_entry_meta2 meta2;
577 if (!(node->flags & EXFAT_ATTRIB_DIRTY))
578 return 0; /* no need to flush */
581 exfat_bug("unable to flush node to read-only FS");
583 if (node->parent == NULL)
584 return 0; /* do not flush unlinked node */
586 cluster = node->entry_cluster;
587 offset = node->entry_offset;
588 meta1_offset = co2o(ef, cluster, offset);
589 if (!next_entry(ef, node->parent, &cluster, &offset))
591 meta2_offset = co2o(ef, cluster, offset);
593 if (exfat_pread(ef->dev, &meta1, sizeof(meta1), meta1_offset) < 0)
595 exfat_error("failed to read meta1 entry on flush");
598 if (meta1.type != EXFAT_ENTRY_FILE)
599 exfat_bug("invalid type of meta1: 0x%hhx", meta1.type);
600 meta1.attrib = cpu_to_le16(node->flags);
601 exfat_unix2exfat(node->mtime, &meta1.mdate, &meta1.mtime, &meta1.mtime_cs);
602 exfat_unix2exfat(node->atime, &meta1.adate, &meta1.atime, NULL);
604 if (exfat_pread(ef->dev, &meta2, sizeof(meta2), meta2_offset) < 0)
606 exfat_error("failed to read meta2 entry on flush");
609 if (meta2.type != EXFAT_ENTRY_FILE_INFO)
610 exfat_bug("invalid type of meta2: 0x%hhx", meta2.type);
611 meta2.size = meta2.real_size = cpu_to_le64(node->size);
612 meta2.start_cluster = cpu_to_le32(node->start_cluster);
613 meta2.flags = EXFAT_FLAG_ALWAYS1;
614 /* empty files must not be marked as contiguous */
615 if (node->size != 0 && IS_CONTIGUOUS(*node))
616 meta2.flags |= EXFAT_FLAG_CONTIGUOUS;
617 /* name hash remains unchanged, no need to recalculate it */
619 meta1.checksum = exfat_calc_checksum(&meta1, &meta2, node->name);
621 if (exfat_pwrite(ef->dev, &meta1, sizeof(meta1), meta1_offset) < 0)
623 exfat_error("failed to write meta1 entry on flush");
626 if (exfat_pwrite(ef->dev, &meta2, sizeof(meta2), meta2_offset) < 0)
628 exfat_error("failed to write meta2 entry on flush");
632 node->flags &= ~EXFAT_ATTRIB_DIRTY;
636 static bool erase_entry(struct exfat* ef, struct exfat_node* node)
638 cluster_t cluster = node->entry_cluster;
639 off_t offset = node->entry_offset;
640 int name_entries = DIV_ROUND_UP(utf16_length(node->name), EXFAT_ENAME_MAX);
643 entry_type = EXFAT_ENTRY_FILE & ~EXFAT_ENTRY_VALID;
644 if (exfat_pwrite(ef->dev, &entry_type, 1, co2o(ef, cluster, offset)) < 0)
646 exfat_error("failed to erase meta1 entry");
650 if (!next_entry(ef, node->parent, &cluster, &offset))
652 entry_type = EXFAT_ENTRY_FILE_INFO & ~EXFAT_ENTRY_VALID;
653 if (exfat_pwrite(ef->dev, &entry_type, 1, co2o(ef, cluster, offset)) < 0)
655 exfat_error("failed to erase meta2 entry");
659 while (name_entries--)
661 if (!next_entry(ef, node->parent, &cluster, &offset))
663 entry_type = EXFAT_ENTRY_FILE_NAME & ~EXFAT_ENTRY_VALID;
664 if (exfat_pwrite(ef->dev, &entry_type, 1,
665 co2o(ef, cluster, offset)) < 0)
667 exfat_error("failed to erase name entry");
674 static int shrink_directory(struct exfat* ef, struct exfat_node* dir,
675 off_t deleted_offset)
677 const struct exfat_node* node;
678 const struct exfat_node* last_node;
679 uint64_t entries = 0;
682 if (!(dir->flags & EXFAT_ATTRIB_DIR))
683 exfat_bug("attempted to shrink a file");
684 if (!(dir->flags & EXFAT_ATTRIB_CACHED))
685 exfat_bug("attempted to shrink uncached directory");
687 for (last_node = node = dir->child; node; node = node->next)
689 if (deleted_offset < node->entry_offset)
691 /* there are other entries after the removed one, no way to shrink
695 if (last_node->entry_offset < node->entry_offset)
701 /* offset of the last entry */
702 entries += last_node->entry_offset / sizeof(struct exfat_entry);
703 /* two subentries with meta info */
705 /* subentries with file name */
706 entries += DIV_ROUND_UP(utf16_length(last_node->name),
710 new_size = DIV_ROUND_UP(entries * sizeof(struct exfat_entry),
711 CLUSTER_SIZE(*ef->sb)) * CLUSTER_SIZE(*ef->sb);
712 if (new_size == 0) /* directory always has at least 1 cluster */
713 new_size = CLUSTER_SIZE(*ef->sb);
714 if (new_size == dir->size)
716 return exfat_truncate(ef, dir, new_size, true);
719 static int delete(struct exfat* ef, struct exfat_node* node)
721 struct exfat_node* parent = node->parent;
722 off_t deleted_offset = node->entry_offset;
725 exfat_get_node(parent);
726 if (!erase_entry(ef, node))
728 exfat_put_node(ef, parent);
731 exfat_update_mtime(parent);
733 rc = shrink_directory(ef, parent, deleted_offset);
734 exfat_put_node(ef, parent);
735 /* file clusters will be freed when node reference counter becomes 0 */
736 node->flags |= EXFAT_ATTRIB_UNLINKED;
740 int exfat_unlink(struct exfat* ef, struct exfat_node* node)
742 if (node->flags & EXFAT_ATTRIB_DIR)
744 return delete(ef, node);
747 int exfat_rmdir(struct exfat* ef, struct exfat_node* node)
749 if (!(node->flags & EXFAT_ATTRIB_DIR))
751 /* check that directory is empty */
752 exfat_cache_directory(ef, node);
755 return delete(ef, node);
758 static int grow_directory(struct exfat* ef, struct exfat_node* dir,
759 uint64_t asize, uint32_t difference)
761 return exfat_truncate(ef, dir,
762 DIV_ROUND_UP(asize + difference, CLUSTER_SIZE(*ef->sb))
763 * CLUSTER_SIZE(*ef->sb), true);
766 static int find_slot(struct exfat* ef, struct exfat_node* dir,
767 cluster_t* cluster, off_t* offset, int subentries)
771 const struct exfat_entry* entry;
774 rc = opendir(ef, dir, &it);
781 *cluster = it.cluster;
784 entry = get_entry_ptr(ef, &it);
785 if (entry->type & EXFAT_ENTRY_VALID)
789 if (contiguous == subentries)
790 break; /* suitable slot is found */
791 if (it.offset + sizeof(struct exfat_entry) >= dir->size)
793 rc = grow_directory(ef, dir, dir->size,
794 (subentries - contiguous) * sizeof(struct exfat_entry));
801 if (fetch_next_entry(ef, dir, &it) != 0)
811 static int write_entry(struct exfat* ef, struct exfat_node* dir,
812 const le16_t* name, cluster_t cluster, off_t offset, uint16_t attrib)
814 struct exfat_node* node;
815 struct exfat_entry_meta1 meta1;
816 struct exfat_entry_meta2 meta2;
817 const size_t name_length = utf16_length(name);
818 const int name_entries = DIV_ROUND_UP(name_length, EXFAT_ENAME_MAX);
821 node = allocate_node();
824 node->entry_cluster = cluster;
825 node->entry_offset = offset;
826 memcpy(node->name, name, name_length * sizeof(le16_t));
828 memset(&meta1, 0, sizeof(meta1));
829 meta1.type = EXFAT_ENTRY_FILE;
830 meta1.continuations = 1 + name_entries;
831 meta1.attrib = cpu_to_le16(attrib);
832 exfat_unix2exfat(time(NULL), &meta1.crdate, &meta1.crtime,
834 meta1.adate = meta1.mdate = meta1.crdate;
835 meta1.atime = meta1.mtime = meta1.crtime;
836 meta1.mtime_cs = meta1.crtime_cs; /* there is no atime_cs */
838 memset(&meta2, 0, sizeof(meta2));
839 meta2.type = EXFAT_ENTRY_FILE_INFO;
840 meta2.flags = EXFAT_FLAG_ALWAYS1;
841 meta2.name_length = name_length;
842 meta2.name_hash = exfat_calc_name_hash(ef, node->name);
843 meta2.start_cluster = cpu_to_le32(EXFAT_CLUSTER_FREE);
845 meta1.checksum = exfat_calc_checksum(&meta1, &meta2, node->name);
847 if (exfat_pwrite(ef->dev, &meta1, sizeof(meta1),
848 co2o(ef, cluster, offset)) < 0)
850 exfat_error("failed to write meta1 entry");
853 if (!next_entry(ef, dir, &cluster, &offset))
855 if (exfat_pwrite(ef->dev, &meta2, sizeof(meta2),
856 co2o(ef, cluster, offset)) < 0)
858 exfat_error("failed to write meta2 entry");
861 for (i = 0; i < name_entries; i++)
863 struct exfat_entry_name name_entry = {EXFAT_ENTRY_FILE_NAME, 0};
864 memcpy(name_entry.name, node->name + i * EXFAT_ENAME_MAX,
865 MIN(EXFAT_ENAME_MAX, EXFAT_NAME_MAX - i * EXFAT_ENAME_MAX) *
867 if (!next_entry(ef, dir, &cluster, &offset))
869 if (exfat_pwrite(ef->dev, &name_entry, sizeof(name_entry),
870 co2o(ef, cluster, offset)) < 0)
872 exfat_error("failed to write name entry");
877 init_node_meta1(node, &meta1);
878 init_node_meta2(node, &meta2);
880 tree_attach(dir, node);
881 exfat_update_mtime(dir);
885 static int create(struct exfat* ef, const char* path, uint16_t attrib)
887 struct exfat_node* dir;
888 struct exfat_node* existing;
889 cluster_t cluster = EXFAT_CLUSTER_BAD;
891 le16_t name[EXFAT_NAME_MAX + 1];
894 rc = exfat_split(ef, &dir, &existing, name, path);
897 if (existing != NULL)
899 exfat_put_node(ef, existing);
900 exfat_put_node(ef, dir);
904 rc = find_slot(ef, dir, &cluster, &offset,
905 2 + DIV_ROUND_UP(utf16_length(name), EXFAT_ENAME_MAX));
908 exfat_put_node(ef, dir);
911 rc = write_entry(ef, dir, name, cluster, offset, attrib);
912 exfat_put_node(ef, dir);
916 int exfat_mknod(struct exfat* ef, const char* path)
918 return create(ef, path, EXFAT_ATTRIB_ARCH);
921 int exfat_mkdir(struct exfat* ef, const char* path)
924 struct exfat_node* node;
926 rc = create(ef, path, EXFAT_ATTRIB_ARCH | EXFAT_ATTRIB_DIR);
929 rc = exfat_lookup(ef, &node, path);
932 /* directories always have at least one cluster */
933 rc = exfat_truncate(ef, node, CLUSTER_SIZE(*ef->sb), true);
937 exfat_put_node(ef, node);
940 exfat_put_node(ef, node);
944 static int rename_entry(struct exfat* ef, struct exfat_node* dir,
945 struct exfat_node* node, const le16_t* name, cluster_t new_cluster,
948 struct exfat_entry_meta1 meta1;
949 struct exfat_entry_meta2 meta2;
950 cluster_t old_cluster = node->entry_cluster;
951 off_t old_offset = node->entry_offset;
952 const size_t name_length = utf16_length(name);
953 const int name_entries = DIV_ROUND_UP(name_length, EXFAT_ENAME_MAX);
956 if (exfat_pread(ef->dev, &meta1, sizeof(meta1),
957 co2o(ef, old_cluster, old_offset)) < 0)
959 exfat_error("failed to read meta1 entry on rename");
962 if (!next_entry(ef, node->parent, &old_cluster, &old_offset))
964 if (exfat_pread(ef->dev, &meta2, sizeof(meta2),
965 co2o(ef, old_cluster, old_offset)) < 0)
967 exfat_error("failed to read meta2 entry on rename");
970 meta1.continuations = 1 + name_entries;
971 meta2.name_hash = exfat_calc_name_hash(ef, name);
972 meta2.name_length = name_length;
973 meta1.checksum = exfat_calc_checksum(&meta1, &meta2, name);
975 if (!erase_entry(ef, node))
978 node->entry_cluster = new_cluster;
979 node->entry_offset = new_offset;
981 if (exfat_pwrite(ef->dev, &meta1, sizeof(meta1),
982 co2o(ef, new_cluster, new_offset)) < 0)
984 exfat_error("failed to write meta1 entry on rename");
987 if (!next_entry(ef, dir, &new_cluster, &new_offset))
989 if (exfat_pwrite(ef->dev, &meta2, sizeof(meta2),
990 co2o(ef, new_cluster, new_offset)) < 0)
992 exfat_error("failed to write meta2 entry on rename");
996 for (i = 0; i < name_entries; i++)
998 struct exfat_entry_name name_entry = {EXFAT_ENTRY_FILE_NAME, 0};
999 memcpy(name_entry.name, name + i * EXFAT_ENAME_MAX,
1000 EXFAT_ENAME_MAX * sizeof(le16_t));
1001 if (!next_entry(ef, dir, &new_cluster, &new_offset))
1003 if (exfat_pwrite(ef->dev, &name_entry, sizeof(name_entry),
1004 co2o(ef, new_cluster, new_offset)) < 0)
1006 exfat_error("failed to write name entry on rename");
1011 memcpy(node->name, name, (EXFAT_NAME_MAX + 1) * sizeof(le16_t));
1013 tree_attach(dir, node);
1017 int exfat_rename(struct exfat* ef, const char* old_path, const char* new_path)
1019 struct exfat_node* node;
1020 struct exfat_node* existing;
1021 struct exfat_node* dir;
1022 cluster_t cluster = EXFAT_CLUSTER_BAD;
1024 le16_t name[EXFAT_NAME_MAX + 1];
1027 rc = exfat_lookup(ef, &node, old_path);
1031 rc = exfat_split(ef, &dir, &existing, name, new_path);
1034 exfat_put_node(ef, node);
1038 /* check that target is not a subdirectory of the source */
1039 if (node->flags & EXFAT_ATTRIB_DIR)
1041 struct exfat_node* p;
1043 for (p = dir; p; p = p->parent)
1046 if (existing != NULL)
1047 exfat_put_node(ef, existing);
1048 exfat_put_node(ef, dir);
1049 exfat_put_node(ef, node);
1054 if (existing != NULL)
1056 /* remove target if it's not the same node as source */
1057 if (existing != node)
1059 if (existing->flags & EXFAT_ATTRIB_DIR)
1061 if (node->flags & EXFAT_ATTRIB_DIR)
1062 rc = exfat_rmdir(ef, existing);
1068 if (!(node->flags & EXFAT_ATTRIB_DIR))
1069 rc = exfat_unlink(ef, existing);
1073 exfat_put_node(ef, existing);
1076 exfat_put_node(ef, dir);
1077 exfat_put_node(ef, node);
1082 exfat_put_node(ef, existing);
1085 rc = find_slot(ef, dir, &cluster, &offset,
1086 2 + DIV_ROUND_UP(utf16_length(name), EXFAT_ENAME_MAX));
1089 exfat_put_node(ef, dir);
1090 exfat_put_node(ef, node);
1093 rc = rename_entry(ef, dir, node, name, cluster, offset);
1094 exfat_put_node(ef, dir);
1095 exfat_put_node(ef, node);
1099 void exfat_utimes(struct exfat_node* node, const struct timespec tv[2])
1101 node->atime = tv[0].tv_sec;
1102 node->mtime = tv[1].tv_sec;
1103 node->flags |= EXFAT_ATTRIB_DIRTY;
1106 void exfat_update_atime(struct exfat_node* node)
1108 node->atime = time(NULL);
1109 node->flags |= EXFAT_ATTRIB_DIRTY;
1112 void exfat_update_mtime(struct exfat_node* node)
1114 node->mtime = time(NULL);
1115 node->flags |= EXFAT_ATTRIB_DIRTY;
1118 const char* exfat_get_label(struct exfat* ef)
1123 static int find_label(struct exfat* ef, cluster_t* cluster, off_t* offset)
1128 rc = opendir(ef, ef->root, &it);
1134 if (it.offset >= ef->root->size)
1140 if (get_entry_ptr(ef, &it)->type == EXFAT_ENTRY_LABEL)
1142 *cluster = it.cluster;
1143 *offset = it.offset;
1148 if (fetch_next_entry(ef, ef->root, &it) != 0)
1156 int exfat_set_label(struct exfat* ef, const char* label)
1158 le16_t label_utf16[EXFAT_ENAME_MAX + 1];
1162 struct exfat_entry_label entry;
1164 memset(label_utf16, 0, sizeof(label_utf16));
1165 rc = utf8_to_utf16(label_utf16, label, EXFAT_ENAME_MAX, strlen(label));
1169 rc = find_label(ef, &cluster, &offset);
1171 rc = find_slot(ef, ef->root, &cluster, &offset, 1);
1175 entry.type = EXFAT_ENTRY_LABEL;
1176 entry.length = utf16_length(label_utf16);
1177 memcpy(entry.name, label_utf16, sizeof(entry.name));
1178 if (entry.length == 0)
1179 entry.type ^= EXFAT_ENTRY_VALID;
1181 if (exfat_pwrite(ef->dev, &entry, sizeof(struct exfat_entry_label),
1182 co2o(ef, cluster, offset)) < 0)
1184 exfat_error("failed to write label entry");
1187 strcpy(ef->label, label);