#define _FILE_OFFSET_BITS 64 #include "fs_dirent64.hpp" #include "fuse_attr.h" #include "fuse_dirent.h" #include "fuse_direntplus.h" #include "fuse_dirents.hpp" #include "fuse_entry.h" #include "stat_utils.h" #include #include #include #include #include #include #include /* 32KB - same as glibc getdents buffer size */ #define DENTS_BUF_EXPAND_SIZE (1024 * 32) /* Initial pre-allocated capacity for directory-entry offset vector */ #define DENTS_OFFS_INITIAL_CAPACITY 64 // Grow through checked reallocations; kvec's unchecked growth loses the // original pointer on failure. Offsets in the returned listing are uint32_t. static uint64_t _round_up(const uint64_t number_, const uint64_t multiple_) { return (((number_ + multiple_ - 1) / multiple_) * multiple_); } static uint64_t _align_uint64_t(uint64_t v_) { return ((v_ + sizeof(uint64_t) - 1) & ~(sizeof(uint64_t) - 1)); } static uint64_t _dirent_size(const uint64_t namelen_) { uint64_t rv; rv = offsetof(fuse_dirent_t,name); rv += namelen_; rv = _align_uint64_t(rv); return rv; } // Grow the data buffer so one more entry of size_ bytes fits. The // reallocation is checked: on failure the buffer's pointer, length // and capacity are left exactly as they were, so a failed add neither // loses written entries nor leaks the old buffer. static int _dirents_buf_resize(fuse_dirents_t *d_, const uint64_t size_) { if(size_ > (UINT32_MAX - kv_size(d_->data))) return -ENOMEM; if((kv_size(d_->data) + size_) > kv_max(d_->data)) { uint64_t new_size; char *new_data; new_size = _round_up((kv_size(d_->data) + size_),DENTS_BUF_EXPAND_SIZE); if(new_size > SIZE_MAX) new_size = SIZE_MAX; new_data = (char*)realloc(d_->data.a,new_size); if(new_data == NULL) return -ENOMEM; d_->data.a = new_data; kv_max(d_->data) = new_size; } return 0; } // Grow the offset vector so entries_ more entries fit. Checked like // _dirents_buf_resize; growth mirrors kv_push's doubling. static int _dirents_offs_resize(fuse_dirents_t *d_, const uint64_t entries_) { uint64_t new_capacity; uint32_t *new_offs; if((kv_size(d_->offs) + entries_) <= kv_max(d_->offs)) return 0; new_capacity = kv_max(d_->offs); while((kv_size(d_->offs) + entries_) > new_capacity) new_capacity = (new_capacity ? (new_capacity << 1) : 2); if(new_capacity > SIZE_MAX / sizeof(uint32_t)) return -ENOMEM; new_offs = (uint32_t*)realloc(d_->offs.a,(new_capacity * sizeof(uint32_t))); if(new_offs == NULL) return -ENOMEM; d_->offs.a = new_offs; kv_max(d_->offs) = new_capacity; return 0; } // Reserve both vectors before committing either length. A failed append // can grow capacity, but never changes the listing or its cookies. // offs stays one longer than the entry count: offs[0] is the zero // sentinel and offs[k] is the data offset of entry k, so an entry's // cookie is its index + 1, the offset the next readdir resumes from. static fuse_dirent_t* _dirents_dirent_alloc(fuse_dirents_t *d_, const uint64_t namelen_) { int rv; uint64_t size; fuse_dirent_t *d; if(namelen_ > UINT32_MAX) return NULL; size = _dirent_size(namelen_); rv = _dirents_offs_resize(d_,1); if(rv) return NULL; rv = _dirents_buf_resize(d_,size); if(rv) return NULL; d = (fuse_dirent_t*)&kv_end(d_->data); kv_size(d_->data) += size; d->off = kv_size(d_->offs); d_->offs.a[d_->offs.n] = kv_size(d_->data); kv_size(d_->offs)++; return d; } int fuse_dirents_add(fuse_dirents_t *d_, const dirent *de_, const uint64_t namelen_) { fuse_dirent_t *d; d = _dirents_dirent_alloc(d_,namelen_); if(d == NULL) return -ENOMEM; d->ino = de_->d_ino; d->namelen = namelen_; d->type = de_->d_type; memcpy(d->name,de_->d_name,namelen_); return 0; } int fuse_dirents_add(fuse_dirents_t *d_, const fs::dirent64 *de_, const uint64_t namelen_) { fuse_dirent_t *d; d = _dirents_dirent_alloc(d_,namelen_); if(d == NULL) return -ENOMEM; d->ino = de_->ino; d->namelen = namelen_; d->type = de_->type; memcpy(d->name,de_->name,namelen_); return 0; } int fuse_dirents_add(fuse_dirents_t *d_, const fuse_dirent_t *de_, const uint64_t namelen_) { fuse_dirent_t *d; d = _dirents_dirent_alloc(d_,namelen_); if(d == NULL) return -ENOMEM; d->ino = de_->ino; d->namelen = namelen_; d->type = de_->type; memcpy(d->name,de_->name,namelen_); return 0; } // Restore the just-initialized state: empty data and a single offset // sentinel. offs[0] is never overwritten by adds, so only lengths // change and no reallocation happens. A fill that fails partway must // be reset before the dirents are reused: a partial listing would // otherwise be served at offsets belonging to a complete enumeration. void fuse_dirents_reset(fuse_dirents_t *d_) { kv_size(d_->data) = 0; kv_size(d_->offs) = 1; } int fuse_dirents_init(fuse_dirents_t *d_) { int rv; kv_init(d_->data); kv_init(d_->offs); rv = _dirents_buf_resize(d_,DENTS_BUF_EXPAND_SIZE); if(rv) return rv; rv = _dirents_offs_resize(d_,DENTS_OFFS_INITIAL_CAPACITY); if(rv) { kv_destroy(d_->data); kv_init(d_->data); return rv; } d_->offs.a[d_->offs.n++] = 0; return 0; } // Like fuse_dirents_init but reserves nothing beyond the single zero // offset sentinel. For scratch buffers filled immediately by the // caller. Returns -ENOMEM on allocation failure with nothing left // allocated. int fuse_dirents_init_noreserve(fuse_dirents_t *d_) { int rv; kv_init(d_->data); kv_init(d_->offs); rv = _dirents_offs_resize(d_,1); if(rv) return rv; d_->offs.a[d_->offs.n++] = 0; return 0; } void fuse_dirents_free(fuse_dirents_t *d_) { kv_destroy(d_->data); kv_destroy(d_->offs); }