2011-10-08 22:50:08 +08:00
|
|
|
/*
|
2021-03-08 18:19:04 +08:00
|
|
|
* Copyright (c) 2006-2021, RT-Thread Development Team
|
2011-10-08 22:50:08 +08:00
|
|
|
*
|
2018-10-29 11:06:58 +08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2019-04-03 18:09:52 +08:00
|
|
|
*
|
2011-10-08 22:50:08 +08:00
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
|
|
|
* 2005-02-22 Bernard The first version.
|
2011-12-31 12:05:40 +08:00
|
|
|
* 2011-12-08 Bernard Merges rename patch from iamcacy.
|
2015-06-04 22:23:53 +08:00
|
|
|
* 2015-05-27 Bernard Fix the fd clear issue.
|
2019-01-24 20:55:27 +08:00
|
|
|
* 2019-01-24 Bernard Remove file repeatedly open check.
|
2011-10-08 22:50:08 +08:00
|
|
|
*/
|
2011-12-23 10:11:55 +08:00
|
|
|
|
2011-10-08 22:50:08 +08:00
|
|
|
#include <dfs.h>
|
|
|
|
#include <dfs_file.h>
|
2017-10-15 22:44:53 +08:00
|
|
|
#include <dfs_private.h>
|
2022-12-03 12:07:44 +08:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2023-04-08 22:25:51 +08:00
|
|
|
#define DFS_VNODE_HASH_NR 128
|
2022-12-03 12:07:44 +08:00
|
|
|
|
2023-04-08 22:25:51 +08:00
|
|
|
struct dfs_vnode_mgr
|
2022-12-03 12:07:44 +08:00
|
|
|
{
|
|
|
|
struct rt_mutex lock;
|
2023-04-08 22:25:51 +08:00
|
|
|
rt_list_t head[DFS_VNODE_HASH_NR];
|
2022-12-03 12:07:44 +08:00
|
|
|
};
|
|
|
|
|
2023-04-08 22:25:51 +08:00
|
|
|
static struct dfs_vnode_mgr dfs_fm;
|
2022-12-03 12:07:44 +08:00
|
|
|
|
|
|
|
void dfs_fm_lock(void)
|
|
|
|
{
|
|
|
|
rt_mutex_take(&dfs_fm.lock, RT_WAITING_FOREVER);
|
|
|
|
}
|
|
|
|
|
|
|
|
void dfs_fm_unlock(void)
|
|
|
|
{
|
|
|
|
rt_mutex_release(&dfs_fm.lock);
|
|
|
|
}
|
|
|
|
|
2023-04-08 22:25:51 +08:00
|
|
|
void dfs_vnode_mgr_init(void)
|
2022-12-03 12:07:44 +08:00
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
rt_mutex_init(&dfs_fm.lock, "dfs_mgr", RT_IPC_FLAG_PRIO);
|
2023-04-08 22:25:51 +08:00
|
|
|
for (i = 0; i < DFS_VNODE_HASH_NR; i++)
|
2022-12-03 12:07:44 +08:00
|
|
|
{
|
|
|
|
rt_list_init(&dfs_fm.head[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* BKDR Hash Function */
|
|
|
|
static unsigned int bkdr_hash(const char *str)
|
|
|
|
{
|
|
|
|
unsigned int seed = 131; // 31 131 1313 13131 131313 etc..
|
|
|
|
unsigned int hash = 0;
|
|
|
|
|
|
|
|
while (*str)
|
|
|
|
{
|
|
|
|
hash = hash * seed + (*str++);
|
|
|
|
}
|
|
|
|
|
2023-04-08 22:25:51 +08:00
|
|
|
return (hash % DFS_VNODE_HASH_NR);
|
2022-12-03 12:07:44 +08:00
|
|
|
}
|
|
|
|
|
2023-04-08 22:25:51 +08:00
|
|
|
static struct dfs_vnode *dfs_vnode_find(const char *path, rt_list_t **hash_head)
|
2022-12-03 12:07:44 +08:00
|
|
|
{
|
2023-04-08 22:25:51 +08:00
|
|
|
struct dfs_vnode *vnode = NULL;
|
2022-12-03 12:07:44 +08:00
|
|
|
int hash = bkdr_hash(path);
|
|
|
|
rt_list_t *hh;
|
|
|
|
|
|
|
|
hh = dfs_fm.head[hash].next;
|
|
|
|
|
|
|
|
if (hash_head)
|
|
|
|
{
|
|
|
|
*hash_head = &dfs_fm.head[hash];
|
|
|
|
}
|
|
|
|
|
|
|
|
while (hh != &dfs_fm.head[hash])
|
|
|
|
{
|
2023-04-08 22:25:51 +08:00
|
|
|
vnode = rt_container_of(hh, struct dfs_vnode, list);
|
2022-12-03 12:07:44 +08:00
|
|
|
if (rt_strcmp(path, vnode->fullpath) == 0)
|
|
|
|
{
|
|
|
|
/* found */
|
|
|
|
return vnode;
|
|
|
|
}
|
|
|
|
hh = hh->next;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-10-08 22:50:08 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @addtogroup FileApi
|
2023-04-15 00:33:43 +08:00
|
|
|
* @{
|
2011-10-08 22:50:08 +08:00
|
|
|
*/
|
2012-12-26 09:12:13 +08:00
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
/**
|
|
|
|
* This function will return whether this file has been opend.
|
|
|
|
*
|
|
|
|
* @param pathname the file path name.
|
|
|
|
*
|
|
|
|
* @return 0 on file has been open successfully, -1 on open failed.
|
|
|
|
*/
|
|
|
|
int dfs_file_is_open(const char *pathname)
|
|
|
|
{
|
|
|
|
char *fullpath = NULL;
|
2023-04-08 22:25:51 +08:00
|
|
|
struct dfs_vnode *vnode = NULL;
|
2022-12-03 12:07:44 +08:00
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
fullpath = dfs_normalize_path(NULL, pathname);
|
|
|
|
|
|
|
|
dfs_fm_lock();
|
2023-04-08 22:25:51 +08:00
|
|
|
vnode = dfs_vnode_find(fullpath, NULL);
|
2022-12-03 12:07:44 +08:00
|
|
|
if (vnode)
|
|
|
|
{
|
|
|
|
ret = 1;
|
|
|
|
}
|
|
|
|
dfs_fm_unlock();
|
|
|
|
|
|
|
|
rt_free(fullpath);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-10-08 22:50:08 +08:00
|
|
|
/**
|
|
|
|
* this function will open a file which specified by path with specified flags.
|
|
|
|
*
|
|
|
|
* @param fd the file descriptor pointer to return the corresponding result.
|
2011-12-31 12:05:40 +08:00
|
|
|
* @param path the specified file path.
|
2011-10-08 22:50:08 +08:00
|
|
|
* @param flags the flags for open operator.
|
|
|
|
*
|
|
|
|
* @return 0 on successful, -1 on failed.
|
|
|
|
*/
|
2023-04-08 22:25:51 +08:00
|
|
|
int dfs_file_open(struct dfs_file *fd, const char *path, int flags)
|
2011-10-08 22:50:08 +08:00
|
|
|
{
|
2012-12-26 09:12:13 +08:00
|
|
|
struct dfs_filesystem *fs;
|
|
|
|
char *fullpath;
|
|
|
|
int result;
|
2023-04-08 22:25:51 +08:00
|
|
|
struct dfs_vnode *vnode = NULL;
|
2022-12-03 12:07:44 +08:00
|
|
|
rt_list_t *hash_head;
|
2012-12-26 09:12:13 +08:00
|
|
|
|
|
|
|
/* parameter check */
|
2017-10-15 22:44:53 +08:00
|
|
|
if (fd == NULL)
|
|
|
|
return -EINVAL;
|
2012-12-26 09:12:13 +08:00
|
|
|
|
|
|
|
/* make sure we have an absolute path */
|
2017-10-15 22:44:53 +08:00
|
|
|
fullpath = dfs_normalize_path(NULL, path);
|
|
|
|
if (fullpath == NULL)
|
2012-12-26 09:12:13 +08:00
|
|
|
{
|
2017-10-15 22:44:53 +08:00
|
|
|
return -ENOMEM;
|
2012-12-26 09:12:13 +08:00
|
|
|
}
|
|
|
|
|
2018-11-02 10:14:08 +08:00
|
|
|
LOG_D("open file:%s", fullpath);
|
2017-10-15 22:44:53 +08:00
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
dfs_fm_lock();
|
|
|
|
/* vnode find */
|
2023-04-08 22:25:51 +08:00
|
|
|
vnode = dfs_vnode_find(fullpath, &hash_head);
|
2022-12-03 12:07:44 +08:00
|
|
|
if (vnode)
|
2012-12-26 09:12:13 +08:00
|
|
|
{
|
2022-12-03 12:07:44 +08:00
|
|
|
vnode->ref_count++;
|
|
|
|
fd->pos = 0;
|
|
|
|
fd->vnode = vnode;
|
|
|
|
dfs_fm_unlock();
|
2012-12-26 09:12:13 +08:00
|
|
|
rt_free(fullpath); /* release path */
|
|
|
|
}
|
2022-12-03 12:07:44 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* find filesystem */
|
|
|
|
fs = dfs_filesystem_lookup(fullpath);
|
|
|
|
if (fs == NULL)
|
|
|
|
{
|
|
|
|
dfs_fm_unlock();
|
|
|
|
rt_free(fullpath); /* release path */
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
|
2023-04-08 22:25:51 +08:00
|
|
|
vnode = rt_calloc(1, sizeof(struct dfs_vnode));
|
2022-12-03 12:07:44 +08:00
|
|
|
if (!vnode)
|
|
|
|
{
|
|
|
|
dfs_fm_unlock();
|
|
|
|
rt_free(fullpath); /* release path */
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
vnode->ref_count = 1;
|
2012-12-26 09:12:13 +08:00
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
LOG_D("open in filesystem:%s", fs->ops->name);
|
|
|
|
vnode->fs = fs; /* set file system */
|
|
|
|
vnode->fops = fs->ops->fops; /* set file ops */
|
2012-12-26 09:12:13 +08:00
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
/* initialize the fd item */
|
|
|
|
vnode->type = FT_REGULAR;
|
|
|
|
vnode->flags = 0;
|
2012-12-26 09:12:13 +08:00
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
if (!(fs->ops->flags & DFS_FS_FLAG_FULLPATH))
|
|
|
|
{
|
|
|
|
if (dfs_subdir(fs->path, fullpath) == NULL)
|
|
|
|
vnode->path = rt_strdup("/");
|
|
|
|
else
|
|
|
|
vnode->path = rt_strdup(dfs_subdir(fs->path, fullpath));
|
|
|
|
LOG_D("Actual file path: %s", vnode->path);
|
|
|
|
}
|
2012-12-26 09:12:13 +08:00
|
|
|
else
|
2022-12-03 12:07:44 +08:00
|
|
|
{
|
|
|
|
vnode->path = fullpath;
|
|
|
|
}
|
|
|
|
vnode->fullpath = fullpath;
|
2012-12-26 09:12:13 +08:00
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
/* specific file system open routine */
|
|
|
|
if (vnode->fops->open == NULL)
|
|
|
|
{
|
|
|
|
dfs_fm_unlock();
|
|
|
|
/* clear fd */
|
|
|
|
if (vnode->path != vnode->fullpath)
|
|
|
|
{
|
|
|
|
rt_free(vnode->fullpath);
|
|
|
|
}
|
|
|
|
rt_free(vnode->path);
|
|
|
|
rt_free(vnode);
|
2012-12-26 09:12:13 +08:00
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
return -ENOSYS;
|
|
|
|
}
|
|
|
|
|
|
|
|
fd->pos = 0;
|
|
|
|
fd->vnode = vnode;
|
|
|
|
|
|
|
|
/* insert vnode to hash */
|
|
|
|
rt_list_insert_after(hash_head, &vnode->list);
|
2012-12-26 09:12:13 +08:00
|
|
|
}
|
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
fd->flags = flags;
|
|
|
|
|
|
|
|
if ((result = vnode->fops->open(fd)) < 0)
|
2012-12-26 09:12:13 +08:00
|
|
|
{
|
2022-12-03 12:07:44 +08:00
|
|
|
vnode->ref_count--;
|
|
|
|
if (vnode->ref_count == 0)
|
|
|
|
{
|
|
|
|
/* remove from hash */
|
|
|
|
rt_list_remove(&vnode->list);
|
|
|
|
/* clear fd */
|
|
|
|
if (vnode->path != vnode->fullpath)
|
|
|
|
{
|
|
|
|
rt_free(vnode->fullpath);
|
|
|
|
}
|
|
|
|
rt_free(vnode->path);
|
|
|
|
fd->vnode = NULL;
|
|
|
|
rt_free(vnode);
|
|
|
|
}
|
2012-12-26 09:12:13 +08:00
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
dfs_fm_unlock();
|
2019-04-12 10:39:05 +08:00
|
|
|
LOG_D("%s open failed", fullpath);
|
2012-12-26 09:12:13 +08:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
fd->flags |= DFS_F_OPEN;
|
2017-10-15 22:44:53 +08:00
|
|
|
if (flags & O_DIRECTORY)
|
2012-12-26 09:12:13 +08:00
|
|
|
{
|
2022-12-03 12:07:44 +08:00
|
|
|
fd->vnode->type = FT_DIRECTORY;
|
2012-12-26 09:12:13 +08:00
|
|
|
fd->flags |= DFS_F_DIRECTORY;
|
|
|
|
}
|
2022-12-03 12:07:44 +08:00
|
|
|
dfs_fm_unlock();
|
2012-12-26 09:12:13 +08:00
|
|
|
|
2019-03-07 15:56:39 +08:00
|
|
|
LOG_D("open successful");
|
2012-12-26 09:12:13 +08:00
|
|
|
return 0;
|
2011-10-08 22:50:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* this function will close a file descriptor.
|
|
|
|
*
|
|
|
|
* @param fd the file descriptor to be closed.
|
|
|
|
*
|
|
|
|
* @return 0 on successful, -1 on failed.
|
|
|
|
*/
|
2023-04-08 22:25:51 +08:00
|
|
|
int dfs_file_close(struct dfs_file *fd)
|
2011-10-08 22:50:08 +08:00
|
|
|
{
|
2023-04-08 22:25:51 +08:00
|
|
|
struct dfs_vnode *vnode = NULL;
|
2012-12-26 09:12:13 +08:00
|
|
|
int result = 0;
|
2011-10-08 22:50:08 +08:00
|
|
|
|
2017-10-15 22:44:53 +08:00
|
|
|
if (fd == NULL)
|
2022-12-03 12:07:44 +08:00
|
|
|
{
|
2017-10-15 22:44:53 +08:00
|
|
|
return -ENXIO;
|
2022-12-03 12:07:44 +08:00
|
|
|
}
|
2017-06-06 18:19:32 +08:00
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
if (fd->ref_count == 1)
|
|
|
|
{
|
|
|
|
dfs_fm_lock();
|
|
|
|
vnode = fd->vnode;
|
2011-10-08 22:50:08 +08:00
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
if (vnode->ref_count <= 0)
|
|
|
|
{
|
|
|
|
dfs_fm_unlock();
|
|
|
|
return -ENXIO;
|
|
|
|
}
|
2011-10-08 22:50:08 +08:00
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
if (vnode->fops->close != NULL)
|
|
|
|
{
|
|
|
|
result = vnode->fops->close(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* close fd error, return */
|
|
|
|
if (result < 0)
|
|
|
|
{
|
|
|
|
dfs_fm_unlock();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (vnode->ref_count == 1)
|
|
|
|
{
|
|
|
|
/* remove from hash */
|
|
|
|
rt_list_remove(&vnode->list);
|
|
|
|
fd->vnode = NULL;
|
|
|
|
|
|
|
|
if (vnode->path != vnode->fullpath)
|
|
|
|
{
|
|
|
|
rt_free(vnode->fullpath);
|
|
|
|
}
|
|
|
|
rt_free(vnode->path);
|
|
|
|
rt_free(vnode);
|
|
|
|
}
|
|
|
|
dfs_fm_unlock();
|
|
|
|
}
|
2011-10-08 22:50:08 +08:00
|
|
|
|
2012-12-26 09:12:13 +08:00
|
|
|
return result;
|
2011-10-08 22:50:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* this function will perform a io control on a file descriptor.
|
|
|
|
*
|
|
|
|
* @param fd the file descriptor.
|
|
|
|
* @param cmd the command to send to file descriptor.
|
|
|
|
* @param args the argument to send to file descriptor.
|
|
|
|
*
|
|
|
|
* @return 0 on successful, -1 on failed.
|
|
|
|
*/
|
2023-04-08 22:25:51 +08:00
|
|
|
int dfs_file_ioctl(struct dfs_file *fd, int cmd, void *args)
|
2011-10-08 22:50:08 +08:00
|
|
|
{
|
2017-12-31 14:46:24 +08:00
|
|
|
if (fd == NULL)
|
2022-12-03 12:07:44 +08:00
|
|
|
{
|
2017-10-15 22:44:53 +08:00
|
|
|
return -EINVAL;
|
2022-12-03 12:07:44 +08:00
|
|
|
}
|
2011-10-08 22:50:08 +08:00
|
|
|
|
2017-12-31 14:46:24 +08:00
|
|
|
/* regular file system fd */
|
2022-12-03 12:07:44 +08:00
|
|
|
if (fd->vnode->type == FT_REGULAR || fd->vnode->type == FT_DEVICE)
|
2017-12-31 14:46:24 +08:00
|
|
|
{
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case F_GETFL:
|
|
|
|
return fd->flags; /* return flags */
|
|
|
|
case F_SETFL:
|
|
|
|
{
|
2018-12-13 14:54:26 +08:00
|
|
|
int flags = (int)(rt_base_t)args;
|
2017-12-31 14:46:24 +08:00
|
|
|
int mask = O_NONBLOCK | O_APPEND;
|
|
|
|
|
|
|
|
flags &= mask;
|
2018-10-13 10:18:10 +08:00
|
|
|
fd->flags &= ~mask;
|
2017-12-31 14:46:24 +08:00
|
|
|
fd->flags |= flags;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
if (fd->vnode->fops->ioctl != NULL)
|
|
|
|
{
|
|
|
|
return fd->vnode->fops->ioctl(fd, cmd, args);
|
|
|
|
}
|
2011-10-08 22:50:08 +08:00
|
|
|
|
2017-10-15 22:44:53 +08:00
|
|
|
return -ENOSYS;
|
2011-10-08 22:50:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-12-26 09:12:13 +08:00
|
|
|
* this function will read specified length data from a file descriptor to a
|
|
|
|
* buffer.
|
2011-10-08 22:50:08 +08:00
|
|
|
*
|
|
|
|
* @param fd the file descriptor.
|
|
|
|
* @param buf the buffer to save the read data.
|
|
|
|
* @param len the length of data buffer to be read.
|
|
|
|
*
|
|
|
|
* @return the actual read data bytes or 0 on end of file or failed.
|
|
|
|
*/
|
2023-04-08 22:25:51 +08:00
|
|
|
int dfs_file_read(struct dfs_file *fd, void *buf, size_t len)
|
2011-10-08 22:50:08 +08:00
|
|
|
{
|
2012-12-26 09:12:13 +08:00
|
|
|
int result = 0;
|
2011-10-08 22:50:08 +08:00
|
|
|
|
2017-10-15 22:44:53 +08:00
|
|
|
if (fd == NULL)
|
2022-12-03 12:07:44 +08:00
|
|
|
{
|
2017-10-15 22:44:53 +08:00
|
|
|
return -EINVAL;
|
2022-12-03 12:07:44 +08:00
|
|
|
}
|
2011-10-08 22:50:08 +08:00
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
if (fd->vnode->fops->read == NULL)
|
|
|
|
{
|
2017-10-15 22:44:53 +08:00
|
|
|
return -ENOSYS;
|
2022-12-03 12:07:44 +08:00
|
|
|
}
|
2011-10-08 22:50:08 +08:00
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
if ((result = fd->vnode->fops->read(fd, buf, len)) < 0)
|
|
|
|
{
|
2012-12-26 09:12:13 +08:00
|
|
|
fd->flags |= DFS_F_EOF;
|
2022-12-03 12:07:44 +08:00
|
|
|
}
|
2011-10-08 22:50:08 +08:00
|
|
|
|
2012-12-26 09:12:13 +08:00
|
|
|
return result;
|
2011-10-08 22:50:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* this function will fetch directory entries from a directory descriptor.
|
|
|
|
*
|
2011-12-31 12:05:40 +08:00
|
|
|
* @param fd the directory descriptor.
|
2011-10-08 22:50:08 +08:00
|
|
|
* @param dirp the dirent buffer to save result.
|
2011-12-31 12:05:40 +08:00
|
|
|
* @param nbytes the available room in the buffer.
|
2011-10-08 22:50:08 +08:00
|
|
|
*
|
|
|
|
* @return the read dirent, others on failed.
|
|
|
|
*/
|
2023-04-08 22:25:51 +08:00
|
|
|
int dfs_file_getdents(struct dfs_file *fd, struct dirent *dirp, size_t nbytes)
|
2011-10-08 22:50:08 +08:00
|
|
|
{
|
2012-12-26 09:12:13 +08:00
|
|
|
/* parameter check */
|
2022-12-03 12:07:44 +08:00
|
|
|
if (fd == NULL)
|
|
|
|
{
|
2017-10-15 22:44:53 +08:00
|
|
|
return -EINVAL;
|
2022-12-03 12:07:44 +08:00
|
|
|
}
|
2011-10-08 22:50:08 +08:00
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
if (fd->vnode->type != FT_DIRECTORY)
|
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fd->vnode->fops->getdents != NULL)
|
|
|
|
{
|
|
|
|
return fd->vnode->fops->getdents(fd, dirp, nbytes);
|
|
|
|
}
|
2011-10-08 22:50:08 +08:00
|
|
|
|
2017-10-15 22:44:53 +08:00
|
|
|
return -ENOSYS;
|
2011-10-08 22:50:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* this function will unlink (remove) a specified path file from file system.
|
|
|
|
*
|
|
|
|
* @param path the specified path file to be unlinked.
|
|
|
|
*
|
|
|
|
* @return 0 on successful, -1 on failed.
|
|
|
|
*/
|
|
|
|
int dfs_file_unlink(const char *path)
|
|
|
|
{
|
2012-12-26 09:12:13 +08:00
|
|
|
int result;
|
|
|
|
char *fullpath;
|
|
|
|
struct dfs_filesystem *fs;
|
|
|
|
|
|
|
|
/* Make sure we have an absolute path */
|
2017-10-15 22:44:53 +08:00
|
|
|
fullpath = dfs_normalize_path(NULL, path);
|
|
|
|
if (fullpath == NULL)
|
2012-12-26 09:12:13 +08:00
|
|
|
{
|
2017-10-15 22:44:53 +08:00
|
|
|
return -EINVAL;
|
2012-12-26 09:12:13 +08:00
|
|
|
}
|
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
/* Check whether file is already open */
|
|
|
|
if (dfs_file_is_open(fullpath))
|
2012-12-26 09:12:13 +08:00
|
|
|
{
|
2022-12-03 12:07:44 +08:00
|
|
|
result = -EBUSY;
|
2012-12-26 09:12:13 +08:00
|
|
|
goto __exit;
|
|
|
|
}
|
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
/* get filesystem */
|
|
|
|
if ((fs = dfs_filesystem_lookup(fullpath)) == NULL)
|
2012-12-26 09:12:13 +08:00
|
|
|
{
|
2022-12-03 12:07:44 +08:00
|
|
|
result = -ENOENT;
|
2012-12-26 09:12:13 +08:00
|
|
|
goto __exit;
|
|
|
|
}
|
|
|
|
|
2017-10-15 22:44:53 +08:00
|
|
|
if (fs->ops->unlink != NULL)
|
2012-12-26 09:12:13 +08:00
|
|
|
{
|
|
|
|
if (!(fs->ops->flags & DFS_FS_FLAG_FULLPATH))
|
|
|
|
{
|
2017-10-15 22:44:53 +08:00
|
|
|
if (dfs_subdir(fs->path, fullpath) == NULL)
|
2012-12-26 09:12:13 +08:00
|
|
|
result = fs->ops->unlink(fs, "/");
|
|
|
|
else
|
|
|
|
result = fs->ops->unlink(fs, dfs_subdir(fs->path, fullpath));
|
|
|
|
}
|
|
|
|
else
|
2015-06-04 22:23:53 +08:00
|
|
|
result = fs->ops->unlink(fs, fullpath);
|
2012-12-26 09:12:13 +08:00
|
|
|
}
|
2017-10-15 22:44:53 +08:00
|
|
|
else result = -ENOSYS;
|
2011-10-08 22:50:08 +08:00
|
|
|
|
|
|
|
__exit:
|
2012-12-26 09:12:13 +08:00
|
|
|
rt_free(fullpath);
|
|
|
|
return result;
|
2011-10-08 22:50:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* this function will write some specified length data to file system.
|
|
|
|
*
|
|
|
|
* @param fd the file descriptor.
|
|
|
|
* @param buf the data buffer to be written.
|
|
|
|
* @param len the data buffer length
|
|
|
|
*
|
|
|
|
* @return the actual written data length.
|
|
|
|
*/
|
2023-04-08 22:25:51 +08:00
|
|
|
int dfs_file_write(struct dfs_file *fd, const void *buf, size_t len)
|
2011-10-08 22:50:08 +08:00
|
|
|
{
|
2017-10-15 22:44:53 +08:00
|
|
|
if (fd == NULL)
|
2022-12-03 12:07:44 +08:00
|
|
|
{
|
2017-10-15 22:44:53 +08:00
|
|
|
return -EINVAL;
|
2022-12-03 12:07:44 +08:00
|
|
|
}
|
2011-10-08 22:50:08 +08:00
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
if (fd->vnode->fops->write == NULL)
|
|
|
|
{
|
2017-10-15 22:44:53 +08:00
|
|
|
return -ENOSYS;
|
2022-12-03 12:07:44 +08:00
|
|
|
}
|
2011-10-08 22:50:08 +08:00
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
return fd->vnode->fops->write(fd, buf, len);
|
2011-10-08 22:50:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* this function will flush buffer on a file descriptor.
|
|
|
|
*
|
|
|
|
* @param fd the file descriptor.
|
|
|
|
*
|
|
|
|
* @return 0 on successful, -1 on failed.
|
|
|
|
*/
|
2023-04-08 22:25:51 +08:00
|
|
|
int dfs_file_flush(struct dfs_file *fd)
|
2011-10-08 22:50:08 +08:00
|
|
|
{
|
2017-10-15 22:44:53 +08:00
|
|
|
if (fd == NULL)
|
|
|
|
return -EINVAL;
|
2011-10-08 22:50:08 +08:00
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
if (fd->vnode->fops->flush == NULL)
|
2017-10-15 22:44:53 +08:00
|
|
|
return -ENOSYS;
|
2011-10-08 22:50:08 +08:00
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
return fd->vnode->fops->flush(fd);
|
2011-10-08 22:50:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* this function will seek the offset for specified file descriptor.
|
|
|
|
*
|
|
|
|
* @param fd the file descriptor.
|
2012-07-10 18:08:46 +08:00
|
|
|
* @param offset the offset to be sought.
|
2011-10-08 22:50:08 +08:00
|
|
|
*
|
|
|
|
* @return the current position after seek.
|
|
|
|
*/
|
2023-04-08 22:25:51 +08:00
|
|
|
int dfs_file_lseek(struct dfs_file *fd, off_t offset)
|
2011-10-08 22:50:08 +08:00
|
|
|
{
|
2012-12-26 09:12:13 +08:00
|
|
|
int result;
|
2011-10-08 22:50:08 +08:00
|
|
|
|
2017-10-15 22:44:53 +08:00
|
|
|
if (fd == NULL)
|
|
|
|
return -EINVAL;
|
2011-10-08 22:50:08 +08:00
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
if (fd->vnode->fops->lseek == NULL)
|
2017-10-15 22:44:53 +08:00
|
|
|
return -ENOSYS;
|
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
result = fd->vnode->fops->lseek(fd, offset);
|
2011-10-08 22:50:08 +08:00
|
|
|
|
2012-12-26 09:12:13 +08:00
|
|
|
/* update current position */
|
|
|
|
if (result >= 0)
|
|
|
|
fd->pos = result;
|
2011-10-08 22:50:08 +08:00
|
|
|
|
2012-12-26 09:12:13 +08:00
|
|
|
return result;
|
2011-10-08 22:50:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* this function will get file information.
|
|
|
|
*
|
|
|
|
* @param path the file path.
|
|
|
|
* @param buf the data buffer to save stat description.
|
|
|
|
*
|
|
|
|
* @return 0 on successful, -1 on failed.
|
|
|
|
*/
|
|
|
|
int dfs_file_stat(const char *path, struct stat *buf)
|
|
|
|
{
|
2012-12-26 09:12:13 +08:00
|
|
|
int result;
|
|
|
|
char *fullpath;
|
|
|
|
struct dfs_filesystem *fs;
|
|
|
|
|
2017-10-15 22:44:53 +08:00
|
|
|
fullpath = dfs_normalize_path(NULL, path);
|
|
|
|
if (fullpath == NULL)
|
2012-12-26 09:12:13 +08:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-10-15 22:44:53 +08:00
|
|
|
if ((fs = dfs_filesystem_lookup(fullpath)) == NULL)
|
2012-12-26 09:12:13 +08:00
|
|
|
{
|
2019-12-18 21:37:42 +08:00
|
|
|
LOG_E("can't find mounted filesystem on this path:%s", fullpath);
|
2012-12-26 09:12:13 +08:00
|
|
|
rt_free(fullpath);
|
|
|
|
|
2017-10-15 22:44:53 +08:00
|
|
|
return -ENOENT;
|
2012-12-26 09:12:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((fullpath[0] == '/' && fullpath[1] == '\0') ||
|
2017-10-15 22:44:53 +08:00
|
|
|
(dfs_subdir(fs->path, fullpath) == NULL))
|
2012-12-26 09:12:13 +08:00
|
|
|
{
|
|
|
|
/* it's the root directory */
|
|
|
|
buf->st_dev = 0;
|
|
|
|
|
2017-10-15 22:44:53 +08:00
|
|
|
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;
|
2012-12-26 09:12:13 +08:00
|
|
|
|
|
|
|
buf->st_size = 0;
|
|
|
|
buf->st_mtime = 0;
|
|
|
|
|
|
|
|
/* release full path */
|
|
|
|
rt_free(fullpath);
|
|
|
|
|
2017-10-15 22:44:53 +08:00
|
|
|
return RT_EOK;
|
2012-12-26 09:12:13 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-10-15 22:44:53 +08:00
|
|
|
if (fs->ops->stat == NULL)
|
2012-12-26 09:12:13 +08:00
|
|
|
{
|
|
|
|
rt_free(fullpath);
|
2019-12-18 21:37:42 +08:00
|
|
|
LOG_E("the filesystem didn't implement this function");
|
2012-12-26 09:12:13 +08:00
|
|
|
|
2017-10-15 22:44:53 +08:00
|
|
|
return -ENOSYS;
|
2012-12-26 09:12:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
rt_free(fullpath);
|
|
|
|
|
|
|
|
return result;
|
2011-10-08 22:50:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-12-31 12:05:40 +08:00
|
|
|
* this function will rename an old path name to a new path name.
|
2011-10-08 22:50:08 +08:00
|
|
|
*
|
|
|
|
* @param oldpath the old path name.
|
|
|
|
* @param newpath the new path name.
|
|
|
|
*
|
|
|
|
* @return 0 on successful, -1 on failed.
|
|
|
|
*/
|
2011-12-23 10:11:55 +08:00
|
|
|
int dfs_file_rename(const char *oldpath, const char *newpath)
|
2011-10-08 22:50:08 +08:00
|
|
|
{
|
2022-12-03 12:07:44 +08:00
|
|
|
int result = RT_EOK;
|
|
|
|
struct dfs_filesystem *oldfs = NULL, *newfs = NULL;
|
|
|
|
char *oldfullpath = NULL, *newfullpath = NULL;
|
2012-12-26 09:12:13 +08:00
|
|
|
|
2017-10-15 22:44:53 +08:00
|
|
|
newfullpath = NULL;
|
|
|
|
oldfullpath = NULL;
|
2012-12-26 09:12:13 +08:00
|
|
|
|
2017-10-15 22:44:53 +08:00
|
|
|
oldfullpath = dfs_normalize_path(NULL, oldpath);
|
|
|
|
if (oldfullpath == NULL)
|
2012-12-26 09:12:13 +08:00
|
|
|
{
|
2017-10-15 22:44:53 +08:00
|
|
|
result = -ENOENT;
|
2012-12-26 09:12:13 +08:00
|
|
|
goto __exit;
|
|
|
|
}
|
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
if (dfs_file_is_open((const char *)oldfullpath))
|
|
|
|
{
|
|
|
|
result = -EBUSY;
|
|
|
|
goto __exit;
|
|
|
|
}
|
|
|
|
|
2017-10-15 22:44:53 +08:00
|
|
|
newfullpath = dfs_normalize_path(NULL, newpath);
|
|
|
|
if (newfullpath == NULL)
|
2012-12-26 09:12:13 +08:00
|
|
|
{
|
2017-10-15 22:44:53 +08:00
|
|
|
result = -ENOENT;
|
2012-12-26 09:12:13 +08:00
|
|
|
goto __exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
oldfs = dfs_filesystem_lookup(oldfullpath);
|
|
|
|
newfs = dfs_filesystem_lookup(newfullpath);
|
|
|
|
|
|
|
|
if (oldfs == newfs)
|
|
|
|
{
|
2017-10-15 22:44:53 +08:00
|
|
|
if (oldfs->ops->rename == NULL)
|
2012-12-26 09:12:13 +08:00
|
|
|
{
|
2017-10-15 22:44:53 +08:00
|
|
|
result = -ENOSYS;
|
2012-12-26 09:12:13 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (oldfs->ops->flags & DFS_FS_FLAG_FULLPATH)
|
|
|
|
result = oldfs->ops->rename(oldfs, oldfullpath, newfullpath);
|
|
|
|
else
|
|
|
|
/* use sub directory to rename in file system */
|
|
|
|
result = oldfs->ops->rename(oldfs,
|
|
|
|
dfs_subdir(oldfs->path, oldfullpath),
|
|
|
|
dfs_subdir(newfs->path, newfullpath));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-10-15 22:44:53 +08:00
|
|
|
result = -EXDEV;
|
2012-12-26 09:12:13 +08:00
|
|
|
}
|
2011-10-08 22:50:08 +08:00
|
|
|
|
|
|
|
__exit:
|
2022-12-03 12:07:44 +08:00
|
|
|
if (oldfullpath)
|
|
|
|
{
|
|
|
|
rt_free(oldfullpath);
|
|
|
|
}
|
|
|
|
if (newfullpath)
|
|
|
|
{
|
|
|
|
rt_free(newfullpath);
|
|
|
|
}
|
2011-10-08 22:50:08 +08:00
|
|
|
|
2012-12-26 09:12:13 +08:00
|
|
|
/* not at same file system, return EXDEV */
|
|
|
|
return result;
|
2011-10-08 22:50:08 +08:00
|
|
|
}
|
|
|
|
|
2019-09-26 10:22:33 +08:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2023-04-08 22:25:51 +08:00
|
|
|
int dfs_file_ftruncate(struct dfs_file *fd, off_t length)
|
2019-09-26 10:22:33 +08:00
|
|
|
{
|
|
|
|
int result;
|
|
|
|
|
|
|
|
/* fd is null or not a regular file system fd, or length is invalid */
|
2022-12-03 12:07:44 +08:00
|
|
|
if (fd == NULL || fd->vnode->type != FT_REGULAR || length < 0)
|
2019-09-26 10:22:33 +08:00
|
|
|
return -EINVAL;
|
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
if (fd->vnode->fops->ioctl == NULL)
|
2019-09-26 10:22:33 +08:00
|
|
|
return -ENOSYS;
|
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
result = fd->vnode->fops->ioctl(fd, RT_FIOFTRUNCATE, (void*)&length);
|
2019-09-26 10:22:33 +08:00
|
|
|
|
|
|
|
/* update current size */
|
|
|
|
if (result == 0)
|
2022-12-03 12:07:44 +08:00
|
|
|
fd->vnode->size = length;
|
2019-09-26 10:22:33 +08:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-04-08 22:25:51 +08:00
|
|
|
int dfs_file_mmap2(struct dfs_file *fd, struct dfs_mmap2_args *mmap2)
|
2022-12-03 12:07:44 +08:00
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if (fd && mmap2)
|
|
|
|
{
|
|
|
|
if (fd->vnode->type != FT_DEVICE || !fd->vnode->fops->ioctl)
|
|
|
|
{
|
|
|
|
rt_set_errno(EINVAL);
|
|
|
|
}
|
|
|
|
else if (fd->vnode->type == FT_DEVICE && fd->vnode->fops->ioctl)
|
|
|
|
{
|
|
|
|
ret = fd->vnode->fops->ioctl(fd, RT_FIOMMAP2, mmap2);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
|
|
|
ret = ret > 0? ret : -ret;
|
|
|
|
rt_set_errno(ret);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-10-08 22:50:08 +08:00
|
|
|
#ifdef RT_USING_FINSH
|
|
|
|
#include <finsh.h>
|
|
|
|
|
2011-12-23 10:11:55 +08:00
|
|
|
void ls(const char *pathname)
|
2011-10-08 22:50:08 +08:00
|
|
|
{
|
2023-04-08 22:25:51 +08:00
|
|
|
struct dfs_file fd;
|
2022-12-03 12:07:44 +08:00
|
|
|
struct dirent dirent;
|
2012-12-26 09:12:13 +08:00
|
|
|
struct stat stat;
|
|
|
|
int length;
|
|
|
|
char *fullpath, *path;
|
2011-10-08 22:50:08 +08:00
|
|
|
|
2017-10-15 22:44:53 +08:00
|
|
|
fullpath = NULL;
|
|
|
|
if (pathname == NULL)
|
2012-12-26 09:12:13 +08:00
|
|
|
{
|
2011-10-08 22:50:08 +08:00
|
|
|
#ifdef DFS_USING_WORKDIR
|
2012-12-26 09:12:13 +08:00
|
|
|
/* open current working directory */
|
|
|
|
path = rt_strdup(working_directory);
|
2011-10-08 22:50:08 +08:00
|
|
|
#else
|
2012-12-26 09:12:13 +08:00
|
|
|
path = rt_strdup("/");
|
2011-10-08 22:50:08 +08:00
|
|
|
#endif
|
2017-10-15 22:44:53 +08:00
|
|
|
if (path == NULL)
|
2012-12-26 09:12:13 +08:00
|
|
|
return ; /* out of memory */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
path = (char *)pathname;
|
|
|
|
}
|
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
fd_init(&fd);
|
2012-12-26 09:12:13 +08:00
|
|
|
/* list directory */
|
2017-10-15 22:44:53 +08:00
|
|
|
if (dfs_file_open(&fd, path, O_DIRECTORY) == 0)
|
2012-12-26 09:12:13 +08:00
|
|
|
{
|
|
|
|
rt_kprintf("Directory %s:\n", path);
|
|
|
|
do
|
|
|
|
{
|
2021-12-31 08:38:20 +08:00
|
|
|
rt_memset(&dirent, 0, sizeof(struct dirent));
|
2012-12-26 09:12:13 +08:00
|
|
|
length = dfs_file_getdents(&fd, &dirent, sizeof(struct dirent));
|
|
|
|
if (length > 0)
|
|
|
|
{
|
2021-12-31 08:38:20 +08:00
|
|
|
rt_memset(&stat, 0, sizeof(struct stat));
|
2012-12-26 09:12:13 +08:00
|
|
|
|
|
|
|
/* build full path for each file */
|
|
|
|
fullpath = dfs_normalize_path(path, dirent.d_name);
|
2017-10-15 22:44:53 +08:00
|
|
|
if (fullpath == NULL)
|
2012-12-26 09:12:13 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
if (dfs_file_stat(fullpath, &stat) == 0)
|
|
|
|
{
|
|
|
|
rt_kprintf("%-20s", dirent.d_name);
|
2017-10-15 22:44:53 +08:00
|
|
|
if (S_ISDIR(stat.st_mode))
|
2012-12-26 09:12:13 +08:00
|
|
|
{
|
|
|
|
rt_kprintf("%-25s\n", "<DIR>");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-12-18 21:37:42 +08:00
|
|
|
rt_kprintf("%-25lu\n", (unsigned long)stat.st_size);
|
2012-12-26 09:12:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
rt_kprintf("BAD file: %s\n", dirent.d_name);
|
|
|
|
rt_free(fullpath);
|
|
|
|
}
|
2019-04-03 18:09:52 +08:00
|
|
|
}
|
|
|
|
while (length > 0);
|
2012-12-26 09:12:13 +08:00
|
|
|
|
|
|
|
dfs_file_close(&fd);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rt_kprintf("No such directory\n");
|
|
|
|
}
|
2017-10-15 22:44:53 +08:00
|
|
|
if (pathname == NULL)
|
2012-12-26 09:12:13 +08:00
|
|
|
rt_free(path);
|
2011-10-08 22:50:08 +08:00
|
|
|
}
|
2014-08-01 12:03:11 +08:00
|
|
|
FINSH_FUNCTION_EXPORT(ls, list directory contents);
|
2011-10-08 22:50:08 +08:00
|
|
|
|
2011-12-23 10:11:55 +08:00
|
|
|
void rm(const char *filename)
|
2011-10-08 22:50:08 +08:00
|
|
|
{
|
2012-12-26 09:12:13 +08:00
|
|
|
if (dfs_file_unlink(filename) < 0)
|
|
|
|
{
|
|
|
|
rt_kprintf("Delete %s failed\n", filename);
|
|
|
|
}
|
2011-10-08 22:50:08 +08:00
|
|
|
}
|
2014-08-01 12:03:11 +08:00
|
|
|
FINSH_FUNCTION_EXPORT(rm, remove files or directories);
|
2017-06-06 18:19:32 +08:00
|
|
|
|
2019-04-03 18:09:52 +08:00
|
|
|
void cat(const char *filename)
|
2011-10-08 22:50:08 +08:00
|
|
|
{
|
2023-04-08 22:25:51 +08:00
|
|
|
struct dfs_file fd;
|
2022-12-03 12:07:44 +08:00
|
|
|
int length = 0;
|
2012-12-26 09:12:13 +08:00
|
|
|
char buffer[81];
|
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
fd_init(&fd);
|
2017-10-15 22:44:53 +08:00
|
|
|
if (dfs_file_open(&fd, filename, O_RDONLY) < 0)
|
2012-12-26 09:12:13 +08:00
|
|
|
{
|
|
|
|
rt_kprintf("Open %s failed\n", filename);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2022-12-03 12:07:44 +08:00
|
|
|
rt_memset(buffer, 0x0, sizeof(buffer));
|
|
|
|
length = dfs_file_read(&fd, (void *)buffer, sizeof(buffer) - 1);
|
2012-12-26 09:12:13 +08:00
|
|
|
if (length > 0)
|
|
|
|
{
|
2022-12-03 12:07:44 +08:00
|
|
|
buffer[length] = '\0';
|
|
|
|
rt_device_t out_device = rt_console_get_device();
|
|
|
|
rt_device_write(out_device, 0, (void *)buffer, sizeof(buffer));
|
2012-12-26 09:12:13 +08:00
|
|
|
}
|
2022-12-03 12:07:44 +08:00
|
|
|
} while (length > 0);
|
2022-12-15 19:36:05 +08:00
|
|
|
rt_kprintf("\n");
|
2012-12-26 09:12:13 +08:00
|
|
|
|
|
|
|
dfs_file_close(&fd);
|
2011-10-08 22:50:08 +08:00
|
|
|
}
|
2014-08-01 12:03:11 +08:00
|
|
|
FINSH_FUNCTION_EXPORT(cat, print file);
|
2011-10-08 22:50:08 +08:00
|
|
|
|
2021-11-16 00:22:49 +08:00
|
|
|
#ifdef DFS_USING_POSIX
|
2012-12-26 09:12:13 +08:00
|
|
|
#define BUF_SZ 4096
|
2014-02-09 15:03:02 +08:00
|
|
|
static void copyfile(const char *src, const char *dst)
|
2011-10-08 22:50:08 +08:00
|
|
|
{
|
2023-04-08 22:25:51 +08:00
|
|
|
struct dfs_file fd;
|
|
|
|
struct dfs_file src_fd;
|
2012-12-26 09:12:13 +08:00
|
|
|
rt_uint8_t *block_ptr;
|
2013-07-06 16:41:10 +08:00
|
|
|
rt_int32_t read_bytes;
|
2012-12-26 09:12:13 +08:00
|
|
|
|
2019-06-18 20:09:19 +08:00
|
|
|
block_ptr = (rt_uint8_t *)rt_malloc(BUF_SZ);
|
2017-10-15 22:44:53 +08:00
|
|
|
if (block_ptr == NULL)
|
2012-12-26 09:12:13 +08:00
|
|
|
{
|
|
|
|
rt_kprintf("out of memory\n");
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
fd_init(&src_fd);
|
2017-10-15 22:44:53 +08:00
|
|
|
if (dfs_file_open(&src_fd, src, O_RDONLY) < 0)
|
2012-12-26 09:12:13 +08:00
|
|
|
{
|
|
|
|
rt_free(block_ptr);
|
|
|
|
rt_kprintf("Read %s failed\n", src);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2022-12-03 12:07:44 +08:00
|
|
|
fd_init(&fd);
|
2017-10-15 22:44:53 +08:00
|
|
|
if (dfs_file_open(&fd, dst, O_WRONLY | O_CREAT) < 0)
|
2012-12-26 09:12:13 +08:00
|
|
|
{
|
|
|
|
rt_free(block_ptr);
|
|
|
|
dfs_file_close(&src_fd);
|
|
|
|
|
|
|
|
rt_kprintf("Write %s failed\n", dst);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
read_bytes = dfs_file_read(&src_fd, block_ptr, BUF_SZ);
|
|
|
|
if (read_bytes > 0)
|
|
|
|
{
|
2015-06-04 22:23:53 +08:00
|
|
|
int length;
|
|
|
|
|
2014-08-01 12:03:11 +08:00
|
|
|
length = dfs_file_write(&fd, block_ptr, read_bytes);
|
2015-06-04 22:23:53 +08:00
|
|
|
if (length != read_bytes)
|
|
|
|
{
|
|
|
|
/* write failed. */
|
|
|
|
rt_kprintf("Write file data failed, errno=%d\n", length);
|
|
|
|
break;
|
|
|
|
}
|
2012-12-26 09:12:13 +08:00
|
|
|
}
|
2019-04-03 18:09:52 +08:00
|
|
|
}
|
|
|
|
while (read_bytes > 0);
|
2012-12-26 09:12:13 +08:00
|
|
|
|
|
|
|
dfs_file_close(&src_fd);
|
|
|
|
dfs_file_close(&fd);
|
|
|
|
rt_free(block_ptr);
|
2011-10-08 22:50:08 +08:00
|
|
|
}
|
2014-02-09 15:03:02 +08:00
|
|
|
|
|
|
|
extern int mkdir(const char *path, mode_t mode);
|
2019-04-03 18:09:52 +08:00
|
|
|
static void copydir(const char *src, const char *dst)
|
2014-02-09 15:03:02 +08:00
|
|
|
{
|
|
|
|
struct dirent dirent;
|
|
|
|
struct stat stat;
|
|
|
|
int length;
|
2023-04-08 22:25:51 +08:00
|
|
|
struct dfs_file cpfd;
|
2017-10-15 22:44:53 +08:00
|
|
|
if (dfs_file_open(&cpfd, src, O_DIRECTORY) < 0)
|
2014-02-09 15:03:02 +08:00
|
|
|
{
|
|
|
|
rt_kprintf("open %s failed\n", src);
|
|
|
|
return ;
|
|
|
|
}
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2021-12-31 08:38:20 +08:00
|
|
|
rt_memset(&dirent, 0, sizeof(struct dirent));
|
2017-10-15 22:44:53 +08:00
|
|
|
|
2017-04-12 10:53:53 +08:00
|
|
|
length = dfs_file_getdents(&cpfd, &dirent, sizeof(struct dirent));
|
2014-02-09 15:03:02 +08:00
|
|
|
if (length > 0)
|
|
|
|
{
|
2019-04-03 18:09:52 +08:00
|
|
|
char *src_entry_full = NULL;
|
|
|
|
char *dst_entry_full = NULL;
|
2014-02-09 15:03:02 +08:00
|
|
|
|
|
|
|
if (strcmp(dirent.d_name, "..") == 0 || strcmp(dirent.d_name, ".") == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* build full path for each file */
|
2017-10-15 22:44:53 +08:00
|
|
|
if ((src_entry_full = dfs_normalize_path(src, dirent.d_name)) == NULL)
|
2014-02-09 15:03:02 +08:00
|
|
|
{
|
|
|
|
rt_kprintf("out of memory!\n");
|
|
|
|
break;
|
|
|
|
}
|
2017-10-15 22:44:53 +08:00
|
|
|
if ((dst_entry_full = dfs_normalize_path(dst, dirent.d_name)) == NULL)
|
2014-02-09 15:03:02 +08:00
|
|
|
{
|
|
|
|
rt_kprintf("out of memory!\n");
|
|
|
|
rt_free(src_entry_full);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-12-31 08:38:20 +08:00
|
|
|
rt_memset(&stat, 0, sizeof(struct stat));
|
2014-02-09 15:03:02 +08:00
|
|
|
if (dfs_file_stat(src_entry_full, &stat) != 0)
|
|
|
|
{
|
|
|
|
rt_kprintf("open file: %s failed\n", dirent.d_name);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-10-15 22:44:53 +08:00
|
|
|
if (S_ISDIR(stat.st_mode))
|
2014-02-09 15:03:02 +08:00
|
|
|
{
|
|
|
|
mkdir(dst_entry_full, 0);
|
|
|
|
copydir(src_entry_full, dst_entry_full);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
copyfile(src_entry_full, dst_entry_full);
|
|
|
|
}
|
|
|
|
rt_free(src_entry_full);
|
|
|
|
rt_free(dst_entry_full);
|
|
|
|
}
|
2019-04-03 18:09:52 +08:00
|
|
|
}
|
|
|
|
while (length > 0);
|
2014-02-09 15:03:02 +08:00
|
|
|
|
2017-04-12 10:53:53 +08:00
|
|
|
dfs_file_close(&cpfd);
|
2014-02-09 15:03:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static const char *_get_path_lastname(const char *path)
|
|
|
|
{
|
2019-04-03 18:09:52 +08:00
|
|
|
char *ptr;
|
2019-06-18 20:09:19 +08:00
|
|
|
if ((ptr = (char *)strrchr(path, '/')) == NULL)
|
2014-02-09 15:03:02 +08:00
|
|
|
return path;
|
|
|
|
|
|
|
|
/* skip the '/' then return */
|
|
|
|
return ++ptr;
|
|
|
|
}
|
2021-10-21 09:44:01 +08:00
|
|
|
|
2014-02-09 15:03:02 +08:00
|
|
|
void copy(const char *src, const char *dst)
|
|
|
|
{
|
|
|
|
#define FLAG_SRC_TYPE 0x03
|
|
|
|
#define FLAG_SRC_IS_DIR 0x01
|
|
|
|
#define FLAG_SRC_IS_FILE 0x02
|
|
|
|
#define FLAG_SRC_NON_EXSIT 0x00
|
|
|
|
|
|
|
|
#define FLAG_DST_TYPE 0x0C
|
|
|
|
#define FLAG_DST_IS_DIR 0x04
|
|
|
|
#define FLAG_DST_IS_FILE 0x08
|
|
|
|
#define FLAG_DST_NON_EXSIT 0x00
|
|
|
|
|
|
|
|
struct stat stat;
|
2017-10-15 22:44:53 +08:00
|
|
|
uint32_t flag = 0;
|
2014-02-09 15:03:02 +08:00
|
|
|
|
|
|
|
/* check the staus of src and dst */
|
|
|
|
if (dfs_file_stat(src, &stat) < 0)
|
|
|
|
{
|
|
|
|
rt_kprintf("copy failed, bad %s\n", src);
|
|
|
|
return;
|
|
|
|
}
|
2017-10-15 22:44:53 +08:00
|
|
|
if (S_ISDIR(stat.st_mode))
|
2014-02-09 15:03:02 +08:00
|
|
|
flag |= FLAG_SRC_IS_DIR;
|
|
|
|
else
|
|
|
|
flag |= FLAG_SRC_IS_FILE;
|
|
|
|
|
|
|
|
if (dfs_file_stat(dst, &stat) < 0)
|
|
|
|
{
|
|
|
|
flag |= FLAG_DST_NON_EXSIT;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-10-15 22:44:53 +08:00
|
|
|
if (S_ISDIR(stat.st_mode))
|
2014-02-09 15:03:02 +08:00
|
|
|
flag |= FLAG_DST_IS_DIR;
|
|
|
|
else
|
|
|
|
flag |= FLAG_DST_IS_FILE;
|
|
|
|
}
|
|
|
|
|
|
|
|
//2. check status
|
|
|
|
if ((flag & FLAG_SRC_IS_DIR) && (flag & FLAG_DST_IS_FILE))
|
|
|
|
{
|
|
|
|
rt_kprintf("cp faild, cp dir to file is not permitted!\n");
|
|
|
|
return ;
|
|
|
|
}
|
|
|
|
|
|
|
|
//3. do copy
|
|
|
|
if (flag & FLAG_SRC_IS_FILE)
|
|
|
|
{
|
|
|
|
if (flag & FLAG_DST_IS_DIR)
|
|
|
|
{
|
2019-04-03 18:09:52 +08:00
|
|
|
char *fdst;
|
2014-02-09 15:03:02 +08:00
|
|
|
fdst = dfs_normalize_path(dst, _get_path_lastname(src));
|
|
|
|
if (fdst == NULL)
|
|
|
|
{
|
|
|
|
rt_kprintf("out of memory\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
copyfile(src, fdst);
|
|
|
|
rt_free(fdst);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
copyfile(src, dst);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else //flag & FLAG_SRC_IS_DIR
|
|
|
|
{
|
|
|
|
if (flag & FLAG_DST_IS_DIR)
|
|
|
|
{
|
2019-04-03 18:09:52 +08:00
|
|
|
char *fdst;
|
2014-02-09 15:03:02 +08:00
|
|
|
fdst = dfs_normalize_path(dst, _get_path_lastname(src));
|
|
|
|
if (fdst == NULL)
|
|
|
|
{
|
|
|
|
rt_kprintf("out of memory\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mkdir(fdst, 0);
|
|
|
|
copydir(src, fdst);
|
|
|
|
rt_free(fdst);
|
|
|
|
}
|
|
|
|
else if ((flag & FLAG_DST_TYPE) == FLAG_DST_NON_EXSIT)
|
|
|
|
{
|
|
|
|
mkdir(dst, 0);
|
|
|
|
copydir(src, dst);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
copydir(src, dst);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
FINSH_FUNCTION_EXPORT(copy, copy file or dir)
|
2021-11-16 00:22:49 +08:00
|
|
|
#endif /* DFS_USING_POSIX */
|
2011-10-08 22:50:08 +08:00
|
|
|
|
2021-10-21 09:44:01 +08:00
|
|
|
#endif /* RT_USING_FINSH */
|
2023-04-15 00:33:43 +08:00
|
|
|
/**@}*/
|
2011-10-08 22:50:08 +08:00
|
|
|
|