144 lines
3.3 KiB
C
144 lines
3.3 KiB
C
#include <fs/path.h>
|
|
#include <fs/tarfs.h>
|
|
#include <fs/vfs.h>
|
|
#include <fs_desc_buffer.h>
|
|
#include <libk/align.h>
|
|
#include <libk/minmax.h>
|
|
#include <libk/std.h>
|
|
#include <libk/string.h>
|
|
#include <limine/requests.h>
|
|
#include <mm/liballoc.h>
|
|
#include <path.h>
|
|
#include <status.h>
|
|
#include <sys/debug.h>
|
|
#include <xdrv_device.h>
|
|
|
|
static struct tar_file* tar_get_file (struct tarfs* tarfs, const char* filename) {
|
|
for (size_t i = 0; i < TARFS_FILES_MAX; i++) {
|
|
if ((tarfs->tarfs_files[i].header != NULL) &&
|
|
(strncmp (tarfs->tarfs_files[i].header->filename, filename, MAX_PATH) == 0))
|
|
return &tarfs->tarfs_files[i];
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
static size_t tar_get_size (uint8_t* in) {
|
|
size_t size = 0;
|
|
size_t j;
|
|
size_t count = 1;
|
|
|
|
for (j = 11; j > 0; j--, count *= 8)
|
|
size += ((in[j - 1] - '0') * count);
|
|
|
|
return size;
|
|
}
|
|
|
|
static size_t tar_parse (struct tarfs* tarfs, uint8_t* addr) {
|
|
size_t i;
|
|
|
|
for (i = 0; i < TARFS_FILES_MAX; i++) {
|
|
struct tar_header* hdr = (struct tar_header*)addr;
|
|
|
|
if (hdr->filename[0] == '\0')
|
|
break;
|
|
|
|
size_t size = tar_get_size (hdr->size);
|
|
|
|
tarfs->tarfs_files[i].header = hdr;
|
|
tarfs->tarfs_files[i].content = (uint8_t*)((uintptr_t)hdr + 512);
|
|
tarfs->tarfs_files[i].size = tar_get_size ((uint8_t*)hdr->size);
|
|
|
|
addr += 512 + ((size + 511) & ~511);
|
|
}
|
|
|
|
return i;
|
|
}
|
|
|
|
int tarfs_mount (struct vfs_mountpoint* mountpoint, struct proc* proc,
|
|
struct reschedule_ctx* rctx) {
|
|
struct tarfs* tarfs = malloc (sizeof (*tarfs));
|
|
|
|
if (tarfs == NULL)
|
|
return -ST_OOM_ERROR;
|
|
|
|
memset (tarfs, 0, sizeof (*tarfs));
|
|
|
|
mountpoint->udata = tarfs;
|
|
|
|
struct device* back_device = mountpoint->back_device;
|
|
size_t total_size;
|
|
int ret;
|
|
|
|
spin_lock (&back_device->lock);
|
|
|
|
ret = back_device->ops[XDRV_GET_SIZE](back_device, proc, rctx, &total_size, NULL, NULL, NULL);
|
|
if (ret < 0) {
|
|
spin_unlock (&back_device->lock);
|
|
free (mountpoint->udata);
|
|
return ret;
|
|
}
|
|
|
|
uint8_t* buffer = malloc (total_size);
|
|
|
|
if (buffer == NULL) {
|
|
spin_unlock (&back_device->lock);
|
|
free (mountpoint->udata);
|
|
return ret;
|
|
}
|
|
|
|
size_t pos = 0;
|
|
ret = back_device->ops[XDRV_READ](back_device, proc, rctx, &pos, &total_size, buffer, NULL);
|
|
|
|
spin_unlock (&back_device->lock);
|
|
|
|
if (ret < 0) {
|
|
free (buffer);
|
|
return ret;
|
|
}
|
|
|
|
tarfs->buffer = buffer;
|
|
|
|
tar_parse (tarfs, tarfs->buffer);
|
|
|
|
return ret;
|
|
}
|
|
|
|
int tarfs_describe (struct vfs_mountpoint* mountpoint, const char* path,
|
|
struct fs_desc_buffer* desc) {
|
|
(void)mountpoint;
|
|
|
|
const char* filename = path_basename (path);
|
|
|
|
if (filename == NULL)
|
|
return -ST_BAD_PATH;
|
|
|
|
struct tar_file* file = tar_get_file ((struct tarfs*)mountpoint->udata, filename);
|
|
|
|
if (file == NULL)
|
|
return -ST_NOT_FOUND;
|
|
|
|
desc->size = file->size;
|
|
desc->type = FS_FILE;
|
|
|
|
return ST_OK;
|
|
}
|
|
|
|
int tarfs_read (struct vfs_mountpoint* mountpoint, const char* path, uint8_t* buffer, size_t off,
|
|
size_t size) {
|
|
(void)mountpoint;
|
|
|
|
const char* filename = path_basename (path);
|
|
|
|
if (filename == NULL)
|
|
return -ST_BAD_PATH;
|
|
|
|
struct tar_file* file = tar_get_file ((struct tarfs*)mountpoint->udata, filename);
|
|
|
|
if (file == NULL)
|
|
return -ST_NOT_FOUND;
|
|
|
|
memcpy (buffer, (void*)((uintptr_t)file->content + off), min (size, file->size));
|
|
|
|
return ST_OK;
|
|
}
|