Skip to content

Commit

Permalink
Ensure superblock size always SIMPLEFS_BLOCK_SIZE
Browse files Browse the repository at this point in the history
In current implementation, the struct superblock used padding via
subtraction to match the SIMPLEFS_BLOCK_SIZE, which could lead
to the need for recalculating the padding when the data structure
changed.

This commit uses union to set struct superblock size and uses
assert macro to check the size.
  • Loading branch information
HotMercury committed May 7, 2024
1 parent a59b479 commit 0c041d1
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
Binary file added mkfs
Binary file not shown.
8 changes: 6 additions & 2 deletions mkfs.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <assert.h>
#include <fcntl.h>
#include <linux/fs.h>
#include <stdint.h>
Expand All @@ -11,8 +12,10 @@
#include "simplefs.h"

struct superblock {
struct simplefs_sb_info info;
char padding[4064]; /* Padding to match block size */
union {
struct simplefs_sb_info info;
char padding[SIMPLEFS_BLOCK_SIZE]; /* Padding to match block size */
};
};

/**
Expand Down Expand Up @@ -237,6 +240,7 @@ static int write_data_blocks(int fd, struct superblock *sb)

int main(int argc, char **argv)
{
assert(sizeof(struct superblock) == SIMPLEFS_BLOCK_SIZE);
if (argc != 2) {
fprintf(stderr, "Usage: %s disk\n", argv[0]);
return EXIT_FAILURE;
Expand Down

0 comments on commit 0c041d1

Please sign in to comment.