Skip to content

Commit

Permalink
add custom path and debuging capability to pa5-encfs
Browse files Browse the repository at this point in the history
  • Loading branch information
Brandon Mikulka committed Apr 17, 2015
1 parent 4fac591 commit b2ad374
Showing 1 changed file with 109 additions and 31 deletions.
140 changes: 109 additions & 31 deletions pa5-encfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#endif

#include <fuse.h>
#include <linux/limits.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
Expand All @@ -23,14 +24,22 @@
#include <sys/xattr.h>
#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;

Expand All @@ -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;

Expand All @@ -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;

Expand All @@ -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));
Expand All @@ -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))
Expand All @@ -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;

Expand All @@ -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;

Expand All @@ -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;

Expand Down Expand Up @@ -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;

Expand All @@ -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;

Expand All @@ -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;

Expand All @@ -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;

Expand All @@ -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;

Expand All @@ -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;

Expand All @@ -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;

Expand All @@ -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;

Expand All @@ -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;

Expand Down Expand Up @@ -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;
Expand All @@ -334,23 +397,32 @@ 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;
}

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;
}

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;
Expand Down Expand Up @@ -389,24 +461,30 @@ static struct fuse_operations xmp_oper = {
};

void usage(){
printf("Usage: ./pa5-encfs <key-phrase> <mirror-dir> <mount-point>");
printf("Usage: ./pa5-encfs <key-phrase> <mirror-dir> <mount-point> [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);
}

0 comments on commit b2ad374

Please sign in to comment.