ioctl() IOCTL_STAT command

This commit is contained in:
2025-09-14 19:30:20 +02:00
parent 26ff717b50
commit 69e23a9ca3
5 changed files with 71 additions and 2 deletions

View File

@ -133,6 +133,35 @@ int32_t SYSCALL5(sys_ioctl, ioh1, cmd1, arg1, arg2, arg3) {
ret = vobj->read(vobj, buffer, len, off); ret = vobj->read(vobj, buffer, len, off);
} break; } break;
case IOCTL_STAT: {
if (ioh >= PROC_VFSHANDLES_MAX) {
ret = E_INVALIDARGUMENT;
goto done;
}
spinlock_acquire(&PROCS.spinlock);
Proc *proc = PROCS.current;
spinlock_release(&PROCS.spinlock);
VfsObj *vobj = proc->vobjs[ioh];
if (vobj == NULL) {
ret = E_INVALIDARGUMENT;
goto done;
}
VfsStat stat1;
ret = vobj->stat(vobj, &stat1);
IoctlStat *iostat = (IoctlStat *)arg1;
if (iostat == NULL) {
ret = E_INVALIDARGUMENT;
goto done;
}
iostat->size = stat1.size;
iostat->type = stat1.type;
} break;
default: { default: {
ret = E_INVALIDARGUMENT; ret = E_INVALIDARGUMENT;
goto done; goto done;

View File

@ -20,8 +20,8 @@ static const char *vfs_strings[] = {
}; };
enum { enum {
VFS_TYPE_DIR, VFS_TYPE_DIR = 0,
VFS_TYPE_FILE, VFS_TYPE_FILE = 1,
}; };
enum { enum {

View File

@ -20,4 +20,14 @@ enum {
IOCTL_F_MAKE = 1<<2, IOCTL_F_MAKE = 1<<2,
}; };
enum {
IOCTLSTAT_DIR = 0,
IOCTLSTAT_FILE = 1,
};
typedef struct IoctlStat {
size_t size;
int32_t type;
} IoctlStat;
#endif // SHARE_SYSDEFS_IOCTL_H_ #endif // SHARE_SYSDEFS_IOCTL_H_

View File

@ -1,6 +1,14 @@
#ifndef ULIB_UTIL_UTIL_H_ #ifndef ULIB_UTIL_UTIL_H_
#define ULIB_UTIL_UTIL_H_ #define ULIB_UTIL_UTIL_H_
#include <string/string.h>
#define ARRLEN(X) (sizeof((X))/sizeof((X)[0])) #define ARRLEN(X) (sizeof((X))/sizeof((X)[0]))
#define ZERO(X) \
({ \
string_memset((X), 0, sizeof(*(X))); \
*(X); \
}) \
#endif // ULIB_UTIL_UTIL_H_ #endif // ULIB_UTIL_UTIL_H_

View File

@ -10,7 +10,9 @@
#include <sysdefs/ipcpipe.h> #include <sysdefs/ipcpipe.h>
#include <sysdefs/ioctl.h> #include <sysdefs/ioctl.h>
#include <sysdefs/processctl.h> #include <sysdefs/processctl.h>
#include <dlmalloc/malloc.h>
#include <errors.h> #include <errors.h>
#include <util/util.h>
struct { struct {
char *modestr; char *modestr;
@ -50,11 +52,31 @@ void process_cmd(char *cmdtext) {
} }
void do_file(char *filepath) { void do_file(char *filepath) {
int32_t ret;
int32_t ioh = ioctl(IOCTL_NOHANDLE, IOCTL_OPENF, (uint64_t)filepath, IOCTL_F_READ, 0); int32_t ioh = ioctl(IOCTL_NOHANDLE, IOCTL_OPENF, (uint64_t)filepath, IOCTL_F_READ, 0);
if (ioh < 0) { if (ioh < 0) {
LOG(LOG_ERR, "Could not open %s: %s\n", filepath, ERRSTRING(ioh)); LOG(LOG_ERR, "Could not open %s: %s\n", filepath, ERRSTRING(ioh));
return; return;
} }
IoctlStat statbuf = ZERO(&statbuf);
ioctl(ioh, IOCTL_STAT, (uint64_t)&statbuf, 0, 0);
if (statbuf.type != IOCTLSTAT_FILE) {
LOG(LOG_ERR, "%s is not a file (%d)\n", filepath, statbuf.type);
return;
}
uint8_t *buf = dlmalloc(statbuf.size);
if ((ret = ioctl(ioh, IOCTL_READ, (uint64_t)buf, statbuf.size, 0)) < 0) {
LOG(LOG_ERR, "Could not read %s (%d): %s\n", filepath, ioh, ERRSTRING(ioh));
goto done;
}
done:
dlfree(buf);
ioctl(ioh, IOCTL_CLOSEF, 0, 0, 0);
} }
void do_mode_interactive(void) { void do_mode_interactive(void) {