From b2ad3743f4b8abf2cab4b140b6cf76fc33ac8c43 Mon Sep 17 00:00:00 2001 From: Brandon Mikulka Date: Fri, 17 Apr 2015 13:51:50 -0600 Subject: [PATCH] add custom path and debuging capability to pa5-encfs --- pa5-encfs.c | 140 ++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 109 insertions(+), 31 deletions(-) diff --git a/pa5-encfs.c b/pa5-encfs.c index 079e25c..73de5fd 100644 --- a/pa5-encfs.c +++ b/pa5-encfs.c @@ -11,6 +11,7 @@ #endif #include +#include #include #include #include @@ -23,14 +24,22 @@ #include #endif -char mirrorDir[256]; -char keyPhrase[256]; +char * mirrorDir; +char * keyPhrase; + +static void appendPath(char newPath[PATH_MAX], const char* path){ + strcpy(newPath, mirrorDir); + strncat(newPath, path, PATH_MAX); +} static int xmp_getattr(const char *path, struct stat *stbuf) { int res; - res = lstat(path, stbuf); + char newPath[PATH_MAX]; + appendPath(newPath, path); + + res = lstat(newPath, stbuf); if (res == -1) return -errno; @@ -41,7 +50,10 @@ static int xmp_access(const char *path, int mask) { int res; - res = access(path, mask); + char newPath[PATH_MAX]; + appendPath(newPath, path); + + res = access(newPath, mask); if (res == -1) return -errno; @@ -51,8 +63,10 @@ static int xmp_access(const char *path, int mask) static int xmp_readlink(const char *path, char *buf, size_t size) { int res; + char newPath[PATH_MAX]; + appendPath(newPath, path); - res = readlink(path, buf, size - 1); + res = readlink(newPath, buf, size - 1); if (res == -1) return -errno; @@ -70,10 +84,12 @@ static int xmp_readdir(const char *path, void *buf, fuse_fill_dir_t filler, (void) offset; (void) fi; - dp = opendir(path); + char newPath[PATH_MAX]; + appendPath(newPath, path); + + dp = opendir(newPath); if (dp == NULL) return -errno; - while ((de = readdir(dp)) != NULL) { struct stat st; memset(&st, 0, sizeof(st)); @@ -91,10 +107,13 @@ static int xmp_mknod(const char *path, mode_t mode, dev_t rdev) { int res; + char newPath[PATH_MAX]; + appendPath(newPath, path); + /* On Linux this could just be 'mknod(path, mode, rdev)' but this is more portable */ if (S_ISREG(mode)) { - res = open(path, O_CREAT | O_EXCL | O_WRONLY, mode); + res = open(newPath, O_CREAT | O_EXCL | O_WRONLY, mode); if (res >= 0) res = close(res); } else if (S_ISFIFO(mode)) @@ -111,7 +130,10 @@ static int xmp_mkdir(const char *path, mode_t mode) { int res; - res = mkdir(path, mode); + char newPath[PATH_MAX]; + appendPath(newPath, path); + + res = mkdir(newPath, mode); if (res == -1) return -errno; @@ -122,7 +144,10 @@ static int xmp_unlink(const char *path) { int res; - res = unlink(path); + char newPath[PATH_MAX]; + appendPath(newPath, path); + + res = unlink(newPath); if (res == -1) return -errno; @@ -133,7 +158,10 @@ static int xmp_rmdir(const char *path) { int res; - res = rmdir(path); + char newPath[PATH_MAX]; + appendPath(newPath, path); + + res = rmdir(newPath); if (res == -1) return -errno; @@ -177,7 +205,10 @@ static int xmp_chmod(const char *path, mode_t mode) { int res; - res = chmod(path, mode); + char newPath[PATH_MAX]; + appendPath(newPath, path); + + res = chmod(newPath, mode); if (res == -1) return -errno; @@ -188,7 +219,10 @@ static int xmp_chown(const char *path, uid_t uid, gid_t gid) { int res; - res = lchown(path, uid, gid); + char newPath[PATH_MAX]; + appendPath(newPath, path); + + res = lchown(newPath, uid, gid); if (res == -1) return -errno; @@ -199,7 +233,10 @@ static int xmp_truncate(const char *path, off_t size) { int res; - res = truncate(path, size); + char newPath[PATH_MAX]; + appendPath(newPath, path); + + res = truncate(newPath, size); if (res == -1) return -errno; @@ -211,12 +248,15 @@ static int xmp_utimens(const char *path, const struct timespec ts[2]) int res; struct timeval tv[2]; + char newPath[PATH_MAX]; + appendPath(newPath, path); + tv[0].tv_sec = ts[0].tv_sec; tv[0].tv_usec = ts[0].tv_nsec / 1000; tv[1].tv_sec = ts[1].tv_sec; tv[1].tv_usec = ts[1].tv_nsec / 1000; - res = utimes(path, tv); + res = utimes(newPath, tv); if (res == -1) return -errno; @@ -227,7 +267,10 @@ static int xmp_open(const char *path, struct fuse_file_info *fi) { int res; - res = open(path, fi->flags); + char newPath[PATH_MAX]; + appendPath(newPath, path); + + res = open(newPath, fi->flags); if (res == -1) return -errno; @@ -238,11 +281,18 @@ static int xmp_open(const char *path, struct fuse_file_info *fi) static int xmp_read(const char *path, char *buf, size_t size, off_t offset, struct fuse_file_info *fi) { + fprintf(stdout, "**************************************\n"); + fprintf(stdout, "path: %s \n", path); + fprintf(stdout, "**************************************\n"); int fd; int res; (void) fi; - fd = open(path, O_RDONLY); + + char newPath[PATH_MAX]; + appendPath(newPath, path); + + fd = open(newPath, O_RDONLY); if (fd == -1) return -errno; @@ -260,8 +310,11 @@ static int xmp_write(const char *path, const char *buf, size_t size, int fd; int res; + char newPath[PATH_MAX]; + appendPath(newPath, path); + (void) fi; - fd = open(path, O_WRONLY); + fd = open(newPath, O_WRONLY); if (fd == -1) return -errno; @@ -277,7 +330,10 @@ static int xmp_statfs(const char *path, struct statvfs *stbuf) { int res; - res = statvfs(path, stbuf); + char newPath[PATH_MAX]; + appendPath(newPath, path); + + res = statvfs(newPath, stbuf); if (res == -1) return -errno; @@ -289,7 +345,11 @@ static int xmp_create(const char* path, mode_t mode, struct fuse_file_info* fi) (void) fi; int res; - res = creat(path, mode); + + char newPath[PATH_MAX]; + appendPath(newPath, path); + + res = creat(newPath, mode); if(res == -1) return -errno; @@ -325,7 +385,10 @@ static int xmp_fsync(const char *path, int isdatasync, 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); + char newPath[PATH_MAX]; + appendPath(newPath, path); + + int res = lsetxattr(newPath, name, value, size, flags); if (res == -1) return -errno; return 0; @@ -334,7 +397,10 @@ static int xmp_setxattr(const char *path, const char *name, const char *value, static int xmp_getxattr(const char *path, const char *name, char *value, size_t size) { - int res = lgetxattr(path, name, value, size); + char newPath[PATH_MAX]; + appendPath(newPath, path); + + int res = lgetxattr(newPath, name, value, size); if (res == -1) return -errno; return res; @@ -342,7 +408,10 @@ static int xmp_getxattr(const char *path, const char *name, char *value, static int xmp_listxattr(const char *path, char *list, size_t size) { - int res = llistxattr(path, list, size); + char newPath[PATH_MAX]; + appendPath(newPath, path); + + int res = llistxattr(newPath, list, size); if (res == -1) return -errno; return res; @@ -350,7 +419,10 @@ static int xmp_listxattr(const char *path, char *list, size_t size) static int xmp_removexattr(const char *path, const char *name) { - int res = lremovexattr(path, name); + char newPath[PATH_MAX]; + appendPath(newPath, path); + + int res = lremovexattr(newPath, name); if (res == -1) return -errno; return 0; @@ -389,24 +461,30 @@ static struct fuse_operations xmp_oper = { }; void usage(){ - printf("Usage: ./pa5-encfs "); + printf("Usage: ./pa5-encfs [options]"); } int main(int argc, char *argv[]) -{ - if(argc < 3){ +{ + if(argc <= 3){ usage(); exit(EXIT_FAILURE); } - strncpy(keyPhrase, argv[1], sizeof(keyPhrase)); - strncpy(mirrorDir, argv[2], sizeof(mirrorDir)); + keyPhrase = argv[1]; + mirrorDir = realpath(argv[2], NULL); printf("Key Phrase: %s \n", keyPhrase); printf("Mirror Directory: %s \n", mirrorDir); - char *fuseArgs[] = {argv[0], argv[3], "-d"}; + int i; + for ( i = 3; i < argc; i++){ + argv[i-2] = argv[i]; + } + argv[argc-1] = NULL; + argv[argc-2] = NULL; + argc -= 2; umask(0); - return fuse_main(2, fuseArgs, &xmp_oper, NULL); + return fuse_main(argc, argv, &xmp_oper, NULL); }