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
RevLine 
[da2f8d10]1/*
[c4bbca8]2 * Copyright (c) 2011 Oleg Romanenko
[da2f8d10]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"
[5dfb1948]39#include "fat_fat.h"
[da2f8d10]40#include <libblock.h>
41#include <errno.h>
42#include <byteorder.h>
[c6aca755]43#include <mem.h>
[4372b49]44#include <str.h>
[521550d]45#include <align.h>
[c065743]46#include <stdio.h>
[c6aca755]47
[da2f8d10]48int fat_directory_open(fat_node_t *nodep, fat_directory_t *di)
49{
[c6aca755]50 di->b = NULL;
[da2f8d10]51 di->nodep = nodep;
52 if (di->nodep->type != FAT_DIRECTORY)
53 return EINVAL;
54
[375ab5e]55 di->bs = block_bb_get(di->nodep->idx->service_id);
[5d95f02]56 di->blocks = ROUND_UP(nodep->size, BPS(di->bs)) / BPS(di->bs);
[da2f8d10]57 di->pos = 0;
58 di->bnum = 0;
59 di->last = false;
[5d95f02]60
[da2f8d10]61 return EOK;
62}
63
64int fat_directory_close(fat_directory_t *di)
65{
[5d95f02]66 int rc = EOK;
[da2f8d10]67
68 if (di->b)
69 rc = block_put(di->b);
70
71 return rc;
72}
73
[5d95f02]74static int fat_directory_block_load(fat_directory_t *di)
[da2f8d10]75{
76 uint32_t i;
77 int rc;
[c6aca755]78
[da2f8d10]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) {
[5d95f02]86 rc = fat_block_get(&di->b, di->bs, di->nodep, i,
87 BLOCK_FLAGS_NONE);
[52ee8b7a]88 if (rc != EOK) {
[7a819535]89 di->b = NULL;
[da2f8d10]90 return rc;
[52ee8b7a]91 }
92 di->bnum = i;
[da2f8d10]93 }
94 return EOK;
95 }
[5d95f02]96
[da2f8d10]97 return ENOENT;
98}
99
[c6aca755]100int fat_directory_next(fat_directory_t *di)
101{
102 int rc;
103
104 di->pos += 1;
105 rc = fat_directory_block_load(di);
[5d95f02]106 if (rc != EOK)
[c6aca755]107 di->pos -= 1;
108
109 return rc;
110}
111
112int fat_directory_prev(fat_directory_t *di)
113{
[5d95f02]114 int rc = EOK;
[c6aca755]115
116 if (di->pos > 0) {
117 di->pos -= 1;
[5d95f02]118 rc = fat_directory_block_load(di);
119 } else
[c6aca755]120 return ENOENT;
121
[5d95f02]122 if (rc != EOK)
[c6aca755]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;
[5dfb1948]132
[c6aca755]133 di->pos = pos;
134 rc = fat_directory_block_load(di);
[5d95f02]135 if (rc != EOK)
[c6aca755]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
[da2f8d10]154int fat_directory_read(fat_directory_t *di, char *name, fat_dentry_t **de)
155{
156 fat_dentry_t *d = NULL;
[fcc3cd8]157 uint16_t wname[FAT_LFN_NAME_SIZE];
[298a6ce]158 size_t lfn_offset, lfn_size;
159 bool long_entry = false;
160 int long_entry_count = 0;
161 uint8_t checksum = 0;
[5d95f02]162 int rc;
[da2f8d10]163
[c6aca755]164 do {
[5d95f02]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);
[c6aca755]183 } else {
[5d95f02]184 /*
185 * Something wrong with order.
186 * Skip this long entries set.
187 */
188 long_entry_count = 0;
189 long_entry = false;
[da2f8d10]190 }
[5d95f02]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);
[c6aca755]203 }
[5d95f02]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)
[da2f8d10]212 fat_dentry_name_get(d, name);
[5d95f02]213 } else
214 fat_dentry_name_get(d, name);
[c6aca755]215
[5d95f02]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;
[da2f8d10]224 }
[c6aca755]225 } while (fat_directory_next(di) == EOK);
226
[da2f8d10]227 return ENOENT;
228}
229
[c6aca755]230int fat_directory_erase(fat_directory_t *di)
231{
232 int rc;
233 fat_dentry_t *d;
234 bool flag = false;
[298a6ce]235 uint8_t checksum;
[c6aca755]236
237 rc = fat_directory_get(di, &d);
238 if (rc != EOK)
239 return rc;
[298a6ce]240 checksum = fat_dentry_chksum(d->name);
[c6aca755]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 &&
[5d95f02]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
[c6aca755]255 break;
256 }
257
258 return EOK;
259}
260
[5dfb1948]261int fat_directory_write(fat_directory_t *di, const char *name, fat_dentry_t *de)
262{
263 int rc;
[5d95f02]264 bool enable_lfn = true; /* TODO: make this a mount option */
[298a6ce]265
[3e018e45]266 if (fat_valid_short_name(name)) {
[5d95f02]267 /*
268 * NAME could be directly stored in dentry without creating
269 * LFN.
270 */
[5dfb1948]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);
[3e018e45]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);
[5dfb1948]287 if (rc != EOK)
288 return rc;
[3e018e45]289
290 lfn_size = utf16_length(wname);
[298a6ce]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);
[5dfb1948]295 if (rc != EOK)
296 return rc;
297 aoff64_t start_pos = di->pos;
298
299 /* Write Short entry */
[3e018e45]300 rc = fat_directory_create_sfn(di, de, name);
[5dfb1948]301 if (rc != EOK)
302 return rc;
[298a6ce]303 checksum = fat_dentry_chksum(de->name);
[5dfb1948]304
[298a6ce]305 rc = fat_directory_seek(di, start_pos+long_entry_count);
[5dfb1948]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 */
[298a6ce]313 lfn_offset = 0;
[5dfb1948]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;
[5d95f02]323 fat_lfn_set_entry(wname, &lfn_offset, lfn_size + 1, d);
[298a6ce]324 FAT_LFN_CHKSUM(d) = checksum;
[5dfb1948]325 FAT_LFN_ORDER(d) = ++idx;
326 di->b->dirty = true;
[298a6ce]327 } while (lfn_offset < lfn_size);
[5dfb1948]328 FAT_LFN_ORDER(d) |= FAT_LFN_LAST;
329
[298a6ce]330 rc = fat_directory_seek(di, start_pos+long_entry_count);
[3e018e45]331 return rc;
[5dfb1948]332 }
[3e018e45]333
334 return ENOTSUP;
[5dfb1948]335}
336
[5d95f02]337int fat_directory_create_sfn(fat_directory_t *di, fat_dentry_t *de,
338 const char *lname)
[5dfb1948]339{
[5d95f02]340 char name[FAT_NAME_LEN + 1];
341 char ext[FAT_EXT_LEN + 1];
342 char number[FAT_NAME_LEN + 1];
[5dfb1948]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
[3e018e45]347 size_t name_len = str_size(lname);
348 char *pdot = str_rchr(lname, '.');
[5dfb1948]349 ext[FAT_EXT_LEN] = '\0';
350 if (pdot) {
351 pdot++;
[3e018e45]352 str_to_ascii(ext, pdot, FAT_EXT_LEN, FAT_SFN_CHAR);
353 name_len = (pdot - lname - 1);
[5dfb1948]354 }
355 if (name_len > FAT_NAME_LEN)
356 name_len = FAT_NAME_LEN;
[3e018e45]357 str_to_ascii(name, lname, name_len, FAT_SFN_CHAR);
[5dfb1948]358
[c065743]359 unsigned idx;
[5d95f02]360 for (idx = 1; idx <= FAT_MAX_SFN; idx++) {
[c065743]361 snprintf(number, sizeof(number), "%u", idx);
[5dfb1948]362
363 /* Fill de->name with FAT_PAD */
[5d95f02]364 memset(de->name, FAT_PAD, FAT_NAME_LEN + FAT_EXT_LEN);
[5dfb1948]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;
[5d95f02]372 if (str_size(name)+str_size(number) + 1 > FAT_NAME_LEN)
373 offset = FAT_NAME_LEN - str_size(number) - 1;
[5dfb1948]374 else
375 offset = str_size(name);
376 de->name[offset] = '~';
377 offset++;
[5d95f02]378 memcpy(de->name + offset, number, str_size(number));
[5dfb1948]379
380 if (!fat_directory_is_sfn_exist(di, de))
381 return EOK;
382 }
[5d95f02]383
[5dfb1948]384 return ERANGE;
385}
386
387int fat_directory_write_dentry(fat_directory_t *di, fat_dentry_t *de)
[2d0d637]388{
[5dfb1948]389 fat_dentry_t *d;
390 int rc;
391
392 rc = fat_directory_get(di, &d);
[5d95f02]393 if (rc != EOK)
[5dfb1948]394 return rc;
395 memcpy(d, de, sizeof(fat_dentry_t));
396 di->b->dirty = true;
[5d95f02]397
[2d0d637]398 return EOK;
399}
400
[5dfb1948]401int fat_directory_expand(fat_directory_t *di)
402{
403 int rc;
404 fat_cluster_t mcl, lcl;
[2d0d637]405
[5dfb1948]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 }
[5d95f02]410 rc = fat_alloc_clusters(di->bs, di->nodep->idx->service_id, 1, &mcl,
411 &lcl);
[5dfb1948]412 if (rc != EOK)
413 return rc;
[375ab5e]414 rc = fat_zero_cluster(di->bs, di->nodep->idx->service_id, mcl);
[5dfb1948]415 if (rc != EOK) {
[5d95f02]416 (void) fat_free_clusters(di->bs, di->nodep->idx->service_id,
417 mcl);
[5dfb1948]418 return rc;
419 }
420 rc = fat_append_clusters(di->bs, di->nodep, mcl, lcl);
421 if (rc != EOK) {
[5d95f02]422 (void) fat_free_clusters(di->bs, di->nodep->idx->service_id,
423 mcl);
[5dfb1948]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;
[5d95f02]438 int rc;
[5dfb1948]439
440 do {
441 found = 0;
[5d95f02]442 pos = 0;
[5dfb1948]443 fat_directory_seek(di, 0);
444 do {
[5d95f02]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;
[5dfb1948]458 }
[5d95f02]459 break;
460 case FAT_DENTRY_VALID:
461 case FAT_DENTRY_LFN:
462 case FAT_DENTRY_SKIP:
463 default:
464 found = 0;
465 break;
[5dfb1948]466 }
467 } while (fat_directory_next(di) == EOK);
468 } while (fat_directory_expand(di) == EOK);
[5d95f02]469
[5dfb1948]470 return ENOSPC;
471}
472
[5d95f02]473int fat_directory_lookup_name(fat_directory_t *di, const char *name,
474 fat_dentry_t **de)
[5dfb1948]475{
476 char entry[FAT_LFN_NAME_SIZE];
[5d95f02]477
[5dfb1948]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 }
[5d95f02]487
[5dfb1948]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;
[5d95f02]494 int rc;
495
[5dfb1948]496 fat_directory_seek(di, 0);
497 do {
[5d95f02]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;
[5dfb1948]515 }
516 } while (fat_directory_next(di) == EOK);
[5d95f02]517
[5dfb1948]518 return false;
519}
[da2f8d10]520
521/**
522 * @}
523 */
Note: See TracBrowser for help on using the repository browser.