2019-10-09 01:35:47 +02:00
|
|
|
/*
|
2022-06-27 19:36:56 +02:00
|
|
|
SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
|
2020-10-21 16:57:54 +02:00
|
|
|
UMKa - User-Mode KolibriOS developer tools
|
|
|
|
umka_fuse - FUSE <-> KolibriOS FS calls converter
|
2019-10-09 01:35:47 +02:00
|
|
|
|
2023-01-17 01:49:11 +01:00
|
|
|
Copyright (C) 2017-2023 Ivan Baravy <dunkaist@gmail.com>
|
2019-10-09 01:35:47 +02:00
|
|
|
*/
|
|
|
|
|
2017-10-18 22:19:53 +02:00
|
|
|
#define FUSE_USE_VERSION 31
|
2017-10-18 02:08:32 +02:00
|
|
|
|
|
|
|
#include <fuse.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stddef.h>
|
2017-11-02 21:41:11 +01:00
|
|
|
#include <stdlib.h>
|
2018-04-24 18:51:48 +02:00
|
|
|
#include <time.h>
|
2017-10-18 22:19:53 +02:00
|
|
|
#include <assert.h>
|
2019-10-13 03:17:29 +02:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
2023-01-17 01:49:11 +01:00
|
|
|
#include "io.h"
|
2020-02-21 00:18:40 +01:00
|
|
|
#include "vdisk.h"
|
2020-05-07 03:57:01 +02:00
|
|
|
#include "umka.h"
|
2018-05-09 23:08:52 +02:00
|
|
|
|
|
|
|
#define DIRENTS_TO_READ 100
|
2017-10-18 02:08:32 +02:00
|
|
|
|
2023-01-17 01:49:11 +01:00
|
|
|
struct umka_fuse_ctx {
|
|
|
|
struct umka_ctx *umka;
|
|
|
|
struct umka_io *io;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct umka_fuse_ctx *
|
|
|
|
umka_fuse_init() {
|
|
|
|
struct umka_fuse_ctx *ctx = malloc(sizeof(struct umka_fuse_ctx));
|
2023-01-18 09:29:08 +01:00
|
|
|
ctx->umka = umka_init(UMKA_FUSE);
|
|
|
|
ctx->io = io_init(&ctx->umka->running);
|
2023-01-17 01:49:11 +01:00
|
|
|
return ctx;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
umka_fuse_close(struct umka_fuse_ctx *ctx) {
|
|
|
|
umka_close(ctx->umka);
|
|
|
|
io_close(ctx->io);
|
|
|
|
free(ctx);
|
|
|
|
}
|
|
|
|
|
2020-10-17 04:13:18 +02:00
|
|
|
static void
|
|
|
|
bdfe_to_stat(bdfe_t *kf, struct stat *st) {
|
2020-05-05 17:48:29 +02:00
|
|
|
// if (kf->attr & KF_FOLDER) {
|
|
|
|
if (st) {
|
2017-11-02 21:41:11 +01:00
|
|
|
st->st_mode = S_IFDIR | 0755;
|
|
|
|
st->st_nlink = 2;
|
|
|
|
} else {
|
2017-11-16 02:16:20 +01:00
|
|
|
st->st_mode = S_IFREG | 0644;
|
2017-11-02 21:41:11 +01:00
|
|
|
st->st_nlink = 1;
|
|
|
|
st->st_size = kf->size;
|
|
|
|
}
|
2019-10-13 03:17:29 +02:00
|
|
|
st->st_atime = kos_time_to_epoch(&(kf->atime));
|
|
|
|
st->st_mtime = kos_time_to_epoch(&(kf->mtime));
|
|
|
|
st->st_ctime = kos_time_to_epoch(&(kf->ctime));
|
2017-11-02 21:41:11 +01:00
|
|
|
}
|
|
|
|
|
2020-10-17 04:13:18 +02:00
|
|
|
static void *
|
2023-01-17 01:49:11 +01:00
|
|
|
fs_init(struct fuse_conn_info *conn, struct fuse_config *cfg) {
|
2017-11-02 21:41:11 +01:00
|
|
|
(void) conn;
|
|
|
|
cfg->kernel_cache = 1;
|
|
|
|
return NULL;
|
2017-10-18 22:19:53 +02:00
|
|
|
}
|
2017-10-18 02:08:32 +02:00
|
|
|
|
2023-01-17 01:49:11 +01:00
|
|
|
static void
|
|
|
|
fs_destroy(void *private_data) {
|
|
|
|
struct umka_fuse_ctx *ctx = private_data;
|
|
|
|
umka_fuse_close(ctx);
|
|
|
|
}
|
2020-10-17 04:13:18 +02:00
|
|
|
static int
|
2023-01-17 01:49:11 +01:00
|
|
|
fs_getattr(const char *path, struct stat *stbuf, struct fuse_file_info *fi) {
|
2018-05-09 23:08:52 +02:00
|
|
|
(void) fi;
|
|
|
|
int res = 0;
|
|
|
|
|
2019-10-21 04:51:56 +02:00
|
|
|
bdfe_t file;
|
2020-05-11 05:38:44 +02:00
|
|
|
f7080s5arg_t fX0 = {.sf = 5,
|
|
|
|
.flags = 0,
|
|
|
|
.buf = &file,
|
|
|
|
.u = {.f80 = {.path_encoding = UTF8,
|
|
|
|
.path = path
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2019-12-08 02:32:07 +01:00
|
|
|
f7080ret_t r;
|
2020-02-06 04:19:20 +01:00
|
|
|
umka_sys_lfn(&fX0, &r, F80);
|
2017-11-02 21:41:11 +01:00
|
|
|
|
2018-05-09 23:08:52 +02:00
|
|
|
bdfe_to_stat(&file, stbuf);
|
|
|
|
// res = -ENOENT;
|
|
|
|
return res;
|
2017-10-18 02:08:32 +02:00
|
|
|
}
|
|
|
|
|
2020-10-17 04:13:18 +02:00
|
|
|
static int
|
2023-01-17 01:49:11 +01:00
|
|
|
fs_readdir(const char *path, void *buf, fuse_fill_dir_t filler, off_t offset,
|
2020-10-17 04:13:18 +02:00
|
|
|
struct fuse_file_info *fi, enum fuse_readdir_flags flags) {
|
2018-05-14 09:54:05 +02:00
|
|
|
(void) offset; // TODO
|
2018-05-09 23:08:52 +02:00
|
|
|
(void) fi;
|
|
|
|
(void) flags;
|
|
|
|
|
2020-10-17 04:13:18 +02:00
|
|
|
f7080s1info_t *dir = (f7080s1info_t*)malloc(sizeof(f7080s1info_t) +
|
|
|
|
BDFE_LEN_UNICODE * DIRENTS_TO_READ);
|
2020-05-11 05:38:44 +02:00
|
|
|
f7080s1arg_t fX0 = {.sf = 1,
|
|
|
|
.offset = 0,
|
|
|
|
.encoding = UTF8,
|
|
|
|
.size = DIRENTS_TO_READ,
|
|
|
|
.buf = dir,
|
|
|
|
.u = {.f80 = {.path_encoding = UTF8,
|
|
|
|
.path = path
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2019-12-08 02:32:07 +01:00
|
|
|
f7080ret_t r;
|
2020-02-06 04:19:20 +01:00
|
|
|
umka_sys_lfn(&fX0, &r, F80);
|
2019-12-08 02:32:07 +01:00
|
|
|
bdfe_t *bdfe = dir->bdfes;
|
2018-05-09 23:08:52 +02:00
|
|
|
for (size_t i = 0; i < dir->cnt; i++) {
|
2019-12-08 02:32:07 +01:00
|
|
|
filler(buf, bdfe->name, NULL, 0, 0);
|
|
|
|
bdfe = (bdfe_t*)((uintptr_t)bdfe + BDFE_LEN_UNICODE);
|
2018-05-09 23:08:52 +02:00
|
|
|
}
|
|
|
|
free(dir);
|
|
|
|
return 0;
|
2017-10-18 02:08:32 +02:00
|
|
|
}
|
|
|
|
|
2020-10-17 04:13:18 +02:00
|
|
|
static int
|
2023-01-17 01:49:11 +01:00
|
|
|
fs_open(const char *path, struct fuse_file_info *fi) {
|
2020-05-11 05:38:44 +02:00
|
|
|
// if (strcmp(path+1, "blah") != 0)
|
|
|
|
// return -ENOENT;
|
|
|
|
(void) path;
|
2017-10-18 02:08:32 +02:00
|
|
|
|
2020-05-11 05:38:44 +02:00
|
|
|
if ((fi->flags & O_ACCMODE) != O_RDONLY)
|
|
|
|
return -EACCES;
|
2017-10-18 02:08:32 +02:00
|
|
|
|
2020-05-11 05:38:44 +02:00
|
|
|
return 0;
|
2017-10-18 02:08:32 +02:00
|
|
|
}
|
|
|
|
|
2020-10-17 04:13:18 +02:00
|
|
|
static int
|
2023-01-17 01:49:11 +01:00
|
|
|
fs_read(const char *path, char *buf, size_t size, off_t offset,
|
2020-10-17 04:13:18 +02:00
|
|
|
struct fuse_file_info *fi) {
|
2018-05-09 23:08:52 +02:00
|
|
|
(void) fi;
|
2017-11-02 21:41:11 +01:00
|
|
|
|
2020-10-17 04:13:18 +02:00
|
|
|
f7080s0arg_t fX0 = {.sf = 0, .offset = offset, .count = size, .buf = buf,
|
|
|
|
.u = {.f80 = {.path_encoding = UTF8, .path = path}}};
|
2019-12-08 02:32:07 +01:00
|
|
|
f7080ret_t r;
|
2020-02-06 04:19:20 +01:00
|
|
|
umka_sys_lfn(&fX0, &r, F80);
|
2018-05-09 23:08:52 +02:00
|
|
|
return size;
|
2017-10-18 02:08:32 +02:00
|
|
|
}
|
|
|
|
|
2020-02-19 02:08:03 +01:00
|
|
|
static struct fuse_operations umka_oper = {
|
2023-01-17 01:49:11 +01:00
|
|
|
.init = fs_init,
|
|
|
|
.destroy = fs_destroy,
|
|
|
|
.getattr = fs_getattr,
|
|
|
|
.readdir = fs_readdir,
|
|
|
|
.open = fs_open,
|
|
|
|
.read = fs_read,
|
2017-10-18 02:08:32 +02:00
|
|
|
};
|
|
|
|
|
2020-10-17 04:13:18 +02:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[]) {
|
2020-05-11 05:38:44 +02:00
|
|
|
if (argc != 3) {
|
|
|
|
printf("usage: umka_fuse dir img\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2021-11-03 15:28:03 +01:00
|
|
|
|
2023-01-17 01:49:11 +01:00
|
|
|
kos_boot.bpp = UMKA_DEFAULT_DISPLAY_BPP;
|
2021-11-03 15:28:03 +01:00
|
|
|
kos_boot.x_res = UMKA_DEFAULT_DISPLAY_WIDTH;
|
|
|
|
kos_boot.y_res = UMKA_DEFAULT_DISPLAY_HEIGHT;
|
2023-01-17 01:49:11 +01:00
|
|
|
kos_boot.pitch = UMKA_DEFAULT_DISPLAY_WIDTH * UMKA_DEFAULT_DISPLAY_BPP / 8;
|
|
|
|
|
|
|
|
struct umka_fuse_ctx *ctx = umka_fuse_init();
|
2021-11-03 15:28:03 +01:00
|
|
|
|
2023-01-16 05:54:34 +01:00
|
|
|
struct vdisk *umka_disk = vdisk_init(argv[2], 1, 0u, 0);
|
2023-01-04 01:15:40 +01:00
|
|
|
disk_t *disk = disk_add(&umka_disk->diskfunc, "hd0", umka_disk, 0);
|
|
|
|
disk_media_changed(disk, 1);
|
2023-01-17 01:49:11 +01:00
|
|
|
return fuse_main(argc-1, argv, &umka_oper, ctx);
|
2017-10-18 02:08:32 +02:00
|
|
|
}
|