Lines Matching refs:file

490 static lfs_ssize_t lfs_file_flushedwrite(lfs_t *lfs, lfs_file_t *file,
492 static lfs_ssize_t lfs_file_rawwrite(lfs_t *lfs, lfs_file_t *file,
494 static int lfs_file_rawsync(lfs_t *lfs, lfs_file_t *file);
495 static int lfs_file_outline(lfs_t *lfs, lfs_file_t *file);
496 static int lfs_file_flush(lfs_t *lfs, lfs_file_t *file);
516 static lfs_ssize_t lfs_file_flushedread(lfs_t *lfs, lfs_file_t *file,
518 static lfs_ssize_t lfs_file_rawread(lfs_t *lfs, lfs_file_t *file,
520 static int lfs_file_rawclose(lfs_t *lfs, lfs_file_t *file);
521 static lfs_soff_t lfs_file_rawsize(lfs_t *lfs, lfs_file_t *file);
2841 static int lfs_file_rawopencfg(lfs_t *lfs, lfs_file_t *file, argument
2858 file->cfg = cfg;
2859 file->flags = flags;
2860 file->pos = 0;
2861 file->off = 0;
2862 file->cache.buffer = NULL;
2865 lfs_stag_t tag = lfs_dir_find(lfs, &file->m, &path, &file->id);
2866 if (tag < 0 && !(tag == LFS_ERR_NOENT && file->id != 0x3ff)) {
2872 file->type = LFS_TYPE_REG;
2873 lfs_mlist_append(lfs, (struct lfs_mlist *)file);
2894 err = lfs_dir_commit(lfs, &file->m, LFS_MKATTRS(
2895 {LFS_MKTAG(LFS_TYPE_CREATE, file->id, 0), NULL},
2896 {LFS_MKTAG(LFS_TYPE_REG, file->id, nlen), path},
2897 {LFS_MKTAG(LFS_TYPE_INLINESTRUCT, file->id, 0), NULL}));
2917 tag = LFS_MKTAG(LFS_TYPE_INLINESTRUCT, file->id, 0);
2918 file->flags |= LFS_F_DIRTY;
2922 tag = lfs_dir_get(lfs, &file->m, LFS_MKTAG(0x700, 0x3ff, 0),
2923 LFS_MKTAG(LFS_TYPE_STRUCT, file->id, 8), &file->ctz);
2928 lfs_ctz_fromle32(&file->ctz);
2932 for (unsigned i = 0; i < file->cfg->attr_count; i++) {
2934 if ((file->flags & LFS_O_RDONLY) == LFS_O_RDONLY) {
2935 lfs_stag_t res = lfs_dir_get(lfs, &file->m,
2937 LFS_MKTAG(LFS_TYPE_USERATTR + file->cfg->attrs[i].type,
2938 file->id, file->cfg->attrs[i].size),
2939 file->cfg->attrs[i].buffer);
2948 if ((file->flags & LFS_O_WRONLY) == LFS_O_WRONLY) {
2949 if (file->cfg->attrs[i].size > lfs->attr_max) {
2954 file->flags |= LFS_F_DIRTY;
2960 if (file->cfg->buffer) {
2961 file->cache.buffer = file->cfg->buffer;
2963 file->cache.buffer = lfs_malloc(lfs->cfg->cache_size);
2964 if (!file->cache.buffer) {
2971 lfs_cache_zero(lfs, &file->cache);
2975 file->ctz.head = LFS_BLOCK_INLINE;
2976 file->ctz.size = lfs_tag_size(tag);
2977 file->flags |= LFS_F_INLINE;
2978 file->cache.block = file->ctz.head;
2979 file->cache.off = 0;
2980 file->cache.size = lfs->cfg->cache_size;
2983 if (file->ctz.size > 0) {
2984 lfs_stag_t res = lfs_dir_get(lfs, &file->m,
2986 LFS_MKTAG(LFS_TYPE_STRUCT, file->id,
2987 lfs_min(file->cache.size, 0x3fe)),
2988 file->cache.buffer);
3001 file->flags |= LFS_F_ERRED;
3003 lfs_file_rawclose(lfs, file);
3007 static int lfs_file_rawopen(lfs_t *lfs, lfs_file_t *file, argument
3010 int err = lfs_file_rawopencfg(lfs, file, path, flags, &defaults);
3014 static int lfs_file_rawclose(lfs_t *lfs, lfs_file_t *file) { argument
3016 int err = lfs_file_rawsync(lfs, file);
3022 lfs_mlist_remove(lfs, (struct lfs_mlist*)file);
3025 if (!file->cfg->buffer) {
3026 lfs_free(file->cache.buffer);
3034 static int lfs_file_relocate(lfs_t *lfs, lfs_file_t *file) { argument
3052 for (lfs_off_t i = 0; i < file->off; i++) {
3054 if (file->flags & LFS_F_INLINE) {
3055 err = lfs_dir_getread(lfs, &file->m,
3057 NULL, &file->cache, file->off-i,
3059 LFS_MKTAG(LFS_TYPE_INLINESTRUCT, file->id, 0),
3066 &file->cache, &lfs->rcache, file->off-i,
3067 file->block, i, &data, 1);
3085 memcpy(file->cache.buffer, lfs->pcache.buffer, lfs->cfg->cache_size);
3086 file->cache.block = lfs->pcache.block;
3087 file->cache.off = lfs->pcache.off;
3088 file->cache.size = lfs->pcache.size;
3091 file->block = nblock;
3092 file->flags |= LFS_F_WRITING;
3105 static int lfs_file_outline(lfs_t *lfs, lfs_file_t *file) { argument
3106 file->off = file->pos;
3108 int err = lfs_file_relocate(lfs, file);
3113 file->flags &= ~LFS_F_INLINE;
3118 static int lfs_file_flush(lfs_t *lfs, lfs_file_t *file) { argument
3119 if (file->flags & LFS_F_READING) {
3120 if (!(file->flags & LFS_F_INLINE)) {
3121 lfs_cache_drop(lfs, &file->cache);
3123 file->flags &= ~LFS_F_READING;
3127 if (file->flags & LFS_F_WRITING) {
3128 lfs_off_t pos = file->pos;
3130 if (!(file->flags & LFS_F_INLINE)) {
3133 .ctz.head = file->ctz.head,
3134 .ctz.size = file->ctz.size,
3136 .pos = file->pos,
3141 while (file->pos < file->ctz.size) {
3150 res = lfs_file_flushedwrite(lfs, file, &data, 1);
3164 int err = lfs_bd_flush(lfs, &file->cache, &lfs->rcache, true);
3175 LFS_DEBUG("Bad block at 0x%"PRIx32, file->block);
3176 err = lfs_file_relocate(lfs, file);
3182 file->pos = lfs_max(file->pos, file->ctz.size);
3186 file->ctz.head = file->block;
3187 file->ctz.size = file->pos;
3188 file->flags &= ~LFS_F_WRITING;
3189 file->flags |= LFS_F_DIRTY;
3191 file->pos = pos;
3199 static int lfs_file_rawsync(lfs_t *lfs, lfs_file_t *file) { argument
3200 if (file->flags & LFS_F_ERRED) {
3205 int err = lfs_file_flush(lfs, file);
3207 file->flags |= LFS_F_ERRED;
3212 if ((file->flags & LFS_F_DIRTY) &&
3213 !lfs_pair_isnull(file->m.pair)) {
3219 if (file->flags & LFS_F_INLINE) {
3222 buffer = file->cache.buffer;
3223 size = file->ctz.size;
3228 ctz = file->ctz;
3235 err = lfs_dir_commit(lfs, &file->m, LFS_MKATTRS(
3236 {LFS_MKTAG(type, file->id, size), buffer},
3237 {LFS_MKTAG(LFS_FROM_USERATTRS, file->id,
3238 file->cfg->attr_count), file->cfg->attrs}));
3240 file->flags |= LFS_F_ERRED;
3244 file->flags &= ~LFS_F_DIRTY;
3251 static lfs_ssize_t lfs_file_flushedread(lfs_t *lfs, lfs_file_t *file, argument
3256 if (file->pos >= file->ctz.size) {
3261 size = lfs_min(size, file->ctz.size - file->pos);
3266 if (!(file->flags & LFS_F_READING) ||
3267 file->off == lfs->cfg->block_size) {
3268 if (!(file->flags & LFS_F_INLINE)) {
3269 int err = lfs_ctz_find(lfs, NULL, &file->cache,
3270 file->ctz.head, file->ctz.size,
3271 file->pos, &file->block, &file->off);
3276 file->block = LFS_BLOCK_INLINE;
3277 file->off = file->pos;
3280 file->flags |= LFS_F_READING;
3284 lfs_size_t diff = lfs_min(nsize, lfs->cfg->block_size - file->off);
3285 if (file->flags & LFS_F_INLINE) {
3286 int err = lfs_dir_getread(lfs, &file->m,
3287 NULL, &file->cache, lfs->cfg->block_size,
3289 LFS_MKTAG(LFS_TYPE_INLINESTRUCT, file->id, 0),
3290 file->off, data, diff);
3296 NULL, &file->cache, lfs->cfg->block_size,
3297 file->block, file->off, data, diff);
3303 file->pos += diff;
3304 file->off += diff;
3312 static lfs_ssize_t lfs_file_rawread(lfs_t *lfs, lfs_file_t *file, argument
3314 LFS_ASSERT((file->flags & LFS_O_RDONLY) == LFS_O_RDONLY);
3317 if (file->flags & LFS_F_WRITING) {
3319 int err = lfs_file_flush(lfs, file);
3326 return lfs_file_flushedread(lfs, file, buffer, size);
3331 static lfs_ssize_t lfs_file_flushedwrite(lfs_t *lfs, lfs_file_t *file, argument
3336 if ((file->flags & LFS_F_INLINE) &&
3337 lfs_max(file->pos+nsize, file->ctz.size) >
3343 int err = lfs_file_outline(lfs, file);
3345 file->flags |= LFS_F_ERRED;
3352 if (!(file->flags & LFS_F_WRITING) ||
3353 file->off == lfs->cfg->block_size) {
3354 if (!(file->flags & LFS_F_INLINE)) {
3355 if (!(file->flags & LFS_F_WRITING) && file->pos > 0) {
3357 int err = lfs_ctz_find(lfs, NULL, &file->cache,
3358 file->ctz.head, file->ctz.size,
3359 file->pos-1, &file->block, &file->off);
3361 file->flags |= LFS_F_ERRED;
3366 lfs_cache_zero(lfs, &file->cache);
3371 int err = lfs_ctz_extend(lfs, &file->cache, &lfs->rcache,
3372 file->block, file->pos,
3373 &file->block, &file->off);
3375 file->flags |= LFS_F_ERRED;
3379 file->block = LFS_BLOCK_INLINE;
3380 file->off = file->pos;
3383 file->flags |= LFS_F_WRITING;
3387 lfs_size_t diff = lfs_min(nsize, lfs->cfg->block_size - file->off);
3389 int err = lfs_bd_prog(lfs, &file->cache, &lfs->rcache, true,
3390 file->block, file->off, data, diff);
3395 file->flags |= LFS_F_ERRED;
3401 err = lfs_file_relocate(lfs, file);
3403 file->flags |= LFS_F_ERRED;
3408 file->pos += diff;
3409 file->off += diff;
3419 static lfs_ssize_t lfs_file_rawwrite(lfs_t *lfs, lfs_file_t *file, argument
3421 LFS_ASSERT((file->flags & LFS_O_WRONLY) == LFS_O_WRONLY);
3423 if (file->flags & LFS_F_READING) {
3425 int err = lfs_file_flush(lfs, file);
3431 if ((file->flags & LFS_O_APPEND) && file->pos < file->ctz.size) {
3432 file->pos = file->ctz.size;
3435 if (file->pos + size > lfs->file_max) {
3440 if (!(file->flags & LFS_F_WRITING) && file->pos > file->ctz.size) {
3442 lfs_off_t pos = file->pos;
3443 file->pos = file->ctz.size;
3445 while (file->pos < pos) {
3446 lfs_ssize_t res = lfs_file_flushedwrite(lfs, file, &(uint8_t){0}, 1);
3453 lfs_ssize_t nsize = lfs_file_flushedwrite(lfs, file, buffer, size);
3458 file->flags &= ~LFS_F_ERRED;
3463 static lfs_soff_t lfs_file_rawseek(lfs_t *lfs, lfs_file_t *file, argument
3466 lfs_off_t npos = file->pos;
3470 if ((lfs_soff_t)file->pos + off < 0) {
3473 npos = file->pos + off;
3476 lfs_soff_t res = lfs_file_rawsize(lfs, file) + off;
3489 if (file->pos == npos) {
3498 !(file->flags & LFS_F_WRITING)
3503 int oindex = lfs_ctz_index(lfs, &(lfs_off_t){file->pos});
3507 && noff >= file->cache.off
3508 && noff < file->cache.off + file->cache.size) {
3509 file->pos = npos;
3510 file->off = noff;
3516 int err = lfs_file_flush(lfs, file);
3522 file->pos = npos;
3527 static int lfs_file_rawtruncate(lfs_t *lfs, lfs_file_t *file, lfs_off_t size) { argument
3528 LFS_ASSERT((file->flags & LFS_O_WRONLY) == LFS_O_WRONLY);
3534 lfs_off_t pos = file->pos;
3535 lfs_off_t oldsize = lfs_file_rawsize(lfs, file);
3538 int err = lfs_file_flush(lfs, file);
3544 err = lfs_ctz_find(lfs, NULL, &file->cache,
3545 file->ctz.head, file->ctz.size,
3546 size, &file->block, &file->off);
3553 file->pos = size;
3554 file->ctz.head = file->block;
3555 file->ctz.size = size;
3556 file->flags |= LFS_F_DIRTY | LFS_F_READING;
3559 lfs_soff_t res = lfs_file_rawseek(lfs, file, 0, LFS_SEEK_END);
3565 while (file->pos < size) {
3566 res = lfs_file_rawwrite(lfs, file, &(uint8_t){0}, 1);
3574 lfs_soff_t res = lfs_file_rawseek(lfs, file, pos, LFS_SEEK_SET);
3583 static lfs_soff_t lfs_file_rawtell(lfs_t *lfs, lfs_file_t *file) { argument
3585 return file->pos;
3588 static int lfs_file_rawrewind(lfs_t *lfs, lfs_file_t *file) { argument
3589 lfs_soff_t res = lfs_file_rawseek(lfs, file, 0, LFS_SEEK_SET);
3597 static lfs_soff_t lfs_file_rawsize(lfs_t *lfs, lfs_file_t *file) { argument
3601 if (file->flags & LFS_F_WRITING) {
3602 return lfs_max(file->pos, file->ctz.size);
3606 return file->ctz.size;
4692 } file; member
4925 entry.d.u.file.head, entry.d.u.file.size, cb, data);
5479 int lfs_file_open(lfs_t *lfs, lfs_file_t *file, const char *path, int flags) { argument
5485 (void*)lfs, (void*)file, path, flags);
5486 LFS_ASSERT(!lfs_mlist_isopen(lfs->mlist, (struct lfs_mlist*)file));
5488 err = lfs_file_rawopen(lfs, file, path, flags);
5496 int lfs_file_opencfg(lfs_t *lfs, lfs_file_t *file, argument
5505 (void*)lfs, (void*)file, path, flags,
5507 LFS_ASSERT(!lfs_mlist_isopen(lfs->mlist, (struct lfs_mlist*)file));
5509 err = lfs_file_rawopencfg(lfs, file, path, flags, cfg);
5516 int lfs_file_close(lfs_t *lfs, lfs_file_t *file) { argument
5521 LFS_TRACE("lfs_file_close(%p, %p)", (void*)lfs, (void*)file);
5522 LFS_ASSERT(lfs_mlist_isopen(lfs->mlist, (struct lfs_mlist*)file));
5524 err = lfs_file_rawclose(lfs, file);
5532 int lfs_file_sync(lfs_t *lfs, lfs_file_t *file) { argument
5537 LFS_TRACE("lfs_file_sync(%p, %p)", (void*)lfs, (void*)file);
5538 LFS_ASSERT(lfs_mlist_isopen(lfs->mlist, (struct lfs_mlist*)file));
5540 err = lfs_file_rawsync(lfs, file);
5548 lfs_ssize_t lfs_file_read(lfs_t *lfs, lfs_file_t *file, argument
5555 (void*)lfs, (void*)file, buffer, size);
5556 LFS_ASSERT(lfs_mlist_isopen(lfs->mlist, (struct lfs_mlist*)file));
5558 lfs_ssize_t res = lfs_file_rawread(lfs, file, buffer, size);
5566 lfs_ssize_t lfs_file_write(lfs_t *lfs, lfs_file_t *file, argument
5573 (void*)lfs, (void*)file, buffer, size);
5574 LFS_ASSERT(lfs_mlist_isopen(lfs->mlist, (struct lfs_mlist*)file));
5576 lfs_ssize_t res = lfs_file_rawwrite(lfs, file, buffer, size);
5584 lfs_soff_t lfs_file_seek(lfs_t *lfs, lfs_file_t *file, argument
5591 (void*)lfs, (void*)file, off, whence);
5592 LFS_ASSERT(lfs_mlist_isopen(lfs->mlist, (struct lfs_mlist*)file));
5594 lfs_soff_t res = lfs_file_rawseek(lfs, file, off, whence);
5602 int lfs_file_truncate(lfs_t *lfs, lfs_file_t *file, lfs_off_t size) { argument
5608 (void*)lfs, (void*)file, size);
5609 LFS_ASSERT(lfs_mlist_isopen(lfs->mlist, (struct lfs_mlist*)file));
5611 err = lfs_file_rawtruncate(lfs, file, size);
5619 lfs_soff_t lfs_file_tell(lfs_t *lfs, lfs_file_t *file) { argument
5624 LFS_TRACE("lfs_file_tell(%p, %p)", (void*)lfs, (void*)file);
5625 LFS_ASSERT(lfs_mlist_isopen(lfs->mlist, (struct lfs_mlist*)file));
5627 lfs_soff_t res = lfs_file_rawtell(lfs, file);
5634 int lfs_file_rewind(lfs_t *lfs, lfs_file_t *file) { argument
5639 LFS_TRACE("lfs_file_rewind(%p, %p)", (void*)lfs, (void*)file);
5641 err = lfs_file_rawrewind(lfs, file);
5648 lfs_soff_t lfs_file_size(lfs_t *lfs, lfs_file_t *file) { argument
5653 LFS_TRACE("lfs_file_size(%p, %p)", (void*)lfs, (void*)file);
5654 LFS_ASSERT(lfs_mlist_isopen(lfs->mlist, (struct lfs_mlist*)file));
5656 lfs_soff_t res = lfs_file_rawsize(lfs, file);