From 782d8db162b69ff1e07608edea32d6e1484b5932 Mon Sep 17 00:00:00 2001 From: Emma Nora Theuer Date: Sat, 19 Oct 2024 19:52:24 +0200 Subject: [PATCH] Various stuff I did during lectures --- inode.c | 4 ---- inode.h | 12 +----------- super.c | 33 ++++++++++++++++++++++++++++++++- util.c | 40 +++++++++++++++++++++++++++++++++++++++- util.h | 17 ++++++++++++++++- 5 files changed, 88 insertions(+), 18 deletions(-) diff --git a/inode.c b/inode.c index 79beaf1..2999bae 100644 --- a/inode.c +++ b/inode.c @@ -1,5 +1 @@ #include "inode.h" - -tm* get_local_time() { - return localtime(¤t_time); -} diff --git a/inode.h b/inode.h index 3680897..e1c4e20 100644 --- a/inode.h +++ b/inode.h @@ -3,23 +3,15 @@ #include #include -#include #include "util.h" typedef struct Inode Inode; -typedef struct tm tm; - -// Global Variables -time_t current_time; - -// Structs -struct tm *local_time; struct Inode { // file information char name[64]; - uint32_t size; + uint32_t filesize; // ownership and permissions uint16_t permissions; @@ -36,8 +28,6 @@ struct Inode { block* data_block; }; -tm* get_local_time(); Inode* create_inode(char name[64], uint16_t owner, uint16_t group, block* data_block); -Inode* find_inode(char name[]); #endif // INODE_H_ diff --git a/super.c b/super.c index 439511f..950b624 100644 --- a/super.c +++ b/super.c @@ -1,8 +1,8 @@ #ifndef SUPER_H_ #define SUPER_H_ -#include "inode.h" #include "util.h" +#include "inode.h" // fs attributes #define MAGIC 0x7F631EC4 @@ -12,5 +12,36 @@ uint32_t free_blocks; uint32_t inode_count; +struct inode_table { + // TODO: Change currently hardcoded value to actually accurately describe the size of the filesystem, only for temporary purposes. + Inode* inodes[640000]; +}; + +struct super_block { + // fs info + char version[8]; + uint64_t magic; + // fs attributes + uint16_t blocksize; + uint64_t block_count; + uint64_t free_bloks; + uint64_t inode_count; + uint64_t free_inodes; + // Timestamps + tm fs_creation; + tm last_write; + + // inode table + struct inode_table* inode_table; +}; + +// TODO: Do research on how the fuck to actually do this kind of Array operation +Inode* find_inode(char filename[], struct super_block sblock) { + struct inode_table* itable = sblock.inode_table; + // while (itable[inode] != NULL; inode++) { + // if (strcmp(filename, itable[inode]->filesize)) + // } + return NULL; +} #endif // SUPER_H_ diff --git a/util.c b/util.c index e639112..795ab21 100644 --- a/util.c +++ b/util.c @@ -1,6 +1,44 @@ #include "util.h" +#include +// NOTE: No idea if that's needed yet. Will see. Might or might not. +#include -int find_number_of_inodes(uint64_t fs_size){ +// DEPRECATED(?) This code will likely never be used. I have to handle it differently. +int find_number_of_inodes(uint64_t fs_size) { int number_of_inodes = fs_size / BITS_PER_INODE; return number_of_inodes; } + +// NOTE: formerly part of inode. Now transferred to util because it's both used in Inodes and the Superblock. The superblock does include inode.h, but this makes the code ordering easier to understand. +tm* get_local_time() { + return localtime(¤t_time); +} + +// TODO Implement file struct so this function can be implemented +// uint16_t calc_needed_blocks(struct *file ) + +// returns in bytes. +uint32_t get_file_size(uint16_t block_count) { + return BLOCKSIZE * block_count; +} + +// Make numbers prettier for the user. Might never be used. Inode will store file size in bytes, this should probably be implemented in user space. +uint16_t bytes_to_kb(uint32_t bytes) { + return bytes/1024; +} + +uint16_t kb_to_mb(uint16_t kb) { + return kb/1024; +} + +uint16_t mb_to_gb(uint16_t mb) { + return mb/1024; +} + +uint16_t gb_to_tb(uint16_t gb) { + return gb/1024; +} + +uint8_t tb_to_pb(uint16_t tb) { + return tb/1024; +} diff --git a/util.h b/util.h index 6afe680..417314b 100644 --- a/util.h +++ b/util.h @@ -2,18 +2,33 @@ #define BLOCK_H_ #include +#include + #define BLOCKSIZE 4096 +// NOTE: This is about how many inode get reserved per bit at fs creation. TODO: Come up witha better name. #define BITS_PER_INODE 16384 - +typedef struct tm tm; typedef struct block block; struct block { char data[BLOCKSIZE]; }; +struct tm *local_time; + +// Global Variables +time_t current_time; + // Provide fs size in bytes int find_number_of_inodes(uint64_t fs_size); +tm* get_local_time(); +uint32_t get_file_size(uint16_t block_count); +uint16_t bytes_to_kb(uint32_t bytes); +uint16_t kb_to_mb(uint16_t kb); +uint16_t mb_to_gb(uint16_t mb); +uint16_t gb_to_tb(uint16_t gb); +uint8_t tb_to_pb(uint16_t tb); #endif // BLOCK_H_