继续看rt-thread

/*

+------------------------------------------------------------------------------

| Project : Device Filesystem

+------------------------------------------------------------------------------

| Copyright 2004, 2005 www.fayfayspace.org.

| All rights reserved.

|------------------------------------------------------------------------------

| File : dfs_opts.h, the option definitions of Device FileSystem

|------------------------------------------------------------------------------

| Chang Logs:

| Date Author Notes

| 2005-01-22 ffxz The first version.

+------------------------------------------------------------------------------

*/


#ifndef __DFS_CONFIG_H__

#define __DFS_CONFIG_H__


/*

+------------------------------------------------------------------------------

| Device Manager options

+------------------------------------------------------------------------------

*/

/* Device Options parameters */

#define DEVICES_MAX8

#define DEVICE_NAME_MAX8


/*

+------------------------------------------------------------------------------

| Device Filesystem options

+------------------------------------------------------------------------------

*/

/* the max length of filesystem name */

#define DFS_FS_NAME_MAX4

/* the max type of filesystem */

#define DFS_FILESYSTEM_TYPES_MAX2


/* the max length of path name */

#define DFS_PATH_MAX256

/* the max length of file name */

#define DFS_FILE_MAX256


/* options for server task */

#define DFS_MBOX_NUMBER32

#define DFS_SERVER_STACK1024

#define DFS_SERVER_PRI110

#define DFS_SERVER_SLICE20


/*

+------------------------------------------------------------------------------

| FAT filesystem options

+------------------------------------------------------------------------------

*/

/* file name max length */

#define FAT_NAME_MAX256

/* max number of FAT filesystem */

#define FATFS_MAX2


/* the size of sector */

#define SECTOR_SIZE512


/* FAT table sector cache options */

#define FAT_CACHE_SIZE 0x10 /* config parameter */

#define FAT_CACHE_MASK (FAT_CACHE_SIZE-1)


#define GET16(x) (*(x)) | (*((x)+1) << 8)

#define GET32(x) (*(x)) | (*((x)+1) << 8) | (*((x)+2) << 16) | (*((x)+3) << 24)


#define SET16(x, v) \

do \

{ \

*(x) = (v) & 0x00ff; \

(*((x)+1)) = (v) >> 8; \

} while ( 0 )


#define SET32(x, v) \

do \

{ \

*(x) = (v) & 0x000000ff; \

(*((x)+1)) = ((v) >> 8) & 0x000000ff; \

(*((x)+2)) = ((v) >> 16) & 0x000000ff; \

(*((x)+3)) = ((v) >> 24); \

} while ( 0 )


#define DFS_DEBUG_INFO0x01

#define DFS_DEBUG_WARNING0x02

#define DFS_DEBUG_ERROR0x04


#define DFS_DEBUG_LEVEL(DFS_DEBUG_INFO | DFS_DEBUG_WARNING | DFS_DEBUG_ERROR)


/* #define DFS_DEBUG */

#ifdef DFS_DEBUG

#define dfs_log(level, x)do { if (level & DFS_DEBUG_LEVEL) \

{rt_kprintf("DFS %s, %d:", __FILE__, __LINE__); rt_kprintf x;\

rt_kprintf ("\n");}}while (0)

#else

#define dfs_log(level, x)

#endif


#endif


/*
+------------------------------------------------------------------------------
|        Device FileSystem
+------------------------------------------------------------------------------
| Copyright 2004, 2005  www.fayfayspace.org.
| All rights reserved.
|------------------------------------------------------------------------------
| File      : dfs_def.h, the definitions of Device FileSystem
|------------------------------------------------------------------------------
| Chang Logs:
| Date           Author       notes
| 2004-10-01     ffxz         The first version.
| 2004-10-14     ffxz         Clean up the code.
| 2005-01-22     ffxz         Clean up the code, port to MinGW
+------------------------------------------------------------------------------
*/
#ifndef __DFS_DEF_H__
#define __DFS_DEF_H__
#include <rtthread.h>   //这个也与线程有关
#include <dfs_config.h>
#if defined(RT_USING_NEWLIB) || defined (RT_USING_MINILIBC)
#include <string.h>
#endif
#ifndef __D_FS__
#define __D_FS__
#endif
/* Device error codes */
#define DFS_STATUS_OK           0       /* no error */
#define DFS_STATUS_ENOENT       2       /* No such file or directory */
#define DFS_STATUS_EIO          5       /* I/O error */
#define DFS_STATUS_ENXIO        6       /* No such device or address */
#define DFS_STATUS_EBADF        9       /* Bad file number */
#define DFS_STATUS_EAGIAN       11      /* Try again */
#define DFS_STATUS_ENOMEM       12      /* no memory */
#define DFS_STATUS_EBUSY        16      /* Device or resource busy */
#define DFS_STATUS_EEXIST       17      /* File exists */
#define DFS_STATUS_EXDEV        18      /* Cross-device link */
#define DFS_STATUS_ENODEV       19      /* No such device */
#define DFS_STATUS_ENOTDIR      20      /* Not a directory */
#define DFS_STATUS_EISDIR       21      /* Is a directory */
#define DFS_STATUS_EINVAL       22      /* Invalid argument */
#define DFS_STATUS_ENOSPC       28      /* No space left on device */
#define DFS_STATUS_EROFS        30      /* Read-only file system */
#define DFS_STATUS_ENOSYS       38      /* Function not implemented */
#define DFS_STATUS_ENOTEMPTY    39      /* Directory not empty */
#define DFS_STATUS_EMMOUNT      128     /* Filesystem table full */
/* Operation flags */
#define DFS_O_RDONLY        0000000
#define DFS_O_WRONLY        0000001
#define DFS_O_RDWR          0000002
#define DFS_O_ACCMODE       0000003
#define DFS_O_CREAT         0000100
#define DFS_O_EXCL          0000200
#define DFS_O_TRUNC         0001000
#define DFS_O_APPEND        0002000
#define DFS_O_DIRECTORY     0200000
/* File flags */
#define DFS_F_OPEN          0x01000000
#define DFS_F_DIRECTORY     0x02000000
#define DFS_F_EOF           0x04000000
#define DFS_F_ERR           0x08000000
/* Seek flags */
#define DFS_SEEK_SET         0
#define DFS_SEEK_CUR         1
#define DFS_SEEK_END         2
/* Stat codes */
#define DFS_S_IFMT      00170000
#define DFS_S_IFSOCK    0140000
#define DFS_S_IFLNK     0120000
#define DFS_S_IFREG     0100000
#define DFS_S_IFBLK     0060000
#define DFS_S_IFDIR     0040000
#define DFS_S_IFCHR     0020000
#define DFS_S_IFIFO     0010000
#define DFS_S_ISUID     0004000
#define DFS_S_ISGID     0002000
#define DFS_S_ISVTX     0001000
#define DFS_S_ISLNK(m)  (((m) & DFS_S_IFMT) == DFS_S_IFLNK)
#define DFS_S_ISREG(m)  (((m) & DFS_S_IFMT) == DFS_S_IFREG)
#define DFS_S_ISDIR(m)  (((m) & DFS_S_IFMT) == DFS_S_IFDIR)
#define DFS_S_ISCHR(m)  (((m) & DFS_S_IFMT) == DFS_S_IFCHR)
#define DFS_S_ISBLK(m)  (((m) & DFS_S_IFMT) == DFS_S_IFBLK)
#define DFS_S_ISFIFO(m) (((m) & DFS_S_IFMT) == DFS_S_IFIFO)
#define DFS_S_ISSOCK(m) (((m) & DFS_S_IFMT) == DFS_S_IFSOCK)
#define DFS_S_IRWXU     00700
#define DFS_S_IRUSR     00400
#define DFS_S_IWUSR     00200
#define DFS_S_IXUSR     00100
#define DFS_S_IRWXG     00070
#define DFS_S_IRGRP     00040
#define DFS_S_IWGRP     00020
#define DFS_S_IXGRP     00010
#define DFS_S_IRWXO     00007
#define DFS_S_IROTH     00004
#define DFS_S_IWOTH     00002
#define DFS_S_IXOTH     00001
#define DEVICE_GETGEOME         0
#define DEVICE_GETINFO          1
#define DEVICE_FORMAT           2
#define DEVICE_CLEAN_SECTOR     3
struct device_geometry
{
    rt_uint32_t sectro_count;       /* count of sectors */
    rt_uint32_t cylinder_count;     /* count of cylinder */
    rt_uint32_t sectors_per_track;  /* number of sectors per track */
    rt_uint32_t head_count;         /* count of head */
    rt_uint32_t bytes_per_sector;   /* number of bytes per sector */
};
struct dfs_stat
{
    rt_device_t st_dev;
    rt_uint16_t st_mode;
    rt_uint32_t st_size;
    rt_time_t   st_mtime;
    rt_uint32_t st_blksize;
};
#define stat dfs_stat
/* File types */
#define FT_REGULAR      0   /* regular file */
#define FT_SOCKET       1   /* socket file  */
#define FT_DIRECTORY    2   /* directory    */
#define FT_USER         3   /* user defined */
/* file descriptor */
struct dfs_fd
{
    char path[DFS_PATH_MAX + 1];/* Name (below mount point) */
    int type;                   /* Type (regular or socket) */
    int ref_count;              /* Descriptor reference count */
    struct dfs_filesystem* fs;  /* Resident file system */
    rt_uint32_t flags;          /* Descriptor flags */
    rt_size_t   size;           /* Size in bytes */
    rt_off_t    pos;            /* Current file position */
    void *data;                 /* Specific file system data */
};
#define DFS_DT_UNKNOWN  0x00
#define DFS_DT_REG      0x01
#define DFS_DT_DIR      0x02
struct dfs_dirent
{
    rt_uint8_t d_type;              /* The type of the file */
    rt_uint8_t d_namlen;            /* The length of the not including the terminating null file name */
    rt_uint16_t d_reclen;               /* length of this record */
    char   d_name[256];         /* The null-terminated file name */
};
#define dirent dfs_dirent
struct dfs_session
{
    rt_mailbox_t mbox;
};
#endif


/*
+------------------------------------------------------------------------------
| Project   : Device Filesystem
+------------------------------------------------------------------------------
| Copyright 2004, 2005  www.fayfayspace.org.
| All rights reserved.
|------------------------------------------------------------------------------
| File      : dfs_efs.h
|------------------------------------------------------------------------------
| Chang Logs:
| Date           Author       Notes
| 2010-02-06     Bernard      Add elm_init function declaration
+------------------------------------------------------------------------------
*/
#ifndef __DFS_ELM_H__
#define __DFS_ELM_H__
#ifdef __cplusplus
extern "C" {
#endif
int elm_init(void);
#ifdef __cplusplus
}
#endif
#endif
/*
+------------------------------------------------------------------------------
| Project   : Device Filesystem
+------------------------------------------------------------------------------
| Copyright 2004, 2005  www.fayfayspace.org.
| All rights reserved.
|------------------------------------------------------------------------------
| File      : dfs_fat.h, FAT 12/16/32 file system definitions
|------------------------------------------------------------------------------
| Chang Logs:
| Date           Author       Notes
| 2003-03-30     ffxz
+------------------------------------------------------------------------------
*/
#ifndef __DFS_FAT_H__
#define __DFS_FAT_H__
#ifdef __cplusplus
extern "C" {
#endif
int fatfs_init(void);  //这也单独一个文件,真奢侈
#ifdef __cplusplus
}
#endif
#endif
/*
+------------------------------------------------------------------------------
| Project   : Device Filesystem
+------------------------------------------------------------------------------
| Copyright 2004, 2005  www.fayfayspace.org.
| All rights reserved.
|------------------------------------------------------------------------------
| File      : dfs_init.h, the initilization definitions of Device FileSystem
|------------------------------------------------------------------------------
| Chang Logs:
| Date           Author       Notes
| 2005-02-21     ffxz         The first version.
+------------------------------------------------------------------------------
*/
#ifndef __DFS_INIT_H__
#define __DFS_INIT_H__
#ifdef __cplusplus
extern "C" {
#endif
/* initilization of dfs */
void dfs_init(void);
/* initilization of dfs with filesystem server */
void dfs_server_init(void);
#ifdef __cplusplus
}
#endif
#endif
/*
+------------------------------------------------------------------------------
| Project   : Device Filesystem
+------------------------------------------------------------------------------
| Copyright 2004, 2005  www.fayfayspace.org.
| All rights reserved.
|------------------------------------------------------------------------------
| File      : dfs_fs.h, the filesystem related defines of Device FileSystem
|------------------------------------------------------------------------------
| Chang Logs:
| Date           Author       Notes
| 2005-02-22     ffxz         The first version.
+------------------------------------------------------------------------------
*/
#ifndef __DFS_FS_H__
#define __DFS_FS_H__
#include <dfs_def.h>
#include <dfs_config.h>
/* Pre-declaration */
struct dfs_filesystem;
struct dfs_fd;
struct dfs_dirent;
/* File system operations struct */
struct dfs_filesystem_operation     //全是函数指针。。。这种用法才是关键技术,回调函数。见过很多次,自己从来没这么写过
{
    char name[DFS_FS_NAME_MAX + 1];
    int (*mount)    (struct dfs_filesystem* fs);
    int (*unmount)  (struct dfs_filesystem* fs);
    int (*open) (struct dfs_fd* fd);
    int (*close)    (struct dfs_fd* fd);
    int (*ioctl)    (struct dfs_fd* fd, int cmd, void *args);
    int (*read) (struct dfs_fd* fd, void* buf, rt_size_t count);
    int (*write)    (struct dfs_fd* fd, const void* buf, rt_size_t count);
    int (*lseek)    (struct dfs_fd* fd, rt_off_t offset);
    int (*getdents) (struct dfs_fd* fd, struct dfs_dirent* dirp, rt_uint32_t count);
    int (*unlink)   (struct dfs_filesystem* fs, const char* pathname);
    int (*stat)     (struct dfs_filesystem* fs, const char* filename, struct dfs_stat* buf);
    int (*rename)   (struct dfs_filesystem* fs, const char* oldpath, const char* newpath);
};
/* Mounted file system */
struct dfs_filesystem
{
    rt_device_t dev_id;                 /* Attached device */
    char path[DFS_PATH_MAX + 1];            /* File system mount point */
    struct dfs_filesystem_operation* ops;   /* Operations for file system type */
    rt_uint32_t block_id;                   /* Current block_id on attached device */
    void *data;                         /* Specific file system data */
};
/* file system partition table */
struct dfs_partition
{
    rt_uint8_t type;        /* file system type */
    rt_off_t  offset;       /* partition start offset */
    rt_size_t size;         /* partition size */
    rt_sem_t lock; 
};
//总共就这几个函数,就是文件系统?
int dfs_register(struct dfs_filesystem_operation* ops);
struct dfs_filesystem* dfs_filesystem_lookup(const char *path);
rt_err_t dfs_filesystem_get_partition(struct dfs_partition* part, rt_uint8_t* buf, rt_uint32_t pindex);
int dfs_mount(const char* device_name, const char* path,
       const char* filesystemtype, rt_uint32_t rwflag, const
       void* data);
int dfs_unmount(const char *specialfile);
/* extern variable */
extern struct dfs_filesystem_operation* filesystem_operation_table[];
extern struct dfs_filesystem filesystem_table[];
extern char working_directory[];
void dfs_lock(void);
void dfs_unlock(void);
#endif
/*
+------------------------------------------------------------------------------
| Project   : Device Filesystem
+------------------------------------------------------------------------------
| Copyright 2004, 2005  www.fayfayspace.org.
| All rights reserved.
|------------------------------------------------------------------------------
| File      : dfs_posix.h, the filesystem related defines of Device FileSystem
|------------------------------------------------------------------------------
| Chang Logs:
| Date           Author       Notes
| 2009-05-27     Yi.qiu         The first version.
+------------------------------------------------------------------------------
*/
#ifndef __DFS_POSIX_H__
#define __DFS_POSIX_H__
#include <dfs_raw.h>
#define O_RDONLY    DFS_O_RDONLY
#define O_WRONLY    DFS_O_WRONLY
#define O_RDWR      DFS_O_RDWR
#define O_ACCMODE   DFS_O_ACCMODE
#define O_CREAT     DFS_O_CREAT
#define O_EXCL      DFS_O_EXCL
#define O_TRUNC     DFS_O_TRUNC
#define O_APPEND    DFS_O_APPEND
#define O_DIRECTORY DFS_O_DIRECTORY
#define S_IFMT      DFS_S_IFMT
#define S_IFSOCK    DFS_S_IFSOCK
#define S_IFLNK     DFS_S_IFLNK
#define S_IFREG     DFS_S_IFREG
#define S_IFBLK     DFS_S_IFBLK
#define S_IFDIR     DFS_S_IFDIR
#define S_IFCHR     DFS_S_IFCHR
#define S_IFIFO     DFS_S_IFIFO
#define S_ISUID     DFS_S_ISUID
#define S_ISGID     DFS_S_ISGID
#define S_ISVTX     DFS_S_ISVTX
#define S_ISLNK(m)  (((m) & DFS_S_IFMT) == DFS_S_IFLNK)
#define S_ISREG(m)  (((m) & DFS_S_IFMT) == DFS_S_IFREG)
#define S_ISDIR(m)  (((m) & DFS_S_IFMT) == DFS_S_IFDIR)
#define S_ISCHR(m)  (((m) & DFS_S_IFMT) == DFS_S_IFCHR)
#define S_ISBLK(m)  (((m) & DFS_S_IFMT) == DFS_S_IFBLK)
#define S_ISFIFO(m) (((m) & DFS_S_IFMT) == DFS_S_IFIFO)
#define S_ISSOCK(m) (((m) & DFS_S_IFMT) == DFS_S_IFSOCK)
#define S_IRWXU     DFS_S_IRWXU
#define S_IRUSR     DFS_S_IRUSR
#define S_IWUSR     DFS_S_IWUSR
#define S_IXUSR     DFS_S_IXUSR
#define S_IRWXG     DFS_S_IRWXG
#define S_IRGRP     DFS_S_IRGRP
#define S_IWGRP     DFS_S_IWGRP
#define S_IXGRP     DFS_S_IXGRP
#define S_IRWXO     DFS_S_IRWXO
#define S_IROTH     DFS_S_IROTH
#define S_IWOTH     DFS_S_IWOTH
#define S_IXOTH     DFS_S_IXOTH
#define SEEK_SET    DFS_SEEK_SET
#define SEEK_CUR    DFS_SEEK_CUR
#define SEEK_END    DFS_SEEK_END
typedef struct
{
    int fd;     /* directory file */
    char buf[512];
    int num;
    int cur;
} DIR;
//哇。。标准函数啊
/* file api*/
int open(const char *file, int flags, int mode);
int close(int d);
int read(int fd, char *buf, int len);
int write(int fd, char *buf, int len);
int lseek(int fd, int offset, int dir);
int rename(const char* old, const char* new );
int unlink(const char *pathname);
int stat(const char *file, struct dfs_stat *buf);
/* directory api*/
int mkdir (const char *path, rt_uint16_t mode);
int rmdir(const char *path);
DIR* opendir(const char* name);
struct dfs_dirent* readdir(DIR *d);
rt_off_t telldir(DIR *d);
void seekdir(DIR *d, rt_off_t offset);
void rewinddir(DIR *d);
int closedir(DIR* d);
int chdir(const char *path);
char* getcwd(char *buf, rt_size_t size);
#endif
/*
+------------------------------------------------------------------------------
| Project   : Device Filesystem
+------------------------------------------------------------------------------
| Copyright 2004, 2005  www.fayfayspace.org.
| All rights reserved.
|------------------------------------------------------------------------------
| File      : dfs_raw.h, the raw APIs of Device FileSystem
|------------------------------------------------------------------------------
| Chang Logs:
| Date           Author       Notes
| 2005-01-26     ffxz         The first version
+------------------------------------------------------------------------------
*/
#ifndef __DFS_RAW_H__
#define __DFS_RAW_H__
#include <dfs_def.h>
#include <dfs_fs.h>
//底层的读写?--》内核的读写?
int dfile_raw_open(struct dfs_fd* fd, const char *path, int flags);
int dfile_raw_close(struct dfs_fd* fd);
int dfile_raw_ioctl(struct dfs_fd* fd, int cmd, void *args);
int dfile_raw_read(struct dfs_fd* fd, void *buf, rt_size_t len);
int dfile_raw_getdents(struct dfs_fd* fd, struct dfs_dirent* dirp, rt_size_t nbytes);
int dfile_raw_unlink(const char *path);
int dfile_raw_write(struct dfs_fd* fd, const void *buf, rt_size_t len);
int dfile_raw_lseek(struct dfs_fd* fd, rt_off_t offset);
int dfile_raw_stat(const char *path, struct dfs_stat *buf);
int dfile_raw_rename(const char* oldpath, const char* newpath);
/* FD APIs */
int fd_new(void);
struct dfs_fd* fd_get(int fd);
void fd_put(struct dfs_fd* fd);
#endif
/*
+------------------------------------------------------------------------------
| Project   : Device Filesystem
+------------------------------------------------------------------------------
| Copyright 2004, 2005  www.fayfayspace.org.
| All rights reserved.
|------------------------------------------------------------------------------
| File      : dfs_util.h, some misc definitions of Device FileSystem
|------------------------------------------------------------------------------
| Chang Logs:
| Date           Author       Notes
| 2005-01-26     ffxz         The first version
+------------------------------------------------------------------------------
*/
#ifndef __DFS_UTIL_H__
#define __DFS_UTIL_H__
#include <dfs_def.h>
int dir_name(const char* path, char* dirname, int len);
int file_name(const char* path, char* filename, int len);
int next_dir_name(const char* path, int pos, char* next);
void build_fullpath(const char *directory, const char *filename, char *fullpath);
int str_is_prefix(const char* prefix, const char* str);
#if !defined(RT_USING_MINILIBC) && !defined(RT_USING_NEWLIB)
char *strrchr(const char *t, int c);
#if defined (__ARMCC_VERSION) && (__ARMCC_VERSION / 10000 < 35)
#include <stddef.h>
int strncasecmp(const char* s1, const char* s2, size_t len);
#endif /* end of __ARMCC_VERSION */
#endif
#endif


头文件看完。c文件才是技术关键

你可能感兴趣的:(c,RT-Thread)