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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since b7fd2a0 was b7fd2a0, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 8 years ago

Use errno_t in all uspace and kernel code.

Change type of every variable, parameter and return value that holds an
<errno.h> constant to either errno_t (the usual case), or sys_errno_t
(some places in kernel). This is for the purpose of self-documentation,
as well as for type-checking with a bit of type definition hackery.

Although this is a massive commit, it is a simple text replacement, and thus
is very easy to verify. Simply do the following:

`
git checkout <this commit's hash>
git reset HEAD
git add .
tools/srepl '\berrno_t\b' int
git add .
tools/srepl '\bsys_errno_t\b' sysarg_t
git reset
git diff
`

While this doesn't ensure that the replacements are correct, it does ensure
that the commit doesn't do anything except those replacements. Since errno_t
is typedef'd to int in the usual case (and sys_errno_t to sysarg_t), even if
incorrect, this commit cannot change behavior.

  • Property mode set to 100644
File size: 12.1 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 = nodep->size / BPS(di->bs); */
74 di->blocks = ROUND_UP(nodep->size, BPS(di->bs)) / BPS(di->bs);
75 return EOK;
76}
77
78errno_t 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
90errno_t exfat_directory_close(exfat_directory_t *di)
91{
92 errno_t 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 errno_t exfat_directory_block_load(exfat_directory_t *di)
103{
104 uint32_t i;
105 errno_t 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
135errno_t exfat_directory_next(exfat_directory_t *di)
136{
137 errno_t 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
147errno_t exfat_directory_prev(exfat_directory_t *di)
148{
149 errno_t 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
163errno_t exfat_directory_seek(exfat_directory_t *di, aoff64_t pos)
164{
165 aoff64_t _pos = di->pos;
166 errno_t 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
176errno_t exfat_directory_get(exfat_directory_t *di, exfat_dentry_t **d)
177{
178 errno_t 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
189errno_t 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
203errno_t
204exfat_directory_find_continue(exfat_directory_t *di, exfat_dentry_clsf_t type,
205 exfat_dentry_t **d)
206{
207 errno_t 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
215errno_t 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 errno_t rc;
221 int i;
222 size_t offset = 0;
223 aoff64_t start_pos = 0;
224
225 rc = exfat_directory_find(di, EXFAT_DENTRY_FILE, &d);
226 if (rc != EOK)
227 return rc;
228 start_pos = di->pos;
229 *df = d->file;
230
231 rc = exfat_directory_next(di);
232 if (rc != EOK)
233 return rc;
234 rc = exfat_directory_get(di, &d);
235 if (rc != EOK)
236 return rc;
237 if (exfat_classify_dentry(d) != EXFAT_DENTRY_STREAM)
238 return ENOENT;
239 *ds = d->stream;
240
241 if (ds->name_size > size)
242 return EOVERFLOW;
243
244 for (i = 0; i < df->count - 1; i++) {
245 rc = exfat_directory_next(di);
246 if (rc != EOK)
247 return rc;
248 rc = exfat_directory_get(di, &d);
249 if (rc != EOK)
250 return rc;
251 if (exfat_classify_dentry(d) != EXFAT_DENTRY_NAME)
252 return ENOENT;
253 exfat_dentry_get_name(&d->name, ds->name_size, wname, &offset);
254 }
255 rc = utf16_to_str(name, size, wname);
256 if (rc != EOK)
257 return rc;
258
259 exfat_directory_seek(di, start_pos);
260 return EOK;
261}
262
263errno_t exfat_directory_read_vollabel(exfat_directory_t *di, char *label,
264 size_t size)
265{
266 uint16_t wlabel[EXFAT_VOLLABEL_LEN + 1];
267 exfat_dentry_t *d = NULL;
268 errno_t rc;
269 aoff64_t start_pos = 0;
270
271 start_pos = di->pos;
272
273 rc = exfat_directory_seek(di, 0);
274 if (rc != EOK)
275 return rc;
276
277 rc = exfat_directory_find(di, EXFAT_DENTRY_VOLLABEL, &d);
278 if (rc != EOK)
279 return rc;
280
281 exfat_dentry_get_vollabel(&d->vollabel, EXFAT_VOLLABEL_LEN, wlabel);
282
283 rc = utf16_to_str(label, size, wlabel);
284 if (rc != EOK)
285 return rc;
286
287 exfat_directory_seek(di, start_pos);
288 return EOK;
289}
290
291static uint16_t exfat_directory_set_checksum(const uint8_t *bytes, size_t count)
292{
293 uint16_t checksum = 0;
294 size_t idx;
295
296 for (idx = 0; idx < count; idx++) {
297 if (idx == 2 || idx == 3)
298 continue;
299 checksum = ((checksum << 15) | (checksum >> 1)) +
300 (uint16_t)bytes[idx];
301 }
302 return checksum;
303}
304
305errno_t exfat_directory_sync_file(exfat_directory_t *di, exfat_file_dentry_t *df,
306 exfat_stream_dentry_t *ds)
307{
308 errno_t rc;
309 int i, count;
310 exfat_dentry_t *array = NULL, *de;
311 aoff64_t pos = di->pos;
312
313 rc = exfat_directory_get(di, &de);
314 if (rc != EOK)
315 return rc;
316 count = de->file.count + 1;
317 array = (exfat_dentry_t *) malloc(count * sizeof(exfat_dentry_t));
318 if (!array)
319 return ENOMEM;
320 for (i = 0; i < count; i++) {
321 rc = exfat_directory_get(di, &de);
322 if (rc != EOK) {
323 free(array);
324 return rc;
325 }
326 array[i] = *de;
327 rc = exfat_directory_next(di);
328 if (rc != EOK) {
329 free(array);
330 return rc;
331 }
332 }
333 rc = exfat_directory_seek(di, pos);
334 if (rc != EOK) {
335 free(array);
336 return rc;
337 }
338
339 /* Sync */
340 array[0].file.attr = host2uint16_t_le(df->attr);
341 array[1].stream.firstc = host2uint32_t_le(ds->firstc);
342 array[1].stream.flags = ds->flags;
343 array[1].stream.valid_data_size = host2uint64_t_le(ds->valid_data_size);
344 array[1].stream.data_size = host2uint64_t_le(ds->data_size);
345 array[0].file.checksum =
346 host2uint16_t_le(exfat_directory_set_checksum((uint8_t *)array,
347 count * sizeof(exfat_dentry_t)));
348
349 /* Store */
350 for (i = 0; i < count; i++) {
351 rc = exfat_directory_get(di, &de);
352 if (rc != EOK) {
353 free(array);
354 return rc;
355 }
356 *de = array[i];
357 di->b->dirty = true;
358 rc = exfat_directory_next(di);
359 if (rc != EOK) {
360 free(array);
361 return rc;
362 }
363 }
364 free(array);
365
366 return EOK;
367}
368
369errno_t exfat_directory_write_file(exfat_directory_t *di, const char *name)
370{
371 fs_node_t *fn;
372 exfat_node_t *uctablep;
373 uint16_t *uctable;
374 exfat_dentry_t df, ds, *de;
375 uint16_t wname[EXFAT_FILENAME_LEN + 1];
376 errno_t rc;
377 int i;
378 size_t uctable_chars, j;
379 aoff64_t pos;
380
381 rc = str_to_utf16(wname, EXFAT_FILENAME_LEN, name);
382 if (rc != EOK)
383 return rc;
384 rc = exfat_uctable_get(&fn, di->service_id);
385 if (rc != EOK)
386 return rc;
387 uctablep = EXFAT_NODE(fn);
388
389 uctable_chars = ALIGN_DOWN(uctablep->size,
390 sizeof(uint16_t)) / sizeof(uint16_t);
391 uctable = (uint16_t *) malloc(uctable_chars * sizeof(uint16_t));
392 rc = exfat_read_uctable(di->bs, uctablep, (uint8_t *)uctable);
393 if (rc != EOK) {
394 (void) exfat_node_put(fn);
395 free(uctable);
396 return rc;
397 }
398
399 /* Fill stream entry */
400 ds.type = EXFAT_TYPE_STREAM;
401 ds.stream.flags = 0;
402 ds.stream.valid_data_size = 0;
403 ds.stream.data_size = 0;
404 ds.stream.name_size = utf16_wsize(wname);
405 ds.stream.hash = host2uint16_t_le(exfat_name_hash(wname, uctable,
406 uctable_chars));
407
408 /* Fill file entry */
409 df.type = EXFAT_TYPE_FILE;
410 df.file.attr = 0;
411 df.file.count = ROUND_UP(ds.stream.name_size, EXFAT_NAME_PART_LEN) /
412 EXFAT_NAME_PART_LEN + 1;
413 df.file.checksum = 0;
414
415 free(uctable);
416 rc = exfat_node_put(fn);
417 if (rc != EOK)
418 return rc;
419
420 /* Looking for set of free entries */
421 rc = exfat_directory_lookup_free(di, df.file.count + 1);
422 if (rc != EOK)
423 return rc;
424 pos = di->pos;
425
426 /* Write file entry */
427 rc = exfat_directory_get(di, &de);
428 if (rc != EOK)
429 return rc;
430 *de = df;
431 di->b->dirty = true;
432 rc = exfat_directory_next(di);
433 if (rc != EOK)
434 return rc;
435
436 /* Write stream entry */
437 rc = exfat_directory_get(di, &de);
438 if (rc != EOK)
439 return rc;
440 *de = ds;
441 di->b->dirty = true;
442
443 /* Write file name */
444 size_t chars = EXFAT_NAME_PART_LEN;
445 uint16_t *sname = wname;
446
447 for (i = 0; i < ds.stream.name_size; i++)
448 wname[i] = host2uint16_t_le(wname[i]);
449
450 for (i = 0; i < df.file.count - 1; i++) {
451 rc = exfat_directory_next(di);
452 if (rc != EOK)
453 return rc;
454
455 if (i == df.file.count - 2) {
456 chars = ds.stream.name_size -
457 EXFAT_NAME_PART_LEN*(df.file.count - 2);
458 }
459
460 rc = exfat_directory_get(di, &de);
461 if (rc != EOK)
462 return rc;
463 de->type = EXFAT_TYPE_NAME;
464 /* test */
465 for (j = 0; j < chars; j++) {
466 de->name.name[j] = *sname;
467 sname++;
468 }
469
470 di->b->dirty = true;
471 }
472
473 return exfat_directory_seek(di, pos);
474}
475
476errno_t exfat_directory_erase_file(exfat_directory_t *di, aoff64_t pos)
477{
478 errno_t rc;
479 int count;
480 exfat_dentry_t *de;
481
482 di->pos = pos;
483
484 rc = exfat_directory_get(di, &de);
485 if (rc != EOK)
486 return rc;
487 count = de->file.count + 1;
488
489 while (count) {
490 rc = exfat_directory_get(di, &de);
491 if (rc != EOK)
492 return rc;
493 de->type &= ~EXFAT_TYPE_USED;
494 di->b->dirty = true;
495
496 rc = exfat_directory_next(di);
497 if (rc != EOK)
498 return rc;
499 count--;
500 }
501 return EOK;
502}
503
504errno_t exfat_directory_expand(exfat_directory_t *di)
505{
506 errno_t rc;
507
508 if (!di->nodep)
509 return ENOSPC;
510
511 rc = exfat_node_expand(di->nodep->idx->service_id, di->nodep, 1);
512 if (rc != EOK)
513 return rc;
514
515 di->fragmented = di->nodep->fragmented;
516 di->nodep->size += BPC(di->bs);
517 di->nodep->dirty = true; /* need to sync node */
518 di->blocks = di->nodep->size / BPS(di->bs);
519
520 return EOK;
521}
522
523errno_t exfat_directory_lookup_free(exfat_directory_t *di, size_t count)
524{
525 errno_t rc;
526 exfat_dentry_t *d;
527 size_t found;
528 aoff64_t pos;
529
530 rc = exfat_directory_seek(di, 0);
531 if (rc != EOK)
532 return rc;
533
534 do {
535 found = 0;
536 pos = 0;
537 do {
538 if (exfat_directory_get(di, &d) == EOK) {
539 switch (exfat_classify_dentry(d)) {
540 case EXFAT_DENTRY_LAST:
541 case EXFAT_DENTRY_FREE:
542 if (found == 0)
543 pos = di->pos;
544 found++;
545 if (found == count) {
546 exfat_directory_seek(di, pos);
547 return EOK;
548 }
549 break;
550 default:
551 found = 0;
552 break;
553 }
554 }
555 } while (exfat_directory_next(di) == EOK);
556 } while (exfat_directory_expand(di) == EOK);
557 return ENOSPC;
558}
559
560
561/**
562 * @}
563 */
Note: See TracBrowser for help on using the repository browser.