diff options
| author | zhangjun <[email protected]> | 2019-09-26 10:22:33 +0800 |
|---|---|---|
| committer | zhangjun <[email protected]> | 2019-09-26 10:22:33 +0800 |
| commit | 60f2ae4d3cee0cbf200a89df21a3949c92125cf4 (patch) | |
| tree | 26ccd41430442e07e9509711df6cbfcf2ff64228 /components/dfs/src | |
| parent | 2a39e590244cc0a8501669e409c5b07fa4d33176 (diff) | |
[components][dfs]添加ftruncate接口
Diffstat (limited to 'components/dfs/src')
| -rw-r--r-- | components/dfs/src/dfs_file.c | 29 | ||||
| -rw-r--r-- | components/dfs/src/dfs_posix.c | 46 |
2 files changed, 75 insertions, 0 deletions
diff --git a/components/dfs/src/dfs_file.c b/components/dfs/src/dfs_file.c index 5f090a7f6b..1063a278ab 100644 --- a/components/dfs/src/dfs_file.c +++ b/components/dfs/src/dfs_file.c @@ -482,6 +482,35 @@ __exit: return result; } +/** + * this function is will 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 the status of truncated. + */ +int dfs_file_ftruncate(struct dfs_fd *fd, off_t length) +{ + int result; + + /* fd is null or not a regular file system fd, or length is invalid */ + if (fd == NULL || fd->type != FT_REGULAR || length < 0) + return -EINVAL; + + if (fd->fops->ioctl == NULL) + return -ENOSYS; + + result = fd->fops->ioctl(fd, RT_FIOFTRUNCATE, (void*)&length); + + /* update current size */ + if (result == 0) + fd->size = length; + + return result; +} + #ifdef RT_USING_FINSH #include <finsh.h> diff --git a/components/dfs/src/dfs_posix.c b/components/dfs/src/dfs_posix.c index 69fe56a84e..fc603df437 100644 --- a/components/dfs/src/dfs_posix.c +++ b/components/dfs/src/dfs_posix.c @@ -472,6 +472,52 @@ int ioctl(int fildes, int cmd, ...) 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. * |
