Skip to content

Commit

Permalink
Code added
Browse files Browse the repository at this point in the history
  • Loading branch information
harsh098 committed Mar 11, 2023
0 parents commit 417e7fd
Show file tree
Hide file tree
Showing 6 changed files with 191 additions and 0 deletions.
Empty file added bin/.gitkeep
Empty file.
Empty file added build/.gitkeep
Empty file.
25 changes: 25 additions & 0 deletions include/fs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#define FUSE_USE_VERSION 30

#include <fuse.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>

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 );
18 changes: 18 additions & 0 deletions src/dirs.c
Original file line number Diff line number Diff line change
@@ -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;
}
42 changes: 42 additions & 0 deletions src/files.c
Original file line number Diff line number Diff line change
@@ -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 );
}
106 changes: 106 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
@@ -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 );
}

0 comments on commit 417e7fd

Please sign in to comment.