From b06aba1da8abd9e493d9423cdf596f0d982374c9 Mon Sep 17 00:00:00 2001 From: d0p1 Date: Sun, 21 Jul 2024 11:31:09 +0000 Subject: [PATCH] refactor: reorganize source (wip) --- .gitignore | 3 +- fuse/Makefile.am | 2 +- include/stupidfs.h | 149 +++++++++++++++++++++++++++------------------ lib/Makefile.am | 1 + lib/stpdfs.h | 126 +------------------------------------- tests/Makefile.am | 2 +- tools/Makefile.am | 2 +- tools/mkfs/main.c | 23 +++---- tools/tools.stpd | Bin 16200 -> 0 bytes 9 files changed, 110 insertions(+), 198 deletions(-) delete mode 100755 tools/tools.stpd diff --git a/.gitignore b/.gitignore index 63a6676..2bb9796 100644 --- a/.gitignore +++ b/.gitignore @@ -38,4 +38,5 @@ test_base64 *.trs stpdfs-fuse test_hchacha -test_xchacha \ No newline at end of file +test_xchacha +tools.stpd \ No newline at end of file diff --git a/fuse/Makefile.am b/fuse/Makefile.am index e05d1d7..3f2a7c1 100644 --- a/fuse/Makefile.am +++ b/fuse/Makefile.am @@ -1,4 +1,4 @@ -AM_CFLAGS = $(FUSE_CFLAGS) -I$(top_srcdir)/lib -I$(top_srcdir) +AM_CFLAGS = $(FUSE_CFLAGS) -I$(top_srcdir)/lib -I$(top_srcdir)/include -I$(top_srcdir) LDADD = $(FUSE_LIBS) ../lib/libstpdfs.a bin_PROGRAMS = stpdfs-fuse diff --git a/include/stupidfs.h b/include/stupidfs.h index 299b112..bbc0734 100644 --- a/include/stupidfs.h +++ b/include/stupidfs.h @@ -1,66 +1,35 @@ /** * \file stupidfs.h * - * StupidFS + * StupidFS Filesystem + * * ``` * ┌──────────┬───────────┬──────┬───┬──────┬────┬───┬────┐ * │Boot block│Super block│Inodes│...│Inodes│Data│...│Data│ * └──────────┴───────────┴──────┴───┴──────┴────┴───┴────┘ * ``` */ -#ifndef STPDFS_H -# define STPDFS_H 1 +#ifndef STUPIDFS_H +# define STUPIDFS_H 1 # ifndef __KERNEL__ # include # include # endif -# define STPDFS_SB_MAGIC 0x44505453 -# define STPDFS_SB_REV STPDFS_SB_REV_1 -# define STPDFS_SB_REV_1 1 - -# define STPDFS_INO_ROOTDIR 1 - -# define STPDFS_BLOCK_SIZE 512 - -# define STPDFS_NAME_MAX 28 - +# define STPDFS_BLOCK_SIZE 512 /**< StupidFS block size */ # define STPDFS_INODES_PER_BLOCK (STPDFS_BLOCK_SIZE / (sizeof(struct stpdfs_inode))) # define STPDFS_ZONES_PER_BLOCK (STPDFS_BLOCK_SIZE / sizeof(uint32_t)) # define STPDFS_DIRENT_PER_BLOCK (STPDFS_BLOCK_SIZE / (sizeof(struct stpdfs_dirent))) -# define STPDFS_NDIR 7 -# define STPDFS_ROOT_INO 1 -# define STPDFS_INO_FLAG_ALOC (1 << 15) -# define STPDFS_INO_FLAG_LZP (1 << 1) -# define STPDFS_INO_FLAG_ENC (1 << 2) +# define STPDFS_SB_MAGIC 0x44505453 /**< Superblock magic number */ +# define STPDFS_SB_REV STPDFS_SB_REV_1 /**< current revision */ +# define STPDFS_SB_REV_1 1 -# define STPDFS_S_IFMT 0xF000 -# define STPDFS_S_IFSOCK 0xA000 -# define STPDFS_S_IFLNK 0xC000 -# define STPDFS_S_IFREG 0x8000 -# define STPDFS_S_IFBLK 0x6000 -# define STPDFS_S_IFDIR 0x4000 +# define STPDFS_BADINO 0 /**< StupidFS bad inode number */ +# define STPDFS_ROOTINO 1 /**< StupidFS root inode number */ -# define STPDFS_S_ISUID 0x0800 -# define STPDFS_S_ISGID 0x0400 -# define STPDFS_S_ISVTX 0x0200 - -# define STPDFS_S_IRWXU 0x01C0 -# define STPDFS_S_IRUSR 0x0100 -# define STPDFS_S_IWUSR 0x0080 -# define STPDFS_S_IXUSR 0x0040 - -# define STPDFS_S_IRWXG 0x0038 -# define STPDFS_S_IRGRP 0x0020 -# define STPDFS_S_IWGRP 0x0010 -# define STPDFS_S_IXGRP 0x0008 - -# define STPDFS_S_IRWXO 0x0007 -# define STPDFS_S_IROTH 0x0004 -# define STPDFS_S_IWOTH 0x0002 -# define STPDFS_S_IXOTH 0x0001 +# define STPDFS_NAME_MAX 28 /**< Max filename length */ /** * \brief free block list @@ -89,8 +58,8 @@ * ``` */ struct stpdfs_free { - uint32_t free[100]; - uint8_t nfree; + uint32_t free[100]; /**< List of free block (0-99), index 0 point to next freelist */ + uint8_t nfree; /**< index */ } __attribute__((packed)); enum stpdfs_state { @@ -104,36 +73,100 @@ enum stpdfs_state { */ struct stpdfs_sb { uint32_t magic; /**< MUST be \ref STPDFS_SB_MAGIC */ - uint32_t isize; /**< size in block of the I list */ - uint32_t fsize; /**< size in block of the entire volume */ + uint32_t isize; /**< Size in block of the I list */ + uint32_t fsize; /**< Size in block of the entire volume */ struct stpdfs_free freelist; /**< \see stpdfs_free */ uint8_t revision; /**< MUST be \ref STPDFS_SB_REV */ uint16_t state; /**< \see stpdfs_state */ - uint64_t time; /**< last time the superblock was modified */ + uint64_t time; /**< Last time the superblock was modified */ } __attribute__((packed)); #define STPDFS_SB_SIZE sizeof(struct stpdfs_sb) /** * \brief StupidFS I-node + * + * ``` + * ┌────────┐ + * │ │ + * ┌───────►│ │ + * │ │ │ + * ┌──────┐ Direct│ └────────┘ + * │zone 0├───────┘ + * ├──────┤ + * │... │ + * ├──────┤ ┌────────┐ ┌────────┐ + * │zone 6│ │ ├─────►│ │ + * ├──────┤ Indirect │ │ │ │ + * │zone 7├─────────►│ │ │ │ + * ├──────┤ └────────┘ └────────┘ + * │zone 8├───────┐ + * ├──────┤ │Double indirect┌────────┐ ┌────────┐ ┌────────┐ + * │zone 9│ └──────────────►│ ├───►│ ├───►│ │ + * └──┬───┘ │ │ │ │ │ │ + * │ │ │ │ │ │ │ + * │ └────────┘ └────────┘ └────────┘ + * │ Triple indirect ┌────────┐ + * └────────────────►│ │ ┌────────┐ ┌────────┐ ┌────────┐ + * │ ├───►│ │ │ │ │ │ + * │ │ │ ├─────►│ ├─────►│ │ + * └────────┘ │ │ │ │ │ │ + * └────────┘ └────────┘ └────────┘ + * ``` */ struct stpdfs_inode { - uint16_t mode; /**< file mode */ - uint16_t nlink; /**< link count */ - uint16_t uid; /**< owner user id */ - uint16_t gid; /**< group id */ - uint16_t flags; - uint32_t size; + uint16_t mode; /**< File mode */ + uint16_t nlink; /**< Link count */ + uint16_t uid; /**< Owner user id */ + uint16_t gid; /**< Group id */ + uint16_t flags; /** File flags */ + uint32_t size; /** Data size in byte */ uint32_t zones[10]; - uint64_t actime; - uint64_t modtime; + uint64_t actime; /**< Access time */ + uint64_t modtime; /**< Modification time */ } __attribute__((packed)); #define STPDFS_INODE_SIZE sizeof(struct inode) +# define STPDFS_NDIR 7 /**< Number of direct block */ + +# define STPDFS_INO_FLAG_ALOC (1 << 15) +# define STPDFS_INO_FLAG_LZP (1 << 1) +# define STPDFS_INO_FLAG_ENC (1 << 2) + + +# define STPDFS_S_IFMT 0xF000 +# define STPDFS_S_IFSOCK 0xA000 +# define STPDFS_S_IFLNK 0xC000 +# define STPDFS_S_IFREG 0x8000 +# define STPDFS_S_IFBLK 0x6000 +# define STPDFS_S_IFDIR 0x4000 + +# define STPDFS_S_ISUID 0x0800 +# define STPDFS_S_ISGID 0x0400 +# define STPDFS_S_ISVTX 0x0200 + +# define STPDFS_S_IRWXU 0x01C0 +# define STPDFS_S_IRUSR 0x0100 +# define STPDFS_S_IWUSR 0x0080 +# define STPDFS_S_IXUSR 0x0040 + +# define STPDFS_S_IRWXG 0x0038 +# define STPDFS_S_IRGRP 0x0020 +# define STPDFS_S_IWGRP 0x0010 +# define STPDFS_S_IXGRP 0x0008 + +# define STPDFS_S_IRWXO 0x0007 +# define STPDFS_S_IROTH 0x0004 +# define STPDFS_S_IWOTH 0x0002 +# define STPDFS_S_IXOTH 0x0001 + +/** + * \brief StupidFS directory entry + */ struct stpdfs_dirent { - uint32_t inode; - char filename[STPDFS_NAME_MAX]; + uint32_t inode; /**< inode containing file */ + char filename[STPDFS_NAME_MAX]; /** null terminated file name (\see STPDFS_NAME_MAX) */ }; #define STPDFS_DIRENT_SIZE sizeof(struct stpdfs_dirent) -#endif /* !STPDFS_H */ \ No newline at end of file +#endif /* !STPIDFS_H */ \ No newline at end of file diff --git a/lib/Makefile.am b/lib/Makefile.am index 8ef82e7..0ce3799 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -1,4 +1,5 @@ noinst_LIBRARIES = libstpdfs.a +AM_CFLAGS = -I$(top_srcdir)/include -I$(top_srcdir)/lib -I$(top_srcdir) libstpdfs_a_SOURCES = block.c \ inode.c \ dir.c \ diff --git a/lib/stpdfs.h b/lib/stpdfs.h index ef52273..b966152 100644 --- a/lib/stpdfs.h +++ b/lib/stpdfs.h @@ -11,134 +11,10 @@ #ifndef STPDFS_H # define STPDFS_H 1 +# include # include # include -# define STPDFS_SB_MAGIC 0x44505453 -# define STPDFS_SB_REV STPDFS_SB_REV_1 -# define STPDFS_SB_REV_1 1 - -# define STPDFS_INO_ROOTDIR 1 - -# define STPDFS_BLOCK_SIZE_BITS 9 -# define STPDFS_BLOCK_SIZE (1 << STPDFS_BLOCK_SIZE_BITS) - -# define STPDFS_NAME_MAX 28 - -# define STPDFS_INODES_PER_BLOCK (STPDFS_BLOCK_SIZE / (sizeof(struct stpdfs_inode))) -# define STPDFS_ZONES_PER_BLOCK (STPDFS_BLOCK_SIZE / sizeof(uint32_t)) -# define STPDFS_DIRENT_PER_BLOCK (STPDFS_BLOCK_SIZE / (sizeof(struct stpdfs_dirent))) -# define STPDFS_NDIR 7 -# define STPDFS_ROOT_INO 1 - -# define STPDFS_INO_FLAG_ALOC (1 << 15) -# define STPDFS_INO_FLAG_LARGE (1 << 0) -# define STPDFS_INO_FLAG_LZP (1 << 1) -# define STPDFS_INO_FLAG_ENC (1 << 2) - -# define STPDFS_S_IFMT 0xF000 -# define STPDFS_S_IFSOCK 0xA000 -# define STPDFS_S_IFLNK 0xC000 -# define STPDFS_S_IFREG 0x8000 -# define STPDFS_S_IFBLK 0x6000 -# define STPDFS_S_IFDIR 0x4000 - -# define STPDFS_S_ISUID 0x0800 -# define STPDFS_S_ISGID 0x0400 -# define STPDFS_S_ISVTX 0x0200 - -# define STPDFS_S_IRWXU 0x01C0 -# define STPDFS_S_IRUSR 0x0100 -# define STPDFS_S_IWUSR 0x0080 -# define STPDFS_S_IXUSR 0x0040 - -# define STPDFS_S_IRWXG 0x0038 -# define STPDFS_S_IRGRP 0x0020 -# define STPDFS_S_IWGRP 0x0010 -# define STPDFS_S_IXGRP 0x0008 - -# define STPDFS_S_IRWXO 0x0007 -# define STPDFS_S_IROTH 0x0004 -# define STPDFS_S_IWOTH 0x0002 -# define STPDFS_S_IXOTH 0x0001 - -typedef uint32_t zone_t; /**< zone number */ -typedef uint32_t block_t; /**< block number */ - -/** - * \brief free block list - * ``` - * ┌──────────┐ - * │ block 99 │ - * ├──────────┤ - * │ block 98 │ - * ├──────────┤ - * │ ... │ - * ├──────────┤ - * │ block 2 │ - * ├──────────┤ - * │ block 1 │ - * ├──────────┤ ┌──────────┐ - * │ block 0 ├───►│ block 99 │ - * └──────────┘ ├──────────┤ - * │ ... │ - * ├──────────┤ ┌──────────┐ - * │ block 0 ├──►│ block 99 │ - * └──────────┘ ├──────────┤ - * │ ... │ - * ├──────────┤ - * │ block 0 │ - * └──────────┘ - * ``` - */ -struct stpdfs_free { - block_t free[100]; - uint8_t nfree; -} __attribute__((packed)); - -enum stpdfs_state { - STPDFS_CLEANLY_UNMOUNTED = 0, - STPDFS_ERROR = 1, - STPDFS_DIRTY = 1, -}; - -/** - * \brief StupidFS Superblock - */ -struct stpdfs_sb { - uint32_t magic; /**< MUST be \ref STPDFS_SB_MAGIC */ - uint32_t isize; /**< size in block of the I list */ - uint32_t fsize; /**< size in block of the entire volume */ - struct stpdfs_free freelist; /**< \see stpdfs_free */ - uint8_t revision; /**< MUST be \ref STPDFS_SB_REV */ - uint16_t state; /**< \see stpdfs_state */ - uint64_t time; /**< last time the superblock was modified */ -} __attribute__((packed)); - -#define STPDFS_SB_SIZE sizeof(struct stpdfs_sb) - -/** - * \brief StupidFS I-node - */ -struct stpdfs_inode { - uint16_t mode; /**< file mode */ - uint16_t nlink; /**< link count */ - uint16_t uid; /**< owner user id */ - uint16_t gid; /**< group id */ - uint16_t flags; - uint32_t size; - zone_t zones[10]; - uint64_t actime; - uint64_t modtime; -} __attribute__((packed)); - -#define STPDFS_INODE_SIZE sizeof(struct inode) - -struct stpdfs_dirent { - uint32_t inode; - char filename[STPDFS_NAME_MAX]; -}; - /* * API */ diff --git a/tests/Makefile.am b/tests/Makefile.am index 670acee..3d9aeb7 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -1,4 +1,4 @@ -AM_CPPFLAGS = -I$(top_srcdir)/lib +AM_CPPFLAGS = -I$(top_srcdir)/lib -I$(top_srcdir)/include TESTS = test_lzp test_hchacha test_xchacha check_PROGRAMS = test_lzp test_hchacha test_xchacha diff --git a/tools/Makefile.am b/tools/Makefile.am index dd548dd..86a5a32 100644 --- a/tools/Makefile.am +++ b/tools/Makefile.am @@ -1,4 +1,4 @@ -AM_CFLAGS = -I$(top_srcdir)/lib -I$(top_srcdir) +AM_CFLAGS = -I$(top_srcdir)/include -I$(top_srcdir)/lib -I$(top_srcdir) LDADD = ../lib/libstpdfs.a bin_PROGRAMS = mkfs.stpd tools.stpd diff --git a/tools/mkfs/main.c b/tools/mkfs/main.c index 153b2ff..70af679 100644 --- a/tools/mkfs/main.c +++ b/tools/mkfs/main.c @@ -2,12 +2,13 @@ #include #include #include -#include #include #include #include #include #include +#include +#include #ifdef HAVE_CONFIG_H # include "config.h" #endif /* HAVE_CONFIG_H */ @@ -146,20 +147,20 @@ mkfs() /* create root dir */ stpdfs_read(fd, 2, &inds, sizeof(struct stpdfs_inode) * STPDFS_INODES_PER_BLOCK); - inds[STPDFS_INO_ROOTDIR].modtime = time(NULL); - inds[STPDFS_INO_ROOTDIR].actime = time(NULL); - inds[STPDFS_INO_ROOTDIR].size = sizeof(struct stpdfs_dirent) * 2; - inds[STPDFS_INO_ROOTDIR].flags = STPDFS_INO_FLAG_ALOC; - inds[STPDFS_INO_ROOTDIR].mode = STPDFS_S_IFDIR | STPDFS_S_IRUSR | STPDFS_S_IWUSR | STPDFS_S_IXUSR | STPDFS_S_IRGRP | STPDFS_S_IXGRP | STPDFS_S_IXOTH; - inds[STPDFS_INO_ROOTDIR].zones[0] = stpdfs_alloc_block(fd, &super); - inds[STPDFS_INO_ROOTDIR].size = sizeof(struct stpdfs_dirent) * 2; - stpdfs_read(fd, inds[STPDFS_INO_ROOTDIR].zones[0], rootdirent, sizeof(struct stpdfs_dirent) * 2); + inds[STPDFS_ROOTINO].modtime = time(NULL); + inds[STPDFS_ROOTINO].actime = time(NULL); + inds[STPDFS_ROOTINO].size = sizeof(struct stpdfs_dirent) * 2; + inds[STPDFS_ROOTINO].flags = STPDFS_INO_FLAG_ALOC; + inds[STPDFS_ROOTINO].mode = STPDFS_S_IFDIR | STPDFS_S_IRUSR | STPDFS_S_IWUSR | STPDFS_S_IXUSR | STPDFS_S_IRGRP | STPDFS_S_IXGRP | STPDFS_S_IXOTH; + inds[STPDFS_ROOTINO].zones[0] = stpdfs_alloc_block(fd, &super); + inds[STPDFS_ROOTINO].size = sizeof(struct stpdfs_dirent) * 2; + stpdfs_read(fd, inds[STPDFS_ROOTINO].zones[0], rootdirent, sizeof(struct stpdfs_dirent) * 2); strcpy(rootdirent[0].filename, "."); rootdirent[1].inode = 1; strcpy(rootdirent[1].filename, ".."); rootdirent[1].inode = 1; - inds[STPDFS_INO_ROOTDIR].nlink += 2; - stpdfs_write(fd, inds[STPDFS_INO_ROOTDIR].zones[0], rootdirent, sizeof(struct stpdfs_dirent) * 2); + inds[STPDFS_ROOTINO].nlink += 2; + stpdfs_write(fd, inds[STPDFS_ROOTINO].zones[0], rootdirent, sizeof(struct stpdfs_dirent) * 2); stpdfs_write(fd, 2, &inds, sizeof(struct stpdfs_inode) * STPDFS_INODES_PER_BLOCK); /* write super block */ diff --git a/tools/tools.stpd b/tools/tools.stpd deleted file mode 100755 index 98c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