]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - LinuxGUI/Ventoy2Disk/Lib/exfat/src/libexfat/node.c
Add Linux native GUI program for Ventoy2Disk.
[Ventoy.git] / LinuxGUI / Ventoy2Disk / Lib / exfat / src / libexfat / node.c
1 /*
2 node.c (09.10.09)
3 exFAT file system implementation library.
4
5 Free exFAT implementation.
6 Copyright (C) 2010-2018 Andrew Nayenko
7
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.
12
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.
17
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.
21 */
22
23 #include "exfat.h"
24 #include <errno.h>
25 #include <string.h>
26 #include <inttypes.h>
27
28 #define EXFAT_ENTRY_NONE (-1)
29
30 struct exfat_node* exfat_get_node(struct exfat_node* node)
31 {
32 /* if we switch to multi-threaded mode we will need atomic
33 increment here and atomic decrement in exfat_put_node() */
34 node->references++;
35 return node;
36 }
37
38 void exfat_put_node(struct exfat* ef, struct exfat_node* node)
39 {
40 char buffer[EXFAT_UTF8_NAME_BUFFER_MAX];
41
42 --node->references;
43 if (node->references < 0)
44 {
45 exfat_get_name(node, buffer);
46 exfat_bug("reference counter of '%s' is below zero", buffer);
47 }
48 else if (node->references == 0 && node != ef->root)
49 {
50 if (node->is_dirty)
51 {
52 exfat_get_name(node, buffer);
53 exfat_warn("dirty node '%s' with zero references", buffer);
54 }
55 }
56 }
57
58 /**
59 * This function must be called on rmdir and unlink (after the last
60 * exfat_put_node()) to free clusters.
61 */
62 int exfat_cleanup_node(struct exfat* ef, struct exfat_node* node)
63 {
64 int rc = 0;
65
66 if (node->references != 0)
67 exfat_bug("unable to cleanup a node with %d references",
68 node->references);
69
70 if (node->is_unlinked)
71 {
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 */
75 free(node);
76 }
77 return rc;
78 }
79
80 static int read_entries(struct exfat* ef, struct exfat_node* dir,
81 struct exfat_entry* entries, int n, off_t offset)
82 {
83 ssize_t size;
84
85 if (!(dir->attrib & EXFAT_ATTRIB_DIR))
86 exfat_bug("attempted to read entries from a file");
87
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 */
92 if (size == 0)
93 return -ENOENT;
94 if (size < 0)
95 return -EIO;
96 exfat_error("read %zd bytes instead of %zu bytes", size,
97 sizeof(struct exfat_entry[n]));
98 return -EIO;
99 }
100
101 static int write_entries(struct exfat* ef, struct exfat_node* dir,
102 const struct exfat_entry* entries, int n, off_t offset)
103 {
104 ssize_t size;
105
106 if (!(dir->attrib & EXFAT_ATTRIB_DIR))
107 exfat_bug("attempted to write entries into a file");
108
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 */
113 if (size < 0)
114 return -EIO;
115 exfat_error("wrote %zd bytes instead of %zu bytes", size,
116 sizeof(struct exfat_entry[n]));
117 return -EIO;
118 }
119
120 static struct exfat_node* allocate_node(void)
121 {
122 struct exfat_node* node = malloc(sizeof(struct exfat_node));
123 if (node == NULL)
124 {
125 exfat_error("failed to allocate node");
126 return NULL;
127 }
128 memset(node, 0, sizeof(struct exfat_node));
129 return node;
130 }
131
132 static void init_node_meta1(struct exfat_node* node,
133 const struct exfat_entry_meta1* meta1)
134 {
135 node->attrib = le16_to_cpu(meta1->attrib);
136 node->continuations = meta1->continuations;
137 node->mtime = exfat_exfat2unix(meta1->mdate, meta1->mtime,
138 meta1->mtime_cs);
139 /* there is no centiseconds field for atime */
140 node->atime = exfat_exfat2unix(meta1->adate, meta1->atime, 0);
141 }
142
143 static void init_node_meta2(struct exfat_node* node,
144 const struct exfat_entry_meta2* meta2)
145 {
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);
150 }
151
152 static void init_node_name(struct exfat_node* node,
153 const struct exfat_entry* entries, int n)
154 {
155 int i;
156
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));
161 }
162
163 static bool check_entries(const struct exfat_entry* entry, int n)
164 {
165 int previous = EXFAT_ENTRY_NONE;
166 int current;
167 int i;
168
169 /* check transitions between entries types */
170 for (i = 0; i < n + 1; previous = current, i++)
171 {
172 bool valid = false;
173
174 current = (i < n) ? entry[i].type : EXFAT_ENTRY_NONE;
175 switch (previous)
176 {
177 case EXFAT_ENTRY_NONE:
178 valid = (current == EXFAT_ENTRY_FILE);
179 break;
180 case EXFAT_ENTRY_FILE:
181 valid = (current == EXFAT_ENTRY_FILE_INFO);
182 break;
183 case EXFAT_ENTRY_FILE_INFO:
184 valid = (current == EXFAT_ENTRY_FILE_NAME);
185 break;
186 case EXFAT_ENTRY_FILE_NAME:
187 valid = (current == EXFAT_ENTRY_FILE_NAME ||
188 current == EXFAT_ENTRY_NONE ||
189 current >= EXFAT_ENTRY_FILE_TAIL);
190 break;
191 case EXFAT_ENTRY_FILE_TAIL ... 0xff:
192 valid = (current >= EXFAT_ENTRY_FILE_TAIL ||
193 current == EXFAT_ENTRY_NONE);
194 break;
195 }
196
197 if (!valid)
198 {
199 exfat_error("unexpected entry type %#x after %#x at %d/%d",
200 current, previous, i, n);
201 return false;
202 }
203 }
204 return true;
205 }
206
207 static bool check_node(const struct exfat* ef, struct exfat_node* node,
208 le16_t actual_checksum, const struct exfat_entry_meta1* meta1,
209 const struct exfat_entry_meta2* meta2)
210 {
211 int cluster_size = CLUSTER_SIZE(*ef->sb);
212 uint64_t clusters_heap_size =
213 (uint64_t) le32_to_cpu(ef->sb->cluster_count) * cluster_size;
214 char buffer[EXFAT_UTF8_NAME_BUFFER_MAX];
215 bool ret = true;
216
217 /*
218 Validate checksum first. If it's invalid all other fields probably
219 contain just garbage.
220 */
221 if (le16_to_cpu(actual_checksum) != le16_to_cpu(meta1->checksum))
222 {
223 exfat_get_name(node, buffer);
224 exfat_error("'%s' has invalid checksum (%#hx != %#hx)", buffer,
225 le16_to_cpu(actual_checksum), le16_to_cpu(meta1->checksum));
226 if (!EXFAT_REPAIR(invalid_node_checksum, ef, node))
227 ret = false;
228 }
229
230 /*
231 exFAT does not support sparse files but allows files with uninitialized
232 clusters. For such files valid_size means initialized data size and
233 cannot be greater than file size. See SetFileValidData() function
234 description in MSDN.
235 */
236 if (le64_to_cpu(meta2->valid_size) > node->size)
237 {
238 exfat_get_name(node, buffer);
239 exfat_error("'%s' has valid size (%"PRIu64") greater than size "
240 "(%"PRIu64")", buffer, le64_to_cpu(meta2->valid_size),
241 node->size);
242 ret = false;
243 }
244
245 /*
246 Empty file must have zero start cluster. Non-empty file must start
247 with a valid cluster. Directories cannot be empty (i.e. must always
248 have a valid start cluster), but we will check this later while
249 reading that directory to give user a chance to read this directory.
250 */
251 if (node->size == 0 && node->start_cluster != EXFAT_CLUSTER_FREE)
252 {
253 exfat_get_name(node, buffer);
254 exfat_error("'%s' is empty but start cluster is %#x", buffer,
255 node->start_cluster);
256 ret = false;
257 }
258 if (node->size > 0 && CLUSTER_INVALID(*ef->sb, node->start_cluster))
259 {
260 exfat_get_name(node, buffer);
261 exfat_error("'%s' points to invalid cluster %#x", buffer,
262 node->start_cluster);
263 ret = false;
264 }
265
266 /* File or directory cannot be larger than clusters heap. */
267 if (node->size > clusters_heap_size)
268 {
269 exfat_get_name(node, buffer);
270 exfat_error("'%s' is larger than clusters heap: %"PRIu64" > %"PRIu64,
271 buffer, node->size, clusters_heap_size);
272 ret = false;
273 }
274
275 /* Empty file or directory must be marked as non-contiguous. */
276 if (node->size == 0 && node->is_contiguous)
277 {
278 exfat_get_name(node, buffer);
279 exfat_error("'%s' is empty but marked as contiguous (%#hx)", buffer,
280 node->attrib);
281 ret = false;
282 }
283
284 /* Directory size must be aligned on at cluster boundary. */
285 if ((node->attrib & EXFAT_ATTRIB_DIR) && node->size % cluster_size != 0)
286 {
287 exfat_get_name(node, buffer);
288 exfat_error("'%s' directory size %"PRIu64" is not divisible by %d", buffer,
289 node->size, cluster_size);
290 ret = false;
291 }
292
293 return ret;
294 }
295
296 static int parse_file_entries(struct exfat* ef, struct exfat_node* node,
297 const struct exfat_entry* entries, int n)
298 {
299 const struct exfat_entry_meta1* meta1;
300 const struct exfat_entry_meta2* meta2;
301 int mandatory_entries;
302
303 if (!check_entries(entries, n))
304 return -EIO;
305
306 meta1 = (const struct exfat_entry_meta1*) &entries[0];
307 if (meta1->continuations < 2)
308 {
309 exfat_error("too few continuations (%hhu)", meta1->continuations);
310 return -EIO;
311 }
312 meta2 = (const struct exfat_entry_meta2*) &entries[1];
313 if (meta2->flags & ~(EXFAT_FLAG_ALWAYS1 | EXFAT_FLAG_CONTIGUOUS))
314 {
315 exfat_error("unknown flags in meta2 (%#hhx)", meta2->flags);
316 return -EIO;
317 }
318 mandatory_entries = 2 + DIV_ROUND_UP(meta2->name_length, EXFAT_ENAME_MAX);
319 if (meta1->continuations < mandatory_entries - 1)
320 {
321 exfat_error("too few continuations (%hhu < %d)",
322 meta1->continuations, mandatory_entries - 1);
323 return -EIO;
324 }
325
326 init_node_meta1(node, meta1);
327 init_node_meta2(node, meta2);
328 init_node_name(node, entries + 2, mandatory_entries - 2);
329
330 if (!check_node(ef, node, exfat_calc_checksum(entries, n), meta1, meta2))
331 return -EIO;
332
333 return 0;
334 }
335
336 static int parse_file_entry(struct exfat* ef, struct exfat_node* parent,
337 struct exfat_node** node, off_t* offset, int n)
338 {
339 struct exfat_entry entries[n];
340 int rc;
341
342 rc = read_entries(ef, parent, entries, n, *offset);
343 if (rc != 0)
344 return rc;
345
346 /* a new node has zero references */
347 *node = allocate_node();
348 if (*node == NULL)
349 return -ENOMEM;
350 (*node)->entry_offset = *offset;
351
352 rc = parse_file_entries(ef, *node, entries, n);
353 if (rc != 0)
354 {
355 free(*node);
356 return rc;
357 }
358
359 *offset += sizeof(struct exfat_entry[n]);
360 return 0;
361 }
362
363 static void decompress_upcase(uint16_t* output, const le16_t* source,
364 size_t size)
365 {
366 size_t si;
367 size_t oi;
368
369 for (oi = 0; oi < EXFAT_UPCASE_CHARS; oi++)
370 output[oi] = oi;
371
372 for (si = 0, oi = 0; si < size && oi < EXFAT_UPCASE_CHARS; si++)
373 {
374 uint16_t ch = le16_to_cpu(source[si]);
375
376 if (ch == 0xffff && si + 1 < size) /* indicates a run */
377 oi += le16_to_cpu(source[++si]);
378 else
379 output[oi++] = ch;
380 }
381 }
382
383 /*
384 * Read one entry in a directory at offset position and build a new node
385 * structure.
386 */
387 static int readdir(struct exfat* ef, struct exfat_node* parent,
388 struct exfat_node** node, off_t* offset)
389 {
390 int rc;
391 struct exfat_entry entry;
392 const struct exfat_entry_meta1* meta1;
393 const struct exfat_entry_upcase* upcase;
394 const struct exfat_entry_bitmap* bitmap;
395 const struct exfat_entry_label* label;
396 uint64_t upcase_size = 0;
397 le16_t* upcase_comp = NULL;
398
399 for (;;)
400 {
401 rc = read_entries(ef, parent, &entry, 1, *offset);
402 if (rc != 0)
403 return rc;
404
405 switch (entry.type)
406 {
407 case EXFAT_ENTRY_FILE:
408 meta1 = (const struct exfat_entry_meta1*) &entry;
409 return parse_file_entry(ef, parent, node, offset,
410 1 + meta1->continuations);
411
412 case EXFAT_ENTRY_UPCASE:
413 if (ef->upcase != NULL)
414 break;
415 upcase = (const struct exfat_entry_upcase*) &entry;
416 if (CLUSTER_INVALID(*ef->sb, le32_to_cpu(upcase->start_cluster)))
417 {
418 exfat_error("invalid cluster 0x%x in upcase table",
419 le32_to_cpu(upcase->start_cluster));
420 return -EIO;
421 }
422 upcase_size = le64_to_cpu(upcase->size);
423 if (upcase_size == 0 ||
424 upcase_size > EXFAT_UPCASE_CHARS * sizeof(uint16_t) ||
425 upcase_size % sizeof(uint16_t) != 0)
426 {
427 exfat_error("bad upcase table size (%"PRIu64" bytes)",
428 upcase_size);
429 return -EIO;
430 }
431 upcase_comp = malloc(upcase_size);
432 if (upcase_comp == NULL)
433 {
434 exfat_error("failed to allocate upcase table (%"PRIu64" bytes)",
435 upcase_size);
436 return -ENOMEM;
437 }
438
439 /* read compressed upcase table */
440 if (exfat_pread(ef->dev, upcase_comp, upcase_size,
441 exfat_c2o(ef, le32_to_cpu(upcase->start_cluster))) < 0)
442 {
443 free(upcase_comp);
444 exfat_error("failed to read upper case table "
445 "(%"PRIu64" bytes starting at cluster %#x)",
446 upcase_size,
447 le32_to_cpu(upcase->start_cluster));
448 return -EIO;
449 }
450
451 /* decompress upcase table */
452 ef->upcase = calloc(EXFAT_UPCASE_CHARS, sizeof(uint16_t));
453 if (ef->upcase == NULL)
454 {
455 free(upcase_comp);
456 exfat_error("failed to allocate decompressed upcase table");
457 return -ENOMEM;
458 }
459 decompress_upcase(ef->upcase, upcase_comp,
460 upcase_size / sizeof(uint16_t));
461 free(upcase_comp);
462 break;
463
464 case EXFAT_ENTRY_BITMAP:
465 bitmap = (const struct exfat_entry_bitmap*) &entry;
466 ef->cmap.start_cluster = le32_to_cpu(bitmap->start_cluster);
467 if (CLUSTER_INVALID(*ef->sb, ef->cmap.start_cluster))
468 {
469 exfat_error("invalid cluster 0x%x in clusters bitmap",
470 ef->cmap.start_cluster);
471 return -EIO;
472 }
473 ef->cmap.size = le32_to_cpu(ef->sb->cluster_count);
474 if (le64_to_cpu(bitmap->size) < DIV_ROUND_UP(ef->cmap.size, 8))
475 {
476 exfat_error("invalid clusters bitmap size: %"PRIu64
477 " (expected at least %u)",
478 le64_to_cpu(bitmap->size),
479 DIV_ROUND_UP(ef->cmap.size, 8));
480 return -EIO;
481 }
482 /* FIXME bitmap can be rather big, up to 512 MB */
483 ef->cmap.chunk_size = ef->cmap.size;
484 ef->cmap.chunk = malloc(BMAP_SIZE(ef->cmap.chunk_size));
485 if (ef->cmap.chunk == NULL)
486 {
487 exfat_error("failed to allocate clusters bitmap chunk "
488 "(%"PRIu64" bytes)", le64_to_cpu(bitmap->size));
489 return -ENOMEM;
490 }
491
492 if (exfat_pread(ef->dev, ef->cmap.chunk,
493 BMAP_SIZE(ef->cmap.chunk_size),
494 exfat_c2o(ef, ef->cmap.start_cluster)) < 0)
495 {
496 exfat_error("failed to read clusters bitmap "
497 "(%"PRIu64" bytes starting at cluster %#x)",
498 le64_to_cpu(bitmap->size), ef->cmap.start_cluster);
499 return -EIO;
500 }
501 break;
502
503 case EXFAT_ENTRY_LABEL:
504 label = (const struct exfat_entry_label*) &entry;
505 if (label->length > EXFAT_ENAME_MAX)
506 {
507 exfat_error("too long label (%hhu chars)", label->length);
508 return -EIO;
509 }
510 if (utf16_to_utf8(ef->label, label->name,
511 sizeof(ef->label), EXFAT_ENAME_MAX) != 0)
512 return -EIO;
513 break;
514
515 default:
516 if (!(entry.type & EXFAT_ENTRY_VALID))
517 break; /* deleted entry, ignore it */
518
519 exfat_error("unknown entry type %#hhx", entry.type);
520 if (!EXFAT_REPAIR(unknown_entry, ef, parent, &entry, *offset))
521 return -EIO;
522 }
523 *offset += sizeof(entry);
524 }
525 /* we never reach here */
526 }
527
528 int exfat_cache_directory(struct exfat* ef, struct exfat_node* dir)
529 {
530 off_t offset = 0;
531 int rc;
532 struct exfat_node* node;
533 struct exfat_node* current = NULL;
534
535 if (dir->is_cached)
536 return 0; /* already cached */
537
538 while ((rc = readdir(ef, dir, &node, &offset)) == 0)
539 {
540 node->parent = dir;
541 if (current != NULL)
542 {
543 current->next = node;
544 node->prev = current;
545 }
546 else
547 dir->child = node;
548
549 current = node;
550 }
551
552 if (rc != -ENOENT)
553 {
554 /* rollback */
555 for (current = dir->child; current; current = node)
556 {
557 node = current->next;
558 free(current);
559 }
560 dir->child = NULL;
561 return rc;
562 }
563
564 dir->is_cached = true;
565 return 0;
566 }
567
568 static void tree_attach(struct exfat_node* dir, struct exfat_node* node)
569 {
570 node->parent = dir;
571 if (dir->child)
572 {
573 dir->child->prev = node;
574 node->next = dir->child;
575 }
576 dir->child = node;
577 }
578
579 static void tree_detach(struct exfat_node* node)
580 {
581 if (node->prev)
582 node->prev->next = node->next;
583 else /* this is the first node in the list */
584 node->parent->child = node->next;
585 if (node->next)
586 node->next->prev = node->prev;
587 node->parent = NULL;
588 node->prev = NULL;
589 node->next = NULL;
590 }
591
592 static void reset_cache(struct exfat* ef, struct exfat_node* node)
593 {
594 char buffer[EXFAT_UTF8_NAME_BUFFER_MAX];
595
596 while (node->child)
597 {
598 struct exfat_node* p = node->child;
599 reset_cache(ef, p);
600 tree_detach(p);
601 free(p);
602 }
603 node->is_cached = false;
604 if (node->references != 0)
605 {
606 exfat_get_name(node, buffer);
607 exfat_warn("non-zero reference counter (%d) for '%s'",
608 node->references, buffer);
609 }
610 if (node != ef->root && node->is_dirty)
611 {
612 exfat_get_name(node, buffer);
613 exfat_bug("node '%s' is dirty", buffer);
614 }
615 while (node->references)
616 exfat_put_node(ef, node);
617 }
618
619 void exfat_reset_cache(struct exfat* ef)
620 {
621 reset_cache(ef, ef->root);
622 }
623
624 int exfat_flush_node(struct exfat* ef, struct exfat_node* node)
625 {
626 struct exfat_entry entries[1 + node->continuations];
627 struct exfat_entry_meta1* meta1 = (struct exfat_entry_meta1*) &entries[0];
628 struct exfat_entry_meta2* meta2 = (struct exfat_entry_meta2*) &entries[1];
629 int rc;
630
631 if (!node->is_dirty)
632 return 0; /* no need to flush */
633
634 if (ef->ro)
635 exfat_bug("unable to flush node to read-only FS");
636
637 if (node->parent == NULL)
638 return 0; /* do not flush unlinked node */
639
640 rc = read_entries(ef, node->parent, entries, 1 + node->continuations,
641 node->entry_offset);
642 if (rc != 0)
643 return rc;
644 if (!check_entries(entries, 1 + node->continuations))
645 return -EIO;
646
647 meta1->attrib = cpu_to_le16(node->attrib);
648 exfat_unix2exfat(node->mtime, &meta1->mdate, &meta1->mtime,
649 &meta1->mtime_cs);
650 exfat_unix2exfat(node->atime, &meta1->adate, &meta1->atime, NULL);
651 meta2->size = meta2->valid_size = cpu_to_le64(node->size);
652 meta2->start_cluster = cpu_to_le32(node->start_cluster);
653 meta2->flags = EXFAT_FLAG_ALWAYS1;
654 /* empty files must not be marked as contiguous */
655 if (node->size != 0 && node->is_contiguous)
656 meta2->flags |= EXFAT_FLAG_CONTIGUOUS;
657 /* name hash remains unchanged, no need to recalculate it */
658
659 meta1->checksum = exfat_calc_checksum(entries, 1 + node->continuations);
660 rc = write_entries(ef, node->parent, entries, 1 + node->continuations,
661 node->entry_offset);
662 if (rc != 0)
663 return rc;
664
665 node->is_dirty = false;
666 return exfat_flush(ef);
667 }
668
669 static int erase_entries(struct exfat* ef, struct exfat_node* dir, int n,
670 off_t offset)
671 {
672 struct exfat_entry entries[n];
673 int rc;
674 int i;
675
676 rc = read_entries(ef, dir, entries, n, offset);
677 if (rc != 0)
678 return rc;
679 for (i = 0; i < n; i++)
680 entries[i].type &= ~EXFAT_ENTRY_VALID;
681 return write_entries(ef, dir, entries, n, offset);
682 }
683
684 static int erase_node(struct exfat* ef, struct exfat_node* node)
685 {
686 int rc;
687
688 exfat_get_node(node->parent);
689 rc = erase_entries(ef, node->parent, 1 + node->continuations,
690 node->entry_offset);
691 if (rc != 0)
692 {
693 exfat_put_node(ef, node->parent);
694 return rc;
695 }
696 rc = exfat_flush_node(ef, node->parent);
697 exfat_put_node(ef, node->parent);
698 return rc;
699 }
700
701 static int shrink_directory(struct exfat* ef, struct exfat_node* dir,
702 off_t deleted_offset)
703 {
704 const struct exfat_node* node;
705 const struct exfat_node* last_node;
706 uint64_t entries = 0;
707 uint64_t new_size;
708
709 if (!(dir->attrib & EXFAT_ATTRIB_DIR))
710 exfat_bug("attempted to shrink a file");
711 if (!dir->is_cached)
712 exfat_bug("attempted to shrink uncached directory");
713
714 for (last_node = node = dir->child; node; node = node->next)
715 {
716 if (deleted_offset < node->entry_offset)
717 {
718 /* there are other entries after the removed one, no way to shrink
719 this directory */
720 return 0;
721 }
722 if (last_node->entry_offset < node->entry_offset)
723 last_node = node;
724 }
725
726 if (last_node)
727 {
728 /* offset of the last entry */
729 entries += last_node->entry_offset / sizeof(struct exfat_entry);
730 /* two subentries with meta info */
731 entries += 2;
732 /* subentries with file name */
733 entries += DIV_ROUND_UP(utf16_length(last_node->name),
734 EXFAT_ENAME_MAX);
735 }
736
737 new_size = DIV_ROUND_UP(entries * sizeof(struct exfat_entry),
738 CLUSTER_SIZE(*ef->sb)) * CLUSTER_SIZE(*ef->sb);
739 if (new_size == 0) /* directory always has at least 1 cluster */
740 new_size = CLUSTER_SIZE(*ef->sb);
741 if (new_size == dir->size)
742 return 0;
743 return exfat_truncate(ef, dir, new_size, true);
744 }
745
746 static int delete(struct exfat* ef, struct exfat_node* node)
747 {
748 struct exfat_node* parent = node->parent;
749 off_t deleted_offset = node->entry_offset;
750 int rc;
751
752 exfat_get_node(parent);
753 rc = erase_node(ef, node);
754 if (rc != 0)
755 {
756 exfat_put_node(ef, parent);
757 return rc;
758 }
759 tree_detach(node);
760 rc = shrink_directory(ef, parent, deleted_offset);
761 node->is_unlinked = true;
762 if (rc != 0)
763 {
764 exfat_flush_node(ef, parent);
765 exfat_put_node(ef, parent);
766 return rc;
767 }
768 exfat_update_mtime(parent);
769 rc = exfat_flush_node(ef, parent);
770 exfat_put_node(ef, parent);
771 return rc;
772 }
773
774 int exfat_unlink(struct exfat* ef, struct exfat_node* node)
775 {
776 if (node->attrib & EXFAT_ATTRIB_DIR)
777 return -EISDIR;
778 return delete(ef, node);
779 }
780
781 int exfat_rmdir(struct exfat* ef, struct exfat_node* node)
782 {
783 int rc;
784
785 if (!(node->attrib & EXFAT_ATTRIB_DIR))
786 return -ENOTDIR;
787 /* check that directory is empty */
788 rc = exfat_cache_directory(ef, node);
789 if (rc != 0)
790 return rc;
791 if (node->child)
792 return -ENOTEMPTY;
793 return delete(ef, node);
794 }
795
796 static int check_slot(struct exfat* ef, struct exfat_node* dir, off_t offset,
797 int n)
798 {
799 struct exfat_entry entries[n];
800 int rc;
801 size_t i;
802
803 /* Root directory contains entries, that don't have any nodes associated
804 with them (clusters bitmap, upper case table, label). We need to be
805 careful not to overwrite them. */
806 if (dir != ef->root)
807 return 0;
808
809 rc = read_entries(ef, dir, entries, n, offset);
810 if (rc != 0)
811 return rc;
812 for (i = 0; i < n; i++)
813 if (entries[i].type & EXFAT_ENTRY_VALID)
814 return -EINVAL;
815 return 0;
816 }
817
818 static int find_slot(struct exfat* ef, struct exfat_node* dir,
819 off_t* offset, int n)
820 {
821 bitmap_t* dmap;
822 struct exfat_node* p;
823 size_t i;
824 int contiguous = 0;
825
826 if (!dir->is_cached)
827 exfat_bug("directory is not cached");
828
829 /* build a bitmap of valid entries in the directory */
830 dmap = calloc(BMAP_SIZE(dir->size / sizeof(struct exfat_entry)),
831 sizeof(bitmap_t));
832 if (dmap == NULL)
833 {
834 exfat_error("failed to allocate directory bitmap (%"PRIu64")",
835 dir->size / sizeof(struct exfat_entry));
836 return -ENOMEM;
837 }
838 for (p = dir->child; p != NULL; p = p->next)
839 for (i = 0; i < 1 + p->continuations; i++)
840 BMAP_SET(dmap, p->entry_offset / sizeof(struct exfat_entry) + i);
841
842 /* find a slot in the directory entries bitmap */
843 for (i = 0; i < dir->size / sizeof(struct exfat_entry); i++)
844 {
845 if (BMAP_GET(dmap, i) == 0)
846 {
847 if (contiguous++ == 0)
848 *offset = (off_t) i * sizeof(struct exfat_entry);
849 if (contiguous == n)
850 /* suitable slot is found, check that it's not occupied */
851 switch (check_slot(ef, dir, *offset, n))
852 {
853 case 0:
854 free(dmap);
855 return 0;
856 case -EIO:
857 free(dmap);
858 return -EIO;
859 case -EINVAL:
860 /* slot at (i-n) is occupied, go back and check (i-n+1) */
861 i -= contiguous - 1;
862 contiguous = 0;
863 break;
864 }
865 }
866 else
867 contiguous = 0;
868 }
869 free(dmap);
870
871 /* no suitable slots found, extend the directory */
872 if (contiguous == 0)
873 *offset = dir->size;
874 return exfat_truncate(ef, dir,
875 ROUND_UP(dir->size + sizeof(struct exfat_entry[n - contiguous]),
876 CLUSTER_SIZE(*ef->sb)),
877 true);
878 }
879
880 static int commit_entry(struct exfat* ef, struct exfat_node* dir,
881 const le16_t* name, off_t offset, uint16_t attrib)
882 {
883 struct exfat_node* node;
884 const size_t name_length = utf16_length(name);
885 const int name_entries = DIV_ROUND_UP(name_length, EXFAT_ENAME_MAX);
886 struct exfat_entry entries[2 + name_entries];
887 struct exfat_entry_meta1* meta1 = (struct exfat_entry_meta1*) &entries[0];
888 struct exfat_entry_meta2* meta2 = (struct exfat_entry_meta2*) &entries[1];
889 int i;
890 int rc;
891
892 memset(entries, 0, sizeof(struct exfat_entry[2]));
893
894 meta1->type = EXFAT_ENTRY_FILE;
895 meta1->continuations = 1 + name_entries;
896 meta1->attrib = cpu_to_le16(attrib);
897 exfat_unix2exfat(time(NULL), &meta1->crdate, &meta1->crtime,
898 &meta1->crtime_cs);
899 meta1->adate = meta1->mdate = meta1->crdate;
900 meta1->atime = meta1->mtime = meta1->crtime;
901 meta1->mtime_cs = meta1->crtime_cs; /* there is no atime_cs */
902
903 meta2->type = EXFAT_ENTRY_FILE_INFO;
904 meta2->flags = EXFAT_FLAG_ALWAYS1;
905 meta2->name_length = name_length;
906 meta2->name_hash = exfat_calc_name_hash(ef, name, name_length);
907 meta2->start_cluster = cpu_to_le32(EXFAT_CLUSTER_FREE);
908
909 for (i = 0; i < name_entries; i++)
910 {
911 struct exfat_entry_name* name_entry;
912
913 name_entry = (struct exfat_entry_name*) &entries[2 + i];
914 name_entry->type = EXFAT_ENTRY_FILE_NAME;
915 name_entry->__unknown = 0;
916 memcpy(name_entry->name, name + i * EXFAT_ENAME_MAX,
917 EXFAT_ENAME_MAX * sizeof(le16_t));
918 }
919
920 meta1->checksum = exfat_calc_checksum(entries, 2 + name_entries);
921 rc = write_entries(ef, dir, entries, 2 + name_entries, offset);
922 if (rc != 0)
923 return rc;
924
925 node = allocate_node();
926 if (node == NULL)
927 return -ENOMEM;
928 node->entry_offset = offset;
929 memcpy(node->name, name, name_length * sizeof(le16_t));
930 init_node_meta1(node, meta1);
931 init_node_meta2(node, meta2);
932
933 tree_attach(dir, node);
934 return 0;
935 }
936
937 static int create(struct exfat* ef, const char* path, uint16_t attrib)
938 {
939 struct exfat_node* dir;
940 struct exfat_node* existing;
941 off_t offset = -1;
942 le16_t name[EXFAT_NAME_MAX + 1];
943 int rc;
944
945 rc = exfat_split(ef, &dir, &existing, name, path);
946 if (rc != 0)
947 return rc;
948 if (existing != NULL)
949 {
950 exfat_put_node(ef, existing);
951 exfat_put_node(ef, dir);
952 return -EEXIST;
953 }
954
955 rc = find_slot(ef, dir, &offset,
956 2 + DIV_ROUND_UP(utf16_length(name), EXFAT_ENAME_MAX));
957 if (rc != 0)
958 {
959 exfat_put_node(ef, dir);
960 return rc;
961 }
962 rc = commit_entry(ef, dir, name, offset, attrib);
963 if (rc != 0)
964 {
965 exfat_put_node(ef, dir);
966 return rc;
967 }
968 exfat_update_mtime(dir);
969 rc = exfat_flush_node(ef, dir);
970 exfat_put_node(ef, dir);
971 return rc;
972 }
973
974 int exfat_mknod(struct exfat* ef, const char* path)
975 {
976 return create(ef, path, EXFAT_ATTRIB_ARCH);
977 }
978
979 int exfat_mkdir(struct exfat* ef, const char* path)
980 {
981 int rc;
982 struct exfat_node* node;
983
984 rc = create(ef, path, EXFAT_ATTRIB_DIR);
985 if (rc != 0)
986 return rc;
987 rc = exfat_lookup(ef, &node, path);
988 if (rc != 0)
989 return 0;
990 /* directories always have at least one cluster */
991 rc = exfat_truncate(ef, node, CLUSTER_SIZE(*ef->sb), true);
992 if (rc != 0)
993 {
994 delete(ef, node);
995 exfat_put_node(ef, node);
996 return rc;
997 }
998 rc = exfat_flush_node(ef, node);
999 if (rc != 0)
1000 {
1001 delete(ef, node);
1002 exfat_put_node(ef, node);
1003 return rc;
1004 }
1005 exfat_put_node(ef, node);
1006 return 0;
1007 }
1008
1009 static int rename_entry(struct exfat* ef, struct exfat_node* dir,
1010 struct exfat_node* node, const le16_t* name, off_t new_offset)
1011 {
1012 const size_t name_length = utf16_length(name);
1013 const int name_entries = DIV_ROUND_UP(name_length, EXFAT_ENAME_MAX);
1014 struct exfat_entry entries[2 + name_entries];
1015 struct exfat_entry_meta1* meta1 = (struct exfat_entry_meta1*) &entries[0];
1016 struct exfat_entry_meta2* meta2 = (struct exfat_entry_meta2*) &entries[1];
1017 int rc;
1018 int i;
1019
1020 rc = read_entries(ef, node->parent, entries, 2, node->entry_offset);
1021 if (rc != 0)
1022 return rc;
1023
1024 meta1->continuations = 1 + name_entries;
1025 meta2->name_length = name_length;
1026 meta2->name_hash = exfat_calc_name_hash(ef, name, name_length);
1027
1028 rc = erase_node(ef, node);
1029 if (rc != 0)
1030 return rc;
1031
1032 node->entry_offset = new_offset;
1033 node->continuations = 1 + name_entries;
1034
1035 for (i = 0; i < name_entries; i++)
1036 {
1037 struct exfat_entry_name* name_entry;
1038
1039 name_entry = (struct exfat_entry_name*) &entries[2 + i];
1040 name_entry->type = EXFAT_ENTRY_FILE_NAME;
1041 name_entry->__unknown = 0;
1042 memcpy(name_entry->name, name + i * EXFAT_ENAME_MAX,
1043 EXFAT_ENAME_MAX * sizeof(le16_t));
1044 }
1045
1046 meta1->checksum = exfat_calc_checksum(entries, 2 + name_entries);
1047 rc = write_entries(ef, dir, entries, 2 + name_entries, new_offset);
1048 if (rc != 0)
1049 return rc;
1050
1051 memcpy(node->name, name, (EXFAT_NAME_MAX + 1) * sizeof(le16_t));
1052 tree_detach(node);
1053 tree_attach(dir, node);
1054 return 0;
1055 }
1056
1057 int exfat_rename(struct exfat* ef, const char* old_path, const char* new_path)
1058 {
1059 struct exfat_node* node;
1060 struct exfat_node* existing;
1061 struct exfat_node* dir;
1062 off_t offset = -1;
1063 le16_t name[EXFAT_NAME_MAX + 1];
1064 int rc;
1065
1066 rc = exfat_lookup(ef, &node, old_path);
1067 if (rc != 0)
1068 return rc;
1069
1070 rc = exfat_split(ef, &dir, &existing, name, new_path);
1071 if (rc != 0)
1072 {
1073 exfat_put_node(ef, node);
1074 return rc;
1075 }
1076
1077 /* check that target is not a subdirectory of the source */
1078 if (node->attrib & EXFAT_ATTRIB_DIR)
1079 {
1080 struct exfat_node* p;
1081
1082 for (p = dir; p; p = p->parent)
1083 if (node == p)
1084 {
1085 if (existing != NULL)
1086 exfat_put_node(ef, existing);
1087 exfat_put_node(ef, dir);
1088 exfat_put_node(ef, node);
1089 return -EINVAL;
1090 }
1091 }
1092
1093 if (existing != NULL)
1094 {
1095 /* remove target if it's not the same node as source */
1096 if (existing != node)
1097 {
1098 if (existing->attrib & EXFAT_ATTRIB_DIR)
1099 {
1100 if (node->attrib & EXFAT_ATTRIB_DIR)
1101 rc = exfat_rmdir(ef, existing);
1102 else
1103 rc = -ENOTDIR;
1104 }
1105 else
1106 {
1107 if (!(node->attrib & EXFAT_ATTRIB_DIR))
1108 rc = exfat_unlink(ef, existing);
1109 else
1110 rc = -EISDIR;
1111 }
1112 exfat_put_node(ef, existing);
1113 if (rc != 0)
1114 {
1115 /* free clusters even if something went wrong; overwise they
1116 will be just lost */
1117 exfat_cleanup_node(ef, existing);
1118 exfat_put_node(ef, dir);
1119 exfat_put_node(ef, node);
1120 return rc;
1121 }
1122 rc = exfat_cleanup_node(ef, existing);
1123 if (rc != 0)
1124 {
1125 exfat_put_node(ef, dir);
1126 exfat_put_node(ef, node);
1127 return rc;
1128 }
1129 }
1130 else
1131 exfat_put_node(ef, existing);
1132 }
1133
1134 rc = find_slot(ef, dir, &offset,
1135 2 + DIV_ROUND_UP(utf16_length(name), EXFAT_ENAME_MAX));
1136 if (rc != 0)
1137 {
1138 exfat_put_node(ef, dir);
1139 exfat_put_node(ef, node);
1140 return rc;
1141 }
1142 rc = rename_entry(ef, dir, node, name, offset);
1143 if (rc != 0)
1144 {
1145 exfat_put_node(ef, dir);
1146 exfat_put_node(ef, node);
1147 return rc;
1148 }
1149 rc = exfat_flush_node(ef, dir);
1150 exfat_put_node(ef, dir);
1151 exfat_put_node(ef, node);
1152 /* node itself is not marked as dirty, no need to flush it */
1153 return rc;
1154 }
1155
1156 void exfat_utimes(struct exfat_node* node, const struct timespec tv[2])
1157 {
1158 node->atime = tv[0].tv_sec;
1159 node->mtime = tv[1].tv_sec;
1160 node->is_dirty = true;
1161 }
1162
1163 void exfat_update_atime(struct exfat_node* node)
1164 {
1165 node->atime = time(NULL);
1166 node->is_dirty = true;
1167 }
1168
1169 void exfat_update_mtime(struct exfat_node* node)
1170 {
1171 node->mtime = time(NULL);
1172 node->is_dirty = true;
1173 }
1174
1175 const char* exfat_get_label(struct exfat* ef)
1176 {
1177 return ef->label;
1178 }
1179
1180 static int find_label(struct exfat* ef, off_t* offset)
1181 {
1182 struct exfat_entry entry;
1183 int rc;
1184
1185 for (*offset = 0; ; *offset += sizeof(entry))
1186 {
1187 rc = read_entries(ef, ef->root, &entry, 1, *offset);
1188 if (rc != 0)
1189 return rc;
1190
1191 if (entry.type == EXFAT_ENTRY_LABEL)
1192 return 0;
1193 }
1194 }
1195
1196 int exfat_set_label(struct exfat* ef, const char* label)
1197 {
1198 le16_t label_utf16[EXFAT_ENAME_MAX + 1];
1199 int rc;
1200 off_t offset;
1201 struct exfat_entry_label entry;
1202
1203 memset(label_utf16, 0, sizeof(label_utf16));
1204 rc = utf8_to_utf16(label_utf16, label, EXFAT_ENAME_MAX + 1, strlen(label));
1205 if (rc != 0)
1206 return rc;
1207
1208 rc = find_label(ef, &offset);
1209 if (rc == -ENOENT)
1210 rc = find_slot(ef, ef->root, &offset, 1);
1211 if (rc != 0)
1212 return rc;
1213
1214 entry.type = EXFAT_ENTRY_LABEL;
1215 entry.length = utf16_length(label_utf16);
1216 memcpy(entry.name, label_utf16, sizeof(entry.name));
1217 if (entry.length == 0)
1218 entry.type ^= EXFAT_ENTRY_VALID;
1219
1220 rc = write_entries(ef, ef->root, (struct exfat_entry*) &entry, 1, offset);
1221 if (rc != 0)
1222 return rc;
1223
1224 strcpy(ef->label, label);
1225 return 0;
1226 }