source: mainline/uspace/srv/fs/exfat/exfat_directory.c@ ee8c5ec

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since ee8c5ec was e8975278, checked in by Jiri Svoboda <jiri@…>, 8 years ago

A few more cstyle fixes.

  • Property mode set to 100644
File size: 12.0 KB
Line 
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 <block.h>
42#include <errno.h>
43#include <byteorder.h>
44#include <mem.h>
45#include <stdlib.h>
46#include <str.h>
47#include <align.h>
48
49void 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
62errno_t 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 = ROUND_UP(nodep->size, BPS(di->bs)) / BPS(di->bs);
74 return EOK;
75}
76
77errno_t exfat_directory_open_parent(exfat_directory_t *di,
78 service_id_t service_id, exfat_cluster_t firstc, bool fragmented)
79{
80 exfat_directory_init(di);
81 di->service_id = service_id;
82 di->fragmented = fragmented;
83 di->firstc = firstc;
84 di->bs = block_bb_get(service_id);
85 di->blocks = 0;
86 return EOK;
87}
88
89errno_t exfat_directory_close(exfat_directory_t *di)
90{
91 errno_t rc = EOK;
92
93 if (di->b) {
94 rc = block_put(di->b);
95 di->b = NULL;
96 }
97
98 return rc;
99}
100
101static errno_t exfat_directory_block_load(exfat_directory_t *di)
102{
103 uint32_t i;
104 errno_t rc = EOK;
105
106 i = (di->pos * sizeof(exfat_dentry_t)) / BPS(di->bs);
107 if (di->nodep && (i >= di->blocks))
108 return ENOENT;
109
110 if (di->b && di->bnum != i) {
111 rc = block_put(di->b);
112 di->b = NULL;
113 if (rc != EOK)
114 return rc;
115 }
116 if (!di->b) {
117 if (di->nodep) {
118 rc = exfat_block_get(&di->b, di->bs, di->nodep, i,
119 BLOCK_FLAGS_NONE);
120 } else {
121 rc = exfat_block_get_by_clst(&di->b, di->bs,
122 di->service_id, di->fragmented, di->firstc, NULL, i,
123 BLOCK_FLAGS_NONE);
124 }
125 if (rc != EOK) {
126 di->b = NULL;
127 return rc;
128 }
129 di->bnum = i;
130 }
131 return rc;
132}
133
134errno_t exfat_directory_next(exfat_directory_t *di)
135{
136 errno_t rc;
137
138 di->pos += 1;
139 rc = exfat_directory_block_load(di);
140 if (rc != EOK)
141 di->pos -= 1;
142
143 return rc;
144}
145
146errno_t exfat_directory_prev(exfat_directory_t *di)
147{
148 errno_t rc = EOK;
149
150 if (di->pos > 0) {
151 di->pos -= 1;
152 rc = exfat_directory_block_load(di);
153 } else
154 return ENOENT;
155
156 if (rc != EOK)
157 di->pos += 1;
158
159 return rc;
160}
161
162errno_t exfat_directory_seek(exfat_directory_t *di, aoff64_t pos)
163{
164 aoff64_t _pos = di->pos;
165 errno_t rc;
166
167 di->pos = pos;
168 rc = exfat_directory_block_load(di);
169 if (rc != EOK)
170 di->pos = _pos;
171
172 return rc;
173}
174
175errno_t exfat_directory_get(exfat_directory_t *di, exfat_dentry_t **d)
176{
177 errno_t rc;
178
179 rc = exfat_directory_block_load(di);
180 if (rc == EOK) {
181 aoff64_t o = di->pos % (BPS(di->bs) / sizeof(exfat_dentry_t));
182 *d = ((exfat_dentry_t *)di->b->data) + o;
183 }
184
185 return rc;
186}
187
188errno_t exfat_directory_find(exfat_directory_t *di, exfat_dentry_clsf_t type,
189 exfat_dentry_t **d)
190{
191 do {
192 if (exfat_directory_get(di, d) == EOK) {
193 if (exfat_classify_dentry(*d) == type)
194 return EOK;
195 } else
196 return ENOENT;
197 } while (exfat_directory_next(di) == EOK);
198
199 return ENOENT;
200}
201
202errno_t
203exfat_directory_find_continue(exfat_directory_t *di, exfat_dentry_clsf_t type,
204 exfat_dentry_t **d)
205{
206 errno_t rc;
207 rc = exfat_directory_next(di);
208 if (rc != EOK)
209 return rc;
210 return exfat_directory_find(di, type, d);
211}
212
213
214errno_t exfat_directory_read_file(exfat_directory_t *di, char *name, size_t size,
215 exfat_file_dentry_t *df, exfat_stream_dentry_t *ds)
216{
217 uint16_t wname[EXFAT_FILENAME_LEN + 1];
218 exfat_dentry_t *d = NULL;
219 errno_t rc;
220 int i;
221 size_t offset = 0;
222 aoff64_t start_pos = 0;
223
224 rc = exfat_directory_find(di, EXFAT_DENTRY_FILE, &d);
225 if (rc != EOK)
226 return rc;
227 start_pos = di->pos;
228 *df = d->file;
229
230 rc = exfat_directory_next(di);
231 if (rc != EOK)
232 return rc;
233 rc = exfat_directory_get(di, &d);
234 if (rc != EOK)
235 return rc;
236 if (exfat_classify_dentry(d) != EXFAT_DENTRY_STREAM)
237 return ENOENT;
238 *ds = d->stream;
239
240 if (ds->name_size > size)
241 return EOVERFLOW;
242
243 for (i = 0; i < df->count - 1; i++) {
244 rc = exfat_directory_next(di);
245 if (rc != EOK)
246 return rc;
247 rc = exfat_directory_get(di, &d);
248 if (rc != EOK)
249 return rc;
250 if (exfat_classify_dentry(d) != EXFAT_DENTRY_NAME)
251 return ENOENT;
252 exfat_dentry_get_name(&d->name, ds->name_size, wname, &offset);
253 }
254 rc = utf16_to_str(name, size, wname);
255 if (rc != EOK)
256 return rc;
257
258 exfat_directory_seek(di, start_pos);
259 return EOK;
260}
261
262errno_t exfat_directory_read_vollabel(exfat_directory_t *di, char *label,
263 size_t size)
264{
265 uint16_t wlabel[EXFAT_VOLLABEL_LEN + 1];
266 exfat_dentry_t *d = NULL;
267 errno_t rc;
268 aoff64_t start_pos = 0;
269
270 start_pos = di->pos;
271
272 rc = exfat_directory_seek(di, 0);
273 if (rc != EOK)
274 return rc;
275
276 rc = exfat_directory_find(di, EXFAT_DENTRY_VOLLABEL, &d);
277 if (rc != EOK)
278 return rc;
279
280 exfat_dentry_get_vollabel(&d->vollabel, EXFAT_VOLLABEL_LEN, wlabel);
281
282 rc = utf16_to_str(label, size, wlabel);
283 if (rc != EOK)
284 return rc;
285
286 exfat_directory_seek(di, start_pos);
287 return EOK;
288}
289
290static uint16_t exfat_directory_set_checksum(const uint8_t *bytes, size_t count)
291{
292 uint16_t checksum = 0;
293 size_t idx;
294
295 for (idx = 0; idx < count; idx++) {
296 if (idx == 2 || idx == 3)
297 continue;
298 checksum = ((checksum << 15) | (checksum >> 1)) +
299 (uint16_t)bytes[idx];
300 }
301 return checksum;
302}
303
304errno_t exfat_directory_sync_file(exfat_directory_t *di, exfat_file_dentry_t *df,
305 exfat_stream_dentry_t *ds)
306{
307 errno_t rc;
308 int i, count;
309 exfat_dentry_t *array = NULL, *de;
310 aoff64_t pos = di->pos;
311
312 rc = exfat_directory_get(di, &de);
313 if (rc != EOK)
314 return rc;
315 count = de->file.count + 1;
316 array = (exfat_dentry_t *) malloc(count * sizeof(exfat_dentry_t));
317 if (!array)
318 return ENOMEM;
319 for (i = 0; i < count; i++) {
320 rc = exfat_directory_get(di, &de);
321 if (rc != EOK) {
322 free(array);
323 return rc;
324 }
325 array[i] = *de;
326 rc = exfat_directory_next(di);
327 if (rc != EOK) {
328 free(array);
329 return rc;
330 }
331 }
332 rc = exfat_directory_seek(di, pos);
333 if (rc != EOK) {
334 free(array);
335 return rc;
336 }
337
338 /* Sync */
339 array[0].file.attr = host2uint16_t_le(df->attr);
340 array[1].stream.firstc = host2uint32_t_le(ds->firstc);
341 array[1].stream.flags = ds->flags;
342 array[1].stream.valid_data_size = host2uint64_t_le(ds->valid_data_size);
343 array[1].stream.data_size = host2uint64_t_le(ds->data_size);
344 array[0].file.checksum =
345 host2uint16_t_le(exfat_directory_set_checksum((uint8_t *)array,
346 count * sizeof(exfat_dentry_t)));
347
348 /* Store */
349 for (i = 0; i < count; i++) {
350 rc = exfat_directory_get(di, &de);
351 if (rc != EOK) {
352 free(array);
353 return rc;
354 }
355 *de = array[i];
356 di->b->dirty = true;
357 rc = exfat_directory_next(di);
358 if (rc != EOK) {
359 free(array);
360 return rc;
361 }
362 }
363 free(array);
364
365 return EOK;
366}
367
368errno_t exfat_directory_write_file(exfat_directory_t *di, const char *name)
369{
370 fs_node_t *fn;
371 exfat_node_t *uctablep;
372 uint16_t *uctable;
373 exfat_dentry_t df, ds, *de;
374 uint16_t wname[EXFAT_FILENAME_LEN + 1];
375 errno_t rc;
376 int i;
377 size_t uctable_chars, j;
378 aoff64_t pos;
379
380 rc = str_to_utf16(wname, EXFAT_FILENAME_LEN, name);
381 if (rc != EOK)
382 return rc;
383 rc = exfat_uctable_get(&fn, di->service_id);
384 if (rc != EOK)
385 return rc;
386 uctablep = EXFAT_NODE(fn);
387
388 uctable_chars = ALIGN_DOWN(uctablep->size,
389 sizeof(uint16_t)) / sizeof(uint16_t);
390 uctable = (uint16_t *) malloc(uctable_chars * sizeof(uint16_t));
391 rc = exfat_read_uctable(di->bs, uctablep, (uint8_t *)uctable);
392 if (rc != EOK) {
393 (void) exfat_node_put(fn);
394 free(uctable);
395 return rc;
396 }
397
398 /* Fill stream entry */
399 ds.type = EXFAT_TYPE_STREAM;
400 ds.stream.flags = 0;
401 ds.stream.valid_data_size = 0;
402 ds.stream.data_size = 0;
403 ds.stream.name_size = utf16_wsize(wname);
404 ds.stream.hash = host2uint16_t_le(exfat_name_hash(wname, uctable,
405 uctable_chars));
406
407 /* Fill file entry */
408 df.type = EXFAT_TYPE_FILE;
409 df.file.attr = 0;
410 df.file.count = ROUND_UP(ds.stream.name_size, EXFAT_NAME_PART_LEN) /
411 EXFAT_NAME_PART_LEN + 1;
412 df.file.checksum = 0;
413
414 free(uctable);
415 rc = exfat_node_put(fn);
416 if (rc != EOK)
417 return rc;
418
419 /* Looking for set of free entries */
420 rc = exfat_directory_lookup_free(di, df.file.count + 1);
421 if (rc != EOK)
422 return rc;
423 pos = di->pos;
424
425 /* Write file entry */
426 rc = exfat_directory_get(di, &de);
427 if (rc != EOK)
428 return rc;
429 *de = df;
430 di->b->dirty = true;
431 rc = exfat_directory_next(di);
432 if (rc != EOK)
433 return rc;
434
435 /* Write stream entry */
436 rc = exfat_directory_get(di, &de);
437 if (rc != EOK)
438 return rc;
439 *de = ds;
440 di->b->dirty = true;
441
442 /* Write file name */
443 size_t chars = EXFAT_NAME_PART_LEN;
444 uint16_t *sname = wname;
445
446 for (i = 0; i < ds.stream.name_size; i++)
447 wname[i] = host2uint16_t_le(wname[i]);
448
449 for (i = 0; i < df.file.count - 1; i++) {
450 rc = exfat_directory_next(di);
451 if (rc != EOK)
452 return rc;
453
454 if (i == df.file.count - 2) {
455 chars = ds.stream.name_size -
456 EXFAT_NAME_PART_LEN * (df.file.count - 2);
457 }
458
459 rc = exfat_directory_get(di, &de);
460 if (rc != EOK)
461 return rc;
462 de->type = EXFAT_TYPE_NAME;
463 /* test */
464 for (j = 0; j < chars; j++) {
465 de->name.name[j] = *sname;
466 sname++;
467 }
468
469 di->b->dirty = true;
470 }
471
472 return exfat_directory_seek(di, pos);
473}
474
475errno_t exfat_directory_erase_file(exfat_directory_t *di, aoff64_t pos)
476{
477 errno_t rc;
478 int count;
479 exfat_dentry_t *de;
480
481 di->pos = pos;
482
483 rc = exfat_directory_get(di, &de);
484 if (rc != EOK)
485 return rc;
486 count = de->file.count + 1;
487
488 while (count) {
489 rc = exfat_directory_get(di, &de);
490 if (rc != EOK)
491 return rc;
492 de->type &= ~EXFAT_TYPE_USED;
493 di->b->dirty = true;
494
495 rc = exfat_directory_next(di);
496 if (rc != EOK)
497 return rc;
498 count--;
499 }
500 return EOK;
501}
502
503errno_t exfat_directory_expand(exfat_directory_t *di)
504{
505 errno_t rc;
506
507 if (!di->nodep)
508 return ENOSPC;
509
510 rc = exfat_node_expand(di->nodep->idx->service_id, di->nodep, 1);
511 if (rc != EOK)
512 return rc;
513
514 di->fragmented = di->nodep->fragmented;
515 di->nodep->size += BPC(di->bs);
516 di->nodep->dirty = true; /* need to sync node */
517 di->blocks = di->nodep->size / BPS(di->bs);
518
519 return EOK;
520}
521
522errno_t exfat_directory_lookup_free(exfat_directory_t *di, size_t count)
523{
524 errno_t rc;
525 exfat_dentry_t *d;
526 size_t found;
527 aoff64_t pos;
528
529 rc = exfat_directory_seek(di, 0);
530 if (rc != EOK)
531 return rc;
532
533 do {
534 found = 0;
535 pos = 0;
536 do {
537 if (exfat_directory_get(di, &d) == EOK) {
538 switch (exfat_classify_dentry(d)) {
539 case EXFAT_DENTRY_LAST:
540 case EXFAT_DENTRY_FREE:
541 if (found == 0)
542 pos = di->pos;
543 found++;
544 if (found == count) {
545 exfat_directory_seek(di, pos);
546 return EOK;
547 }
548 break;
549 default:
550 found = 0;
551 break;
552 }
553 }
554 } while (exfat_directory_next(di) == EOK);
555 } while (exfat_directory_expand(di) == EOK);
556 return ENOSPC;
557}
558
559
560/**
561 * @}
562 */
Note: See TracBrowser for help on using the repository browser.