feat(tools): list file in root dir

This commit is contained in:
d0p1 🏳️‍⚧️ 2024-07-28 13:22:32 +02:00
parent c5f4b5878b
commit 7043ece1c3
5 changed files with 97 additions and 7 deletions

View file

@ -97,7 +97,13 @@ stpdfs_brelse(struct stpdfs_buffer *buff)
struct stpdfs_buffer *tmp; struct stpdfs_buffer *tmp;
buff->refcount--; buff->refcount--;
if (buff->refcount <= 0) if (buff->refcount > 0) return;
if (buff == head)
{
head = buff->next;
}
else
{ {
for (tmp = head; tmp != NULL; tmp = tmp->next) for (tmp = head; tmp != NULL; tmp = tmp->next)
{ {

View file

@ -16,6 +16,8 @@ struct stpdfs_inode_info *
stpdfs_inode_get(struct stpdfs_super_info *sbi, uint32_t inum) stpdfs_inode_get(struct stpdfs_super_info *sbi, uint32_t inum)
{ {
struct stpdfs_inode_info *empty; struct stpdfs_inode_info *empty;
struct stpdfs_buffer *buff;
struct stpdfs_inode *dinode;
int idx; int idx;
empty = NULL; empty = NULL;
@ -38,7 +40,12 @@ stpdfs_inode_get(struct stpdfs_super_info *sbi, uint32_t inum)
empty->sbi = sbi; empty->sbi = sbi;
empty->inum = inum; empty->inum = inum;
empty->refcount = 1; empty->refcount = 1;
empty->valid = 0;
empty->valid = 1;
buff = stpdfs_bread(sbi->fd, IBLOCK(inum));
dinode = (struct stpdfs_inode *)buff->data + inum % STPDFS_INODES_PER_BLOCK;
memcpy(&empty->inode, dinode, sizeof(struct stpdfs_inode));
stpdfs_brelse(buff);
return (empty); return (empty);
} }

View file

@ -49,6 +49,7 @@ void stpdfs_brelse(struct stpdfs_buffer *buff);
void stpdfs_bpin(struct stpdfs_buffer *buff); void stpdfs_bpin(struct stpdfs_buffer *buff);
/* super.c */ /* 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_read_super(struct stpdfs_super_info *sbi, int fd);
int stpdfs_super_validate(struct stpdfs_sb *sb); int stpdfs_super_validate(struct stpdfs_sb *sb);
int stpdfs_super_kill(struct stpdfs_super_info *sbi); int stpdfs_super_kill(struct stpdfs_super_info *sbi);
@ -58,4 +59,7 @@ int stpdfs_super_bfreee(struct stpdfs_super_info *sbi, uint32_t blocknum);
uint32_t stpdfs_alloc_block(int fd, struct stpdfs_sb *sb); uint32_t stpdfs_alloc_block(int fd, struct stpdfs_sb *sb);
int stpdfs_free_block(int fd, struct stpdfs_sb *sb, uint32_t blocknum); 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);
#endif /* !STPDFS_H */ #endif /* !STPDFS_H */

View file

@ -7,8 +7,7 @@
#include <fcntl.h> #include <fcntl.h>
#include <unistd.h> #include <unistd.h>
#include <time.h> #include <time.h>
#include <stupidfs.h> #include <libstpdfs/stpdfs.h>
#include <stpdfs.h>
#ifdef HAVE_CONFIG_H #ifdef HAVE_CONFIG_H
# include "config.h" # include "config.h"
#endif /* HAVE_CONFIG_H */ #endif /* HAVE_CONFIG_H */

View file

@ -1,3 +1,5 @@
#include "stupidfs.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#ifdef HAVE_CONFIG_H #ifdef HAVE_CONFIG_H
@ -9,16 +11,88 @@
#ifdef HAVE_GETOPT_H #ifdef HAVE_GETOPT_H
# include <getopt.h> # include <getopt.h>
#endif /* HAVE_GETOPT_H */ #endif /* HAVE_GETOPT_H */
#include <libstpdfs/stpdfs.h>
#ifdef HAVE_STRUCT_OPTION
static struct option long_options[] = {
{"help", no_argument, 0, 'h'},
{"image", required_argument, 0, 'i'},
{0, 0, 0, 0}
};
#endif /* HAVE_STRUCT_OPTION */
static char *path = "/";
static char *image = NULL;
static void static void
usage(void) usage(int retval)
{ {
} }
static int
do_ls(void)
{
struct stpdfs_super_info sbi;
struct stpdfs_inode_info *ip;
struct stpdfs_dirent dirent;
size_t idx;
if (stpdfs_super_open(&sbi, image))
{
return (EXIT_FAILURE);
}
ip = stpdfs_inode_get(&sbi, STPDFS_ROOTINO);
for (idx = 0; idx < ip->inode.size; idx += STPDFS_DIRENT_SIZE)
{
stpdfs_inode_read(ip, (uint8_t *)&dirent, idx, STPDFS_DIRENT_SIZE);
printf("%s\n", dirent.filename);
}
stpdfs_super_kill(&sbi);
return (EXIT_SUCCESS);
}
int int
ls(int argc, char **argv) ls(int argc, char **argv)
{ {
int idx;
int c;
return (EXIT_SUCCESS); #if defined(HAVE_GETOPT_LONG) && defined(HAVE_STRUCT_OPTION)
while ((c = getopt_long(argc, argv, "hi:", long_options, &idx)) != EOF)
#else
while ((c = getopt(argc, argv, "hi:")) != EOF)
#endif
{
switch (c)
{
case 'h':
usage(EXIT_SUCCESS);
break;
case 'i':
image = optarg;
break;
default:
usage(EXIT_FAILURE);
break;
}
}
if (image == NULL)
{
return (EXIT_FAILURE);
}
if (optind < argc)
{
path = argv[optind];
}
return (do_ls());
} }