From b4a98e4457c3a58301b41db5db50bfbca4546255 Mon Sep 17 00:00:00 2001 From: "bernard.xiong" Date: Tue, 19 Oct 2010 08:18:24 +0000 Subject: Improve the Device File System of the POSIX compatibility git-svn-id: https://rt-thread.googlecode.com/svn/trunk@1015 bbd45198-f89e-11dd-88c7-29a3b14d5316 --- components/dfs/include/dfs_def.h | 146 +++++++++++++++++++++++++++++++------ components/dfs/include/dfs_file.h | 4 +- components/dfs/include/dfs_fs.h | 9 +-- components/dfs/include/dfs_posix.h | 36 +++++---- 4 files changed, 149 insertions(+), 46 deletions(-) (limited to 'components/dfs/include') diff --git a/components/dfs/include/dfs_def.h b/components/dfs/include/dfs_def.h index e220487043..62ade80bd2 100644 --- a/components/dfs/include/dfs_def.h +++ b/components/dfs/include/dfs_def.h @@ -19,14 +19,115 @@ #include #include -#if defined(RT_USING_NEWLIB) || defined (RT_USING_MINILIBC) -#include -#endif - #ifndef __D_FS__ #define __D_FS__ #endif +#define DEVICE_GETGEOME 0 +#define DEVICE_GETINFO 1 +#define DEVICE_FORMAT 2 +#define DEVICE_CLEAN_SECTOR 3 + +/* File flags */ +#define DFS_F_OPEN 0x01000000 +#define DFS_F_DIRECTORY 0x02000000 +#define DFS_F_EOF 0x04000000 +#define DFS_F_ERR 0x08000000 + +#if defined(RT_USING_NEWLIB) +#include +#include /* used for struct stat */ +#include /* used for struct statfs */ +#include /* used for error number */ +#include /* used for operation flags */ +#include /* used for SEEK_SET/CUR/END */ +#include /* used for struct dirent */ + +/* Device error codes */ +#define DFS_STATUS_OK 0 /* no error */ +#define DFS_STATUS_ENOENT ENOENT /* No such file or directory */ +#define DFS_STATUS_EIO EIO /* I/O error */ +#define DFS_STATUS_ENXIO ENXIO /* No such device or address */ +#define DFS_STATUS_EBADF EBADF /* Bad file number */ +#define DFS_STATUS_EAGAIN EAGAIN /* Try again */ +#define DFS_STATUS_ENOMEM ENOMEM /* no memory */ +#define DFS_STATUS_EBUSY EBUSY /* Device or resource busy */ +#define DFS_STATUS_EEXIST EEXIST /* File exists */ +#define DFS_STATUS_EXDEV EXDEV /* Cross-device link */ +#define DFS_STATUS_ENODEV ENODEV /* No such device */ +#define DFS_STATUS_ENOTDIR ENOTDIR /* Not a directory */ +#define DFS_STATUS_EISDIR EISDIR /* Is a directory */ +#define DFS_STATUS_EINVAL EINVAL /* Invalid argument */ +#define DFS_STATUS_ENOSPC ENOSPC /* No space left on device */ +#define DFS_STATUS_EROFS EROFS /* Read-only file system */ +#define DFS_STATUS_ENOSYS ENOSYS /* Function not implemented */ +#define DFS_STATUS_ENOTEMPTY ENOTEMPTY /* Directory not empty */ + +/* Operation flags */ +#define DFS_O_RDONLY O_RDONLY +#define DFS_O_WRONLY O_WRONLY +#define DFS_O_RDWR O_RDWR +#define DFS_O_ACCMODE O_ACCMODE +#define DFS_O_CREAT O_CREAT +#define DFS_O_EXCL O_EXCL +#define DFS_O_TRUNC O_TRUNC +#define DFS_O_APPEND O_APPEND +#define DFS_O_DIRECTORY O_DIRECTORY + +/* Seek flags */ +#define DFS_SEEK_SET SEEK_SET +#define DFS_SEEK_CUR SEEK_CUR +#define DFS_SEEK_END SEEK_END + +/* Stat codes */ +#define DFS_S_IFMT S_IFMT +#define DFS_S_IFSOCK S_IFSOCK +#define DFS_S_IFLNK S_IFLNK +#define DFS_S_IFREG S_IFREG +#define DFS_S_IFBLK S_IFBLK +#define DFS_S_IFDIR S_IFDIR +#define DFS_S_IFCHR S_IFCHR +#define DFS_S_IFIFO S_IFIFO +#define DFS_S_ISUID S_ISUID +#define DFS_S_ISGID S_ISGID +#define DFS_S_ISVTX S_ISVTX + +#define DFS_S_ISLNK(m) S_ISLNK(m) +#define DFS_S_ISREG(m) S_ISREG(m) +#define DFS_S_ISDIR(m) S_ISDIR(m) +#define DFS_S_ISCHR(m) S_ISCHR(m) +#define DFS_S_ISBLK(m) S_ISBLK(m) +#define DFS_S_ISFIFO(m) S_ISFIFO(m) +#define DFS_S_ISSOCK(m) S_ISSOCK(m) + +#define DFS_S_IRWXU S_IRWXU +#define DFS_S_IRUSR S_IRUSR +#define DFS_S_IWUSR S_IWUSR +#define DFS_S_IXUSR S_IXUSR + +#define DFS_S_IRWXG S_IRWXG +#define DFS_S_IRGRP S_IRGRP +#define DFS_S_IWGRP S_IWGRP +#define DFS_S_IXGRP S_IXGRP + +#define DFS_S_IRWXO S_IRWXO +#define DFS_S_IROTH S_IROTH +#define DFS_S_IWOTH S_IWOTH +#define DFS_S_IXOTH S_IXOTH + +/* Dirent types */ +#define DFS_DT_UNKNOWN DT_UNKNOWN +#define DFS_DT_REG DT_REG +#define DFS_DT_DIR DT_DIR + +#else +#ifdef RT_USING_MINILIBC +#include +#else +typedef long off_t; +typedef int mode_t; +#endif + /* Device error codes */ #define DFS_STATUS_OK 0 /* no error */ #define DFS_STATUS_ENOENT 2 /* No such file or directory */ @@ -46,7 +147,6 @@ #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 @@ -106,12 +206,7 @@ #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 _stat +struct stat { rt_device_t st_dev; rt_uint16_t st_mode; @@ -120,7 +215,7 @@ struct _stat rt_uint32_t st_blksize; }; -struct _statfs +struct statfs { rt_size_t f_bsize; /* block size */ rt_size_t f_blocks; /* total data blocks in file system */ @@ -133,6 +228,20 @@ struct _statfs #define FT_DIRECTORY 2 /* directory */ #define FT_USER 3 /* user defined */ +/* Dirent types */ +#define DFS_DT_UNKNOWN 0x00 +#define DFS_DT_REG 0x01 +#define DFS_DT_DIR 0x02 + +struct 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[DFS_PATH_MAX]; /* The null-terminated file name */ +}; +#endif + /* file descriptor */ struct dfs_fd { @@ -149,16 +258,5 @@ struct dfs_fd void *data; /* Specific file system data */ }; -#define DFS_DT_UNKNOWN 0x00 -#define DFS_DT_REG 0x01 -#define DFS_DT_DIR 0x02 - -struct _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[DFS_PATH_MAX]; /* The null-terminated file name */ -}; - #endif + diff --git a/components/dfs/include/dfs_file.h b/components/dfs/include/dfs_file.h index e0455e00c9..ab75916977 100644 --- a/components/dfs/include/dfs_file.h +++ b/components/dfs/include/dfs_file.h @@ -24,11 +24,11 @@ int dfs_file_open(struct dfs_fd* fd, const char *path, int flags); int dfs_file_close(struct dfs_fd* fd); int dfs_file_ioctl(struct dfs_fd* fd, int cmd, void *args); int dfs_file_read(struct dfs_fd* fd, void *buf, rt_size_t len); -int dfs_file_getdents(struct dfs_fd* fd, struct _dirent* dirp, rt_size_t nbytes); +int dfs_file_getdents(struct dfs_fd* fd, struct dirent* dirp, rt_size_t nbytes); int dfs_file_unlink(const char *path); int dfs_file_write(struct dfs_fd* fd, const void *buf, rt_size_t len); int dfs_file_lseek(struct dfs_fd* fd, rt_off_t offset); -int dfs_file_stat(const char *path, struct _stat *buf); +int dfs_file_stat(const char *path, struct stat *buf); int dfs_file_rename(const char* oldpath, const char* newpath); #endif diff --git a/components/dfs/include/dfs_fs.h b/components/dfs/include/dfs_fs.h index d47e5c93cf..d698df429b 100644 --- a/components/dfs/include/dfs_fs.h +++ b/components/dfs/include/dfs_fs.h @@ -20,7 +20,6 @@ /* Pre-declaration */ struct dfs_filesystem; struct dfs_fd; -struct dfs_dirent; /* File system operations struct */ struct dfs_filesystem_operation @@ -33,7 +32,7 @@ struct dfs_filesystem_operation /* make a file system */ int (*mkfs) (const char* device_name); - int (*statfs) (struct dfs_filesystem* fs, struct _statfs *buf); + int (*statfs) (struct dfs_filesystem* fs, struct statfs *buf); int (*open) (struct dfs_fd* fd); int (*close) (struct dfs_fd* fd); @@ -42,10 +41,10 @@ struct dfs_filesystem_operation int (*write) (struct dfs_fd* fd, const void* buf, rt_size_t count); int (*flush) (struct dfs_fd* fd); int (*lseek) (struct dfs_fd* fd, rt_off_t offset); - int (*getdents) (struct dfs_fd* fd, struct _dirent* dirp, rt_uint32_t count); + int (*getdents) (struct dfs_fd* fd, struct 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 _stat* buf); + int (*stat) (struct dfs_filesystem* fs, const char* filename, struct stat* buf); int (*rename) (struct dfs_filesystem* fs, const char* oldpath, const char* newpath); }; @@ -86,6 +85,6 @@ extern char working_directory[]; void dfs_lock(void); void dfs_unlock(void); -int dfs_statfs(const char* path, struct _statfs* buffer); +int dfs_statfs(const char* path, struct statfs* buffer); #endif diff --git a/components/dfs/include/dfs_posix.h b/components/dfs/include/dfs_posix.h index d59421fd8d..7897893bef 100644 --- a/components/dfs/include/dfs_posix.h +++ b/components/dfs/include/dfs_posix.h @@ -18,6 +18,7 @@ #include #include +#ifndef RT_USING_NEWLIB #define O_RDONLY DFS_O_RDONLY #define O_WRONLY DFS_O_WRONLY #define O_RDWR DFS_O_RDWR @@ -75,30 +76,35 @@ typedef struct int cur; } DIR; -#define statfs _statfs -#define dirent _dirent +/* directory api*/ +int mkdir (const char *path, mode_t mode); +DIR* opendir(const char* name); +struct dirent* readdir(DIR *d); +long telldir(DIR *d); +void seekdir(DIR *d, off_t offset); +void rewinddir(DIR *d); +int closedir(DIR* d); + +#else +/* use newlib header file */ +#include +#endif /* 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 read(int fd, void *buf, size_t len); +int write(int fd, const void *buf, size_t len); +off_t lseek(int fd, off_t offset, int whence); int rename(const char* old, const char* new ); int unlink(const char *pathname); -int stat(const char *file, struct _stat *buf); -int statfs(const char *path, struct _statfs *buf); +int stat(const char *file, struct stat *buf); +int statfs(const char *path, struct statfs *buf); /* directory api*/ -int mkdir (const char *path, rt_uint16_t mode); int rmdir(const char *path); -DIR* opendir(const char* name); -struct _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); +char *getcwd(char *buf, size_t size); #endif + -- cgit v1.3.1