From 3627ede1b1dee1084015887a612da758e656c9ef Mon Sep 17 00:00:00 2001 From: Falk Werner Date: Sun, 3 Mar 2019 15:44:34 +0100 Subject: [PATCH] added implementation for lookup (provider) --- example/provider/main.c | 73 ++++++++++++++++++---------- lib/wsfs/provider/operation/lookup.c | 27 ++++++++-- 2 files changed, 72 insertions(+), 28 deletions(-) diff --git a/example/provider/main.c b/example/provider/main.c index 26b2904..dbe2576 100644 --- a/example/provider/main.c +++ b/example/provider/main.c @@ -61,6 +61,43 @@ static struct fs_entry const * fs_getentry( return NULL; } +static struct fs_entry const * fs_getentry_byname( + struct fs * fs, + ino_t parent, + char const * name) +{ + for( size_t i = 0; 0 != fs->entries[i].inode; i++) + { + struct fs_entry const * entry = &fs->entries[i]; + if ((parent == entry->parent) && (0 == strcmp(name, entry->name))) + { + return entry; + } + } + + return NULL; +} + +static void fs_stat( + struct fs_entry const * entry, + struct stat * stat) +{ + memset(stat, 0, sizeof(struct stat)); + + stat->st_ino = entry->inode; + stat->st_mode = entry->mode; + + if (FS_DIR == entry->type) + { + stat->st_mode |= S_IFDIR; + } + + if (FS_FILE == entry->type) + { + stat->st_mode |= S_IFREG; + stat->st_size = entry->content_length; + } +} static void fs_lookup( struct wsfsp_request * request, @@ -69,18 +106,18 @@ static void fs_lookup( void * user_data) { struct fs * fs = (struct fs*) user_data; - struct fs_entry const * dir = fs_getentry(fs, parent); - if ((NULL != dir) && (FS_DIR == dir->type)) + struct fs_entry const * entry = fs_getentry_byname(fs, parent, name); + if (NULL != entry) { + struct stat stat; + fs_stat(entry, &stat); + wsfsp_respond_lookup(request, &stat); + } + else + { + wsfsp_respond_error(request, -1); } - - - (void) name; - (void) user_data; - - puts("lookup"); - wsfsp_respond_error(request, -1); } @@ -95,21 +132,7 @@ static void fs_getattr( if (NULL != entry) { struct stat stat; - memset(&stat, 0, sizeof(stat)); - - stat.st_ino = entry->inode; - stat.st_mode = entry->mode; - - if (FS_DIR == entry->type) - { - stat.st_mode |= S_IFDIR; - } - - if (FS_FILE == entry->type) - { - stat.st_mode |= S_IFREG; - stat.st_size = entry->content_length; - } + fs_stat(entry, &stat); wsfsp_respond_getattr(request, &stat); } @@ -131,7 +154,7 @@ static void fs_readdir( { struct wsfsp_dirbuffer * buffer = wsfsp_dirbuffer_create(); wsfsp_dirbuffer_add(buffer, ".", dir->inode); - wsfsp_dirbuffer_add(buffer, "..", dir->parent); + wsfsp_dirbuffer_add(buffer, "..", dir->inode); for(size_t i = 0; 0 != fs->entries[i].inode; i++) { diff --git a/lib/wsfs/provider/operation/lookup.c b/lib/wsfs/provider/operation/lookup.c index e809e57..79ba9f4 100644 --- a/lib/wsfs/provider/operation/lookup.c +++ b/lib/wsfs/provider/operation/lookup.c @@ -1,5 +1,8 @@ #include "wsfs/provider/operation/lookup_intern.h" + #include +#include + #include "wsfs/provider/operation/error.h" #include "wsfs/util.h" @@ -30,10 +33,28 @@ void wsfsp_respond_lookup( struct wsfsp_request * request, struct stat const * stat) { - (void) request; - (void) stat; + bool const is_file = (0 != (stat->st_mode & S_IFREG)); + bool const is_dir = (0 != (stat->st_mode & S_IFDIR)); - wsfsp_respond_error(request, -1); + json_t * result = json_object(); + json_object_set_new(result, "inode", json_integer(stat->st_ino)); + json_object_set_new(result, "mode", json_integer(stat->st_mode & 0777)); + json_object_set_new(result, "atime", json_integer(stat->st_atime)); + json_object_set_new(result, "mtime", json_integer(stat->st_mtime)); + json_object_set_new(result, "ctime", json_integer(stat->st_ctime)); + + if (is_file) + { + json_object_set_new(result, "type", json_string("file")); + json_object_set_new(result, "size", json_integer(stat->st_size)); + } + + if (is_dir) + { + json_object_set_new(result, "type", json_string("dir")); + } + + wsfsp_respond(request, result); } void wsfsp_lookup_default(