diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..91110b1 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "files.associations": { + "unlink.h": "c" + } +} \ No newline at end of file diff --git a/lib/destroy.h b/lib/destroy.h index cc7ac2f..8be89cc 100644 --- a/lib/destroy.h +++ b/lib/destroy.h @@ -1,13 +1,22 @@ #ifndef destroy_h #define destroy_h -#include /* mode_t */ + +#include +#include +#include #include #include #include #include #include -#include "logger.h" /* logger */ +#include +#include +#include +#include + +#include "logger.h" #include "tree.h" + void sifs_destroy(void*); -#endif +#endif diff --git a/lib/getattr.h b/lib/getattr.h index ebbfd2a..564d172 100644 --- a/lib/getattr.h +++ b/lib/getattr.h @@ -5,8 +5,10 @@ #include #include #include + #include "tree.h" #include "logger.h" + int sifs_getattr(const char*, struct stat*, struct fuse_file_info*); #endif diff --git a/lib/logger.h b/lib/logger.h index 7e1f9e8..873c23d 100644 --- a/lib/logger.h +++ b/lib/logger.h @@ -2,6 +2,8 @@ #define LOGGING_HEADERS #include +#include +#include #define LOG_FG_BLACK "\x1b[31m" #define LOG_FG_RED "\x1b[31m" @@ -28,7 +30,9 @@ enum log_level_t { DEBUG, WARN, ERROR, OFF }; typedef enum log_level_t log_level; + void set_log_level(log_level level); void set_log_output(FILE *level); void logger(log_level, char *s, ...); + #endif diff --git a/lib/mkdir.h b/lib/mkdir.h index 3a2b212..22cfcfb 100644 --- a/lib/mkdir.h +++ b/lib/mkdir.h @@ -1,11 +1,18 @@ -#include /* mode_t */ +#ifndef mkdir_h +#define mkdir_h + +#include #include #include #include #include #include #include -#include "logger.h" /* logger */ + +#include "logger.h" #include "tree.h" #include "fuse.h" + int sifs_mkdir(const char*, mode_t); + +#endif diff --git a/lib/mknod.h b/lib/mknod.h index 3f00821..33a9b36 100644 --- a/lib/mknod.h +++ b/lib/mknod.h @@ -1,12 +1,18 @@ -#include /* mode_t */ +#ifndef mknod_h +#define mknod_h + +#include #include #include #include #include #include #include -#include "logger.h" /* logger */ + +#include "logger.h" #include "tree.h" #include "fuse.h" + int sifs_mknod(const char*, mode_t, dev_t); +#endif diff --git a/lib/open.h b/lib/open.h index be06b7e..5e9858a 100644 --- a/lib/open.h +++ b/lib/open.h @@ -1,8 +1,12 @@ #ifndef open_h #define open_h -#include "./logger.h" /* logger */ -#include /* fuse_file_info */ + #include +#include + +#include "logger.h" + int sifs_open(const char*, struct fuse_file_info*); + #endif diff --git a/lib/opendir.h b/lib/opendir.h index 10e11b7..4298344 100644 --- a/lib/opendir.h +++ b/lib/opendir.h @@ -1,6 +1,10 @@ #ifndef opendir_h #define opendir_h -#include "./logger.h" /* logger */ -#include /* fuse_file_info */ + +#include + +#include "logger.h" + int sifs_opendir(const char*, struct fuse_file_info*); + #endif diff --git a/lib/read.h b/lib/read.h index 1ca5189..86107a1 100644 --- a/lib/read.h +++ b/lib/read.h @@ -1,15 +1,16 @@ #ifndef read_h #define read_h -#include "./logger.h" /* logger */ -#include /* fuse_file_info */ + #include #include #include #include #include + #include "tree.h" #include "logger.h" -#include "tar_structure.h" + int sifs_read(const char*, char*, size_t, off_t, struct fuse_file_info*); + #endif diff --git a/lib/readdir.h b/lib/readdir.h index ab95b0a..4e67b60 100644 --- a/lib/readdir.h +++ b/lib/readdir.h @@ -1,12 +1,13 @@ #ifndef readdir_h #define readdir_h -#include "./logger.h" /* logger */ -#include /* fuse_fill_dir_t fuse_file_info */ + #include #include #include + #include "tree.h" #include "logger.h" + int sifs_readdir( const char*, void*, @@ -15,4 +16,5 @@ int sifs_readdir( struct fuse_file_info*, enum fuse_readdir_flags ); + #endif diff --git a/lib/rmdir.h b/lib/rmdir.h index 4f106fc..2c7100f 100644 --- a/lib/rmdir.h +++ b/lib/rmdir.h @@ -1,15 +1,17 @@ #ifndef rmdir_h #define rmdir_h -#include "./logger.h" /* logger */ -#include /* fuse_file_info */ + #include #include #include #include #include + #include "tree.h" #include "logger.h" #include "tar_structure.h" + int sifs_rmdir(const char* path); + #endif diff --git a/lib/tree.h b/lib/tree.h index 638b955..aba7dc6 100644 --- a/lib/tree.h +++ b/lib/tree.h @@ -2,6 +2,7 @@ #define tree_h #include + #include "tar_structure.h" #include "logger.h" @@ -13,9 +14,6 @@ struct node { void *file; }; -int node_init(struct node *node); -int tree_add_child(struct node *node, struct node *new_node); -int tree_remove_node(struct node *); struct node* get_node_from_path(struct node*, const char*); #endif \ No newline at end of file diff --git a/lib/unlink.h b/lib/unlink.h index 66a0faf..968a4e8 100644 --- a/lib/unlink.h +++ b/lib/unlink.h @@ -1,15 +1,16 @@ #ifndef unlink_h #define unlink_h -#include "./logger.h" /* logger */ -#include /* fuse_file_info */ + #include #include #include #include #include + #include "tree.h" #include "logger.h" -#include "tar_structure.h" + int sifs_unlink(const char* path); + #endif diff --git a/lib/write.h b/lib/write.h index e57852c..62fd644 100644 --- a/lib/write.h +++ b/lib/write.h @@ -1,15 +1,16 @@ #ifndef write_h #define write_h -#include "./logger.h" /* logger */ -#include /* fuse_file_info */ + #include #include #include #include #include + #include "tree.h" #include "logger.h" -#include "tar_structure.h" + int sifs_write(const char*, const char*, size_t, off_t, struct fuse_file_info*); + #endif diff --git a/sifs.c b/sifs.c index d708c7a..93e0a6e 100644 --- a/sifs.c +++ b/sifs.c @@ -89,8 +89,6 @@ int populate_tree_directory(int fd, struct node *dir) { struct node *auxNode; struct node **auxDirChildren; - //printf("\nCurrent directory node: %s\n", dir->header->name); - while (read(fd, auxTar, sizeof(struct tar_header))) { /// Verifying if the file we read is in the same directory if (strncmp( @@ -111,24 +109,20 @@ int populate_tree_directory(int fd, struct node *dir) { auxNode->parent = dir; auxNode->header = auxTar; - //printf("\tCreated node.\n"); // Adding node to parent dir->children_size++; auxDirChildren = realloc(dir->children, dir->children_size * sizeof(struct node *)); dir->children = auxDirChildren; dir->children[dir->children_size - 1] = auxNode; - //printf("\tAdded node to parent.\n"); if (strcmp(auxNode->header->typeflag, "5") == 0) { // Node is a directory - //printf("\tAdding children to directory node for %s\n", auxTar->name); sprintf(auxNode->header->size, "%ld", (long int)10000); fd = populate_tree_directory(fd, auxNode); - //printf("\t\tFinished adding children to %s\n", auxTar->name); - } else { + } + else { // Node is a file - //printf("\tAdding file for %s\n", auxTar->name); int sz = strtoul(auxTar->size, NULL, 8); //auxNode->file = malloc(sz); @@ -156,6 +150,7 @@ int populate_tree_directory(int fd, struct node *dir) { // Returning new reading position return fd; } + // Function that prints tree void print_tree(struct node *n) { logger(DEBUG, "\t%s\n", n->header->name); @@ -182,33 +177,14 @@ void* sifs_init(struct fuse_conn_info* conn, struct fuse_config* cfg) { root->header = malloc(sizeof(struct tar_header)); strcpy(root->header->name, "./"); - /* - sprintf(root->header->mode, "%u", s.st_mode); - sprintf(root->header->uid, "%u", s.st_uid); - sprintf(root->header->gid, "%u", s.st_gid); - strcpy(root->header->chksum, "00000000"); - strcpy(root->header->typeflag, "5"); - sprintf(root->header->size, "%ld", s.st_size); - sprintf(root->header->mtime, "%ld", s.st_mtime); - sprintf(root->header->atime, "%ld", s.st_atime); - sprintf(root->header->ctime, "%ld", s.st_ctime); - */ sprintf(root->header->mode, "%07o", S_IRWXU | S_IRWXG | S_IRWXO); sprintf(root->header->uid, "%07o", getuid()); sprintf(root->header->gid, "%07o", getgid()); strcpy(root->header->typeflag, "5"); - //sprintf(root->header->size, "%011ld", (long int)0); - //strcpy(root->header->size, "10000"); sprintf(root->header->size, "%ld", (long int)10000); - - //sprintf(root->header->uname, "%s", getpwuid(getuid())->pw_name); - //sprintf(root->header->gname, "%s", getgrgid(getgid())->gr_name); strcpy(root->header->uname, getpwuid(getuid())->pw_name); strcpy(root->header->gname, getgrgid(getgid())->gr_name); - - //sprintf(root->header->magic, "%o" "ustar "); - //root->header->magic[7] = '\0'; strcpy(root->header->magic, "ustar "); root->header->magic[7] = '\0'; @@ -236,17 +212,17 @@ void* sifs_init(struct fuse_conn_info* conn, struct fuse_config* cfg) { static struct fuse_operations sifs_oper = { .init = sifs_init, - .destroy = sifs_destroy, + .destroy = sifs_destroy, .getattr = sifs_getattr, // .fgetattr = sifs_fgetattr, // .access = sifs_access, // .readlink = sifs_readlink, .opendir = sifs_opendir, .readdir = sifs_readdir, - .mknod = sifs_mknod, + .mknod = sifs_mknod, .mkdir = sifs_mkdir, .unlink = sifs_unlink, - .rmdir = sifs_rmdir, + .rmdir = sifs_rmdir, // .symlink = sifs_symlink, // .rename = sifs_rename, // .link = sifs_link, @@ -257,7 +233,7 @@ static struct fuse_operations sifs_oper = { // .utimens = sifs_utimens, .open = sifs_open, .read = sifs_read, - .write = sifs_write + .write = sifs_write // .statfs = sifs_statfs, // .release = sifs_release, // .releasedir = sifs_releasedir, @@ -268,6 +244,7 @@ static struct fuse_operations sifs_oper = { }; int main(int argc, char **argv) { + logger(DEBUG, "[main] Started\n"); set_log_level(DEBUG); set_log_output(stdout); @@ -288,8 +265,10 @@ int main(int argc, char **argv) { struct fuse_args args = FUSE_ARGS_INIT(argc - 1, argv); // Parse options - //if (fuse_opt_parse(&args, &options, option_spec, NULL) == -1) - // return 1; + /* + if (fuse_opt_parse(&args, &options, option_spec, NULL) == -1) + return 1; + */ int ret; ret = fuse_main(args.argc, args.argv, &sifs_oper, NULL); @@ -299,6 +278,5 @@ int main(int argc, char **argv) { close(fdd); logger(DEBUG, "[main] Ended\n"); - return ret; } diff --git a/src/destroy.c b/src/destroy.c index f6316c3..c214666 100644 --- a/src/destroy.c +++ b/src/destroy.c @@ -1,57 +1,20 @@ #include "../lib/destroy.h" -#include -#include -#include -#include -#include -#include -#include -#include -#include -int fd; /* gets value from sifs.c */ -int fdd; + +int fd; int enfd; char * argument; -void print_tree2(struct node *n) { - //~ logger(DEBUG, "\t%s%d\n", n->header->name, strtoul(n->header->size, NULL, 8)); +void write_tree(struct node *n) { if(strcmp(n->header->name, "./") != 0){ - //printf("%s\n", n->header->name); - //printf("%s\n%06o\n\n", n->header->chksum, generate_checksum(n->header)); - //generate_checksum(n->header); - if(strcmp(n->header->typeflag, "5") == 0) { - //n->header->typeflag[-1] = ' '; sprintf(n->header->size, "%011ld", (long int)0); } - /* - if(strcmp(n->header->name, "./directory2/") == 0){ - sprintf(n->header->chksum, "%06o", 0); - } - */ - - /* - printf("%s\n", n->header->name); - printf("\t%ld\n", sizeof(n->header->name)); - printf("\t%ld\n", sizeof(n->header->mode)); - printf("\t%ld\n", sizeof(n->header->uid)); - printf("\t%ld\n", sizeof(n->header->gid)); - printf("\t%ld\n", sizeof(n->header->size)); - printf("\t%ld\n", sizeof(n->header->mtime)); - printf("\t%ld\n", sizeof(n->header->chksum)); - printf("\t%ld\n", sizeof(n->header->typeflag)); - printf("\t%ld\n", sizeof(n->header->linkname)); - */ - printf("\tPozitie pentru %s:\t%ld\n", n->header->name, lseek(fd, 0, SEEK_CUR)); write(fd, n->header, sizeof(struct tar_header)); if(strcmp(n->header->typeflag, "0") == 0){ int file_size = strtoul(n->header->size, NULL, 8); if(file_size!=0) { - - //write(fdd, n->file, 512 * ((file_size/512) + 1)); - write(fd, n->file, file_size); int pos = lseek(fd, 0, SEEK_CUR); lseek(fd, (512 - pos % 512), SEEK_CUR); @@ -61,51 +24,26 @@ void print_tree2(struct node *n) { int i; for (i = 0; i < n->children_size; i++) { - print_tree2(n->children[i]); + write_tree(n->children[i]); } } void sifs_destroy(void* private_data) { - + logger(DEBUG, "[destroy] Started\n"); off_t ofs = lseek(fd, 0, SEEK_END); - printf("\n\n\n\n\n\n%ld\n\n\n\n\n", ofs); lseek(fd, 0, SEEK_SET); int crt = 0; while(crt < ofs) { crt += write(fd, "\0", ofs); - printf("\n\n\n\n\n\n\n%d\n\n\n\n\n\n\n", crt); } - //~ printf("\n\n\n\n\n\n\n%d\n\n\n\n\n\n\n", write(fd, "\0", ofs)); - //~ printf("\n\n\n\n\n\n\n%d\n\n\n\n\n\n\n", write(fd, "\0", ofs)); - //~ printf("\n\n\n\n\n\n\n%d\n\n\n\n\n\n\n", write(fd, "\0", ofs)); - //~ printf("\n\n\n\n\n\n\n%d\n\n\n\n\n\n\n", write(fd, "\0", ofs)); - //printf("\n\n\n\n\n\n\n%d\n\n\n\n\n\n\n", write(fd, "\0", ofs)); - - lseek(fd, 0, SEEK_SET); /* move to the beginning of file */ - //~ printf("FAILED!!!"); - //~ } - - //struct node* parent; - //parent = get_node_from_path(private_data,"/"); - print_tree2((struct node*)private_data); - - - /* - lseek(fdd, 0, SEEK_END); - for (int i = 0; i < 18*512 - 10; i++){ - write(fdd, "\0", 1); - } - lseek(fdd, 0, SEEK_SET); - */ + lseek(fd, 0, SEEK_SET); + write_tree((struct node*)private_data); + logger(DEBUG, "[destroy] Tree written\n"); lseek(fd, 17 * 512 - 1, SEEK_CUR); - /* - char *e = calloc(1, sizeof(char)); - e[0] = '\0'; - write(fdd, e, 1); - */ write(fd, "\0", 1); + logger(DEBUG, "[destroy] Ended\n"); } diff --git a/src/getattr.c b/src/getattr.c index f9feb07..72cd0b0 100644 --- a/src/getattr.c +++ b/src/getattr.c @@ -1,20 +1,7 @@ #include "../lib/getattr.h" -/* - ./sifs -f -d -o default_permissions ./f ../Tars/testTar.tar - ./sifs -f -d -o default_permissions ../tests/testFolder ../tests/tars/testTar.tar - -*/ int sifs_getattr(const char* path, struct stat* sbuf, struct fuse_file_info* fi) { logger(DEBUG, "[getattr] Started on path: %s\n", path); - /* - char *aux; - aux = malloc(strlen(path) + 2); - aux[0] = '.'; aux[1] = '\0'; - strcpy(aux + 1, path); - stat(aux, sbuf); - //return 0; - */ struct fuse_context* context; context = fuse_get_context(); @@ -30,26 +17,18 @@ int sifs_getattr(const char* path, struct stat* sbuf, struct fuse_file_info* fi) } logger(DEBUG, "[getattr] \tNode returned: %s\n", n->header->name); - sbuf->st_dev = 0; - sbuf->st_rdev = 0; - sbuf->st_blocks = 0; - // The 'st_dev' and 'st_blksize' fields are ignored. - // The 'st_ino' field is ignored except if the 'use_ino' mount option is given - //sbuf->st_mode = strtoul(n->header->mode, NULL, 10); - //sbuf->st_mode ^= 1UL << 15; + sbuf->st_dev = 0; + sbuf->st_rdev = 0; + sbuf->st_blocks = 0; sbuf->st_mode = ((strtoul(n->header->typeflag, NULL, 10) == 5) ? S_IFDIR : S_IFREG) | S_IRWXU | S_IRWXG | S_IRWXO; - //logger(DEBUG, "[getattr] mode: %o\n", sbuf->st_mode); - //logger(DEBUG, "[getattr] mode: %o\n", S_IFDIR | S_IRWXU | S_IRWXG | S_IRWXO ); - - sbuf->st_nlink = n->children_size;/* number of hard links */ - sbuf->st_uid = strtoul(n->header->uid, NULL, 10); /* user ID of owner */ - - sbuf->st_gid = strtoul(n->header->gid, NULL, 10); /* group ID of owner */ - sbuf->st_size = strtol(n->header->size, NULL, 8); /* total size, in bytes */ - sbuf->st_blksize = 4096; /* blocksize for file system I/O */ - sbuf->st_atime = strtol(n->header->atime, NULL, 10); /* time of last access */ - sbuf->st_mtime = strtol(n->header->mtime, NULL, 10); /* time of last modification */ - sbuf->st_ctime = strtol(n->header->ctime, NULL, 10); /* time of last status change */ + sbuf->st_nlink = n->children_size; + sbuf->st_uid = strtoul(n->header->uid, NULL, 10); + sbuf->st_gid = strtoul(n->header->gid, NULL, 10); + sbuf->st_size = strtol(n->header->size, NULL, 8); + sbuf->st_blksize = 4096; + sbuf->st_atime = strtol(n->header->atime, NULL, 10); + sbuf->st_mtime = strtol(n->header->mtime, NULL, 10); + sbuf->st_ctime = strtol(n->header->ctime, NULL, 10); logger(DEBUG, "[getattr] Ended\n"); return 0; diff --git a/src/logger.c b/src/logger.c index b2e88b7..e4d8533 100644 --- a/src/logger.c +++ b/src/logger.c @@ -1,7 +1,4 @@ #include "../lib/logger.h" -#include -#include -#include FILE *OUTPUT = NULL; log_level LOG_LEVEL = ERROR; diff --git a/src/mkdir.c b/src/mkdir.c index 539f7c7..dd052f6 100644 --- a/src/mkdir.c +++ b/src/mkdir.c @@ -1,8 +1,8 @@ #include "../lib/mkdir.h" int sifs_mkdir(const char* path, mode_t mode) { - logger(DEBUG, LOG_BOLD LOG_FG_RED "[mkdir] Started on path: %s\n" LOG_RESET, path); - logger(DEBUG, LOG_BOLD LOG_FG_RED "[mkdir] Mode: %d\t%o\n" LOG_RESET, mode, mode); + logger(DEBUG, "[mkdir] Started on path: %s\n", path); + logger(DEBUG, "[mkdir] Mode: %d\t%o\n", mode, mode); struct fuse_context* context; context = fuse_get_context(); @@ -12,7 +12,7 @@ int sifs_mkdir(const char* path, mode_t mode) { int last_slash; last_slash = (strrchr(path, '/') - path); - logger(DEBUG, LOG_BOLD LOG_FG_RED "[mkdir] Position of last slash: %d\n" LOG_RESET, last_slash); + logger(DEBUG, "[mkdir] Position of last slash: %d\n", last_slash); // Root exception if(!last_slash) last_slash++; @@ -21,11 +21,11 @@ int sifs_mkdir(const char* path, mode_t mode) { parent_path = malloc((last_slash + 1) * sizeof(char)); strncpy(parent_path, path, last_slash); parent_path[last_slash] = '\0'; - logger(DEBUG, LOG_BOLD LOG_FG_RED "[mkdir] Path of parent node: %s-\n" LOG_RESET, parent_path); + logger(DEBUG, "[mkdir] Path of parent node: %s-\n", parent_path); struct node* parent; parent = get_node_from_path(root, parent_path); - logger(DEBUG, LOG_BOLD LOG_FG_RED "[mkdir] Returned node: %s\n" LOG_RESET, parent->header->name); + logger(DEBUG, "[mkdir] Returned node: %s\n", parent->header->name); free(parent_path); // Creating node @@ -46,26 +46,18 @@ int sifs_mkdir(const char* path, mode_t mode) { strcpy(n->header->name + 1, path); n->header->name[strlen(path) + 1] = '/'; n->header->name[strlen(path) + 2] = '\0'; - logger(DEBUG, LOG_BOLD LOG_FG_RED "[mkdir] New node name: %s\n" LOG_RESET, n->header->name); + logger(DEBUG, "[mkdir] New node name: %s\n", n->header->name); sprintf(n->header->mode, "%07o", mode); sprintf(n->header->uid, "%07o", getuid()); sprintf(n->header->gid, "%07o", getgid()); - //sprintf(n->header->uid, "%s", getpwuid(getuid())->pw_name);qq - //sprintf(n->header->gid, "%s", getgrgid(getgid())->gr_name); + strcpy(n->header->uname, getpwuid(getuid())->pw_name); + strcpy(n->header->gname, getgrgid(getgid())->gr_name); strcpy(n->header->typeflag, "5"); - //sprintf(n->header->size, "%011ld", (long int)10000); - //printf("%s\t%o\n", n->header->size); - strcpy(n->header->size, "0000010000"); //sprintf(n->header->size, "%011ld", (long int)10000); + strcpy(n->header->size, "0000010000"); - - strcpy(n->header->uname, getpwuid(getuid())->pw_name); - strcpy(n->header->gname, getgrgid(getgid())->gr_name); - - //sprintf(n->header->magic, "%o" "ustar "); - //n->header->magic[7] = '\0'; strcpy(n->header->magic, "ustar "); n->header->magic[7] = '\0'; @@ -78,19 +70,12 @@ int sifs_mkdir(const char* path, mode_t mode) { sprintf(n->header->chksum, "%06o", generate_checksum(n->header)); n->header->chksum[7] = ' '; - logger(DEBUG, LOG_BOLD LOG_BG_YELLOW LOG_FG_WHITE); - - logger(DEBUG, "%s\n", parent->header->chksum); - logger(DEBUG, "%06o\n", generate_checksum(parent->header)); - - logger(DEBUG, LOG_RESET); - struct node **auxDirChildren; parent->children_size++; auxDirChildren = realloc(parent->children, parent->children_size * sizeof(struct node*)); parent->children = auxDirChildren; parent->children[parent->children_size - 1] = n; - logger(DEBUG, LOG_BOLD LOG_FG_RED "[mkdir] Ended\n" LOG_RESET); + logger(DEBUG, "[mkdir] Ended\n"); return 0; } diff --git a/src/mknod.c b/src/mknod.c index a267a7c..72cca25 100644 --- a/src/mknod.c +++ b/src/mknod.c @@ -1,8 +1,8 @@ #include "../lib/mknod.h" int sifs_mknod(const char* path, mode_t mode, dev_t dev) { - logger(DEBUG, LOG_BOLD LOG_FG_BLUE "[mknod] Started on path: %s\n" LOG_RESET, path); - logger(DEBUG, LOG_BOLD LOG_FG_BLUE "[mknod] Mode: %d\t%o\n" LOG_RESET, mode, mode); + logger(DEBUG, "[mknod] Started on path: %s\n", path); + logger(DEBUG, "[mknod] Mode: %d\t%o\n", mode, mode); struct fuse_context* context; context = fuse_get_context(); @@ -21,14 +21,15 @@ int sifs_mknod(const char* path, mode_t mode, dev_t dev) { parent_path = malloc((last_slash + 1) * sizeof(char)); strncpy(parent_path, path, last_slash); parent_path[last_slash] = '\0'; - logger(DEBUG, LOG_BOLD LOG_FG_BLUE "[mknod] Path of parent node: %s-\n" LOG_RESET, parent_path); + logger(DEBUG, "[mknod] Path of parent node: %s-\n", parent_path); struct node* parent; parent = get_node_from_path(root, parent_path); - logger(DEBUG, LOG_BOLD LOG_FG_BLUE "[mknod] Returned node: %s\n" LOG_RESET, parent->header->name); + logger(DEBUG, "[mknod] Returned node: %s\n", parent->header->name); free(parent_path); // Creating node + //struct node *n = malloc(sizeof(struct node)); struct node *n = calloc(1, sizeof(struct node)); n->parent = parent; n->children = NULL; @@ -37,32 +38,31 @@ int sifs_mknod(const char* path, mode_t mode, dev_t dev) { strcpy(n->file, ""); // Adding node metadata + //n->header = malloc(sizeof(struct tar_header)); n->header = calloc(1, sizeof(struct tar_header)); + //memcpy(n->header, parent->header, sizeof(struct tar_header)); + n->header->name[0] = '.'; strcpy(n->header->name + 1, path); n->header->name[strlen(path) + 1] = '\0'; - logger(DEBUG, LOG_BOLD LOG_FG_BLUE "[mknod] New node name: %s\n" LOG_RESET, n->header->name); + logger(DEBUG, "[mknod] New node name: %s\n", n->header->name); sprintf(n->header->mode, "%07o", mode); sprintf(n->header->uid, "%07o", getuid()); sprintf(n->header->gid, "%07o", getgid()); - strcpy(n->header->typeflag, "0"); - strcpy(n->header->size, "0000000000"); - strcpy(n->header->uname, getpwuid(getuid())->pw_name); strcpy(n->header->gname, getgrgid(getgid())->gr_name); - - //sprintf(n->header->magic, "%o" "ustar "); - //n->header->magic[7] = '\0'; + strcpy(n->header->typeflag, "0"); + strcpy(n->header->size, "0000000000"); strcpy(n->header->magic, "ustar "); n->header->magic[7] = '\0'; - time_t t; t = time(NULL); sprintf(n->header->mtime, "%lo", t); - + //sprintf(n->header->atime, "%ld", t); + //sprintf(n->header->ctime, "%ld", t); sprintf(n->header->chksum, "%06o", generate_checksum(n->header)); n->header->chksum[7] = ' '; @@ -73,6 +73,6 @@ int sifs_mknod(const char* path, mode_t mode, dev_t dev) { parent->children = auxDirChildren; parent->children[parent->children_size - 1] = n; - logger(DEBUG, LOG_BOLD LOG_FG_BLUE "[mknod] Ended\n" LOG_RESET); + logger(DEBUG, "[mknod] Ended\n"); return 0; } diff --git a/src/open.c b/src/open.c index aec0586..4a9f796 100644 --- a/src/open.c +++ b/src/open.c @@ -1,7 +1,8 @@ #include "../lib/open.h" int sifs_open(const char* path, struct fuse_file_info* fi) { - logger(ERROR, "[open] File opened, path: %s\n", path); + logger(ERROR, "[open] Started on path: %s\n", path); + logger(ERROR, "[open] Ended"); return 0; } diff --git a/src/read.c b/src/read.c index 3ca8d58..3800b15 100644 --- a/src/read.c +++ b/src/read.c @@ -1,6 +1,8 @@ #include "../lib/read.h" int sifs_read(const char* path, char *buf, size_t size, off_t offset, struct fuse_file_info* fi) { + logger(DEBUG, "[read] Started on path: %s\n", path); + // Get to current file node: struct fuse_context* context; context = fuse_get_context(); @@ -12,16 +14,18 @@ int sifs_read(const char* path, char *buf, size_t size, off_t offset, struct fus n = get_node_from_path(root, path); - size_t file_length = strlen( (char*)n->file ); + size_t file_length = strlen((char*)n->file); if (offset < file_length) { if (offset + size > file_length) size = file_length - offset; logger(DEBUG, "[read] %s\n\n\n", n->file + offset); memcpy(buf, (char*)n->file + offset, size); - } else { + } + else { size = 0; } + logger(DEBUG, "[read] Ended"); return size; } diff --git a/src/readdir.c b/src/readdir.c index 9801138..e3fe470 100644 --- a/src/readdir.c +++ b/src/readdir.c @@ -6,13 +6,6 @@ int sifs_readdir( ) { logger(DEBUG, "[readdir] Started on path: %s\n", path); - /* - if (strcmp(path, "/") != 0) { - logger(DEBUG, "[readdir] Ended\n"); - return -1; - } - */ - filler(buf, ".", NULL, 0, 0); filler(buf, "..", NULL, 0, 0); @@ -25,8 +18,6 @@ int sifs_readdir( struct node *n; n = get_node_from_path(root, path); - - //filler(buf, "...", NULL, 0,0); for(int i = 0; i < n->children_size; i++){ char buffer[50]; strcpy(buffer, n->children[i]->header->name+2); @@ -34,15 +25,13 @@ int sifs_readdir( if( strcmp(n->children[i]->header->typeflag, "5") == 0){ buffer[ strlen(buffer) - 1 ] = '\0'; } - //filler(buf, buffer, NULL, 0, 0); - //logger(DEBUG, "\t[readdit] Ce se intampla, doctore? %s\n", strrchr(buffer, '/')); if(strrchr(buffer, '/')) { filler(buf, strrchr(buffer, '/') + 1, NULL, 0, 0); } else { filler(buf, buffer, NULL, 0, 0); } - logger(DEBUG, LOG_BOLD LOG_FG_GREEN "[readdir] Buffer: %s\n" LOG_RESET , buffer); + logger(DEBUG, "[readdir] Buffer: %s\n" , buffer); } logger(DEBUG, "[readdir] Ended\n"); diff --git a/src/rmdir.c b/src/rmdir.c index 0e9cd08..95ea578 100644 --- a/src/rmdir.c +++ b/src/rmdir.c @@ -1,6 +1,8 @@ #include "../lib/rmdir.h" int sifs_rmdir(const char* path) { + logger(DEBUG, "[rmdir] Started on path: %s\n", path); + struct fuse_context* context; context=fuse_get_context(); @@ -9,7 +11,6 @@ int sifs_rmdir(const char* path) { struct node *deleted = get_node_from_path(root, path); struct node* parent = deleted->parent; - printf("#######%s########\n\n\n", parent->header->name); int del_index; for(int i = 0; i < parent->children_size; i++) { @@ -22,7 +23,9 @@ int sifs_rmdir(const char* path) { for (int i = del_index + 1; i < parent->children_size; i++) { parent->children[i-1] = parent->children[i]; } - parent->children_size --; + parent->children_size--; + + logger(DEBUG, "[readdir] Ended\n"); return 0; } diff --git a/src/tree.c b/src/tree.c index f5b5de2..2b3670e 100644 --- a/src/tree.c +++ b/src/tree.c @@ -4,43 +4,6 @@ #include #include -int node_init(struct node *n) { - n = malloc(sizeof(struct node)); - //node->header = NULL; - n->header = malloc(sizeof(struct tar_header)); - n->parent = NULL; - n->children = NULL; - n->children_size = 0; - n->file = NULL; - return 1; -} -int tree_add_child(struct node *parent, struct node *new_node) { - parent->children_size++; - parent->children = malloc(new_node->children_size * sizeof(struct node *)); - parent->children[new_node->children_size - 1] = new_node; - - new_node->parent = parent; - - return 1; -} -int tree_remove_node(struct node *node) { - struct node *parent = node->parent; - - int i; - for (i = 0; i < parent->children_size; i++) { - if (parent->children[i] == node) { - while (i < parent->children_size - 1) { - parent->children[i] = parent->children[i + 1]; - i++; - } - parent->children_size--; - return 1; - } - } - - return 0; -} - struct node* get_node_from_path(struct node* n, const char* path) { // +1 to remove . from filename logger(DEBUG, "(tree/get_node_from_path) Started with path: %s\n", path); @@ -54,7 +17,6 @@ struct node* get_node_from_path(struct node* n, const char* path) { return aux; } - int modified = 0; while ( (aux->header->name[strlen(aux->header->name) - 1] == '/') ? @@ -62,7 +24,6 @@ struct node* get_node_from_path(struct node* n, const char* path) { (strncmp(aux->header->name + 1, path, strlen(aux->header->name + 1)) == 0) ) { logger(DEBUG, "(tree/get_node_from_path) Current node: %s\n", aux->header->name + 1); - //logger(DEBUG, "(tree/get_node_from_path) Children: %d\n", aux->children_size); int i; for(i = 0; i < aux->children_size; i++) { @@ -86,81 +47,11 @@ struct node* get_node_from_path(struct node* n, const char* path) { break; } - - /* - if (aux->children[i]->header->name[strlen(aux->children[i]->header->name) - 1] == '/') { - logger(DEBUG, LOG_FG_MAGENTA "\t\t%d\t%d\n" LOG_RESET, strlen(path), strlen(aux->children[i]->header->name + 1) - 1); - if(strlen(path) != strlen(aux->children[i]->header->name + 1) - 1) { - if(strncmp(aux->children[i]->header->name + 1, path, strlen(aux->children[i]->header->name + 1) - 1) == 0) - { - aux = aux->children[i]; - modified = 1; - break; - } - } - else { - if( - (strncmp(aux->children[i]->header->name + 1, path, strlen(aux->children[i]->header->name + 1) - 1) == 0) && - ((path[strlen(aux->children[i]->header->name + 1) - 1] == '\0') && ((aux->children[i]->header->name + 1)[strlen(aux->children[i]->header->name + 1) - 1] == '/')) - ) { - aux = aux->children[i]; - modified = 1; - break; - } - } - } - else { - if( - (strncmp(aux->children[i]->header->name + 1, path, strlen(aux->children[i]->header->name + 1)) == 0) && - (strncmp(aux->children[i]->header->name + 1, path, strlen(path)) == 0) && - (strlen(aux->children[i]->header->name + 1) == strlen(path)) - ) { - aux = aux->children[i]; - modified = 1; - break; - } - } - */ } if(!modified) break; else modified = 0; } - /* - int modified, found; - found = 0; - while(1) { - modified = 0; - - int i; - for(i = 0; i < aux->children_size; i++) { - if (aux->children[i]->header->name[strlen(aux->children[i]->header->name) - 1] == '/') { - if(strncmp( - aux->children[i]->header->name + 1, - path, - strlen(aux->children[i]->header->name + 1) - 1) == 0) { - - } - } - } - } - */ - - - /* - if ((strcmp(aux->header->name, n->header->name) == 0) && !modified) { - logger(DEBUG, "(tree/get_node_from_path) Node not found\n"); - return NULL; - } - */ - - /* - if ( - (aux->header->name[strlen(aux->header->name) - 1] == '/') ? - (strncmp(aux->header->name + 1, path, strlen(path)) == 0) : - (strncmp(aux->header->name + 1, path, strlen(path)) == 0) - ) { - */ if (strncmp(aux->header->name + 1, path, strlen(path)) == 0) { logger(DEBUG, "(tree/get_node_from_path) Node found: %s\n", aux->header->name); return aux; diff --git a/src/unlink.c b/src/unlink.c index a000716..fb18ff8 100644 --- a/src/unlink.c +++ b/src/unlink.c @@ -1,16 +1,17 @@ #include "../lib/unlink.h" int sifs_unlink(const char* path) { + logger(DEBUG, "[unlink] Started on path: %s\n", path); + struct fuse_context* context; context=fuse_get_context(); struct node* root; root=(struct node*)context->private_data; struct node *deleted = get_node_from_path(root, path); - + struct node* parent = deleted->parent; - printf("#######%s########\n\n\n", parent->header->name); - + int del_index; for(int i = 0; i < parent->children_size; i++) { if(!strcmp(parent->children[i]->header->name, deleted->header->name)){ @@ -22,7 +23,9 @@ int sifs_unlink(const char* path) { for (int i = del_index + 1; i < parent->children_size; i++) { parent->children[i-1] = parent->children[i]; } - parent->children_size --; + parent->children_size--; + + logger(DEBUG, "[unlink] Ended\n"); return 0; } diff --git a/src/write.c b/src/write.c index 92bf24a..6643b47 100644 --- a/src/write.c +++ b/src/write.c @@ -1,7 +1,6 @@ #include "../lib/write.h" int sifs_write(const char* path, const char *buf, size_t size, off_t offset, struct fuse_file_info* fi) { - logger(DEBUG, LOG_BOLD LOG_BG_YELLOW LOG_FG_BLACK); logger(DEBUG, "[write] Started on path: %s\n", path); struct fuse_context* context; context=fuse_get_context(); @@ -13,29 +12,15 @@ int sifs_write(const char* path, const char *buf, size_t size, off_t offset, str n=get_node_from_path(root,path); n->file = realloc(n->file, strlen(buf)); - //printf("\n\n\n\n\n\n\n\n\n\n\n\n%s", buf); - //~ size_t file_length = strlen(buf); - //~ if (offset < file_length) { - //~ if (offset + size > file_length) - //~ size = file_length-offset; - //~ logger(DEBUG, "[wrote] %s\n\n\n", buf); + memcpy(n->file + offset, buf, size); - //~ } else { - //~ return -1; - //~ } - //sprintf(n->header->size, "%011lo", strtol(n->header->size, NULL, 8) + size); - //sprintf(n->header->size, "%011lo", strlen(n->file) - 1); sprintf(n->header->size, "%011lo", strlen(n->file)); printf("%s\n", n->header->size); sprintf(n->header->chksum, "%06o", generate_checksum(n->header)); n->header->chksum[7] = ' '; - //logger(DEBUG, "[write] size: %s\n", n->header->size); - //sprintf(n->header->size, "%lo", strlen(buf)); - //logger(DEBUG, "[write] size: %s\n", n->header->size); - logger(DEBUG, "[write] Ended\n"); logger(DEBUG, LOG_RESET); return size;