summaryrefslogtreecommitdiff
path: root/components/dfs/src
diff options
context:
space:
mode:
authoryangjie11 <[email protected]>2020-11-18 09:56:35 +0800
committerGitHub <[email protected]>2020-11-18 09:56:35 +0800
commitd4a682e8d97fa131e5a8d795f8c5451d55cdd5b0 (patch)
tree4773b40cb012475c5daf74bfd55707e141477669 /components/dfs/src
parent2b98f4720c827ae1850df78299f6b273d568c8c7 (diff)
parent2ef3c9d861f55253620996a94831a4be31ca6620 (diff)
Merge pull request #3 from RT-Thread/master
sync
Diffstat (limited to 'components/dfs/src')
-rw-r--r--components/dfs/src/dfs.c42
-rw-r--r--components/dfs/src/dfs_file.c91
-rw-r--r--components/dfs/src/dfs_fs.c138
-rw-r--r--components/dfs/src/dfs_posix.c66
-rw-r--r--components/dfs/src/poll.c27
-rw-r--r--components/dfs/src/select.c7
6 files changed, 279 insertions, 92 deletions
diff --git a/components/dfs/src/dfs.c b/components/dfs/src/dfs.c
index cf0b2b0ef9..7ace0bc160 100644
--- a/components/dfs/src/dfs.c
+++ b/components/dfs/src/dfs.c
@@ -18,6 +18,10 @@
#include <lwp.h>
#endif
+#if defined(RT_USING_DFS_DEVFS) && defined(RT_USING_POSIX)
+#include <libc.h>
+#endif
+
/* Global variables */
const struct dfs_filesystem_ops *filesystem_operation_table[DFS_FILESYSTEM_TYPES_MAX];
struct dfs_filesystem filesystem_table[DFS_FILESYSTEMS_MAX];
@@ -119,7 +123,7 @@ static int fd_alloc(struct dfs_fdtable *fdt, int startfd)
int idx;
/* find an empty fd entry */
- for (idx = startfd; idx < fdt->maxfd; idx++)
+ for (idx = startfd; idx < (int)fdt->maxfd; idx++)
{
if (fdt->fds[idx] == RT_NULL)
break;
@@ -135,9 +139,9 @@ static int fd_alloc(struct dfs_fdtable *fdt, int startfd)
/* increase the number of FD with 4 step length */
cnt = fdt->maxfd + 4;
- cnt = cnt > DFS_FD_MAX? DFS_FD_MAX : cnt;
+ 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 */
@@ -151,9 +155,9 @@ static int fd_alloc(struct dfs_fdtable *fdt, int startfd)
}
/* allocate 'struct dfs_fd' */
- if (idx < fdt->maxfd &&fdt->fds[idx] == RT_NULL)
+ 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;
}
@@ -185,7 +189,7 @@ int fd_new(void)
if (idx == fdt->maxfd)
{
idx = -(1 + DFS_FD_OFFSET);
- LOG_E( "DFS fd new is failed! Could not found an empty fd entry.");
+ LOG_E("DFS fd new is failed! Could not found an empty fd entry.");
goto __result;
}
@@ -212,9 +216,14 @@ struct dfs_fd *fd_get(int fd)
struct dfs_fd *d;
struct dfs_fdtable *fdt;
+#if defined(RT_USING_DFS_DEVFS) && defined(RT_USING_POSIX)
+ if ((0 <= fd) && (fd <= 2))
+ fd = libc_stdio_get_console();
+#endif
+
fdt = dfs_fdtable_get();
fd = fd - DFS_FD_OFFSET;
- if (fd < 0 || fd >= fdt->maxfd)
+ if (fd < 0 || fd >= (int)fdt->maxfd)
return NULL;
dfs_lock();
@@ -254,7 +263,7 @@ void fd_put(struct dfs_fd *fd)
struct dfs_fdtable *fdt;
fdt = dfs_fdtable_get();
- for (index = 0; index < fdt->maxfd; index ++)
+ for (index = 0; index < (int)fdt->maxfd; index ++)
{
if (fdt->fds[index] == fd)
{
@@ -311,7 +320,7 @@ int fd_is_open(const char *pathname)
fd = fdt->fds[index];
if (fd == NULL || fd->fops == NULL || fd->path == NULL) continue;
- if (fd->fops == fs->ops->fops && strcmp(fd->path, mountpath) == 0)
+ if (fd->fs == fs && strcmp(fd->path, mountpath) == 0)
{
/* found file in file descriptor table */
rt_free(fullpath);
@@ -384,14 +393,14 @@ 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;
/* join path and file name */
rt_snprintf(fullpath, strlen(directory) + strlen(filename) + 2,
- "%s/%s", directory, filename);
+ "%s/%s", directory, filename);
}
else
{
@@ -490,7 +499,7 @@ RTM_EXPORT(dfs_normalize_path);
/**
* This function will get the file descriptor table of current process.
*/
-struct dfs_fdtable* dfs_fdtable_get(void)
+struct dfs_fdtable *dfs_fdtable_get(void)
{
struct dfs_fdtable *fdt;
#ifdef RT_USING_LWP
@@ -514,25 +523,26 @@ int list_fd(void)
{
int index;
struct dfs_fdtable *fd_table;
-
+
fd_table = dfs_fdtable_get();
if (!fd_table) return -1;
rt_enter_critical();
-
+
rt_kprintf("fd type ref magic path\n");
rt_kprintf("-- ------ --- ----- ------\n");
- for (index = 0; index < fd_table->maxfd; index ++)
+ for (index = 0; index < (int)fd_table->maxfd; index ++)
{
struct dfs_fd *fd = fd_table->fds[index];
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");
else if (fd->type == FT_USER) rt_kprintf("%-7.7s ", "user");
+ else if (fd->type == FT_DEVICE) rt_kprintf("%-7.7s ", "device");
else rt_kprintf("%-8.8s ", "unknown");
rt_kprintf("%3d ", fd->ref_count);
rt_kprintf("%04x ", fd->magic);
diff --git a/components/dfs/src/dfs_file.c b/components/dfs/src/dfs_file.c
index 646ae58e36..fd205f2f3a 100644
--- a/components/dfs/src/dfs_file.c
+++ b/components/dfs/src/dfs_file.c
@@ -2,12 +2,13 @@
* Copyright (c) 2006-2018, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
- *
+ *
* Change Logs:
* Date Author Notes
* 2005-02-22 Bernard The first version.
* 2011-12-08 Bernard Merges rename patch from iamcacy.
* 2015-05-27 Bernard Fix the fd clear issue.
+ * 2019-01-24 Bernard Remove file repeatedly open check.
*/
#include <dfs.h>
@@ -48,14 +49,6 @@ int dfs_file_open(struct dfs_fd *fd, const char *path, int flags)
LOG_D("open file:%s", fullpath);
- /* Check whether file is already open */
- if (fd_is_open(fullpath) == 0)
- {
- rt_free(fullpath); /* release path */
-
- return -EBUSY;
- }
-
/* find filesystem */
fs = dfs_filesystem_lookup(fullpath);
if (fs == NULL)
@@ -66,7 +59,8 @@ int dfs_file_open(struct dfs_fd *fd, const char *path, int flags)
}
LOG_D("open in filesystem:%s", fs->ops->name);
- fd->fops = fs->ops->fops; /* set file ops */
+ fd->fs = fs; /* set file system */
+ fd->fops = fs->ops->fops; /* set file ops */
/* initialize the fd item */
fd->type = FT_REGULAR;
@@ -105,7 +99,7 @@ int dfs_file_open(struct dfs_fd *fd, const char *path, int flags)
rt_free(fd->path);
fd->path = NULL;
- LOG_E("open failed");
+ LOG_D("%s open failed", fullpath);
return result;
}
@@ -117,7 +111,7 @@ int dfs_file_open(struct dfs_fd *fd, const char *path, int flags)
fd->flags |= DFS_F_DIRECTORY;
}
- LOG_I("open successful");
+ LOG_D("open successful");
return 0;
}
@@ -163,7 +157,7 @@ int dfs_file_ioctl(struct dfs_fd *fd, int cmd, void *args)
return -EINVAL;
/* regular file system fd */
- if (fd->type == FT_REGULAR)
+ if (fd->type == FT_REGULAR || fd->type == FT_DEVICE)
{
switch (cmd)
{
@@ -171,7 +165,7 @@ int dfs_file_ioctl(struct dfs_fd *fd, int cmd, void *args)
return fd->flags; /* return flags */
case F_SETFL:
{
- int flags = (int)args;
+ int flags = (int)(rt_base_t)args;
int mask = O_NONBLOCK | O_APPEND;
flags &= mask;
@@ -375,8 +369,7 @@ int dfs_file_stat(const char *path, struct stat *buf)
if ((fs = dfs_filesystem_lookup(fullpath)) == NULL)
{
- LOG_E(
- "can't find mounted filesystem on this path:%s", fullpath);
+ LOG_E("can't find mounted filesystem on this path:%s", fullpath);
rt_free(fullpath);
return -ENOENT;
@@ -405,8 +398,7 @@ int dfs_file_stat(const char *path, struct stat *buf)
if (fs->ops->stat == NULL)
{
rt_free(fullpath);
- LOG_E(
- "the filesystem didn't implement this function");
+ LOG_E("the filesystem didn't implement this function");
return -ENOSYS;
}
@@ -488,6 +480,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>
@@ -542,14 +563,15 @@ void ls(const char *pathname)
}
else
{
- rt_kprintf("%-25lu\n", stat.st_size);
+ rt_kprintf("%-25lu\n", (unsigned long)stat.st_size);
}
}
else
rt_kprintf("BAD file: %s\n", dirent.d_name);
rt_free(fullpath);
}
- }while(length > 0);
+ }
+ while (length > 0);
dfs_file_close(&fd);
}
@@ -571,7 +593,7 @@ void rm(const char *filename)
}
FINSH_FUNCTION_EXPORT(rm, remove files or directories);
-void cat(const char* filename)
+void cat(const char *filename)
{
uint32_t length;
char buffer[81];
@@ -586,12 +608,13 @@ void cat(const char* filename)
do
{
memset(buffer, 0, sizeof(buffer));
- length = dfs_file_read(&fd, buffer, sizeof(buffer)-1 );
+ length = dfs_file_read(&fd, buffer, sizeof(buffer) - 1);
if (length > 0)
{
rt_kprintf("%s", buffer);
}
- }while (length > 0);
+ }
+ while (length > 0);
dfs_file_close(&fd);
}
@@ -604,7 +627,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");
@@ -644,7 +667,8 @@ static void copyfile(const char *src, const char *dst)
break;
}
}
- } while (read_bytes > 0);
+ }
+ while (read_bytes > 0);
dfs_file_close(&src_fd);
dfs_file_close(&fd);
@@ -652,7 +676,7 @@ static void copyfile(const char *src, const char *dst)
}
extern int mkdir(const char *path, mode_t mode);
-static void copydir(const char * src, const char * dst)
+static void copydir(const char *src, const char *dst)
{
struct dirent dirent;
struct stat stat;
@@ -671,8 +695,8 @@ static void copydir(const char * src, const char * dst)
length = dfs_file_getdents(&cpfd, &dirent, sizeof(struct dirent));
if (length > 0)
{
- char * src_entry_full = NULL;
- char * dst_entry_full = NULL;
+ char *src_entry_full = NULL;
+ char *dst_entry_full = NULL;
if (strcmp(dirent.d_name, "..") == 0 || strcmp(dirent.d_name, ".") == 0)
continue;
@@ -709,15 +733,16 @@ static void copydir(const char * src, const char * dst)
rt_free(src_entry_full);
rt_free(dst_entry_full);
}
- }while(length > 0);
+ }
+ while (length > 0);
dfs_file_close(&cpfd);
}
static const char *_get_path_lastname(const char *path)
{
- char * ptr;
- if ((ptr = strrchr(path, '/')) == NULL)
+ char *ptr;
+ if ((ptr = (char *)strrchr(path, '/')) == NULL)
return path;
/* skip the '/' then return */
@@ -773,7 +798,7 @@ void copy(const char *src, const char *dst)
{
if (flag & FLAG_DST_IS_DIR)
{
- char * fdst;
+ char *fdst;
fdst = dfs_normalize_path(dst, _get_path_lastname(src));
if (fdst == NULL)
{
@@ -792,7 +817,7 @@ void copy(const char *src, const char *dst)
{
if (flag & FLAG_DST_IS_DIR)
{
- char * fdst;
+ char *fdst;
fdst = dfs_normalize_path(dst, _get_path_lastname(src));
if (fdst == NULL)
{
diff --git a/components/dfs/src/dfs_fs.c b/components/dfs/src/dfs_fs.c
index a183e7f124..c21b914727 100644
--- a/components/dfs/src/dfs_fs.c
+++ b/components/dfs/src/dfs_fs.c
@@ -38,7 +38,7 @@ int dfs_register(const struct dfs_filesystem_ops *ops)
dfs_lock();
/* check if this filesystem was already registered */
for (iter = &filesystem_operation_table[0];
- iter < &filesystem_operation_table[DFS_FILESYSTEM_TYPES_MAX]; iter ++)
+ iter < &filesystem_operation_table[DFS_FILESYSTEM_TYPES_MAX]; iter ++)
{
/* find out an empty filesystem type entry */
if (*iter == NULL)
@@ -55,7 +55,7 @@ int dfs_register(const struct dfs_filesystem_ops *ops)
if (empty == NULL)
{
rt_set_errno(-ENOSPC);
- LOG_E("There is no space to register this file system (%d).", ops->name);
+ LOG_E("There is no space to register this file system (%s).", ops->name);
ret = -1;
}
else if (ret == RT_EOK)
@@ -120,16 +120,16 @@ struct dfs_filesystem *dfs_filesystem_lookup(const char *path)
*
* @return the mounted path or NULL if none device mounted.
*/
-const char* dfs_filesystem_get_mounted_path(struct rt_device* device)
+const char *dfs_filesystem_get_mounted_path(struct rt_device *device)
{
- const char* path = NULL;
+ const char *path = NULL;
struct dfs_filesystem *iter;
dfs_lock();
for (iter = &filesystem_table[0];
iter < &filesystem_table[DFS_FILESYSTEMS_MAX]; iter++)
{
- /* fint the mounted device */
+ /* find the mounted device */
if (iter->ops == NULL) continue;
else if (iter->dev_id == device)
{
@@ -154,8 +154,8 @@ const char* dfs_filesystem_get_mounted_path(struct rt_device* device)
* @return RT_EOK on successful or -RT_ERROR on failed.
*/
int dfs_filesystem_get_partition(struct dfs_partition *part,
- uint8_t *buf,
- uint32_t pindex)
+ uint8_t *buf,
+ uint32_t pindex)
{
#define DPT_ADDRESS 0x1be /* device partition offset in Boot Sector */
#define DPT_ITEM_SIZE 16 /* partition item size */
@@ -173,28 +173,28 @@ int dfs_filesystem_get_partition(struct dfs_partition *part,
return -EIO;
/* get partition type */
- type = *(dpt+4);
+ type = *(dpt + 4);
if (type == 0)
return -EIO;
/* set partition information
* size is the number of 512-Byte */
part->type = type;
- part->offset = *(dpt+8) | *(dpt+9)<<8 | *(dpt+10)<<16 | *(dpt+11)<<24;
- part->size = *(dpt+12) | *(dpt+13)<<8 | *(dpt+14)<<16 | *(dpt+15)<<24;
+ part->offset = *(dpt + 8) | *(dpt + 9) << 8 | *(dpt + 10) << 16 | *(dpt + 11) << 24;
+ part->size = *(dpt + 12) | *(dpt + 13) << 8 | *(dpt + 14) << 16 | *(dpt + 15) << 24;
rt_kprintf("found part[%d], begin: %d, size: ",
- pindex, part->offset*512);
- if ((part->size>>11) == 0)
- rt_kprintf("%d%s",part->size>>1,"KB\n"); /* KB */
+ pindex, part->offset * 512);
+ if ((part->size >> 11) == 0)
+ rt_kprintf("%d%s", part->size >> 1, "KB\n"); /* KB */
else
{
unsigned int part_size;
part_size = part->size >> 11; /* MB */
- if ((part_size>>10) == 0)
- rt_kprintf("%d.%d%s",part_size,(part->size>>1)&0x3FF,"MB\n");
+ if ((part_size >> 10) == 0)
+ rt_kprintf("%d.%d%s", part_size, (part->size >> 1) & 0x3FF, "MB\n");
else
- rt_kprintf("%d.%d%s",part_size>>10,part_size&0x3FF,"GB\n");
+ rt_kprintf("%d.%d%s", part_size >> 10, part_size & 0x3FF, "GB\n");
}
return RT_EOK;
@@ -240,7 +240,7 @@ int dfs_mount(const char *device_name,
dfs_lock();
for (ops = &filesystem_operation_table[0];
- ops < &filesystem_operation_table[DFS_FILESYSTEM_TYPES_MAX]; ops++)
+ ops < &filesystem_operation_table[DFS_FILESYSTEM_TYPES_MAX]; ops++)
if ((*ops != NULL) && (strcmp((*ops)->name, filesystemtype) == 0))
break;
@@ -321,7 +321,7 @@ int dfs_mount(const char *device_name,
if (rt_device_open(fs->dev_id,
RT_DEVICE_OFLAG_RDWR) != RT_EOK)
{
- /* The underlaying device has error, clear the entry. */
+ /* The underlying device has error, clear the entry. */
dfs_lock();
memset(fs, 0, sizeof(struct dfs_filesystem));
@@ -444,7 +444,7 @@ int dfs_mkfs(const char *fs_name, const char *device_name)
/* lock file system */
dfs_lock();
/* find the file system operations */
- for (index = 0; index <= DFS_FILESYSTEM_TYPES_MAX; index ++)
+ for (index = 0; index < DFS_FILESYSTEM_TYPES_MAX; index ++)
{
if (filesystem_operation_table[index] != NULL &&
strcmp(filesystem_operation_table[index]->name, fs_name) == 0)
@@ -452,7 +452,7 @@ int dfs_mkfs(const char *fs_name, const char *device_name)
}
dfs_unlock();
- if (index <= DFS_FILESYSTEM_TYPES_MAX)
+ if (index < DFS_FILESYSTEM_TYPES_MAX)
{
/* find file system operation */
const struct dfs_filesystem_ops *ops = filesystem_operation_table[index];
@@ -503,13 +503,13 @@ int dfs_mount_table(void)
if (mount_table[index].path == NULL) break;
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)
+ mount_table[index].path,
+ mount_table[index].filesystemtype,
+ mount_table[index].rwflag,
+ mount_table[index].data) != 0)
{
- rt_kprintf("mount fs[%s] on %s failed.\n", mount_table[index].filesystemtype,
- mount_table[index].path);
+ LOG_E("mount fs[%s] on %s failed.\n", mount_table[index].filesystemtype,
+ mount_table[index].path);
return -RT_ERROR;
}
@@ -518,6 +518,90 @@ 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;
+}
+
+int dfs_unmount_device(rt_device_t dev)
+{
+ struct dfs_filesystem *iter;
+ struct dfs_filesystem *fs = NULL;
+
+ /* lock filesystem */
+ dfs_lock();
+
+ for (iter = &filesystem_table[0];
+ iter < &filesystem_table[DFS_FILESYSTEMS_MAX]; iter++)
+ {
+ /* check if the PATH is mounted */
+ if (strcmp(iter->dev_id->parent.name, dev->parent.name) == 0)
+ {
+ fs = iter;
+ break;
+ }
+ }
+
+ if (fs == NULL ||
+ fs->ops->unmount == NULL ||
+ fs->ops->unmount(fs) < 0)
+ {
+ goto err1;
+ }
+
+ /* close device, but do not check the status of device */
+ if (fs->dev_id != NULL)
+ rt_device_close(fs->dev_id);
+
+ if (fs->path != NULL)
+ rt_free(fs->path);
+
+ /* clear this filesystem table entry */
+ memset(fs, 0, sizeof(struct dfs_filesystem));
+
+ dfs_unlock();
+
+ return 0;
+
+err1:
+ dfs_unlock();
+
+ return -1;
+}
+
#endif
#ifdef RT_USING_FINSH
@@ -555,7 +639,7 @@ int df(const char *path)
}
rt_kprintf("disk free: %d.%d %s [ %d block, %d bytes per block ]\n",
- (unsigned long)cap, minor, unit_str[unit_index], buffer.f_bfree, buffer.f_bsize);
+ (unsigned long)cap, minor, unit_str[unit_index], buffer.f_bfree, buffer.f_bsize);
return 0;
}
FINSH_FUNCTION_EXPORT(df, get disk free);
diff --git a/components/dfs/src/dfs_posix.c b/components/dfs/src/dfs_posix.c
index f47a60ee5d..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);
@@ -423,11 +423,11 @@ int fcntl(int fildes, int cmd, ...)
d = fd_get(fildes);
if (d)
{
- void* arg;
+ void *arg;
va_list ap;
va_start(ap, cmd);
- arg = va_arg(ap, void*);
+ arg = va_arg(ap, void *);
va_end(ap);
ret = dfs_file_ioctl(d, cmd, arg);
@@ -459,11 +459,11 @@ RTM_EXPORT(fcntl);
*/
int ioctl(int fildes, int cmd, ...)
{
- void* arg;
+ void *arg;
va_list ap;
va_start(ap, cmd);
- arg = va_arg(ap, void*);
+ arg = va_arg(ap, void *);
va_end(ap);
/* we use fcntl for this API. */
@@ -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.
*
@@ -645,8 +691,8 @@ struct dirent *readdir(DIR *d)
if (d->num)
{
- struct dirent* dirent_ptr;
- dirent_ptr = (struct dirent*)&d->buf[d->cur];
+ struct dirent *dirent_ptr;
+ dirent_ptr = (struct dirent *)&d->buf[d->cur];
d->cur += dirent_ptr->d_reclen;
}
@@ -654,7 +700,7 @@ struct dirent *readdir(DIR *d)
{
/* get a new entry */
result = dfs_file_getdents(fd,
- (struct dirent*)d->buf,
+ (struct dirent *)d->buf,
sizeof(d->buf) - 1);
if (result <= 0)
{
@@ -670,7 +716,7 @@ struct dirent *readdir(DIR *d)
fd_put(fd);
- return (struct dirent *)(d->buf+d->cur);
+ return (struct dirent *)(d->buf + d->cur);
}
RTM_EXPORT(readdir);
diff --git a/components/dfs/src/poll.c b/components/dfs/src/poll.c
index 6655a8a667..fc48eca758 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
@@ -40,7 +42,7 @@ static int __wqueue_pollwake(struct rt_wqueue_node *wait, void *key)
{
struct rt_poll_node *pn;
- if (key && !((rt_uint32_t)key & wait->key))
+ if (key && !((rt_ubase_t)key & wait->key))
return -1;
pn = rt_container_of(wait, struct rt_poll_node, wqn);
@@ -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;
@@ -132,9 +134,17 @@ static int do_pollfd(struct pollfd *pollfd, rt_pollreq_t *req)
mask = POLLMASK_DEFAULT;
if (f->fops->poll)
{
- req->_key = pollfd->events | POLLERR| POLLHUP;
+ req->_key = pollfd->events | POLLERR | POLLHUP;
mask = f->fops->poll(f, req);
+
+ /* dealwith the device return error -1*/
+ if (mask < 0)
+ {
+ fd_put(f);
+ pollfd->revents = 0;
+ return mask;
+ }
}
/* Mask out unneeded events. */
mask &= pollfd->events | POLLERR | POLLHUP;
@@ -152,6 +162,7 @@ static int poll_do(struct pollfd *fds, nfds_t nfds, struct rt_poll_table *pt, in
int istimeout = 0;
int n;
struct pollfd *pf;
+ int ret = 0;
if (msec == 0)
{
@@ -166,7 +177,14 @@ static int poll_do(struct pollfd *fds, nfds_t nfds, struct rt_poll_table *pt, in
for (n = 0; n < nfds; n ++)
{
- if (do_pollfd(pf, &pt->req))
+ ret = do_pollfd(pf, &pt->req);
+ if(ret < 0)
+ {
+ /*dealwith the device return error -1 */
+ pt->req._proc = RT_NULL;
+ return ret;
+ }
+ else if(ret > 0)
{
num ++;
pt->req._proc = RT_NULL;
@@ -214,3 +232,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 b9a444fe44..181855bc74 100644
--- a/components/dfs/src/select.c
+++ b/components/dfs/src/select.c
@@ -14,16 +14,18 @@
#include <dfs_poll.h>
#include <dfs_select.h>
+#ifdef RT_USING_POSIX
+
static void fdszero(fd_set *set, int nfds)
{
fd_mask *m;
int n;
-
+
/*
The 'sizeof(fd_set)' of the system space may differ from user space,
so the actual size of the 'fd_set' is determined here with the parameter 'nfds'
*/
- m = (fd_mask*)set;
+ m = (fd_mask *)set;
for (n = 0; n < nfds; n += (sizeof(fd_mask) * 8))
{
rt_memset(m, 0, sizeof(fd_mask));
@@ -178,3 +180,4 @@ int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struc
return ret;
}
+#endif