diff options
| author | Bernard Xiong <[email protected]> | 2021-11-24 17:37:03 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2021-11-24 17:37:03 +0800 |
| commit | 64023c38f7aa23f7c53e3c117ee19362aad3f33a (patch) | |
| tree | b14db8ab9fd683e397ec169109a4ceb9bdfa39ce /components | |
| parent | 68f33ba783c49c960747b6bdf881d8834fc7799f (diff) | |
| parent | 973f25901c641f01b19b065a1e263533fc4d8755 (diff) | |
Merge pull request #5283 from mysterywolf/dfs
[dfs] move dfs back
Diffstat (limited to 'components')
| -rw-r--r-- | components/dfs/Kconfig | 4 | ||||
| -rw-r--r-- | components/dfs/SConscript | 3 | ||||
| -rw-r--r-- | components/dfs/include/dfs.h | 8 | ||||
| -rw-r--r-- | components/dfs/src/dfs.c | 4 | ||||
| -rw-r--r-- | components/dfs/src/dfs_file.c | 4 | ||||
| -rw-r--r-- | components/dfs/src/dfs_posix.c | 937 | ||||
| -rw-r--r-- | components/finsh/SConscript | 2 | ||||
| -rw-r--r-- | components/finsh/msh.c | 36 | ||||
| -rw-r--r-- | components/finsh/msh_file.c | 5 | ||||
| -rw-r--r-- | components/finsh/shell.c | 6 | ||||
| -rw-r--r-- | components/libc/compilers/armlibc/syscalls.c | 38 | ||||
| -rw-r--r-- | components/libc/compilers/dlib/SConscript | 2 | ||||
| -rw-r--r-- | components/libc/compilers/dlib/syscall_close.c | 4 | ||||
| -rw-r--r-- | components/libc/compilers/dlib/syscall_lseek.c | 4 | ||||
| -rw-r--r-- | components/libc/compilers/dlib/syscall_open.c | 4 | ||||
| -rw-r--r-- | components/libc/compilers/dlib/syscall_read.c | 4 | ||||
| -rw-r--r-- | components/libc/compilers/dlib/syscall_remove.c | 4 | ||||
| -rw-r--r-- | components/libc/compilers/dlib/syscall_write.c | 8 | ||||
| -rw-r--r-- | components/libc/compilers/gcc/newlib/syscalls.c | 40 | ||||
| -rw-r--r-- | components/libc/posix/src/unistd.c | 927 |
20 files changed, 1032 insertions, 1012 deletions
diff --git a/components/dfs/Kconfig b/components/dfs/Kconfig index 1ec36784d7..b93c65512b 100644 --- a/components/dfs/Kconfig +++ b/components/dfs/Kconfig @@ -8,6 +8,10 @@ config RT_USING_DFS The device file system is a light weight virtual file system. if RT_USING_DFS + config DFS_USING_POSIX + bool "Using posix-like functions, open/read/write/close" + default y + config DFS_USING_WORKDIR bool "Using working directory" default y diff --git a/components/dfs/SConscript b/components/dfs/SConscript index 5a0b394ce2..790fbd3efe 100644 --- a/components/dfs/SConscript +++ b/components/dfs/SConscript @@ -9,6 +9,9 @@ src/dfs_fs.c cwd = GetCurrentDir() CPPPATH = [cwd + "/include"] +if GetDepend('DFS_USING_POSIX'): + src += ['src/dfs_posix.c'] + group = DefineGroup('Filesystem', src, depend = ['RT_USING_DFS'], CPPPATH = CPPPATH) if GetDepend('RT_USING_DFS'): diff --git a/components/dfs/include/dfs.h b/components/dfs/include/dfs.h index 38f19aa7e7..678ff0e377 100644 --- a/components/dfs/include/dfs.h +++ b/components/dfs/include/dfs.h @@ -81,16 +81,18 @@ int dfs_init(void); char *dfs_normalize_path(const char *directory, const char *filename); const char *dfs_subdir(const char *directory, const char *filename); +int fd_is_open(const char *pathname); +struct dfs_fdtable *dfs_fdtable_get(void); + void dfs_lock(void); void dfs_unlock(void); +#ifdef DFS_USING_POSIX /* FD APIs */ int fd_new(void); struct dfs_fd *fd_get(int fd); void fd_put(struct dfs_fd *fd); -int fd_is_open(const char *pathname); - -struct dfs_fdtable *dfs_fdtable_get(void); +#endif /* DFS_USING_POSIX */ #ifdef __cplusplus } diff --git a/components/dfs/src/dfs.c b/components/dfs/src/dfs.c index 51ae53de17..d3a859e287 100644 --- a/components/dfs/src/dfs.c +++ b/components/dfs/src/dfs.c @@ -34,7 +34,6 @@ char working_directory[DFS_PATH_MAX] = {"/"}; #endif static struct dfs_fdtable _fdtab; -static int fd_alloc(struct dfs_fdtable *fdt, int startfd); /** * @addtogroup DFS @@ -118,6 +117,7 @@ void dfs_unlock(void) rt_mutex_release(&fslock); } +#ifdef DFS_USING_POSIX static int fd_alloc(struct dfs_fdtable *fdt, int startfd) { int idx; @@ -276,6 +276,8 @@ void fd_put(struct dfs_fd *fd) dfs_unlock(); } +#endif /* DFS_USING_POSIX */ + /** * @ingroup Fd * diff --git a/components/dfs/src/dfs_file.c b/components/dfs/src/dfs_file.c index 978c1056c2..711a461e20 100644 --- a/components/dfs/src/dfs_file.c +++ b/components/dfs/src/dfs_file.c @@ -621,7 +621,7 @@ void cat(const char *filename) } FINSH_FUNCTION_EXPORT(cat, print file); -#ifdef RT_USING_POSIX +#ifdef DFS_USING_POSIX #define BUF_SZ 4096 static void copyfile(const char *src, const char *dst) { @@ -843,7 +843,7 @@ void copy(const char *src, const char *dst) } } FINSH_FUNCTION_EXPORT(copy, copy file or dir) -#endif /* RT_USING_POSIX */ +#endif /* DFS_USING_POSIX */ #endif /* RT_USING_FINSH */ /* @} */ diff --git a/components/dfs/src/dfs_posix.c b/components/dfs/src/dfs_posix.c new file mode 100644 index 0000000000..e6336c2544 --- /dev/null +++ b/components/dfs/src/dfs_posix.c @@ -0,0 +1,937 @@ +/* + * Copyright (c) 2006-2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2009-05-27 Yi.qiu The first version + * 2018-02-07 Bernard Change the 3rd parameter of open/fcntl/ioctl to '...' + */ + +#include <dfs_file.h> +#include <dfs_private.h> +#include <sys/errno.h> + +/** + * this function is a POSIX compliant version, which will open a file and + * return a file descriptor according specified flags. + * + * @param file the path name of file. + * @param flags the file open flags. + * + * @return the non-negative integer on successful open, others for failed. + */ +int open(const char *file, int flags, ...) +{ + int fd, result; + struct dfs_fd *d; + + /* allocate a fd */ + fd = fd_new(); + if (fd < 0) + { + rt_set_errno(-ENOMEM); + + return -1; + } + d = fd_get(fd); + + result = dfs_file_open(d, file, flags); + if (result < 0) + { + /* release the ref-count of fd */ + fd_put(d); + fd_put(d); + + rt_set_errno(result); + + return -1; + } + + /* release the ref-count of fd */ + fd_put(d); + + return fd; +} +RTM_EXPORT(open); + +/** + * this function is a POSIX compliant version, which will close the open + * file descriptor. + * + * @param fd the file descriptor. + * + * @return 0 on successful, -1 on failed. + */ +int close(int fd) +{ + int result; + struct dfs_fd *d; + + d = fd_get(fd); + if (d == NULL) + { + rt_set_errno(-EBADF); + + return -1; + } + + result = dfs_file_close(d); + fd_put(d); + + if (result < 0) + { + rt_set_errno(result); + + return -1; + } + + fd_put(d); + + return 0; +} +RTM_EXPORT(close); + +/** + * this function is a POSIX compliant version, which will read specified data + * buffer length for an open file descriptor. + * + * @param fd the file descriptor. + * @param buf the buffer to save the read data. + * @param len the maximal length of data buffer + * + * @return the actual read data buffer length. If the returned value is 0, it + * may be reach the end of file, please check errno. + */ +#ifdef _READ_WRITE_RETURN_TYPE +_READ_WRITE_RETURN_TYPE read(int fd, void *buf, size_t len) /* some gcc tool chains will use different data structure */ +#else +ssize_t read(int fd, void *buf, size_t len) +#endif +{ + int result; + struct dfs_fd *d; + + /* get the fd */ + d = fd_get(fd); + if (d == NULL) + { + rt_set_errno(-EBADF); + + return -1; + } + + result = dfs_file_read(d, buf, len); + if (result < 0) + { + fd_put(d); + rt_set_errno(result); + + return -1; + } + + /* release the ref-count of fd */ + fd_put(d); + + return result; +} +RTM_EXPORT(read); + +/** + * this function is a POSIX compliant version, which will write specified data + * buffer length for an open file descriptor. + * + * @param fd the file descriptor + * @param buf the data buffer to be written. + * @param len the data buffer length. + * + * @return the actual written data buffer length. + */ +#ifdef _READ_WRITE_RETURN_TYPE +_READ_WRITE_RETURN_TYPE write(int fd, const void *buf, size_t len) /* some gcc tool chains will use different data structure */ +#else +ssize_t write(int fd, const void *buf, size_t len) +#endif +{ + int result; + struct dfs_fd *d; + + /* get the fd */ + d = fd_get(fd); + if (d == NULL) + { + rt_set_errno(-EBADF); + + return -1; + } + + result = dfs_file_write(d, buf, len); + if (result < 0) + { + fd_put(d); + rt_set_errno(result); + + return -1; + } + + /* release the ref-count of fd */ + fd_put(d); + + return result; +} +RTM_EXPORT(write); + +/** + * this function is a POSIX compliant version, which will seek the offset for + * an open file descriptor. + * + * @param fd the file descriptor. + * @param offset the offset to be seeked. + * @param whence the directory of seek. + * + * @return the current read/write position in the file, or -1 on failed. + */ +off_t lseek(int fd, off_t offset, int whence) +{ + int result; + struct dfs_fd *d; + + d = fd_get(fd); + if (d == NULL) + { + rt_set_errno(-EBADF); + + return -1; + } + + switch (whence) + { + case SEEK_SET: + break; + + case SEEK_CUR: + offset += d->pos; + break; + + case SEEK_END: + offset += d->size; + break; + + default: + fd_put(d); + rt_set_errno(-EINVAL); + + return -1; + } + + if (offset < 0) + { + fd_put(d); + rt_set_errno(-EINVAL); + + return -1; + } + result = dfs_file_lseek(d, offset); + if (result < 0) + { + fd_put(d); + rt_set_errno(result); + + return -1; + } + + /* release the ref-count of fd */ + fd_put(d); + + return offset; +} +RTM_EXPORT(lseek); + +#ifndef _WIN32 +/** + * this function is a POSIX compliant version, which will rename old file name + * to new file name. + * + * @param old the old file name. + * @param new the new file name. + * + * @return 0 on successful, -1 on failed. + * + * note: the old and new file name must be belong to a same file system. + */ +int rename(const char *old_file, const char *new_file) +{ + int result; + + result = dfs_file_rename(old_file, new_file); + if (result < 0) + { + rt_set_errno(result); + + return -1; + } + + return 0; +} +RTM_EXPORT(rename); +#endif + +/** + * this function is a POSIX compliant version, which will unlink (remove) a + * specified path file from file system. + * + * @param pathname the specified path name to be unlinked. + * + * @return 0 on successful, -1 on failed. + */ +int unlink(const char *pathname) +{ + int result; + + result = dfs_file_unlink(pathname); + if (result < 0) + { + rt_set_errno(result); + + return -1; + } + + return 0; +} +RTM_EXPORT(unlink); + +/** + * this function is a POSIX compliant version, which will get file information. + * + * @param file the file name + * @param buf the data buffer to save stat description. + * + * @return 0 on successful, -1 on failed. + */ +int stat(const char *file, struct stat *buf) +{ + int result; + + result = dfs_file_stat(file, buf); + if (result < 0) + { + rt_set_errno(result); + + return -1; + } + + return result; +} +RTM_EXPORT(stat); + +/** + * this function is a POSIX compliant version, which will get file status. + * + * @param fildes the file description + * @param buf the data buffer to save stat description. + * + * @return 0 on successful, -1 on failed. + */ +int fstat(int fildes, struct stat *buf) +{ + struct dfs_fd *d; + + /* get the fd */ + d = fd_get(fildes); + if (d == NULL) + { + rt_set_errno(-EBADF); + + return -1; + } + + /* it's the root directory */ + buf->st_dev = 0; + + buf->st_mode = S_IFREG | S_IRUSR | S_IRGRP | S_IROTH | + S_IWUSR | S_IWGRP | S_IWOTH; + if (d->type == FT_DIRECTORY) + { + buf->st_mode &= ~S_IFREG; + buf->st_mode |= S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH; + } + + buf->st_size = d->size; + buf->st_mtime = 0; + + fd_put(d); + + return RT_EOK; +} +RTM_EXPORT(fstat); + +/** + * this function is a POSIX compliant version, which shall request that all data + * for the open file descriptor named by fildes is to be transferred to the storage + * device associated with the file described by fildes. + * + * @param fildes the file description + * + * @return 0 on successful completion. Otherwise, -1 shall be returned and errno + * set to indicate the error. + */ +int fsync(int fildes) +{ + int ret; + struct dfs_fd *d; + + /* get the fd */ + d = fd_get(fildes); + if (d == NULL) + { + rt_set_errno(-EBADF); + return -1; + } + + ret = dfs_file_flush(d); + + fd_put(d); + return ret; +} +RTM_EXPORT(fsync); + +/** + * this function is a POSIX compliant version, which shall perform a variety of + * control functions on devices. + * + * @param fildes the file description + * @param cmd the specified command + * @param data represents the additional information that is needed by this + * specific device to perform the requested function. + * + * @return 0 on successful completion. Otherwise, -1 shall be returned and errno + * set to indicate the error. + */ +int fcntl(int fildes, int cmd, ...) +{ + int ret = -1; + struct dfs_fd *d; + + /* get the fd */ + d = fd_get(fildes); + if (d) + { + void *arg; + va_list ap; + + va_start(ap, cmd); + arg = va_arg(ap, void *); + va_end(ap); + + ret = dfs_file_ioctl(d, cmd, arg); + fd_put(d); + } + else ret = -EBADF; + + if (ret < 0) + { + rt_set_errno(ret); + ret = -1; + } + + return ret; +} +RTM_EXPORT(fcntl); + +/** + * this function is a POSIX compliant version, which shall perform a variety of + * control functions on devices. + * + * @param fildes the file description + * @param cmd the specified command + * @param data represents the additional information that is needed by this + * specific device to perform the requested function. + * + * @return 0 on successful completion. Otherwise, -1 shall be returned and errno + * set to indicate the error. + */ +int ioctl(int fildes, int cmd, ...) +{ + void *arg; + va_list ap; + + va_start(ap, cmd); + arg = va_arg(ap, void *); + va_end(ap); + + /* we use fcntl for this API. */ + return fcntl(fildes, cmd, arg); +} +RTM_EXPORT(ioctl); + +/** + * + * this function is a POSIX compliant version, which cause the regular file + * referenced by fd to be truncated to a size of precisely length bytes. + * @param fd the file descriptor. + * @param length the length to be truncated. + * + * @return Upon successful completion, ftruncate() shall return 0; + * otherwise, -1 shall be returned and errno set to indicate the error. + */ +int ftruncate(int fd, off_t length) +{ + int result; + struct dfs_fd *d; + + d = fd_get(fd); + if (d == NULL) + { + rt_set_errno(-EBADF); + + return -1; + } + + if (length < 0) + { + fd_put(d); + rt_set_errno(-EINVAL); + + return -1; + } + result = dfs_file_ftruncate(d, length); + if (result < 0) + { + fd_put(d); + rt_set_errno(result); + + return -1; + } + + /* release the ref-count of fd */ + fd_put(d); + + return 0; +} +RTM_EXPORT(ftruncate); + +/** + * this function is a POSIX compliant version, which will return the + * information about a mounted file system. + * + * @param path the path which mounted file system. + * @param buf the buffer to save the returned information. + * + * @return 0 on successful, others on failed. + */ +int statfs(const char *path, struct statfs *buf) +{ + int result; + + result = dfs_statfs(path, buf); + if (result < 0) + { + rt_set_errno(result); + + return -1; + } + + return result; +} +RTM_EXPORT(statfs); + +/** + * this function is a POSIX compliant version, which will make a directory + * + * @param path the directory path to be made. + * @param mode + * + * @return 0 on successful, others on failed. + */ +int mkdir(const char *path, mode_t mode) +{ + int fd; + struct dfs_fd *d; + int result; + + fd = fd_new(); + if (fd == -1) + { + rt_set_errno(-ENOMEM); + + return -1; + } + + d = fd_get(fd); + + result = dfs_file_open(d, path, O_DIRECTORY | O_CREAT); + + if (result < 0) + { + fd_put(d); + fd_put(d); + rt_set_errno(result); + + return -1; + } + + dfs_file_close(d); + fd_put(d); + fd_put(d); + + return 0; +} +RTM_EXPORT(mkdir); + +/** + * this function is a POSIX compliant version, which will remove a directory. + * + * @param pathname the path name to be removed. + * + * @return 0 on successful, others on failed. + */ +int rmdir(const char *pathname) +{ + int result; + + result = dfs_file_unlink(pathname); + if (result < 0) + { + rt_set_errno(result); + + return -1; + } + + return 0; +} +RTM_EXPORT(rmdir); + +/** + * this function is a POSIX compliant version, which will open a directory. + * + * @param name the path name to be open. + * + * @return the DIR pointer of directory, NULL on open directory failed. + */ +DIR *opendir(const char *name) +{ + struct dfs_fd *d; + int fd, result; + DIR *t; + + t = NULL; + + /* allocate a fd */ + fd = fd_new(); + if (fd == -1) + { + rt_set_errno(-ENOMEM); + + return NULL; + } + d = fd_get(fd); + + result = dfs_file_open(d, name, O_RDONLY | O_DIRECTORY); + if (result >= 0) + { + /* open successfully */ + t = (DIR *) rt_malloc(sizeof(DIR)); + if (t == NULL) + { + dfs_file_close(d); + fd_put(d); + } + else + { + memset(t, 0, sizeof(DIR)); + + t->fd = fd; + } + fd_put(d); + + return t; + } + + /* open failed */ + fd_put(d); + fd_put(d); + rt_set_errno(result); + + return NULL; +} +RTM_EXPORT(opendir); + +/** + * this function is a POSIX compliant version, which will return a pointer + * to a dirent structure representing the next directory entry in the + * directory stream. + * + * @param d the directory stream pointer. + * + * @return the next directory entry, NULL on the end of directory or failed. + */ +struct dirent *readdir(DIR *d) +{ + int result; + struct dfs_fd *fd; + + fd = fd_get(d->fd); + if (fd == NULL) + { + rt_set_errno(-EBADF); + return NULL; + } + + if (d->num) + { + struct dirent *dirent_ptr; + dirent_ptr = (struct dirent *)&d->buf[d->cur]; + d->cur += dirent_ptr->d_reclen; + } + + if (!d->num || d->cur >= d->num) + { + /* get a new entry */ + result = dfs_file_getdents(fd, + (struct dirent *)d->buf, + sizeof(d->buf) - 1); + if (result <= 0) + { + fd_put(fd); + rt_set_errno(result); + + return NULL; + } + + d->num = result; + d->cur = 0; /* current entry index */ + } + + fd_put(fd); + + return (struct dirent *)(d->buf + d->cur); +} +RTM_EXPORT(readdir); + +/** + * this function is a POSIX compliant version, which will return current + * location in directory stream. + * + * @param d the directory stream pointer. + * + * @return the current location in directory stream. + */ +long telldir(DIR *d) +{ + struct dfs_fd *fd; + long result; + + fd = fd_get(d->fd); + if (fd == NULL) + { + rt_set_errno(-EBADF); + + return 0; + } + + result = fd->pos - d->num + d->cur; + fd_put(fd); + + return result; +} +RTM_EXPORT(telldir); + +/** + * this function is a POSIX compliant version, which will set position of + * next directory structure in the directory stream. + * + * @param d the directory stream. + * @param offset the offset in directory stream. + */ +void seekdir(DIR *d, off_t offset) +{ + struct dfs_fd *fd; + + fd = fd_get(d->fd); + if (fd == NULL) + { + rt_set_errno(-EBADF); + + return ; + } + + /* seek to the offset position of directory */ + if (dfs_file_lseek(fd, offset) >= 0) + d->num = d->cur = 0; + fd_put(fd); +} +RTM_EXPORT(seekdir); + +/** + * this function is a POSIX compliant version, which will reset directory + * stream. + * + * @param d the directory stream. + */ +void rewinddir(DIR *d) +{ + struct dfs_fd *fd; + + fd = fd_get(d->fd); + if (fd == NULL) + { + rt_set_errno(-EBADF); + + return ; + } + + /* seek to the beginning of directory */ + if (dfs_file_lseek(fd, 0) >= 0) + d->num = d->cur = 0; + fd_put(fd); +} +RTM_EXPORT(rewinddir); + +/** + * this function is a POSIX compliant version, which will close a directory + * stream. + * + * @param d the directory stream. + * + * @return 0 on successful, -1 on failed. + */ +int closedir(DIR *d) +{ + int result; + struct dfs_fd *fd; + + fd = fd_get(d->fd); + if (fd == NULL) + { + rt_set_errno(-EBADF); + + return -1; + } + + result = dfs_file_close(fd); + fd_put(fd); + + fd_put(fd); + rt_free(d); + + if (result < 0) + { + rt_set_errno(result); + + return -1; + } + else + return 0; +} +RTM_EXPORT(closedir); + +#ifdef DFS_USING_WORKDIR +/** + * this function is a POSIX compliant version, which will change working + * directory. + * + * @param path the path name to be changed to. + * + * @return 0 on successful, -1 on failed. + */ +int chdir(const char *path) +{ + char *fullpath; + DIR *d; + + if (path == NULL) + { + dfs_lock(); + rt_kprintf("%s\n", working_directory); + dfs_unlock(); + + return 0; + } + + if (strlen(path) > DFS_PATH_MAX) + { + rt_set_errno(-ENOTDIR); + + return -1; + } + + fullpath = dfs_normalize_path(NULL, path); + if (fullpath == NULL) + { + rt_set_errno(-ENOTDIR); + + return -1; /* build path failed */ + } + + dfs_lock(); + d = opendir(fullpath); + if (d == NULL) + { + rt_free(fullpath); + /* this is a not exist directory */ + dfs_unlock(); + + return -1; + } + + /* close directory stream */ + closedir(d); + + /* copy full path to working directory */ + strncpy(working_directory, fullpath, DFS_PATH_MAX); + /* release normalize directory path name */ + rt_free(fullpath); + + dfs_unlock(); + + return 0; +} +RTM_EXPORT(chdir); + +#ifdef RT_USING_FINSH +FINSH_FUNCTION_EXPORT_ALIAS(chdir, cd, change current working directory); +#endif +#endif + +/** + * this function is a POSIX compliant version, which shall check the file named + * by the pathname pointed to by the path argument for accessibility according + * to the bit pattern contained in amode. + * + * @param path the specified file/dir path. + * @param amode the value is either the bitwise-inclusive OR of the access + * permissions to be checked (R_OK, W_OK, X_OK) or the existence test (F_OK). + */ +int access(const char *path, int amode) +{ + struct stat sb; + if (stat(path, &sb) < 0) + return -1; /* already sets errno */ + + /* ignore R_OK,W_OK,X_OK condition */ + return 0; +} + +/** + * this function is a POSIX compliant version, which will return current + * working directory. + * + * @param buf the returned current directory. + * @param size the buffer size. + * + * @return the returned current directory. + */ +char *getcwd(char *buf, size_t size) +{ +#ifdef DFS_USING_WORKDIR + dfs_lock(); + strncpy(buf, working_directory, size); + dfs_unlock(); +#else + rt_kprintf(NO_WORKING_DIR); +#endif + + return buf; +} +RTM_EXPORT(getcwd); diff --git a/components/finsh/SConscript b/components/finsh/SConscript index 5eabe47c0e..9b2faa2283 100644 --- a/components/finsh/SConscript +++ b/components/finsh/SConscript @@ -9,7 +9,7 @@ msh.c if GetDepend('MSH_USING_BUILT_IN_COMMANDS'): src += ['cmd.c'] -if GetDepend('RT_USING_POSIX'): +if GetDepend('DFS_USING_POSIX'): src += ['msh_file.c'] CPPPATH = [cwd] diff --git a/components/finsh/msh.c b/components/finsh/msh.c index 5cb54c26e7..727f343a68 100644 --- a/components/finsh/msh.c +++ b/components/finsh/msh.c @@ -15,19 +15,17 @@ #ifdef RT_USING_FINSH #ifndef FINSH_ARG_MAX - #define FINSH_ARG_MAX 8 -#endif +#define FINSH_ARG_MAX 8 +#endif /* FINSH_ARG_MAX */ #include "msh.h" #include "shell.h" - -#ifdef RT_USING_POSIX - #include <dfs_posix.h> -#endif - +#ifdef DFS_USING_POSIX +#include <dfs_posix.h> +#endif /* DFS_USING_POSIX */ #ifdef RT_USING_MODULE - #include <dlmodule.h> -#endif +#include <dlmodule.h> +#endif /* RT_USING_MODULE */ typedef int (*cmd_function_t)(int argc, char **argv); @@ -186,7 +184,7 @@ static cmd_function_t msh_get_cmd(char *cmd, int size) return cmd_func; } -#if defined(RT_USING_MODULE) && defined(RT_USING_POSIX) +#if defined(RT_USING_MODULE) && defined(DFS_USING_POSIX) /* Return 0 on module executed. Other value indicate error. */ int msh_exec_module(const char *cmd_line, int size) @@ -257,7 +255,7 @@ int msh_exec_module(const char *cmd_line, int size) rt_free(pg_name); return ret; } -#endif /* defined(RT_USING_MODULE) && defined(RT_USING_POSIX) */ +#endif /* defined(RT_USING_MODULE) && defined(DFS_USING_POSIX) */ static int _msh_exec_cmd(char *cmd, rt_size_t length, int *retp) { @@ -290,7 +288,7 @@ static int _msh_exec_cmd(char *cmd, rt_size_t length, int *retp) return 0; } -#if defined(RT_USING_LWP) && defined(RT_USING_POSIX) +#if defined(RT_USING_LWP) && defined(DFS_USING_POSIX) static int _msh_exec_lwp(char *cmd, rt_size_t length) { int argc; @@ -326,7 +324,7 @@ static int _msh_exec_lwp(char *cmd, rt_size_t length) return 0; } -#endif /* defined(RT_USING_LWP) && defined(RT_USING_DFS) */ +#endif /* defined(RT_USING_LWP) && defined(DFS_USING_POSIX) */ int msh_exec(char *cmd, rt_size_t length) { @@ -350,7 +348,7 @@ int msh_exec(char *cmd, rt_size_t length) { return cmd_ret; } -#ifdef RT_USING_POSIX +#ifdef DFS_USING_POSIX #ifdef DFS_USING_WORKDIR if (msh_exec_script(cmd, length) == 0) { @@ -371,7 +369,7 @@ int msh_exec(char *cmd, rt_size_t length) return 0; } #endif /* RT_USING_LWP */ -#endif /* RT_USING_POSIX */ +#endif /* DFS_USING_POSIX */ /* truncate the cmd at the first space. */ { @@ -400,7 +398,7 @@ static int str_common(const char *str1, const char *str2) return (str - str1); } -#ifdef RT_USING_POSIX +#ifdef DFS_USING_POSIX void msh_auto_complete_path(char *path) { DIR *dir = RT_NULL; @@ -521,7 +519,7 @@ void msh_auto_complete_path(char *path) closedir(dir); rt_free(full_path); } -#endif /* RT_USING_POSIX */ +#endif /* DFS_USING_POSIX */ void msh_auto_complete(char *prefix) { @@ -538,7 +536,7 @@ void msh_auto_complete(char *prefix) return; } -#ifdef RT_USING_POSIX +#ifdef DFS_USING_POSIX /* check whether a spare in the command */ { char *ptr; @@ -564,7 +562,7 @@ void msh_auto_complete(char *prefix) } #endif /* RT_USING_MODULE */ } -#endif /* RT_USING_DFS */ +#endif /* DFS_USING_POSIX */ /* checks in internal command */ { diff --git a/components/finsh/msh_file.c b/components/finsh/msh_file.c index 4e9ed53b31..9dcb820c44 100644 --- a/components/finsh/msh_file.c +++ b/components/finsh/msh_file.c @@ -11,7 +11,7 @@ #include <rtthread.h> -#if defined(RT_USING_FINSH) && defined(RT_USING_POSIX) +#if defined(RT_USING_FINSH) && defined(DFS_USING_POSIX) #include <finsh.h> #include "msh.h" @@ -702,5 +702,4 @@ static int cmd_tail(int argc, char **argv) } MSH_CMD_EXPORT_ALIAS(cmd_tail, tail, print the last N - lines data of the given file); -#endif /* defined(RT_USING_FINSH) && defined(RT_USING_POSIX) */ - +#endif /* defined(RT_USING_FINSH) && defined(DFS_USING_POSIX) */ diff --git a/components/finsh/shell.c b/components/finsh/shell.c index c2f6e2a550..5b96d0daf9 100644 --- a/components/finsh/shell.c +++ b/components/finsh/shell.c @@ -27,9 +27,9 @@ #include "shell.h" #include "msh.h" -#ifdef RT_USING_POSIX +#ifdef DFS_USING_POSIX #include <dfs_posix.h> -#endif /* RT_USING_POSIX */ +#endif /* DFS_USING_POSIX */ /* finsh thread */ #ifndef RT_USING_HEAP @@ -104,7 +104,7 @@ const char *finsh_get_prompt(void) } strcpy(finsh_prompt, _MSH_PROMPT); -#if defined(RT_USING_POSIX) && defined(DFS_USING_WORKDIR) +#if defined(DFS_USING_POSIX) && defined(DFS_USING_WORKDIR) /* get current working directory */ getcwd(&finsh_prompt[rt_strlen(finsh_prompt)], RT_CONSOLEBUF_SIZE - rt_strlen(finsh_prompt)); #endif diff --git a/components/libc/compilers/armlibc/syscalls.c b/components/libc/compilers/armlibc/syscalls.c index b6448f2165..71eacb1c41 100644 --- a/components/libc/compilers/armlibc/syscalls.c +++ b/components/libc/compilers/armlibc/syscalls.c @@ -33,7 +33,7 @@ __asm(".global __use_no_semihosting\n\t"); #else #pragma import(__use_no_semihosting_swi) -#endif +#endif /* __CLANG_ARM */ /* Standard IO device handles. */ #define STDIN 0 @@ -55,10 +55,10 @@ const char __stderr_name[] = "STDERR"; */ FILEHANDLE _sys_open(const char *name, int openmode) { -#ifdef RT_USING_POSIX +#ifdef DFS_USING_POSIX int fd; int mode = O_RDONLY; -#endif +#endif /* DFS_USING_POSIX */ /* Register standard Input Output devices. */ if (strcmp(name, __stdin_name) == 0) @@ -68,7 +68,7 @@ FILEHANDLE _sys_open(const char *name, int openmode) if (strcmp(name, __stderr_name) == 0) return (STDERR); -#ifndef RT_USING_POSIX +#ifndef DFS_USING_POSIX return 0; /* error */ #else /* Correct openmode from fopen to open */ @@ -102,19 +102,19 @@ FILEHANDLE _sys_open(const char *name, int openmode) return 0; /* error */ else return fd; -#endif /* RT_USING_POSIX */ +#endif /* DFS_USING_POSIX */ } int _sys_close(FILEHANDLE fh) { -#ifdef RT_USING_POSIX +#ifdef DFS_USING_POSIX if (fh <= STDERR) return 0; /* error */ return close(fh); #else return 0; -#endif /* RT_USING_POSIX */ +#endif /* DFS_USING_POSIX */ } /* @@ -144,7 +144,7 @@ int _sys_close(FILEHANDLE fh) */ int _sys_read(FILEHANDLE fh, unsigned char *buf, unsigned len, int mode) { -#ifdef RT_USING_POSIX +#ifdef DFS_USING_POSIX int size; if (fh == STDIN) @@ -175,7 +175,7 @@ int _sys_read(FILEHANDLE fh, unsigned char *buf, unsigned len, int mode) } #else return 0; /* error */ -#endif /* RT_USING_POSIX */ +#endif /* DFS_USING_POSIX */ } /* @@ -189,9 +189,9 @@ int _sys_read(FILEHANDLE fh, unsigned char *buf, unsigned len, int mode) */ int _sys_write(FILEHANDLE fh, const unsigned char *buf, unsigned len, int mode) { -#ifdef RT_USING_POSIX +#ifdef DFS_USING_POSIX int size; -#endif /* RT_USING_POSIX */ +#endif /* DFS_USING_POSIX */ if (fh == STDOUT || fh == STDERR) { @@ -213,7 +213,7 @@ int _sys_write(FILEHANDLE fh, const unsigned char *buf, unsigned len, int mode) } else { -#ifdef RT_USING_POSIX +#ifdef DFS_USING_POSIX size = write(fh, buf, len); if (size >= 0) return 0; /* success */ @@ -221,7 +221,7 @@ int _sys_write(FILEHANDLE fh, const unsigned char *buf, unsigned len, int mode) return 0; /* error */ #else return 0; /* error */ -#endif /* RT_USING_POSIX */ +#endif /* DFS_USING_POSIX */ } } @@ -231,7 +231,7 @@ int _sys_write(FILEHANDLE fh, const unsigned char *buf, unsigned len, int mode) */ int _sys_seek(FILEHANDLE fh, long pos) { -#ifdef RT_USING_POSIX +#ifdef DFS_USING_POSIX if (fh < STDERR) return 0; /* error */ @@ -239,7 +239,7 @@ int _sys_seek(FILEHANDLE fh, long pos) return lseek(fh, pos, 0); #else return 0; /* error */ -#endif /* RT_USING_POSIX */ +#endif /* DFS_USING_POSIX */ } /** @@ -284,7 +284,7 @@ RT_WEAK void _sys_exit(int return_code) */ long _sys_flen(FILEHANDLE fh) { -#ifdef RT_USING_POSIX +#ifdef DFS_USING_POSIX struct stat stat; if (fh < STDERR) @@ -294,7 +294,7 @@ long _sys_flen(FILEHANDLE fh) return stat.st_size; #else return 0; -#endif /* RT_USING_POSIX */ +#endif /* DFS_USING_POSIX */ } int _sys_istty(FILEHANDLE fh) @@ -307,11 +307,11 @@ int _sys_istty(FILEHANDLE fh) int remove(const char *filename) { -#ifdef RT_USING_POSIX +#ifdef DFS_USING_POSIX return unlink(filename); #else return 0; /* error */ -#endif /* RT_USING_POSIX */ +#endif /* DFS_USING_POSIX */ } #ifdef __MICROLIB diff --git a/components/libc/compilers/dlib/SConscript b/components/libc/compilers/dlib/SConscript index 39707abc61..f685873e16 100644 --- a/components/libc/compilers/dlib/SConscript +++ b/components/libc/compilers/dlib/SConscript @@ -7,7 +7,7 @@ group = [] CPPDEFINES = ['RT_USING_DLIBC'] if rtconfig.PLATFORM == 'iar': - if GetDepend('RT_USING_POSIX'): + if GetDepend('DFS_USING_POSIX'): from distutils.version import LooseVersion from iar import IARVersion diff --git a/components/libc/compilers/dlib/syscall_close.c b/components/libc/compilers/dlib/syscall_close.c index aef9673153..30ee6f2388 100644 --- a/components/libc/compilers/dlib/syscall_close.c +++ b/components/libc/compilers/dlib/syscall_close.c @@ -24,9 +24,9 @@ int __close(int handle) handle == _LLIO_STDERR || handle == _LLIO_STDIN) return _LLIO_ERROR; -#ifdef RT_USING_POSIX +#ifdef DFS_USING_POSIX return close(handle); #else return _LLIO_ERROR; -#endif /* RT_USING_POSIX */ +#endif /* DFS_USING_POSIX */ } diff --git a/components/libc/compilers/dlib/syscall_lseek.c b/components/libc/compilers/dlib/syscall_lseek.c index b1547f699e..622f613c51 100644 --- a/components/libc/compilers/dlib/syscall_lseek.c +++ b/components/libc/compilers/dlib/syscall_lseek.c @@ -33,9 +33,9 @@ long __lseek(int handle, long offset, int whence) handle == _LLIO_STDERR || handle == _LLIO_STDIN) return _LLIO_ERROR; -#ifdef RT_USING_POSIX +#ifdef DFS_USING_POSIX return lseek(handle, offset, whence); #else return _LLIO_ERROR; -#endif /* RT_USING_POSIX */ +#endif /* DFS_USING_POSIX */ } diff --git a/components/libc/compilers/dlib/syscall_open.c b/components/libc/compilers/dlib/syscall_open.c index 3336bc8885..66cddc83c1 100644 --- a/components/libc/compilers/dlib/syscall_open.c +++ b/components/libc/compilers/dlib/syscall_open.c @@ -21,7 +21,7 @@ int __open(const char *filename, int mode) { -#ifdef RT_USING_POSIX +#ifdef DFS_USING_POSIX int handle; int open_mode = O_RDONLY; @@ -73,5 +73,5 @@ int __open(const char *filename, int mode) return handle; #else return _LLIO_ERROR; -#endif /* RT_USING_POSIX */ +#endif /* DFS_USING_POSIX */ } diff --git a/components/libc/compilers/dlib/syscall_read.c b/components/libc/compilers/dlib/syscall_read.c index e88f88ead4..469ea7c1dd 100644 --- a/components/libc/compilers/dlib/syscall_read.c +++ b/components/libc/compilers/dlib/syscall_read.c @@ -34,7 +34,7 @@ size_t __read(int handle, unsigned char *buf, size_t len) { -#ifdef RT_USING_POSIX +#ifdef DFS_USING_POSIX int size; if (handle == _LLIO_STDIN) @@ -59,5 +59,5 @@ size_t __read(int handle, unsigned char *buf, size_t len) return size; /* return the length of the data read */ #else return _LLIO_ERROR; -#endif /* RT_USING_POSIX */ +#endif /* DFS_USING_POSIX */ } diff --git a/components/libc/compilers/dlib/syscall_remove.c b/components/libc/compilers/dlib/syscall_remove.c index cf6541132a..870f91b8c1 100644 --- a/components/libc/compilers/dlib/syscall_remove.c +++ b/components/libc/compilers/dlib/syscall_remove.c @@ -20,9 +20,9 @@ int remove(const char *filename) { -#ifdef RT_USING_POSIX +#ifdef DFS_USING_POSIX return unlink(filename); #else return _LLIO_ERROR; -#endif /* RT_USING_POSIX */ +#endif /* DFS_USING_POSIX */ } diff --git a/components/libc/compilers/dlib/syscall_write.c b/components/libc/compilers/dlib/syscall_write.c index 9a7707e9d3..d5ede3e59e 100644 --- a/components/libc/compilers/dlib/syscall_write.c +++ b/components/libc/compilers/dlib/syscall_write.c @@ -37,9 +37,9 @@ size_t __write(int handle, const unsigned char *buf, size_t len) { -#ifdef RT_USING_POSIX +#ifdef DFS_USING_POSIX int size; -#endif /* RT_USING_POSIX */ +#endif /* DFS_USING_POSIX */ if ((handle == _LLIO_STDOUT) || (handle == _LLIO_STDERR)) { @@ -63,11 +63,11 @@ size_t __write(int handle, const unsigned char *buf, size_t len) } else { -#ifdef RT_USING_POSIX +#ifdef DFS_USING_POSIX size = write(handle, buf, len); return size; /* return the length of the data written */ #else return _LLIO_ERROR; -#endif /* RT_USING_POSIX */ +#endif /* DFS_USING_POSIX */ } } diff --git a/components/libc/compilers/gcc/newlib/syscalls.c b/components/libc/compilers/gcc/newlib/syscalls.c index f3a2f5bfcf..8bb9af940d 100644 --- a/components/libc/compilers/gcc/newlib/syscalls.c +++ b/components/libc/compilers/gcc/newlib/syscalls.c @@ -109,12 +109,12 @@ int _getpid_r(struct _reent *ptr) int _close_r(struct _reent *ptr, int fd) { -#ifdef RT_USING_POSIX +#ifdef DFS_USING_POSIX return close(fd); #else ptr->_errno = ENOTSUP; return -1; -#endif +#endif /* DFS_USING_POSIX */ } int _execve_r(struct _reent *ptr, const char * name, char *const *argv, char *const *env) @@ -183,7 +183,7 @@ int flock(int fd, int operation) _off_t _lseek_r(struct _reent *ptr, int fd, _off_t pos, int whence) { -#ifdef RT_USING_POSIX +#ifdef DFS_USING_POSIX _off_t rc; rc = lseek(fd, pos, whence); @@ -191,12 +191,12 @@ _off_t _lseek_r(struct _reent *ptr, int fd, _off_t pos, int whence) #else ptr->_errno = ENOTSUP; return -1; -#endif /* RT_USING_POSIX */ +#endif /* DFS_USING_POSIX */ } int _mkdir_r(struct _reent *ptr, const char *name, int mode) { -#ifdef RT_USING_POSIX +#ifdef DFS_USING_POSIX int rc; rc = mkdir(name, mode); @@ -204,24 +204,24 @@ int _mkdir_r(struct _reent *ptr, const char *name, int mode) #else ptr->_errno = ENOTSUP; return -1; -#endif /* RT_USING_POSIX */ +#endif /* DFS_USING_POSIX */ } int _open_r(struct _reent *ptr, const char *file, int flags, int mode) { -#ifdef RT_USING_POSIX +#ifdef DFS_USING_POSIX int rc; rc = open(file, flags, mode); return rc; #else ptr->_errno = ENOTSUP; return -1; -#endif /* RT_USING_POSIX */ +#endif /* DFS_USING_POSIX */ } _ssize_t _read_r(struct _reent *ptr, int fd, void *buf, size_t nbytes) { -#ifdef RT_USING_POSIX +#ifdef DFS_USING_POSIX _ssize_t rc; if (fd == STDIN_FILENO) { @@ -247,12 +247,12 @@ _ssize_t _read_r(struct _reent *ptr, int fd, void *buf, size_t nbytes) #else ptr->_errno = ENOTSUP; return -1; -#endif /* RT_USING_POSIX */ +#endif /* DFS_USING_POSIX */ } int _rename_r(struct _reent *ptr, const char *old, const char *new) { -#ifdef RT_USING_POSIX +#ifdef DFS_USING_POSIX int rc; rc = rename(old, new); @@ -260,12 +260,12 @@ int _rename_r(struct _reent *ptr, const char *old, const char *new) #else ptr->_errno = ENOTSUP; return -1; -#endif /* RT_USING_POSIX */ +#endif /* DFS_USING_POSIX */ } int _stat_r(struct _reent *ptr, const char *file, struct stat *pstat) { -#ifdef RT_USING_POSIX +#ifdef DFS_USING_POSIX int rc; rc = stat(file, pstat); @@ -273,24 +273,24 @@ int _stat_r(struct _reent *ptr, const char *file, struct stat *pstat) #else ptr->_errno = ENOTSUP; return -1; -#endif /* RT_USING_POSIX */ +#endif /* DFS_USING_POSIX */ } int _unlink_r(struct _reent *ptr, const char *file) { -#ifdef RT_USING_POSIX +#ifdef DFS_USING_POSIX return unlink(file); #else ptr->_errno = ENOTSUP; return -1; -#endif /* RT_USING_POSIX */ +#endif /* DFS_USING_POSIX */ } _ssize_t _write_r(struct _reent *ptr, int fd, const void *buf, size_t nbytes) { -#ifdef RT_USING_POSIX +#ifdef DFS_USING_POSIX _ssize_t rc; -#endif /* RT_USING_POSIX */ +#endif /* DFS_USING_POSIX */ if (fd == STDOUT_FILENO || fd == STDERR_FILENO) { @@ -311,13 +311,13 @@ _ssize_t _write_r(struct _reent *ptr, int fd, const void *buf, size_t nbytes) return -1; } -#ifdef RT_USING_POSIX +#ifdef DFS_USING_POSIX rc = write(fd, buf, nbytes); return rc; #else ptr->_errno = ENOTSUP; return -1; -#endif /* RT_USING_POSIX */ +#endif /* DFS_USING_POSIX */ } /* for exit() and abort() */ diff --git a/components/libc/posix/src/unistd.c b/components/libc/posix/src/unistd.c index 85235965ea..cb18dd3499 100644 --- a/components/libc/posix/src/unistd.c +++ b/components/libc/posix/src/unistd.c @@ -8,935 +8,10 @@ * 2020-09-01 Meco Man first Version * 2021-02-12 Meco Man move all functions located in <pthread_sleep.c> to this file */ + #include <rtthread.h> -#include <dfs_file.h> -#include <dfs_private.h> -#include <sys/errno.h> #include <unistd.h> -/** - * this function is a POSIX compliant version, which will open a file and - * return a file descriptor according specified flags. - * - * @param file the path name of file. - * @param flags the file open flags. - * - * @return the non-negative integer on successful open, others for failed. - */ -int open(const char *file, int flags, ...) -{ - int fd, result; - struct dfs_fd *d; - - /* allocate a fd */ - fd = fd_new(); - if (fd < 0) - { - rt_set_errno(-ENOMEM); - - return -1; - } - d = fd_get(fd); - - result = dfs_file_open(d, file, flags); - if (result < 0) - { - /* release the ref-count of fd */ - fd_put(d); - fd_put(d); - - rt_set_errno(result); - - return -1; - } - - /* release the ref-count of fd */ - fd_put(d); - - return fd; -} -RTM_EXPORT(open); - -/** - * this function is a POSIX compliant version, which will close the open - * file descriptor. - * - * @param fd the file descriptor. - * - * @return 0 on successful, -1 on failed. - */ -int close(int fd) -{ - int result; - struct dfs_fd *d; - - d = fd_get(fd); - if (d == NULL) - { - rt_set_errno(-EBADF); - - return -1; - } - - result = dfs_file_close(d); - fd_put(d); - - if (result < 0) - { - rt_set_errno(result); - - return -1; - } - - fd_put(d); - - return 0; -} -RTM_EXPORT(close); - -/** - * this function is a POSIX compliant version, which will read specified data - * buffer length for an open file descriptor. - * - * @param fd the file descriptor. - * @param buf the buffer to save the read data. - * @param len the maximal length of data buffer - * - * @return the actual read data buffer length. If the returned value is 0, it - * may be reach the end of file, please check errno. - */ -#ifdef _READ_WRITE_RETURN_TYPE -_READ_WRITE_RETURN_TYPE read(int fd, void *buf, size_t len) /* some gcc tool chains will use different data structure */ -#else -ssize_t read(int fd, void *buf, size_t len) -#endif -{ - int result; - struct dfs_fd *d; - - /* get the fd */ - d = fd_get(fd); - if (d == NULL) - { - rt_set_errno(-EBADF); - - return -1; - } - - result = dfs_file_read(d, buf, len); - if (result < 0) - { - fd_put(d); - rt_set_errno(result); - - return -1; - } - - /* release the ref-count of fd */ - fd_put(d); - - return result; -} -RTM_EXPORT(read); - -/** - * this function is a POSIX compliant version, which will write specified data - * buffer length for an open file descriptor. - * - * @param fd the file descriptor - * @param buf the data buffer to be written. - * @param len the data buffer length. - * - * @return the actual written data buffer length. - */ -#ifdef _READ_WRITE_RETURN_TYPE -_READ_WRITE_RETURN_TYPE write(int fd, const void *buf, size_t len) /* some gcc tool chains will use different data structure */ -#else -ssize_t write(int fd, const void *buf, size_t len) -#endif -{ - int result; - struct dfs_fd *d; - - /* get the fd */ - d = fd_get(fd); - if (d == NULL) - { - rt_set_errno(-EBADF); - - return -1; - } - - result = dfs_file_write(d, buf, len); - if (result < 0) - { - fd_put(d); - rt_set_errno(result); - - return -1; - } - - /* release the ref-count of fd */ - fd_put(d); - - return result; -} -RTM_EXPORT(write); - -/** - * this function is a POSIX compliant version, which will seek the offset for - * an open file descriptor. - * - * @param fd the file descriptor. - * @param offset the offset to be seeked. - * @param whence the directory of seek. - * - * @return the current read/write position in the file, or -1 on failed. - */ -off_t lseek(int fd, off_t offset, int whence) -{ - int result; - struct dfs_fd *d; - - d = fd_get(fd); - if (d == NULL) - { - rt_set_errno(-EBADF); - - return -1; - } - - switch (whence) - { - case SEEK_SET: - break; - - case SEEK_CUR: - offset += d->pos; - break; - - case SEEK_END: - offset += d->size; - break; - - default: - fd_put(d); - rt_set_errno(-EINVAL); - - return -1; - } - - if (offset < 0) - { - fd_put(d); - rt_set_errno(-EINVAL); - - return -1; - } - result = dfs_file_lseek(d, offset); - if (result < 0) - { - fd_put(d); - rt_set_errno(result); - - return -1; - } - - /* release the ref-count of fd */ - fd_put(d); - - return offset; -} -RTM_EXPORT(lseek); - -#ifndef _WIN32 /* we can not implement these functions */ -/** - * this function is a POSIX compliant version, which will rename old file name - * to new file name. - * - * @param old the old file name. - * @param new the new file name. - * - * @return 0 on successful, -1 on failed. - * - * note: the old and new file name must be belong to a same file system. - */ -int rename(const char *old_file, const char *new_file) -{ - int result; - - result = dfs_file_rename(old_file, new_file); - if (result < 0) - { - rt_set_errno(result); - - return -1; - } - - return 0; -} -RTM_EXPORT(rename); -#endif - -/** - * this function is a POSIX compliant version, which will unlink (remove) a - * specified path file from file system. - * - * @param pathname the specified path name to be unlinked. - * - * @return 0 on successful, -1 on failed. - */ -int unlink(const char *pathname) -{ - int result; - - result = dfs_file_unlink(pathname); - if (result < 0) - { - rt_set_errno(result); - - return -1; - } - - return 0; -} -RTM_EXPORT(unlink); - -/** - * this function is a POSIX compliant version, which will get file information. - * - * @param file the file name - * @param buf the data buffer to save stat description. - * - * @return 0 on successful, -1 on failed. - */ -int stat(const char *file, struct stat *buf) -{ - int result; - - result = dfs_file_stat(file, buf); - if (result < 0) - { - rt_set_errno(result); - - return -1; - } - - return result; -} -RTM_EXPORT(stat); - -/** - * this function is a POSIX compliant version, which will get file status. - * - * @param fildes the file description - * @param buf the data buffer to save stat description. - * - * @return 0 on successful, -1 on failed. - */ -int fstat(int fildes, struct stat *buf) -{ - struct dfs_fd *d; - - /* get the fd */ - d = fd_get(fildes); - if (d == NULL) - { - rt_set_errno(-EBADF); - - return -1; - } - - /* it's the root directory */ - buf->st_dev = 0; - - buf->st_mode = S_IFREG | S_IRUSR | S_IRGRP | S_IROTH | - S_IWUSR | S_IWGRP | S_IWOTH; - if (d->type == FT_DIRECTORY) - { - buf->st_mode &= ~S_IFREG; - buf->st_mode |= S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH; - } - - buf->st_size = d->size; - buf->st_mtime = 0; - - fd_put(d); - - return RT_EOK; -} -RTM_EXPORT(fstat); - -/** - * this function is a POSIX compliant version, which shall request that all data - * for the open file descriptor named by fildes is to be transferred to the storage - * device associated with the file described by fildes. - * - * @param fildes the file description - * - * @return 0 on successful completion. Otherwise, -1 shall be returned and errno - * set to indicate the error. - */ -int fsync(int fildes) -{ - int ret; - struct dfs_fd *d; - - /* get the fd */ - d = fd_get(fildes); - if (d == NULL) - { - rt_set_errno(-EBADF); - return -1; - } - - ret = dfs_file_flush(d); - - fd_put(d); - return ret; -} -RTM_EXPORT(fsync); - -/** - * this function is a POSIX compliant version, which shall perform a variety of - * control functions on devices. - * - * @param fildes the file description - * @param cmd the specified command - * @param data represents the additional information that is needed by this - * specific device to perform the requested function. - * - * @return 0 on successful completion. Otherwise, -1 shall be returned and errno - * set to indicate the error. - */ -int fcntl(int fildes, int cmd, ...) -{ - int ret = -1; - struct dfs_fd *d; - - /* get the fd */ - d = fd_get(fildes); - if (d) - { - void *arg; - va_list ap; - - va_start(ap, cmd); - arg = va_arg(ap, void *); - va_end(ap); - - ret = dfs_file_ioctl(d, cmd, arg); - fd_put(d); - } - else ret = -EBADF; - - if (ret < 0) - { - rt_set_errno(ret); - ret = -1; - } - - return ret; -} -RTM_EXPORT(fcntl); - -/** - * this function is a POSIX compliant version, which shall perform a variety of - * control functions on devices. - * - * @param fildes the file description - * @param cmd the specified command - * @param data represents the additional information that is needed by this - * specific device to perform the requested function. - * - * @return 0 on successful completion. Otherwise, -1 shall be returned and errno - * set to indicate the error. - */ -int ioctl(int fildes, int cmd, ...) -{ - void *arg; - va_list ap; - - va_start(ap, cmd); - arg = va_arg(ap, void *); - va_end(ap); - - /* we use fcntl for this API. */ - return fcntl(fildes, cmd, arg); -} -RTM_EXPORT(ioctl); - -/** - * - * this function is a POSIX compliant version, which cause the regular file - * referenced by fd to be truncated to a size of precisely length bytes. - * @param fd the file descriptor. - * @param length the length to be truncated. - * - * @return Upon successful completion, ftruncate() shall return 0; - * otherwise, -1 shall be returned and errno set to indicate the error. - */ -int ftruncate(int fd, off_t length) -{ - int result; - struct dfs_fd *d; - - d = fd_get(fd); - if (d == NULL) - { - rt_set_errno(-EBADF); - - return -1; - } - - if (length < 0) - { - fd_put(d); - rt_set_errno(-EINVAL); - - return -1; - } - result = dfs_file_ftruncate(d, length); - if (result < 0) - { - fd_put(d); - rt_set_errno(result); - - return -1; - } - - /* release the ref-count of fd */ - fd_put(d); - - return 0; -} -RTM_EXPORT(ftruncate); - -/** - * this function is a POSIX compliant version, which will return the - * information about a mounted file system. - * - * @param path the path which mounted file system. - * @param buf the buffer to save the returned information. - * - * @return 0 on successful, others on failed. - */ -int statfs(const char *path, struct statfs *buf) -{ - int result; - - result = dfs_statfs(path, buf); - if (result < 0) - { - rt_set_errno(result); - - return -1; - } - - return result; -} -RTM_EXPORT(statfs); - -/** - * this function is a POSIX compliant version, which will make a directory - * - * @param path the directory path to be made. - * @param mode - * - * @return 0 on successful, others on failed. - */ -int mkdir(const char *path, mode_t mode) -{ - int fd; - struct dfs_fd *d; - int result; - - fd = fd_new(); - if (fd == -1) - { - rt_set_errno(-ENOMEM); - - return -1; - } - - d = fd_get(fd); - - result = dfs_file_open(d, path, O_DIRECTORY | O_CREAT); - - if (result < 0) - { - fd_put(d); - fd_put(d); - rt_set_errno(result); - - return -1; - } - - dfs_file_close(d); - fd_put(d); - fd_put(d); - - return 0; -} -RTM_EXPORT(mkdir); - -/** - * this function is a POSIX compliant version, which will remove a directory. - * - * @param pathname the path name to be removed. - * - * @return 0 on successful, others on failed. - */ -int rmdir(const char *pathname) -{ - int result; - - result = dfs_file_unlink(pathname); - if (result < 0) - { - rt_set_errno(result); - - return -1; - } - - return 0; -} -RTM_EXPORT(rmdir); - -/** - * this function is a POSIX compliant version, which will open a directory. - * - * @param name the path name to be open. - * - * @return the DIR pointer of directory, NULL on open directory failed. - */ -DIR *opendir(const char *name) -{ - struct dfs_fd *d; - int fd, result; - DIR *t; - - t = NULL; - - /* allocate a fd */ - fd = fd_new(); - if (fd == -1) - { - rt_set_errno(-ENOMEM); - - return NULL; - } - d = fd_get(fd); - - result = dfs_file_open(d, name, O_RDONLY | O_DIRECTORY); - if (result >= 0) - { - /* open successfully */ - t = (DIR *) rt_malloc(sizeof(DIR)); - if (t == NULL) - { - dfs_file_close(d); - fd_put(d); - } - else - { - memset(t, 0, sizeof(DIR)); - - t->fd = fd; - } - fd_put(d); - - return t; - } - - /* open failed */ - fd_put(d); - fd_put(d); - rt_set_errno(result); - - return NULL; -} -RTM_EXPORT(opendir); - -/** - * this function is a POSIX compliant version, which will return a pointer - * to a dirent structure representing the next directory entry in the - * directory stream. - * - * @param d the directory stream pointer. - * - * @return the next directory entry, NULL on the end of directory or failed. - */ -struct dirent *readdir(DIR *d) -{ - int result; - struct dfs_fd *fd; - - fd = fd_get(d->fd); - if (fd == NULL) - { - rt_set_errno(-EBADF); - return NULL; - } - - if (d->num) - { - struct dirent *dirent_ptr; - dirent_ptr = (struct dirent *)&d->buf[d->cur]; - d->cur += dirent_ptr->d_reclen; - } - - if (!d->num || d->cur >= d->num) - { - /* get a new entry */ - result = dfs_file_getdents(fd, - (struct dirent *)d->buf, - sizeof(d->buf) - 1); - if (result <= 0) - { - fd_put(fd); - rt_set_errno(result); - - return NULL; - } - - d->num = result; - d->cur = 0; /* current entry index */ - } - - fd_put(fd); - - return (struct dirent *)(d->buf + d->cur); -} -RTM_EXPORT(readdir); - -/** - * this function is a POSIX compliant version, which will return current - * location in directory stream. - * - * @param d the directory stream pointer. - * - * @return the current location in directory stream. - */ -long telldir(DIR *d) -{ - struct dfs_fd *fd; - long result; - - fd = fd_get(d->fd); - if (fd == NULL) - { - rt_set_errno(-EBADF); - - return 0; - } - - result = fd->pos - d->num + d->cur; - fd_put(fd); - - return result; -} -RTM_EXPORT(telldir); - -/** - * this function is a POSIX compliant version, which will set position of - * next directory structure in the directory stream. - * - * @param d the directory stream. - * @param offset the offset in directory stream. - */ -void seekdir(DIR *d, off_t offset) -{ - struct dfs_fd *fd; - - fd = fd_get(d->fd); - if (fd == NULL) - { - rt_set_errno(-EBADF); - - return ; - } - - /* seek to the offset position of directory */ - if (dfs_file_lseek(fd, offset) >= 0) - d->num = d->cur = 0; - fd_put(fd); -} -RTM_EXPORT(seekdir); - -/** - * this function is a POSIX compliant version, which will reset directory - * stream. - * - * @param d the directory stream. - */ -void rewinddir(DIR *d) -{ - struct dfs_fd *fd; - - fd = fd_get(d->fd); - if (fd == NULL) - { - rt_set_errno(-EBADF); - - return ; - } - - /* seek to the beginning of directory */ - if (dfs_file_lseek(fd, 0) >= 0) - d->num = d->cur = 0; - fd_put(fd); -} -RTM_EXPORT(rewinddir); - -/** - * this function is a POSIX compliant version, which will close a directory - * stream. - * - * @param d the directory stream. - * - * @return 0 on successful, -1 on failed. - */ -int closedir(DIR *d) -{ - int result; - struct dfs_fd *fd; - - fd = fd_get(d->fd); - if (fd == NULL) - { - rt_set_errno(-EBADF); - - return -1; - } - - result = dfs_file_close(fd); - fd_put(fd); - - fd_put(fd); - rt_free(d); - - if (result < 0) - { - rt_set_errno(result); - - return -1; - } - else - return 0; -} -RTM_EXPORT(closedir); - -#ifdef DFS_USING_WORKDIR -/** - * this function is a POSIX compliant version, which will change working - * directory. - * - * @param path the path name to be changed to. - * - * @return 0 on successful, -1 on failed. - */ -int chdir(const char *path) -{ - char *fullpath; - DIR *d; - - if (path == NULL) - { - dfs_lock(); - rt_kprintf("%s\n", working_directory); - dfs_unlock(); - - return 0; - } - - if (strlen(path) > DFS_PATH_MAX) - { - rt_set_errno(-ENOTDIR); - - return -1; - } - - fullpath = dfs_normalize_path(NULL, path); - if (fullpath == NULL) - { - rt_set_errno(-ENOTDIR); - - return -1; /* build path failed */ - } - - dfs_lock(); - d = opendir(fullpath); - if (d == NULL) - { - rt_free(fullpath); - /* this is a not exist directory */ - dfs_unlock(); - - return -1; - } - - /* close directory stream */ - closedir(d); - - /* copy full path to working directory */ - strncpy(working_directory, fullpath, DFS_PATH_MAX); - /* release normalize directory path name */ - rt_free(fullpath); - - dfs_unlock(); - - return 0; -} -RTM_EXPORT(chdir); - -#ifdef RT_USING_FINSH -FINSH_FUNCTION_EXPORT_ALIAS(chdir, cd, change current working directory); -#endif -#endif - -/** - * this function is a POSIX compliant version, which shall check the file named - * by the pathname pointed to by the path argument for accessibility according - * to the bit pattern contained in amode. - * - * @param path the specified file/dir path. - * @param amode the value is either the bitwise-inclusive OR of the access - * permissions to be checked (R_OK, W_OK, X_OK) or the existence test (F_OK). - */ -int access(const char *path, int amode) -{ - struct stat sb; - if (stat(path, &sb) < 0) - return -1; /* already sets errno */ - - /* ignore R_OK,W_OK,X_OK condition */ - return 0; -} - -/** - * this function is a POSIX compliant version, which will return current - * working directory. - * - * @param buf the returned current directory. - * @param size the buffer size. - * - * @return the returned current directory. - */ -char *getcwd(char *buf, size_t size) -{ -#ifdef DFS_USING_WORKDIR - dfs_lock(); - strncpy(buf, working_directory, size); - dfs_unlock(); -#else - rt_kprintf(NO_WORKING_DIR); -#endif - - return buf; -} -RTM_EXPORT(getcwd); - #ifdef RT_USING_POSIX_TERMIOS #include "termios.h" int isatty(int fd) |
