1 | /*
|
---|
2 | * Copyright (c) 2011 Oleg Romanenko
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup fs
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * @file exfat_directory.c
|
---|
35 | * @brief Functions that work with FAT directory.
|
---|
36 | */
|
---|
37 |
|
---|
38 | #include "exfat.h"
|
---|
39 | #include "exfat_directory.h"
|
---|
40 | #include "exfat_fat.h"
|
---|
41 | #include <libblock.h>
|
---|
42 | #include <errno.h>
|
---|
43 | #include <byteorder.h>
|
---|
44 | #include <mem.h>
|
---|
45 | #include <malloc.h>
|
---|
46 | #include <str.h>
|
---|
47 | #include <align.h>
|
---|
48 |
|
---|
49 | void exfat_directory_init(exfat_directory_t *di)
|
---|
50 | {
|
---|
51 | di->b = NULL;
|
---|
52 | di->nodep = NULL;
|
---|
53 | di->bs = NULL;
|
---|
54 | di->blocks = 0;
|
---|
55 | di->pos = 0;
|
---|
56 | di->bnum = 0;
|
---|
57 | di->last = false;
|
---|
58 | di->fragmented = false;
|
---|
59 | di->firstc = 0;
|
---|
60 | }
|
---|
61 |
|
---|
62 | int exfat_directory_open(exfat_node_t *nodep, exfat_directory_t *di)
|
---|
63 | {
|
---|
64 | exfat_directory_init(di);
|
---|
65 | di->nodep = nodep;
|
---|
66 | if (di->nodep->type != EXFAT_DIRECTORY)
|
---|
67 | return EINVAL;
|
---|
68 | di->service_id = nodep->idx->service_id;
|
---|
69 | di->fragmented = nodep->fragmented;
|
---|
70 | di->firstc = nodep->firstc;
|
---|
71 |
|
---|
72 | di->bs = block_bb_get(di->service_id);
|
---|
73 | /* di->blocks = nodep->size / BPS(di->bs); */
|
---|
74 | di->blocks = ROUND_UP(nodep->size, BPS(di->bs)) / BPS(di->bs);
|
---|
75 | return EOK;
|
---|
76 | }
|
---|
77 |
|
---|
78 | int exfat_directory_open_parent(exfat_directory_t *di,
|
---|
79 | service_id_t service_id, exfat_cluster_t firstc, bool fragmented)
|
---|
80 | {
|
---|
81 | exfat_directory_init(di);
|
---|
82 | di->service_id = service_id;
|
---|
83 | di->fragmented = fragmented;
|
---|
84 | di->firstc = firstc;
|
---|
85 | di->bs = block_bb_get(service_id);
|
---|
86 | di->blocks = 0;
|
---|
87 | return EOK;
|
---|
88 | }
|
---|
89 |
|
---|
90 | int exfat_directory_close(exfat_directory_t *di)
|
---|
91 | {
|
---|
92 | int rc = EOK;
|
---|
93 |
|
---|
94 | if (di->b)
|
---|
95 | rc = block_put(di->b);
|
---|
96 |
|
---|
97 | return rc;
|
---|
98 | }
|
---|
99 |
|
---|
100 | static int exfat_directory_block_load(exfat_directory_t *di)
|
---|
101 | {
|
---|
102 | uint32_t i;
|
---|
103 | int rc;
|
---|
104 |
|
---|
105 | i = (di->pos * sizeof(exfat_dentry_t)) / BPS(di->bs);
|
---|
106 | if (di->nodep && (i >= di->blocks))
|
---|
107 | return ENOENT;
|
---|
108 |
|
---|
109 | if (di->b && di->bnum != i) {
|
---|
110 | block_put(di->b);
|
---|
111 | di->b = NULL;
|
---|
112 | }
|
---|
113 | if (!di->b) {
|
---|
114 | if (di->nodep) {
|
---|
115 | rc = exfat_block_get(&di->b, di->bs, di->nodep, i,
|
---|
116 | BLOCK_FLAGS_NONE);
|
---|
117 | } else {
|
---|
118 | rc = exfat_block_get_by_clst(&di->b, di->bs,
|
---|
119 | di->service_id, di->fragmented, di->firstc, NULL, i,
|
---|
120 | BLOCK_FLAGS_NONE);
|
---|
121 | }
|
---|
122 | if (rc != EOK) {
|
---|
123 | di->b = NULL;
|
---|
124 | return rc;
|
---|
125 | }
|
---|
126 | di->bnum = i;
|
---|
127 | }
|
---|
128 | return EOK;
|
---|
129 | }
|
---|
130 |
|
---|
131 | int exfat_directory_next(exfat_directory_t *di)
|
---|
132 | {
|
---|
133 | int rc;
|
---|
134 |
|
---|
135 | di->pos += 1;
|
---|
136 | rc = exfat_directory_block_load(di);
|
---|
137 | if (rc != EOK)
|
---|
138 | di->pos -= 1;
|
---|
139 |
|
---|
140 | return rc;
|
---|
141 | }
|
---|
142 |
|
---|
143 | int exfat_directory_prev(exfat_directory_t *di)
|
---|
144 | {
|
---|
145 | int rc = EOK;
|
---|
146 |
|
---|
147 | if (di->pos > 0) {
|
---|
148 | di->pos -= 1;
|
---|
149 | rc=exfat_directory_block_load(di);
|
---|
150 | }
|
---|
151 | else
|
---|
152 | return ENOENT;
|
---|
153 |
|
---|
154 | if (rc!=EOK)
|
---|
155 | di->pos += 1;
|
---|
156 |
|
---|
157 | return rc;
|
---|
158 | }
|
---|
159 |
|
---|
160 | int exfat_directory_seek(exfat_directory_t *di, aoff64_t pos)
|
---|
161 | {
|
---|
162 | aoff64_t _pos = di->pos;
|
---|
163 | int rc;
|
---|
164 |
|
---|
165 | di->pos = pos;
|
---|
166 | rc = exfat_directory_block_load(di);
|
---|
167 | if (rc != EOK)
|
---|
168 | di->pos = _pos;
|
---|
169 |
|
---|
170 | return rc;
|
---|
171 | }
|
---|
172 |
|
---|
173 | int exfat_directory_get(exfat_directory_t *di, exfat_dentry_t **d)
|
---|
174 | {
|
---|
175 | int rc;
|
---|
176 |
|
---|
177 | rc = exfat_directory_block_load(di);
|
---|
178 | if (rc == EOK) {
|
---|
179 | aoff64_t o = di->pos % (BPS(di->bs) / sizeof(exfat_dentry_t));
|
---|
180 | *d = ((exfat_dentry_t *)di->b->data) + o;
|
---|
181 | }
|
---|
182 |
|
---|
183 | return rc;
|
---|
184 | }
|
---|
185 |
|
---|
186 | int exfat_directory_find(exfat_directory_t *di, exfat_dentry_clsf_t type,
|
---|
187 | exfat_dentry_t **d)
|
---|
188 | {
|
---|
189 | do {
|
---|
190 | if (exfat_directory_get(di, d) == EOK) {
|
---|
191 | if (exfat_classify_dentry(*d) == type)
|
---|
192 | return EOK;
|
---|
193 | } else
|
---|
194 | return ENOENT;
|
---|
195 | } while (exfat_directory_next(di) == EOK);
|
---|
196 |
|
---|
197 | return ENOENT;
|
---|
198 | }
|
---|
199 |
|
---|
200 | int
|
---|
201 | exfat_directory_find_continue(exfat_directory_t *di, exfat_dentry_clsf_t type,
|
---|
202 | exfat_dentry_t **d)
|
---|
203 | {
|
---|
204 | int rc;
|
---|
205 | rc = exfat_directory_next(di);
|
---|
206 | if (rc != EOK)
|
---|
207 | return rc;
|
---|
208 | return exfat_directory_find(di, type, d);
|
---|
209 | }
|
---|
210 |
|
---|
211 |
|
---|
212 | int exfat_directory_read_file(exfat_directory_t *di, char *name, size_t size,
|
---|
213 | exfat_file_dentry_t *df, exfat_stream_dentry_t *ds)
|
---|
214 | {
|
---|
215 | uint16_t wname[EXFAT_FILENAME_LEN + 1];
|
---|
216 | exfat_dentry_t *d = NULL;
|
---|
217 | int rc, i;
|
---|
218 | size_t offset = 0;
|
---|
219 | aoff64_t start_pos = 0;
|
---|
220 |
|
---|
221 | rc = exfat_directory_find(di, EXFAT_DENTRY_FILE, &d);
|
---|
222 | if (rc != EOK)
|
---|
223 | return rc;
|
---|
224 | start_pos = di->pos;
|
---|
225 | *df = d->file;
|
---|
226 |
|
---|
227 | rc = exfat_directory_next(di);
|
---|
228 | if (rc != EOK)
|
---|
229 | return rc;
|
---|
230 | rc = exfat_directory_get(di, &d);
|
---|
231 | if (rc != EOK)
|
---|
232 | return rc;
|
---|
233 | if (exfat_classify_dentry(d) != EXFAT_DENTRY_STREAM)
|
---|
234 | return ENOENT;
|
---|
235 | *ds = d->stream;
|
---|
236 |
|
---|
237 | if (ds->name_size > size)
|
---|
238 | return EOVERFLOW;
|
---|
239 |
|
---|
240 | for (i = 0; i < df->count - 1; i++) {
|
---|
241 | rc = exfat_directory_next(di);
|
---|
242 | if (rc != EOK)
|
---|
243 | return rc;
|
---|
244 | rc = exfat_directory_get(di, &d);
|
---|
245 | if (rc != EOK)
|
---|
246 | return rc;
|
---|
247 | if (exfat_classify_dentry(d) != EXFAT_DENTRY_NAME)
|
---|
248 | return ENOENT;
|
---|
249 | exfat_dentry_get_name(&d->name, ds->name_size, wname, &offset);
|
---|
250 | }
|
---|
251 | rc = utf16_to_str(name, size, wname);
|
---|
252 | if (rc != EOK)
|
---|
253 | return rc;
|
---|
254 |
|
---|
255 | exfat_directory_seek(di, start_pos);
|
---|
256 | return EOK;
|
---|
257 | }
|
---|
258 |
|
---|
259 | static uint16_t exfat_directory_set_checksum(const uint8_t *bytes, size_t count)
|
---|
260 | {
|
---|
261 | uint16_t checksum = 0;
|
---|
262 | size_t idx;
|
---|
263 |
|
---|
264 | for (idx = 0; idx < count; idx++) {
|
---|
265 | if (idx == 2 || idx == 3)
|
---|
266 | continue;
|
---|
267 | checksum = ((checksum << 15) | (checksum >> 1)) + (uint16_t)bytes[idx];
|
---|
268 | }
|
---|
269 | return checksum;
|
---|
270 | }
|
---|
271 |
|
---|
272 | int exfat_directory_sync_file(exfat_directory_t *di, exfat_file_dentry_t *df,
|
---|
273 | exfat_stream_dentry_t *ds)
|
---|
274 | {
|
---|
275 | int rc, i, count;
|
---|
276 | exfat_dentry_t *array = NULL, *de;
|
---|
277 | aoff64_t pos = di->pos;
|
---|
278 |
|
---|
279 | rc = exfat_directory_get(di, &de);
|
---|
280 | if (rc != EOK)
|
---|
281 | return rc;
|
---|
282 | count = de->file.count + 1;
|
---|
283 | array = (exfat_dentry_t *) malloc(count*sizeof(exfat_dentry_t));
|
---|
284 | if (!array)
|
---|
285 | return ENOMEM;
|
---|
286 | for (i = 0; i < count; i++) {
|
---|
287 | rc = exfat_directory_get(di, &de);
|
---|
288 | if (rc != EOK)
|
---|
289 | return rc;
|
---|
290 | array[i] = *de;
|
---|
291 | rc = exfat_directory_next(di);
|
---|
292 | if (rc!=EOK) {
|
---|
293 | free(array);
|
---|
294 | return rc;
|
---|
295 | }
|
---|
296 | }
|
---|
297 | rc = exfat_directory_seek(di, pos);
|
---|
298 | if (rc != EOK) {
|
---|
299 | free(array);
|
---|
300 | return rc;
|
---|
301 | }
|
---|
302 |
|
---|
303 | /* Sync */
|
---|
304 | array[0].file.attr = host2uint16_t_le(df->attr);
|
---|
305 | array[1].stream.firstc = host2uint32_t_le(ds->firstc);
|
---|
306 | array[1].stream.flags = ds->flags;
|
---|
307 | array[1].stream.valid_data_size = host2uint64_t_le(ds->valid_data_size);
|
---|
308 | array[1].stream.data_size = host2uint64_t_le(ds->data_size);
|
---|
309 | array[0].file.checksum = host2uint16_t_le(exfat_directory_set_checksum((uint8_t *)array,
|
---|
310 | count * sizeof(exfat_dentry_t)));
|
---|
311 |
|
---|
312 | /* Store */
|
---|
313 | for (i = 0; i < count; i++) {
|
---|
314 | rc = exfat_directory_get(di, &de);
|
---|
315 | if (rc != EOK)
|
---|
316 | return rc;
|
---|
317 | *de = array[i];
|
---|
318 | di->b->dirty = true;
|
---|
319 | rc = exfat_directory_next(di);
|
---|
320 | if (rc != EOK) {
|
---|
321 | free(array);
|
---|
322 | return rc;
|
---|
323 | }
|
---|
324 | }
|
---|
325 | free(array);
|
---|
326 |
|
---|
327 | return EOK;
|
---|
328 | }
|
---|
329 |
|
---|
330 | int exfat_directory_write_file(exfat_directory_t *di, const char *name)
|
---|
331 | {
|
---|
332 | fs_node_t *fn;
|
---|
333 | exfat_node_t *uctablep;
|
---|
334 | uint16_t *uctable;
|
---|
335 | exfat_dentry_t df, ds, *de;
|
---|
336 | uint16_t wname[EXFAT_FILENAME_LEN + 1];
|
---|
337 | int rc, i;
|
---|
338 | size_t uctable_chars, j;
|
---|
339 | aoff64_t pos;
|
---|
340 |
|
---|
341 | rc = str_to_utf16(wname, EXFAT_FILENAME_LEN, name);
|
---|
342 | if (rc != EOK)
|
---|
343 | return rc;
|
---|
344 | rc = exfat_uctable_get(&fn, di->service_id);
|
---|
345 | if (rc != EOK)
|
---|
346 | return rc;
|
---|
347 | uctablep = EXFAT_NODE(fn);
|
---|
348 |
|
---|
349 | uctable_chars = ALIGN_DOWN(uctablep->size, sizeof(uint16_t)) / sizeof(uint16_t);
|
---|
350 | uctable = (uint16_t *) malloc(uctable_chars * sizeof(uint16_t));
|
---|
351 | rc = exfat_read_uctable(di->bs, uctablep, (uint8_t *)uctable);
|
---|
352 | if (rc != EOK) {
|
---|
353 | (void) exfat_node_put(fn);
|
---|
354 | free(uctable);
|
---|
355 | return rc;
|
---|
356 | }
|
---|
357 |
|
---|
358 | /* Fill stream entry */
|
---|
359 | ds.type = EXFAT_TYPE_STREAM;
|
---|
360 | ds.stream.flags = 0;
|
---|
361 | ds.stream.valid_data_size = 0;
|
---|
362 | ds.stream.data_size = 0;
|
---|
363 | ds.stream.name_size = utf16_length(wname);
|
---|
364 | ds.stream.hash = host2uint16_t_le(exfat_name_hash(wname, uctable,
|
---|
365 | uctable_chars));
|
---|
366 |
|
---|
367 | /* Fill file entry */
|
---|
368 | df.type = EXFAT_TYPE_FILE;
|
---|
369 | df.file.attr = 0;
|
---|
370 | df.file.count = ROUND_UP(ds.stream.name_size, EXFAT_NAME_PART_LEN) /
|
---|
371 | EXFAT_NAME_PART_LEN + 1;
|
---|
372 | df.file.checksum = 0;
|
---|
373 |
|
---|
374 | free(uctable);
|
---|
375 | rc = exfat_node_put(fn);
|
---|
376 | if (rc != EOK)
|
---|
377 | return rc;
|
---|
378 |
|
---|
379 | /* Looking for set of free entries */
|
---|
380 | rc = exfat_directory_lookup_free(di, df.file.count+1);
|
---|
381 | if (rc != EOK)
|
---|
382 | return rc;
|
---|
383 | pos = di->pos;
|
---|
384 |
|
---|
385 | /* Write file entry */
|
---|
386 | rc = exfat_directory_get(di, &de);
|
---|
387 | if (rc != EOK)
|
---|
388 | return rc;
|
---|
389 | *de = df;
|
---|
390 | di->b->dirty = true;
|
---|
391 | rc = exfat_directory_next(di);
|
---|
392 | if (rc != EOK)
|
---|
393 | return rc;
|
---|
394 |
|
---|
395 | /* Write stream entry */
|
---|
396 | rc = exfat_directory_get(di, &de);
|
---|
397 | if (rc != EOK)
|
---|
398 | return rc;
|
---|
399 | *de = ds;
|
---|
400 | di->b->dirty = true;
|
---|
401 |
|
---|
402 | /* Write file name */
|
---|
403 | size_t chars = EXFAT_NAME_PART_LEN;
|
---|
404 | uint16_t *sname = wname;
|
---|
405 |
|
---|
406 | for (i = 0; i < ds.stream.name_size; i++)
|
---|
407 | wname[i] = host2uint16_t_le(wname[i]);
|
---|
408 |
|
---|
409 | for (i = 0; i < df.file.count - 1; i++) {
|
---|
410 | rc = exfat_directory_next(di);
|
---|
411 | if (rc != EOK)
|
---|
412 | return rc;
|
---|
413 |
|
---|
414 | if (i == df.file.count - 2)
|
---|
415 | chars = ds.stream.name_size - EXFAT_NAME_PART_LEN*(df.file.count - 2);
|
---|
416 | rc = exfat_directory_get(di, &de);
|
---|
417 | if (rc != EOK)
|
---|
418 | return rc;
|
---|
419 | de->type = EXFAT_TYPE_NAME;
|
---|
420 | /* test */
|
---|
421 | for (j = 0; j < chars; j++) {
|
---|
422 | de->name.name[j] = *sname;
|
---|
423 | sname++;
|
---|
424 | }
|
---|
425 |
|
---|
426 | di->b->dirty = true;
|
---|
427 | sname += chars;
|
---|
428 | }
|
---|
429 |
|
---|
430 | return exfat_directory_seek(di, pos);
|
---|
431 | }
|
---|
432 |
|
---|
433 | int exfat_directory_erase_file(exfat_directory_t *di, aoff64_t pos)
|
---|
434 | {
|
---|
435 | int rc, count;
|
---|
436 | exfat_dentry_t *de;
|
---|
437 |
|
---|
438 | rc = exfat_directory_get(di, &de);
|
---|
439 | if (rc != EOK)
|
---|
440 | return rc;
|
---|
441 | count = de->file.count + 1;
|
---|
442 |
|
---|
443 | while (count) {
|
---|
444 | rc = exfat_directory_get(di, &de);
|
---|
445 | if (rc != EOK)
|
---|
446 | return rc;
|
---|
447 | de->type &= ~EXFAT_TYPE_USED;
|
---|
448 | di->b->dirty = true;
|
---|
449 |
|
---|
450 | rc = exfat_directory_next(di);
|
---|
451 | if (rc != EOK)
|
---|
452 | return rc;
|
---|
453 | count--;
|
---|
454 | }
|
---|
455 | return EOK;
|
---|
456 | }
|
---|
457 |
|
---|
458 | int exfat_directory_expand(exfat_directory_t *di)
|
---|
459 | {
|
---|
460 | int rc;
|
---|
461 |
|
---|
462 | if (!di->nodep)
|
---|
463 | return ENOSPC;
|
---|
464 |
|
---|
465 | rc = exfat_node_expand(di->nodep->idx->service_id, di->nodep, 1);
|
---|
466 | if (rc != EOK)
|
---|
467 | return rc;
|
---|
468 |
|
---|
469 | di->fragmented = di->nodep->fragmented;
|
---|
470 | di->nodep->size += BPC(di->bs);
|
---|
471 | di->nodep->dirty = true; /* need to sync node */
|
---|
472 | di->blocks = di->nodep->size / BPS(di->bs);
|
---|
473 |
|
---|
474 | return EOK;
|
---|
475 | }
|
---|
476 |
|
---|
477 | int exfat_directory_lookup_free(exfat_directory_t *di, size_t count)
|
---|
478 | {
|
---|
479 | int rc;
|
---|
480 | exfat_dentry_t *d;
|
---|
481 | size_t found;
|
---|
482 | aoff64_t pos;
|
---|
483 |
|
---|
484 | rc = exfat_directory_seek(di, 0);
|
---|
485 | if (rc != EOK)
|
---|
486 | return rc;
|
---|
487 |
|
---|
488 | do {
|
---|
489 | found = 0;
|
---|
490 | pos = 0;
|
---|
491 | do {
|
---|
492 | if (exfat_directory_get(di, &d) == EOK) {
|
---|
493 | switch (exfat_classify_dentry(d)) {
|
---|
494 | case EXFAT_DENTRY_LAST:
|
---|
495 | case EXFAT_DENTRY_FREE:
|
---|
496 | if (found == 0)
|
---|
497 | pos = di->pos;
|
---|
498 | found++;
|
---|
499 | if (found == count) {
|
---|
500 | exfat_directory_seek(di, pos);
|
---|
501 | return EOK;
|
---|
502 | }
|
---|
503 | break;
|
---|
504 | default:
|
---|
505 | found = 0;
|
---|
506 | break;
|
---|
507 | }
|
---|
508 | }
|
---|
509 | } while (exfat_directory_next(di) == EOK);
|
---|
510 | } while (exfat_directory_expand(di) == EOK);
|
---|
511 | return ENOSPC;
|
---|
512 | }
|
---|
513 |
|
---|
514 |
|
---|
515 | /**
|
---|
516 | * @}
|
---|
517 | */
|
---|