Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define FUSE_USE_VERSION 28
- #include <fuse.h>
- #include <stdio.h>
- #include <string.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <dirent.h>
- #include <errno.h>
- #include <sys/time.h>
- static const char *dirpath = "/home/almond/Documents";
- void writelog(const char *path)
- {
- char *filename = strrchr(path, '/') + 1;
- char *extension = strrchr(filename, '.');
- char text[100];
- time_t now = time(NULL);
- struct tm *t = localtime(&now);
- strftime(text, sizeof(text) - 1, "%H_%M_%S:%d-%m-%Y", t);
- FILE *logfile = fopen("log.txt", "a");
- if (extension != NULL)
- {
- *extension = '\0';
- fprintf(logfile, "%s.%s.%s\n", filename, text, extension + 1);
- }
- else
- fprintf(logfile, "%s.%s.unknown\n", filename, text);
- fclose(logfile);
- }
- static int xmp_getattr(const char *path, struct stat *stbuf)
- {
- int res;
- char fpath[1000];
- sprintf(fpath, "%s%s", dirpath, path);
- res = lstat(fpath, stbuf);
- if (res == -1)
- return -errno;
- return 0;
- }
- static int xmp_readdir(const char *path, void *buf, fuse_fill_dir_t filler, off_t offset, struct fuse_file_info *fi)
- {
- char fpath[1000];
- if (strcmp(path, "/") == 0)
- {
- path = dirpath;
- sprintf(fpath, "%s", path);
- }
- else
- sprintf(fpath, "%s%s", dirpath, path);
- int res = 0;
- DIR *dp;
- struct dirent *de;
- (void)offset;
- (void)fi;
- dp = opendir(fpath);
- if (dp == NULL)
- return -errno;
- while ((de = readdir(dp)) != NULL)
- {
- struct stat st;
- memset(&st, 0, sizeof(st));
- st.st_ino = de->d_ino;
- st.st_mode = de->d_type << 12;
- res = (filler(buf, de->d_name, &st, 0));
- if (res != 0)
- break;
- }
- closedir(dp);
- return 0;
- }
- static int xmp_read(const char *path, char *buf, size_t size, off_t offset, struct fuse_file_info *fi)
- {
- char fpath[1000];
- if (strcmp(path, "/") == 0)
- {
- path = dirpath;
- sprintf(fpath, "%s", path);
- }
- else
- sprintf(fpath, "%s%s", dirpath, path);
- int res = 0;
- int fd = 0;
- (void)fi;
- fd = open(fpath, O_RDONLY);
- if (fd == -1)
- return -errno;
- res = pread(fd, buf, size, offset);
- if (res == -1)
- res = -errno;
- close(fd);
- return res;
- }
- static int xmp_mknod(const char *path, mode_t mode, dev_t rdev)
- {
- char fpath[1000];
- if (strcmp(path, "/") == 0)
- {
- path = dirpath;
- sprintf(fpath, "%s", path);
- }
- else
- sprintf(fpath, "%s%s", dirpath, path);
- int res;
- if (S_ISREG(mode))
- {
- res = open(fpath, O_CREAT | O_EXCL | O_WRONLY, mode);
- if (res >= 0)
- res = close(res);
- }
- else if (S_ISFIFO(mode))
- res = mkfifo(fpath, mode);
- else
- res = mknod(fpath, mode, rdev);
- if (res == -1)
- return -errno;
- return 0;
- }
- static int xmp_write(const char *path, const char *buf, size_t size, off_t offset, struct fuse_file_info *fi)
- {
- char fpath[1000];
- if (strcmp(path, "/") == 0)
- {
- path = dirpath;
- sprintf(fpath, "%s", path);
- }
- else
- sprintf(fpath, "%s%s", dirpath, path);
- int fd = 0;
- int res = 0;
- (void)fi;
- fd = open(fpath, O_WRONLY);
- if (fd == -1)
- return -errno;
- res = pwrite(fd, buf, size, offset);
- if (res == -1)
- res = -errno;
- else
- writelog(path);
- close(fd);
- return res;
- }
- static int xmp_unlink(const char *path)
- {
- int res;
- char fpath[1000];
- if (strcmp(path, "/") == 0)
- {
- path = dirpath;
- sprintf(fpath, "%s", path);
- }
- else
- sprintf(fpath, "%s%s", dirpath, path);
- res = unlink(fpath);
- if (res == -1)
- return -errno;
- return 0;
- }
- static int xmp_truncate(const char *path, off_t size)
- {
- int res;
- char fpath[1000];
- if (strcmp(path, "/") == 0)
- {
- path = dirpath;
- sprintf(fpath, "%s", path);
- }
- else
- sprintf(fpath, "%s%s", dirpath, path);
- res = truncate(fpath, size);
- if (res == -1)
- return -errno;
- return 0;
- }
- static struct fuse_operations xmp_oper = {
- .getattr = xmp_getattr,
- .readdir = xmp_readdir,
- .read = xmp_read,
- .write = xmp_write,
- .mknod = xmp_mknod,
- .unlink = xmp_unlink,
- // .truncate = xmp_truncate,
- };
- int main(int argc, char *argv[])
- {
- umask(0);
- return fuse_main(argc, argv, &xmp_oper, NULL);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement