refactor: remove libstpdfs

This commit is contained in:
d0p1 🏳️‍⚧️ 2024-08-15 17:32:20 +02:00
parent a9874daf28
commit a6011f24a0
14 changed files with 73 additions and 662 deletions

View file

@ -1,4 +1,4 @@
SUBDIRS = libstpdfs libcrypto liblzp libfs tools
SUBDIRS = libcrypto liblzp libfs tools
if BUILD_FUSE
SUBDIRS += fuse

View file

@ -51,7 +51,6 @@ AC_CONFIG_FILES([Makefile
libcrypto/tests/Makefile
liblzp/Makefile
liblzp/tests/Makefile
libstpdfs/Makefile
libfs/Makefile
tools/Makefile
linux/Makefile

View file

@ -20,5 +20,6 @@ struct fs_inode *fs_inode_get(struct fs_super *super, uint32_t inum);
int fs_inode_update(struct fs_inode *ip);
void fs_inode_release(struct fs_inode *ip);
struct fs_inode *fs_inode_read(struct fs_inode *ip);
struct fs_inode *fs_inode_alloc(struct fs_super *super);
#endif /* !FS_INODE_H */

View file

@ -1,3 +0,0 @@
AM_CFLAGS = -I$(top_srcdir)/include -I$(top_srcdir)
noinst_LIBRARIES = libstpdfs.a
libstpdfs_a_SOURCES = super.c bio.c inode.c file.c dir.c

View file

@ -1,128 +0,0 @@
#include "stupidfs.h"
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <libstpdfs/stpdfs.h>
static struct stpdfs_buffer *head = NULL;
size_t
stpdfs_write(int fd, uint32_t blocknum, void *data, size_t size)
{
uint8_t buffer[STPDFS_BLOCK_SIZE];
if (size > STPDFS_BLOCK_SIZE) return (-1);
lseek(fd, blocknum * STPDFS_BLOCK_SIZE, SEEK_SET);
if (size > 0)
{
memcpy(buffer, data, size);
return (write(fd, buffer, STPDFS_BLOCK_SIZE));
}
return (write(fd, data, size));
}
size_t
stpdfs_read(int fd, uint32_t blocknum, void *data, size_t size)
{
lseek(fd, blocknum * STPDFS_BLOCK_SIZE, SEEK_SET);
return (read(fd, data, size));
}
static struct stpdfs_buffer *
bget(int fd, uint32_t blocknum)
{
struct stpdfs_buffer *buff;
for (buff = head; buff != NULL; buff = buff->next)
{
if (buff->blocknum == blocknum)
{
buff->refcount++;
return (buff);
}
}
buff = (struct stpdfs_buffer *)malloc(sizeof(struct stpdfs_buffer));
if (buff == NULL) return (NULL);
buff->next = head;
buff->blocknum = blocknum;
buff->valid = 0;
buff->refcount = 1;
buff->fd = fd;
head = buff;
return (buff);
}
struct stpdfs_buffer *
stpdfs_bread(int fd, uint32_t blocknum)
{
struct stpdfs_buffer *buff;
buff = bget(fd, blocknum);
if (buff == NULL)
{
return (NULL);
}
if (buff->valid == 0)
{
stpdfs_read(fd, blocknum, buff->data, STPDFS_BLOCK_SIZE);
buff->valid = 1;
}
return (buff);
}
void
stpdfs_bwrite(struct stpdfs_buffer *buff)
{
stpdfs_write(buff->fd, buff->blocknum, buff->data, STPDFS_BLOCK_SIZE);
}
void
stpdfs_brelse(struct stpdfs_buffer *buff)
{
struct stpdfs_buffer *tmp;
buff->refcount--;
if (buff->refcount > 0) return;
if (buff == head)
{
head = buff->next;
}
else
{
for (tmp = head; tmp != NULL; tmp = tmp->next)
{
if (tmp->next == buff)
{
tmp->next = buff->next;
break;
}
}
}
free(buff);
}
void
stpdfs_bpin(struct stpdfs_buffer *buff)
{
buff->refcount++;
}
void
stpdfs_bunpin(struct stpdfs_buffer *buff)
{
buff->refcount--;
}

View file

View file

@ -1,52 +0,0 @@
#include <stdlib.h>
#include <libstpdfs/stpdfs.h>
static struct stpdfs_file *head = NULL;
struct stpdfs_file *
stpdfs_file_alloc(void)
{
struct stpdfs_file *f;
f = (struct stpdfs_file *)malloc(sizeof(struct stpdfs_file));
if (f == NULL)
{
return (NULL);
}
f->refcount = 1;
f->ip = NULL;
f->offset = 0;
f->next = head;
head = f;
return (f);
}
void
stpdfs_file_close(struct stpdfs_file *f)
{
struct stpdfs_file *tmp;
f->refcount--;
if (f->refcount > 0) return;
if (f == head)
{
head = f->next;
}
else
{
for (tmp = head; tmp != NULL; tmp = tmp->next)
{
if (tmp->next == f)
{
tmp->next = f->next;
break;
}
}
}
free(f);
}

View file

@ -1,251 +0,0 @@
#include "stupidfs.h"
#include <stdlib.h>
#include <libstpdfs/stpdfs.h>
#include <stdint.h>
#include <string.h>
#ifndef MIN
# define MIN(x, y) (((x) < (y)) ? (x) : (y))
#endif /* !MIN */
#define INODE_CACHE 50
#define IBLOCK(x) (x / STPDFS_INODES_PER_BLOCK + 2)
static struct stpdfs_inode_info *head;
struct stpdfs_inode_info *
stpdfs_inode_get(struct stpdfs_super_info *sbi, uint32_t inum)
{
struct stpdfs_inode_info *ip;
struct stpdfs_buffer *buff;
struct stpdfs_inode *dinode;
int idx;
for (ip = head; ip != NULL; ip = ip->next)
{
if (ip->inum == inum)
{
ip->refcount++;
return (ip);
}
}
ip = (struct stpdfs_inode_info *)malloc(sizeof(struct stpdfs_inode_info));
if (ip == NULL) return (NULL);
ip->sbi = sbi;
ip->inum = inum;
ip->refcount = 1;
ip->valid = 1;
buff = stpdfs_bread(sbi->fd, IBLOCK(inum));
dinode = (struct stpdfs_inode *)buff->data + inum % STPDFS_INODES_PER_BLOCK;
memcpy(&ip->inode, dinode, sizeof(struct stpdfs_inode));
stpdfs_brelse(buff);
return (ip);
}
struct stpdfs_inode_info *
stpdfs_inode_alloc(struct stpdfs_super_info *sbi)
{
uint32_t inum;
struct stpdfs_buffer *buff;
struct stpdfs_inode *dinode;
for (inum = 1; inum < sbi->sb.isize; inum++)
{
buff = stpdfs_bread(sbi->fd, IBLOCK(inum));
dinode = (struct stpdfs_inode *)buff->data + inum % STPDFS_INODES_PER_BLOCK;
if ((dinode->flags & STPDFS_INO_FLAG_ALOC) == 0)
{
memset(dinode, 0, sizeof(struct stpdfs_inode));
dinode->flags = STPDFS_INO_FLAG_ALOC;
stpdfs_bwrite(buff);
stpdfs_brelse(buff);
return (stpdfs_inode_get(sbi, inum));
}
stpdfs_brelse(buff);
}
return (NULL);
}
void
stpdfs_inode_update(struct stpdfs_inode_info *ip)
{
struct stpdfs_buffer *buff;
struct stpdfs_inode *dinode;
buff = stpdfs_bread(ip->sbi->fd, IBLOCK(ip->inum));
dinode = (struct stpdfs_inode *)buff->data + ip->inum % STPDFS_INODES_PER_BLOCK;
memcpy(dinode, &ip->inode, sizeof(struct stpdfs_inode));
stpdfs_bwrite(buff);
stpdfs_brelse(buff);
}
static int
bmap(struct stpdfs_inode_info *ip, uint32_t blocknum)
{
uint32_t *addrs;
uint32_t index;
struct stpdfs_buffer *buff;
if (blocknum < STPDFS_NDIR)
{
if (ip->inode.zones[blocknum] == 0)
{
ip->inode.zones[blocknum] = stpdfs_super_balloc(ip->sbi);
}
return (ip->inode.zones[blocknum]);
}
index = blocknum - STPDFS_NDIR;
if (blocknum < (STPDFS_BLOCK_SIZE/sizeof(int32_t) + STPDFS_SIND))
{
if (ip->inode.zones[STPDFS_SIND] == 0)
{
ip->inode.zones[STPDFS_SIND] = stpdfs_super_balloc(ip->sbi);
}
buff = stpdfs_bread(ip->sbi->fd, ip->inode.zones[STPDFS_SIND]);
addrs = (uint32_t *)buff->data;
if (addrs[index] == 0)
{
addrs[index] = stpdfs_super_balloc(ip->sbi);
}
stpdfs_brelse(buff);
return (addrs[index]);
}
/* TODO double and triple ind */
return (0);
}
int
stpdfs_inode_read(struct stpdfs_inode_info *ip, uint8_t *dest, size_t offset, size_t size)
{
size_t total;
size_t rd;
uint32_t zone;
struct stpdfs_buffer *buff;
if (offset > ip->inode.size)
{
return (0);
}
total = 0;
while (total < size)
{
zone = bmap(ip, offset/STPDFS_BLOCK_SIZE);
if (zone == 0) return (total);
buff = stpdfs_bread(ip->sbi->fd, zone);
rd = MIN(size - total, STPDFS_BLOCK_SIZE - offset % STPDFS_BLOCK_SIZE);
memcpy(dest, buff->data + (offset % STPDFS_BLOCK_SIZE), rd);
stpdfs_brelse(buff);
total += rd;
offset += rd;
dest += rd;
}
return (total);
}
int
stpdfs_inode_write(struct stpdfs_inode_info *ip, const uint8_t *src, size_t offset, size_t size)
{
size_t total;
size_t rd;
uint32_t zone;
struct stpdfs_buffer *buff;
if (offset > ip->inode.size)
{
return (0);
}
total = 0;
while (total < size)
{
zone = bmap(ip, offset/STPDFS_BLOCK_SIZE);
if (zone == 0) break;
buff = stpdfs_bread(ip->sbi->fd, zone);
rd = MIN(size - total, STPDFS_BLOCK_SIZE - offset % STPDFS_BLOCK_SIZE);
memcpy(buff->data + (offset % STPDFS_BLOCK_SIZE), src, rd);
stpdfs_bwrite(buff);
stpdfs_brelse(buff);
total += rd;
offset += rd;
src += rd;
}
if (offset > ip->inode.size)
{
ip->inode.size = offset;
}
stpdfs_inode_update(ip);
return (total);
}
struct stpdfs_inode_info *
stpdfs_dirlookup(struct stpdfs_inode_info *dp, const char *name, size_t *offset)
{
struct stpdfs_dirent dirent;
size_t idx;
if (!(dp->inode.flags & STPDFS_S_IFDIR))
{
return (NULL);
}
for (idx = 0; idx < dp->inode.size; idx += STPDFS_DIRENT_SIZE)
{
if (stpdfs_inode_read(dp, (uint8_t *)&dirent, idx, STPDFS_DIRENT_SIZE) != STPDFS_DIRENT_SIZE)
{
return (NULL);
}
if (strncmp(name, dirent.filename, STPDFS_NAME_MAX))
{
if (offset) *offset = idx;
return (stpdfs_inode_get(dp->sbi, dirent.inode));
}
}
return (NULL);
}
int
stpdfs_dirlink(struct stpdfs_inode_info *dp, const char *name, uint32_t inum)
{
struct stpdfs_inode_info *ip;
ip = stpdfs_dirlookup(dp, name, 0);
return (0);
}
int
stpdfs_create(struct stpdfs_inode_info *dp, struct stpdfs_dirent *dirent, uint16_t mode)
{
struct stpdfs_inode_info *ip;
struct stpdfs_buffer *buff;
uint32_t blocknum;
ip = stpdfs_inode_alloc(dp->sbi);
ip->inode.mode = mode;
dirent->inode = ip->inum;
stpdfs_inode_write(dp, (uint8_t *)dirent, dp->inode.size, STPDFS_DIRENT_SIZE);
stpdfs_inode_update(ip);
stpdfs_inode_update(dp);
return (0);
}

View file

@ -1,67 +0,0 @@
/**
* \file stpdfs.h
*/
#ifndef STPDFS_H
# define STPDFS_H 1
# include <stupidfs.h>
struct stpdfs_super_info {
struct stpdfs_sb sb;
int fd;
};
struct stpdfs_inode_info {
int valid;
int refcount;
uint32_t inum;
struct stpdfs_super_info *sbi;
struct stpdfs_inode inode;
struct stpdfs_inode_info *next;
};
struct stpdfs_file {
int refcount;
struct stpdfs_inode_info *ip;
size_t offset;
struct stpdfs_file *next;
};
struct stpdfs_buffer {
int valid;
uint32_t blocknum;
int refcount;
int fd;
struct stpdfs_buffer *next;
uint8_t data[STPDFS_BLOCK_SIZE];
};
/* bio.c */
size_t stpdfs_write(int fd, uint32_t blocknum, void *data, size_t size);
size_t stpdfs_read(int fd, uint32_t blocknum, void *data, size_t size);
struct stpdfs_buffer *stpdfs_bread(int fd, uint32_t blocknum);
void stpdfs_bwrite(struct stpdfs_buffer *buff);
void stpdfs_brelse(struct stpdfs_buffer *buff);
void stpdfs_bpin(struct stpdfs_buffer *buff);
/* super.c */
int stpdfs_super_open(struct stpdfs_super_info *sbi, const char *fname);
int stpdfs_read_super(struct stpdfs_super_info *sbi, int fd);
int stpdfs_super_validate(struct stpdfs_sb *sb);
int stpdfs_super_kill(struct stpdfs_super_info *sbi);
uint32_t stpdfs_super_balloc(struct stpdfs_super_info *sbi);
int stpdfs_super_bfreee(struct stpdfs_super_info *sbi, uint32_t blocknum);
uint32_t stpdfs_alloc_block(int fd, struct stpdfs_sb *sb);
int stpdfs_free_block(int fd, struct stpdfs_sb *sb, uint32_t blocknum);
struct stpdfs_inode_info *stpdfs_inode_get(struct stpdfs_super_info *sbi, uint32_t inum);
int stpdfs_inode_read(struct stpdfs_inode_info *ip, uint8_t *dest, size_t offset, size_t size);
int stpdfs_create(struct stpdfs_inode_info *dp, struct stpdfs_dirent *dirent, uint16_t mode);
#endif /* !STPDFS_H */

View file

@ -1,149 +0,0 @@
#include "stupidfs.h"
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include <time.h>
#include <libstpdfs/stpdfs.h>
int
stpdfs_super_validate(struct stpdfs_sb *sb)
{
if (sb->magic != STPDFS_SB_MAGIC)
{
return (-1);
}
if (sb->revision != STPDFS_SB_REV)
{
return (-1);
}
if (sb->fsize == 0 || sb->isize == 0)
{
return (-1);
}
if (sb->isize > sb->fsize)
{
return (-1);
}
return (0);
}
int
stpdfs_super_open(struct stpdfs_super_info *sbi, const char *fname)
{
struct stpdfs_buffer *buff;
sbi->fd = open(fname, O_RDWR | O_BINARY);
if (sbi->fd < 0)
{
perror(fname);
return (-1);
}
buff = stpdfs_bread(sbi->fd, 1);
if (!buff) goto err;
if (stpdfs_super_validate((struct stpdfs_sb *)buff->data) != 0) goto err;
memcpy(&sbi->sb, buff->data, sizeof(struct stpdfs_sb));
return (0);
err:
close(sbi->fd);
return (-1);
}
int
stpdfs_super_kill(struct stpdfs_super_info *sbi)
{
struct stpdfs_buffer *buff;
buff = stpdfs_bread(sbi->fd, 1);
if (buff == NULL) goto end;
memcpy(buff->data, &sbi->sb, sizeof(struct stpdfs_sb));
stpdfs_bwrite(buff);
stpdfs_brelse(buff);
end:
close(sbi->fd);
return (0);
}
uint32_t
stpdfs_super_balloc(struct stpdfs_super_info *sbi)
{
uint32_t blocknum;
struct stpdfs_free freelist;
struct stpdfs_buffer *buff;
sbi->sb.state = STPDFS_DIRTY;
redo:
sbi->sb.freelist.nfree--;
blocknum = sbi->sb.freelist.free[sbi->sb.freelist.nfree];
if (sbi->sb.freelist.nfree == 0 && blocknum != 0)
{
buff = stpdfs_bread(sbi->fd, blocknum);
memcpy(&sbi->sb.freelist, buff->data, sizeof(struct stpdfs_free));
goto redo;
}
buff = stpdfs_bread(sbi->fd, 1);
memcpy(buff->data, &sbi->sb, sizeof(struct stpdfs_sb));
stpdfs_bwrite(buff);
buff = stpdfs_bread(sbi->fd, blocknum);
memset(buff->data, 0, STPDFS_BLOCK_SIZE);
stpdfs_bwrite(buff);
stpdfs_brelse(buff);
sbi->sb.time = time(NULL);
return (blocknum);
}
int
stpdfs_super_bfreee(struct stpdfs_super_info *sbi, uint32_t blocknum)
{
struct stpdfs_free copy;
struct stpdfs_buffer *buff;
if (blocknum == 0 || blocknum >= sbi->sb.fsize)
{
return (-1);
}
sbi->sb.state = STPDFS_DIRTY;
if (sbi->sb.freelist.nfree == 100)
{
buff = stpdfs_bread(sbi->fd, blocknum);
memcpy(buff->data, &sbi->sb.freelist, sizeof(struct stpdfs_free));
stpdfs_bwrite(buff);
stpdfs_brelse(buff);
sbi->sb.freelist.nfree = 1;
sbi->sb.freelist.free[0] = blocknum;
}
else
{
sbi->sb.freelist.free[sbi->sb.freelist.nfree++] = blocknum;
}
sbi->sb.time = time(NULL);
buff = stpdfs_bread(sbi->fd, 1);
memcpy(buff->data, &sbi->sb, sizeof(struct stpdfs_sb));
stpdfs_bwrite(buff);
return (0);
}

View file

@ -1,5 +1,5 @@
AM_CFLAGS = -I$(top_srcdir)/include -I$(top_srcdir)/lib -I$(top_srcdir)
LDADD = ../libstpdfs/libstpdfs.a ../libfs/libfs.a
LDADD = ../libfs/libfs.a
bin_PROGRAMS = mkfs.stpd tools.stpd inspect.stpd

View file

@ -1,5 +1,8 @@
#include "libfs/bio/bio.h"
#include "libfs/inode.h"
#include "libfs/super.h"
#include "stupidfs.h"
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
@ -37,8 +40,11 @@ inspect(void)
{
struct fs_super sb;
struct fs_inode *ip;
struct fs_buffer *buff;
time_t time;
int idx;
int j;
char c;
if (fs_super_open(&sb, device) != 0)
{
@ -74,6 +80,9 @@ inspect(void)
printf("Inode %d:\n", inode);
printf(" mode: %ho\n", ip->inode.mode);
printf(" nlink: %hu\n", ip->inode.nlink);
printf(" uid: %hx\n", ip->inode.uid);
printf(" gid: %hx\n", ip->inode.gid);
printf(" flags: %hx\n", ip->inode.flags);
printf(" size: %u\n", ip->inode.size);
time = ip->inode.actime;
printf(" actime: %s", ctime(&time));
@ -81,6 +90,38 @@ inspect(void)
printf(" modtime: %s", ctime(&time));
}
if (block >= 0)
{
buff = fs_bio_bread(sb.fd, block);
if (buff != NULL)
{
idx = 0;
while (idx < STPDFS_BLOCK_SIZE)
{
printf("\033[32m%04x\033[0m\t", idx);
for (j = 0; j < 8; j++)
{
printf("%02x ", buff->data[idx+j]);
}
printf(" | ");
for (j = 0; j < 8; j++)
{
c = buff->data[idx++];
if (isgraph(c))
{
printf("\033[31m%c\033[0m", c);
}
else
{
printf(".");
}
}
printf("\n");
}
fs_bio_brelse(buff);
}
}
fs_super_kill(&sb);
return (EXIT_SUCCESS);

View file

@ -1,7 +1,11 @@
#include "libfs/dir.h"
#include "libfs/inode.h"
#include "libfs/super.h"
#include "stupidfs.h"
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <time.h>
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif /* HAVE_CONFIG_H */
@ -43,23 +47,39 @@ usage(int retval)
int
do_copy(void)
{
struct stpdfs_super_info sbi;
struct stpdfs_inode_info *ip;
struct stpdfs_dirent dirent;
struct fs_super super;
struct fs_inode *ip;
struct fs_inode *dp;
struct stat st;
stat(src, &st);
strcpy(dirent.filename, dest);
if (stpdfs_super_open(&sbi, image))
if (fs_super_open(&super, image))
{
return (EXIT_FAILURE);
}
ip = stpdfs_inode_get(&sbi, STPDFS_ROOTINO);
dp = fs_inode_get(&super, STPDFS_ROOTINO);
if (dp->valid == 0)
{
fs_inode_read(ip);
}
stpdfs_create(ip, &dirent, st.st_mode);
ip = fs_inode_alloc(&super);
ip->inode.mode = st.st_mode;
ip->inode.uid = st.st_uid;
ip->inode.gid = st.st_gid;
ip->inode.modtime = st.st_mtime;
ip->inode.actime = st.st_atime;
stpdfs_super_kill(&sbi);
fs_inode_update(ip);
fs_dir_link(dp, dest, ip->inum);
fs_inode_release(dp);
fs_inode_release(ip);
fs_super_kill(&super);
return (EXIT_SUCCESS);
}

View file

@ -38,7 +38,7 @@ usage(int retval)
}
else
{
printf("Usage: %s copy -i /dev/name [path]\n", prg_name);
printf("Usage: %s ls -i /dev/name [path]\n", prg_name);
}
exit(retval);