You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

236 lines
6.0 KiB

#include <fuse.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "source_lsfs/lsfs_disk_controller.h"
int lsfs_getattr( const char *, struct stat * );
int lsfs_mkdir(const char *path, mode_t mode);
int lsfs_mknod(const char *path, mode_t mode, dev_t device);
int lsfs_open( const char *, struct fuse_file_info * );
int lsfs_read( const char *, char *, size_t, off_t, struct fuse_file_info * );
int lsfs_readdir( const char *, void *, fuse_fill_dir_t, off_t, struct fuse_file_info * );
int lsfs_release(const char *path, struct fuse_file_info *fi);
int lsfs_rename(const char *from, const char *to);
int lsfs_rmdir(const char *path);
int lsfs_truncate(const char *path, off_t offset);
int lsfs_unlink(const char *);
int lsfs_write(const char *, const char *, size_t, off_t, struct fuse_file_info *);
static inline int lsfs_utime_STUB(const char *path, struct utimbuf *buf) {
(void)path;
(void)buf;
return 0;
}
static struct fuse_operations lsfs_oper = {
.getattr = lsfs_getattr,
.readdir = lsfs_readdir,
.mknod = lsfs_mknod,
.mkdir = lsfs_mkdir,
.link = NULL,
.unlink = lsfs_unlink,
.rmdir = lsfs_rmdir,
.truncate = lsfs_truncate,
.open = lsfs_open,
.read = lsfs_read,
.release = lsfs_release,
.write = lsfs_write,
.rename = lsfs_rename,
.utime = lsfs_utime_STUB,
};
int lsfs_readdir( const char *path, void *buf, fuse_fill_dir_t filler, off_t offset_to_next_entry, struct fuse_file_info *fi )
{
//(void) offset;
(void) fi;
//printf("readdir: (path=%s)\n", path);
filler(buf, ".", NULL, 0);
filler(buf, "..", NULL, 0);
Directory_Table *directory_table;
directory_table = lsfs_find_directory(path, false);
if (directory_table != NULL)
{
for (int i = 0; i < DEFAULT_TABLE_SIZE; ++i)
{
if (strcmp( "", directory_table->entries[i].filename ) != 0)
{
filler(buf, directory_table->entries[i].filename, NULL, 0);
}
}
}
if (strcmp(path, "/") != 0)
{
//free(directory_table);
}
return 0;
}
int lsfs_getattr( const char *path, struct stat *stbuf )
{
int res = 0;
lsfs_file *found_file = calloc(1, sizeof(lsfs_file));
//printf("getattr: (path=%s)\n", path);
memset(stbuf, 0, sizeof(struct stat));
if( strcmp( path, "/" ) == 0 )
{
stbuf->st_mode = S_IFDIR | 0755;
stbuf->st_nlink = 2;
}
else
{
if(lsfs_disk_getattr(found_file, path))
{
if (found_file->entry_kind == ENTRY_FILE)
{
stbuf->st_mode = S_IFREG | 0777;
}
else if (found_file->entry_kind == ENTRY_DIRECTORY)
{
stbuf->st_mode = S_IFDIR | 0755;
stbuf->st_nlink = 2;
}
else
{
res = -ENOENT;
}
stbuf->st_nlink = 1; // @Hardcode
stbuf->st_size = found_file->size;
stbuf->st_uid = found_file->owner_id;
stbuf->st_gid = found_file->owner_id;
stbuf->st_atime = found_file->access_time;
stbuf->st_mtime = found_file->modification_time;
//free(found_file);
}
else
{
res = -ENOENT;
}
}
return res;
}
int lsfs_mkdir(const char *path, mode_t mode) {
(void)mode;
return lsfs_disk_create_entry(path, ENTRY_DIRECTORY);
}
int lsfs_rmdir(const char *path) {
// call to the disk controller to remove a dir
if (lsfs_disk_delete_directory(path))
{
// error case:
// directory is not empty
return -EINVAL;
}
return 0;
}
int lsfs_unlink(const char *path) {
lsfs_file *found_file = calloc(1, sizeof(lsfs_file));
lsfs_disk_getattr(found_file, path);
lsfs_disk_delete_entry(found_file);
free(found_file);
return 0;
}
int lsfs_truncate(const char *path, off_t offset) {
(void)offset; // Truncate to this byte?
lsfs_file *found_file = calloc(1, sizeof(lsfs_file));
lsfs_disk_getattr(found_file, path);
lsfs_disk_truncate_file(found_file, offset);
free(found_file);
return 0;
}
int lsfs_rename(const char *path, const char *to) {
return lsfs_disk_rename_file(path, to);
}
int lsfs_write(const char *path, const char *content, size_t content_length, off_t offset_to_next_entry, struct fuse_file_info *file_info)
{
int res = lsfs_disk_write_data_to_file(
((lsfs_file*) file_info->fh),
content_length,
(void*) content, offset_to_next_entry);
return res;
}
//Permission
int lsfs_open( const char *path, struct fuse_file_info *fi ) {
// printf("open: (path=%s)\n", path);
// We can store a pinter in the *fi, this pointer can be used in both read and write.
// https://libfuse.github.io/doxygen/structfuse__operations.html
// printf("read: (path=%s)\n", path);
lsfs_file *found_file = calloc(1, sizeof(lsfs_file));
if (lsfs_disk_getattr(found_file, path)) {
fi->fh = (uint64_t) found_file;
}
return 0;
}
int lsfs_read( const char *path, char *buf, size_t size, off_t offset_to_next_entry, struct fuse_file_info *fi ) {
// printf("read: (path=%s)\n", path);
int res = lsfs_disk_read_data_from_file(
((lsfs_file*) fi->fh),
size,
buf,
offset_to_next_entry);
return res;
}
int lsfs_release(const char *path, struct fuse_file_info *fi) {
return 0;
}
int lsfs_mknod(const char *path, mode_t mode, dev_t device) {
return lsfs_disk_create_entry(path, ENTRY_FILE);
}
int main( int argc, char *argv[] )
{
if (argc != 3)
{
printf("Wrong number of arguments, format should be ./lsfs_fuse mountpoint diskname\n");
}
if (lsfs_disk_load_disk(argv[2]))
{
return fuse_main( argc - 1 , argv, &lsfs_oper );
}
else
{
printf("[ERROR] - Correct file format not found \n");
return 0;
}
}