Implement kos_fuse_getattr.

This commit is contained in:
Ivan Baravy 2017-11-02 23:41:11 +03:00
parent 9c815e82ab
commit 8724153230
6 changed files with 172 additions and 74 deletions

View File

@ -1,7 +1,7 @@
format ELF format ELF
__DEBUG__ = 1 __DEBUG__ = 1
__DEBUG_LEVEL__ = 2 __DEBUG_LEVEL__ = 1
include 'proc32.inc' include 'proc32.inc'
include 'struct.inc' include 'struct.inc'
@ -26,6 +26,21 @@ ERROR_ACCESS_DENIED = 10
ERROR_DEVICE = 11 ERROR_DEVICE = 11
ERROR_OUT_OF_MEMORY = 12 ERROR_OUT_OF_MEMORY = 12
struct FS_FUNCTIONS
Free dd ?
Size dd ?
ReadFile dd ?
ReadFolder dd ?
CreateFile dd ?
WriteFile dd ?
SetFileEnd dd ?
GetFileInfo dd ?
SetFileInfo dd ?
Run dd ?
Delete dd ?
CreateFolder dd ?
ends
purge section,mov,add,sub purge section,mov,add,sub
section '.text' executable align 16 section '.text' executable align 16
@ -88,6 +103,32 @@ kos_fuse_readdir:
ret ret
public kos_fuse_getattr
kos_fuse_getattr:
push ebx esi edi ebp
mov edx, sf70_params
mov dword[edx + 0x00], 5
mov dword[edx + 0x04], 0
mov dword[edx + 0x08], 0
mov dword[edx + 0x0c], 0
mov dword[edx + 0x10], sf70_buffer
mov eax, [esp + 0x14] ; path
inc eax ; skip '/'
mov [edx + 0x14], eax
mov ebp, [fs_struct]
mov ebx, sf70_params
mov esi, eax
push 0
call xfs_GetFileInfo
pop eax
pop ebp edi esi ebx
mov eax, sf70_buffer
ret
; in: eax = sector, ebx = buffer, ebp = pointer to PARTITION structure ; in: eax = sector, ebx = buffer, ebp = pointer to PARTITION structure
fs_read32_sys: fs_read32_sys:
pushad pushad
@ -178,6 +219,7 @@ put_board:
ret ret
;include 'ext.inc'
include 'xfs.asm' include 'xfs.asm'

View File

@ -1,7 +1,29 @@
#ifndef KOS_H_INCLUDED #ifndef KOS_H_INCLUDED
#define KOS_H_INCLUDED #define KOS_H_INCLUDED
#include <stdint.h>
struct bdfe {
uint32_t attr;
uint32_t enc;
uint32_t ctime;
uint32_t cdate;
uint32_t atime;
uint32_t adate;
uint32_t mtime;
uint32_t mdate;
uint64_t size;
char name[264];
};
#define KF_READONLY 0x01
#define KF_HIDDEN 0x02
#define KF_SYSTEM 0x04
#define KF_LABEL 0x08
#define KF_FOLDER 0x10
void kos_fuse_init(int fd); void kos_fuse_init(int fd);
char *kos_fuse_readdir(const char *path, off_t offset); uint8_t *kos_fuse_readdir(const char *path, off_t offset);
void *kos_fuse_getattr(const char *path);
#endif #endif

27
kofu.c
View File

@ -2,6 +2,7 @@
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h> #include <stdint.h>
#include <stddef.h> #include <stddef.h>
#include <string.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <fcntl.h> #include <fcntl.h>
@ -23,13 +24,25 @@ int main(int argc, char **argv) {
//msg_not_xfs_partition db 'not xfs partition',0x0a //msg_not_xfs_partition db 'not xfs partition',0x0a
while(true) { while(true) {
prompt(); prompt();
scanf("%s", cmd_buf); fgets(cmd_buf, 4095, stdin);
char *bdfe = kos_fuse_readdir("", 0); int len = strlen(cmd_buf);
uint32_t file_cnt = *(uint32_t*)(bdfe + 4); cmd_buf[len-1] = '\0';
bdfe += 0x20; // printf("'%s'\n", cmd_buf);
for (; file_cnt > 0; file_cnt--) { if (!strncmp(cmd_buf, "ls ", 3)) {
printf("%s\n", bdfe + 0x28); void *header = kos_fuse_readdir(cmd_buf + 4, 0);
bdfe += 304; uint32_t file_cnt = ((uint32_t*)header)[1];
printf("file_cnt: %u\n", file_cnt);
struct bdfe *kf = header + 0x20;
for (; file_cnt > 0; file_cnt--) {
printf("%s\n", kf->name);
kf++;
}
} else if (!strncmp(cmd_buf, "stat ", 5)) {
struct bdfe *kf = kos_fuse_getattr(cmd_buf + 5);
printf("attr: 0x%2.2x\n", kf->attr);
printf("size: %llu\n", kf->size);
} else {
printf("unknown command: %s\n", cmd_buf);
} }
} }

145
kofuse.c
View File

@ -6,104 +6,123 @@
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <stddef.h> #include <stddef.h>
#include <stdlib.h>
#include <assert.h> #include <assert.h>
#include "kocdecl.h" #include "kocdecl.h"
static void bdfe_to_stat(struct bdfe *kf, struct stat *st) {
if (kf->attr & KF_FOLDER) {
st->st_mode = S_IFDIR | 0755;
st->st_nlink = 2;
} else {
st->st_mode = S_IFREG | 0444;
st->st_nlink = 1;
st->st_size = kf->size;
}
}
static void *hello_init(struct fuse_conn_info *conn, static void *hello_init(struct fuse_conn_info *conn,
struct fuse_config *cfg) struct fuse_config *cfg) {
{ (void) conn;
(void) conn; cfg->kernel_cache = 1;
cfg->kernel_cache = 1; return NULL;
return NULL;
} }
static int hello_getattr(const char *path, struct stat *stbuf, static int hello_getattr(const char *path, struct stat *stbuf,
struct fuse_file_info *fi) struct fuse_file_info *fi) {
{ (void) fi;
(void) fi; int res = 0;
int res = 0;
memset(stbuf, 0, sizeof(struct stat)); // memset(stbuf, 0, sizeof(struct stat));
if (strcmp(path, "/") == 0) { // if (strcmp(path, "/") == 0) {
stbuf->st_mode = S_IFDIR | 0755; // stbuf->st_mode = S_IFDIR | 0755;
stbuf->st_nlink = 2; // stbuf->st_nlink = 2;
} else if (strcmp(path+1, "blah") == 0) { // } else {
stbuf->st_mode = S_IFREG | 0444; struct bdfe *kf = kos_fuse_getattr(path);
stbuf->st_nlink = 1; bdfe_to_stat(kf, stbuf);
stbuf->st_size = strlen("blah"); // }
} else
res = -ENOENT;
return res; /*
if (strcmp(path, "/") == 0) {
stbuf->st_mode = S_IFDIR | 0755;
stbuf->st_nlink = 2;
} else if (strcmp(path+1, "blah") == 0) {
stbuf->st_mode = S_IFREG | 0444;
stbuf->st_nlink = 1;
stbuf->st_size = strlen("blah");
} else
res = -ENOENT;
*/
return res;
} }
static int hello_readdir(const char *path, void *buf, fuse_fill_dir_t filler, static int hello_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
off_t offset, struct fuse_file_info *fi, off_t offset, struct fuse_file_info *fi,
enum fuse_readdir_flags flags) enum fuse_readdir_flags flags) {
{ (void) offset;
(void) offset; (void) fi;
(void) fi; (void) flags;
(void) flags;
char *bdfe = kos_fuse_readdir(path, offset); void *header = kos_fuse_readdir(path, offset);
// if (strcmp(path, "/") != 0) // if (strcmp(path, "/") != 0)
// return -ENOENT; // return -ENOENT;
uint32_t i = *(uint32_t*)(bdfe + 4); uint32_t i = *(uint32_t*)(header + 4);
// int f = open("/tmp/t", O_RDWR | O_CREAT); // int f = open("/tmp/t", O_RDWR | O_CREAT);
// write(f, bdfe, 1000); // write(f, bdfe, 1000);
// write(f, &i, 4); // write(f, &i, 4);
// close(f); // close(f);
bdfe += 0x20; struct bdfe *kf = header + 0x20;
for(; i>0; i--) { for(; i>0; i--) {
filler(buf, bdfe + 0x28, NULL, 0, 0); filler(buf, kf->name, NULL, 0, 0);
bdfe += 304; kf++;
} }
return 0; return 0;
} }
static int hello_open(const char *path, struct fuse_file_info *fi) static int hello_open(const char *path, struct fuse_file_info *fi) {
{ if (strcmp(path+1, "blah") != 0)
if (strcmp(path+1, "blah") != 0) return -ENOENT;
return -ENOENT;
if ((fi->flags & O_ACCMODE) != O_RDONLY) if ((fi->flags & O_ACCMODE) != O_RDONLY)
return -EACCES; return -EACCES;
return 0; return 0;
} }
static int hello_read(const char *path, char *buf, size_t size, off_t offset, static int hello_read(const char *path, char *buf, size_t size, off_t offset,
struct fuse_file_info *fi) struct fuse_file_info *fi) {
{ size_t len;
size_t len; (void) fi;
(void) fi; if(strcmp(path+1, "blah") != 0)
if(strcmp(path+1, "blah") != 0) return -ENOENT;
return -ENOENT;
len = strlen("blah"); len = strlen("blah");
if (offset < len) { if (offset < len) {
if (offset + size > len) if (offset + size > len)
size = len - offset; size = len - offset;
memcpy(buf, "blah" + offset, size); memcpy(buf, "blah" + offset, size);
} else } else
size = 0; size = 0;
return size; return size;
} }
static struct fuse_operations hello_oper = { static struct fuse_operations hello_oper = {
.init = hello_init, .init = hello_init,
.getattr = hello_getattr, .getattr = hello_getattr,
.readdir = hello_readdir, .readdir = hello_readdir,
.open = hello_open, .open = hello_open,
.read = hello_read, .read = hello_read,
}; };
int main(int argc, char *argv[]) int main(int argc, char *argv[]) {
{ if (argc != 3) {
printf("usage: kofuse dir img\n");
exit(1);
}
int fd = open(argv[2], O_RDONLY); int fd = open(argv[2], O_RDONLY);
kos_fuse_init(fd); kos_fuse_init(fd);
return fuse_main(argc-1, argv, &hello_oper, NULL); return fuse_main(argc-1, argv, &hello_oper, NULL);
} }

View File

@ -13,10 +13,10 @@ kofuse: kofuse.o kocdecl.o
kocdecl.o: kocdecl.asm xfs.inc xfs.asm kocdecl.o: kocdecl.asm xfs.inc xfs.asm
$(FASM) $< $@ $(FASM) $< $@
kofu.o: kofu.c kofu.o: kofu.c kocdecl.h
$(CC) $(CFLAGS) -c $< $(CC) $(CFLAGS) -c $<
kofuse.o: kofuse.c kofuse.o: kofuse.c kocdecl.h
$(CC) $(CFLAGS) `pkg-config fuse3 --cflags` -c $< $(CC) $(CFLAGS) `pkg-config fuse3 --cflags` -c $<
.PHONY: all clean .PHONY: all clean

View File

@ -15,3 +15,5 @@ O_LARGEFILE = 0x8000
STDIN = 0 STDIN = 0
STDOUT = 1 STDOUT = 1
STDERR = 2 STDERR = 2
ENOENT = 2