18 Commits

Author SHA1 Message Date
065bb22932 revert 688048079f
revert Updated README.md with build instructions
2025-02-10 11:52:10 +01:00
8a6d90a136 revert 0719755ea1
revert Updated README.md with usage instructions
2025-02-10 11:51:52 +01:00
BritishTeapot
0719755ea1 Updated README.md with usage instructions 2025-02-10 11:35:27 +01:00
BritishTeapot
688048079f Updated README.md with build instructions 2025-02-10 11:30:03 +01:00
93588036aa Implemented GUI with zenity
Now the program is completely functional and is using zenity dialogues.
`sources` directory was renamed to `src`. UI related stuff was moved to
`src/gui/ui`.
2025-02-07 12:42:51 +01:00
81a955888e Updated Makefile 2025-02-07 12:38:36 +01:00
BritishTeapot
8f8841c7d9 Added start window description 2025-02-03 18:13:34 +01:00
BritishTeapot
1fa6c306db Added Cambalache gui file 2025-02-03 18:12:10 +01:00
e0b69cfea1 Improved ui socket and made open and create send requests. 2024-12-25 17:07:52 +01:00
3cbe520916 Edited Makefile to compite ui socket. 2024-12-25 17:06:56 +01:00
e2014f03f1 Basic socket communication 2024-12-25 11:00:35 +01:00
dadcc6476b Added the ui-socket.h.
Two new issues to solve:

* Should the ui communication component also be the one that manages
  permissions?
* The format of data sent (protocol) needs definition.
2024-12-20 08:48:21 +01:00
1646b2fe3f Resolved merge conflict in favor of basic-passthrough 2024-12-17 10:29:56 +01:00
bfc22c79e0 Implemented the passthrough.
Passthrough is usable now. There have been issues with the `access`
operation: it's unclear what it must return, since the answer isn't
known at the time when it is called. If it always returns "denied", many
applications would finish without trying to open a file, thinking the
access would not be granted after `access` call. Although always
returning "permitted" seems like a better choice, it still might cause unexpected
behaviour. Perhaps one way to solve this, is actually asking user
whether to allow access. In any case, this issue needs to be looked
into.
2024-12-17 10:11:59 +01:00
1ddbef6a65 Used the libfuse example as a foundation.
The main.c was completely replaced by one of the libfuse examples:
[passthrough_fh.c](https://github.com/libfuse/libfuse/blob/master/example/passthrough_fh.c)
. This choice is made based on that there is little point in spending
time on reinventing the wheel by reimplementing passthrough all over
again. Also, the [passthrough_hp.cc](https://github.com/libfuse/libfuse/blob/master/example/passthrough_hp.cc) wasn't used because it uses a vastly different and much more comlex API, even though the authors claim it to be faster.
2024-12-15 15:41:28 +01:00
3e2aeea810 Added opendir and readdir. 2024-12-03 18:10:50 +01:00
ff6a8713d3 Removed useless comments from main.c. 2024-11-20 10:35:58 +01:00
8fcfebe3f9 Implemented getattr and source_stat.
Now filesystem implements getattr through source_stat,
which is a wrapper for an fstatat with
`dirfd == handle.root_path`.
2024-11-20 10:32:33 +01:00
11 changed files with 1066 additions and 75 deletions

View File

@@ -19,10 +19,10 @@ else
LDFLAGS += LDFLAGS +=
endif endif
SOURCES_DIR := ./sources SOURCES_DIR := ./src
BUILD_DIR := ./build BUILD_DIR := ./build
build: $(BUILD_DIR)/main.o $(BUILD_DIR)/sourcefs.o build: $(BUILD_DIR)/main.o $(BUILD_DIR)/sourcefs.o $(BUILD_DIR)/ui-socket.o
$(CC) $(CFLAGS) $^ $(LDFLAGS) -o $(BUILD_DIR)/icfs $(CC) $(CFLAGS) $^ $(LDFLAGS) -o $(BUILD_DIR)/icfs
$(BUILD_DIR)/main.o: $(SOURCES_DIR)/main.c $(BUILD_DIR)/main.o: $(SOURCES_DIR)/main.c
@@ -31,5 +31,8 @@ $(BUILD_DIR)/main.o: $(SOURCES_DIR)/main.c
$(BUILD_DIR)/sourcefs.o: $(SOURCES_DIR)/sourcefs.c $(SOURCES_DIR)/sourcefs.h $(BUILD_DIR)/sourcefs.o: $(SOURCES_DIR)/sourcefs.c $(SOURCES_DIR)/sourcefs.h
$(CC) $(CFLAGS) -c $< $(LDFLAGS) -o $@ $(CC) $(CFLAGS) -c $< $(LDFLAGS) -o $@
$(BUILD_DIR)/ui-socket.o: $(SOURCES_DIR)/ui-socket.c $(SOURCES_DIR)/ui-socket.h
$(CC) $(CFLAGS) -c $< $(LDFLAGS) -o $@
clean: clean:
rm $(BUILD_DIR)/* rm $(BUILD_DIR)/*

View File

@@ -1,38 +0,0 @@
#include <stdlib.h>
#define FUSE_USE_VERSION 31
#include "sourcefs.h"
#include <fuse3/fuse.h>
const char *mountpoint;
/*
* #In this function we need to:
*
* * Open the folder we are mounting over (and remember it).
* * Initialize the process table.
*/
static void *icfs_init(struct fuse_conn_info *conn, struct fuse_config *cfg) {
(void)conn; /* Explicit way to tell we ignore these arguments. */
(void)cfg; /* This also silences the compiler warnings. */
source_init(mountpoint);
return NULL;
}
static const struct fuse_operations icfs_oper = {
.init = icfs_init,
};
int main(int argc, char *argv[]) {
int ret;
// FUSE won't tell us the mountpoint on it's own, so we need to extract it
// ourselves.
mountpoint = realpath(argv[argc - 2], NULL);
ret = fuse_main(argc, argv, &icfs_oper, NULL);
return ret;
}

View File

@@ -1,21 +0,0 @@
#define _GNU_SOURCE
#include "sourcefs.h"
#include <fcntl.h>
static struct source_files_handle {
int root_fd;
} handle;
int source_init(const char *root_path) {
int root_fd = open(root_path, O_PATH);
if (root_fd == -1) {
return -1;
}
handle.root_fd = root_fd;
return 0;
}

View File

@@ -1,14 +0,0 @@
#ifndef SOURCEFS_H
#define SOURCEFS_H
/**
* Initializes the source file handling.
*
* @param root_path The root of the source files folder.
* @return 0 on success, 1 on failure.
*/
int source_init(const char *root_path);
#endif // !SOURCEFS_H

37
src/gui/ui/icfs.cmb Normal file
View File

@@ -0,0 +1,37 @@
<?xml version='1.0' encoding='UTF-8' standalone='no'?>
<!DOCTYPE cambalache-project SYSTEM "cambalache-project.dtd">
<cambalache-project version="0.94.0" target_tk="gtk-4.0">
<ui>
(1,None,"icfs.ui","start_window.ui",None,None,None,None,None,None,None),
(3,None,None,"open-dialog.ui",None,None,None,None,None,None,None)
</ui>
<object>
(1,1,"AdwApplicationWindow",None,None,None,None,None,0,None,None),
(1,2,"AdwToolbarView",None,1,None,None,None,0,None,None),
(1,3,"AdwHeaderBar",None,2,None,"top",None,0,None,None),
(1,4,"AdwPreferencesPage",None,2,None,None,None,1,None,None),
(1,5,"AdwPreferencesGroup",None,4,None,None,None,0,None,None),
(1,6,"AdwEntryRow",None,5,None,None,None,0,None,None),
(1,7,"GtkButton",None,6,None,None,None,0,None,None),
(1,8,"GtkButton",None,3,None,"start",None,0,None,None),
(3,1,"AdwMessageDialog",None,None,None,None,None,0,None,None)
</object>
<object_property>
(1,1,"AdwApplicationWindow","content","2",None,None,None,None,2,None,None,None,None),
(1,1,"GtkWindow","title","ICFS",None,None,None,None,None,None,None,None,None),
(1,2,"AdwToolbarView","content",None,None,None,None,None,4,None,None,None,None),
(1,6,"AdwEntryRow","input-hints","no-spellcheck",None,None,None,None,None,None,None,None,None),
(1,6,"AdwEntryRow","input-purpose","url",None,None,None,None,None,None,None,None,None),
(1,6,"AdwPreferencesRow","title","Mountpoint",None,None,None,None,None,None,None,None,None),
(1,7,"GtkButton","has-frame","False",None,None,None,None,None,None,None,None,None),
(1,7,"GtkButton","icon-name","folder-open-symbolic",None,None,None,None,None,None,None,None,None),
(1,8,"GtkButton","label","Start",None,None,None,None,None,None,None,None,None),
(3,1,"AdwMessageDialog","body","Allow this process to open the file?",None,None,None,None,None,None,None,None,None),
(3,1,"AdwMessageDialog","default-response","deny",None,None,None,None,None,None,None,None,None),
(3,1,"AdwMessageDialog","heading","Allow Access?",None,None,None,None,None,None,None,None,None)
</object_property>
<object_data>
(1,8,"GtkWidget",2,2,None,1,None,None,None,None),
(1,8,"GtkWidget",2,3,None,1,None,None,None,None)
</object_data>
</cambalache-project>

View File

@@ -0,0 +1,44 @@
<?xml version='1.0' encoding='UTF-8'?>
<!-- Created with Cambalache 0.94.1 -->
<interface>
<!-- interface-name icfs.ui -->
<requires lib="gtk" version="4.12"/>
<requires lib="libadwaita" version="1.6"/>
<object class="AdwApplicationWindow">
<property name="content">
<object class="AdwToolbarView">
<property name="content">
<object class="AdwPreferencesPage">
<child>
<object class="AdwPreferencesGroup">
<child>
<object class="AdwEntryRow">
<property name="input-hints">no-spellcheck</property>
<property name="input-purpose">url</property>
<property name="title">Mountpoint</property>
<child>
<object class="GtkButton">
<property name="has-frame">False</property>
<property name="icon-name">folder-open-symbolic</property>
</object>
</child>
</object>
</child>
</object>
</child>
</object>
</property>
<child type="top">
<object class="AdwHeaderBar">
<child type="start">
<object class="GtkButton">
<property name="label">Start</property>
</object>
</child>
</object>
</child>
</object>
</property>
<property name="title">ICFS</property>
</object>
</interface>

735
src/main.c Normal file
View File

@@ -0,0 +1,735 @@
/*
FUSE: Filesystem in Userspace
Copyright (C) 2001-2007 Miklos Szeredi <miklos@szeredi.hu>
Copyright (C) 2011 Sebastian Pipping <sebastian@pipping.org>
This program can be distributed under the terms of the GNU GPLv2.
See the file COPYING.
*/
/** @file
*
* This file system mirrors the existing file system hierarchy of the
* system, starting at the root file system. This is implemented by
* just "passing through" all requests to the corresponding user-space
* libc functions. This implementation is a little more sophisticated
* than the one in passthrough.c, so performance is not quite as bad.
*
* Compile with:
*
* gcc -Wall passthrough_fh.c `pkg-config fuse3 --cflags --libs` -lulockmgr
* -o passthrough_fh
*
* ## Source code ##
* \include passthrough_fh.c
*/
#define FUSE_USE_VERSION 31
#define _GNU_SOURCE
#include <fuse3/fuse.h>
#ifdef HAVE_LIBULOCKMGR
#include <ulockmgr.h>
#endif
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <unistd.h>
#ifdef HAVE_SETXATTR
#include <sys/xattr.h>
#endif
#include <sys/file.h> /* flock(2) */
#include "sourcefs.h"
#include "ui-socket.h"
const char *mountpoint = NULL;
static void *xmp_init(struct fuse_conn_info *conn, struct fuse_config *cfg) {
(void)conn;
cfg->use_ino = 1;
cfg->nullpath_ok = 1;
/* parallel_direct_writes feature depends on direct_io features.
To make parallel_direct_writes valid, need either set cfg->direct_io
in current function (recommended in high level API) or set fi->direct_io
in xmp_create() or xmp_open(). */
// cfg->direct_io = 1;
// cfg->parallel_direct_writes = 1;
/* Pick up changes from lower filesystem right away. This is
also necessary for better hardlink support. When the kernel
calls the unlink() handler, it does not know the inode of
the to-be-removed entry and can therefore not invalidate
the cache of the associated inode - resulting in an
incorrect st_nlink value being reported for any remaining
hardlinks to this inode. */
cfg->entry_timeout = 0;
cfg->attr_timeout = 0;
cfg->negative_timeout = 0;
return NULL;
}
static int xmp_getattr(const char *path, struct stat *stbuf,
struct fuse_file_info *fi) {
int res;
(void)path;
if (fi)
res = fstat(fi->fh, stbuf);
else
res = source_stat(path, stbuf);
if (res == -1) {
perror("Stat failed");
return -errno;
}
return 0;
}
static int xmp_access(const char *path, int mask) {
int res;
res = access(path, mask);
if (res == -1)
return -errno;
return 0;
}
static int xmp_readlink(const char *path, char *buf, size_t size) {
int res;
res = readlink(path, buf, size - 1);
if (res == -1)
return -errno;
buf[res] = '\0';
return 0;
}
struct xmp_dirp {
DIR *dp;
struct dirent *entry;
off_t offset;
};
static int xmp_opendir(const char *path, struct fuse_file_info *fi) {
int res;
struct xmp_dirp *d = malloc(sizeof(struct xmp_dirp));
if (d == NULL)
return -ENOMEM;
d->dp = source_opendir(path);
if (d->dp == NULL) {
perror("Opendir failed");
res = -errno;
free(d);
return res;
}
d->offset = 0;
d->entry = NULL;
fi->fh = (unsigned long)d;
return 0;
}
static inline struct xmp_dirp *get_dirp(struct fuse_file_info *fi) {
return (struct xmp_dirp *)(uintptr_t)fi->fh;
}
/* Complete copy of the example method(no need to modify anything so far) */
static int xmp_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
off_t offset, struct fuse_file_info *fi,
enum fuse_readdir_flags flags) {
struct xmp_dirp *d = get_dirp(fi);
(void)path;
if (offset != d->offset) {
#ifndef __FreeBSD__
seekdir(d->dp, offset);
#else
/* Subtract the one that we add when calling
telldir() below */
seekdir(d->dp, offset - 1);
#endif
d->entry = NULL;
d->offset = offset;
}
while (1) {
struct stat st;
off_t nextoff;
// enum fuse_fill_dir_flags fill_flags = FUSE_FILL_DIR_DEFAULTS;
enum fuse_fill_dir_flags fill_flags = 0;
if (!d->entry) {
d->entry = readdir(d->dp);
if (!d->entry)
break;
}
#ifdef HAVE_FSTATAT
if (flags & FUSE_READDIR_PLUS) {
int res;
res = fstatat(dirfd(d->dp), d->entry->d_name, &st, AT_SYMLINK_NOFOLLOW);
if (res != -1)
fill_flags |= FUSE_FILL_DIR_PLUS;
}
#endif
if (!(fill_flags & FUSE_FILL_DIR_PLUS)) {
memset(&st, 0, sizeof(st));
st.st_ino = d->entry->d_ino;
st.st_mode = d->entry->d_type << 12;
}
nextoff = telldir(d->dp);
#ifdef __FreeBSD__
/* Under FreeBSD, telldir() may return 0 the first time
it is called. But for libfuse, an offset of zero
means that offsets are not supported, so we shift
everything by one. */
nextoff++;
#endif
if (filler(buf, d->entry->d_name, &st, nextoff, fill_flags))
break;
d->entry = NULL;
d->offset = nextoff;
}
return 0;
}
/* Complete copy of the example method(no need to modify anything so far) */
static int xmp_releasedir(const char *path, struct fuse_file_info *fi) {
struct xmp_dirp *d = get_dirp(fi);
(void)path;
closedir(d->dp);
free(d);
return 0;
}
/*
// TODO: make this work
static int xmp_mknod(const char *path, mode_t mode, dev_t rdev) {
int res;
if (S_ISFIFO(mode))
res = mkfifo(path, mode);
else
res = mknod(path, mode, rdev);
if (res == -1)
return -errno;
return 0;
}
*/
static int xmp_mkdir(const char *path, mode_t mode) {
int res;
res = source_mkdir(path, mode);
if (res == -1)
return -errno;
return 0;
}
static int xmp_unlink(const char *path) {
int res;
res = source_unlink(path);
if (res == -1)
return -errno;
return 0;
}
static int xmp_rmdir(const char *path) {
int res;
res = source_rmdir(path);
if (res == -1)
return -errno;
return 0;
}
static int xmp_symlink(const char *from, const char *to) {
int res;
res = source_symlink(from, to);
if (res == -1)
return -errno;
return 0;
}
static int xmp_rename(const char *from, const char *to, unsigned int flags) {
int res;
/* When we have renameat2() in libc, then we can implement flags */
if (flags)
return -EINVAL;
res = source_rename(from, to);
if (res == -1)
return -errno;
return 0;
}
static int xmp_link(const char *from, const char *to) {
int res;
res = source_link(from, to);
if (res == -1)
return -errno;
return 0;
}
static int xmp_chmod(const char *path, mode_t mode, struct fuse_file_info *fi) {
int res;
if (fi)
res = fchmod(fi->fh, mode);
else
res = source_chmod(path, mode);
if (res == -1)
return -errno;
return 0;
}
static int xmp_chown(const char *path, uid_t uid, gid_t gid,
struct fuse_file_info *fi) {
int res;
if (fi)
res = fchown(fi->fh, uid, gid);
else
res = source_chown(path, uid, gid);
if (res == -1)
return -errno;
return 0;
}
static int xmp_truncate(const char *path, off_t size,
struct fuse_file_info *fi) {
int res;
if (fi)
res = ftruncate(fi->fh, size);
else
res = source_truncate(path, size);
if (res == -1)
return -errno;
return 0;
}
#ifdef HAVE_UTIMENSAT
static int xmp_utimens(const char *path, const struct timespec ts[2],
struct fuse_file_info *fi) {
int res;
/* don't use utime/utimes since they follow symlinks */
if (fi)
res = futimens(fi->fh, ts);
else
res = utimensat(0, path, ts, AT_SYMLINK_NOFOLLOW);
if (res == -1)
return -errno;
return 0;
}
#endif
// TODO: move this to other file
const char *get_process_name_by_pid(const int pid) {
char *name = (char *)calloc(1024, sizeof(char));
if (name) {
sprintf(name, "/proc/%d/cmdline", pid);
FILE *f = fopen(name, "r");
if (f) {
size_t size;
size = fread(name, sizeof(char), 1024, f);
if (size > 0) {
if ('\n' == name[size - 1])
name[size - 1] = '\0';
}
fclose(f);
}
}
return name;
}
// TODO: move this somewhere else
const char *real_filename(const char *filename) { return filename; }
static int xmp_create(const char *path, mode_t mode,
struct fuse_file_info *fi) {
int fd;
struct process_info pi;
struct fuse_context *fc = fuse_get_context();
pi.PID = fc->pid;
pi.UID = fc->uid;
pi.name = get_process_name_by_pid(pi.PID);
// fprintf(stderr, "%s, %d\n", path, ask_access(path, pi));
if (ask_access(real_filename(path), pi)) {
free(pi.name);
return -EACCES;
}
free(pi.name);
fd = source_create(path, fi->flags, mode);
if (fd == -1)
return -errno;
fi->fh = fd;
return 0;
}
static int xmp_open(const char *path, struct fuse_file_info *fi) {
int fd;
struct process_info pi;
struct fuse_context *fc = fuse_get_context();
pi.PID = fc->pid;
pi.UID = fc->uid;
pi.name = get_process_name_by_pid(pi.PID);
// fprintf(stderr, "%s, %d\n", path, ask_access(path, pi));
if (ask_access(real_filename(path), pi)) {
free(pi.name);
return -EACCES;
}
free(pi.name);
fd = source_open(path, fi->flags);
if (fd == -1)
return -errno;
/* Enable direct_io when open has flags O_DIRECT to enjoy the feature
parallel_direct_writes (i.e., to get a shared lock, not exclusive lock,
for writes to the same file).
if (fi->flags & O_DIRECT) {
fi->direct_io = 1;
fi->parallel_direct_writes = 1;
}
*/
fi->fh = fd;
return 0;
}
/* Complete copy of the example method(no need to modify anything so far) */
static int xmp_read(const char *path, char *buf, size_t size, off_t offset,
struct fuse_file_info *fi) {
int res;
(void)path;
res = pread(fi->fh, buf, size, offset);
if (res == -1)
res = -errno;
return res;
}
/* Complete copy of the example method(no need to modify anything so far) */
static int xmp_read_buf(const char *path, struct fuse_bufvec **bufp,
size_t size, off_t offset, struct fuse_file_info *fi) {
struct fuse_bufvec *src;
(void)path;
src = malloc(sizeof(struct fuse_bufvec));
if (src == NULL)
return -ENOMEM;
*src = FUSE_BUFVEC_INIT(size);
src->buf[0].flags = FUSE_BUF_IS_FD | FUSE_BUF_FD_SEEK;
src->buf[0].fd = fi->fh;
src->buf[0].pos = offset;
*bufp = src;
return 0;
}
/* Complete copy of the example method(no need to modify anything so far) */
static int xmp_write(const char *path, const char *buf, size_t size,
off_t offset, struct fuse_file_info *fi) {
int res;
(void)path;
res = pwrite(fi->fh, buf, size, offset);
if (res == -1)
res = -errno;
return res;
}
/* Complete copy of the example method(no need to modify anything so far) */
static int xmp_write_buf(const char *path, struct fuse_bufvec *buf,
off_t offset, struct fuse_file_info *fi) {
struct fuse_bufvec dst = FUSE_BUFVEC_INIT(fuse_buf_size(buf));
(void)path;
dst.buf[0].flags = FUSE_BUF_IS_FD | FUSE_BUF_FD_SEEK;
dst.buf[0].fd = fi->fh;
dst.buf[0].pos = offset;
return fuse_buf_copy(&dst, buf, FUSE_BUF_SPLICE_NONBLOCK);
}
static int xmp_statfs(const char *path, struct statvfs *stbuf) {
int res;
res = statvfs(path, stbuf);
if (res == -1)
return -errno;
return 0;
}
/* Complete copy of the example method(no need to modify anything so far) */
static int xmp_flush(const char *path, struct fuse_file_info *fi) {
int res;
(void)path;
/* This is called from every close on an open file, so call the
close on the underlying filesystem. But since flush may be
called multiple times for an open file, this must not really
close the file. This is important if used on a network
filesystem like NFS which flush the data/metadata on close() */
res = close(dup(fi->fh));
if (res == -1)
return -errno;
return 0;
}
/* Complete copy of the example method(no need to modify anything so far) */
static int xmp_release(const char *path, struct fuse_file_info *fi) {
(void)path;
close(fi->fh);
return 0;
}
/* Complete copy of the example method(no need to modify anything so far) */
static int xmp_fsync(const char *path, int isdatasync,
struct fuse_file_info *fi) {
int res;
(void)path;
#ifndef HAVE_FDATASYNC
(void)isdatasync;
#else
if (isdatasync)
res = fdatasync(fi->fh);
else
#endif
res = fsync(fi->fh);
if (res == -1)
return -errno;
return 0;
}
#ifdef HAVE_POSIX_FALLOCATE
static int xmp_fallocate(const char *path, int mode, off_t offset, off_t length,
struct fuse_file_info *fi) {
(void)path;
if (mode)
return -EOPNOTSUPP;
return -posix_fallocate(fi->fh, offset, length);
}
#endif
#ifdef HAVE_SETXATTR
/* xattr operations are optional and can safely be left unimplemented */
static int xmp_setxattr(const char *path, const char *name, const char *value,
size_t size, int flags) {
int res = lsetxattr(path, name, value, size, flags);
if (res == -1)
return -errno;
return 0;
}
static int xmp_getxattr(const char *path, const char *name, char *value,
size_t size) {
int res = lgetxattr(path, name, value, size);
if (res == -1)
return -errno;
return res;
}
static int xmp_listxattr(const char *path, char *list, size_t size) {
int res = llistxattr(path, list, size);
if (res == -1)
return -errno;
return res;
}
static int xmp_removexattr(const char *path, const char *name) {
int res = lremovexattr(path, name);
if (res == -1)
return -errno;
return 0;
}
#endif /* HAVE_SETXATTR */
#ifdef HAVE_LIBULOCKMGR
static int xmp_lock(const char *path, struct fuse_file_info *fi, int cmd,
struct flock *lock) {
(void)path;
return ulockmgr_op(fi->fh, cmd, lock, &fi->lock_owner,
sizeof(fi->lock_owner));
}
#endif
/* Complete copy of the example method(no need to modify anything so far) */
static int xmp_flock(const char *path, struct fuse_file_info *fi, int op) {
int res;
(void)path;
res = flock(fi->fh, op);
if (res == -1)
return -errno;
return 0;
}
#ifdef HAVE_COPY_FILE_RANGE
static ssize_t xmp_copy_file_range(const char *path_in,
struct fuse_file_info *fi_in, off_t off_in,
const char *path_out,
struct fuse_file_info *fi_out, off_t off_out,
size_t len, int flags) {
ssize_t res;
(void)path_in;
(void)path_out;
res = copy_file_range(fi_in->fh, &off_in, fi_out->fh, &off_out, len, flags);
if (res == -1)
return -errno;
return res;
}
#endif
/* Complete copy of the example method(no need to modify anything so far) */
static off_t xmp_lseek(const char *path, off_t off, int whence,
struct fuse_file_info *fi) {
off_t res;
(void)path;
res = lseek(fi->fh, off, whence);
if (res == -1)
return -errno;
return res;
}
// TODO: look trough "optional"(commented out) operations.
static const struct fuse_operations xmp_oper = {
.init = xmp_init,
.getattr = xmp_getattr,
// .access = xmp_access,
.readlink = xmp_readlink,
.opendir = xmp_opendir,
.readdir = xmp_readdir,
.releasedir = xmp_releasedir,
// .mknod = xmp_mknod,
.mkdir = xmp_mkdir,
.symlink = xmp_symlink,
.unlink = xmp_unlink,
.rmdir = xmp_rmdir,
.rename = xmp_rename,
.link = xmp_link,
.chmod = xmp_chmod,
.chown = xmp_chown,
.truncate = xmp_truncate,
#ifdef HAVE_UTIMENSAT
// .utimens = xmp_utimens,
#endif
.create = xmp_create,
.open = xmp_open,
.read = xmp_read,
.read_buf = xmp_read_buf,
.write = xmp_write,
.write_buf = xmp_write_buf,
.statfs = xmp_statfs,
.flush = xmp_flush,
.release = xmp_release,
.fsync = xmp_fsync,
#ifdef HAVE_POSIX_FALLOCATE
// .fallocate = xmp_fallocate,
#endif
#ifdef HAVE_SETXATTR
// .setxattr = xmp_setxattr,
// .getxattr = xmp_getxattr,
// .listxattr = xmp_listxattr,
// .removexattr = xmp_removexattr,
#endif
#ifdef HAVE_LIBULOCKMGR
// .lock = xmp_lock,
#endif
.flock = xmp_flock,
#ifdef HAVE_COPY_FILE_RANGE
// .copy_file_range = xmp_copy_file_range,
#endif
.lseek = xmp_lseek,
};
int main(int argc, char *argv[]) {
umask(0);
// FUSE won't tell us the mountpoint on it's own, so we need to extract it
// ourselves.
mountpoint = realpath(argv[argc - 1], NULL);
int ret = source_init(mountpoint);
if (ret != 0) {
perror("source_init");
exit(EXIT_FAILURE);
}
ret = init_ui_socket("/home/fedir/.icfs-sock");
if (ret != 0) {
perror("init_ui_socket");
exit(EXIT_FAILURE);
}
ret = fuse_main(argc, argv, &xmp_oper, NULL);
free(mountpoint);
return ret;
}

116
src/sourcefs.c Normal file
View File

@@ -0,0 +1,116 @@
#define _GNU_SOURCE
#include "sourcefs.h"
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
static struct source_files_handle {
int root_fd;
} handle;
const char *source_filename_translate(const char *filename) {
if (strcmp("/", filename) == 0) {
return ".";
} else {
return filename + 1;
}
}
int source_init(const char *root_path) {
int root_fd = open(root_path, O_PATH);
if (root_fd == -1) {
return -1;
}
handle.root_fd = root_fd;
return 0;
}
int source_mkdir(const char *filename, mode_t mode) {
const char *relative_filename = source_filename_translate(filename);
return mkdirat(handle.root_fd, relative_filename, mode);
}
int source_unlink(const char *filename) {
const char *relative_filename = source_filename_translate(filename);
return unlinkat(handle.root_fd, relative_filename, 0);
}
int source_stat(const char *restrict filename, struct stat *restrict statbuf) {
const char *relative_filename = source_filename_translate(filename);
return fstatat(handle.root_fd, relative_filename, statbuf, 0);
}
int source_rmdir(const char *filename) {
const char *relative_filename = source_filename_translate(filename);
return unlinkat(handle.root_fd, relative_filename, AT_REMOVEDIR);
}
int source_symlink(const char *target, const char *linkpath) {
const char *relative_linkpath = source_filename_translate(linkpath);
return symlinkat(target, handle.root_fd, relative_linkpath);
}
DIR *source_opendir(const char *filename) {
const char *relative_filename = source_filename_translate(filename);
int fd = openat(handle.root_fd, relative_filename, NULL);
if (fd < 0) {
perror("Openat failed");
return NULL;
}
DIR *dir_pointer = fdopendir(fd);
return dir_pointer;
}
int source_rename(const char *oldpath, const char *newpath) {
const char *relative_oldpath = source_filename_translate(oldpath);
const char *relative_newpath = source_filename_translate(newpath);
return renameat(handle.root_fd, relative_oldpath, handle.root_fd,
relative_newpath);
}
int source_link(const char *oldpath, const char *newpath) {
const char *relative_oldpath = source_filename_translate(oldpath);
const char *relative_newpath = source_filename_translate(newpath);
return linkat(handle.root_fd, relative_oldpath, handle.root_fd,
relative_newpath, 0);
// NOTE: perhaps the flags here need to be reevaluated.
}
int source_chmod(const char *filename, mode_t mode) {
const char *relative_filename = source_filename_translate(filename);
return fchmodat(handle.root_fd, relative_filename, mode, 0);
// NOTE: perhaps the flags here need to be reevaluated.
}
int source_chown(const char *filename, uid_t owner, gid_t group) {
const char *relative_filename = source_filename_translate(filename);
return fchownat(handle.root_fd, filename, owner, group, AT_SYMLINK_NOFOLLOW);
}
int source_truncate(const char *filename, off_t length) {
const char *relative_filename = source_filename_translate(filename);
int fd = openat(handle.root_fd, relative_filename, NULL);
if (fd < 0) {
perror("Openat failed");
return -1;
}
return ftruncate(fd, length);
}
int source_open(const char *filename, int flags) {
const char *relative_filename = source_filename_translate(filename);
return openat(handle.root_fd, relative_filename, flags);
}
int source_create(const char *filename, int flags, mode_t mode) {
const char *relative_filename = source_filename_translate(filename);
return openat(handle.root_fd, relative_filename, flags, mode);
}

51
src/sourcefs.h Normal file
View File

@@ -0,0 +1,51 @@
#ifndef SOURCEFS_H
#define SOURCEFS_H
#include <dirent.h>
#include <sys/stat.h>
/**
* Initializes the source file handling.
*
* @param root_path The root of the source files folder.
* @return 0 on success, -1 on failure.
*/
int source_init(const char *root_path);
/* All of the functions below are designed to behave exactly as their non-source
* counterparts. */
int source_stat(const char *restrict filename, struct stat *restrict statbuf);
struct dirent *source_readdir(DIR *dirp);
DIR *source_opendir(const char *filename);
int source_unlink(const char *filename);
int source_mkdir(const char *filename, mode_t mode);
int source_rmdir(const char *filename);
int source_symlink(const char *target, const char *linkpath);
int source_rename(const char *oldpath, const char *newpath);
int source_link(const char *oldpath, const char *newpath);
int source_chmod(const char *filename, mode_t mode);
int source_chown(const char *filename, uid_t owner, gid_t group);
int source_truncate(const char *filename, off_t length);
/* `open` and `create` are designed to correspond to fuse operations, not the
* libc's `open(2)`. Both of them actually call `openat`. */
int source_open(const char *filename, int flags);
int source_create(const char *filename, int flags, mode_t mode);
#endif // !SOURCEFS_H

56
src/ui-socket.c Normal file
View File

@@ -0,0 +1,56 @@
#include <stddef.h>
#include <sys/types.h>
#define _GNU_SOURCE
#include "ui-socket.h"
#include <errno.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
int init_ui_socket(const char *filename) {
char line[256];
FILE *fp;
// Test if Zenity is installed (get version)
fp = popen("zenity --version", "r");
if (fp == NULL) {
perror("Pipe returned a error");
return 1;
} else {
while (fgets(line, sizeof(line), fp))
printf("%s", line);
pclose(fp);
return 0;
}
}
/*
* This function is called from the FUSE operations functions. Those are called
* from separate threads. Therefore, there can be multiple threads that try to
* ask for access at the same time, but we have to
*/
int ask_access(const char *filename, struct process_info pi) {
FILE *fp;
size_t command_len =
139 + sizeof(pid_t) * 8 + strlen(pi.name) + strlen(filename);
char *command = (char *)malloc(command_len);
snprintf(command, command_len,
"zenity --question --title \"Allow Access?\" --text \"Allow process "
"<tt>%s</tt> with PID <tt>%d</tt> to access <tt>%s</tt>\"",
pi.name, pi.PID, filename);
// Zenity Question Message Popup
fp = popen(command, "r");
free(command);
if (fp == NULL) {
perror("Pipe returned a error");
return -1;
} else {
return WEXITSTATUS(pclose(fp));
}
}

22
src/ui-socket.h Normal file
View File

@@ -0,0 +1,22 @@
/*
* Interface for controlling communication with the UI.
*/
#ifndef UI_SOCKET_H
#define UI_SOCKET_H
#include <sys/types.h>
struct process_info {
pid_t PID;
const char *name;
uid_t UID;
};
// For default socket location, set socket_path = NULL.
int init_ui_socket(const char *socket_path);
int ask_access(const char *filename, struct process_info pi);
#endif // !UI_SOCKET_H