Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define FUSE_USE_VERSION 30
- #include <fuse.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <errno.h>
- #include <fcntl.h>
- #include <dirent.h>
- #include <unistd.h>
- #ifndef PATH_MAX
- #define PATH_MAX 4096
- #endif
- static const char *basedir;
- static char mountpoint[PATH_MAX];
- static int simplefs_getattr(const char *path, struct stat *stbuf)
- {
- int res = 0;
- char full_path[PATH_MAX];
- snprintf(full_path, sizeof(full_path), "%s%s", basedir, path);
- res = lstat(full_path, stbuf);
- if (res == -1)
- return -errno;
- return 0;
- }
- static int simplefs_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
- off_t offset, struct fuse_file_info *fi)
- {
- DIR *dp;
- struct dirent *de;
- (void)offset;
- (void)fi;
- char full_path[PATH_MAX];
- snprintf(full_path, sizeof(full_path), "%s%s", basedir, path);
- dp = opendir(full_path);
- if (dp == NULL)
- return -errno;
- filler(buf, ".", NULL, 0);
- filler(buf, "..", NULL, 0);
- while ((de = readdir(dp)) != NULL) {
- filler(buf, de->d_name, NULL, 0);
- }
- closedir(dp);
- return 0;
- }
- static int simplefs_read(const char *path, char *buf, size_t size, off_t offset,
- struct fuse_file_info *fi)
- {
- int fd;
- int res;
- char full_path[PATH_MAX];
- snprintf(full_path, sizeof(full_path), "%s%s", basedir, path);
- (void)fi;
- fd = open(full_path, O_RDONLY);
- if (fd == -1)
- return -errno;
- res = pread(fd, buf, size, offset);
- if (res == -1)
- res = -errno;
- close(fd);
- return res;
- }
- static int simplefs_write(const char *path, const char *buf, size_t size,
- off_t offset, struct fuse_file_info *fi)
- {
- int fd;
- int res;
- char full_path[PATH_MAX];
- snprintf(full_path, sizeof(full_path), "%s%s", basedir, path);
- (void)fi;
- fd = open(full_path, O_WRONLY);
- if (fd == -1)
- return -errno;
- res = pwrite(fd, buf, size, offset);
- if (res == -1)
- res = -errno;
- close(fd);
- return res;
- }
- static int simplefs_mkdir(const char *path, mode_t mode)
- {
- int res;
- char full_path[PATH_MAX];
- snprintf(full_path, sizeof(full_path), "%s%s", basedir, path);
- res = mkdir(full_path, mode);
- if (res == -1)
- return -errno;
- return 0;
- }
- static int simplefs_rmdir(const char *path)
- {
- int res;
- char full_path[PATH_MAX];
- snprintf(full_path, sizeof(full_path), "%s%s", basedir, path);
- res = rmdir(full_path);
- if (res == -1)
- return -errno;
- return 0;
- }
- static int simplefs_unlink(const char *path)
- {
- int res;
- char full_path[PATH_MAX];
- snprintf(full_path, sizeof(full_path), "%s%s", basedir, path);
- res = unlink(full_path);
- if (res == -1)
- return -errno;
- return 0;
- }
- static struct fuse_operations simplefs_operations = {
- .getattr = simplefs_getattr,
- .readdir = simplefs_readdir,
- .read = simplefs_read,
- .write = simplefs_write,
- .mkdir = simplefs_mkdir,
- .rmdir = simplefs_rmdir,
- .unlink = simplefs_unlink,
- };
- int main(int argc, char *argv[])
- {
- if (argc != 3) {
- fprintf(stderr, "Usage: %s <mountpoint> <basedir>\n", argv[0]);
- return EXIT_FAILURE;
- }
- basedir = realpath(argv[2], NULL);
- if (basedir == NULL) {
- perror("Error getting realpath");
- return EXIT_FAILURE;
- }
- strcpy(mountpoint, argv[1]);
- strcat(mountpoint, "1");
- return fuse_main(argc - 1, argv + 1, &simplefs_operations, NULL);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement