source: mainline/uspace/srv/fs/fat/fat_directory.c@ 5bf76c1

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

Cleanup.

  • Property mode set to 100644
File size: 11.6 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 fat_directory.c
35 * @brief Functions that work with FAT directory.
36 */
37
38#include "fat_directory.h"
39#include "fat_fat.h"
40#include <libblock.h>
41#include <errno.h>
42#include <byteorder.h>
43#include <mem.h>
44#include <str.h>
45#include <align.h>
46#include <stdio.h>
47
48int fat_directory_open(fat_node_t *nodep, fat_directory_t *di)
49{
50 di->b = NULL;
51 di->nodep = nodep;
52 if (di->nodep->type != FAT_DIRECTORY)
53 return EINVAL;
54
55 di->bs = block_bb_get(di->nodep->idx->service_id);
56 di->blocks = ROUND_UP(nodep->size, BPS(di->bs)) / BPS(di->bs);
57 di->pos = 0;
58 di->bnum = 0;
59 di->last = false;
60
61 return EOK;
62}
63
64int fat_directory_close(fat_directory_t *di)
65{
66 int rc = EOK;
67
68 if (di->b)
69 rc = block_put(di->b);
70
71 return rc;
72}
73
74static int fat_directory_block_load(fat_directory_t *di)
75{
76 uint32_t i;
77 int rc;
78
79 i = (di->pos * sizeof(fat_dentry_t)) / BPS(di->bs);
80 if (i < di->blocks) {
81 if (di->b && di->bnum != i) {
82 block_put(di->b);
83 di->b = NULL;
84 }
85 if (!di->b) {
86 rc = fat_block_get(&di->b, di->bs, di->nodep, i,
87 BLOCK_FLAGS_NONE);
88 if (rc != EOK) {
89 di->b = NULL;
90 return rc;
91 }
92 di->bnum = i;
93 }
94 return EOK;
95 }
96
97 return ENOENT;
98}
99
100int fat_directory_next(fat_directory_t *di)
101{
102 int rc;
103
104 di->pos += 1;
105 rc = fat_directory_block_load(di);
106 if (rc != EOK)
107 di->pos -= 1;
108
109 return rc;
110}
111
112int fat_directory_prev(fat_directory_t *di)
113{
114 int rc = EOK;
115
116 if (di->pos > 0) {
117 di->pos -= 1;
118 rc = fat_directory_block_load(di);
119 } else
120 return ENOENT;
121
122 if (rc != EOK)
123 di->pos += 1;
124
125 return rc;
126}
127
128int fat_directory_seek(fat_directory_t *di, aoff64_t pos)
129{
130 aoff64_t _pos = di->pos;
131 int rc;
132
133 di->pos = pos;
134 rc = fat_directory_block_load(di);
135 if (rc != EOK)
136 di->pos = _pos;
137
138 return rc;
139}
140
141int fat_directory_get(fat_directory_t *di, fat_dentry_t **d)
142{
143 int rc;
144
145 rc = fat_directory_block_load(di);
146 if (rc == EOK) {
147 aoff64_t o = di->pos % (BPS(di->bs) / sizeof(fat_dentry_t));
148 *d = ((fat_dentry_t *)di->b->data) + o;
149 }
150
151 return rc;
152}
153
154int fat_directory_read(fat_directory_t *di, char *name, fat_dentry_t **de)
155{
156 fat_dentry_t *d = NULL;
157 uint16_t wname[FAT_LFN_NAME_SIZE];
158 size_t lfn_offset, lfn_size;
159 bool long_entry = false;
160 int long_entry_count = 0;
161 uint8_t checksum = 0;
162 int rc;
163
164 do {
165 rc = fat_directory_get(di, &d);
166 if (rc != EOK)
167 return rc;
168
169 switch (fat_classify_dentry(d)) {
170 case FAT_DENTRY_LAST:
171 long_entry_count = 0;
172 long_entry = false;
173 return ENOENT;
174 case FAT_DENTRY_LFN:
175 if (long_entry) {
176 /* We found long entry */
177 long_entry_count--;
178 if ((FAT_LFN_ORDER(d) == long_entry_count) &&
179 (checksum == FAT_LFN_CHKSUM(d))) {
180 /* Right order! */
181 fat_lfn_get_entry(d, wname,
182 &lfn_offset);
183 } else {
184 /*
185 * Something wrong with order.
186 * Skip this long entries set.
187 */
188 long_entry_count = 0;
189 long_entry = false;
190 }
191 } else if (FAT_IS_LFN(d)) {
192 /* We found Last long entry! */
193 if (FAT_LFN_COUNT(d) <= FAT_LFN_MAX_COUNT) {
194 long_entry = true;
195 long_entry_count = FAT_LFN_COUNT(d);
196 lfn_size = (FAT_LFN_ENTRY_SIZE *
197 (FAT_LFN_COUNT(d) - 1)) +
198 fat_lfn_size(d);
199 lfn_offset = lfn_size;
200 fat_lfn_get_entry(d, wname,
201 &lfn_offset);
202 checksum = FAT_LFN_CHKSUM(d);
203 }
204 }
205 break;
206 case FAT_DENTRY_VALID:
207 if (long_entry &&
208 (checksum == fat_dentry_chksum(d->name))) {
209 wname[lfn_size] = '\0';
210 if (utf16_to_str(name, FAT_LFN_NAME_SIZE,
211 wname) != EOK)
212 fat_dentry_name_get(d, name);
213 } else
214 fat_dentry_name_get(d, name);
215
216 *de = d;
217 return EOK;
218 default:
219 case FAT_DENTRY_SKIP:
220 case FAT_DENTRY_FREE:
221 long_entry_count = 0;
222 long_entry = false;
223 break;
224 }
225 } while (fat_directory_next(di) == EOK);
226
227 return ENOENT;
228}
229
230int fat_directory_erase(fat_directory_t *di)
231{
232 int rc;
233 fat_dentry_t *d;
234 bool flag = false;
235 uint8_t checksum;
236
237 rc = fat_directory_get(di, &d);
238 if (rc != EOK)
239 return rc;
240 checksum = fat_dentry_chksum(d->name);
241
242 d->name[0] = FAT_DENTRY_ERASED;
243 di->b->dirty = true;
244
245 while (!flag && fat_directory_prev(di) == EOK) {
246 if (fat_directory_get(di, &d) == EOK &&
247 fat_classify_dentry(d) == FAT_DENTRY_LFN &&
248 checksum == FAT_LFN_CHKSUM(d)) {
249 if (FAT_IS_LFN(d))
250 flag = true;
251 memset(d, 0, sizeof(fat_dentry_t));
252 d->name[0] = FAT_DENTRY_ERASED;
253 di->b->dirty = true;
254 } else
255 break;
256 }
257
258 return EOK;
259}
260
261int fat_directory_write(fat_directory_t *di, const char *name, fat_dentry_t *de)
262{
263 int rc;
264 bool enable_lfn = true; /* TODO: make this a mount option */
265
266 if (fat_valid_short_name(name)) {
267 /*
268 * NAME could be directly stored in dentry without creating
269 * LFN.
270 */
271 fat_dentry_name_set(de, name);
272 if (fat_directory_is_sfn_exist(di, de))
273 return EEXIST;
274 rc = fat_directory_lookup_free(di, 1);
275 if (rc != EOK)
276 return rc;
277 rc = fat_directory_write_dentry(di, de);
278 return rc;
279 } else if (enable_lfn && fat_valid_name(name)) {
280 /* We should create long entries to store name */
281 int long_entry_count;
282 uint8_t checksum;
283 uint16_t wname[FAT_LFN_NAME_SIZE];
284 size_t lfn_size, lfn_offset;
285
286 rc = str_to_utf16(wname, FAT_LFN_NAME_SIZE, name);
287 if (rc != EOK)
288 return rc;
289
290 lfn_size = utf16_length(wname);
291 long_entry_count = lfn_size / FAT_LFN_ENTRY_SIZE;
292 if (lfn_size % FAT_LFN_ENTRY_SIZE)
293 long_entry_count++;
294 rc = fat_directory_lookup_free(di, long_entry_count+1);
295 if (rc != EOK)
296 return rc;
297 aoff64_t start_pos = di->pos;
298
299 /* Write Short entry */
300 rc = fat_directory_create_sfn(di, de, name);
301 if (rc != EOK)
302 return rc;
303 checksum = fat_dentry_chksum(de->name);
304
305 rc = fat_directory_seek(di, start_pos+long_entry_count);
306 if (rc != EOK)
307 return rc;
308 rc = fat_directory_write_dentry(di, de);
309 if (rc != EOK)
310 return rc;
311
312 /* Write Long entry by parts */
313 lfn_offset = 0;
314 fat_dentry_t *d;
315 size_t idx = 0;
316 do {
317 rc = fat_directory_prev(di);
318 if (rc != EOK)
319 return rc;
320 rc = fat_directory_get(di, &d);
321 if (rc != EOK)
322 return rc;
323 fat_lfn_set_entry(wname, &lfn_offset, lfn_size + 1, d);
324 FAT_LFN_CHKSUM(d) = checksum;
325 FAT_LFN_ORDER(d) = ++idx;
326 di->b->dirty = true;
327 } while (lfn_offset < lfn_size);
328 FAT_LFN_ORDER(d) |= FAT_LFN_LAST;
329
330 rc = fat_directory_seek(di, start_pos+long_entry_count);
331 return rc;
332 }
333
334 return ENOTSUP;
335}
336
337int fat_directory_create_sfn(fat_directory_t *di, fat_dentry_t *de,
338 const char *lname)
339{
340 char name[FAT_NAME_LEN + 1];
341 char ext[FAT_EXT_LEN + 1];
342 char number[FAT_NAME_LEN + 1];
343 memset(name, FAT_PAD, FAT_NAME_LEN);
344 memset(ext, FAT_PAD, FAT_EXT_LEN);
345 memset(number, FAT_PAD, FAT_NAME_LEN);
346
347 size_t name_len = str_size(lname);
348 char *pdot = str_rchr(lname, '.');
349 ext[FAT_EXT_LEN] = '\0';
350 if (pdot) {
351 pdot++;
352 str_to_ascii(ext, pdot, FAT_EXT_LEN, FAT_SFN_CHAR);
353 name_len = (pdot - lname - 1);
354 }
355 if (name_len > FAT_NAME_LEN)
356 name_len = FAT_NAME_LEN;
357 str_to_ascii(name, lname, name_len, FAT_SFN_CHAR);
358
359 unsigned idx;
360 for (idx = 1; idx <= FAT_MAX_SFN; idx++) {
361 snprintf(number, sizeof(number), "%u", idx);
362
363 /* Fill de->name with FAT_PAD */
364 memset(de->name, FAT_PAD, FAT_NAME_LEN + FAT_EXT_LEN);
365 /* Copy ext */
366 memcpy(de->ext, ext, str_size(ext));
367 /* Copy name */
368 memcpy(de->name, name, str_size(name));
369
370 /* Copy number */
371 size_t offset;
372 if (str_size(name)+str_size(number) + 1 > FAT_NAME_LEN)
373 offset = FAT_NAME_LEN - str_size(number) - 1;
374 else
375 offset = str_size(name);
376 de->name[offset] = '~';
377 offset++;
378 memcpy(de->name + offset, number, str_size(number));
379
380 if (!fat_directory_is_sfn_exist(di, de))
381 return EOK;
382 }
383
384 return ERANGE;
385}
386
387int fat_directory_write_dentry(fat_directory_t *di, fat_dentry_t *de)
388{
389 fat_dentry_t *d;
390 int rc;
391
392 rc = fat_directory_get(di, &d);
393 if (rc != EOK)
394 return rc;
395 memcpy(d, de, sizeof(fat_dentry_t));
396 di->b->dirty = true;
397
398 return EOK;
399}
400
401int fat_directory_expand(fat_directory_t *di)
402{
403 int rc;
404 fat_cluster_t mcl, lcl;
405
406 if (!FAT_IS_FAT32(di->bs) && di->nodep->firstc == FAT_CLST_ROOT) {
407 /* Can't grow the root directory on FAT12/16. */
408 return ENOSPC;
409 }
410 rc = fat_alloc_clusters(di->bs, di->nodep->idx->service_id, 1, &mcl,
411 &lcl);
412 if (rc != EOK)
413 return rc;
414 rc = fat_zero_cluster(di->bs, di->nodep->idx->service_id, mcl);
415 if (rc != EOK) {
416 (void) fat_free_clusters(di->bs, di->nodep->idx->service_id,
417 mcl);
418 return rc;
419 }
420 rc = fat_append_clusters(di->bs, di->nodep, mcl, lcl);
421 if (rc != EOK) {
422 (void) fat_free_clusters(di->bs, di->nodep->idx->service_id,
423 mcl);
424 return rc;
425 }
426 di->nodep->size += BPS(di->bs) * SPC(di->bs);
427 di->nodep->dirty = true; /* need to sync node */
428 di->blocks = di->nodep->size / BPS(di->bs);
429
430 return EOK;
431}
432
433int fat_directory_lookup_free(fat_directory_t *di, size_t count)
434{
435 fat_dentry_t *d;
436 size_t found;
437 aoff64_t pos;
438 int rc;
439
440 do {
441 found = 0;
442 pos = 0;
443 fat_directory_seek(di, 0);
444 do {
445 rc = fat_directory_get(di, &d);
446 if (rc != EOK)
447 return rc;
448
449 switch (fat_classify_dentry(d)) {
450 case FAT_DENTRY_LAST:
451 case FAT_DENTRY_FREE:
452 if (found == 0)
453 pos = di->pos;
454 found++;
455 if (found == count) {
456 fat_directory_seek(di, pos);
457 return EOK;
458 }
459 break;
460 case FAT_DENTRY_VALID:
461 case FAT_DENTRY_LFN:
462 case FAT_DENTRY_SKIP:
463 default:
464 found = 0;
465 break;
466 }
467 } while (fat_directory_next(di) == EOK);
468 } while (fat_directory_expand(di) == EOK);
469
470 return ENOSPC;
471}
472
473int fat_directory_lookup_name(fat_directory_t *di, const char *name,
474 fat_dentry_t **de)
475{
476 char entry[FAT_LFN_NAME_SIZE];
477
478 fat_directory_seek(di, 0);
479 while (fat_directory_read(di, entry, de) == EOK) {
480 if (fat_dentry_namecmp(entry, name) == 0) {
481 return EOK;
482 } else {
483 if (fat_directory_next(di) != EOK)
484 break;
485 }
486 }
487
488 return ENOENT;
489}
490
491bool fat_directory_is_sfn_exist(fat_directory_t *di, fat_dentry_t *de)
492{
493 fat_dentry_t *d;
494 int rc;
495
496 fat_directory_seek(di, 0);
497 do {
498 rc = fat_directory_get(di, &d);
499 if (rc != EOK)
500 return false;
501
502 switch (fat_classify_dentry(d)) {
503 case FAT_DENTRY_LAST:
504 return false;
505 case FAT_DENTRY_VALID:
506 if (bcmp(de->name, d->name,
507 FAT_NAME_LEN + FAT_EXT_LEN)==0)
508 return true;
509 break;
510 default:
511 case FAT_DENTRY_LFN:
512 case FAT_DENTRY_SKIP:
513 case FAT_DENTRY_FREE:
514 break;
515 }
516 } while (fat_directory_next(di) == EOK);
517
518 return false;
519}
520
521/**
522 * @}
523 */
Note: See TracBrowser for help on using the repository browser.