#define FUSE_USE_VERSION 26 /* ---------------------------------------------------------------------------- */ #include #include #include #include #include #include "myfs_tools.h" /* ---------------------------------------------------------------------------- */ static const char *hello_str = "Hello World!\n"; /* ---------------------------------------------------------------------------- */ static int myfs_getattr(const char *path, struct stat *stbuf) { int res = 0; memset(stbuf, 0, sizeof(struct stat)); if (strcmp(path, "/") == 0) { stbuf->st_mode = S_IFDIR | 0755; stbuf->st_nlink = 2; } else if(myfs_file_exists(path)) { char* fullpath = myfs_file_full_path(path); stat(fullpath, stbuf); free(fullpath); } else res = -ENOENT; return res; } /* ---------------------------------------------------------------------------- */ static int myfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler, off_t offset, struct fuse_file_info *fi) { (void) offset; (void) fi; if (strcmp(path, "/") != 0) return -ENOENT; filler(buf, ".", NULL, 0); filler(buf, "..", NULL, 0); // open dir char* dirname = myfs_dir_get_fuse_home_path(); DIR* dirptr = myfs_dir_open(dirname); free(dirname); if(dirptr == NULL) return -errno; // read dir content while(1) { struct dirent * entry = myfs_dir_read(dirptr); if(!entry) break; filler(buf, entry->d_name, NULL, 0); } // close dir myfs_dir_close(dirptr); return 0; } /* ---------------------------------------------------------------------------- */ static int myfs_open(const char *path, struct fuse_file_info *fi) { if(myfs_file_exists(path) == 0) return -ENOENT; if ((fi->flags & 3) != O_RDONLY) return -EACCES; return 0; } /* ---------------------------------------------------------------------------- */ static int myfs_read(const char *path, char *outputbuf, size_t size, off_t offset, struct fuse_file_info *fi) { pid_t pid; int pfd[2], i; char offbuf[1]; ssize_t nread; ssize_t totalread = 0; (void) fi; pipe(pfd); pid = fork(); // child process if(pid == 0) { dup2(pfd[1],STDOUT_FILENO); close(pfd[0]); close(pfd[1]); char* fullpath = myfs_file_full_path(path); execlp(fullpath, fullpath, NULL); // on error close(STDOUT_FILENO); exit(0); } // parent process else if(pid > 0) { close(pfd[1]); for(i=0;i