feat(tools): ls list details

This commit is contained in:
d0p1 🏳️‍⚧️ 2024-09-03 14:41:37 +02:00
parent 1c5ba90296
commit 8618a18754
3 changed files with 139 additions and 10 deletions

View file

@ -52,11 +52,14 @@ fs_inode_get(struct fs_super *super, uint32_t inum)
ip = (struct fs_inode *)malloc(sizeof(struct fs_inode)); ip = (struct fs_inode *)malloc(sizeof(struct fs_inode));
if (ip == NULL) return (NULL); if (ip == NULL) return (NULL);
ip->next = head;
ip->super = super; ip->super = super;
ip->inum = inum; ip->inum = inum;
ip->refcount = 1; ip->refcount = 1;
ip->valid = 0; ip->valid = 0;
head = ip;
return (ip); return (ip);
} }

View file

@ -20,6 +20,8 @@
static struct option long_options[] = { static struct option long_options[] = {
{"help", no_argument, 0, 'h'}, {"help", no_argument, 0, 'h'},
{"image", required_argument, 0, 'i'}, {"image", required_argument, 0, 'i'},
{"uid", required_argument, 0, 'u'},
{"gid", required_argument, 0, 'g'},
{0, 0, 0, 0} {0, 0, 0, 0}
}; };
#endif /* HAVE_STRUCT_OPTION */ #endif /* HAVE_STRUCT_OPTION */
@ -27,6 +29,8 @@ static struct option long_options[] = {
static char *dest = NULL; static char *dest = NULL;
static char *src = NULL; static char *src = NULL;
static char *image = NULL; static char *image = NULL;
static int gid = -1;
static int uid = -1;
extern char *prg_name; extern char *prg_name;
@ -74,8 +78,24 @@ do_copy(void)
fs_inode_read(ip); fs_inode_read(ip);
} }
ip->inode.mode = st.st_mode; ip->inode.mode = st.st_mode;
if (uid >= 0)
{
ip->inode.uid = uid;
}
else
{
ip->inode.uid = st.st_uid; ip->inode.uid = st.st_uid;
}
if (gid >= 0)
{
ip->inode.gid = gid;
}
else
{
ip->inode.gid = st.st_gid; ip->inode.gid = st.st_gid;
}
ip->inode.modtime = st.st_mtime; ip->inode.modtime = st.st_mtime;
ip->inode.actime = st.st_atime; ip->inode.actime = st.st_atime;
ip->inode.flags = STPDFS_INO_FLAG_ALOC; ip->inode.flags = STPDFS_INO_FLAG_ALOC;
@ -114,9 +134,9 @@ cmd_copy(int argc, char **argv)
int c; int c;
#if defined(HAVE_GETOPT_LONG) && defined(HAVE_STRUCT_OPTION) #if defined(HAVE_GETOPT_LONG) && defined(HAVE_STRUCT_OPTION)
while ((c = getopt_long(argc, argv, "hi:", long_options, &idx)) != EOF) while ((c = getopt_long(argc, argv, "hi:u:g:", long_options, &idx)) != EOF)
#else #else
while ((c = getopt(argc, argv, "hi:")) != EOF) while ((c = getopt(argc, argv, "hi:u:g:")) != EOF)
#endif #endif
{ {
switch (c) switch (c)
@ -129,6 +149,14 @@ cmd_copy(int argc, char **argv)
image = optarg; image = optarg;
break; break;
case 'u':
uid = atoi(optarg);
break;
case 'g':
gid = atoi(optarg);
break;
default: default:
usage(EXIT_FAILURE); usage(EXIT_FAILURE);
break; break;

View file

@ -1,10 +1,8 @@
#include "libfs/fs.h"
#include "libfs/inode.h" #include "libfs/inode.h"
#include "libfs/super.h"
#include "stupidfs.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <sys/types.h>
#include <time.h>
#ifdef HAVE_CONFIG_H #ifdef HAVE_CONFIG_H
# include "config.h" # include "config.h"
#endif /* HAVE_CONFIG_H */ #endif /* HAVE_CONFIG_H */
@ -14,11 +12,14 @@
#ifdef HAVE_GETOPT_H #ifdef HAVE_GETOPT_H
# include <getopt.h> # include <getopt.h>
#endif /* HAVE_GETOPT_H */ #endif /* HAVE_GETOPT_H */
#include <stupidfs.h>
#include "libfs/fs.h"
#ifdef HAVE_STRUCT_OPTION #ifdef HAVE_STRUCT_OPTION
static struct option long_options[] = { static struct option long_options[] = {
{"help", no_argument, 0, 'h'}, {"help", no_argument, 0, 'h'},
{"image", required_argument, 0, 'i'}, {"image", required_argument, 0, 'i'},
{"all", no_argument, 0, 'a'},
{0, 0, 0, 0} {0, 0, 0, 0}
}; };
#endif /* HAVE_STRUCT_OPTION */ #endif /* HAVE_STRUCT_OPTION */
@ -48,6 +49,98 @@ usage(int retval)
exit(retval); exit(retval);
} }
static void
print_mode(struct fs_inode *ip)
{
char modebuff[] = "----------";
if (ip->inode.mode & STPDFS_S_IFDIR)
{
modebuff[0] = 'd';
}
if (ip->inode.mode & STPDFS_S_IRUSR)
{
modebuff[1] = 'r';
}
if (ip->inode.mode & STPDFS_S_IWUSR)
{
modebuff[2] = 'w';
}
if (ip->inode.mode & STPDFS_S_IXUSR)
{
modebuff[3] = 'x';
}
if (ip->inode.mode & STPDFS_S_IRGRP)
{
modebuff[4] = 'r';
}
if (ip->inode.mode & STPDFS_S_IWGRP)
{
modebuff[5] = 'w';
}
if (ip->inode.mode & STPDFS_S_IXGRP)
{
modebuff[6] = 'x';
}
if (ip->inode.mode & STPDFS_S_IROTH)
{
modebuff[7] = 'r';
}
if (ip->inode.mode & STPDFS_S_IWOTH)
{
modebuff[8] = 'w';
}
if (ip->inode.mode & STPDFS_S_IXOTH)
{
modebuff[9] = 'x';
}
printf("%s ", modebuff);
}
static void
print_time(struct fs_inode *ip)
{
char timebuff[13];
time_t time;
time = ip->inode.modtime;
strftime(timebuff, 13, "%b %y %H:%M", localtime(&time));
printf("%s ", timebuff);
}
static void
print_dirent(struct fs_super *super, struct stpdfs_dirent *dirent)
{
struct fs_inode *ip;
ip = fs_inode_get(super, dirent->inode);
if (ip->valid == 0)
{
fs_inode_read(ip);
}
print_mode(ip);
printf("%u ", ip->inode.nlink);
printf("%3u %3u ", ip->inode.uid, ip->inode.gid);
printf("%5u ", ip->inode.size);
print_time(ip);
printf("%s\n", dirent->filename);
fs_inode_release(ip);
}
static int static int
do_ls(void) do_ls(void)
{ {
@ -70,13 +163,18 @@ do_ls(void)
for (idx = 0; idx < ip->inode.size; idx += STPDFS_DIRENT_SIZE) for (idx = 0; idx < ip->inode.size; idx += STPDFS_DIRENT_SIZE)
{ {
fs_read(ip, (uint8_t *)&dirent, idx, STPDFS_DIRENT_SIZE); fs_read(ip, (uint8_t *)&dirent, idx, STPDFS_DIRENT_SIZE);
if (dirent.inode == 0)
{
continue;
}
if (dirent.filename[0] == '.') if (dirent.filename[0] == '.')
{ {
if (all) printf("%s\n", dirent.filename); if (all) print_dirent(&super, &dirent);
} }
else else
{ {
printf("%s\n", dirent.filename); print_dirent(&super, &dirent);
} }
} }