source: mainline/uspace/srv/fs/fat/fat_directory.c@ 411e9ca

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

Using new function to convert LFN UTF16 data to UTF8 string
in fat_directory_read()

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