550W Document
550W, a high-end OS
fs.c 文件参考
#include <asm/pgtable.h>
#include <asm/sbi.h>
#include <arch/riscv/include/asm/io.h>
#include <drivers/screen/screen.h>
#include <drivers/virtio/virtio.h>
#include <fs/fat32.h>
#include <fs/file.h>
#include <fs/fs.h>
#include <lib/stdio.h>
#include <lib/string.h>
#include <os/mm.h>
#include <os/pcb.h>
#include <os/sync.h>
#include <os/time.h>
#include <user/user_programs.h>
fs.c 的引用(Include)关系图:

struct  fd_mbox
 

宏定义

#define END_OF_DIR   0x00
 
#define EMPTY_ENTRY   0xE5
 

类型定义

typedef struct fd_mbox fd_mbox_t
 

函数

uint32_t fat32_fcluster2size (uint32_t first)
 
void * read_whole_dir (uint32_t first, uint32_t size)
 read whle file by first cluster 更多...
 
int write_whole_dir (uint32_t first, void *data, uint32_t write)
 write whole file by first cluster 更多...
 
uint32_t alloc_cluster (uint32_t first)
 alloc a nea cluster for a file/dir 更多...
 
void filename2path (char *path, char *name, const char *filename)
 divede filename into path and name 更多...
 
char * path2name (char *path, char *name)
 
int is_end_of_dir (dentry_t *entry)
 
int is_empty_entry (dentry_t *entry)
 
int ldentry2name (dentry_t *entry, char *name)
 get name from ldentry, return offset of these lentry 更多...
 
int sdentry2name (dentry_t *entry, char *name)
 get name from sdentry, return offset(1) of this entry 更多...
 
int dentry2name (dentry_t *entry, char *name)
 get name from dentry, return offset of this whole entry 更多...
 
int fat32_name2offset (char *name, dentry_t *entry)
 
int fat32_name2dir (char *name, dir_info_t *now)
 search file in dir, fail then return 0, other means hit 更多...
 
int fat32_new_dentry (dentry_t *entry, uint32_t flags, int len, char *name)
 set new dentry 更多...
 
int alloc_dentry (dentry_t **dtable, char *name, mode_t flags, mode_t mode, dir_info_t *now)
 create a new dentry in dtable, return sdentry offest of new entry, -1 means fail 更多...
 
int destroy_dentry (dentry_t *dtable, int offset)
 destroy an dir entry, include LFN and sn 更多...
 
int fat32_path2dir (char *path, dir_info_t *new, dir_info_t now)
 fail return 0 更多...
 
int fat32_dentry2fd (dentry_t *entry, fd_t *file, mode_t flags, mode_t mode, char *name)
 
int fs_init ()
 
long sys_getcwd (char *buf, size_t size)
 
int sys_pipe2 (int *fd, mode_t flags)
 allocate pipe to fd[2]. 更多...
 
int sys_dup (int old)
 copy a fd and return the new one 更多...
 
int sys_dup3 (int old, int new, mode_t flags)
 copy a fd and return the new one 更多...
 
int sys_mkdirat (int dirfd, const char *path_0, mode_t mode)
 mk dir at patch, success 0 fail -1 更多...
 
int sys_chdir (char *path)
 change dir by patch, fail return -1 更多...
 
int sys_getdents64 (int fd, dirent64_t *dirent, size_t len)
 
int sys_openat (int dirfd, const char *filename, mode_t flags, mode_t mode)
 open a file with path/cwd+path/dirfd+path 更多...
 
int sys_close (int fd)
 
int sys_linkat (int old, const char *oldname, int newd, const char *newname, mode_t flags)
 
int sys_unlinkat (int dirfd, const char *path_0, mode_t flags)
 rm file directed by path, 0 success -1 fail 更多...
 
int sys_mount (const char *special, const char *dir, const char *type, mode_t flags, void *data)
 
int sys_umount2 (const char *special, mode_t flags)
 
ssize_t sys_read (int fd, char *buf, size_t count)
 read count bytes to buf of fd file 更多...
 
ssize_t sys_write (int fd, const char *buf, size_t count)
 
int sys_fstat (int fd, kstat_t *statbuf)
 
int fs_load_file (const char *name, uint8_t **bin, int *len)
 load elf file in root dir, 0 success, -1 fail 更多...
 
void * sys_mmap (void *addr, size_t length, int prot, int flags, int fd, off_t offset)
 
int sys_munmap (void *addr, size_t length)
 
bool fs_check_file_existence (const char *name)
 

变量

fat32_t fat
 
dir_info_t root_dir = {.name = "/\0"}
 
dir_info_t cur_dir = {.name = "/\0"}
 
fd_mbox_t fd_mbox_map [MAX_FD]
 

宏定义说明

◆ EMPTY_ENTRY

#define EMPTY_ENTRY   0xE5

◆ END_OF_DIR

#define END_OF_DIR   0x00

类型定义说明

◆ fd_mbox_t

typedef struct fd_mbox fd_mbox_t

函数说明

◆ alloc_cluster()

uint32_t alloc_cluster ( uint32_t  first)

alloc a nea cluster for a file/dir

参数
firstid of first cluster
返回
alloced cluster num

◆ alloc_dentry()

int alloc_dentry ( dentry_t **  dtable,
char *  name,
mode_t  flags,
mode_t  mode,
dir_info_t now 
)

create a new dentry in dtable, return sdentry offest of new entry, -1 means fail

参数
dtablewhole table of dir
namefile name
nownow dir info
flagscreate file(RW) or dir by O_DIRECTORY

◆ dentry2name()

int dentry2name ( dentry_t entry,
char *  name 
)

get name from dentry, return offset of this whole entry

参数
namefile name
entryfirst entry of file

◆ destroy_dentry()

int destroy_dentry ( dentry_t dtable,
int  offset 
)

destroy an dir entry, include LFN and sn

参数
dtablewhole table of dir
offsetoffset for sn

◆ fat32_dentry2fd()

int fat32_dentry2fd ( dentry_t entry,
fd_t file,
mode_t  flags,
mode_t  mode,
char *  name 
)

◆ fat32_fcluster2size()

uint32_t fat32_fcluster2size ( uint32_t  first)

◆ fat32_name2dir()

int fat32_name2dir ( char *  name,
dir_info_t now 
)

search file in dir, fail then return 0, other means hit

参数
namefile name
nownow dir info

◆ fat32_name2offset()

int fat32_name2offset ( char *  name,
dentry_t entry 
)

◆ fat32_new_dentry()

int fat32_new_dentry ( dentry_t entry,
uint32_t  flags,
int  len,
char *  name 
)

set new dentry

参数
namefile name
flagsfile flags
lenlength of LFN

◆ fat32_path2dir()

int fat32_path2dir ( char *  path,
dir_info_t new,
dir_info_t  now 
)

fail return 0

参数
pathbuffer for dir path
newtarget dir info
nownow dir info

◆ filename2path()

void filename2path ( char *  path,
char *  name,
const char *  filename 
)

divede filename into path and name

参数
pathbuffer for dir path
namebuffer file name
filenamefilename with path

◆ fs_check_file_existence()

bool fs_check_file_existence ( const char *  name)

◆ fs_init()

int fs_init ( )

◆ fs_load_file()

int fs_load_file ( const char *  name,
uint8_t **  bin,
int *  len 
)

load elf file in root dir, 0 success, -1 fail

参数
nameelf file should be loaded
返回
elf bit array

◆ is_empty_entry()

int is_empty_entry ( dentry_t entry)

◆ is_end_of_dir()

int is_end_of_dir ( dentry_t entry)

◆ ldentry2name()

int ldentry2name ( dentry_t entry,
char *  name 
)

get name from ldentry, return offset of these lentry

参数
namefile name
entryfirst entry of LFN entry

◆ path2name()

char* path2name ( char *  path,
char *  name 
)
参数
pathbuffer for dir path
namefile in now dir
lenname len

◆ read_whole_dir()

void* read_whole_dir ( uint32_t  first,
uint32_t  size 
)

read whle file by first cluster

参数
firstid of first cluster
返回
addr of data, callee should free buffer by calling func(TODO)

◆ sdentry2name()

int sdentry2name ( dentry_t entry,
char *  name 
)

get name from sdentry, return offset(1) of this entry

参数
namefile name
entryfirst entry of file

◆ sys_chdir()

int sys_chdir ( char *  path)

change dir by patch, fail return -1

参数
pathbuffer for dir path

◆ sys_close()

int sys_close ( int  fd)

◆ sys_dup()

int sys_dup ( int  old)

copy a fd and return the new one

参数
oldold fd

◆ sys_dup3()

int sys_dup3 ( int  old,
int  new,
mode_t  flags 
)

copy a fd and return the new one

参数
oldold fd
newnew fd

◆ sys_fstat()

int sys_fstat ( int  fd,
kstat_t statbuf 
)

◆ sys_getcwd()

long sys_getcwd ( char *  buf,
size_t  size 
)
参数
bufbuffer for cur_dir path
sizebytes of buf

◆ sys_getdents64()

int sys_getdents64 ( int  fd,
dirent64_t dirent,
size_t  len 
)

◆ sys_linkat()

int sys_linkat ( int  old,
const char *  oldname,
int  newd,
const char *  newname,
mode_t  flags 
)

◆ sys_mkdirat()

int sys_mkdirat ( int  dirfd,
const char *  path_0,
mode_t  mode 
)

mk dir at patch, success 0 fail -1

参数
dir_fdmkdir at this dir
flagAT_FDCWD means mkdir at cwd
pathpath if from '/', other just means dir name

◆ sys_mmap()

void* sys_mmap ( void *  addr,
size_t  length,
int  prot,
int  flags,
int  fd,
off_t  offset 
)

◆ sys_mount()

int sys_mount ( const char *  special,
const char *  dir,
const char *  type,
mode_t  flags,
void *  data 
)

◆ sys_munmap()

int sys_munmap ( void *  addr,
size_t  length 
)

◆ sys_openat()

int sys_openat ( int  dirfd,
const char *  filename,
mode_t  flags,
mode_t  mode 
)

open a file with path/cwd+path/dirfd+path

参数
fdfile descripter
bufbytes buf
countread len

◆ sys_pipe2()

int sys_pipe2 ( int *  fd,
mode_t  flags 
)

allocate pipe to fd[2].

参数
fdtarget fd array.
返回值
-1failed 0: succeed

◆ sys_read()

ssize_t sys_read ( int  fd,
char *  buf,
size_t  count 
)

read count bytes to buf of fd file

参数
fdfile descripter
bufbytes buf
countread len

◆ sys_umount2()

int sys_umount2 ( const char *  special,
mode_t  flags 
)

◆ sys_unlinkat()

int sys_unlinkat ( int  dirfd,
const char *  path_0,
mode_t  flags 
)

rm file directed by path, 0 success -1 fail

参数
dirfdfile descripter
pathdile path
count0 or AT_REMOVEDIR

◆ sys_write()

ssize_t sys_write ( int  fd,
const char *  buf,
size_t  count 
)

◆ write_whole_dir()

int write_whole_dir ( uint32_t  first,
void *  data,
uint32_t  write 
)

write whole file by first cluster

参数
firstid of first cluster
writewrite or not
datadata size need to be size from first cluster?
返回
0 means success, fail otherwise

变量说明

◆ cur_dir

dir_info_t cur_dir = {.name = "/\0"}

◆ fat

fat32_t fat

◆ fd_mbox_map

fd_mbox_t fd_mbox_map[MAX_FD]

◆ root_dir

dir_info_t root_dir = {.name = "/\0"}