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

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

exfat: Fix memory leak in the exfat_directory_sync_file() function.

  • 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
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;
[b741888]103 int rc = EOK;
[4dd9395]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) {
[b741888]110 rc = block_put(di->b);
[f061de75]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 }
[b741888]128 return rc;
[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;
[0dbe5ac]149 rc = exfat_directory_block_load(di);
150 } else
[4dd9395]151 return ENOENT;
152
[0dbe5ac]153 if (rc != EOK)
[4dd9395]154 di->pos += 1;
155
156 return rc;
157}
158
159int exfat_directory_seek(exfat_directory_t *di, aoff64_t pos)
160{
161 aoff64_t _pos = di->pos;
162 int rc;
163
164 di->pos = pos;
165 rc = exfat_directory_block_load(di);
[ff0c270]166 if (rc != EOK)
[4dd9395]167 di->pos = _pos;
168
169 return rc;
170}
171
172int exfat_directory_get(exfat_directory_t *di, exfat_dentry_t **d)
173{
174 int rc;
175
176 rc = exfat_directory_block_load(di);
177 if (rc == EOK) {
178 aoff64_t o = di->pos % (BPS(di->bs) / sizeof(exfat_dentry_t));
179 *d = ((exfat_dentry_t *)di->b->data) + o;
180 }
181
182 return rc;
183}
184
[ff0c270]185int exfat_directory_find(exfat_directory_t *di, exfat_dentry_clsf_t type,
186 exfat_dentry_t **d)
[27678a0]187{
188 do {
189 if (exfat_directory_get(di, d) == EOK) {
190 if (exfat_classify_dentry(*d) == type)
191 return EOK;
192 } else
193 return ENOENT;
194 } while (exfat_directory_next(di) == EOK);
195
196 return ENOENT;
197}
198
[ff0c270]199int
200exfat_directory_find_continue(exfat_directory_t *di, exfat_dentry_clsf_t type,
201 exfat_dentry_t **d)
[27678a0]202{
203 int rc;
204 rc = exfat_directory_next(di);
205 if (rc != EOK)
206 return rc;
207 return exfat_directory_find(di, type, d);
208}
[4dd9395]209
[73b1218]210
[f5f1e1c]211int exfat_directory_read_file(exfat_directory_t *di, char *name, size_t size,
212 exfat_file_dentry_t *df, exfat_stream_dentry_t *ds)
[73b1218]213{
[ff0c270]214 uint16_t wname[EXFAT_FILENAME_LEN + 1];
[73b1218]215 exfat_dentry_t *d = NULL;
216 int rc, i;
217 size_t offset = 0;
[963ea42]218 aoff64_t start_pos = 0;
[73b1218]219
220 rc = exfat_directory_find(di, EXFAT_DENTRY_FILE, &d);
221 if (rc != EOK)
222 return rc;
[963ea42]223 start_pos = di->pos;
[73b1218]224 *df = d->file;
225
226 rc = exfat_directory_next(di);
227 if (rc != EOK)
228 return rc;
229 rc = exfat_directory_get(di, &d);
230 if (rc != EOK)
231 return rc;
232 if (exfat_classify_dentry(d) != EXFAT_DENTRY_STREAM)
233 return ENOENT;
234 *ds = d->stream;
[f5f1e1c]235
236 if (ds->name_size > size)
[73b1218]237 return EOVERFLOW;
[f5f1e1c]238
[ff0c270]239 for (i = 0; i < df->count - 1; i++) {
[73b1218]240 rc = exfat_directory_next(di);
241 if (rc != EOK)
242 return rc;
243 rc = exfat_directory_get(di, &d);
244 if (rc != EOK)
245 return rc;
246 if (exfat_classify_dentry(d) != EXFAT_DENTRY_NAME)
247 return ENOENT;
248 exfat_dentry_get_name(&d->name, ds->name_size, wname, &offset);
249 }
[f5f1e1c]250 rc = utf16_to_str(name, size, wname);
[73b1218]251 if (rc != EOK)
252 return rc;
253
[963ea42]254 exfat_directory_seek(di, start_pos);
[73b1218]255 return EOK;
256}
257
[d8df2fb]258static uint16_t exfat_directory_set_checksum(const uint8_t *bytes, size_t count)
[151a4e2]259{
[d8df2fb]260 uint16_t checksum = 0;
261 size_t idx;
262
263 for (idx = 0; idx < count; idx++) {
264 if (idx == 2 || idx == 3)
265 continue;
266 checksum = ((checksum << 15) | (checksum >> 1)) + (uint16_t)bytes[idx];
267 }
268 return checksum;
269}
270
271int exfat_directory_sync_file(exfat_directory_t *di, exfat_file_dentry_t *df,
272 exfat_stream_dentry_t *ds)
273{
274 int rc, i, count;
[ff0c270]275 exfat_dentry_t *array = NULL, *de;
[d8df2fb]276 aoff64_t pos = di->pos;
277
278 rc = exfat_directory_get(di, &de);
279 if (rc != EOK)
280 return rc;
[ff0c270]281 count = de->file.count + 1;
[0dbe5ac]282 array = (exfat_dentry_t *) malloc(count * sizeof(exfat_dentry_t));
[d8df2fb]283 if (!array)
284 return ENOMEM;
[ff0c270]285 for (i = 0; i < count; i++) {
[d8df2fb]286 rc = exfat_directory_get(di, &de);
[055be8a]287 if (rc != EOK) {
288 free(array);
[d8df2fb]289 return rc;
[055be8a]290 }
[c56c4576]291 array[i] = *de;
[d8df2fb]292 rc = exfat_directory_next(di);
[0dbe5ac]293 if (rc != EOK) {
[d8df2fb]294 free(array);
295 return rc;
296 }
297 }
298 rc = exfat_directory_seek(di, pos);
[ff0c270]299 if (rc != EOK) {
[d8df2fb]300 free(array);
301 return rc;
302 }
303
304 /* Sync */
[c56c4576]305 array[0].file.attr = host2uint16_t_le(df->attr);
306 array[1].stream.firstc = host2uint32_t_le(ds->firstc);
[d8df2fb]307 array[1].stream.flags = ds->flags;
[c56c4576]308 array[1].stream.valid_data_size = host2uint64_t_le(ds->valid_data_size);
309 array[1].stream.data_size = host2uint64_t_le(ds->data_size);
[bca3eac]310 array[0].file.checksum = host2uint16_t_le(exfat_directory_set_checksum((uint8_t *)array,
[ff0c270]311 count * sizeof(exfat_dentry_t)));
[d8df2fb]312
313 /* Store */
[ff0c270]314 for (i = 0; i < count; i++) {
[d8df2fb]315 rc = exfat_directory_get(di, &de);
[055be8a]316 if (rc != EOK) {
317 free(array);
[d8df2fb]318 return rc;
[055be8a]319 }
[c56c4576]320 *de = array[i];
[d8df2fb]321 di->b->dirty = true;
322 rc = exfat_directory_next(di);
[ff0c270]323 if (rc != EOK) {
[d8df2fb]324 free(array);
325 return rc;
326 }
327 }
[c56c4576]328 free(array);
[d8df2fb]329
[151a4e2]330 return EOK;
331}
332
[bca3eac]333int exfat_directory_write_file(exfat_directory_t *di, const char *name)
334{
335 fs_node_t *fn;
336 exfat_node_t *uctablep;
337 uint16_t *uctable;
338 exfat_dentry_t df, ds, *de;
[ff0c270]339 uint16_t wname[EXFAT_FILENAME_LEN + 1];
[bca3eac]340 int rc, i;
[c56c4576]341 size_t uctable_chars, j;
[bca3eac]342 aoff64_t pos;
343
344 rc = str_to_utf16(wname, EXFAT_FILENAME_LEN, name);
345 if (rc != EOK)
346 return rc;
[375ab5e]347 rc = exfat_uctable_get(&fn, di->service_id);
[bca3eac]348 if (rc != EOK)
349 return rc;
350 uctablep = EXFAT_NODE(fn);
351
352 uctable_chars = ALIGN_DOWN(uctablep->size, sizeof(uint16_t)) / sizeof(uint16_t);
353 uctable = (uint16_t *) malloc(uctable_chars * sizeof(uint16_t));
354 rc = exfat_read_uctable(di->bs, uctablep, (uint8_t *)uctable);
355 if (rc != EOK) {
356 (void) exfat_node_put(fn);
357 free(uctable);
358 return rc;
359 }
360
361 /* Fill stream entry */
362 ds.type = EXFAT_TYPE_STREAM;
363 ds.stream.flags = 0;
364 ds.stream.valid_data_size = 0;
365 ds.stream.data_size = 0;
366 ds.stream.name_size = utf16_length(wname);
367 ds.stream.hash = host2uint16_t_le(exfat_name_hash(wname, uctable,
368 uctable_chars));
369
370 /* Fill file entry */
371 df.type = EXFAT_TYPE_FILE;
372 df.file.attr = 0;
373 df.file.count = ROUND_UP(ds.stream.name_size, EXFAT_NAME_PART_LEN) /
374 EXFAT_NAME_PART_LEN + 1;
375 df.file.checksum = 0;
376
377 free(uctable);
378 rc = exfat_node_put(fn);
379 if (rc != EOK)
380 return rc;
381
382 /* Looking for set of free entries */
[0dbe5ac]383 rc = exfat_directory_lookup_free(di, df.file.count + 1);
[bca3eac]384 if (rc != EOK)
385 return rc;
386 pos = di->pos;
387
388 /* Write file entry */
389 rc = exfat_directory_get(di, &de);
390 if (rc != EOK)
391 return rc;
[c56c4576]392 *de = df;
[bca3eac]393 di->b->dirty = true;
394 rc = exfat_directory_next(di);
395 if (rc != EOK)
396 return rc;
397
398 /* Write stream entry */
399 rc = exfat_directory_get(di, &de);
400 if (rc != EOK)
401 return rc;
[c56c4576]402 *de = ds;
[bca3eac]403 di->b->dirty = true;
404
405 /* Write file name */
406 size_t chars = EXFAT_NAME_PART_LEN;
407 uint16_t *sname = wname;
408
[ff0c270]409 for (i = 0; i < ds.stream.name_size; i++)
[bca3eac]410 wname[i] = host2uint16_t_le(wname[i]);
411
[ff0c270]412 for (i = 0; i < df.file.count - 1; i++) {
[bca3eac]413 rc = exfat_directory_next(di);
414 if (rc != EOK)
415 return rc;
416
[ff0c270]417 if (i == df.file.count - 2)
418 chars = ds.stream.name_size - EXFAT_NAME_PART_LEN*(df.file.count - 2);
[bca3eac]419 rc = exfat_directory_get(di, &de);
420 if (rc != EOK)
421 return rc;
422 de->type = EXFAT_TYPE_NAME;
[c56c4576]423 /* test */
[ff0c270]424 for (j = 0; j < chars; j++) {
[c56c4576]425 de->name.name[j] = *sname;
426 sname++;
427 }
428
[bca3eac]429 di->b->dirty = true;
430 sname += chars;
431 }
432
433 return exfat_directory_seek(di, pos);
434}
435
[7f0c08c]436int exfat_directory_erase_file(exfat_directory_t *di, aoff64_t pos)
437{
[4ba3535]438 int rc, count;
439 exfat_dentry_t *de;
440
[78257fe]441 di->pos = pos;
442
[4ba3535]443 rc = exfat_directory_get(di, &de);
444 if (rc != EOK)
445 return rc;
[ff0c270]446 count = de->file.count + 1;
[4ba3535]447
448 while (count) {
449 rc = exfat_directory_get(di, &de);
450 if (rc != EOK)
451 return rc;
[ff0c270]452 de->type &= ~EXFAT_TYPE_USED;
[4ba3535]453 di->b->dirty = true;
454
455 rc = exfat_directory_next(di);
[ff0c270]456 if (rc != EOK)
[4ba3535]457 return rc;
458 count--;
459 }
[7f0c08c]460 return EOK;
461}
462
[7d78d163]463int exfat_directory_expand(exfat_directory_t *di)
464{
465 int rc;
466
467 if (!di->nodep)
468 return ENOSPC;
469
[375ab5e]470 rc = exfat_node_expand(di->nodep->idx->service_id, di->nodep, 1);
[7d78d163]471 if (rc != EOK)
472 return rc;
473
474 di->fragmented = di->nodep->fragmented;
475 di->nodep->size += BPC(di->bs);
476 di->nodep->dirty = true; /* need to sync node */
477 di->blocks = di->nodep->size / BPS(di->bs);
478
479 return EOK;
480}
481
482int exfat_directory_lookup_free(exfat_directory_t *di, size_t count)
483{
484 int rc;
485 exfat_dentry_t *d;
486 size_t found;
487 aoff64_t pos;
488
489 rc = exfat_directory_seek(di, 0);
490 if (rc != EOK)
491 return rc;
492
493 do {
494 found = 0;
495 pos = 0;
496 do {
497 if (exfat_directory_get(di, &d) == EOK) {
498 switch (exfat_classify_dentry(d)) {
499 case EXFAT_DENTRY_LAST:
500 case EXFAT_DENTRY_FREE:
[ff0c270]501 if (found == 0)
502 pos = di->pos;
[7d78d163]503 found++;
504 if (found == count) {
505 exfat_directory_seek(di, pos);
506 return EOK;
507 }
508 break;
509 default:
510 found = 0;
511 break;
512 }
513 }
514 } while (exfat_directory_next(di) == EOK);
515 } while (exfat_directory_expand(di) == EOK);
516 return ENOSPC;
517}
518
[73b1218]519
[4dd9395]520/**
521 * @}
522 */
Note: See TracBrowser for help on using the repository browser.