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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since b4af128 was 7d78d163, checked in by Oleg Romanenko <romanenko.oleg@…>, 14 years ago

exFAT: add exfat_directory_expand and exfat_directory_lookup_free functions

  • Property mode set to 100644
File size: 8.9 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_directory.h"
39#include "exfat_fat.h"
40#include <libblock.h>
41#include <errno.h>
42#include <byteorder.h>
43#include <mem.h>
44#include <malloc.h>
45#include <str.h>
46
47
48void exfat_directory_init(exfat_directory_t *di)
49{
50 di->b = NULL;
51 di->nodep = NULL;
52 di->bs = NULL;
53 di->blocks = 0;
54 di->pos = 0;
55 di->bnum = 0;
56 di->last = false;
57 di->fragmented = false;
58 di->firstc = 0;
59}
60
61int exfat_directory_open(exfat_node_t *nodep, exfat_directory_t *di)
62{
63 exfat_directory_init(di);
64 di->nodep = nodep;
65 if (di->nodep->type != EXFAT_DIRECTORY)
66 return EINVAL;
67 di->devmap_handle = nodep->idx->devmap_handle;
68 di->fragmented = nodep->fragmented;
69 di->firstc = nodep->firstc;
70
71 di->bs = block_bb_get(di->devmap_handle);
72 di->blocks = nodep->size / BPS(di->bs);
73 return EOK;
74}
75
76int exfat_directory_open_parent(exfat_directory_t *di,
77 devmap_handle_t devmap_handle, exfat_cluster_t firstc, bool fragmented)
78{
79 exfat_directory_init(di);
80 di->devmap_handle = devmap_handle;
81 di->fragmented = fragmented;
82 di->firstc = firstc;
83 di->bs = block_bb_get(devmap_handle);
84 di->blocks = 0;
85 return EOK;
86}
87
88int exfat_directory_close(exfat_directory_t *di)
89{
90 int rc=EOK;
91
92 if (di->b)
93 rc = block_put(di->b);
94
95 return rc;
96}
97
98static int exfat_directory_block_load(exfat_directory_t *di)
99{
100 uint32_t i;
101 int rc;
102
103 i = (di->pos * sizeof(exfat_dentry_t)) / BPS(di->bs);
104 if (di->nodep && (i >= di->blocks))
105 return ENOENT;
106
107 if (di->b && di->bnum != i) {
108 block_put(di->b);
109 di->b = NULL;
110 }
111 if (!di->b) {
112 if (di->nodep) {
113 rc = exfat_block_get(&di->b, di->bs, di->nodep, i, BLOCK_FLAGS_NONE);
114 } else {
115 rc = exfat_block_get_by_clst(&di->b, di->bs, di->devmap_handle,
116 di->fragmented, di->firstc, NULL, i, BLOCK_FLAGS_NONE);
117 }
118 if (rc != EOK) {
119 di->b = NULL;
120 return rc;
121 }
122 di->bnum = i;
123 }
124 return EOK;
125}
126
127int exfat_directory_next(exfat_directory_t *di)
128{
129 int rc;
130
131 di->pos += 1;
132 rc = exfat_directory_block_load(di);
133 if (rc!=EOK)
134 di->pos -= 1;
135
136 return rc;
137}
138
139int exfat_directory_prev(exfat_directory_t *di)
140{
141 int rc=EOK;
142
143 if (di->pos > 0) {
144 di->pos -= 1;
145 rc=exfat_directory_block_load(di);
146 }
147 else
148 return ENOENT;
149
150 if (rc!=EOK)
151 di->pos += 1;
152
153 return rc;
154}
155
156int exfat_directory_seek(exfat_directory_t *di, aoff64_t pos)
157{
158 aoff64_t _pos = di->pos;
159 int rc;
160
161 di->pos = pos;
162 rc = exfat_directory_block_load(di);
163 if (rc!=EOK)
164 di->pos = _pos;
165
166 return rc;
167}
168
169int exfat_directory_get(exfat_directory_t *di, exfat_dentry_t **d)
170{
171 int rc;
172
173 rc = exfat_directory_block_load(di);
174 if (rc == EOK) {
175 aoff64_t o = di->pos % (BPS(di->bs) / sizeof(exfat_dentry_t));
176 *d = ((exfat_dentry_t *)di->b->data) + o;
177 }
178
179 return rc;
180}
181
182int exfat_directory_find(exfat_directory_t *di, exfat_dentry_clsf_t type, exfat_dentry_t **d)
183{
184 do {
185 if (exfat_directory_get(di, d) == EOK) {
186 if (exfat_classify_dentry(*d) == type)
187 return EOK;
188 } else
189 return ENOENT;
190 } while (exfat_directory_next(di) == EOK);
191
192 return ENOENT;
193}
194
195int exfat_directory_find_continue(exfat_directory_t *di, exfat_dentry_clsf_t type, exfat_dentry_t **d)
196{
197 int rc;
198 rc = exfat_directory_next(di);
199 if (rc != EOK)
200 return rc;
201 return exfat_directory_find(di, type, d);
202}
203
204
205int exfat_directory_read_file(exfat_directory_t *di, char *name, size_t size,
206 exfat_file_dentry_t *df, exfat_stream_dentry_t *ds)
207{
208 uint16_t wname[EXFAT_FILENAME_LEN+1];
209 exfat_dentry_t *d = NULL;
210 int rc, i;
211 size_t offset = 0;
212 aoff64_t start_pos = 0;
213
214 rc = exfat_directory_find(di, EXFAT_DENTRY_FILE, &d);
215 if (rc != EOK)
216 return rc;
217 start_pos = di->pos;
218 *df = d->file;
219
220 rc = exfat_directory_next(di);
221 if (rc != EOK)
222 return rc;
223 rc = exfat_directory_get(di, &d);
224 if (rc != EOK)
225 return rc;
226 if (exfat_classify_dentry(d) != EXFAT_DENTRY_STREAM)
227 return ENOENT;
228 *ds = d->stream;
229
230 if (ds->name_size > size)
231 return EOVERFLOW;
232
233 for (i=0; i<df->count-1; i++) {
234 rc = exfat_directory_next(di);
235 if (rc != EOK)
236 return rc;
237 rc = exfat_directory_get(di, &d);
238 if (rc != EOK)
239 return rc;
240 if (exfat_classify_dentry(d) != EXFAT_DENTRY_NAME)
241 return ENOENT;
242 exfat_dentry_get_name(&d->name, ds->name_size, wname, &offset);
243 }
244 rc = utf16_to_str(name, size, wname);
245 if (rc != EOK)
246 return rc;
247
248 exfat_directory_seek(di, start_pos);
249 return EOK;
250}
251
252static uint16_t exfat_directory_set_checksum(const uint8_t *bytes, size_t count)
253{
254 uint16_t checksum = 0;
255 size_t idx;
256
257 for (idx = 0; idx < count; idx++) {
258 if (idx == 2 || idx == 3)
259 continue;
260 checksum = ((checksum << 15) | (checksum >> 1)) + (uint16_t)bytes[idx];
261 }
262 return checksum;
263}
264
265int exfat_directory_sync_file(exfat_directory_t *di, exfat_file_dentry_t *df,
266 exfat_stream_dentry_t *ds)
267{
268 int rc, i, count;
269 exfat_dentry_t *array=NULL, *de;
270 aoff64_t pos = di->pos;
271
272 rc = exfat_directory_get(di, &de);
273 if (rc != EOK)
274 return rc;
275 count = de->file.count+1;
276 array = (exfat_dentry_t *) malloc(count*sizeof(exfat_dentry_t));
277 if (!array)
278 return ENOMEM;
279 for (i=0; i<count; i++) {
280 rc = exfat_directory_get(di, &de);
281 if (rc != EOK)
282 return rc;
283 memcpy((uint8_t *)&array[i], (uint8_t *)de, sizeof(exfat_dentry_t));
284 rc = exfat_directory_next(di);
285 if (rc!=EOK) {
286 free(array);
287 return rc;
288 }
289 }
290 rc = exfat_directory_seek(di, pos);
291 if (rc!=EOK) {
292 free(array);
293 return rc;
294 }
295
296 /* Sync */
297 array[0].file.attr = df->attr;
298 array[1].stream.firstc = ds->firstc;
299 array[1].stream.flags = ds->flags;
300 array[1].stream.valid_data_size = ds->valid_data_size;
301 array[1].stream.data_size = ds->data_size;
302 array[0].file.checksum = exfat_directory_set_checksum((uint8_t *)array,
303 count*sizeof(exfat_dentry_t));
304
305 /* Store */
306 for (i=0; i<count; i++) {
307 rc = exfat_directory_get(di, &de);
308 if (rc != EOK)
309 return rc;
310 memcpy((uint8_t *)de, (uint8_t *)&array[i], sizeof(exfat_dentry_t));
311 di->b->dirty = true;
312 rc = exfat_directory_next(di);
313 if (rc!=EOK) {
314 free(array);
315 return rc;
316 }
317 }
318
319 return EOK;
320}
321
322int exfat_directory_write_file(exfat_directory_t *di, const char *name)
323{
324 /* TODO */
325 return EOK;
326}
327
328int exfat_directory_erase_file(exfat_directory_t *di, aoff64_t pos)
329{
330 int rc, count;
331 exfat_dentry_t *de;
332
333 rc = exfat_directory_get(di, &de);
334 if (rc != EOK)
335 return rc;
336 count = de->file.count+1;
337
338 while (count) {
339 rc = exfat_directory_get(di, &de);
340 if (rc != EOK)
341 return rc;
342 de->type &= (~EXFAT_TYPE_USED);
343 di->b->dirty = true;
344
345 rc = exfat_directory_next(di);
346 if (rc!=EOK)
347 return rc;
348 count--;
349 }
350 return EOK;
351}
352
353int exfat_directory_expand(exfat_directory_t *di)
354{
355 int rc;
356
357 if (!di->nodep)
358 return ENOSPC;
359
360 rc = exfat_node_expand(di->nodep->idx->devmap_handle, di->nodep, 1);
361 if (rc != EOK)
362 return rc;
363
364 di->fragmented = di->nodep->fragmented;
365 di->nodep->size += BPC(di->bs);
366 di->nodep->dirty = true; /* need to sync node */
367 di->blocks = di->nodep->size / BPS(di->bs);
368
369 return EOK;
370}
371
372int exfat_directory_lookup_free(exfat_directory_t *di, size_t count)
373{
374 int rc;
375 exfat_dentry_t *d;
376 size_t found;
377 aoff64_t pos;
378
379 rc = exfat_directory_seek(di, 0);
380 if (rc != EOK)
381 return rc;
382
383 do {
384 found = 0;
385 pos = 0;
386 do {
387 if (exfat_directory_get(di, &d) == EOK) {
388 switch (exfat_classify_dentry(d)) {
389 case EXFAT_DENTRY_LAST:
390 case EXFAT_DENTRY_FREE:
391 if (found==0) pos = di->pos;
392 found++;
393 if (found == count) {
394 exfat_directory_seek(di, pos);
395 return EOK;
396 }
397 break;
398 default:
399 found = 0;
400 break;
401 }
402 }
403 } while (exfat_directory_next(di) == EOK);
404 } while (exfat_directory_expand(di) == EOK);
405 return ENOSPC;
406}
407
408
409/**
410 * @}
411 */
Note: See TracBrowser for help on using the repository browser.