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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since ff0c270 was ff0c270, checked in by Jakub Jermar <jakub@…>, 14 years ago

Cstyle.

  • Property mode set to 100644
File size: 11.3 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
94 if (di->b)
95 rc = block_put(di->b);
96
97 return rc;
98}
99
100static 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);
[f061de75]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) {
[ff0c270]115 rc = exfat_block_get(&di->b, di->bs, di->nodep, i,
116 BLOCK_FLAGS_NONE);
[f061de75]117 } else {
[ff0c270]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);
[4dd9395]121 }
[f061de75]122 if (rc != EOK) {
123 di->b = NULL;
124 return rc;
125 }
126 di->bnum = i;
[4dd9395]127 }
[f061de75]128 return EOK;
[4dd9395]129}
130
131int exfat_directory_next(exfat_directory_t *di)
132{
133 int rc;
134
135 di->pos += 1;
136 rc = exfat_directory_block_load(di);
[ff0c270]137 if (rc != EOK)
[4dd9395]138 di->pos -= 1;
139
140 return rc;
141}
142
143int exfat_directory_prev(exfat_directory_t *di)
144{
[ff0c270]145 int rc = EOK;
[4dd9395]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
160int 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);
[ff0c270]167 if (rc != EOK)
[4dd9395]168 di->pos = _pos;
169
170 return rc;
171}
172
173int 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
[ff0c270]186int exfat_directory_find(exfat_directory_t *di, exfat_dentry_clsf_t type,
187 exfat_dentry_t **d)
[27678a0]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
[ff0c270]200int
201exfat_directory_find_continue(exfat_directory_t *di, exfat_dentry_clsf_t type,
202 exfat_dentry_t **d)
[27678a0]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}
[4dd9395]210
[73b1218]211
[f5f1e1c]212int exfat_directory_read_file(exfat_directory_t *di, char *name, size_t size,
213 exfat_file_dentry_t *df, exfat_stream_dentry_t *ds)
[73b1218]214{
[ff0c270]215 uint16_t wname[EXFAT_FILENAME_LEN + 1];
[73b1218]216 exfat_dentry_t *d = NULL;
217 int rc, i;
218 size_t offset = 0;
[963ea42]219 aoff64_t start_pos = 0;
[73b1218]220
221 rc = exfat_directory_find(di, EXFAT_DENTRY_FILE, &d);
222 if (rc != EOK)
223 return rc;
[963ea42]224 start_pos = di->pos;
[73b1218]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;
[f5f1e1c]236
237 if (ds->name_size > size)
[73b1218]238 return EOVERFLOW;
[f5f1e1c]239
[ff0c270]240 for (i = 0; i < df->count - 1; i++) {
[73b1218]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 }
[f5f1e1c]251 rc = utf16_to_str(name, size, wname);
[73b1218]252 if (rc != EOK)
253 return rc;
254
[963ea42]255 exfat_directory_seek(di, start_pos);
[73b1218]256 return EOK;
257}
258
[d8df2fb]259static uint16_t exfat_directory_set_checksum(const uint8_t *bytes, size_t count)
[151a4e2]260{
[d8df2fb]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
272int 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;
[ff0c270]276 exfat_dentry_t *array = NULL, *de;
[d8df2fb]277 aoff64_t pos = di->pos;
278
279 rc = exfat_directory_get(di, &de);
280 if (rc != EOK)
281 return rc;
[ff0c270]282 count = de->file.count + 1;
[d8df2fb]283 array = (exfat_dentry_t *) malloc(count*sizeof(exfat_dentry_t));
284 if (!array)
285 return ENOMEM;
[ff0c270]286 for (i = 0; i < count; i++) {
[d8df2fb]287 rc = exfat_directory_get(di, &de);
288 if (rc != EOK)
289 return rc;
[c56c4576]290 array[i] = *de;
[d8df2fb]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);
[ff0c270]298 if (rc != EOK) {
[d8df2fb]299 free(array);
300 return rc;
301 }
302
303 /* Sync */
[c56c4576]304 array[0].file.attr = host2uint16_t_le(df->attr);
305 array[1].stream.firstc = host2uint32_t_le(ds->firstc);
[d8df2fb]306 array[1].stream.flags = ds->flags;
[c56c4576]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);
[bca3eac]309 array[0].file.checksum = host2uint16_t_le(exfat_directory_set_checksum((uint8_t *)array,
[ff0c270]310 count * sizeof(exfat_dentry_t)));
[d8df2fb]311
312 /* Store */
[ff0c270]313 for (i = 0; i < count; i++) {
[d8df2fb]314 rc = exfat_directory_get(di, &de);
315 if (rc != EOK)
316 return rc;
[c56c4576]317 *de = array[i];
[d8df2fb]318 di->b->dirty = true;
319 rc = exfat_directory_next(di);
[ff0c270]320 if (rc != EOK) {
[d8df2fb]321 free(array);
322 return rc;
323 }
324 }
[c56c4576]325 free(array);
[d8df2fb]326
[151a4e2]327 return EOK;
328}
329
[bca3eac]330int 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;
[ff0c270]336 uint16_t wname[EXFAT_FILENAME_LEN + 1];
[bca3eac]337 int rc, i;
[c56c4576]338 size_t uctable_chars, j;
[bca3eac]339 aoff64_t pos;
340
341 rc = str_to_utf16(wname, EXFAT_FILENAME_LEN, name);
342 if (rc != EOK)
343 return rc;
[375ab5e]344 rc = exfat_uctable_get(&fn, di->service_id);
[bca3eac]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;
[c56c4576]389 *de = df;
[bca3eac]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;
[c56c4576]399 *de = ds;
[bca3eac]400 di->b->dirty = true;
401
402 /* Write file name */
403 size_t chars = EXFAT_NAME_PART_LEN;
404 uint16_t *sname = wname;
405
[ff0c270]406 for (i = 0; i < ds.stream.name_size; i++)
[bca3eac]407 wname[i] = host2uint16_t_le(wname[i]);
408
[ff0c270]409 for (i = 0; i < df.file.count - 1; i++) {
[bca3eac]410 rc = exfat_directory_next(di);
411 if (rc != EOK)
412 return rc;
413
[ff0c270]414 if (i == df.file.count - 2)
415 chars = ds.stream.name_size - EXFAT_NAME_PART_LEN*(df.file.count - 2);
[bca3eac]416 rc = exfat_directory_get(di, &de);
417 if (rc != EOK)
418 return rc;
419 de->type = EXFAT_TYPE_NAME;
[c56c4576]420 /* test */
[ff0c270]421 for (j = 0; j < chars; j++) {
[c56c4576]422 de->name.name[j] = *sname;
423 sname++;
424 }
425
[bca3eac]426 di->b->dirty = true;
427 sname += chars;
428 }
429
430 return exfat_directory_seek(di, pos);
431}
432
[7f0c08c]433int exfat_directory_erase_file(exfat_directory_t *di, aoff64_t pos)
434{
[4ba3535]435 int rc, count;
436 exfat_dentry_t *de;
437
438 rc = exfat_directory_get(di, &de);
439 if (rc != EOK)
440 return rc;
[ff0c270]441 count = de->file.count + 1;
[4ba3535]442
443 while (count) {
444 rc = exfat_directory_get(di, &de);
445 if (rc != EOK)
446 return rc;
[ff0c270]447 de->type &= ~EXFAT_TYPE_USED;
[4ba3535]448 di->b->dirty = true;
449
450 rc = exfat_directory_next(di);
[ff0c270]451 if (rc != EOK)
[4ba3535]452 return rc;
453 count--;
454 }
[7f0c08c]455 return EOK;
456}
457
[7d78d163]458int exfat_directory_expand(exfat_directory_t *di)
459{
460 int rc;
461
462 if (!di->nodep)
463 return ENOSPC;
464
[375ab5e]465 rc = exfat_node_expand(di->nodep->idx->service_id, di->nodep, 1);
[7d78d163]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
477int 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:
[ff0c270]496 if (found == 0)
497 pos = di->pos;
[7d78d163]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
[73b1218]514
[4dd9395]515/**
516 * @}
517 */
Note: See TracBrowser for help on using the repository browser.