diff options
| author | armink <[email protected]> | 2019-12-17 20:11:46 +0800 |
|---|---|---|
| committer | armink <[email protected]> | 2019-12-17 20:11:46 +0800 |
| commit | 4875ee8a3ab95c4fbf1e09941d3f44d99a9a632b (patch) | |
| tree | 15c596c04a842bac747bbcf05a703b8da76f8eac /components/dfs/src | |
| parent | fa46d6ac861f26b472d62abdf1278c445431ce4c (diff) | |
Sync from master (75a3f3dd3a930eab1dc58fa0258a9d1605a0ac11)
Diffstat (limited to 'components/dfs/src')
| -rw-r--r-- | components/dfs/src/dfs.c | 8 | ||||
| -rw-r--r-- | components/dfs/src/dfs_file.c | 33 | ||||
| -rw-r--r-- | components/dfs/src/dfs_fs.c | 39 | ||||
| -rw-r--r-- | components/dfs/src/dfs_posix.c | 50 | ||||
| -rw-r--r-- | components/dfs/src/poll.c | 5 | ||||
| -rw-r--r-- | components/dfs/src/select.c | 3 |
6 files changed, 128 insertions, 10 deletions
diff --git a/components/dfs/src/dfs.c b/components/dfs/src/dfs.c index 5bddb96930..b20de13988 100644 --- a/components/dfs/src/dfs.c +++ b/components/dfs/src/dfs.c @@ -141,7 +141,7 @@ static int fd_alloc(struct dfs_fdtable *fdt, int startfd) cnt = fdt->maxfd + 4; cnt = cnt > DFS_FD_MAX ? DFS_FD_MAX : cnt; - fds = rt_realloc(fdt->fds, cnt * sizeof(struct dfs_fd *)); + fds = (struct dfs_fd **)rt_realloc(fdt->fds, cnt * sizeof(struct dfs_fd *)); if (fds == NULL) goto __exit; /* return fdt->maxfd */ /* clean the new allocated fds */ @@ -157,7 +157,7 @@ static int fd_alloc(struct dfs_fdtable *fdt, int startfd) /* allocate 'struct dfs_fd' */ if (idx < (int)fdt->maxfd && fdt->fds[idx] == RT_NULL) { - fdt->fds[idx] = rt_calloc(1, sizeof(struct dfs_fd)); + fdt->fds[idx] = (struct dfs_fd *)rt_calloc(1, sizeof(struct dfs_fd)); if (fdt->fds[idx] == RT_NULL) idx = fdt->maxfd; } @@ -393,7 +393,7 @@ char *dfs_normalize_path(const char *directory, const char *filename) if (filename[0] != '/') /* it's a absolute path, use it directly */ { - fullpath = rt_malloc(strlen(directory) + strlen(filename) + 2); + fullpath = (char *)rt_malloc(strlen(directory) + strlen(filename) + 2); if (fullpath == NULL) return NULL; @@ -537,7 +537,7 @@ int list_fd(void) if (fd && fd->fops) { - rt_kprintf("%2d ", index); + rt_kprintf("%2d ", index + DFS_FD_OFFSET); if (fd->type == FT_DIRECTORY) rt_kprintf("%-7.7s ", "dir"); else if (fd->type == FT_REGULAR) rt_kprintf("%-7.7s ", "file"); else if (fd->type == FT_SOCKET) rt_kprintf("%-7.7s ", "socket"); diff --git a/components/dfs/src/dfs_file.c b/components/dfs/src/dfs_file.c index 65fd0c45e8..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> @@ -600,7 +629,7 @@ static void copyfile(const char *src, const char *dst) rt_uint8_t *block_ptr; rt_int32_t read_bytes; - block_ptr = rt_malloc(BUF_SZ); + block_ptr = (rt_uint8_t *)rt_malloc(BUF_SZ); if (block_ptr == NULL) { rt_kprintf("out of memory\n"); @@ -715,7 +744,7 @@ static void copydir(const char *src, const char *dst) static const char *_get_path_lastname(const char *path) { char *ptr; - if ((ptr = strrchr(path, '/')) == NULL) + if ((ptr = (char *)strrchr(path, '/')) == NULL) return path; /* skip the '/' then return */ diff --git a/components/dfs/src/dfs_fs.c b/components/dfs/src/dfs_fs.c index 04d862e85d..164beed719 100644 --- a/components/dfs/src/dfs_fs.c +++ b/components/dfs/src/dfs_fs.c @@ -508,7 +508,7 @@ int dfs_mount_table(void) mount_table[index].rwflag, mount_table[index].data) != 0) { - rt_kprintf("mount fs[%s] on %s failed.\n", mount_table[index].filesystemtype, + LOG_E("mount fs[%s] on %s failed.\n", mount_table[index].filesystemtype, mount_table[index].path); return -RT_ERROR; } @@ -518,6 +518,43 @@ int dfs_mount_table(void) return 0; } INIT_ENV_EXPORT(dfs_mount_table); + +int dfs_mount_device(rt_device_t dev) +{ + int index = 0; + + if(dev == RT_NULL) { + rt_kprintf("the device is NULL to be mounted.\n"); + return -RT_ERROR; + } + + while (1) + { + if (mount_table[index].path == NULL) break; + + if(strcmp(mount_table[index].device_name, dev->parent.name) == 0) { + if (dfs_mount(mount_table[index].device_name, + mount_table[index].path, + mount_table[index].filesystemtype, + mount_table[index].rwflag, + mount_table[index].data) != 0) + { + LOG_E("mount fs[%s] device[%s] to %s failed.\n", mount_table[index].filesystemtype, dev->parent.name, + mount_table[index].path); + return -RT_ERROR; + } else { + LOG_D("mount fs[%s] device[%s] to %s ok.\n", mount_table[index].filesystemtype, dev->parent.name, + mount_table[index].path); + return RT_EOK; + } + } + + index ++; + } + + rt_kprintf("can't find device:%s to be mounted.\n", dev->parent.name); + return -RT_ERROR; +} #endif #ifdef RT_USING_FINSH diff --git a/components/dfs/src/dfs_posix.c b/components/dfs/src/dfs_posix.c index 6d643e2d4a..fc603df437 100644 --- a/components/dfs/src/dfs_posix.c +++ b/components/dfs/src/dfs_posix.c @@ -265,11 +265,11 @@ RTM_EXPORT(lseek); * * note: the old and new file name must be belong to a same file system. */ -int rename(const char *old, const char *new) +int rename(const char *old_file, const char *new_file) { int result; - result = dfs_file_rename(old, new); + result = dfs_file_rename(old_file, new_file); if (result < 0) { rt_set_errno(result); @@ -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. * diff --git a/components/dfs/src/poll.c b/components/dfs/src/poll.c index aee157eed5..3f304549f7 100644 --- a/components/dfs/src/poll.c +++ b/components/dfs/src/poll.c @@ -19,6 +19,8 @@ #include <dfs_posix.h> #include <dfs_poll.h> +#ifdef RT_USING_POSIX + struct rt_poll_node; struct rt_poll_table @@ -54,7 +56,7 @@ static void _poll_add(rt_wqueue_t *wq, rt_pollreq_t *req) struct rt_poll_table *pt; struct rt_poll_node *node; - node = rt_malloc(sizeof(struct rt_poll_node)); + node = (struct rt_poll_node *)rt_malloc(sizeof(struct rt_poll_node)); if (node == RT_NULL) return; @@ -214,3 +216,4 @@ int poll(struct pollfd *fds, nfds_t nfds, int timeout) return num; } +#endif diff --git a/components/dfs/src/select.c b/components/dfs/src/select.c index b20f7cf656..181855bc74 100644 --- a/components/dfs/src/select.c +++ b/components/dfs/src/select.c @@ -14,6 +14,8 @@ #include <dfs_poll.h> #include <dfs_select.h> +#ifdef RT_USING_POSIX + static void fdszero(fd_set *set, int nfds) { fd_mask *m; @@ -178,3 +180,4 @@ int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struc return ret; } +#endif |
