Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion components/dfs/dfs_v1/filesystems/tmpfs/dfs_tmpfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ int dfs_tmpfs_stat(struct dfs_filesystem *fs,
if (d_file == NULL)
return -ENOENT;

st->st_dev = 0;
st->st_dev = (dev_t)dfs_filesystem_lookup(fs->path);
st->st_mode = S_IFREG | S_IRUSR | S_IRGRP | S_IROTH |
S_IWUSR | S_IWGRP | S_IWOTH;
if (d_file->type == TMPFS_TYPE_DIR)
Expand Down
38 changes: 11 additions & 27 deletions components/dfs/dfs_v1/src/dfs_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -591,39 +591,23 @@ int dfs_file_stat(const char *path, struct stat *buf)
return -ENOENT;
}

if ((fullpath[0] == '/' && fullpath[1] == '\0') ||
(dfs_subdir(fs->path, fullpath) == NULL))
if (fs->ops->stat == NULL)
{
/* it's the root directory */
buf->st_dev = 0;

buf->st_mode = S_IRUSR | S_IRGRP | S_IROTH |
S_IWUSR | S_IWGRP | S_IWOTH;
buf->st_mode |= S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH;

buf->st_size = 0;
buf->st_mtime = 0;

/* release full path */
rt_free(fullpath);
LOG_E("the filesystem didn't implement this function");

return RT_EOK;
return -ENOSYS;
}
/* get the real file path and get file stat */
if (fs->ops->flags & DFS_FS_FLAG_FULLPATH)
{
result = fs->ops->stat(fs, fullpath, buf);
}
else
{
if (fs->ops->stat == NULL)
{
rt_free(fullpath);
LOG_E("the filesystem didn't implement this function");

return -ENOSYS;
}

/* get the real file path and get file stat */
if (fs->ops->flags & DFS_FS_FLAG_FULLPATH)
result = fs->ops->stat(fs, fullpath, buf);
else
result = fs->ops->stat(fs, dfs_subdir(fs->path, fullpath), buf);
const char *subdir = dfs_subdir(fs->path, fullpath);
subdir = subdir ? subdir : "/";
result = fs->ops->stat(fs, subdir, buf);
}

rt_free(fullpath);
Expand Down
4 changes: 4 additions & 0 deletions components/dfs/dfs_v2/filesystems/devfs/devfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,10 @@ int dfs_devfs_free_vnode(struct dfs_vnode *vnode)
int dfs_devfs_mount(struct dfs_mnt *mnt, unsigned long rwflag, const void *data)
{
RT_ASSERT(mnt != RT_NULL);

rt_atomic_add(&(mnt->ref_count), 1);
mnt->flags |= MNT_IS_LOCKED;

return RT_EOK;
}

Expand Down
38 changes: 25 additions & 13 deletions components/dfs/dfs_v2/filesystems/elmfat/dfs_elm.c
Original file line number Diff line number Diff line change
Expand Up @@ -507,15 +507,13 @@ int dfs_elm_close(struct dfs_file *file)
else if (file->vnode->type == FT_REGULAR)
{
FIL *fd = RT_NULL;

fd = (FIL *)(file->vnode->data);
RT_ASSERT(fd != RT_NULL);

result = f_close(fd);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

为什么反而把错误检查忽略了?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

在强制卸载的情况下检查返回值已经没有意义了,即使返回出错也必须释放掉。

if (result == FR_OK)
{
/* release memory */
rt_free(fd);
}
f_close(fd);
/* release memory */
rt_free(fd);
}

return elm_result_to_dfs(result);
Expand Down Expand Up @@ -573,8 +571,7 @@ ssize_t dfs_elm_read(struct dfs_file *file, void *buf, size_t len, off_t *pos)

result = f_read(fd, buf, len, &byte_read);
/* update position */
file->fpos = fd->fptr;
*pos = file->fpos;
*pos = fd->fptr;
if (result == FR_OK)
return byte_read;

Expand All @@ -597,8 +594,7 @@ ssize_t dfs_elm_write(struct dfs_file *file, const void *buf, size_t len, off_t

result = f_write(fd, buf, len, &byte_write);
/* update position and file size */
file->fpos = fd->fptr;
*pos = file->fpos;
*pos = fd->fptr;
file->vnode->size = f_size(fd);
if (result == FR_OK)
return byte_write;
Expand All @@ -621,6 +617,24 @@ int dfs_elm_flush(struct dfs_file *file)
off_t dfs_elm_lseek(struct dfs_file *file, off_t offset, int wherece)
{
FRESULT result = FR_OK;

switch (wherece)
{
case SEEK_SET:
break;

case SEEK_CUR:
offset += file->fpos;
break;

case SEEK_END:
offset += file->vnode->size;
break;

default:
return -EINVAL;
}

if (file->vnode->type == FT_REGULAR)
{
FIL *fd;
Expand All @@ -633,7 +647,6 @@ off_t dfs_elm_lseek(struct dfs_file *file, off_t offset, int wherece)
if (result == FR_OK)
{
/* return current position */
file->fpos = fd->fptr;
return fd->fptr;
}
}
Expand All @@ -649,8 +662,7 @@ off_t dfs_elm_lseek(struct dfs_file *file, off_t offset, int wherece)
if (result == FR_OK)
{
/* update file position */
file->fpos = offset;
return file->fpos;
return offset;
}
}

Expand Down
7 changes: 1 addition & 6 deletions components/dfs/dfs_v2/filesystems/mqueue/dfs_mqueue.c
Original file line number Diff line number Diff line change
Expand Up @@ -230,11 +230,6 @@ static struct dfs_filesystem_type _mqueue = {

int dfs_mqueue_init(void) {
/* register mqueue file system */
dfs_register(&_mqueue);
mkdir("/dev/mqueue", 0x777);
if (dfs_mount(RT_NULL, "/dev/mqueue", "mqueue", 0, 0) != 0) {
rt_kprintf("Dir /dev/mqueue mount failed!\n");
}
return 0;
return dfs_register(&_mqueue);
}
INIT_COMPONENT_EXPORT(dfs_mqueue_init);
43 changes: 28 additions & 15 deletions components/dfs/dfs_v2/filesystems/tmpfs/dfs_tmpfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -280,17 +280,16 @@ static ssize_t dfs_tmpfs_read(struct dfs_file *file, void *buf, size_t count, of
d_file = (struct tmpfs_file *)file->vnode->data;
RT_ASSERT(d_file != NULL);

if (count < file->vnode->size - file->fpos)
if (count < file->vnode->size - *pos)
length = count;
else
length = file->vnode->size - file->fpos;
length = file->vnode->size - *pos;

if (length > 0)
memcpy(buf, &(d_file->data[file->fpos]), length);
memcpy(buf, &(d_file->data[*pos]), length);

/* update file current position */
file->fpos += length;
*pos = file->fpos;
*pos += length;

return length;
}
Expand All @@ -306,41 +305,55 @@ static ssize_t dfs_tmpfs_write(struct dfs_file *file, const void *buf, size_t co
superblock = d_file->sb;
RT_ASSERT(superblock != NULL);

if (count + file->fpos > file->vnode->size)
if (count + *pos > file->vnode->size)
{
rt_uint8_t *ptr;
ptr = rt_realloc(d_file->data, file->fpos + count);
ptr = rt_realloc(d_file->data, *pos + count);
if (ptr == NULL)
{
rt_set_errno(-ENOMEM);
return 0;
}

superblock->df_size += (file->fpos - d_file->size + count);
superblock->df_size += (*pos - d_file->size + count);
/* update d_file and file size */
d_file->data = ptr;
d_file->size = file->fpos + count;
d_file->size = *pos + count;
file->vnode->size = d_file->size;
LOG_D("tmpfile ptr:%x, size:%d", ptr, d_file->size);
}

if (count > 0)
memcpy(d_file->data + file->fpos, buf, count);
memcpy(d_file->data + *pos, buf, count);

/* update file current position */
file->fpos += count;
*pos = file->fpos;
*pos += count;

return count;
}

static off_t dfs_tmpfs_lseek(struct dfs_file *file, off_t offset, int wherece)
{
if (offset <= (off_t)file->vnode->size)
switch (wherece)
{
file->fpos = offset;
case SEEK_SET:
break;

case SEEK_CUR:
offset += file->fpos;
break;

case SEEK_END:
offset += file->vnode->size;
break;

return file->fpos;
default:
return -EINVAL;
}

if (offset <= (off_t)file->vnode->size)
{
return offset;
}

return -EIO;
Expand Down
2 changes: 2 additions & 0 deletions components/dfs/dfs_v2/include/dfs_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ void dfs_file_deinit(struct dfs_file *file);
int dfs_file_open(struct dfs_file *file, const char *path, int flags, mode_t mode);
int dfs_file_close(struct dfs_file *file);

off_t dfs_file_get_fpos(struct dfs_file *file);
void dfs_file_set_fpos(struct dfs_file *file, off_t fpos);
ssize_t dfs_file_read(struct dfs_file *file, void *buf, size_t len);
ssize_t dfs_file_write(struct dfs_file *file, const void *buf, size_t len);
off_t generic_dfs_lseek(struct dfs_file *file, off_t offset, int whence);
Expand Down
4 changes: 3 additions & 1 deletion components/dfs/dfs_v2/include/dfs_fs.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ struct dfs_filesystem_type
struct dfs_filesystem_type *next;
};

struct dfs_filesystem_type *dfs_filesystems(void);
int dfs_unregister(struct dfs_filesystem_type *fs);
int dfs_register(struct dfs_filesystem_type *fs);
const char *dfs_filesystem_get_mounted_path(struct rt_device *device);
Expand All @@ -86,8 +87,9 @@ int dfs_mount(const char *device_name,
const char *filesystemtype,
unsigned long rwflag,
const void *data);
int dfs_umount(const char *specialfile);
int dfs_umount(const char *specialfile, int flags);
int dfs_unmount(const char *specialfile);
int dfs_is_mounted(struct dfs_mnt *mnt);
int dfs_mkfs(const char *fs_name, const char *device_name);
int dfs_statfs(const char *path, struct statfs *buffer);
int dfs_filesystem_get_partition(struct dfs_partition *part,
Expand Down
5 changes: 5 additions & 0 deletions components/dfs/dfs_v2/include/dfs_mnt.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ struct dfs_mnt
#define MNT_IS_ALLOCED 0x1 /* the mnt struct is allocated */
#define MNT_IS_ADDLIST 0x2 /* the mnt struct is added into list */
#define MNT_IS_MOUNTED 0x4 /* the mnt struct is mounted */
#define MNT_IS_UMOUNT 0x8 /* the mnt is unmount */
#define MNT_IS_LOCKED 0x10 /* the mnt is locked */
#define MNT_FORCE 0x20 /* the mnt force unmount */

rt_atomic_t ref_count; /* reference count */

Expand All @@ -58,6 +61,8 @@ int dfs_mnt_unref(struct dfs_mnt* mnt);

rt_bool_t dfs_mnt_has_child_mnt(struct dfs_mnt *mnt, const char* fullpath);

int dfs_mnt_foreach(struct dfs_mnt* (*func)(struct dfs_mnt *mnt, void *parameter), void *parameter);

#ifdef __cplusplus
}
#endif
Expand Down
4 changes: 2 additions & 2 deletions components/dfs/dfs_v2/src/dfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ int fdt_fd_new(struct dfs_fdtable *fdt)
dfs_file_lock();

/* find an empty fd entry */
idx = fd_alloc(fdt, 0);
idx = fd_alloc(fdt, (fdt == &_fdtab) ? DFS_STDIO_OFFSET : 0);
/* can't find an empty fd entry */
if (idx < 0)
{
Expand Down Expand Up @@ -452,7 +452,7 @@ sysret_t sys_dup(int oldfd)
int sys_dup(int oldfd)
#endif
{
int newfd = dfs_dup(oldfd, DFS_STDIO_OFFSET);
int newfd = dfs_dup(oldfd, (dfs_fdtable_get() == &_fdtab) ? DFS_STDIO_OFFSET : 0);

#ifdef RT_USING_SMART
return (sysret_t)newfd;
Expand Down
9 changes: 7 additions & 2 deletions components/dfs/dfs_v2/src/dfs_dentry.c
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ void dfs_dentry_insert(struct dfs_dentry *dentry)
struct dfs_dentry *dfs_dentry_lookup(struct dfs_mnt *mnt, const char *path, uint32_t flags)
{
struct dfs_dentry *dentry;
struct dfs_vnode *vnode;
struct dfs_vnode *vnode = RT_NULL;
int mntpoint_len = strlen(mnt->fullpath);

if (rt_strncmp(mnt->fullpath, path, mntpoint_len) == 0)
Expand All @@ -210,7 +210,12 @@ struct dfs_dentry *dfs_dentry_lookup(struct dfs_mnt *mnt, const char *path, uint
if (dentry)
{
DLOG(msg, "dentry", mnt->fs_ops->name, DLOG_MSG, "vnode=fs_ops->lookup(dentry)");
vnode = mnt->fs_ops->lookup(dentry);

if (dfs_is_mounted(mnt) == 0)
{
vnode = mnt->fs_ops->lookup(dentry);
}

if (vnode)
{
DLOG(msg, mnt->fs_ops->name, "dentry", DLOG_MSG_RET, "return vnode");
Expand Down
Loading