[4dd9395] | 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 |
|
---|
[b1834a01] | 29 | /** @addtogroup exfat
|
---|
[4dd9395] | 30 | * @{
|
---|
[1b20da0] | 31 | */
|
---|
[4dd9395] | 32 |
|
---|
| 33 | /**
|
---|
| 34 | * @file exfat_directory.c
|
---|
| 35 | * @brief Functions that work with FAT directory.
|
---|
| 36 | */
|
---|
| 37 |
|
---|
[bca3eac] | 38 | #include "exfat.h"
|
---|
[4dd9395] | 39 | #include "exfat_directory.h"
|
---|
| 40 | #include "exfat_fat.h"
|
---|
[f73b291] | 41 | #include <block.h>
|
---|
[4dd9395] | 42 | #include <errno.h>
|
---|
| 43 | #include <byteorder.h>
|
---|
| 44 | #include <mem.h>
|
---|
[38d150e] | 45 | #include <stdlib.h>
|
---|
[4dd9395] | 46 | #include <str.h>
|
---|
[bca3eac] | 47 | #include <align.h>
|
---|
[4dd9395] | 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;
|
---|
[f061de75] | 58 | di->fragmented = false;
|
---|
| 59 | di->firstc = 0;
|
---|
[4dd9395] | 60 | }
|
---|
| 61 |
|
---|
[b7fd2a0] | 62 | errno_t exfat_directory_open(exfat_node_t *nodep, exfat_directory_t *di)
|
---|
[4dd9395] | 63 | {
|
---|
| 64 | exfat_directory_init(di);
|
---|
[1b20da0] | 65 | di->nodep = nodep;
|
---|
[4dd9395] | 66 | if (di->nodep->type != EXFAT_DIRECTORY)
|
---|
| 67 | return EINVAL;
|
---|
[375ab5e] | 68 | di->service_id = nodep->idx->service_id;
|
---|
[f061de75] | 69 | di->fragmented = nodep->fragmented;
|
---|
| 70 | di->firstc = nodep->firstc;
|
---|
[4dd9395] | 71 |
|
---|
[375ab5e] | 72 | di->bs = block_bb_get(di->service_id);
|
---|
[ff0c270] | 73 | di->blocks = ROUND_UP(nodep->size, BPS(di->bs)) / BPS(di->bs);
|
---|
[f061de75] | 74 | return EOK;
|
---|
| 75 | }
|
---|
| 76 |
|
---|
[1b20da0] | 77 | errno_t exfat_directory_open_parent(exfat_directory_t *di,
|
---|
[375ab5e] | 78 | service_id_t service_id, exfat_cluster_t firstc, bool fragmented)
|
---|
[f061de75] | 79 | {
|
---|
| 80 | exfat_directory_init(di);
|
---|
[375ab5e] | 81 | di->service_id = service_id;
|
---|
[f061de75] | 82 | di->fragmented = fragmented;
|
---|
| 83 | di->firstc = firstc;
|
---|
[375ab5e] | 84 | di->bs = block_bb_get(service_id);
|
---|
[f061de75] | 85 | di->blocks = 0;
|
---|
[4dd9395] | 86 | return EOK;
|
---|
| 87 | }
|
---|
| 88 |
|
---|
[b7fd2a0] | 89 | errno_t exfat_directory_close(exfat_directory_t *di)
|
---|
[4dd9395] | 90 | {
|
---|
[b7fd2a0] | 91 | errno_t rc = EOK;
|
---|
[a35b458] | 92 |
|
---|
[a885ab8] | 93 | if (di->b) {
|
---|
[4dd9395] | 94 | rc = block_put(di->b);
|
---|
[a885ab8] | 95 | di->b = NULL;
|
---|
| 96 | }
|
---|
[a35b458] | 97 |
|
---|
[4dd9395] | 98 | return rc;
|
---|
| 99 | }
|
---|
| 100 |
|
---|
[b7fd2a0] | 101 | static errno_t exfat_directory_block_load(exfat_directory_t *di)
|
---|
[4dd9395] | 102 | {
|
---|
| 103 | uint32_t i;
|
---|
[b7fd2a0] | 104 | errno_t rc = EOK;
|
---|
[4dd9395] | 105 |
|
---|
| 106 | i = (di->pos * sizeof(exfat_dentry_t)) / BPS(di->bs);
|
---|
[f061de75] | 107 | if (di->nodep && (i >= di->blocks))
|
---|
| 108 | return ENOENT;
|
---|
| 109 |
|
---|
| 110 | if (di->b && di->bnum != i) {
|
---|
[b741888] | 111 | rc = block_put(di->b);
|
---|
[f061de75] | 112 | di->b = NULL;
|
---|
[c62751f] | 113 | if (rc != EOK)
|
---|
| 114 | return rc;
|
---|
[f061de75] | 115 | }
|
---|
| 116 | if (!di->b) {
|
---|
| 117 | if (di->nodep) {
|
---|
[ff0c270] | 118 | rc = exfat_block_get(&di->b, di->bs, di->nodep, i,
|
---|
| 119 | BLOCK_FLAGS_NONE);
|
---|
[f061de75] | 120 | } else {
|
---|
[ff0c270] | 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);
|
---|
[4dd9395] | 124 | }
|
---|
[f061de75] | 125 | if (rc != EOK) {
|
---|
| 126 | di->b = NULL;
|
---|
| 127 | return rc;
|
---|
| 128 | }
|
---|
| 129 | di->bnum = i;
|
---|
[4dd9395] | 130 | }
|
---|
[b741888] | 131 | return rc;
|
---|
[4dd9395] | 132 | }
|
---|
| 133 |
|
---|
[b7fd2a0] | 134 | errno_t exfat_directory_next(exfat_directory_t *di)
|
---|
[4dd9395] | 135 | {
|
---|
[b7fd2a0] | 136 | errno_t rc;
|
---|
[4dd9395] | 137 |
|
---|
| 138 | di->pos += 1;
|
---|
| 139 | rc = exfat_directory_block_load(di);
|
---|
[ff0c270] | 140 | if (rc != EOK)
|
---|
[4dd9395] | 141 | di->pos -= 1;
|
---|
[a35b458] | 142 |
|
---|
[4dd9395] | 143 | return rc;
|
---|
| 144 | }
|
---|
| 145 |
|
---|
[b7fd2a0] | 146 | errno_t exfat_directory_prev(exfat_directory_t *di)
|
---|
[4dd9395] | 147 | {
|
---|
[b7fd2a0] | 148 | errno_t rc = EOK;
|
---|
[a35b458] | 149 |
|
---|
[4dd9395] | 150 | if (di->pos > 0) {
|
---|
| 151 | di->pos -= 1;
|
---|
[0dbe5ac] | 152 | rc = exfat_directory_block_load(di);
|
---|
| 153 | } else
|
---|
[4dd9395] | 154 | return ENOENT;
|
---|
[a35b458] | 155 |
|
---|
[0dbe5ac] | 156 | if (rc != EOK)
|
---|
[4dd9395] | 157 | di->pos += 1;
|
---|
[a35b458] | 158 |
|
---|
[4dd9395] | 159 | return rc;
|
---|
| 160 | }
|
---|
| 161 |
|
---|
[b7fd2a0] | 162 | errno_t exfat_directory_seek(exfat_directory_t *di, aoff64_t pos)
|
---|
[4dd9395] | 163 | {
|
---|
| 164 | aoff64_t _pos = di->pos;
|
---|
[b7fd2a0] | 165 | errno_t rc;
|
---|
[4dd9395] | 166 |
|
---|
| 167 | di->pos = pos;
|
---|
| 168 | rc = exfat_directory_block_load(di);
|
---|
[ff0c270] | 169 | if (rc != EOK)
|
---|
[4dd9395] | 170 | di->pos = _pos;
|
---|
[a35b458] | 171 |
|
---|
[4dd9395] | 172 | return rc;
|
---|
| 173 | }
|
---|
| 174 |
|
---|
[b7fd2a0] | 175 | errno_t exfat_directory_get(exfat_directory_t *di, exfat_dentry_t **d)
|
---|
[4dd9395] | 176 | {
|
---|
[b7fd2a0] | 177 | errno_t rc;
|
---|
[a35b458] | 178 |
|
---|
[4dd9395] | 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 | }
|
---|
[a35b458] | 184 |
|
---|
[4dd9395] | 185 | return rc;
|
---|
| 186 | }
|
---|
| 187 |
|
---|
[b7fd2a0] | 188 | errno_t exfat_directory_find(exfat_directory_t *di, exfat_dentry_clsf_t type,
|
---|
[ff0c270] | 189 | exfat_dentry_t **d)
|
---|
[27678a0] | 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);
|
---|
[a35b458] | 198 |
|
---|
[27678a0] | 199 | return ENOENT;
|
---|
| 200 | }
|
---|
| 201 |
|
---|
[b7fd2a0] | 202 | errno_t
|
---|
[ff0c270] | 203 | exfat_directory_find_continue(exfat_directory_t *di, exfat_dentry_clsf_t type,
|
---|
| 204 | exfat_dentry_t **d)
|
---|
[27678a0] | 205 | {
|
---|
[b7fd2a0] | 206 | errno_t rc;
|
---|
[27678a0] | 207 | rc = exfat_directory_next(di);
|
---|
| 208 | if (rc != EOK)
|
---|
| 209 | return rc;
|
---|
| 210 | return exfat_directory_find(di, type, d);
|
---|
| 211 | }
|
---|
[4dd9395] | 212 |
|
---|
[1b20da0] | 213 | errno_t exfat_directory_read_file(exfat_directory_t *di, char *name, size_t size,
|
---|
[f5f1e1c] | 214 | exfat_file_dentry_t *df, exfat_stream_dentry_t *ds)
|
---|
[73b1218] | 215 | {
|
---|
[ff0c270] | 216 | uint16_t wname[EXFAT_FILENAME_LEN + 1];
|
---|
[73b1218] | 217 | exfat_dentry_t *d = NULL;
|
---|
[b7fd2a0] | 218 | errno_t rc;
|
---|
[d5c1051] | 219 | int i;
|
---|
[73b1218] | 220 | size_t offset = 0;
|
---|
[963ea42] | 221 | aoff64_t start_pos = 0;
|
---|
[a35b458] | 222 |
|
---|
[73b1218] | 223 | rc = exfat_directory_find(di, EXFAT_DENTRY_FILE, &d);
|
---|
| 224 | if (rc != EOK)
|
---|
| 225 | return rc;
|
---|
[963ea42] | 226 | start_pos = di->pos;
|
---|
[73b1218] | 227 | *df = d->file;
|
---|
| 228 |
|
---|
| 229 | rc = exfat_directory_next(di);
|
---|
| 230 | if (rc != EOK)
|
---|
| 231 | return rc;
|
---|
[1b20da0] | 232 | rc = exfat_directory_get(di, &d);
|
---|
[73b1218] | 233 | if (rc != EOK)
|
---|
| 234 | return rc;
|
---|
| 235 | if (exfat_classify_dentry(d) != EXFAT_DENTRY_STREAM)
|
---|
| 236 | return ENOENT;
|
---|
| 237 | *ds = d->stream;
|
---|
[a35b458] | 238 |
|
---|
[f5f1e1c] | 239 | if (ds->name_size > size)
|
---|
[73b1218] | 240 | return EOVERFLOW;
|
---|
[f5f1e1c] | 241 |
|
---|
[ff0c270] | 242 | for (i = 0; i < df->count - 1; i++) {
|
---|
[73b1218] | 243 | rc = exfat_directory_next(di);
|
---|
| 244 | if (rc != EOK)
|
---|
| 245 | return rc;
|
---|
[1b20da0] | 246 | rc = exfat_directory_get(di, &d);
|
---|
[73b1218] | 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 | }
|
---|
[f5f1e1c] | 253 | rc = utf16_to_str(name, size, wname);
|
---|
[73b1218] | 254 | if (rc != EOK)
|
---|
| 255 | return rc;
|
---|
| 256 |
|
---|
[963ea42] | 257 | exfat_directory_seek(di, start_pos);
|
---|
[73b1218] | 258 | return EOK;
|
---|
| 259 | }
|
---|
| 260 |
|
---|
[b7fd2a0] | 261 | errno_t exfat_directory_read_vollabel(exfat_directory_t *di, char *label,
|
---|
[f3504c1] | 262 | size_t size)
|
---|
| 263 | {
|
---|
| 264 | uint16_t wlabel[EXFAT_VOLLABEL_LEN + 1];
|
---|
| 265 | exfat_dentry_t *d = NULL;
|
---|
[b7fd2a0] | 266 | errno_t rc;
|
---|
[f3504c1] | 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 |
|
---|
[d8df2fb] | 289 | static uint16_t exfat_directory_set_checksum(const uint8_t *bytes, size_t count)
|
---|
[151a4e2] | 290 | {
|
---|
[d8df2fb] | 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;
|
---|
[92d9406] | 297 | checksum = ((checksum << 15) | (checksum >> 1)) +
|
---|
| 298 | (uint16_t)bytes[idx];
|
---|
[d8df2fb] | 299 | }
|
---|
| 300 | return checksum;
|
---|
| 301 | }
|
---|
| 302 |
|
---|
[1b20da0] | 303 | errno_t exfat_directory_sync_file(exfat_directory_t *di, exfat_file_dentry_t *df,
|
---|
[d8df2fb] | 304 | exfat_stream_dentry_t *ds)
|
---|
| 305 | {
|
---|
[b7fd2a0] | 306 | errno_t rc;
|
---|
[d5c1051] | 307 | int i, count;
|
---|
[ff0c270] | 308 | exfat_dentry_t *array = NULL, *de;
|
---|
[d8df2fb] | 309 | aoff64_t pos = di->pos;
|
---|
| 310 |
|
---|
| 311 | rc = exfat_directory_get(di, &de);
|
---|
| 312 | if (rc != EOK)
|
---|
| 313 | return rc;
|
---|
[ff0c270] | 314 | count = de->file.count + 1;
|
---|
[0dbe5ac] | 315 | array = (exfat_dentry_t *) malloc(count * sizeof(exfat_dentry_t));
|
---|
[d8df2fb] | 316 | if (!array)
|
---|
| 317 | return ENOMEM;
|
---|
[ff0c270] | 318 | for (i = 0; i < count; i++) {
|
---|
[d8df2fb] | 319 | rc = exfat_directory_get(di, &de);
|
---|
[055be8a] | 320 | if (rc != EOK) {
|
---|
| 321 | free(array);
|
---|
[d8df2fb] | 322 | return rc;
|
---|
[055be8a] | 323 | }
|
---|
[c56c4576] | 324 | array[i] = *de;
|
---|
[d8df2fb] | 325 | rc = exfat_directory_next(di);
|
---|
[0dbe5ac] | 326 | if (rc != EOK) {
|
---|
[d8df2fb] | 327 | free(array);
|
---|
| 328 | return rc;
|
---|
| 329 | }
|
---|
| 330 | }
|
---|
| 331 | rc = exfat_directory_seek(di, pos);
|
---|
[ff0c270] | 332 | if (rc != EOK) {
|
---|
[d8df2fb] | 333 | free(array);
|
---|
| 334 | return rc;
|
---|
| 335 | }
|
---|
| 336 |
|
---|
| 337 | /* Sync */
|
---|
[c56c4576] | 338 | array[0].file.attr = host2uint16_t_le(df->attr);
|
---|
| 339 | array[1].stream.firstc = host2uint32_t_le(ds->firstc);
|
---|
[d8df2fb] | 340 | array[1].stream.flags = ds->flags;
|
---|
[c56c4576] | 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);
|
---|
[92d9406] | 343 | array[0].file.checksum =
|
---|
| 344 | host2uint16_t_le(exfat_directory_set_checksum((uint8_t *)array,
|
---|
[ff0c270] | 345 | count * sizeof(exfat_dentry_t)));
|
---|
[d8df2fb] | 346 |
|
---|
| 347 | /* Store */
|
---|
[ff0c270] | 348 | for (i = 0; i < count; i++) {
|
---|
[d8df2fb] | 349 | rc = exfat_directory_get(di, &de);
|
---|
[055be8a] | 350 | if (rc != EOK) {
|
---|
| 351 | free(array);
|
---|
[d8df2fb] | 352 | return rc;
|
---|
[055be8a] | 353 | }
|
---|
[c56c4576] | 354 | *de = array[i];
|
---|
[d8df2fb] | 355 | di->b->dirty = true;
|
---|
| 356 | rc = exfat_directory_next(di);
|
---|
[ff0c270] | 357 | if (rc != EOK) {
|
---|
[d8df2fb] | 358 | free(array);
|
---|
| 359 | return rc;
|
---|
| 360 | }
|
---|
| 361 | }
|
---|
[c56c4576] | 362 | free(array);
|
---|
[d8df2fb] | 363 |
|
---|
[151a4e2] | 364 | return EOK;
|
---|
| 365 | }
|
---|
| 366 |
|
---|
[b7fd2a0] | 367 | errno_t exfat_directory_write_file(exfat_directory_t *di, const char *name)
|
---|
[bca3eac] | 368 | {
|
---|
| 369 | fs_node_t *fn;
|
---|
| 370 | exfat_node_t *uctablep;
|
---|
| 371 | uint16_t *uctable;
|
---|
| 372 | exfat_dentry_t df, ds, *de;
|
---|
[ff0c270] | 373 | uint16_t wname[EXFAT_FILENAME_LEN + 1];
|
---|
[b7fd2a0] | 374 | errno_t rc;
|
---|
[d5c1051] | 375 | int i;
|
---|
[c56c4576] | 376 | size_t uctable_chars, j;
|
---|
[bca3eac] | 377 | aoff64_t pos;
|
---|
| 378 |
|
---|
| 379 | rc = str_to_utf16(wname, EXFAT_FILENAME_LEN, name);
|
---|
| 380 | if (rc != EOK)
|
---|
| 381 | return rc;
|
---|
[375ab5e] | 382 | rc = exfat_uctable_get(&fn, di->service_id);
|
---|
[bca3eac] | 383 | if (rc != EOK)
|
---|
| 384 | return rc;
|
---|
| 385 | uctablep = EXFAT_NODE(fn);
|
---|
| 386 |
|
---|
[92d9406] | 387 | uctable_chars = ALIGN_DOWN(uctablep->size,
|
---|
[1b20da0] | 388 | sizeof(uint16_t)) / sizeof(uint16_t);
|
---|
[bca3eac] | 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;
|
---|
[b2906c0] | 402 | ds.stream.name_size = utf16_wsize(wname);
|
---|
[1b20da0] | 403 | ds.stream.hash = host2uint16_t_le(exfat_name_hash(wname, uctable,
|
---|
[bca3eac] | 404 | uctable_chars));
|
---|
| 405 |
|
---|
| 406 | /* Fill file entry */
|
---|
| 407 | df.type = EXFAT_TYPE_FILE;
|
---|
| 408 | df.file.attr = 0;
|
---|
[1b20da0] | 409 | df.file.count = ROUND_UP(ds.stream.name_size, EXFAT_NAME_PART_LEN) /
|
---|
[bca3eac] | 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 */
|
---|
[0dbe5ac] | 419 | rc = exfat_directory_lookup_free(di, df.file.count + 1);
|
---|
[bca3eac] | 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;
|
---|
[c56c4576] | 428 | *de = df;
|
---|
[bca3eac] | 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;
|
---|
[c56c4576] | 438 | *de = ds;
|
---|
[bca3eac] | 439 | di->b->dirty = true;
|
---|
| 440 |
|
---|
| 441 | /* Write file name */
|
---|
| 442 | size_t chars = EXFAT_NAME_PART_LEN;
|
---|
| 443 | uint16_t *sname = wname;
|
---|
| 444 |
|
---|
[ff0c270] | 445 | for (i = 0; i < ds.stream.name_size; i++)
|
---|
[bca3eac] | 446 | wname[i] = host2uint16_t_le(wname[i]);
|
---|
| 447 |
|
---|
[ff0c270] | 448 | for (i = 0; i < df.file.count - 1; i++) {
|
---|
[bca3eac] | 449 | rc = exfat_directory_next(di);
|
---|
| 450 | if (rc != EOK)
|
---|
| 451 | return rc;
|
---|
| 452 |
|
---|
[92d9406] | 453 | if (i == df.file.count - 2) {
|
---|
| 454 | chars = ds.stream.name_size -
|
---|
[18b6a88] | 455 | EXFAT_NAME_PART_LEN * (df.file.count - 2);
|
---|
[92d9406] | 456 | }
|
---|
| 457 |
|
---|
[bca3eac] | 458 | rc = exfat_directory_get(di, &de);
|
---|
| 459 | if (rc != EOK)
|
---|
| 460 | return rc;
|
---|
| 461 | de->type = EXFAT_TYPE_NAME;
|
---|
[c56c4576] | 462 | /* test */
|
---|
[ff0c270] | 463 | for (j = 0; j < chars; j++) {
|
---|
[c56c4576] | 464 | de->name.name[j] = *sname;
|
---|
| 465 | sname++;
|
---|
| 466 | }
|
---|
| 467 |
|
---|
[bca3eac] | 468 | di->b->dirty = true;
|
---|
| 469 | }
|
---|
[a35b458] | 470 |
|
---|
[bca3eac] | 471 | return exfat_directory_seek(di, pos);
|
---|
| 472 | }
|
---|
| 473 |
|
---|
[b7fd2a0] | 474 | errno_t exfat_directory_erase_file(exfat_directory_t *di, aoff64_t pos)
|
---|
[7f0c08c] | 475 | {
|
---|
[b7fd2a0] | 476 | errno_t rc;
|
---|
[d5c1051] | 477 | int count;
|
---|
[4ba3535] | 478 | exfat_dentry_t *de;
|
---|
| 479 |
|
---|
[78257fe] | 480 | di->pos = pos;
|
---|
| 481 |
|
---|
[4ba3535] | 482 | rc = exfat_directory_get(di, &de);
|
---|
| 483 | if (rc != EOK)
|
---|
| 484 | return rc;
|
---|
[ff0c270] | 485 | count = de->file.count + 1;
|
---|
[a35b458] | 486 |
|
---|
[4ba3535] | 487 | while (count) {
|
---|
| 488 | rc = exfat_directory_get(di, &de);
|
---|
| 489 | if (rc != EOK)
|
---|
| 490 | return rc;
|
---|
[ff0c270] | 491 | de->type &= ~EXFAT_TYPE_USED;
|
---|
[4ba3535] | 492 | di->b->dirty = true;
|
---|
| 493 |
|
---|
| 494 | rc = exfat_directory_next(di);
|
---|
[ff0c270] | 495 | if (rc != EOK)
|
---|
[4ba3535] | 496 | return rc;
|
---|
| 497 | count--;
|
---|
| 498 | }
|
---|
[7f0c08c] | 499 | return EOK;
|
---|
| 500 | }
|
---|
| 501 |
|
---|
[b7fd2a0] | 502 | errno_t exfat_directory_expand(exfat_directory_t *di)
|
---|
[7d78d163] | 503 | {
|
---|
[b7fd2a0] | 504 | errno_t rc;
|
---|
[7d78d163] | 505 |
|
---|
| 506 | if (!di->nodep)
|
---|
| 507 | return ENOSPC;
|
---|
| 508 |
|
---|
[375ab5e] | 509 | rc = exfat_node_expand(di->nodep->idx->service_id, di->nodep, 1);
|
---|
[7d78d163] | 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);
|
---|
[a35b458] | 517 |
|
---|
[7d78d163] | 518 | return EOK;
|
---|
| 519 | }
|
---|
| 520 |
|
---|
[b7fd2a0] | 521 | errno_t exfat_directory_lookup_free(exfat_directory_t *di, size_t count)
|
---|
[7d78d163] | 522 | {
|
---|
[b7fd2a0] | 523 | errno_t rc;
|
---|
[7d78d163] | 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:
|
---|
[ff0c270] | 540 | if (found == 0)
|
---|
| 541 | pos = di->pos;
|
---|
[7d78d163] | 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 | }
|
---|
[1b20da0] | 553 | } while (exfat_directory_next(di) == EOK);
|
---|
[7d78d163] | 554 | } while (exfat_directory_expand(di) == EOK);
|
---|
| 555 | return ENOSPC;
|
---|
| 556 | }
|
---|
| 557 |
|
---|
[4dd9395] | 558 | /**
|
---|
| 559 | * @}
|
---|
[1b20da0] | 560 | */
|
---|