Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Dummm committed Jan 13, 2019
1 parent 1715997 commit 0251fca
Show file tree
Hide file tree
Showing 27 changed files with 151 additions and 352 deletions.
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"files.associations": {
"unlink.h": "c"
}
}
15 changes: 12 additions & 3 deletions lib/destroy.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
#ifndef destroy_h
#define destroy_h
#include <sys/stat.h> /* mode_t */

#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h>
#include <stdlib.h>
#include <fcntl.h>
#include <time.h>
#include <unistd.h>
#include <stddef.h>
#include "logger.h" /* logger */
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>

#include "logger.h"
#include "tree.h"

void sifs_destroy(void*);
#endif

#endif
2 changes: 2 additions & 0 deletions lib/getattr.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
#include <stdlib.h>
#include <errno.h>
#include <fuse.h>

#include "tree.h"
#include "logger.h"

int sifs_getattr(const char*, struct stat*, struct fuse_file_info*);

#endif
4 changes: 4 additions & 0 deletions lib/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#define LOGGING_HEADERS

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>

#define LOG_FG_BLACK "\x1b[31m"
#define LOG_FG_RED "\x1b[31m"
Expand All @@ -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
11 changes: 9 additions & 2 deletions lib/mkdir.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
#include <sys/stat.h> /* mode_t */
#ifndef mkdir_h
#define mkdir_h

#include <sys/stat.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <stddef.h>
#include <pwd.h>
#include <grp.h>
#include "logger.h" /* logger */

#include "logger.h"
#include "tree.h"
#include "fuse.h"

int sifs_mkdir(const char*, mode_t);

#endif
10 changes: 8 additions & 2 deletions lib/mknod.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
#include <sys/stat.h> /* mode_t */
#ifndef mknod_h
#define mknod_h

#include <sys/stat.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <stddef.h>
#include <pwd.h>
#include <grp.h>
#include "logger.h" /* logger */

#include "logger.h"
#include "tree.h"
#include "fuse.h"

int sifs_mknod(const char*, mode_t, dev_t);

#endif
8 changes: 6 additions & 2 deletions lib/open.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
#ifndef open_h
#define open_h
#include "./logger.h" /* logger */
#include <fuse.h> /* fuse_file_info */

#include <unistd.h>
#include <fuse.h>

#include "logger.h"

int sifs_open(const char*, struct fuse_file_info*);

#endif

8 changes: 6 additions & 2 deletions lib/opendir.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#ifndef opendir_h
#define opendir_h
#include "./logger.h" /* logger */
#include <fuse.h> /* fuse_file_info */

#include <fuse.h>

#include "logger.h"

int sifs_opendir(const char*, struct fuse_file_info*);

#endif
7 changes: 4 additions & 3 deletions lib/read.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
#ifndef read_h
#define read_h
#include "./logger.h" /* logger */
#include <fuse.h> /* fuse_file_info */

#include <sys/stat.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <fuse.h>

#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

6 changes: 4 additions & 2 deletions lib/readdir.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
#ifndef readdir_h
#define readdir_h
#include "./logger.h" /* logger */
#include <fuse.h> /* fuse_fill_dir_t fuse_file_info */

#include <sys/stat.h>
#include <stdlib.h>
#include <fuse.h>

#include "tree.h"
#include "logger.h"

int sifs_readdir(
const char*,
void*,
Expand All @@ -15,4 +16,5 @@ int sifs_readdir(
struct fuse_file_info*,
enum fuse_readdir_flags
);

#endif
6 changes: 4 additions & 2 deletions lib/rmdir.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
#ifndef rmdir_h
#define rmdir_h
#include "./logger.h" /* logger */
#include <fuse.h> /* fuse_file_info */

#include <sys/stat.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <fuse.h>

#include "tree.h"
#include "logger.h"
#include "tar_structure.h"

int sifs_rmdir(const char* path);

#endif

4 changes: 1 addition & 3 deletions lib/tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define tree_h

#include <string.h>

#include "tar_structure.h"
#include "logger.h"

Expand All @@ -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
7 changes: 4 additions & 3 deletions lib/unlink.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
#ifndef unlink_h
#define unlink_h
#include "./logger.h" /* logger */
#include <fuse.h> /* fuse_file_info */

#include <sys/stat.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <fuse.h>

#include "tree.h"
#include "logger.h"
#include "tar_structure.h"

int sifs_unlink(const char* path);

#endif

7 changes: 4 additions & 3 deletions lib/write.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
#ifndef write_h
#define write_h
#include "./logger.h" /* logger */
#include <fuse.h> /* fuse_file_info */

#include <sys/stat.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <fuse.h>

#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

46 changes: 12 additions & 34 deletions sifs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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';

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

Expand All @@ -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);
Expand All @@ -299,6 +278,5 @@ int main(int argc, char **argv) {
close(fdd);

logger(DEBUG, "[main] Ended\n");

return ret;
}
Loading

0 comments on commit 0251fca

Please sign in to comment.