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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 6722766 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
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 <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
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
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;
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
78int 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
90int exfat_directory_close(exfat_directory_t *di)
91{
92 int rc = EOK;
93
94 if (di->b) {
95 rc = block_put(di->b);
96 di->b = NULL;
97 }
98
99 return rc;
100}
101
102static int exfat_directory_block_load(exfat_directory_t *di)
103{
104 uint32_t i;
105 int rc = EOK;
106
107 i = (di->pos * sizeof(exfat_dentry_t)) / BPS(di->bs);
108 if (di->nodep && (i >= di->blocks))
109 return ENOENT;
110
111 if (di->b && di->bnum != i) {
112 rc = block_put(di->b);
113 di->b = NULL;
114 if (rc != EOK)
115 return rc;
116 }
117 if (!di->b) {
118 if (di->nodep) {
119 rc = exfat_block_get(&di->b, di->bs, di->nodep, i,
120 BLOCK_FLAGS_NONE);
121 } else {
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);
125 }
126 if (rc != EOK) {
127 di->b = NULL;
128 return rc;
129 }
130 di->bnum = i;
131 }
132 return rc;
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);
141 if (rc != EOK)
142 di->pos -= 1;
143
144 return rc;
145}
146
147int exfat_directory_prev(exfat_directory_t *di)
148{
149 int rc = EOK;
150
151 if (di->pos > 0) {
152 di->pos -= 1;
153 rc = exfat_directory_block_load(di);
154 } else
155 return ENOENT;
156
157 if (rc != EOK)
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);
170 if (rc != EOK)
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
189int exfat_directory_find(exfat_directory_t *di, exfat_dentry_clsf_t type,
190 exfat_dentry_t **d)
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
203int
204exfat_directory_find_continue(exfat_directory_t *di, exfat_dentry_clsf_t type,
205 exfat_dentry_t **d)
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}
213
214
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)
217{
218 uint16_t wname[EXFAT_FILENAME_LEN + 1];
219 exfat_dentry_t *d = NULL;
220 int rc, 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
262static uint16_t exfat_directory_set_checksum(const uint8_t *bytes, size_t count)
263{
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;
279 exfat_dentry_t *array = NULL, *de;
280 aoff64_t pos = di->pos;
281
282 rc = exfat_directory_get(di, &de);
283 if (rc != EOK)
284 return rc;
285 count = de->file.count + 1;
286 array = (exfat_dentry_t *) malloc(count * sizeof(exfat_dentry_t));
287 if (!array)
288 return ENOMEM;
289 for (i = 0; i < count; i++) {
290 rc = exfat_directory_get(di, &de);
291 if (rc != EOK) {
292 free(array);
293 return rc;
294 }
295 array[i] = *de;
296 rc = exfat_directory_next(di);
297 if (rc != EOK) {
298 free(array);
299 return rc;
300 }
301 }
302 rc = exfat_directory_seek(di, pos);
303 if (rc != EOK) {
304 free(array);
305 return rc;
306 }
307
308 /* Sync */
309 array[0].file.attr = host2uint16_t_le(df->attr);
310 array[1].stream.firstc = host2uint32_t_le(ds->firstc);
311 array[1].stream.flags = ds->flags;
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);
314 array[0].file.checksum = host2uint16_t_le(exfat_directory_set_checksum((uint8_t *)array,
315 count * sizeof(exfat_dentry_t)));
316
317 /* Store */
318 for (i = 0; i < count; i++) {
319 rc = exfat_directory_get(di, &de);
320 if (rc != EOK) {
321 free(array);
322 return rc;
323 }
324 *de = array[i];
325 di->b->dirty = true;
326 rc = exfat_directory_next(di);
327 if (rc != EOK) {
328 free(array);
329 return rc;
330 }
331 }
332 free(array);
333
334 return EOK;
335}
336
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;
343 uint16_t wname[EXFAT_FILENAME_LEN + 1];
344 int rc, i;
345 size_t uctable_chars, j;
346 aoff64_t pos;
347
348 rc = str_to_utf16(wname, EXFAT_FILENAME_LEN, name);
349 if (rc != EOK)
350 return rc;
351 rc = exfat_uctable_get(&fn, di->service_id);
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 */
387 rc = exfat_directory_lookup_free(di, df.file.count + 1);
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;
396 *de = df;
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;
406 *de = ds;
407 di->b->dirty = true;
408
409 /* Write file name */
410 size_t chars = EXFAT_NAME_PART_LEN;
411 uint16_t *sname = wname;
412
413 for (i = 0; i < ds.stream.name_size; i++)
414 wname[i] = host2uint16_t_le(wname[i]);
415
416 for (i = 0; i < df.file.count - 1; i++) {
417 rc = exfat_directory_next(di);
418 if (rc != EOK)
419 return rc;
420
421 if (i == df.file.count - 2)
422 chars = ds.stream.name_size - EXFAT_NAME_PART_LEN*(df.file.count - 2);
423 rc = exfat_directory_get(di, &de);
424 if (rc != EOK)
425 return rc;
426 de->type = EXFAT_TYPE_NAME;
427 /* test */
428 for (j = 0; j < chars; j++) {
429 de->name.name[j] = *sname;
430 sname++;
431 }
432
433 di->b->dirty = true;
434 }
435
436 return exfat_directory_seek(di, pos);
437}
438
439int exfat_directory_erase_file(exfat_directory_t *di, aoff64_t pos)
440{
441 int rc, count;
442 exfat_dentry_t *de;
443
444 di->pos = pos;
445
446 rc = exfat_directory_get(di, &de);
447 if (rc != EOK)
448 return rc;
449 count = de->file.count + 1;
450
451 while (count) {
452 rc = exfat_directory_get(di, &de);
453 if (rc != EOK)
454 return rc;
455 de->type &= ~EXFAT_TYPE_USED;
456 di->b->dirty = true;
457
458 rc = exfat_directory_next(di);
459 if (rc != EOK)
460 return rc;
461 count--;
462 }
463 return EOK;
464}
465
466int exfat_directory_expand(exfat_directory_t *di)
467{
468 int rc;
469
470 if (!di->nodep)
471 return ENOSPC;
472
473 rc = exfat_node_expand(di->nodep->idx->service_id, di->nodep, 1);
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:
504 if (found == 0)
505 pos = di->pos;
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
522
523/**
524 * @}
525 */
Note: See TracBrowser for help on using the repository browser.