| 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 exfat
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 |
|
|---|
| 33 | /**
|
|---|
| 34 | * @file exfat_directory.c
|
|---|
| 35 | * @brief Functions that work with FAT directory.
|
|---|
| 36 | */
|
|---|
| 37 |
|
|---|
| 38 | #include "exfat.h"
|
|---|
| 39 | #include "exfat_directory.h"
|
|---|
| 40 | #include "exfat_fat.h"
|
|---|
| 41 | #include <block.h>
|
|---|
| 42 | #include <errno.h>
|
|---|
| 43 | #include <byteorder.h>
|
|---|
| 44 | #include <mem.h>
|
|---|
| 45 | #include <stdlib.h>
|
|---|
| 46 | #include <str.h>
|
|---|
| 47 | #include <align.h>
|
|---|
| 48 |
|
|---|
| 49 | void exfat_directory_init(exfat_directory_t *di)
|
|---|
| 50 | {
|
|---|
| 51 | di->b = NULL;
|
|---|
| 52 | di->nodep = NULL;
|
|---|
| 53 | di->bs = NULL;
|
|---|
| 54 | di->blocks = 0;
|
|---|
| 55 | di->pos = 0;
|
|---|
| 56 | di->bnum = 0;
|
|---|
| 57 | di->last = false;
|
|---|
| 58 | di->fragmented = false;
|
|---|
| 59 | di->firstc = 0;
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | errno_t exfat_directory_open(exfat_node_t *nodep, exfat_directory_t *di)
|
|---|
| 63 | {
|
|---|
| 64 | exfat_directory_init(di);
|
|---|
| 65 | di->nodep = nodep;
|
|---|
| 66 | if (di->nodep->type != EXFAT_DIRECTORY)
|
|---|
| 67 | return EINVAL;
|
|---|
| 68 | di->service_id = nodep->idx->service_id;
|
|---|
| 69 | di->fragmented = nodep->fragmented;
|
|---|
| 70 | di->firstc = nodep->firstc;
|
|---|
| 71 |
|
|---|
| 72 | di->bs = block_bb_get(di->service_id);
|
|---|
| 73 | di->blocks = ROUND_UP(nodep->size, BPS(di->bs)) / BPS(di->bs);
|
|---|
| 74 | return EOK;
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | errno_t exfat_directory_open_parent(exfat_directory_t *di,
|
|---|
| 78 | service_id_t service_id, exfat_cluster_t firstc, bool fragmented)
|
|---|
| 79 | {
|
|---|
| 80 | exfat_directory_init(di);
|
|---|
| 81 | di->service_id = service_id;
|
|---|
| 82 | di->fragmented = fragmented;
|
|---|
| 83 | di->firstc = firstc;
|
|---|
| 84 | di->bs = block_bb_get(service_id);
|
|---|
| 85 | di->blocks = 0;
|
|---|
| 86 | return EOK;
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | errno_t exfat_directory_close(exfat_directory_t *di)
|
|---|
| 90 | {
|
|---|
| 91 | errno_t rc = EOK;
|
|---|
| 92 |
|
|---|
| 93 | if (di->b) {
|
|---|
| 94 | rc = block_put(di->b);
|
|---|
| 95 | di->b = NULL;
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | return rc;
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | static errno_t exfat_directory_block_load(exfat_directory_t *di)
|
|---|
| 102 | {
|
|---|
| 103 | uint32_t i;
|
|---|
| 104 | errno_t rc = EOK;
|
|---|
| 105 |
|
|---|
| 106 | i = (di->pos * sizeof(exfat_dentry_t)) / BPS(di->bs);
|
|---|
| 107 | if (di->nodep && (i >= di->blocks))
|
|---|
| 108 | return ENOENT;
|
|---|
| 109 |
|
|---|
| 110 | if (di->b && di->bnum != i) {
|
|---|
| 111 | rc = block_put(di->b);
|
|---|
| 112 | di->b = NULL;
|
|---|
| 113 | if (rc != EOK)
|
|---|
| 114 | return rc;
|
|---|
| 115 | }
|
|---|
| 116 | if (!di->b) {
|
|---|
| 117 | if (di->nodep) {
|
|---|
| 118 | rc = exfat_block_get(&di->b, di->bs, di->nodep, i,
|
|---|
| 119 | BLOCK_FLAGS_NONE);
|
|---|
| 120 | } else {
|
|---|
| 121 | rc = exfat_block_get_by_clst(&di->b, di->bs,
|
|---|
| 122 | di->service_id, di->fragmented, di->firstc, NULL, i,
|
|---|
| 123 | BLOCK_FLAGS_NONE);
|
|---|
| 124 | }
|
|---|
| 125 | if (rc != EOK) {
|
|---|
| 126 | di->b = NULL;
|
|---|
| 127 | return rc;
|
|---|
| 128 | }
|
|---|
| 129 | di->bnum = i;
|
|---|
| 130 | }
|
|---|
| 131 | return rc;
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | errno_t exfat_directory_next(exfat_directory_t *di)
|
|---|
| 135 | {
|
|---|
| 136 | errno_t rc;
|
|---|
| 137 |
|
|---|
| 138 | di->pos += 1;
|
|---|
| 139 | rc = exfat_directory_block_load(di);
|
|---|
| 140 | if (rc != EOK)
|
|---|
| 141 | di->pos -= 1;
|
|---|
| 142 |
|
|---|
| 143 | return rc;
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| 146 | errno_t exfat_directory_prev(exfat_directory_t *di)
|
|---|
| 147 | {
|
|---|
| 148 | errno_t rc = EOK;
|
|---|
| 149 |
|
|---|
| 150 | if (di->pos > 0) {
|
|---|
| 151 | di->pos -= 1;
|
|---|
| 152 | rc = exfat_directory_block_load(di);
|
|---|
| 153 | } else
|
|---|
| 154 | return ENOENT;
|
|---|
| 155 |
|
|---|
| 156 | if (rc != EOK)
|
|---|
| 157 | di->pos += 1;
|
|---|
| 158 |
|
|---|
| 159 | return rc;
|
|---|
| 160 | }
|
|---|
| 161 |
|
|---|
| 162 | errno_t exfat_directory_seek(exfat_directory_t *di, aoff64_t pos)
|
|---|
| 163 | {
|
|---|
| 164 | aoff64_t _pos = di->pos;
|
|---|
| 165 | errno_t rc;
|
|---|
| 166 |
|
|---|
| 167 | di->pos = pos;
|
|---|
| 168 | rc = exfat_directory_block_load(di);
|
|---|
| 169 | if (rc != EOK)
|
|---|
| 170 | di->pos = _pos;
|
|---|
| 171 |
|
|---|
| 172 | return rc;
|
|---|
| 173 | }
|
|---|
| 174 |
|
|---|
| 175 | errno_t exfat_directory_get(exfat_directory_t *di, exfat_dentry_t **d)
|
|---|
| 176 | {
|
|---|
| 177 | errno_t rc;
|
|---|
| 178 |
|
|---|
| 179 | rc = exfat_directory_block_load(di);
|
|---|
| 180 | if (rc == EOK) {
|
|---|
| 181 | aoff64_t o = di->pos % (BPS(di->bs) / sizeof(exfat_dentry_t));
|
|---|
| 182 | *d = ((exfat_dentry_t *)di->b->data) + o;
|
|---|
| 183 | }
|
|---|
| 184 |
|
|---|
| 185 | return rc;
|
|---|
| 186 | }
|
|---|
| 187 |
|
|---|
| 188 | errno_t exfat_directory_find(exfat_directory_t *di, exfat_dentry_clsf_t type,
|
|---|
| 189 | exfat_dentry_t **d)
|
|---|
| 190 | {
|
|---|
| 191 | do {
|
|---|
| 192 | if (exfat_directory_get(di, d) == EOK) {
|
|---|
| 193 | if (exfat_classify_dentry(*d) == type)
|
|---|
| 194 | return EOK;
|
|---|
| 195 | } else
|
|---|
| 196 | return ENOENT;
|
|---|
| 197 | } while (exfat_directory_next(di) == EOK);
|
|---|
| 198 |
|
|---|
| 199 | return ENOENT;
|
|---|
| 200 | }
|
|---|
| 201 |
|
|---|
| 202 | errno_t
|
|---|
| 203 | exfat_directory_find_continue(exfat_directory_t *di, exfat_dentry_clsf_t type,
|
|---|
| 204 | exfat_dentry_t **d)
|
|---|
| 205 | {
|
|---|
| 206 | errno_t rc;
|
|---|
| 207 | rc = exfat_directory_next(di);
|
|---|
| 208 | if (rc != EOK)
|
|---|
| 209 | return rc;
|
|---|
| 210 | return exfat_directory_find(di, type, d);
|
|---|
| 211 | }
|
|---|
| 212 |
|
|---|
| 213 | errno_t exfat_directory_read_file(exfat_directory_t *di, char *name, size_t size,
|
|---|
| 214 | exfat_file_dentry_t *df, exfat_stream_dentry_t *ds)
|
|---|
| 215 | {
|
|---|
| 216 | uint16_t wname[EXFAT_FILENAME_LEN + 1];
|
|---|
| 217 | exfat_dentry_t *d = NULL;
|
|---|
| 218 | errno_t rc;
|
|---|
| 219 | int i;
|
|---|
| 220 | size_t offset = 0;
|
|---|
| 221 | aoff64_t start_pos = 0;
|
|---|
| 222 |
|
|---|
| 223 | rc = exfat_directory_find(di, EXFAT_DENTRY_FILE, &d);
|
|---|
| 224 | if (rc != EOK)
|
|---|
| 225 | return rc;
|
|---|
| 226 | start_pos = di->pos;
|
|---|
| 227 | *df = d->file;
|
|---|
| 228 |
|
|---|
| 229 | rc = exfat_directory_next(di);
|
|---|
| 230 | if (rc != EOK)
|
|---|
| 231 | return rc;
|
|---|
| 232 | rc = exfat_directory_get(di, &d);
|
|---|
| 233 | if (rc != EOK)
|
|---|
| 234 | return rc;
|
|---|
| 235 | if (exfat_classify_dentry(d) != EXFAT_DENTRY_STREAM)
|
|---|
| 236 | return ENOENT;
|
|---|
| 237 | *ds = d->stream;
|
|---|
| 238 |
|
|---|
| 239 | if (ds->name_size > size)
|
|---|
| 240 | return EOVERFLOW;
|
|---|
| 241 |
|
|---|
| 242 | for (i = 0; i < df->count - 1; i++) {
|
|---|
| 243 | rc = exfat_directory_next(di);
|
|---|
| 244 | if (rc != EOK)
|
|---|
| 245 | return rc;
|
|---|
| 246 | rc = exfat_directory_get(di, &d);
|
|---|
| 247 | if (rc != EOK)
|
|---|
| 248 | return rc;
|
|---|
| 249 | if (exfat_classify_dentry(d) != EXFAT_DENTRY_NAME)
|
|---|
| 250 | return ENOENT;
|
|---|
| 251 | exfat_dentry_get_name(&d->name, ds->name_size, wname, &offset);
|
|---|
| 252 | }
|
|---|
| 253 | rc = utf16_to_str(name, size, wname);
|
|---|
| 254 | if (rc != EOK)
|
|---|
| 255 | return rc;
|
|---|
| 256 |
|
|---|
| 257 | exfat_directory_seek(di, start_pos);
|
|---|
| 258 | return EOK;
|
|---|
| 259 | }
|
|---|
| 260 |
|
|---|
| 261 | errno_t exfat_directory_read_vollabel(exfat_directory_t *di, char *label,
|
|---|
| 262 | size_t size)
|
|---|
| 263 | {
|
|---|
| 264 | uint16_t wlabel[EXFAT_VOLLABEL_LEN + 1];
|
|---|
| 265 | exfat_dentry_t *d = NULL;
|
|---|
| 266 | errno_t rc;
|
|---|
| 267 | aoff64_t start_pos = 0;
|
|---|
| 268 |
|
|---|
| 269 | start_pos = di->pos;
|
|---|
| 270 |
|
|---|
| 271 | rc = exfat_directory_seek(di, 0);
|
|---|
| 272 | if (rc != EOK)
|
|---|
| 273 | return rc;
|
|---|
| 274 |
|
|---|
| 275 | rc = exfat_directory_find(di, EXFAT_DENTRY_VOLLABEL, &d);
|
|---|
| 276 | if (rc != EOK)
|
|---|
| 277 | return rc;
|
|---|
| 278 |
|
|---|
| 279 | exfat_dentry_get_vollabel(&d->vollabel, EXFAT_VOLLABEL_LEN, wlabel);
|
|---|
| 280 |
|
|---|
| 281 | rc = utf16_to_str(label, size, wlabel);
|
|---|
| 282 | if (rc != EOK)
|
|---|
| 283 | return rc;
|
|---|
| 284 |
|
|---|
| 285 | exfat_directory_seek(di, start_pos);
|
|---|
| 286 | return EOK;
|
|---|
| 287 | }
|
|---|
| 288 |
|
|---|
| 289 | static uint16_t exfat_directory_set_checksum(const uint8_t *bytes, size_t count)
|
|---|
| 290 | {
|
|---|
| 291 | uint16_t checksum = 0;
|
|---|
| 292 | size_t idx;
|
|---|
| 293 |
|
|---|
| 294 | for (idx = 0; idx < count; idx++) {
|
|---|
| 295 | if (idx == 2 || idx == 3)
|
|---|
| 296 | continue;
|
|---|
| 297 | checksum = ((checksum << 15) | (checksum >> 1)) +
|
|---|
| 298 | (uint16_t)bytes[idx];
|
|---|
| 299 | }
|
|---|
| 300 | return checksum;
|
|---|
| 301 | }
|
|---|
| 302 |
|
|---|
| 303 | errno_t exfat_directory_sync_file(exfat_directory_t *di, exfat_file_dentry_t *df,
|
|---|
| 304 | exfat_stream_dentry_t *ds)
|
|---|
| 305 | {
|
|---|
| 306 | errno_t rc;
|
|---|
| 307 | int i, count;
|
|---|
| 308 | exfat_dentry_t *array = NULL, *de;
|
|---|
| 309 | aoff64_t pos = di->pos;
|
|---|
| 310 |
|
|---|
| 311 | rc = exfat_directory_get(di, &de);
|
|---|
| 312 | if (rc != EOK)
|
|---|
| 313 | return rc;
|
|---|
| 314 | count = de->file.count + 1;
|
|---|
| 315 | array = (exfat_dentry_t *) malloc(count * sizeof(exfat_dentry_t));
|
|---|
| 316 | if (!array)
|
|---|
| 317 | return ENOMEM;
|
|---|
| 318 | for (i = 0; i < count; i++) {
|
|---|
| 319 | rc = exfat_directory_get(di, &de);
|
|---|
| 320 | if (rc != EOK) {
|
|---|
| 321 | free(array);
|
|---|
| 322 | return rc;
|
|---|
| 323 | }
|
|---|
| 324 | array[i] = *de;
|
|---|
| 325 | rc = exfat_directory_next(di);
|
|---|
| 326 | if (rc != EOK) {
|
|---|
| 327 | free(array);
|
|---|
| 328 | return rc;
|
|---|
| 329 | }
|
|---|
| 330 | }
|
|---|
| 331 | rc = exfat_directory_seek(di, pos);
|
|---|
| 332 | if (rc != EOK) {
|
|---|
| 333 | free(array);
|
|---|
| 334 | return rc;
|
|---|
| 335 | }
|
|---|
| 336 |
|
|---|
| 337 | /* Sync */
|
|---|
| 338 | array[0].file.attr = host2uint16_t_le(df->attr);
|
|---|
| 339 | array[1].stream.firstc = host2uint32_t_le(ds->firstc);
|
|---|
| 340 | array[1].stream.flags = ds->flags;
|
|---|
| 341 | array[1].stream.valid_data_size = host2uint64_t_le(ds->valid_data_size);
|
|---|
| 342 | array[1].stream.data_size = host2uint64_t_le(ds->data_size);
|
|---|
| 343 | array[0].file.checksum =
|
|---|
| 344 | host2uint16_t_le(exfat_directory_set_checksum((uint8_t *)array,
|
|---|
| 345 | count * sizeof(exfat_dentry_t)));
|
|---|
| 346 |
|
|---|
| 347 | /* Store */
|
|---|
| 348 | for (i = 0; i < count; i++) {
|
|---|
| 349 | rc = exfat_directory_get(di, &de);
|
|---|
| 350 | if (rc != EOK) {
|
|---|
| 351 | free(array);
|
|---|
| 352 | return rc;
|
|---|
| 353 | }
|
|---|
| 354 | *de = array[i];
|
|---|
| 355 | di->b->dirty = true;
|
|---|
| 356 | rc = exfat_directory_next(di);
|
|---|
| 357 | if (rc != EOK) {
|
|---|
| 358 | free(array);
|
|---|
| 359 | return rc;
|
|---|
| 360 | }
|
|---|
| 361 | }
|
|---|
| 362 | free(array);
|
|---|
| 363 |
|
|---|
| 364 | return EOK;
|
|---|
| 365 | }
|
|---|
| 366 |
|
|---|
| 367 | errno_t exfat_directory_write_file(exfat_directory_t *di, const char *name)
|
|---|
| 368 | {
|
|---|
| 369 | fs_node_t *fn;
|
|---|
| 370 | exfat_node_t *uctablep;
|
|---|
| 371 | uint16_t *uctable;
|
|---|
| 372 | exfat_dentry_t df, ds, *de;
|
|---|
| 373 | uint16_t wname[EXFAT_FILENAME_LEN + 1];
|
|---|
| 374 | errno_t rc;
|
|---|
| 375 | int i;
|
|---|
| 376 | size_t uctable_chars, j;
|
|---|
| 377 | aoff64_t pos;
|
|---|
| 378 |
|
|---|
| 379 | rc = str_to_utf16(wname, EXFAT_FILENAME_LEN, name);
|
|---|
| 380 | if (rc != EOK)
|
|---|
| 381 | return rc;
|
|---|
| 382 | rc = exfat_uctable_get(&fn, di->service_id);
|
|---|
| 383 | if (rc != EOK)
|
|---|
| 384 | return rc;
|
|---|
| 385 | uctablep = EXFAT_NODE(fn);
|
|---|
| 386 |
|
|---|
| 387 | uctable_chars = ALIGN_DOWN(uctablep->size,
|
|---|
| 388 | sizeof(uint16_t)) / sizeof(uint16_t);
|
|---|
| 389 | uctable = (uint16_t *) malloc(uctable_chars * sizeof(uint16_t));
|
|---|
| 390 | rc = exfat_read_uctable(di->bs, uctablep, (uint8_t *)uctable);
|
|---|
| 391 | if (rc != EOK) {
|
|---|
| 392 | (void) exfat_node_put(fn);
|
|---|
| 393 | free(uctable);
|
|---|
| 394 | return rc;
|
|---|
| 395 | }
|
|---|
| 396 |
|
|---|
| 397 | /* Fill stream entry */
|
|---|
| 398 | ds.type = EXFAT_TYPE_STREAM;
|
|---|
| 399 | ds.stream.flags = 0;
|
|---|
| 400 | ds.stream.valid_data_size = 0;
|
|---|
| 401 | ds.stream.data_size = 0;
|
|---|
| 402 | ds.stream.name_size = utf16_wsize(wname);
|
|---|
| 403 | ds.stream.hash = host2uint16_t_le(exfat_name_hash(wname, uctable,
|
|---|
| 404 | uctable_chars));
|
|---|
| 405 |
|
|---|
| 406 | /* Fill file entry */
|
|---|
| 407 | df.type = EXFAT_TYPE_FILE;
|
|---|
| 408 | df.file.attr = 0;
|
|---|
| 409 | df.file.count = ROUND_UP(ds.stream.name_size, EXFAT_NAME_PART_LEN) /
|
|---|
| 410 | EXFAT_NAME_PART_LEN + 1;
|
|---|
| 411 | df.file.checksum = 0;
|
|---|
| 412 |
|
|---|
| 413 | free(uctable);
|
|---|
| 414 | rc = exfat_node_put(fn);
|
|---|
| 415 | if (rc != EOK)
|
|---|
| 416 | return rc;
|
|---|
| 417 |
|
|---|
| 418 | /* Looking for set of free entries */
|
|---|
| 419 | rc = exfat_directory_lookup_free(di, df.file.count + 1);
|
|---|
| 420 | if (rc != EOK)
|
|---|
| 421 | return rc;
|
|---|
| 422 | pos = di->pos;
|
|---|
| 423 |
|
|---|
| 424 | /* Write file entry */
|
|---|
| 425 | rc = exfat_directory_get(di, &de);
|
|---|
| 426 | if (rc != EOK)
|
|---|
| 427 | return rc;
|
|---|
| 428 | *de = df;
|
|---|
| 429 | di->b->dirty = true;
|
|---|
| 430 | rc = exfat_directory_next(di);
|
|---|
| 431 | if (rc != EOK)
|
|---|
| 432 | return rc;
|
|---|
| 433 |
|
|---|
| 434 | /* Write stream entry */
|
|---|
| 435 | rc = exfat_directory_get(di, &de);
|
|---|
| 436 | if (rc != EOK)
|
|---|
| 437 | return rc;
|
|---|
| 438 | *de = ds;
|
|---|
| 439 | di->b->dirty = true;
|
|---|
| 440 |
|
|---|
| 441 | /* Write file name */
|
|---|
| 442 | size_t chars = EXFAT_NAME_PART_LEN;
|
|---|
| 443 | uint16_t *sname = wname;
|
|---|
| 444 |
|
|---|
| 445 | for (i = 0; i < ds.stream.name_size; i++)
|
|---|
| 446 | wname[i] = host2uint16_t_le(wname[i]);
|
|---|
| 447 |
|
|---|
| 448 | for (i = 0; i < df.file.count - 1; i++) {
|
|---|
| 449 | rc = exfat_directory_next(di);
|
|---|
| 450 | if (rc != EOK)
|
|---|
| 451 | return rc;
|
|---|
| 452 |
|
|---|
| 453 | if (i == df.file.count - 2) {
|
|---|
| 454 | chars = ds.stream.name_size -
|
|---|
| 455 | EXFAT_NAME_PART_LEN * (df.file.count - 2);
|
|---|
| 456 | }
|
|---|
| 457 |
|
|---|
| 458 | rc = exfat_directory_get(di, &de);
|
|---|
| 459 | if (rc != EOK)
|
|---|
| 460 | return rc;
|
|---|
| 461 | de->type = EXFAT_TYPE_NAME;
|
|---|
| 462 | /* test */
|
|---|
| 463 | for (j = 0; j < chars; j++) {
|
|---|
| 464 | de->name.name[j] = *sname;
|
|---|
| 465 | sname++;
|
|---|
| 466 | }
|
|---|
| 467 |
|
|---|
| 468 | di->b->dirty = true;
|
|---|
| 469 | }
|
|---|
| 470 |
|
|---|
| 471 | return exfat_directory_seek(di, pos);
|
|---|
| 472 | }
|
|---|
| 473 |
|
|---|
| 474 | errno_t exfat_directory_erase_file(exfat_directory_t *di, aoff64_t pos)
|
|---|
| 475 | {
|
|---|
| 476 | errno_t rc;
|
|---|
| 477 | int count;
|
|---|
| 478 | exfat_dentry_t *de;
|
|---|
| 479 |
|
|---|
| 480 | di->pos = pos;
|
|---|
| 481 |
|
|---|
| 482 | rc = exfat_directory_get(di, &de);
|
|---|
| 483 | if (rc != EOK)
|
|---|
| 484 | return rc;
|
|---|
| 485 | count = de->file.count + 1;
|
|---|
| 486 |
|
|---|
| 487 | while (count) {
|
|---|
| 488 | rc = exfat_directory_get(di, &de);
|
|---|
| 489 | if (rc != EOK)
|
|---|
| 490 | return rc;
|
|---|
| 491 | de->type &= ~EXFAT_TYPE_USED;
|
|---|
| 492 | di->b->dirty = true;
|
|---|
| 493 |
|
|---|
| 494 | rc = exfat_directory_next(di);
|
|---|
| 495 | if (rc != EOK)
|
|---|
| 496 | return rc;
|
|---|
| 497 | count--;
|
|---|
| 498 | }
|
|---|
| 499 | return EOK;
|
|---|
| 500 | }
|
|---|
| 501 |
|
|---|
| 502 | errno_t exfat_directory_expand(exfat_directory_t *di)
|
|---|
| 503 | {
|
|---|
| 504 | errno_t rc;
|
|---|
| 505 |
|
|---|
| 506 | if (!di->nodep)
|
|---|
| 507 | return ENOSPC;
|
|---|
| 508 |
|
|---|
| 509 | rc = exfat_node_expand(di->nodep->idx->service_id, di->nodep, 1);
|
|---|
| 510 | if (rc != EOK)
|
|---|
| 511 | return rc;
|
|---|
| 512 |
|
|---|
| 513 | di->fragmented = di->nodep->fragmented;
|
|---|
| 514 | di->nodep->size += BPC(di->bs);
|
|---|
| 515 | di->nodep->dirty = true; /* need to sync node */
|
|---|
| 516 | di->blocks = di->nodep->size / BPS(di->bs);
|
|---|
| 517 |
|
|---|
| 518 | return EOK;
|
|---|
| 519 | }
|
|---|
| 520 |
|
|---|
| 521 | errno_t exfat_directory_lookup_free(exfat_directory_t *di, size_t count)
|
|---|
| 522 | {
|
|---|
| 523 | errno_t rc;
|
|---|
| 524 | exfat_dentry_t *d;
|
|---|
| 525 | size_t found;
|
|---|
| 526 | aoff64_t pos;
|
|---|
| 527 |
|
|---|
| 528 | rc = exfat_directory_seek(di, 0);
|
|---|
| 529 | if (rc != EOK)
|
|---|
| 530 | return rc;
|
|---|
| 531 |
|
|---|
| 532 | do {
|
|---|
| 533 | found = 0;
|
|---|
| 534 | pos = 0;
|
|---|
| 535 | do {
|
|---|
| 536 | if (exfat_directory_get(di, &d) == EOK) {
|
|---|
| 537 | switch (exfat_classify_dentry(d)) {
|
|---|
| 538 | case EXFAT_DENTRY_LAST:
|
|---|
| 539 | case EXFAT_DENTRY_FREE:
|
|---|
| 540 | if (found == 0)
|
|---|
| 541 | pos = di->pos;
|
|---|
| 542 | found++;
|
|---|
| 543 | if (found == count) {
|
|---|
| 544 | exfat_directory_seek(di, pos);
|
|---|
| 545 | return EOK;
|
|---|
| 546 | }
|
|---|
| 547 | break;
|
|---|
| 548 | default:
|
|---|
| 549 | found = 0;
|
|---|
| 550 | break;
|
|---|
| 551 | }
|
|---|
| 552 | }
|
|---|
| 553 | } while (exfat_directory_next(di) == EOK);
|
|---|
| 554 | } while (exfat_directory_expand(di) == EOK);
|
|---|
| 555 | return ENOSPC;
|
|---|
| 556 | }
|
|---|
| 557 |
|
|---|
| 558 | /**
|
|---|
| 559 | * @}
|
|---|
| 560 | */
|
|---|