From 2b28a1f3e5fd62a3c59989e21b8b5a950339ff5c Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Sun, 17 Jul 2016 15:59:45 -0600 Subject: [PATCH] SIM HOSTFS: First cut at changes to get it working on Cygwin. --- arch/sim/src/up_hostfs.c | 120 ++++++++++++++--------------- fs/hostfs/hostfs.c | 88 ++------------------- include/nuttx/fs/hostfs.h | 158 ++++++++++++++++++++++++++------------ 3 files changed, 169 insertions(+), 197 deletions(-) diff --git a/arch/sim/src/up_hostfs.c b/arch/sim/src/up_hostfs.c index f0928ed0da..9a4a0ed433 100644 --- a/arch/sim/src/up_hostfs.c +++ b/arch/sim/src/up_hostfs.c @@ -36,6 +36,7 @@ /**************************************************************************** * Included Files ****************************************************************************/ + #define _BSD_SOURCE #include @@ -50,6 +51,7 @@ #include #include +#define __SIM__ 1 #include "hostfs.h" /**************************************************************************** @@ -62,40 +64,44 @@ int host_open(const char *pathname, int flags, int mode) /* Perform flag mapping */ - if ((flags & (HOSTFS_FLAG_RDOK | HOSTFS_FLAG_WROK)) == - (HOSTFS_FLAG_RDOK | HOSTFS_FLAG_WROK)) + if ((flags & NUTTX_O_RDWR) == NUTTX_O_RDWR) { mapflags = O_RDWR; } - else if (flags & HOSTFS_FLAG_RDOK) + else if (flags & NUTTX_O_RDONLY) { mapflags = O_RDONLY; } - else if (flags & HOSTFS_FLAG_WROK) + else if (flags & NUTTX_O_WRONLY) { mapflags = O_WRONLY; } - if (flags & HOSTFS_FLAG_APPEND) + if (flags & NUTTX_O_APPEND) { mapflags |= O_APPEND; } - if (flags & HOSTFS_FLAG_CREAT) + if (flags & NUTTX_O_CREAT) { mapflags |= O_CREAT; } - if (flags & HOSTFS_FLAG_EXCL) + if (flags & NUTTX_O_EXCL) { mapflags |= O_EXCL; } - if (flags & HOSTFS_FLAG_TRUNC) + if (flags & NUTTX_O_TRUNC) { mapflags |= O_TRUNC; } + if (flags & NUTTX_O_NONBLOCK) + { + mapflags |= O_NONBLOCK; + } + return open(pathname, mapflags, mode); } @@ -180,14 +186,14 @@ int host_dup(int fd) void *host_opendir(const char *name) { - return (void *) opendir(name); + return (void *)opendir(name); } /**************************************************************************** * Public Functions ****************************************************************************/ -int host_readdir(void* dirp, struct host_dirent_s* entry) +int host_readdir(void* dirp, struct nuttx_dirent_s* entry) { struct dirent *ent; @@ -206,19 +212,19 @@ int host_readdir(void* dirp, struct host_dirent_s* entry) entry->d_type = 0; if (ent->d_type == DT_REG) { - entry->d_type = HOSTFS_DTYPE_FILE; + entry->d_type = NUTTX_DTYPE_FILE; } else if (ent->d_type == DT_CHR) { - entry->d_type = HOSTFS_DTYPE_CHR; + entry->d_type = NUTTX_DTYPE_CHR; } else if (ent->d_type == DT_BLK) { - entry->d_type = HOSTFS_DTYPE_BLK; + entry->d_type = NUTTX_DTYPE_BLK; } else if (ent->d_type == DT_DIR) { - entry->d_type = HOSTFS_DTYPE_DIRECTORY; + entry->d_type = NUTTX_DTYPE_DIRECTORY; } } @@ -254,7 +260,7 @@ int host_closedir(void* dirp) * Public Functions ****************************************************************************/ -int host_statfs(const char *path, struct host_statfs_s *buf) +int host_statfs(const char *path, struct nuttx_statfs_s *buf) { int ret; struct statfs host_buf; @@ -263,18 +269,16 @@ int host_statfs(const char *path, struct host_statfs_s *buf) ret = statfs(path, &host_buf); - /* Map the return values */ + /* Map the struct statfs value */ buf->f_type = host_buf.f_type; + buf->f_namelen = host_buf.f_namelen; buf->f_bsize = host_buf.f_bsize; buf->f_blocks = host_buf.f_blocks; buf->f_bfree = host_buf.f_bfree; buf->f_bavail = host_buf.f_bavail; buf->f_files = host_buf.f_files; buf->f_ffree = host_buf.f_ffree; - buf->f_fsid = 0; - buf->f_namelen = host_buf.f_namelen; - buf->f_frsize = host_buf.f_frsize; return ret; } @@ -321,62 +325,50 @@ int host_rename(const char *oldpath, const char *newpath) * Public Functions ****************************************************************************/ -int host_stat(const char *path, struct host_stat_s *buf) +int host_stat(const char *path, struct nuttx_stat_s *buf) { - struct stat host_buf; - int ret; + struct stat host_buf; + int ret; /* Call the host's stat routine */ ret = stat(path, &host_buf); - /* Now map the return values to the common struct */ + /* Map the return values */ - buf->st_dev = host_buf.st_dev; /* ID of the device containing file */ - buf->st_ino = host_buf.st_ino;; /* inode number */ - buf->st_nlink = host_buf.st_nlink; /* number of hard links */ - buf->st_uid = host_buf.st_uid; /* user ID of owner */ - buf->st_gid = host_buf.st_gid; /* group ID of owner */ - buf->st_rdev = host_buf.st_rdev; /* device ID */ - buf->st_size = host_buf.st_size; /* total size, in bytes */ - buf->st_blksize = host_buf.st_blksize; /* blocksize for file system I/O */ - buf->st_blocks = host_buf.st_blocks; /* number of 512B blocks allocated */ - buf->st_atim = host_buf.st_atime; /* time of last access */ - buf->st_mtim = host_buf.st_mtime; /* time of last modification */ - buf->st_ctim = host_buf.st_ctime; /* time of last status change */ + buf->st_mode = host_buf.st_mode & 0777; - /* Map the mode bits */ - - buf->st_mode = host_buf.st_mode & 0xFFF; - if (S_ISREG(host_buf.st_mode)) + if (host_buf.st_mode & S_IFDIR) { - buf->st_mode |= HOST_ST_MODE_REG; + buf->st_mode |= NUTTX_S_IFDIR; + } + else if (host_buf.st_mode & S_IFREG) + { + buf->st_mode |= NUTTX_S_IFREG; + } + else if (host_buf.st_mode & S_IFCHR) + { + buf->st_mode |= NUTTX_S_IFCHR; + } + else if (host_buf.st_mode & S_IFBLK) + { + buf->st_mode |= NUTTX_S_IFBLK; + } + else if (host_buf.st_mode & S_IFLNK) + { + buf->st_mode |= NUTTX_S_IFLNK; + } + else /* if (host_buf.st_mode & S_IFIFO) */ + { + buf->st_mode |= NUTTX_S_IFIFO; } - if (S_ISDIR(host_buf.st_mode)) - { - buf->st_mode |= HOST_ST_MODE_DIR; - } - - if (S_ISCHR(host_buf.st_mode)) - { - buf->st_mode |= HOST_ST_MODE_CHR; - } - - if (S_ISBLK(host_buf.st_mode)) - { - buf->st_mode |= HOST_ST_MODE_BLK; - } - - if (S_ISFIFO(host_buf.st_mode)) - { - buf->st_mode |= HOST_ST_MODE_PIPE; - } - - if (S_ISLNK(host_buf.st_mode)) - { - buf->st_mode |= HOST_ST_MODE_LINK; - } + buf->st_size = host_buf.st_size; + buf->st_blksize = host_buf.st_blksize; + buf->st_blocks = host_buf.st_blocks; + buf->st_atim = host_buf.st_atim.tv_sec; + buf->st_mtim = host_buf.st_mtim.tv_sec; + buf->st_ctim = host_buf.st_ctim.tv_sec; return ret; } diff --git a/fs/hostfs/hostfs.c b/fs/hostfs/hostfs.c index e4a09658ac..afd3f9f125 100644 --- a/fs/hostfs/hostfs.c +++ b/fs/hostfs/hostfs.c @@ -677,7 +677,6 @@ static int hostfs_opendir(FAR struct inode *mountpt, FAR const char *relpath, /* Call the host's opendir function */ dir->u.hostfs.fs_dir = host_opendir(path); - if (dir->u.hostfs.fs_dir == NULL) { ret = -ENOENT; @@ -735,7 +734,7 @@ static int hostfs_readdir(FAR struct inode *mountpt, FAR struct fs_dirent_s *dir) { FAR struct hostfs_mountpt_s *fs; - FAR struct host_dirent_s entry; + struct dirent entry; int ret; /* Sanity checks */ @@ -754,22 +753,6 @@ static int hostfs_readdir(FAR struct inode *mountpt, ret = host_readdir(dir->u.hostfs.fs_dir, &entry); - /* Save the entry name when successful */ - - if (ret == OK) - { - /* Copy the entry name */ - - memset(dir->fd_dir.d_name, 0, sizeof(dir->fd_dir.d_name)); - strncpy(dir->fd_dir.d_name, entry.d_name, sizeof(dir->fd_dir.d_name)); - - /* Copy the entry type */ - - /* TODO: May need to do some type mapping */ - - dir->fd_dir.d_type = entry.d_type; - } - hostfs_semgive(fs); return ret; } @@ -810,7 +793,7 @@ static int hostfs_bind(FAR struct inode *blkdriver, FAR const void *data, FAR void **handle) { FAR struct hostfs_mountpt_s *fs; - struct host_stat_s buf; + struct stat buf; FAR const char *options; int len; int ret; @@ -879,7 +862,7 @@ static int hostfs_bind(FAR struct inode *blkdriver, FAR const void *data, /* Try to stat the file in the host FS */ ret = host_stat(fs->fs_root, &buf); - if ((ret != 0) || ((buf.st_mode & HOST_ST_MODE_DIR) == 0)) + if (ret != 0 || (buf.st_mode & S_IFDIR) == 0) { hostfs_semgive(fs); kmm_free(fs); @@ -946,7 +929,6 @@ static int hostfs_unbind(FAR void *handle, FAR struct inode **blkdriver, static int hostfs_statfs(FAR struct inode *mountpt, FAR struct statfs *buf) { FAR struct hostfs_mountpt_s *fs; - struct host_statfs_s host_buf; int ret; /* Sanity checks */ @@ -966,15 +948,7 @@ static int hostfs_statfs(FAR struct inode *mountpt, FAR struct statfs *buf) /* Call the host fs to perform the statfs */ - ret = host_statfs(fs->fs_root, &host_buf); - - buf->f_namelen = host_buf.f_namelen; - buf->f_bsize = host_buf.f_bsize; - buf->f_blocks = host_buf.f_blocks; - buf->f_bfree = host_buf.f_bfree; - buf->f_bavail = host_buf.f_bavail; - buf->f_files = host_buf.f_files; - buf->f_ffree = host_buf.f_ffree; + ret = host_statfs(fs->fs_root, buf); hostfs_semgive(fs); return ret; @@ -1139,7 +1113,6 @@ static int hostfs_stat(FAR struct inode *mountpt, FAR const char *relpath, FAR struct stat *buf) { FAR struct hostfs_mountpt_s *fs; - struct host_stat_s host_buf; char path[HOSTFS_MAX_PATH]; int ret; @@ -1157,59 +1130,10 @@ static int hostfs_stat(FAR struct inode *mountpt, FAR const char *relpath, hostfs_mkpath(fs, relpath, path, sizeof(path)); - /* Call the host FS to do the mkdir */ + /* Call the host FS to do the stat operation */ - ret = host_stat(path, &host_buf); - if (ret != 0) - { - goto errout_with_semaphore; - } + ret = host_stat(path, buf); - /* Initialize the stat structure */ - - memset(buf, 0, sizeof(struct stat)); - - buf->st_mode = host_buf.st_mode & 0xFFF; - - if (host_buf.st_mode & HOST_ST_MODE_DIR) - { - buf->st_mode |= S_IFDIR; - } - - if (host_buf.st_mode & HOST_ST_MODE_REG) - { - buf->st_mode |= S_IFREG; - } - - if (host_buf.st_mode & HOST_ST_MODE_CHR) - { - buf->st_mode |= S_IFCHR; - } - - if (host_buf.st_mode & HOST_ST_MODE_BLK) - { - buf->st_mode |= S_IFBLK; - } - - if (host_buf.st_mode & HOST_ST_MODE_LINK) - { - buf->st_mode |= S_IFLNK; - } - - if (host_buf.st_mode & HOST_ST_MODE_PIPE) - { - buf->st_mode |= S_IFIFO; - } - - buf->st_size = host_buf.st_size; - buf->st_blksize = host_buf.st_blksize; - buf->st_blocks = host_buf.st_blocks; - buf->st_atime = host_buf.st_atim; - buf->st_ctime = host_buf.st_ctim; - - ret = OK; - -errout_with_semaphore: hostfs_semgive(fs); return ret; } diff --git a/include/nuttx/fs/hostfs.h b/include/nuttx/fs/hostfs.h index 0a57797e57..e82d0a3af5 100644 --- a/include/nuttx/fs/hostfs.h +++ b/include/nuttx/fs/hostfs.h @@ -40,94 +40,150 @@ * Included Files ****************************************************************************/ +#ifndef __SIM__ +# include +# include +# include +# include +# include +#endif + /**************************************************************************** * Pre-processor Definitions ****************************************************************************/ -#define HOST_ST_MODE_REG 0x01000 -#define HOST_ST_MODE_DIR 0x02000 -#define HOST_ST_MODE_CHR 0x04000 -#define HOST_ST_MODE_BLK 0x08000 -#define HOST_ST_MODE_PIPE 0x10000 -#define HOST_ST_MODE_LINK 0x20000 +#ifdef __SIM__ -#define HOSTFS_FLAG_RDOK 0x0001 -#define HOSTFS_FLAG_WROK 0x0002 -#define HOSTFS_FLAG_CREAT 0x0004 -#define HOSTFS_FLAG_EXCL 0x0008 -#define HOSTFS_FLAG_APPEND 0x0010 -#define HOSTFS_FLAG_TRUNC 0x0020 +/* These must exactly match the definitions from include/dirent.h: */ -#define HOSTFS_DTYPE_FILE 0x0001 -#define HOSTFS_DTYPE_CHR 0x0002 -#define HOSTFS_DTYPE_BLK 0x0004 -#define HOSTFS_DTYPE_DIRECTORY 0x0008 +#define NUTTX_DTYPE_FILE 0x01 +#define NUTTX_DTYPE_CHR 0x02 +#define NUTTX_DTYPE_BLK 0x04 +#define NUTTX_DTYPE_DIRECTORY 0x08 + +/* These must exactly match the definitions from include/sys/stat.h: */ + +#define NUTTX_S_IFIFO 0010000 +#define NUTTX_S_IFCHR 0020000 +#define NUTTX_S_IFDIR 0040000 +#define NUTTX_S_IFBLK 0060000 +#define NUTTX_S_IFREG 0100000 +#define NUTTX_S_IFLNK 0120000 + +/* These must exactly match the definitions from include/fctnl.h: */ + +#define NUTTX_O_RDONLY (1 << 0) /* Open for read access (only) */ +#define NUTTX_O_WRONLY (1 << 1) /* Open for write access (only) */ +#define NUTTX_O_CREAT (1 << 2) /* Create file/sem/mq object */ +#define NUTTX_O_EXCL (1 << 3) /* Name must not exist when opened */ +#define NUTTX_O_APPEND (1 << 4) /* Keep contents, append to end */ +#define NUTTX_O_TRUNC (1 << 5) /* Delete contents */ +#define NUTTX_O_NONBLOCK (1 << 6) /* Don't wait for data */ +#define NUTTX_O_SYNC (1 << 7) /* Synchronize output on write */ +#define NUTTX_O_BINARY (1 << 8) /* Open the file in binary mode. */ + +#define NUTTX_O_RDWR (NUTTX_O_RDONLY | NUTTX_O_WRONLY) + +#endif /* __SIM__ */ /**************************************************************************** * Public Type Definitions ****************************************************************************/ -struct host_dirent_s +#ifdef __SIM__ + +/* These must match the definitions in include/sys/types.h */ + +typedef uintptr_t nuttx_size_t; +typedef int32_t nuttx_off_t; +typedef unsigned int nuttx_mode_t; +typedef int16_t nuttx_blksize_t; +typedef uint32_t nuttx_blkcnt_t; + +/* These must match the definition in include/time.h */ + +typedef uint32_t nuttx_time_t; + +/* These must exactly match the definition from include/dirent.h: */ + +struct nuttx_dirent_s { - size_t d_ino; - size_t d_off; - unsigned short d_reclen; - unsigned char d_type; - char d_name[256]; + uint8_t d_type; /* type of file */ + char d_name[NAME_MAX+1]; /* filename */ }; -struct host_statfs_s +/* These must exactly match the definition from include/sys/statfs.h: */ + +struct nuttx_statfs_s { - size_t f_type; /* Type of file system */ - size_t f_bsize; /* Optimal transfer block size */ - size_t f_blocks; /* Total data blocks in the file system */ - size_t f_bfree; /* Free blocks */ - size_t f_bavail; /* Free blocks available */ - size_t f_files; /* Total file nodes in file system */ - size_t f_ffree; /* Free file nodes in fs */ - size_t f_fsid; /* File Systme ID */ - size_t f_namelen; /* Max length of filenames */ - size_t f_frsize; /* Fragment size */ + uint32_t f_type; /* Type of filesystem */ + nuttx_size_t f_namelen; /* Maximum length of filenames */ + nuttx_size_t f_bsize; /* Optimal block size for transfers */ + nuttx_off_t f_blocks; /* Total data blocks in the file system of this size */ + nuttx_off_t f_bfree; /* Free blocks in the file system */ + nuttx_off_t f_bavail; /* Free blocks avail to non-superuser */ + nuttx_off_t f_files; /* Total file nodes in the file system */ + nuttx_off_t f_ffree; /* Free file nodes in the file system */ }; -struct host_stat_s +/* These must exactly match the definition from include/sys/stat.h: */ + +struct nuttx_stat_s { - int st_dev; /* ID of the device containing file */ - size_t st_ino; /* inode number */ - size_t st_mode; /* protection */ - size_t st_nlink; /* number of hard links */ - size_t st_uid; /* user ID of owner */ - size_t st_gid; /* group ID of owner */ - size_t st_rdev; /* device ID */ - size_t st_size; /* total size, in bytes */ - size_t st_blksize; /* blocksize for file system I/O */ - size_t st_blocks; /* number of 512B blocks allocated */ - size_t st_atim; /* time of last access */ - size_t st_mtim; /* time of last modification */ - size_t st_ctim; /* time of last status change */ + nuttx_mode_t st_mode; /* File type, atributes, and access mode bits */ + nuttx_off_t st_size; /* Size of file/directory, in bytes */ + nuttx_blksize_t st_blksize; /* Blocksize used for filesystem I/O */ + nuttx_blkcnt_t st_blocks; /* Number of blocks allocated */ + nuttx_time_t st_atim; /* Time of last access */ + nuttx_time_t st_mtim; /* Time of last modification */ + nuttx_time_t st_ctim; /* Time of last status change */ }; +#endif /* __SIM__ */ + /**************************************************************************** * Public Function Prototypes ****************************************************************************/ +#ifdef __SIM__ int host_open(const char *pathname, int flags, int mode); int host_close(int fd); -ssize_t host_read(int fd, void* buf, size_t count); +ssize_t host_read(int fd, void *buf, nuttx_size_t count); +ssize_t host_write(int fd, const void *buf, nuttx_size_t count); +off_t host_lseek(int fd, off_t offset, int whence); +int host_ioctl(int fd, int request, unsigned long arg); +void host_sync(int fd); +int host_dup(int fd); +void *host_opendir(const char *name); +int host_readdir(void* dirp, struct nuttx_dirent_s* entry); +void host_rewinddir(void* dirp); +int host_closedir(void* dirp); +int host_statfs(const char *path, struct nuttx_statfs_s *buf); +int host_unlink(const char *pathname); +int host_mkdir(const char *pathname, mode_t mode); +int host_rmdir(const char *pathname); +int host_rename(const char *oldpath, const char *newpath); +int host_stat(const char *path, struct nuttx_stat_s *buf); +#else +int host_open(const char *pathname, int flags, int mode); +int host_close(int fd); +ssize_t host_read(int fd, void *buf, size_t count); ssize_t host_write(int fd, const void *buf, size_t count); off_t host_lseek(int fd, off_t offset, int whence); int host_ioctl(int fd, int request, unsigned long arg); void host_sync(int fd); int host_dup(int fd); void *host_opendir(const char *name); -int host_readdir(void* dirp, struct host_dirent_s* entry); +int host_readdir(void* dirp, struct dirent *entry); void host_rewinddir(void* dirp); int host_closedir(void* dirp); -int host_statfs(const char *path, struct host_statfs_s *buf); +int host_statfs(const char *path, struct statfs *buf); int host_unlink(const char *pathname); int host_mkdir(const char *pathname, mode_t mode); int host_rmdir(const char *pathname); int host_rename(const char *oldpath, const char *newpath); -int host_stat(const char *path, struct host_stat_s *buf); +int host_stat(const char *path, struct stat *buf); + +#endif /* __SIM__ */ #endif /* __INCLUDE_NUTTX_FS_HOSTFS_H */