24#define DBG_LVL DBG_INFO
31#ifdef DFS_USING_WORKDIR
42static int _fdt_slot_expand(
struct dfs_fdtable *fdt,
int fd)
69 for (index = fdt->
maxfd; index < nr; index++)
79static int _fdt_slot_alloc(
struct dfs_fdtable *fdt,
int startfd)
84 for (idx = startfd; idx < (int)fdt->
maxfd; idx++)
98 if (_fdt_slot_expand(fdt, idx) < 0)
106static int _fdt_fd_alloc(
struct dfs_fdtable *fdt,
int startfd)
110 idx = _fdt_slot_alloc(fdt, startfd);
124 while (result == -RT_EBUSY)
156 while (result == -RT_EBUSY)
176 LOG_E(
"DFS was already initialized.\n");
214 LOG_E(
"DFS fd new is failed! Could not found an empty fd entry.");
216 else if (!fdt->
fds[idx])
227 fdt->
fds[idx] = file;
229 LOG_D(
"allocate a new fd @ %d", idx);
239 LOG_E(
"DFS not found an empty fds entry.");
290 if (fd < 0 || fd >= (
int)fdt->
maxfd)
325 if ((fd < 0) || (fd >= fdt->
maxfd))
383 struct rt_lwp *lwp = NULL;
388 lwp = (
struct rt_lwp *)thread->lwp;
403struct dfs_fdtable *dfs_fdtable_get_from_pid(
int pid)
409 lwp = lwp_from_pid_locked(pid);
414 lwp_pid_lock_release();
456 if ((fd_src < 0) || (fd_src >= fdt_src->
maxfd))
460 if (!fdt_src->
fds[fd_src])
538 if ((oldfd < 0) || (oldfd >= fdt->
maxfd))
540 rt_set_errno(-EBADF);
543 if (!fdt->
fds[oldfd])
548 newfd = _fdt_slot_alloc(fdt, startfd);
551 fdt->
fds[newfd] = fdt->
fds[oldfd];
587 if ((oldfd < 0) || (oldfd >= fdt->
maxfd))
591 if (!fdt->
fds[oldfd])
599 fdtab->
fds[newfd] = fdt->
fds[oldfd];
635 if ((oldfd < 0) || (oldfd >= fdtab->
maxfd))
639 if (!fdtab->
fds[oldfd])
646 if (newfd >= 0 && file)
676 err = rt_get_errno();
680 return err < 0 ? err : newfd;
682 return err < 0 ? err : newfd;
699 if ((oldfd < 0) || (oldfd >= fdt->
maxfd))
703 if (!fdt->
fds[oldfd])
711 if (newfd >= fdt->
maxfd)
713 newfd = _fdt_slot_expand(fdt, newfd);
719 if (fdt->
fds[newfd] == fdt->
fds[oldfd])
736 fdt->
fds[newfd] = fdt->
fds[oldfd];
753const char *
dfs_subdir(
const char *directory,
const char *filename)
757 if (strlen(directory) == strlen(filename))
760 dir = filename + strlen(directory);
761 if ((*dir !=
'/') && (dir != filename))
782 char *dst0, *dst, *src;
787#ifdef DFS_USING_WORKDIR
788 if (directory == NULL)
791 directory = lwp_getcwd();
797 if ((directory == NULL) && (filename[0] !=
'/'))
805 if (filename[0] !=
'/')
809 path_len = strlen(directory) + strlen(filename) + 2;
816 if (fullpath == NULL)
822 rt_snprintf(fullpath, strlen(directory) + strlen(filename) + 2,
823 "%s/%s", directory, filename);
827 fullpath = rt_strdup(filename);
829 if (fullpath == NULL)
845 else if (src[1] ==
'/')
850 while ((*src ==
'/') && (*src !=
'\0'))
854 else if (src[1] ==
'.')
862 else if (src[2] ==
'/')
867 while ((*src ==
'/') && (*src !=
'\0'))
875 while ((c = *src++) !=
'\0' && c !=
'/')
898 while (dst0 < dst && dst[-1] !=
'/')
906 if (dst > fullpath && (*dst ==
'/'))
910 if (
'\0' == fullpath[0])
928 if (!fd_table)
return -1;
934 for (index = 0; index < (int)fd_table->
maxfd; index++)
938 if (file && file->
vnode)
975 for (index = 0; index < _fdtab.maxfd; index++)
977 struct dfs_file *file = _fdtab.fds[index];
983 printf(
"[%d] - %s, ref_count %zd\n", index,
989 printf(
"[%d] - %s, ref_count %zd\n", index,
1000#ifdef PKG_USING_DLOG
1002int dfs_dlog(
int argc,
char** argv)
1006 if (strcmp(argv[1],
"on") == 0)
1008 dlog_session_start();
1009 dlog_participant(
"dfs");
1010 dlog_participant(
"dfs_file");
1011 dlog_participant(
"dentry");
1012 dlog_participant(
"vnode");
1013 dlog_participant(
"mnt");
1014 dlog_participant(
"rom");
1015 dlog_participant(
"devfs");
1017 else if (strcmp(argv[1],
"off") == 0)
1019 dlog_session_stop();
int dfs_dentry_init(void)
char * dfs_dentry_full_path(struct dfs_dentry *dentry)
struct dfs_dentry * dfs_dentry_ref(struct dfs_dentry *dentry)
int dfs_file_close(struct dfs_file *file)
struct dfs_fdtable * dfs_fdtable_get_global(void)
int dfs_dup(int oldfd, int startfd)
char * dfs_normalize_path(const char *directory, const char *filename)
int dfs_dup_to(int oldfd, struct dfs_fdtable *fdtab)
The fd in the current process dup to designate fd table.
int dfs_fd_dump(int argc, char **argv)
void dfs_file_unlock(void)
int fdt_fd_associate_file(struct dfs_fdtable *fdt, int fd, struct dfs_file *file)
int dfs_fdtable_dup(struct dfs_fdtable *fdt_dst, struct dfs_fdtable *fdt_src, int fd_src)
Dup the specified fd_src from fdt_src to fdt_dst.
void fdt_fd_release(struct dfs_fdtable *fdt, int fd)
const char * dfs_subdir(const char *directory, const char *filename)
rt_err_t dfs_file_lock(void)
struct dfs_file * fd_get(int fd)
struct dfs_fdtable * dfs_fdtable_get(void)
rt_err_t sys_dup2(int oldfd, int newfd)
int dfs_dup_from(int oldfd, struct dfs_fdtable *fdtab)
The fd in the designate fd table dup to current process.
int dfs_fdtable_drop_fd(struct dfs_fdtable *fdt, int fd)
drop fd from the fd table.
struct dfs_file * fdt_get_file(struct dfs_fdtable *fdt, int fd)
int fdt_fd_new(struct dfs_fdtable *fdt)
#define RT_WAITING_FOREVER
rt_weak void * rt_calloc(rt_size_t count, rt_size_t size)
This function will contiguously allocate enough space for count objects that are size bytes of memory...
rt_weak void * rt_realloc(void *ptr, rt_size_t newsize)
This function will change the size of previously allocated memory block.
rt_weak void rt_free(void *ptr)
This function will release the previously allocated memory block by rt_malloc. The released memory bl...
rt_weak void * rt_malloc(rt_size_t size)
Allocate a block of memory with a minimum of 'size' bytes.
void rt_exit_critical(void)
This function will unlock the thread scheduler.
rt_thread_t rt_thread_self(void)
This function will return self thread object.
rt_base_t rt_enter_critical(void)
This function will lock the thread scheduler.
struct rt_thread * rt_thread_t
#define MSH_CMD_EXPORT_ALIAS(...)
#define MSH_CMD_EXPORT(...)
rt_err_t rt_mutex_take(rt_mutex_t mutex, rt_int32_t time)
rt_err_t rt_mutex_detach(rt_mutex_t mutex)
This function will detach a static mutex object.
rt_err_t rt_mutex_init(rt_mutex_t mutex, const char *name, rt_uint8_t flag)
Initialize a static mutex object.
rt_err_t rt_mutex_release(rt_mutex_t mutex)
This function will release a mutex. If there is thread suspended on the mutex, the thread will be res...
#define rt_atomic_sub(ptr, v)
#define rt_atomic_add(ptr, v)
#define rt_atomic_load(ptr)
#define INIT_PREV_EXPORT(fn)
#define RTM_EXPORT(symbol)
struct dfs_dentry * dentry
const struct dfs_file_ops * fops