Skip to content

Commit

Permalink
wfs-fuse: Create the file stream on file opening (much faster reading)
Browse files Browse the repository at this point in the history
  • Loading branch information
koolkdev committed Aug 12, 2017
1 parent 08677b0 commit 8845d40
Showing 1 changed file with 27 additions and 11 deletions.
38 changes: 27 additions & 11 deletions wfs-fuse/wfs-fuse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,16 @@
#include <assert.h>

#include <string>
#include <mutex>
#include <wfslib/WfsLib.h>

static std::shared_ptr<Wfs> wfs;

struct locked_stream {
std::unique_ptr<File::stream> stream;
std::mutex lock;
};

static int wfs_getattr(const char *path, struct stat *stbuf) {
memset(stbuf, 0, sizeof(struct stat));

Expand Down Expand Up @@ -59,29 +65,38 @@ static int wfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
}

static int wfs_open(const char *path, struct fuse_file_info *fi) {
if (!wfs->GetObject(path))
auto item = wfs->GetObject(path);
if (!item->IsFile())
return -ENOENT;

if ((fi->flags & O_ACCMODE) != O_RDONLY)
return -EACCES;

return 0;
fi->fh = reinterpret_cast<uint64_t>(new locked_stream {
std::unique_ptr<File::stream>(new File::stream(std::dynamic_pointer_cast<File>(item)))
});

return 0;
}

static int wfs_release(const char *path, struct fuse_file_info *fi) {
(void) path;

delete reinterpret_cast<locked_stream *>(fi->fh);
return 0;
}

static int wfs_read(const char *path, char *buf, size_t size, off_t offset,
struct fuse_file_info *fi) {
(void) fi;

auto item = wfs->GetObject(path);
if (!item || !item->IsFile())
return -ENOENT;
(void) path;

File::stream stream(std::dynamic_pointer_cast<File>(item));
auto stream = reinterpret_cast<locked_stream *>(fi->fh);
std::lock_guard<std::mutex> guard(stream->lock);

stream.seekg(offset, stream.beg);
stream.read(buf, size);
stream->stream->seekg(offset, stream->stream->beg);
stream->stream->read(buf, size);

return static_cast<int>(stream.gcount());
return static_cast<int>(stream->stream->gcount());
}

int wfs_readlink(const char *path, char *buf, size_t size) {
Expand Down Expand Up @@ -225,6 +240,7 @@ int main(int argc, char *argv[]) {
wfs_oper.getattr = wfs_getattr;
wfs_oper.readdir = wfs_readdir;
wfs_oper.open = wfs_open;
wfs_oper.release = wfs_release;
wfs_oper.read = wfs_read;
wfs_oper.readlink = wfs_readlink;

Expand Down

0 comments on commit 8845d40

Please sign in to comment.