From b4a98e4457c3a58301b41db5db50bfbca4546255 Mon Sep 17 00:00:00 2001 From: "bernard.xiong" Date: Tue, 19 Oct 2010 08:18:24 +0000 Subject: [PATCH] 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/filesystems/elmfat/dfs_elm.c | 50 ++++-- components/dfs/filesystems/nfs/dfs_nfs.c | 14 +- components/dfs/filesystems/nfs/rpc/types.h | 6 +- .../dfs/filesystems/skeleton/skeleton.c | 81 ++++++++++ .../dfs/filesystems/skeleton/skeleton.h | 8 + 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 +++-- components/dfs/src/dfs_file.c | 25 +-- components/dfs/src/dfs_fs.c | 6 +- components/dfs/src/dfs_posix.c | 34 ++-- components/libc/minilibc/rand.c | 1 + components/libc/minilibc/sys/types.h | 10 +- components/libc/newlib/sys/dirent.h | 57 +++++++ components/libc/newlib/sys/fcntl.h | 15 ++ components/libc/newlib/sys/statfs.h | 13 ++ 17 files changed, 401 insertions(+), 114 deletions(-) create mode 100644 components/dfs/filesystems/skeleton/skeleton.c create mode 100644 components/dfs/filesystems/skeleton/skeleton.h create mode 100644 components/libc/newlib/sys/dirent.h create mode 100644 components/libc/newlib/sys/fcntl.h create mode 100644 components/libc/newlib/sys/statfs.h diff --git a/components/dfs/filesystems/elmfat/dfs_elm.c b/components/dfs/filesystems/elmfat/dfs_elm.c index 43facf04eb..a130e40ff8 100644 --- a/components/dfs/filesystems/elmfat/dfs_elm.c +++ b/components/dfs/filesystems/elmfat/dfs_elm.c @@ -1,9 +1,12 @@ -#include -#include - #include "ffconf.h" #include "ff.h" +/* ELM FatFs provide a DIR struct */ +#define HAVE_DIR_STRUCTURE + +#include +#include + static rt_device_t disk[_DRIVES] = {0}; static int elm_result_to_dfs(FRESULT result) @@ -67,7 +70,7 @@ int dfs_elm_mount(struct dfs_filesystem* fs, unsigned long rwflag, const void* d break; } } - if (index == _DRIVES) return -DFS_STATUS_EMMOUNT; + if (index == _DRIVES) return -DFS_STATUS_ENOSPC; /* get device */ disk[index] = fs->dev_id; @@ -94,14 +97,31 @@ int dfs_elm_mount(struct dfs_filesystem* fs, unsigned long rwflag, const void* d int dfs_elm_unmount(struct dfs_filesystem* fs) { FATFS *fat; + FRESULT result; + rt_uint32_t index; + fat = (FATFS*) fs->data; RT_ASSERT(fat != RT_NULL); - /* elm not support unmount */ - rt_kprintf("elm fatfs not support unmount\n"); + /* find the device index and then umount it */ + for (index = 0; index < _DRIVES; index ++) + { + if (disk[index] == fs->dev_id) + { + result = f_mount(index, RT_NULL); - return 0; + if (result == FR_OK) + { + fs->data = RT_NULL; + disk[index] = RT_NULL; + rt_free(fat); + return DFS_STATUS_OK; + } + } + } + + return -DFS_STATUS_ENOENT; } int dfs_elm_mkfs(const char* device_name) @@ -134,7 +154,7 @@ int dfs_elm_mkfs(const char* device_name) return -DFS_STATUS_EIO; } -int dfs_elm_statfs(struct dfs_filesystem* fs, struct _statfs *buf) +int dfs_elm_statfs(struct dfs_filesystem* fs, struct statfs *buf) { FATFS *f; FRESULT res; @@ -374,19 +394,19 @@ int dfs_elm_lseek(struct dfs_fd* file, rt_off_t offset) return elm_result_to_dfs(result); } -int dfs_elm_getdents(struct dfs_fd* file, struct _dirent* dirp, rt_uint32_t count) +int dfs_elm_getdents(struct dfs_fd* file, struct dirent* dirp, rt_uint32_t count) { DIR* dir; FILINFO fno; FRESULT result; rt_uint32_t index; - struct _dirent* d; + struct dirent* d; dir = (DIR*)(file->data); RT_ASSERT(dir != RT_NULL); /* make integer count */ - count = (count / sizeof(struct _dirent)) * sizeof(struct _dirent); + count = (count / sizeof(struct dirent)) * sizeof(struct dirent); if ( count == 0 ) return -DFS_STATUS_EINVAL; #if _USE_LFN @@ -416,11 +436,11 @@ int dfs_elm_getdents(struct dfs_fd* file, struct _dirent* dirp, rt_uint32_t coun else d->d_type = DFS_DT_REG; d->d_namlen = rt_strlen(fn); - d->d_reclen = (rt_uint16_t)sizeof(struct _dirent); + d->d_reclen = (rt_uint16_t)sizeof(struct dirent); rt_strncpy(d->d_name, fn, rt_strlen(fn) + 1); index ++; - if ( index * sizeof(struct _dirent) >= count ) + if ( index * sizeof(struct dirent) >= count ) break; } @@ -431,7 +451,7 @@ int dfs_elm_getdents(struct dfs_fd* file, struct _dirent* dirp, rt_uint32_t coun if (index == 0) return elm_result_to_dfs(result); - return index * sizeof(struct _dirent); + return index * sizeof(struct dirent); } int dfs_elm_unlink(struct dfs_filesystem* fs, const char* path) @@ -501,7 +521,7 @@ int dfs_elm_rename(struct dfs_filesystem* fs, const char* oldpath, const char* n return elm_result_to_dfs(result); } -int dfs_elm_stat(struct dfs_filesystem* fs, const char *path, struct _stat *st) +int dfs_elm_stat(struct dfs_filesystem* fs, const char *path, struct stat *st) { FILINFO file_info; FRESULT result; diff --git a/components/dfs/filesystems/nfs/dfs_nfs.c b/components/dfs/filesystems/nfs/dfs_nfs.c index 5824dfd806..a463b0dab4 100644 --- a/components/dfs/filesystems/nfs/dfs_nfs.c +++ b/components/dfs/filesystems/nfs/dfs_nfs.c @@ -722,7 +722,7 @@ int nfs_open(struct dfs_fd* file) return 0; } -int nfs_stat(struct dfs_filesystem* fs, const char *path, struct _stat *st) +int nfs_stat(struct dfs_filesystem* fs, const char *path, struct stat *st) { GETATTR3args args; GETATTR3res res; @@ -984,11 +984,11 @@ int nfs_rename(struct dfs_filesystem* fs, const char *src, const char *dest) return ret; } -int nfs_getdents(struct dfs_fd* file, struct _dirent* dirp, rt_uint32_t count) +int nfs_getdents(struct dfs_fd* file, struct dirent* dirp, rt_uint32_t count) { nfs_dir *dir; rt_uint32_t index; - struct _dirent* d; + struct dirent* d; struct nfs_filesystem* nfs; char *name; @@ -999,7 +999,7 @@ int nfs_getdents(struct dfs_fd* file, struct _dirent* dirp, rt_uint32_t count) nfs = (struct nfs_filesystem *)file->fs->data; /* make integer count */ - count = (count / sizeof(struct _dirent)) * sizeof(struct _dirent); + count = (count / sizeof(struct dirent)) * sizeof(struct dirent); if ( count == 0 ) return -DFS_STATUS_EINVAL; index = 0; @@ -1015,15 +1015,15 @@ int nfs_getdents(struct dfs_fd* file, struct _dirent* dirp, rt_uint32_t count) d->d_type &= DFS_DT_REG; d->d_namlen = rt_strlen(name); - d->d_reclen = (rt_uint16_t)sizeof(struct _dirent); + d->d_reclen = (rt_uint16_t)sizeof(struct dirent); rt_strncpy(d->d_name, name, rt_strlen(name) + 1); index ++; - if ( index * sizeof(struct _dirent) >= count ) + if ( index * sizeof(struct dirent) >= count ) break; } - return index * sizeof(struct _dirent); + return index * sizeof(struct dirent); } static const struct dfs_filesystem_operation _nfs = diff --git a/components/dfs/filesystems/nfs/rpc/types.h b/components/dfs/filesystems/nfs/rpc/types.h index 29f16cccb3..d16f8bca9f 100644 --- a/components/dfs/filesystems/nfs/rpc/types.h +++ b/components/dfs/filesystems/nfs/rpc/types.h @@ -43,7 +43,6 @@ typedef unsigned int u_int; typedef unsigned char u_char; typedef unsigned long u_long; -#endif typedef rt_int8_t int8_t; typedef rt_uint8_t uint8_t; @@ -51,6 +50,10 @@ typedef rt_int16_t int16_t; typedef rt_uint16_t uint16_t; typedef rt_int32_t int32_t; typedef rt_uint32_t uint32_t; +#else +#include +#include +#endif typedef long long int64_t; typedef unsigned long long uint64_t; @@ -60,7 +63,6 @@ typedef int enum_t; #ifndef RT_USING_NEWLIB typedef rt_int32_t ssize_t; -typedef unsigned long mode_t; typedef unsigned long dev_t; #endif diff --git a/components/dfs/filesystems/skeleton/skeleton.c b/components/dfs/filesystems/skeleton/skeleton.c new file mode 100644 index 0000000000..516879d59e --- /dev/null +++ b/components/dfs/filesystems/skeleton/skeleton.c @@ -0,0 +1,81 @@ +/* + * A skeleton of file system in Device File System + */ +#include +#include +#include + +#include "dfs_skt_fs.h" + +int dfs_skt_mount(struct dfs_filesystem* fs, unsigned long rwflag, const void* data) +{ + return DFS_STATUS_OK; +} + +int dfs_skt_unmount(struct dfs_filesystem* fs) +{ + return DFS_STATUS_OK; +} + +int dfs_skt_ioctl(struct dfs_fd* file, int cmd, void* args) +{ + return -DFS_STATUS_EIO; +} + +int dfs_skt_read(struct dfs_fd* file, void *buf, rt_size_t count) +{ + return count; +} + +int dfs_skt_lseek(struct dfs_fd* file, rt_off_t offset) +{ + return -DFS_STATUS_EIO; +} + +int dfs_skt_close(struct dfs_fd* file) +{ + return DFS_STATUS_OK; +} + +int dfs_skt_open(struct dfs_fd* file) +{ + return DFS_STATUS_OK; +} + +int dfs_skt_stat(struct dfs_filesystem* fs, const char *path, struct stat *st) +{ + return DFS_STATUS_OK; +} + +int dfs_skt_getdents(struct dfs_fd* file, struct dirent* dirp, rt_uint32_t count) +{ + return count * sizeof(struct dirent); +} + +static const struct dfs_filesystem_operation _skt_fs = +{ + "skt", + dfs_skt_mount, + dfs_skt_unmount, + RT_NULL, + RT_NULL, + + dfs_skt_open, + dfs_skt_close, + dfs_skt_ioctl, + dfs_skt_read, + RT_NULL, + RT_NULL, + dfs_skt_lseek, + dfs_skt_getdents, + RT_NULL, + dfs_skt_stat, + RT_NULL, +}; + +int dfs_skt_init(void) +{ + /* register rom file system */ + dfs_register(&_skt_fs); + return 0; +} diff --git a/components/dfs/filesystems/skeleton/skeleton.h b/components/dfs/filesystems/skeleton/skeleton.h new file mode 100644 index 0000000000..cacbf36b62 --- /dev/null +++ b/components/dfs/filesystems/skeleton/skeleton.h @@ -0,0 +1,8 @@ +#ifndef __SKELETON_H__ +#define __SKELETON_H__ + +#include + +int dfs_skt_init(void); + +#endif 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 + diff --git a/components/dfs/src/dfs_file.c b/components/dfs/src/dfs_file.c index 0d708d82fb..c326052090 100644 --- a/components/dfs/src/dfs_file.c +++ b/components/dfs/src/dfs_file.c @@ -176,7 +176,7 @@ int dfs_file_read(struct dfs_fd* fd, void *buf, rt_size_t len) * * @return the read dirent, others on failed. */ -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) { struct dfs_filesystem* fs; @@ -306,7 +306,7 @@ int dfs_file_lseek(struct dfs_fd* fd, rt_off_t offset) * * @return 0 on successful, -1 on failed. */ -int dfs_file_stat(const char *path, struct _stat *buf) +int dfs_file_stat(const char *path, struct stat *buf) { int result; char* fullpath; @@ -434,10 +434,10 @@ __exit: #include static struct dfs_fd fd; -static struct _dirent dirent; +static struct dirent dirent; void ls(const char* pathname) { - struct _stat stat; + struct stat stat; int length; char* fullpath; @@ -449,11 +449,11 @@ void ls(const char* pathname) rt_kprintf("Directory %s:\n", pathname); do { - rt_memset(&dirent, 0, sizeof(struct _dirent)); - length = dfs_file_getdents(&fd, &dirent, sizeof(struct _dirent)); + rt_memset(&dirent, 0, sizeof(struct dirent)); + length = dfs_file_getdents(&fd, &dirent, sizeof(struct dirent)); if ( length > 0 ) { - rt_memset(&stat, 0, sizeof(struct _stat)); + rt_memset(&stat, 0, sizeof(struct stat)); /* build full path for each file */ if (pathname[strlen(pathname) - 1] != '/') @@ -483,17 +483,6 @@ void ls(const char* pathname) } FINSH_FUNCTION_EXPORT(ls, list directory contents) -static void mkdir(const char* pathname) -{ - /* make a new directory */ - if (dfs_file_open(&fd, pathname, DFS_O_DIRECTORY | DFS_O_CREAT) == 0) - { - dfs_file_close(&fd); - } - else rt_kprintf("Can't mkdir %s\n", pathname); -} -FINSH_FUNCTION_EXPORT(mkdir, make a directory) - void rm(const char* filename) { if (dfs_file_unlink(filename) < 0) diff --git a/components/dfs/src/dfs_fs.c b/components/dfs/src/dfs_fs.c index 71ddb479ec..9db83a16c9 100644 --- a/components/dfs/src/dfs_fs.c +++ b/components/dfs/src/dfs_fs.c @@ -266,7 +266,7 @@ int dfs_mount(const char* device_name, const char* path, index++) ; if ( index == DFS_FILESYSTEMS_MAX ) /* can't find en empty filesystem table entry */ { - rt_set_errno(-DFS_STATUS_EMMOUNT); + rt_set_errno(-DFS_STATUS_ENOSPC); goto err1; } @@ -408,7 +408,7 @@ int dfs_mkfs(const char* fs_name, const char* device_name) * * @return 0 on successful, others on failed. */ -int dfs_statfs(const char* path, struct _statfs* buffer) +int dfs_statfs(const char* path, struct statfs* buffer) { struct dfs_filesystem* fs; @@ -432,7 +432,7 @@ FINSH_FUNCTION_EXPORT(mkfs, make a file system); void df(const char* path) { - struct _statfs buffer; + struct statfs buffer; if (dfs_statfs(path, &buffer) == 0) { diff --git a/components/dfs/src/dfs_posix.c b/components/dfs/src/dfs_posix.c index 415ae455b1..26395aafc6 100644 --- a/components/dfs/src/dfs_posix.c +++ b/components/dfs/src/dfs_posix.c @@ -95,7 +95,7 @@ int close(int fd) * * @return the actual read data buffer length */ -int read(int fd, char *buf, int len) +int read(int fd, void *buf, size_t len) { int result; struct dfs_fd* d; @@ -132,7 +132,7 @@ int read(int fd, char *buf, int len) * * @return the actual written data buffer length. */ -int write(int fd, char *buf, int len) +int write(int fd, const void *buf, size_t len) { int result; struct dfs_fd* d; @@ -169,7 +169,7 @@ int write(int fd, char *buf, int len) * * @return the current file position, or -1 on failed. */ -int lseek(int fd, int offset, int dir) +off_t lseek(int fd, off_t offset, int whence) { int result; struct dfs_fd* d; @@ -181,7 +181,7 @@ int lseek(int fd, int offset, int dir) return -1; } - switch (dir) + switch (whence) { case DFS_SEEK_SET: break; @@ -261,7 +261,7 @@ int unlink(const char *pathname) * * @return 0 on successful, -1 on failed. */ -int stat(const char *file, struct _stat *buf) +int stat(const char *file, struct stat *buf) { int result; @@ -283,7 +283,7 @@ int stat(const char *file, struct _stat *buf) * * @return 0 on successful, others on failed. */ -int statfs(const char *path, struct _statfs *buf) +int statfs(const char *path, struct statfs *buf) { int result; @@ -305,7 +305,7 @@ int statfs(const char *path, struct _statfs *buf) * * @return 0 on successful, others on failed. */ -int mkdir (const char *path, rt_uint16_t mode) +int mkdir (const char *path, mode_t mode) { int fd; struct dfs_fd* d; @@ -326,6 +326,10 @@ int mkdir (const char *path, rt_uint16_t mode) fd_put(d); return 0; } +#ifdef RT_USING_FINSH +#include +FINSH_FUNCTION_EXPORT(mkdir, create a directory); +#endif /** * this function is a POSIX compliant version, which will remove a directory. @@ -404,7 +408,7 @@ DIR* opendir(const char* name) * * @return the next directory entry, NULL on the end of directory or failed. */ -struct _dirent* readdir(DIR *d) +struct dirent* readdir(DIR *d) { int result; struct dfs_fd* fd; @@ -416,9 +420,9 @@ struct _dirent* readdir(DIR *d) return RT_NULL; } - if (!d->num || (d->cur += ((struct _dirent*)(d->buf + d->cur))->d_reclen) >= d->num) + if (!d->num || (d->cur += ((struct dirent*)(d->buf + d->cur))->d_reclen) >= d->num) { - result = dfs_file_getdents(fd, (struct _dirent*)d->buf, sizeof(d->buf) - 1); + result = dfs_file_getdents(fd, (struct dirent*)d->buf, sizeof(d->buf) - 1); if (result <= 0) { rt_set_errno(result); @@ -432,7 +436,7 @@ struct _dirent* readdir(DIR *d) } fd_put(fd); - return (struct _dirent*)(d->buf+d->cur); + return (struct dirent*)(d->buf+d->cur); } /** @@ -443,10 +447,10 @@ struct _dirent* readdir(DIR *d) * * @return the current location in directory stream. */ -rt_off_t telldir(DIR *d) +long telldir(DIR *d) { struct dfs_fd* fd; - rt_off_t result; + long result; fd = fd_get(d->fd); if (fd == RT_NULL) @@ -468,7 +472,7 @@ rt_off_t telldir(DIR *d) * @param d the directory stream. * @param offset the offset in directory stream. */ -void seekdir(DIR *d, rt_off_t offset) +void seekdir(DIR *d, off_t offset) { struct dfs_fd* fd; @@ -589,7 +593,7 @@ int chdir(const char *path) * * @return the returned current directory. */ -char* getcwd(char *buf, rt_size_t size) +char *getcwd(char *buf, size_t size) { #ifdef DFS_USING_WORKDIR dfs_lock(); diff --git a/components/libc/minilibc/rand.c b/components/libc/minilibc/rand.c index 46a96ac5bb..d97e91bdba 100644 --- a/components/libc/minilibc/rand.c +++ b/components/libc/minilibc/rand.c @@ -1,4 +1,5 @@ #include +#include #include static unsigned int seed=1; diff --git a/components/libc/minilibc/sys/types.h b/components/libc/minilibc/sys/types.h index 2be1f34403..fa0a236636 100644 --- a/components/libc/minilibc/sys/types.h +++ b/components/libc/minilibc/sys/types.h @@ -3,7 +3,7 @@ #include -typedef rt_off_t off_t; +typedef long off_t; typedef rt_size_t size_t; typedef rt_uint8_t u_char; @@ -11,14 +11,8 @@ typedef rt_uint16_t u_short; typedef rt_ubase_t u_int; typedef rt_uint32_t u_long; -typedef rt_uint8_t u_int8_t; -typedef rt_uint16_t u_int16_t; -typedef rt_uint32_t u_int32_t; -typedef rt_int8_t int8_t; -typedef rt_int16_t int16_t; -typedef rt_int32_t int32_t; - typedef rt_time_t time_t; +typedef int mode_t; #ifndef NULL #define NULL RT_NULL diff --git a/components/libc/newlib/sys/dirent.h b/components/libc/newlib/sys/dirent.h new file mode 100644 index 0000000000..18c5d02e11 --- /dev/null +++ b/components/libc/newlib/sys/dirent.h @@ -0,0 +1,57 @@ +#ifndef __RTT_DIRENT_H__ +#define __RTT_DIRENT_H__ + +#include + +/* +* dirent.h - format of directory entries + * Ref: http://www.opengroup.org/onlinepubs/009695399/basedefs/dirent.h.html + */ + +/* 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 */ + +#define DT_UNKNOWN 0x00 +#define DT_REG 0x01 +#define DT_DIR 0x02 + +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef HAVE_DIR_STRUCTURE +typedef struct +{ + int fd; /* directory file */ + char buf[512]; + int num; + int cur; +} DIR; +#endif + +#ifndef HAVE_DIRENT_STRUCTURE +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[256]; /* The null-terminated file name */ +}; +#endif + +int closedir(DIR *); +DIR *opendir(const char *); +struct dirent *readdir(DIR *); +int readdir_r(DIR *, struct dirent *, struct dirent **); +void rewinddir(DIR *); +void seekdir(DIR *, long int); +long telldir(DIR *); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/components/libc/newlib/sys/fcntl.h b/components/libc/newlib/sys/fcntl.h new file mode 100644 index 0000000000..3638029eb2 --- /dev/null +++ b/components/libc/newlib/sys/fcntl.h @@ -0,0 +1,15 @@ +#ifndef __RTT_FCNTL_H__ +#define __RTT_FCNTL_H__ + +/* Operation flags */ +#define O_RDONLY 0000000 +#define O_WRONLY 0000001 +#define O_RDWR 0000002 +#define O_ACCMODE 0000003 +#define O_CREAT 0000100 +#define O_EXCL 0000200 +#define O_TRUNC 0001000 +#define O_APPEND 0002000 +#define O_DIRECTORY 0200000 + +#endif diff --git a/components/libc/newlib/sys/statfs.h b/components/libc/newlib/sys/statfs.h new file mode 100644 index 0000000000..c3bc9be359 --- /dev/null +++ b/components/libc/newlib/sys/statfs.h @@ -0,0 +1,13 @@ +#ifndef __RTT_STATFS_H__ +#define __RTT_STATFS_H__ + +#include + +struct statfs +{ + rt_size_t f_bsize; /* block size */ + rt_size_t f_blocks; /* total data blocks in file system */ + rt_size_t f_bfree; /* free blocks in file system */ +}; + +#endif