From 54e7d8476ca3b41cbb38226e58241e1920ad357a Mon Sep 17 00:00:00 2001 From: Javier Date: Sun, 16 Jul 2017 17:24:34 +0200 Subject: rename to volumefs/userfs and add systemd unit files --- src/Makefile.am | 14 ++--- src/userfs.c | 78 +++++++++++++++++++++++++ src/usersfs.c | 78 ------------------------- src/volumefs.c | 174 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/volumesfs.c | 174 -------------------------------------------------------- 5 files changed, 259 insertions(+), 259 deletions(-) create mode 100644 src/userfs.c delete mode 100644 src/usersfs.c create mode 100644 src/volumefs.c delete mode 100644 src/volumesfs.c (limited to 'src') diff --git a/src/Makefile.am b/src/Makefile.am index de90afd..801f704 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,9 +1,9 @@ -sbin_PROGRAMS = volumesfs usersfs +sbin_PROGRAMS = volumefs userfs -volumesfs_SOURCES = volumesfs.c -volumesfs_CPPFLAGS = $(FUSE_CFLAGS) $(UDISKS2_CFLAGS) -volumesfs_LDADD = $(FUSE_LIBS) $(UDISKS2_LIBS) +volumefs_SOURCES = volumefs.c +volumefs_CPPFLAGS = $(FUSE_CFLAGS) $(UDISKS2_CFLAGS) +volumefs_LDADD = $(FUSE_LIBS) $(UDISKS2_LIBS) -usersfs_SOURCES = usersfs.c -usersfs_CPPFLAGS = $(FUSE_CFLAGS) -usersfs_LDADD = $(FUSE_LIBS) +userfs_SOURCES = userfs.c +userfs_CPPFLAGS = $(FUSE_CFLAGS) +userfs_LDADD = $(FUSE_LIBS) diff --git a/src/userfs.c b/src/userfs.c new file mode 100644 index 0000000..bb76ed1 --- /dev/null +++ b/src/userfs.c @@ -0,0 +1,78 @@ +#define FUSE_USE_VERSION 30 + +#include +#include +#include +#include + +#include + +#include + +static int usersfs_getattr(const char *path, struct stat *stbuf) +{ + if (strcmp(path, "/") == 0) { + stbuf->st_mode = S_IFDIR | 0755; + stbuf->st_nlink = 2; + } else { + struct passwd *pwent = getpwnam(path + 1); + if (pwent && pwent->pw_dir) { + stbuf->st_mode = S_IFLNK | 0444; + stbuf->st_nlink = 1; + } else { + return -ENOENT; + } + } + return 0; +} + +static int usersfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler, + off_t offset, struct fuse_file_info *fi) +{ + if (strcmp(path, "/") != 0) { + return -ENOENT; + } + + (void) offset; + (void) fi; + + filler(buf, ".", NULL, 0); + filler(buf, "..", NULL, 0); + + struct passwd *pwent; + setpwent(); + while ((pwent = getpwent())) { + if ((pwent->pw_uid == 0 || pwent->pw_uid >= 1000) && pwent->pw_name && pwent->pw_dir) { + filler(buf, pwent->pw_name, NULL, 0); + } + } + setpwent(); + + return 0; +} + +static int usersfs_readlink(const char *path, char *buf, size_t size) +{ + if (strcmp(path, "/") == 0) { + return -EINVAL; + } + + struct passwd *pwent = getpwnam(path + 1); + if (pwent && pwent->pw_dir) { + strcpy(buf, pwent->pw_dir); + return 0; + } else { + return -ENOENT; + } +} + +static struct fuse_operations usersfs_oper = { + .getattr = usersfs_getattr, + .readdir = usersfs_readdir, + .readlink = usersfs_readlink +}; + +int main(int argc, char *argv[]) +{ + return fuse_main(argc, argv, &usersfs_oper, NULL); +} diff --git a/src/usersfs.c b/src/usersfs.c deleted file mode 100644 index bb76ed1..0000000 --- a/src/usersfs.c +++ /dev/null @@ -1,78 +0,0 @@ -#define FUSE_USE_VERSION 30 - -#include -#include -#include -#include - -#include - -#include - -static int usersfs_getattr(const char *path, struct stat *stbuf) -{ - if (strcmp(path, "/") == 0) { - stbuf->st_mode = S_IFDIR | 0755; - stbuf->st_nlink = 2; - } else { - struct passwd *pwent = getpwnam(path + 1); - if (pwent && pwent->pw_dir) { - stbuf->st_mode = S_IFLNK | 0444; - stbuf->st_nlink = 1; - } else { - return -ENOENT; - } - } - return 0; -} - -static int usersfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler, - off_t offset, struct fuse_file_info *fi) -{ - if (strcmp(path, "/") != 0) { - return -ENOENT; - } - - (void) offset; - (void) fi; - - filler(buf, ".", NULL, 0); - filler(buf, "..", NULL, 0); - - struct passwd *pwent; - setpwent(); - while ((pwent = getpwent())) { - if ((pwent->pw_uid == 0 || pwent->pw_uid >= 1000) && pwent->pw_name && pwent->pw_dir) { - filler(buf, pwent->pw_name, NULL, 0); - } - } - setpwent(); - - return 0; -} - -static int usersfs_readlink(const char *path, char *buf, size_t size) -{ - if (strcmp(path, "/") == 0) { - return -EINVAL; - } - - struct passwd *pwent = getpwnam(path + 1); - if (pwent && pwent->pw_dir) { - strcpy(buf, pwent->pw_dir); - return 0; - } else { - return -ENOENT; - } -} - -static struct fuse_operations usersfs_oper = { - .getattr = usersfs_getattr, - .readdir = usersfs_readdir, - .readlink = usersfs_readlink -}; - -int main(int argc, char *argv[]) -{ - return fuse_main(argc, argv, &usersfs_oper, NULL); -} diff --git a/src/volumefs.c b/src/volumefs.c new file mode 100644 index 0000000..bc36ba3 --- /dev/null +++ b/src/volumefs.c @@ -0,0 +1,174 @@ +#define FUSE_USE_VERSION 30 + +#include +#include +#include +#include + +#include + +#include + +static GMainLoop *main_loop; +static GThread *sub_thread; +static UDisksClient *client; + +static GHashTable *mounts; +G_LOCK_DEFINE_STATIC(mounts); + +static void udisks_changed(UDisksClient *client, gpointer user_data) +{ + GDBusObjectManager *manager = udisks_client_get_object_manager(client); + GList *objs = g_dbus_object_manager_get_objects(manager); + + G_LOCK(mounts); + + g_debug("udisks changed"); + + g_hash_table_remove_all(mounts); + + for (GList *l = objs; l; l = g_list_next(l)) { + UDisksObject *obj = UDISKS_OBJECT(l->data); + UDisksFilesystem *filesys = udisks_object_get_filesystem(obj); + UDisksBlock *block = udisks_object_get_block(obj); + if (block && filesys) { + const gchar *name = udisks_block_get_id_label(block); + if (!name || strlen(name) == 0) { + name = udisks_block_get_id_uuid(block); + } + + const gchar * const * mpoints = udisks_filesystem_get_mount_points(filesys); + const gchar * mountpoint = mpoints && mpoints[0] ? mpoints[0] : NULL; + + if (name && mountpoint) { + g_debug("%s -> %s", name, mountpoint); + g_hash_table_insert(mounts, g_strdup(name), g_strdup(mountpoint)); + } + } + if (filesys) g_object_unref(filesys); + if (block) g_object_unref(block); + } + + G_UNLOCK(mounts); + + g_list_free_full(objs, g_object_unref); +} + +static gpointer sub_thread_main(gpointer user_data) +{ + GError *error = NULL; + + client = udisks_client_new_sync(NULL, &error); + if (!client) { + g_printerr("Could not connect to UDisks2 service: %s", error->message); + g_error_free(error); + } + + g_signal_connect(client, "changed", + G_CALLBACK(udisks_changed), NULL); + + udisks_changed(client, NULL); + + g_main_loop_run(main_loop); + + return NULL; +} + +static void * volumesfs_init(struct fuse_conn_info *conn) +{ + main_loop = g_main_loop_new(NULL, FALSE); + + mounts = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free); + + sub_thread = g_thread_new("volumesfs-sub-thread", sub_thread_main, NULL); + + return NULL; +} + +static void volumesfs_destroy(void *user_data) +{ + g_main_loop_quit(main_loop); + + g_thread_join(sub_thread); + + g_object_unref(client); + g_hash_table_destroy(mounts); + g_main_loop_unref(main_loop); +} + +static int volumesfs_getattr(const char *path, struct stat *stbuf) +{ + if (strcmp(path, "/") == 0) { + stbuf->st_mode = S_IFDIR | 0755; + stbuf->st_nlink = 2; + } else { + G_LOCK(mounts); + const gchar *mountpoint = g_hash_table_lookup(mounts, path + 1); + G_UNLOCK(mounts); + if (mountpoint) { + stbuf->st_mode = S_IFLNK | 0444; + stbuf->st_nlink = 1; + } else { + return -ENOENT; + } + } + return 0; +} + +static int volumesfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler, + off_t offset, struct fuse_file_info *fi) +{ + if (strcmp(path, "/") != 0) { + return -ENOENT; + } + + G_LOCK(mounts); + + (void) offset; + (void) fi; + + filler(buf, ".", NULL, 0); + filler(buf, "..", NULL, 0); + + GHashTableIter iter; + gchar *name; + g_hash_table_iter_init(&iter, mounts); + while (g_hash_table_iter_next(&iter, (void**)&name, NULL)) { + filler(buf, name, NULL, 0); + } + + G_UNLOCK(mounts); + + return 0; +} + +static int volumesfs_readlink(const char *path, char *buf, size_t size) +{ + if (strcmp(path, "/") == 0) { + return -EINVAL; + } + + G_LOCK(mounts); + gchar *mountpoint = g_hash_table_lookup(mounts, path + 1); + G_UNLOCK(mounts); + + if (mountpoint) { + strcpy(buf, mountpoint); + return 0; + } else { + return -ENOENT; + } +} + +static struct fuse_operations volumesfs_oper = { + .init = volumesfs_init, + .destroy = volumesfs_destroy, + .getattr = volumesfs_getattr, + .readdir = volumesfs_readdir, + .readlink = volumesfs_readlink +}; + +int main(int argc, char *argv[]) +{ + return fuse_main(argc, argv, &volumesfs_oper, NULL); +} diff --git a/src/volumesfs.c b/src/volumesfs.c deleted file mode 100644 index acf991b..0000000 --- a/src/volumesfs.c +++ /dev/null @@ -1,174 +0,0 @@ -#define FUSE_USE_VERSION 30 - -#include -#include -#include -#include - -#include - -#include - -static GMainLoop *main_loop; -static GThread *sub_thread; -static UDisksClient *client; - -static GHashTable *mounts; -G_LOCK_DEFINE_STATIC(mounts); - -static void udisks_changed(UDisksClient *client, gpointer user_data) -{ - GDBusObjectManager *manager = udisks_client_get_object_manager(client); - GList *objs = g_dbus_object_manager_get_objects(manager); - - G_LOCK(mounts); - - g_debug("udisks changed"); - - g_hash_table_remove_all(mounts); - - for (GList *l = objs; l; l = g_list_next(l)) { - UDisksObject *obj = UDISKS_OBJECT(l->data); - UDisksFilesystem *filesys = udisks_object_get_filesystem(obj); - UDisksBlock *block = udisks_object_get_block(obj); - if (block && filesys) { - const gchar *name = udisks_block_get_id_label(block); - if (!name || strlen(name) == 0) { - name = udisks_block_get_id_uuid(block); - } - - const gchar * const * mpoints = udisks_filesystem_get_mount_points(filesys); - const gchar * mountpoint = mpoints && mpoints[0] ? mpoints[0] : NULL; - - if (name && mountpoint) { - g_debug("%s -> %s", name, mountpoint); - g_hash_table_insert(mounts, g_strdup(name), g_strdup(mountpoint)); - } - } - if (filesys) g_object_unref(filesys); - if (block) g_object_unref(block); - } - - G_UNLOCK(mounts); - - g_list_free_full(objs, g_object_unref); -} - -static gpointer sub_thread_main(gpointer user_data) -{ - GError *error = NULL; - - client = udisks_client_new_sync(NULL, &error); - if (!client) { - g_printerr("Could not connect to UDisks2 service: %s", error->message); - g_error_free(error); - } - - g_signal_connect(client, "changed", - G_CALLBACK(udisks_changed), NULL); - - udisks_changed(client, NULL); - - g_main_loop_run(main_loop); - - return NULL; -} - -static void * volumesfs_init(struct fuse_conn_info *conn) -{ - main_loop = g_main_loop_new(NULL, FALSE); - - mounts = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free); - - sub_thread = g_thread_new("volumesfs-sub-thread", sub_thread_main, NULL); - - return NULL; -} - -static void volumesfs_destroy(void *user_data) -{ - g_main_loop_quit(main_loop); - - g_thread_join(sub_thread); - - g_object_unref(client); - g_hash_table_destroy(mounts); - g_object_unref(main_loop); -} - -static int volumesfs_getattr(const char *path, struct stat *stbuf) -{ - if (strcmp(path, "/") == 0) { - stbuf->st_mode = S_IFDIR | 0755; - stbuf->st_nlink = 2; - } else { - G_LOCK(mounts); - const gchar *mountpoint = g_hash_table_lookup(mounts, path + 1); - G_UNLOCK(mounts); - if (mountpoint) { - stbuf->st_mode = S_IFLNK | 0444; - stbuf->st_nlink = 1; - } else { - return -ENOENT; - } - } - return 0; -} - -static int volumesfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler, - off_t offset, struct fuse_file_info *fi) -{ - if (strcmp(path, "/") != 0) { - return -ENOENT; - } - - G_LOCK(mounts); - - (void) offset; - (void) fi; - - filler(buf, ".", NULL, 0); - filler(buf, "..", NULL, 0); - - GHashTableIter iter; - gchar *name; - g_hash_table_iter_init(&iter, mounts); - while (g_hash_table_iter_next(&iter, (void**)&name, NULL)) { - filler(buf, name, NULL, 0); - } - - G_UNLOCK(mounts); - - return 0; -} - -static int volumesfs_readlink(const char *path, char *buf, size_t size) -{ - if (strcmp(path, "/") == 0) { - return -EINVAL; - } - - G_LOCK(mounts); - gchar *mountpoint = g_hash_table_lookup(mounts, path + 1); - G_UNLOCK(mounts); - - if (mountpoint) { - strcpy(buf, mountpoint); - return 0; - } else { - return -ENOENT; - } -} - -static struct fuse_operations volumesfs_oper = { - .init = volumesfs_init, - .destroy = volumesfs_destroy, - .getattr = volumesfs_getattr, - .readdir = volumesfs_readdir, - .readlink = volumesfs_readlink -}; - -int main(int argc, char *argv[]) -{ - return fuse_main(argc, argv, &volumesfs_oper, NULL); -} -- cgit v1.2.3