source: mainline/uspace/srv/fs/fat/fat_directory.c@ 612af1a0

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

Make FAT use instance data.

  • Property mode set to 100644
File size: 11.7 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;
[32f623d9]264 void *data;
265 fat_instance_t *instance;
266
267 rc = fs_instance_get(di->nodep->idx->service_id, &data);
268 assert(rc == EOK);
269 instance = (fat_instance_t *) data;
[298a6ce]270
[3e018e45]271 if (fat_valid_short_name(name)) {
[5d95f02]272 /*
273 * NAME could be directly stored in dentry without creating
274 * LFN.
275 */
[5dfb1948]276 fat_dentry_name_set(de, name);
277 if (fat_directory_is_sfn_exist(di, de))
278 return EEXIST;
279 rc = fat_directory_lookup_free(di, 1);
280 if (rc != EOK)
281 return rc;
282 rc = fat_directory_write_dentry(di, de);
[3e018e45]283 return rc;
[32f623d9]284 } else if (instance->lfn_enabled && fat_valid_name(name)) {
[3e018e45]285 /* We should create long entries to store name */
286 int long_entry_count;
287 uint8_t checksum;
288 uint16_t wname[FAT_LFN_NAME_SIZE];
289 size_t lfn_size, lfn_offset;
290
291 rc = str_to_utf16(wname, FAT_LFN_NAME_SIZE, name);
[5dfb1948]292 if (rc != EOK)
293 return rc;
[3e018e45]294
295 lfn_size = utf16_length(wname);
[298a6ce]296 long_entry_count = lfn_size / FAT_LFN_ENTRY_SIZE;
297 if (lfn_size % FAT_LFN_ENTRY_SIZE)
298 long_entry_count++;
[32f623d9]299 rc = fat_directory_lookup_free(di, long_entry_count + 1);
[5dfb1948]300 if (rc != EOK)
301 return rc;
302 aoff64_t start_pos = di->pos;
303
304 /* Write Short entry */
[3e018e45]305 rc = fat_directory_create_sfn(di, de, name);
[5dfb1948]306 if (rc != EOK)
307 return rc;
[298a6ce]308 checksum = fat_dentry_chksum(de->name);
[5dfb1948]309
[298a6ce]310 rc = fat_directory_seek(di, start_pos+long_entry_count);
[5dfb1948]311 if (rc != EOK)
312 return rc;
313 rc = fat_directory_write_dentry(di, de);
314 if (rc != EOK)
315 return rc;
316
317 /* Write Long entry by parts */
[298a6ce]318 lfn_offset = 0;
[5dfb1948]319 fat_dentry_t *d;
320 size_t idx = 0;
321 do {
322 rc = fat_directory_prev(di);
323 if (rc != EOK)
324 return rc;
325 rc = fat_directory_get(di, &d);
326 if (rc != EOK)
327 return rc;
[5d95f02]328 fat_lfn_set_entry(wname, &lfn_offset, lfn_size + 1, d);
[298a6ce]329 FAT_LFN_CHKSUM(d) = checksum;
[5dfb1948]330 FAT_LFN_ORDER(d) = ++idx;
331 di->b->dirty = true;
[298a6ce]332 } while (lfn_offset < lfn_size);
[5dfb1948]333 FAT_LFN_ORDER(d) |= FAT_LFN_LAST;
334
[32f623d9]335 rc = fat_directory_seek(di, start_pos + long_entry_count);
[3e018e45]336 return rc;
[5dfb1948]337 }
[3e018e45]338
339 return ENOTSUP;
[5dfb1948]340}
341
[5d95f02]342int fat_directory_create_sfn(fat_directory_t *di, fat_dentry_t *de,
343 const char *lname)
[5dfb1948]344{
[5d95f02]345 char name[FAT_NAME_LEN + 1];
346 char ext[FAT_EXT_LEN + 1];
347 char number[FAT_NAME_LEN + 1];
[5dfb1948]348 memset(name, FAT_PAD, FAT_NAME_LEN);
349 memset(ext, FAT_PAD, FAT_EXT_LEN);
350 memset(number, FAT_PAD, FAT_NAME_LEN);
351
[3e018e45]352 size_t name_len = str_size(lname);
353 char *pdot = str_rchr(lname, '.');
[5dfb1948]354 ext[FAT_EXT_LEN] = '\0';
355 if (pdot) {
356 pdot++;
[3e018e45]357 str_to_ascii(ext, pdot, FAT_EXT_LEN, FAT_SFN_CHAR);
358 name_len = (pdot - lname - 1);
[5dfb1948]359 }
360 if (name_len > FAT_NAME_LEN)
361 name_len = FAT_NAME_LEN;
[3e018e45]362 str_to_ascii(name, lname, name_len, FAT_SFN_CHAR);
[5dfb1948]363
[c065743]364 unsigned idx;
[5d95f02]365 for (idx = 1; idx <= FAT_MAX_SFN; idx++) {
[c065743]366 snprintf(number, sizeof(number), "%u", idx);
[5dfb1948]367
368 /* Fill de->name with FAT_PAD */
[5d95f02]369 memset(de->name, FAT_PAD, FAT_NAME_LEN + FAT_EXT_LEN);
[5dfb1948]370 /* Copy ext */
371 memcpy(de->ext, ext, str_size(ext));
372 /* Copy name */
373 memcpy(de->name, name, str_size(name));
374
375 /* Copy number */
376 size_t offset;
[5d95f02]377 if (str_size(name)+str_size(number) + 1 > FAT_NAME_LEN)
378 offset = FAT_NAME_LEN - str_size(number) - 1;
[5dfb1948]379 else
380 offset = str_size(name);
381 de->name[offset] = '~';
382 offset++;
[5d95f02]383 memcpy(de->name + offset, number, str_size(number));
[5dfb1948]384
385 if (!fat_directory_is_sfn_exist(di, de))
386 return EOK;
387 }
[5d95f02]388
[5dfb1948]389 return ERANGE;
390}
391
392int fat_directory_write_dentry(fat_directory_t *di, fat_dentry_t *de)
[2d0d637]393{
[5dfb1948]394 fat_dentry_t *d;
395 int rc;
396
397 rc = fat_directory_get(di, &d);
[5d95f02]398 if (rc != EOK)
[5dfb1948]399 return rc;
400 memcpy(d, de, sizeof(fat_dentry_t));
401 di->b->dirty = true;
[5d95f02]402
[2d0d637]403 return EOK;
404}
405
[5dfb1948]406int fat_directory_expand(fat_directory_t *di)
407{
408 int rc;
409 fat_cluster_t mcl, lcl;
[2d0d637]410
[5dfb1948]411 if (!FAT_IS_FAT32(di->bs) && di->nodep->firstc == FAT_CLST_ROOT) {
412 /* Can't grow the root directory on FAT12/16. */
413 return ENOSPC;
414 }
[5d95f02]415 rc = fat_alloc_clusters(di->bs, di->nodep->idx->service_id, 1, &mcl,
416 &lcl);
[5dfb1948]417 if (rc != EOK)
418 return rc;
[375ab5e]419 rc = fat_zero_cluster(di->bs, di->nodep->idx->service_id, mcl);
[5dfb1948]420 if (rc != EOK) {
[5d95f02]421 (void) fat_free_clusters(di->bs, di->nodep->idx->service_id,
422 mcl);
[5dfb1948]423 return rc;
424 }
425 rc = fat_append_clusters(di->bs, di->nodep, mcl, lcl);
426 if (rc != EOK) {
[5d95f02]427 (void) fat_free_clusters(di->bs, di->nodep->idx->service_id,
428 mcl);
[5dfb1948]429 return rc;
430 }
431 di->nodep->size += BPS(di->bs) * SPC(di->bs);
432 di->nodep->dirty = true; /* need to sync node */
433 di->blocks = di->nodep->size / BPS(di->bs);
434
435 return EOK;
436}
437
438int fat_directory_lookup_free(fat_directory_t *di, size_t count)
439{
440 fat_dentry_t *d;
441 size_t found;
442 aoff64_t pos;
[5d95f02]443 int rc;
[5dfb1948]444
445 do {
446 found = 0;
[5d95f02]447 pos = 0;
[5dfb1948]448 fat_directory_seek(di, 0);
449 do {
[5d95f02]450 rc = fat_directory_get(di, &d);
451 if (rc != EOK)
452 return rc;
453
454 switch (fat_classify_dentry(d)) {
455 case FAT_DENTRY_LAST:
456 case FAT_DENTRY_FREE:
457 if (found == 0)
458 pos = di->pos;
459 found++;
460 if (found == count) {
461 fat_directory_seek(di, pos);
462 return EOK;
[5dfb1948]463 }
[5d95f02]464 break;
465 case FAT_DENTRY_VALID:
466 case FAT_DENTRY_LFN:
467 case FAT_DENTRY_SKIP:
468 default:
469 found = 0;
470 break;
[5dfb1948]471 }
472 } while (fat_directory_next(di) == EOK);
473 } while (fat_directory_expand(di) == EOK);
[5d95f02]474
[5dfb1948]475 return ENOSPC;
476}
477
[5d95f02]478int fat_directory_lookup_name(fat_directory_t *di, const char *name,
479 fat_dentry_t **de)
[5dfb1948]480{
481 char entry[FAT_LFN_NAME_SIZE];
[5d95f02]482
[5dfb1948]483 fat_directory_seek(di, 0);
484 while (fat_directory_read(di, entry, de) == EOK) {
485 if (fat_dentry_namecmp(entry, name) == 0) {
486 return EOK;
487 } else {
488 if (fat_directory_next(di) != EOK)
489 break;
490 }
491 }
[5d95f02]492
[5dfb1948]493 return ENOENT;
494}
495
496bool fat_directory_is_sfn_exist(fat_directory_t *di, fat_dentry_t *de)
497{
498 fat_dentry_t *d;
[5d95f02]499 int rc;
500
[5dfb1948]501 fat_directory_seek(di, 0);
502 do {
[5d95f02]503 rc = fat_directory_get(di, &d);
504 if (rc != EOK)
505 return false;
506
507 switch (fat_classify_dentry(d)) {
508 case FAT_DENTRY_LAST:
509 return false;
510 case FAT_DENTRY_VALID:
511 if (bcmp(de->name, d->name,
512 FAT_NAME_LEN + FAT_EXT_LEN)==0)
513 return true;
514 break;
515 default:
516 case FAT_DENTRY_LFN:
517 case FAT_DENTRY_SKIP:
518 case FAT_DENTRY_FREE:
519 break;
[5dfb1948]520 }
521 } while (fat_directory_next(di) == EOK);
[5d95f02]522
[5dfb1948]523 return false;
524}
[da2f8d10]525
526/**
527 * @}
528 */
Note: See TracBrowser for help on using the repository browser.