tmpfs adds truncate functionality and unlink adaptations
This commit is contained in:
parent
36b79798bd
commit
75b4f3d83a
|
@ -7,6 +7,7 @@
|
||||||
* Date Author Notes
|
* Date Author Notes
|
||||||
* 2022-10-24 flybreak the first version
|
* 2022-10-24 flybreak the first version
|
||||||
* 2023-02-01 xqyjlj fix cannot open the same file repeatedly in 'w' mode
|
* 2023-02-01 xqyjlj fix cannot open the same file repeatedly in 'w' mode
|
||||||
|
* 2023-09-20 zmq810150896 adds truncate functionality and standardized unlink adaptations
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <rthw.h>
|
#include <rthw.h>
|
||||||
|
@ -367,7 +368,28 @@ static off_t dfs_tmpfs_lseek(struct dfs_file *file, off_t offset, int wherece)
|
||||||
|
|
||||||
static int dfs_tmpfs_close(struct dfs_file *file)
|
static int dfs_tmpfs_close(struct dfs_file *file)
|
||||||
{
|
{
|
||||||
|
struct tmpfs_file *d_file;
|
||||||
|
|
||||||
RT_ASSERT(file->vnode->ref_count > 0);
|
RT_ASSERT(file->vnode->ref_count > 0);
|
||||||
|
if (file->vnode->ref_count != 1)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
d_file = (struct tmpfs_file *)file->vnode->data;
|
||||||
|
|
||||||
|
if (d_file == NULL)
|
||||||
|
return -ENOENT;
|
||||||
|
|
||||||
|
if (d_file->fre_memory == RT_TRUE)
|
||||||
|
{
|
||||||
|
if (d_file->data != NULL)
|
||||||
|
{
|
||||||
|
rt_free(d_file->data);
|
||||||
|
d_file->data = RT_NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
rt_free(d_file);
|
||||||
|
}
|
||||||
|
|
||||||
return RT_EOK;
|
return RT_EOK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -506,9 +528,20 @@ static int dfs_tmpfs_unlink(struct dfs_dentry *dentry)
|
||||||
rt_list_remove(&(d_file->sibling));
|
rt_list_remove(&(d_file->sibling));
|
||||||
rt_spin_unlock(&superblock->lock);
|
rt_spin_unlock(&superblock->lock);
|
||||||
|
|
||||||
|
if (rt_atomic_load(&(dentry->ref_count)) == 1)
|
||||||
|
{
|
||||||
if (d_file->data != NULL)
|
if (d_file->data != NULL)
|
||||||
|
{
|
||||||
rt_free(d_file->data);
|
rt_free(d_file->data);
|
||||||
|
d_file->data = RT_NULL;
|
||||||
|
}
|
||||||
|
|
||||||
rt_free(d_file);
|
rt_free(d_file);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
d_file->fre_memory = RT_TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
return RT_EOK;
|
return RT_EOK;
|
||||||
}
|
}
|
||||||
|
@ -644,6 +677,7 @@ static struct dfs_vnode *dfs_tmpfs_create_vnode(struct dfs_dentry *dentry, int t
|
||||||
d_file->data = NULL;
|
d_file->data = NULL;
|
||||||
d_file->size = 0;
|
d_file->size = 0;
|
||||||
d_file->sb = superblock;
|
d_file->sb = superblock;
|
||||||
|
d_file->fre_memory = RT_FALSE;
|
||||||
if (type == FT_DIRECTORY)
|
if (type == FT_DIRECTORY)
|
||||||
{
|
{
|
||||||
d_file->type = TMPFS_TYPE_DIR;
|
d_file->type = TMPFS_TYPE_DIR;
|
||||||
|
@ -679,6 +713,38 @@ static int dfs_tmpfs_free_vnode(struct dfs_vnode *vnode)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int dfs_tmpfs_truncate(struct dfs_file *file, off_t offset)
|
||||||
|
{
|
||||||
|
struct tmpfs_file *d_file = RT_NULL;
|
||||||
|
struct tmpfs_sb *superblock = RT_NULL;
|
||||||
|
rt_uint8_t *ptr = RT_NULL;
|
||||||
|
|
||||||
|
d_file = (struct tmpfs_file *)file->vnode->data;
|
||||||
|
RT_ASSERT(d_file != RT_NULL);
|
||||||
|
|
||||||
|
superblock = d_file->sb;
|
||||||
|
RT_ASSERT(superblock != RT_NULL);
|
||||||
|
|
||||||
|
ptr = rt_realloc(d_file->data, offset);
|
||||||
|
if (ptr == RT_NULL)
|
||||||
|
{
|
||||||
|
rt_set_errno(-ENOMEM);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
rt_spin_lock(&superblock->lock);
|
||||||
|
superblock->df_size = offset;
|
||||||
|
rt_spin_unlock(&superblock->lock);
|
||||||
|
|
||||||
|
/* update d_file and file size */
|
||||||
|
d_file->data = ptr;
|
||||||
|
d_file->size = offset;
|
||||||
|
file->vnode->size = d_file->size;
|
||||||
|
LOG_D("tmpfile ptr:%x, size:%d", ptr, d_file->size);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
static const struct dfs_file_ops _tmp_fops =
|
static const struct dfs_file_ops _tmp_fops =
|
||||||
{
|
{
|
||||||
.open = dfs_tmpfs_open,
|
.open = dfs_tmpfs_open,
|
||||||
|
@ -688,6 +754,7 @@ static const struct dfs_file_ops _tmp_fops =
|
||||||
.write = dfs_tmpfs_write,
|
.write = dfs_tmpfs_write,
|
||||||
.lseek = dfs_tmpfs_lseek,
|
.lseek = dfs_tmpfs_lseek,
|
||||||
.getdents = dfs_tmpfs_getdents,
|
.getdents = dfs_tmpfs_getdents,
|
||||||
|
.truncate = dfs_tmpfs_truncate,
|
||||||
};
|
};
|
||||||
|
|
||||||
static const struct dfs_filesystem_ops _tmpfs_ops =
|
static const struct dfs_filesystem_ops _tmpfs_ops =
|
||||||
|
|
|
@ -30,6 +30,7 @@ struct tmpfs_file
|
||||||
struct tmpfs_sb *sb; /* superblock ptr */
|
struct tmpfs_sb *sb; /* superblock ptr */
|
||||||
rt_uint8_t *data; /* file date ptr */
|
rt_uint8_t *data; /* file date ptr */
|
||||||
rt_size_t size; /* file size */
|
rt_size_t size; /* file size */
|
||||||
|
rt_bool_t fre_memory;/* Whether to release memory upon close */
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1082,7 +1082,7 @@ int dfs_file_unlink(const char *path)
|
||||||
rt_bool_t has_child = RT_FALSE;
|
rt_bool_t has_child = RT_FALSE;
|
||||||
|
|
||||||
has_child = dfs_mnt_has_child_mnt(mnt, fullpath);
|
has_child = dfs_mnt_has_child_mnt(mnt, fullpath);
|
||||||
if (has_child == RT_FALSE && rt_atomic_load(&(dentry->ref_count)) == 1)
|
if (has_child == RT_FALSE)
|
||||||
{
|
{
|
||||||
/* no child mnt point, unlink it */
|
/* no child mnt point, unlink it */
|
||||||
ret = -RT_ERROR;
|
ret = -RT_ERROR;
|
||||||
|
|
|
@ -18,6 +18,10 @@
|
||||||
#include <dfs_file.h>
|
#include <dfs_file.h>
|
||||||
#include <poll.h>
|
#include <poll.h>
|
||||||
|
|
||||||
|
#ifdef RT_USING_DFS_V2
|
||||||
|
#include <dfs_dentry.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* the IPC channel states
|
* the IPC channel states
|
||||||
*/
|
*/
|
||||||
|
@ -371,7 +375,6 @@ static void *_ipc_msg_get_file(int fd)
|
||||||
if (!d->vnode)
|
if (!d->vnode)
|
||||||
return RT_NULL;
|
return RT_NULL;
|
||||||
|
|
||||||
d->vnode->ref_count++;
|
|
||||||
return (void *)d;
|
return (void *)d;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -408,6 +411,7 @@ static int _ipc_msg_fd_new(void *file)
|
||||||
d->fops = df->fops;
|
d->fops = df->fops;
|
||||||
d->mode = df->mode;
|
d->mode = df->mode;
|
||||||
d->dentry = df->dentry;
|
d->dentry = df->dentry;
|
||||||
|
d->dentry->ref_count ++;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
d->vnode = df->vnode;
|
d->vnode = df->vnode;
|
||||||
|
@ -415,6 +419,8 @@ static int _ipc_msg_fd_new(void *file)
|
||||||
d->data = df->data;
|
d->data = df->data;
|
||||||
d->magic = df->magic;
|
d->magic = df->magic;
|
||||||
|
|
||||||
|
d->vnode->ref_count ++;
|
||||||
|
|
||||||
return fd;
|
return fd;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue