1
0
mirror of https://gitlab.os-k.eu/os-k-team/kvisc.git synced 2023-08-25 14:05:46 +02:00
kvisc/vm/dv/diskdev.c
julianb0 63aeda2878
dir
2019-07-01 13:16:17 +02:00

134 lines
2.4 KiB
C

// The OS/K Team licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
#include <pc/device.h>
#include <sys/stat.h>
#include <dirent.h>
#define MAXOPEN 4096
typedef struct disk_t disk_t;
typedef struct dirent dirent_t;
struct disk_t
{
// disk directory
DIR *dir;
// open files
int *table[MAXOPEN];
uint opened;
};
#define GETDISK() \
disk_t *disk = (disk_t *)dev->data
//----------------------------------------------------------------------------//
long diskdev_findnext(ctx_t *ctx, dev_t *dev)
{
struct stat st;
char name[NAME_MAX+4];
dirent_t *ent;
GETDISK();
for (;;)
{
ent = readdir(disk->dir);
if (ent == NULL)
{
rax = 0;
return 0;
}
if (!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, ".."))
continue;
break;
}
rax = copystr(ctx, ax0, ax1, ent->d_name);
snprintf(name, NAME_MAX+4, "fs/%s", ent->d_name);
if (stat(name, &st) < 0)
{
perror("diskdev: couldn't stat file in directory: ");
rdx = -1;
}
else
rdx = st.st_size;
return 0;
}
long diskdev_findfirst(ctx_t *ctx, dev_t *dev)
{
GETDISK();
rewinddir(disk->dir);
return diskdev_findnext(ctx, dev);
}
//----------------------------------------------------------------------------//
long diskdev_poweron(ctx_t *ctx, dev_t *dev)
{
disk_t *disk = calloc(1, sizeof(disk_t));
disk->dir = opendir("fs");
if (disk->dir == NULL)
{
logerr("diskdev: couldn't open fs directory\n");
return -1;
}
dev->data = (void *)disk;
dev->fslots[16] = diskdev_findfirst;
dev->fslots[17] = diskdev_findnext;
dev->state = DEVGOOD;
return 0;
}
long diskdev_poweroff(ctx_t *ctx, dev_t *dev)
{
GETDISK();
if (disk && disk->dir)
closedir(disk->dir);
if (disk)
free(disk);
dev->data = NULL;
dev->state = DEVPWOF;
return 0;
}
//----------------------------------------------------------------------------//
dev_t diskdev =
{
.type = "disk",
.name = "disk",
.modl = "",
.vend = "The OS/K Team",
.major = KARCH_MAJOR,
.minor = KARCH_MINOR,
.revis = KARCH_REVIS,
.fpwon = diskdev_poweron,
.fpwoff = diskdev_poweroff,
};