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

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

exfat: exfat_directory_block_load(), return error if block_put() has failed.

  • 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;
270 checksum = ((checksum << 15) | (checksum >> 1)) + (uint16_t)bytes[idx];
271 }
272 return checksum;
273}
274
275int exfat_directory_sync_file(exfat_directory_t *di, exfat_file_dentry_t *df,
276 exfat_stream_dentry_t *ds)
277{
278 int rc, i, count;
[ff0c270]279 exfat_dentry_t *array = NULL, *de;
[d8df2fb]280 aoff64_t pos = di->pos;
281
282 rc = exfat_directory_get(di, &de);
283 if (rc != EOK)
284 return rc;
[ff0c270]285 count = de->file.count + 1;
[0dbe5ac]286 array = (exfat_dentry_t *) malloc(count * sizeof(exfat_dentry_t));
[d8df2fb]287 if (!array)
288 return ENOMEM;
[ff0c270]289 for (i = 0; i < count; i++) {
[d8df2fb]290 rc = exfat_directory_get(di, &de);
[055be8a]291 if (rc != EOK) {
292 free(array);
[d8df2fb]293 return rc;
[055be8a]294 }
[c56c4576]295 array[i] = *de;
[d8df2fb]296 rc = exfat_directory_next(di);
[0dbe5ac]297 if (rc != EOK) {
[d8df2fb]298 free(array);
299 return rc;
300 }
301 }
302 rc = exfat_directory_seek(di, pos);
[ff0c270]303 if (rc != EOK) {
[d8df2fb]304 free(array);
305 return rc;
306 }
307
308 /* Sync */
[c56c4576]309 array[0].file.attr = host2uint16_t_le(df->attr);
310 array[1].stream.firstc = host2uint32_t_le(ds->firstc);
[d8df2fb]311 array[1].stream.flags = ds->flags;
[c56c4576]312 array[1].stream.valid_data_size = host2uint64_t_le(ds->valid_data_size);
313 array[1].stream.data_size = host2uint64_t_le(ds->data_size);
[bca3eac]314 array[0].file.checksum = host2uint16_t_le(exfat_directory_set_checksum((uint8_t *)array,
[ff0c270]315 count * sizeof(exfat_dentry_t)));
[d8df2fb]316
317 /* Store */
[ff0c270]318 for (i = 0; i < count; i++) {
[d8df2fb]319 rc = exfat_directory_get(di, &de);
[055be8a]320 if (rc != EOK) {
321 free(array);
[d8df2fb]322 return rc;
[055be8a]323 }
[c56c4576]324 *de = array[i];
[d8df2fb]325 di->b->dirty = true;
326 rc = exfat_directory_next(di);
[ff0c270]327 if (rc != EOK) {
[d8df2fb]328 free(array);
329 return rc;
330 }
331 }
[c56c4576]332 free(array);
[d8df2fb]333
[151a4e2]334 return EOK;
335}
336
[bca3eac]337int exfat_directory_write_file(exfat_directory_t *di, const char *name)
338{
339 fs_node_t *fn;
340 exfat_node_t *uctablep;
341 uint16_t *uctable;
342 exfat_dentry_t df, ds, *de;
[ff0c270]343 uint16_t wname[EXFAT_FILENAME_LEN + 1];
[bca3eac]344 int rc, i;
[c56c4576]345 size_t uctable_chars, j;
[bca3eac]346 aoff64_t pos;
347
348 rc = str_to_utf16(wname, EXFAT_FILENAME_LEN, name);
349 if (rc != EOK)
350 return rc;
[375ab5e]351 rc = exfat_uctable_get(&fn, di->service_id);
[bca3eac]352 if (rc != EOK)
353 return rc;
354 uctablep = EXFAT_NODE(fn);
355
356 uctable_chars = ALIGN_DOWN(uctablep->size, sizeof(uint16_t)) / sizeof(uint16_t);
357 uctable = (uint16_t *) malloc(uctable_chars * sizeof(uint16_t));
358 rc = exfat_read_uctable(di->bs, uctablep, (uint8_t *)uctable);
359 if (rc != EOK) {
360 (void) exfat_node_put(fn);
361 free(uctable);
362 return rc;
363 }
364
365 /* Fill stream entry */
366 ds.type = EXFAT_TYPE_STREAM;
367 ds.stream.flags = 0;
368 ds.stream.valid_data_size = 0;
369 ds.stream.data_size = 0;
370 ds.stream.name_size = utf16_length(wname);
371 ds.stream.hash = host2uint16_t_le(exfat_name_hash(wname, uctable,
372 uctable_chars));
373
374 /* Fill file entry */
375 df.type = EXFAT_TYPE_FILE;
376 df.file.attr = 0;
377 df.file.count = ROUND_UP(ds.stream.name_size, EXFAT_NAME_PART_LEN) /
378 EXFAT_NAME_PART_LEN + 1;
379 df.file.checksum = 0;
380
381 free(uctable);
382 rc = exfat_node_put(fn);
383 if (rc != EOK)
384 return rc;
385
386 /* Looking for set of free entries */
[0dbe5ac]387 rc = exfat_directory_lookup_free(di, df.file.count + 1);
[bca3eac]388 if (rc != EOK)
389 return rc;
390 pos = di->pos;
391
392 /* Write file entry */
393 rc = exfat_directory_get(di, &de);
394 if (rc != EOK)
395 return rc;
[c56c4576]396 *de = df;
[bca3eac]397 di->b->dirty = true;
398 rc = exfat_directory_next(di);
399 if (rc != EOK)
400 return rc;
401
402 /* Write stream entry */
403 rc = exfat_directory_get(di, &de);
404 if (rc != EOK)
405 return rc;
[c56c4576]406 *de = ds;
[bca3eac]407 di->b->dirty = true;
408
409 /* Write file name */
410 size_t chars = EXFAT_NAME_PART_LEN;
411 uint16_t *sname = wname;
412
[ff0c270]413 for (i = 0; i < ds.stream.name_size; i++)
[bca3eac]414 wname[i] = host2uint16_t_le(wname[i]);
415
[ff0c270]416 for (i = 0; i < df.file.count - 1; i++) {
[bca3eac]417 rc = exfat_directory_next(di);
418 if (rc != EOK)
419 return rc;
420
[ff0c270]421 if (i == df.file.count - 2)
422 chars = ds.stream.name_size - EXFAT_NAME_PART_LEN*(df.file.count - 2);
[bca3eac]423 rc = exfat_directory_get(di, &de);
424 if (rc != EOK)
425 return rc;
426 de->type = EXFAT_TYPE_NAME;
[c56c4576]427 /* test */
[ff0c270]428 for (j = 0; j < chars; j++) {
[c56c4576]429 de->name.name[j] = *sname;
430 sname++;
431 }
432
[bca3eac]433 di->b->dirty = true;
434 }
435
436 return exfat_directory_seek(di, pos);
437}
438
[7f0c08c]439int exfat_directory_erase_file(exfat_directory_t *di, aoff64_t pos)
440{
[4ba3535]441 int rc, count;
442 exfat_dentry_t *de;
443
[78257fe]444 di->pos = pos;
445
[4ba3535]446 rc = exfat_directory_get(di, &de);
447 if (rc != EOK)
448 return rc;
[ff0c270]449 count = de->file.count + 1;
[4ba3535]450
451 while (count) {
452 rc = exfat_directory_get(di, &de);
453 if (rc != EOK)
454 return rc;
[ff0c270]455 de->type &= ~EXFAT_TYPE_USED;
[4ba3535]456 di->b->dirty = true;
457
458 rc = exfat_directory_next(di);
[ff0c270]459 if (rc != EOK)
[4ba3535]460 return rc;
461 count--;
462 }
[7f0c08c]463 return EOK;
464}
465
[7d78d163]466int exfat_directory_expand(exfat_directory_t *di)
467{
468 int rc;
469
470 if (!di->nodep)
471 return ENOSPC;
472
[375ab5e]473 rc = exfat_node_expand(di->nodep->idx->service_id, di->nodep, 1);
[7d78d163]474 if (rc != EOK)
475 return rc;
476
477 di->fragmented = di->nodep->fragmented;
478 di->nodep->size += BPC(di->bs);
479 di->nodep->dirty = true; /* need to sync node */
480 di->blocks = di->nodep->size / BPS(di->bs);
481
482 return EOK;
483}
484
485int exfat_directory_lookup_free(exfat_directory_t *di, size_t count)
486{
487 int rc;
488 exfat_dentry_t *d;
489 size_t found;
490 aoff64_t pos;
491
492 rc = exfat_directory_seek(di, 0);
493 if (rc != EOK)
494 return rc;
495
496 do {
497 found = 0;
498 pos = 0;
499 do {
500 if (exfat_directory_get(di, &d) == EOK) {
501 switch (exfat_classify_dentry(d)) {
502 case EXFAT_DENTRY_LAST:
503 case EXFAT_DENTRY_FREE:
[ff0c270]504 if (found == 0)
505 pos = di->pos;
[7d78d163]506 found++;
507 if (found == count) {
508 exfat_directory_seek(di, pos);
509 return EOK;
510 }
511 break;
512 default:
513 found = 0;
514 break;
515 }
516 }
517 } while (exfat_directory_next(di) == EOK);
518 } while (exfat_directory_expand(di) == EOK);
519 return ENOSPC;
520}
521
[73b1218]522
[4dd9395]523/**
524 * @}
525 */
Note: See TracBrowser for help on using the repository browser.