diff --git a/bin/.gitkeep b/bin/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/build/.gitkeep b/build/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/include/fs.h b/include/fs.h new file mode 100644 index 0000000..b39f33e --- /dev/null +++ b/include/fs.h @@ -0,0 +1,25 @@ +#define FUSE_USE_VERSION 30 + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +extern char dir_list[256][256]; +extern int curr_dir_idx; +extern char files_list[256][256]; +extern int curr_file_idx; +extern char files_content[256][256]; +extern int curr_file_content_idx; +extern void add_dir( const char *dir_name ); +extern int is_dir( const char *path ); +extern void add_file( const char *filename ); +extern int is_file( const char *path ); +extern int get_file_index( const char *path ); +extern void write_to_file( const char *path, const char *new_content ); diff --git a/src/dirs.c b/src/dirs.c new file mode 100644 index 0000000..31eb28f --- /dev/null +++ b/src/dirs.c @@ -0,0 +1,18 @@ +#include "../include/fs.h" + +void add_dir( const char *dir_name ) +{ + curr_dir_idx++; + strcpy( dir_list[ curr_dir_idx ], dir_name ); +} + +int is_dir( const char *path ) +{ + path++; // Eliminating "/" in the path + + for ( int curr_idx = 0; curr_idx <= curr_dir_idx; curr_idx++ ) + if ( strcmp( path, dir_list[ curr_idx ] ) == 0 ) + return 1; + + return 0; +} \ No newline at end of file diff --git a/src/files.c b/src/files.c new file mode 100644 index 0000000..1a7bed0 --- /dev/null +++ b/src/files.c @@ -0,0 +1,42 @@ +#include "../include/fs.h" + +void add_file( const char *filename ) +{ + curr_file_idx++; + strcpy( files_list[ curr_file_idx ], filename ); + + curr_file_content_idx++; + strcpy( files_content[ curr_file_content_idx ], "" ); +} + +int is_file( const char *path ) +{ + path++; // Eliminating "/" in the path + + for ( int curr_idx = 0; curr_idx <= curr_file_idx; curr_idx++ ) + if ( strcmp( path, files_list[ curr_idx ] ) == 0 ) + return 1; + + return 0; +} + +int get_file_index( const char *path ) +{ + path++; // Eliminating "/" in the path + + for ( int curr_idx = 0; curr_idx <= curr_file_idx; curr_idx++ ) + if ( strcmp( path, files_list[ curr_idx ] ) == 0 ) + return curr_idx; + + return -1; +} + +void write_to_file( const char *path, const char *new_content ) +{ + int file_idx = get_file_index( path ); + + if ( file_idx == -1 ) // No such file + return; + + strcpy( files_content[ file_idx ], new_content ); +} diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..c460ea1 --- /dev/null +++ b/src/main.c @@ -0,0 +1,106 @@ + + +#include "../include/fs.h" + + +char dir_list[ 256 ][ 256 ]; +int curr_dir_idx = -1; +char files_list[ 256 ][ 256 ]; +int curr_file_idx = -1; +char files_content[ 256 ][ 256 ]; +int curr_file_content_idx = -1; + + +static int do_getattr( const char *path, struct stat *st ) +{ + st->st_uid = getuid(); // The owner of the file/directory is the user who mounted the filesystem + st->st_gid = getgid(); // The group of the file/directory is the same as the group of the user who mounted the filesystem + st->st_atime = time( NULL ); // The last "a"ccess of the file/directory is right now + st->st_mtime = time( NULL ); // The last "m"odification of the file/directory is right now + + if ( strcmp( path, "/" ) == 0 || is_dir( path ) == 1 ) + { + st->st_mode = S_IFDIR | 0755; + st->st_nlink = 2; } + else if ( is_file( path ) == 1 ) + { + st->st_mode = S_IFREG | 0644; + st->st_nlink = 1; + st->st_size = 1024; + } + else + { + return -ENOENT; + } + + return 0; +} + +static int do_readdir( const char *path, void *buffer, fuse_fill_dir_t filler, off_t offset, struct fuse_file_info *fi ) +{ + filler( buffer, ".", NULL, 0 ); // Current Directory + filler( buffer, "..", NULL, 0 ); // Parent Directory + + if ( strcmp( path, "/" ) == 0 ) // If the user is trying to show the files/directories of the root directory show the following + { + for ( int curr_idx = 0; curr_idx <= curr_dir_idx; curr_idx++ ) + filler( buffer, dir_list[ curr_idx ], NULL, 0 ); + + for ( int curr_idx = 0; curr_idx <= curr_file_idx; curr_idx++ ) + filler( buffer, files_list[ curr_idx ], NULL, 0 ); + } + + return 0; +} + +static int do_read( const char *path, char *buffer, size_t size, off_t offset, struct fuse_file_info *fi ) +{ + printf("Reading %s at offset: %ld\n", path, offset); + int file_idx = get_file_index( path ); + + if ( file_idx == -1 ) + return -1; + + char *content = files_content[ file_idx ]; + + memcpy( buffer, content + offset, size ); + + return strlen( content ) - offset; +} + +static int do_mkdir( const char *path, mode_t mode ) +{ + path++; + add_dir( path ); + + return 0; +} + +static int do_mknod( const char *path, mode_t mode, dev_t rdev ) +{ + path++; + add_file( path ); + + return 0; +} + +static int do_write( const char *path, const char *buffer, size_t size, off_t offset, struct fuse_file_info *info ) +{ + write_to_file( path, buffer ); + printf("Writing to %s at offset: %ld\n", path, offset); + return size; +} + +static struct fuse_operations operations = { + .getattr = do_getattr, + .readdir = do_readdir, + .read = do_read, + .mkdir = do_mkdir, + .mknod = do_mknod, + .write = do_write, +}; + +int main( int argc, char *argv[] ) +{ + return fuse_main( argc, argv, &operations, NULL ); +}