330 lines
6.7 KiB
C
330 lines
6.7 KiB
C
#include <desc.h>
|
|
#include <device/device.h>
|
|
#include <devices.h>
|
|
#include <dir_entry.h>
|
|
#include <fs/def_vfs_op.h>
|
|
#include <fs/iso9660fs/iso9660fs.h>
|
|
#include <fs/iso9660fs/lib9660.h>
|
|
#include <fs/vfs.h>
|
|
#include <libk/std.h>
|
|
#include <libk/string.h>
|
|
#include <mm/malloc.h>
|
|
#include <proc/proc.h>
|
|
#include <proc/reschedule.h>
|
|
#include <status.h>
|
|
#include <sys/debug.h>
|
|
|
|
#define ISO9660_SECTOR_SIZE 2048
|
|
|
|
static bool iso9660fs_read_sector(l9660_fs* fs, void* buf, uint32_t sector) {
|
|
uint64_t fd;
|
|
struct vfs_volume* volume = fs->udata;
|
|
struct device* back_device = volume->back_device;
|
|
|
|
size_t sector_size;
|
|
size_t phys_sector, phys_sector_count;
|
|
int ret;
|
|
|
|
spin_lock(&back_device->lock, &fd);
|
|
|
|
ret = device_op(back_device, XDRV_GET_SECTOR_SIZE, fs->proc, fs->rctx, &fd, §or_size);
|
|
if (ret < 0) {
|
|
spin_unlock(&back_device->lock, fd);
|
|
return false;
|
|
}
|
|
|
|
vfs_translate(sector, 1, ISO9660_SECTOR_SIZE, sector_size, &phys_sector, &phys_sector_count);
|
|
|
|
ret = device_op(back_device, XDRV_READ, fs->proc, fs->rctx, &fd, &phys_sector, &phys_sector_count,
|
|
buf);
|
|
|
|
if (ret < 0) {
|
|
spin_unlock(&back_device->lock, fd);
|
|
return false;
|
|
}
|
|
|
|
spin_unlock(&back_device->lock, fd);
|
|
|
|
return true;
|
|
}
|
|
|
|
static bool iso9660fs_is_cat(l9660_dirent* dirent) {
|
|
if (dirent->name_len < 4)
|
|
return false;
|
|
|
|
size_t end = dirent->name_len;
|
|
|
|
for (size_t i = 0; i < dirent->name_len; i++) {
|
|
if (dirent->name[i] == ';') {
|
|
end = i;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (end < 4)
|
|
return false;
|
|
|
|
const char* n = dirent->name;
|
|
|
|
return (n[end - 4] == '.' &&
|
|
(n[end - 3] == 'c' || n[end - 3] == 'C') &&
|
|
(n[end - 2] == 'a' || n[end - 2] == 'A') &&
|
|
(n[end - 1] == 't' || n[end - 1] == 'T'));
|
|
}
|
|
|
|
static bool iso9660fs_is_special_entry(l9660_dirent* dirent) {
|
|
return (dirent->name_len == 1 && (dirent->name[0] == '\0' || dirent->name[0] == '\1')) ||
|
|
iso9660fs_is_cat(dirent);
|
|
}
|
|
|
|
static void iso9660fs_copy_visible_name(char* dst, size_t dst_size, l9660_dirent* dirent) {
|
|
size_t i = 0;
|
|
|
|
if (dst_size == 0)
|
|
return;
|
|
|
|
while (i + 1 < dst_size && i < dirent->name_len) {
|
|
if (dirent->name[i] == ';')
|
|
break;
|
|
|
|
dst[i] = dirent->name[i];
|
|
i++;
|
|
}
|
|
|
|
dst[i] = '\0';
|
|
}
|
|
|
|
static l9660_status iso9660fs_count_dir_entries(l9660_dir* dir, size_t* count) {
|
|
l9660_status status;
|
|
l9660_dirent* dirent = NULL;
|
|
|
|
*count = 0;
|
|
status = l9660_seekdir(dir, 0);
|
|
|
|
if (status != L9660_OK)
|
|
return status;
|
|
|
|
for (;;) {
|
|
status = l9660_readdir(dir, &dirent);
|
|
|
|
if (status != L9660_OK)
|
|
return status;
|
|
|
|
if (dirent == NULL)
|
|
break;
|
|
|
|
if (iso9660fs_is_special_entry(dirent))
|
|
continue;
|
|
|
|
(*count)++;
|
|
}
|
|
|
|
return L9660_OK;
|
|
}
|
|
|
|
DEFINE_VFS_MOUNT(iso9660fs_mount) {
|
|
l9660_fs* fs_ctx = malloc(sizeof(*fs_ctx));
|
|
|
|
if (fs_ctx == NULL)
|
|
return -ST_OOM_ERROR;
|
|
|
|
memset(fs_ctx, 0, sizeof(*fs_ctx));
|
|
fs_ctx->udata = volume;
|
|
volume->udata = fs_ctx;
|
|
|
|
fs_ctx->proc = proc;
|
|
fs_ctx->rctx = rctx;
|
|
|
|
l9660_status status = l9660_openfs(fs_ctx, &iso9660fs_read_sector);
|
|
|
|
if (status != L9660_OK) {
|
|
free(fs_ctx);
|
|
return -ST_MOUNT_ERROR;
|
|
}
|
|
|
|
return ST_OK;
|
|
}
|
|
|
|
DEFINE_VFS_UNMOUNT(iso9660fs_unmount) {
|
|
l9660_fs* fs_ctx = volume->udata;
|
|
|
|
free(fs_ctx);
|
|
|
|
return ST_OK;
|
|
}
|
|
|
|
DEFINE_VFS_FORMAT(iso9660fs_format) { return -ST_FORMAT_ERROR; }
|
|
|
|
DEFINE_VFS_DESCRIBE(iso9660fs_describe) {
|
|
l9660_fs* fs = volume->udata;
|
|
|
|
fs->proc = proc;
|
|
fs->rctx = rctx;
|
|
|
|
l9660_status status;
|
|
l9660_dir root_dir;
|
|
l9660_file file;
|
|
l9660_dir dir;
|
|
|
|
const char* relpath = path;
|
|
if (relpath[0] == '/')
|
|
relpath++;
|
|
|
|
status = l9660_fs_open_root(&root_dir, fs);
|
|
|
|
if (status != L9660_OK)
|
|
return -ST_NOT_FOUND;
|
|
|
|
if (relpath[0] == '\0') {
|
|
desc->type = FS_DIR;
|
|
status = iso9660fs_count_dir_entries(&root_dir, &desc->size);
|
|
|
|
if (status != L9660_OK)
|
|
return -ST_NOT_FOUND;
|
|
|
|
return ST_OK;
|
|
}
|
|
|
|
status = l9660_openat(&file, &root_dir, relpath);
|
|
|
|
if (status == L9660_OK) {
|
|
desc->type = FS_FILE;
|
|
desc->size = file.length;
|
|
} else {
|
|
status = l9660_opendirat(&dir, &root_dir, relpath);
|
|
|
|
if (status != L9660_OK)
|
|
return -ST_NOT_FOUND;
|
|
|
|
desc->type = FS_DIR;
|
|
status = iso9660fs_count_dir_entries(&dir, &desc->size);
|
|
|
|
if (status != L9660_OK)
|
|
return -ST_NOT_FOUND;
|
|
}
|
|
|
|
return ST_OK;
|
|
}
|
|
|
|
DEFINE_VFS_READ_FILE(iso9660fs_read_file) {
|
|
l9660_fs* fs = volume->udata;
|
|
|
|
fs->proc = proc;
|
|
fs->rctx = rctx;
|
|
|
|
l9660_status status;
|
|
l9660_dir root_dir;
|
|
l9660_file file;
|
|
|
|
status = l9660_fs_open_root(&root_dir, fs);
|
|
|
|
if (status != L9660_OK)
|
|
return -ST_NOT_FOUND;
|
|
|
|
const char* relpath = path;
|
|
if (relpath[0] == '/')
|
|
relpath++;
|
|
|
|
status = l9660_openat(&file, &root_dir, relpath);
|
|
|
|
if (status != L9660_OK)
|
|
return -ST_NOT_FOUND;
|
|
|
|
status = l9660_seek(&file, L9660_SEEK_SET, off);
|
|
|
|
if (status != L9660_OK)
|
|
return -ST_OOB_ERROR;
|
|
|
|
size_t n;
|
|
status = l9660_read(&file, buffer, size, &n);
|
|
|
|
if (status != L9660_OK)
|
|
return -ST_XDRV_READ_ERROR;
|
|
|
|
return ST_OK;
|
|
}
|
|
|
|
DEFINE_VFS_WRITE_FILE(iso9660fs_write_file) { return -ST_XDRV_WRITE_ERROR; }
|
|
|
|
DEFINE_VFS_READ_DIR_ENTRY(iso9660fs_read_dir_entry) {
|
|
l9660_fs* fs = volume->udata;
|
|
|
|
fs->proc = proc;
|
|
fs->rctx = rctx;
|
|
|
|
l9660_status status;
|
|
l9660_dir root_dir;
|
|
l9660_dir dir;
|
|
l9660_dirent* dirent;
|
|
|
|
const char* relpath = path;
|
|
if (relpath[0] == '/')
|
|
relpath++;
|
|
|
|
status = l9660_fs_open_root(&root_dir, fs);
|
|
|
|
if (status != L9660_OK)
|
|
return -ST_NOT_FOUND;
|
|
|
|
if (relpath[0] == '\0') {
|
|
dir = root_dir;
|
|
} else {
|
|
status = l9660_opendirat(&dir, &root_dir, relpath);
|
|
|
|
if (status != L9660_OK)
|
|
return -ST_NOT_FOUND;
|
|
}
|
|
|
|
status = l9660_seekdir(&dir, 0);
|
|
|
|
if (status != L9660_OK)
|
|
return status;
|
|
|
|
size_t ent = 0;
|
|
|
|
for (;;) {
|
|
status = l9660_readdir(&dir, &dirent);
|
|
|
|
if (status != L9660_OK)
|
|
return status;
|
|
|
|
if (dirent == NULL)
|
|
break;
|
|
|
|
if (iso9660fs_is_special_entry(dirent))
|
|
continue;
|
|
|
|
if (ent == entry_num) {
|
|
char namebuf[256];
|
|
size_t out_len;
|
|
|
|
const char* rockridge_name = l9660_get_rockridge_name(dirent, namebuf, &out_len);
|
|
|
|
if (rockridge_name == NULL) {
|
|
iso9660fs_copy_visible_name(namebuf, sizeof(namebuf), dirent);
|
|
}
|
|
|
|
entry->path[0] = '\0';
|
|
|
|
strncat(entry->path, path, PATH_MAX - 1);
|
|
|
|
if (strcmp(path, "/") != 0)
|
|
strncat(entry->path, "/", PATH_MAX - strlen(entry->path) - 1);
|
|
|
|
strncat(entry->path, namebuf, PATH_MAX - strlen(entry->path) - 1);
|
|
|
|
break;
|
|
}
|
|
|
|
ent++;
|
|
}
|
|
|
|
return ST_OK;
|
|
}
|
|
|
|
DEFINE_VFS_CREATE_FILE(iso9660fs_create_file) { return -ST_XDRV_WRITE_ERROR; }
|
|
|
|
DEFINE_VFS_CREATE_DIR(iso9660fs_create_dir) { return -ST_XDRV_WRITE_ERROR; }
|
|
|
|
DEFINE_VFS_REMOVE(iso9660fs_remove) { return -ST_REMOVE_ERROR; }
|