[dfs] add poll/select

1. Add poll/select APIs;
2. Add wait queue implementation;
3. Remove DFS_STATUS_* code and use errno;
4. Add pipe API;
5. Use a standalone fops in DFS;
This commit is contained in:
bernard
2017-10-15 22:44:53 +08:00
parent 5c7b16d00b
commit 0f5a68a55e
29 changed files with 1799 additions and 1552 deletions

View File

@@ -20,81 +20,21 @@
* Change Logs:
* Date Author Notes
* 2009-05-27 Yi.qiu The first version.
* 2010-07-18 Bernard add stat and statfs structure definitions.
* 2010-07-18 Bernard add stat and statfs structure definitions.
* 2011-05-16 Yi.qiu Change parameter name of rename, "new" is C++ key word.
*/
#ifndef __DFS_POSIX_H__
#define __DFS_POSIX_H__
#include <dfs_file.h>
#include <dfs_def.h>
#include <sys/time.h> /* for struct timeval */
#ifdef __cplusplus
extern "C" {
#endif
#if !defined(RT_USING_NEWLIB)
#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_BINARY DFS_O_BINARY
#define O_DIRECTORY DFS_O_DIRECTORY
#if !defined(_WIN32)
#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
#endif
#if defined(__CC_ARM)
#include <stdio.h>
#include <stdlib.h>
#elif defined(_MSC_VER)
#include <stdio.h>
#else
#define SEEK_SET DFS_SEEK_SET
#define SEEK_CUR DFS_SEEK_CUR
#define SEEK_END DFS_SEEK_END
#endif
typedef struct
typedef struct
{
int fd; /* directory file */
char buf[512];
@@ -111,13 +51,6 @@ void seekdir(DIR *d, off_t offset);
void rewinddir(DIR *d);
int closedir(DIR* d);
#else
/* use newlib header file */
#include <sys/stat.h>
#endif
struct stat;
/* file api*/
int open(const char *file, int flags, int mode);
int close(int d);
@@ -134,7 +67,7 @@ int unlink(const char *pathname);
int stat(const char *file, struct stat *buf);
int fstat(int fildes, struct stat *buf);
int fsync(int fildes);
int ioctl(int fildes, long cmd, void *data);
int ioctl(int fildes, int cmd, void *data);
/* directory api*/
int rmdir(const char *path);
@@ -144,6 +77,100 @@ char *getcwd(char *buf, size_t size);
/* file system api */
int statfs(const char *path, struct statfs *buf);
int access(const char *path, int amode);
int pipe(int fildes[2]);
int mkfifo(const char *path, mode_t mode);
/* poll and select */
#define POLLIN (0x01)
#define POLLRDNORM (0x01)
#define POLLRDBAND (0x01)
#define POLLPRI (0x01)
#define POLLOUT (0x02)
#define POLLWRNORM (0x02)
#define POLLWRBAND (0x02)
#define POLLERR (0x04)
#define POLLHUP (0x08)
#define POLLNVAL (0x10)
#define POLLMASK_DEFAULT (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM)
typedef unsigned int nfds_t;
struct pollfd
{
int fd;
short events;
short revents;
};
int poll(struct pollfd *fds, nfds_t nfds, int timeout);
#ifdef RT_USING_LWIP
/* we use lwIP's structure definitions. */
#include <lwip/sockets.h>
#else
#ifndef FD_SET
/* Get the total number of descriptors that we will have to support */
#define FD_SETSIZE (12)
/* We will use a 32-bit bitsets to represent the set of descriptors. How
* many uint32_t's do we need to span all descriptors?
*/
#if FD_SETSIZE <= 32
# define __SELECT_NUINT32 1
#elif FD_SETSIZE <= 64
# define __SELECT_NUINT32 2
#elif FD_SETSIZE <= 96
# define __SELECT_NUINT32 3
#elif FD_SETSIZE <= 128
# define __SELECT_NUINT32 4
#elif FD_SETSIZE <= 160
# define __SELECT_NUINT32 5
#elif FD_SETSIZE <= 192
# define __SELECT_NUINT32 6
#elif FD_SETSIZE <= 224
# define __SELECT_NUINT32 7
#elif FD_SETSIZE <= 256
# define __SELECT_NUINT32 8
#else
# warning "Larger fd_set needed"
#endif
/* These macros map a file descriptor to an index and bit number */
#define _FD_NDX(fd) ((fd) >> 5)
#define _FD_BIT(fd) ((fd) & 0x1f)
/* Standard helper macros */
#define FD_CLR(fd,set) \
((((fd_set*)(set))->arr)[_FD_NDX(fd)] &= ~(1 << _FD_BIT(fd)))
#define FD_SET(fd,set) \
((((fd_set*)(set))->arr)[_FD_NDX(fd)] |= (1 << _FD_BIT(fd)))
#define FD_ISSET(fd,set) \
(((((fd_set*)(set))->arr)[_FD_NDX(fd)] & (1 << _FD_BIT(fd))) != 0)
#define FD_ZERO(set) \
memset((set), 0, sizeof(fd_set))
typedef struct
{
uint32_t arr[__SELECT_NUINT32];
}fd_set;
#endif
#endif
int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout);
#ifdef __cplusplus
}
#endif