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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since d2f5148 was e738d56, checked in by Maurizio Lombardi <m.lombardi85@…>, 14 years ago

exfat: rename extern symbols not prefixed by "exfat_"

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