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