add flush, statfs, mkfs to device file system interface.

git-svn-id: https://rt-thread.googlecode.com/svn/trunk@802 bbd45198-f89e-11dd-88c7-29a3b14d5316
This commit is contained in:
bernard.xiong@gmail.com 2010-07-18 16:15:53 +00:00
parent fd12462d57
commit 33feb176d0
12 changed files with 462 additions and 1594 deletions

View File

@ -4,11 +4,10 @@ Import('RTT_ROOT')
Import('projects') Import('projects')
dfs = Split(""" dfs = Split("""
src/dfs.c
src/dfs_fs.c src/dfs_fs.c
src/dfs_init.c src/dfs_file.c
src/dfs_posix.c src/dfs_posix.c
src/dfs_raw.c
src/dfs_util.c
""") """)
# DFS-FatFs options # DFS-FatFs options

View File

@ -1,20 +1,18 @@
/* /*
+------------------------------------------------------------------------------ * File : dfs_def.h
| Device FileSystem * This file is part of Device File System in RT-Thread RTOS
+------------------------------------------------------------------------------ * COPYRIGHT (C) 2004-2010, RT-Thread Development Team
| Copyright 2004, 2005 www.fayfayspace.org. *
| All rights reserved. * The license and distribution terms for this file may be
|------------------------------------------------------------------------------ * found in the file LICENSE in this distribution or at
| File : dfs_def.h, the definitions of Device FileSystem * http://www.rt-thread.org/license/LICENSE.
|------------------------------------------------------------------------------ *
| Chang Logs: * Change Logs:
| Date Author notes * Date Author Notes
| 2004-10-01 ffxz The first version. * 2004-10-01 Beranard The first version.
| 2004-10-14 ffxz Clean up the code. * 2004-10-14 Beranard Clean up the code.
| 2005-01-22 ffxz Clean up the code, port to MinGW * 2005-01-22 Beranard Clean up the code, port to MinGW
+------------------------------------------------------------------------------ */
*/
#ifndef __DFS_DEF_H__ #ifndef __DFS_DEF_H__
#define __DFS_DEF_H__ #define __DFS_DEF_H__
@ -113,15 +111,6 @@
#define DEVICE_FORMAT 2 #define DEVICE_FORMAT 2
#define DEVICE_CLEAN_SECTOR 3 #define DEVICE_CLEAN_SECTOR 3
struct device_geometry
{
rt_uint32_t sector_count; /* count of sectors */
rt_uint32_t cylinder_count; /* count of cylinder */
rt_uint32_t sectors_per_track; /* number of sectors per track */
rt_uint32_t head_count; /* count of head */
rt_uint32_t bytes_per_sector; /* number of bytes per sector */
};
struct dfs_stat struct dfs_stat
{ {
rt_device_t st_dev; rt_device_t st_dev;
@ -130,7 +119,13 @@ struct dfs_stat
rt_time_t st_mtime; rt_time_t st_mtime;
rt_uint32_t st_blksize; rt_uint32_t st_blksize;
}; };
#define stat dfs_stat
struct dfs_statfs
{
rt_size_t f_bsize; /* block size */
rt_size_t f_blocks; /* total data blocks in file system */
rt_size_t f_bfree; /* free blocks in file system */
};
/* File types */ /* File types */
#define FT_REGULAR 0 /* regular file */ #define FT_REGULAR 0 /* regular file */
@ -141,7 +136,7 @@ struct dfs_stat
/* file descriptor */ /* file descriptor */
struct dfs_fd struct dfs_fd
{ {
char path[DFS_PATH_MAX + 1];/* Name (below mount point) */ char* path; /* Name (below mount point) */
int type; /* Type (regular or socket) */ int type; /* Type (regular or socket) */
int ref_count; /* Descriptor reference count */ int ref_count; /* Descriptor reference count */
@ -162,14 +157,9 @@ struct dfs_dirent
{ {
rt_uint8_t d_type; /* The type of the file */ rt_uint8_t d_type; /* The type of the file */
rt_uint8_t d_namlen; /* The length of the not including the terminating null file name */ rt_uint8_t d_namlen; /* The length of the not including the terminating null file name */
rt_uint16_t d_reclen; /* length of this record */ rt_uint16_t d_reclen; /* length of this record */
char d_name[256]; /* The null-terminated file name */ char d_name[DFS_PATH_MAX]; /* The null-terminated file name */
}; };
#define dirent dfs_dirent #define dirent dfs_dirent
struct dfs_session
{
rt_mailbox_t mbox;
};
#endif #endif

View File

@ -1,29 +0,0 @@
/*
+------------------------------------------------------------------------------
| Project : Device Filesystem
+------------------------------------------------------------------------------
| Copyright 2004, 2005 www.fayfayspace.org.
| All rights reserved.
|------------------------------------------------------------------------------
| File : dfs_fat.h, FAT 12/16/32 file system definitions
|------------------------------------------------------------------------------
| Chang Logs:
| Date Author Notes
| 2003-03-30 ffxz
+------------------------------------------------------------------------------
*/
#ifndef __DFS_FAT_H__
#define __DFS_FAT_H__
#ifdef __cplusplus
extern "C" {
#endif
int fatfs_init(void);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -1,17 +1,16 @@
/* /*
+------------------------------------------------------------------------------ * File : dfs_fs.h
| Project : Device Filesystem * This file is part of Device File System in RT-Thread RTOS
+------------------------------------------------------------------------------ * COPYRIGHT (C) 2004-2010, RT-Thread Development Team
| Copyright 2004, 2005 www.fayfayspace.org. *
| All rights reserved. * The license and distribution terms for this file may be
|------------------------------------------------------------------------------ * found in the file LICENSE in this distribution or at
| File : dfs_fs.h, the filesystem related defines of Device FileSystem * http://www.rt-thread.org/license/LICENSE.
|------------------------------------------------------------------------------ *
| Chang Logs: * Change Logs:
| Date Author Notes * Date Author Notes
| 2005-02-22 ffxz The first version. * 2005-02-22 Bernard The first version.
+------------------------------------------------------------------------------ */
*/
#ifndef __DFS_FS_H__ #ifndef __DFS_FS_H__
#define __DFS_FS_H__ #define __DFS_FS_H__
@ -26,16 +25,22 @@ struct dfs_dirent;
/* File system operations struct */ /* File system operations struct */
struct dfs_filesystem_operation struct dfs_filesystem_operation
{ {
char name[DFS_FS_NAME_MAX + 1]; char *name;
/* mount and unmount file system */
int (*mount) (struct dfs_filesystem* fs, unsigned long rwflag, const void* data); int (*mount) (struct dfs_filesystem* fs, unsigned long rwflag, const void* data);
int (*unmount) (struct dfs_filesystem* fs); int (*unmount) (struct dfs_filesystem* fs);
int (*open) (struct dfs_fd* fd); /* make a file system */
int (*mkfs) (const char* device_name);
int (*statfs) (struct dfs_filesystem* fs, struct dfs_statfs *buf);
int (*open) (struct dfs_fd* fd);
int (*close) (struct dfs_fd* fd); int (*close) (struct dfs_fd* fd);
int (*ioctl) (struct dfs_fd* fd, int cmd, void *args); int (*ioctl) (struct dfs_fd* fd, int cmd, void *args);
int (*read) (struct dfs_fd* fd, void* buf, rt_size_t count); int (*read) (struct dfs_fd* fd, void* buf, rt_size_t count);
int (*write) (struct dfs_fd* fd, const void* buf, rt_size_t count); int (*write) (struct dfs_fd* fd, const void* buf, rt_size_t count);
int (*flush) (struct dfs_fd* fd);
int (*lseek) (struct dfs_fd* fd, rt_off_t offset); int (*lseek) (struct dfs_fd* fd, rt_off_t offset);
int (*getdents) (struct dfs_fd* fd, struct dfs_dirent* dirp, rt_uint32_t count); int (*getdents) (struct dfs_fd* fd, struct dfs_dirent* dirp, rt_uint32_t count);
@ -47,13 +52,12 @@ struct dfs_filesystem_operation
/* Mounted file system */ /* Mounted file system */
struct dfs_filesystem struct dfs_filesystem
{ {
rt_device_t dev_id; /* Attached device */ rt_device_t dev_id; /* Attached device */
char path[DFS_PATH_MAX + 1]; /* File system mount point */ char* path; /* File system mount point */
struct dfs_filesystem_operation* ops; /* Operations for file system type */ const struct dfs_filesystem_operation* ops; /* Operations for file system type */
rt_uint32_t block_id; /* Current block_id on attached device */
void *data; /* Specific file system data */ void *data; /* Specific file system data */
}; };
/* file system partition table */ /* file system partition table */
@ -65,7 +69,7 @@ struct dfs_partition
rt_sem_t lock; rt_sem_t lock;
}; };
int dfs_register(struct dfs_filesystem_operation* ops); int dfs_register(const struct dfs_filesystem_operation* ops);
struct dfs_filesystem* dfs_filesystem_lookup(const char *path); struct dfs_filesystem* dfs_filesystem_lookup(const char *path);
rt_err_t dfs_filesystem_get_partition(struct dfs_partition* part, rt_uint8_t* buf, rt_uint32_t pindex); rt_err_t dfs_filesystem_get_partition(struct dfs_partition* part, rt_uint8_t* buf, rt_uint32_t pindex);
@ -75,12 +79,13 @@ int dfs_mount(const char* device_name, const char* path,
int dfs_unmount(const char *specialfile); int dfs_unmount(const char *specialfile);
/* extern variable */ /* extern variable */
extern struct dfs_filesystem_operation* filesystem_operation_table[]; extern const struct dfs_filesystem_operation* filesystem_operation_table[];
extern struct dfs_filesystem filesystem_table[]; extern struct dfs_filesystem filesystem_table[];
extern char working_directory[]; extern char working_directory[];
void dfs_lock(void); void dfs_lock(void);
void dfs_unlock(void); void dfs_unlock(void);
int dfs_statfs(const char* path, struct dfs_statfs* buffer);
#endif #endif

View File

@ -1,21 +1,21 @@
/* /*
+------------------------------------------------------------------------------ * File : dfs_def.h
| Project : Device Filesystem * This file is part of Device File System in RT-Thread RTOS
+------------------------------------------------------------------------------ * COPYRIGHT (C) 2004-2010, RT-Thread Development Team
| Copyright 2004, 2005 www.fayfayspace.org. *
| All rights reserved. * The license and distribution terms for this file may be
|------------------------------------------------------------------------------ * found in the file LICENSE in this distribution or at
| File : dfs_posix.h, the filesystem related defines of Device FileSystem * http://www.rt-thread.org/license/LICENSE.
|------------------------------------------------------------------------------ *
| Chang Logs: * Change Logs:
| Date Author Notes * Date Author Notes
| 2009-05-27 Yi.qiu The first version. * 2009-05-27 Yi.qiu The first version.
+------------------------------------------------------------------------------ * 2010-07-18 Bernard add stat and statfs structure definitions.
*/ */
#ifndef __DFS_POSIX_H__ #ifndef __DFS_POSIX_H__
#define __DFS_POSIX_H__ #define __DFS_POSIX_H__
#include <dfs_raw.h> #include <dfs_file.h>
#define O_RDONLY DFS_O_RDONLY #define O_RDONLY DFS_O_RDONLY
#define O_WRONLY DFS_O_WRONLY #define O_WRONLY DFS_O_WRONLY
@ -74,6 +74,15 @@ typedef struct
int cur; int cur;
} DIR; } DIR;
struct stat
{
struct dfs_stat parent;
};
struct statfs
{
struct dfs_statfs parent;
};
/* file api*/ /* file api*/
int open(const char *file, int flags, int mode); int open(const char *file, int flags, int mode);
int close(int d); int close(int d);
@ -83,6 +92,7 @@ int lseek(int fd, int offset, int dir);
int rename(const char* old, const char* new ); int rename(const char* old, const char* new );
int unlink(const char *pathname); int unlink(const char *pathname);
int stat(const char *file, struct dfs_stat *buf); int stat(const char *file, struct dfs_stat *buf);
int statfs(const char *path, struct dfs_statfs *buf);
/* directory api*/ /* directory api*/
int mkdir (const char *path, rt_uint16_t mode); int mkdir (const char *path, rt_uint16_t mode);

View File

@ -1,38 +0,0 @@
/*
+------------------------------------------------------------------------------
| Project : Device Filesystem
+------------------------------------------------------------------------------
| Copyright 2004, 2005 www.fayfayspace.org.
| All rights reserved.
|------------------------------------------------------------------------------
| File : dfs_raw.h, the raw APIs of Device FileSystem
|------------------------------------------------------------------------------
| Chang Logs:
| Date Author Notes
| 2005-01-26 ffxz The first version
+------------------------------------------------------------------------------
*/
#ifndef __DFS_RAW_H__
#define __DFS_RAW_H__
#include <dfs_def.h>
#include <dfs_fs.h>
int dfile_raw_open(struct dfs_fd* fd, const char *path, int flags);
int dfile_raw_close(struct dfs_fd* fd);
int dfile_raw_ioctl(struct dfs_fd* fd, int cmd, void *args);
int dfile_raw_read(struct dfs_fd* fd, void *buf, rt_size_t len);
int dfile_raw_getdents(struct dfs_fd* fd, struct dfs_dirent* dirp, rt_size_t nbytes);
int dfile_raw_unlink(const char *path);
int dfile_raw_write(struct dfs_fd* fd, const void *buf, rt_size_t len);
int dfile_raw_lseek(struct dfs_fd* fd, rt_off_t offset);
int dfile_raw_stat(const char *path, struct dfs_stat *buf);
int dfile_raw_rename(const char* oldpath, const char* newpath);
/* FD APIs */
int fd_new(void);
struct dfs_fd* fd_get(int fd);
void fd_put(struct dfs_fd* fd);
#endif

View File

@ -1,36 +0,0 @@
/*
+------------------------------------------------------------------------------
| Project : Device Filesystem
+------------------------------------------------------------------------------
| Copyright 2004, 2005 www.fayfayspace.org.
| All rights reserved.
|------------------------------------------------------------------------------
| File : dfs_util.h, some misc definitions of Device FileSystem
|------------------------------------------------------------------------------
| Chang Logs:
| Date Author Notes
| 2005-01-26 ffxz The first version
+------------------------------------------------------------------------------
*/
#ifndef __DFS_UTIL_H__
#define __DFS_UTIL_H__
#include <dfs_def.h>
int dir_name(const char* path, char* dirname, int len);
int file_name(const char* path, char* filename, int len);
int next_dir_name(const char* path, int pos, char* next);
void build_fullpath(const char *directory, const char *filename, char *fullpath);
int str_is_prefix(const char* prefix, const char* str);
#if !defined(RT_USING_MINILIBC) && !defined(RT_USING_NEWLIB)
char *strrchr(const char *t, int c);
#if defined (__ARMCC_VERSION) && (__ARMCC_VERSION / 10000 < 35)
#include <stddef.h>
int strncasecmp(const char* s1, const char* s2, size_t len);
#endif /* end of __ARMCC_VERSION */
#endif
#endif

View File

@ -1,36 +1,28 @@
/* /*
+------------------------------------------------------------------------------ * File : dfs_fs.c
| Project : Device Filesystem * This file is part of Device File System in RT-Thread RTOS
+------------------------------------------------------------------------------ * COPYRIGHT (C) 2004-2010, RT-Thread Development Team
| Copyright 2004, 2005 www.fayfayspace.org. *
| All rights reserved. * The license and distribution terms for this file may be
|------------------------------------------------------------------------------ * found in the file LICENSE in this distribution or at
| File : dfs_fs.c, the filesystem related implementations of Device FileSystem * http://www.rt-thread.org/license/LICENSE.
|------------------------------------------------------------------------------ *
| Chang Logs: * Change Logs:
| Date Author Notes * Date Author Notes
| 2005-02-22 ffxz The first version. * 2005-02-22 Bernard The first version.
+------------------------------------------------------------------------------ * 2010-06-30 Bernard Optimize for RT-Thread RTOS
*/ */
#include <dfs_fs.h> #include <dfs_fs.h>
#include <dfs_raw.h> #include <dfs_file.h>
#include <dfs_util.h>
#include <string.h> /**
* this function will register a file system instance to device file system.
/* *
+------------------------------------------------------------------------------ * @param ops the file system instance to be registered.
| Function : dfs_register *
+------------------------------------------------------------------------------ * @return 0 on sucessful, -1 on failed.
| Description : registers a filesystem */
| int dfs_register(const struct dfs_filesystem_operation* ops)
| Parameters : ops, the implementation of filesystem
| Returns : the status of register
| 0 , successful
| -1, failed
+------------------------------------------------------------------------------
*/
int dfs_register(struct dfs_filesystem_operation* ops)
{ {
int index, result; int index, result;
@ -69,16 +61,14 @@ err:
return result; return result;
} }
/* /**
+------------------------------------------------------------------------------ * this function will return the file system mounted on specified path.
| Function : dfs_filesystem_lookup *
+------------------------------------------------------------------------------ * @param path the specified path string.
| Description : lookup the mounted filesystem on the specified path *
| * @return the found file system or NULL if no file system mounted on
| Parameters : path, the specified path string * specified path
| Returns : the found filesystem */
+------------------------------------------------------------------------------
*/
struct dfs_filesystem* dfs_filesystem_lookup(const char *path) struct dfs_filesystem* dfs_filesystem_lookup(const char *path)
{ {
struct dfs_filesystem* fs; struct dfs_filesystem* fs;
@ -97,7 +87,7 @@ struct dfs_filesystem* dfs_filesystem_lookup(const char *path)
if (fspath < prefixlen) continue; if (fspath < prefixlen) continue;
if ((filesystem_table[index].ops != RT_NULL) && if ((filesystem_table[index].ops != RT_NULL) &&
str_is_prefix(filesystem_table[index].path, path) == 0) strncmp(filesystem_table[index].path, path, fspath) == 0)
{ {
fs = &filesystem_table[index]; fs = &filesystem_table[index];
prefixlen = fspath; prefixlen = fspath;
@ -109,6 +99,15 @@ struct dfs_filesystem* dfs_filesystem_lookup(const char *path)
return fs; return fs;
} }
/**
* this function will fetch the partition table on specified buffer.
*
* @param part the returned partition structure.
* @param buf the buffer contains partition table.
* @param pindex the index of partition table to fetch.
*
* @return RT_EOK on successful or -RT_ERROR on failed.
*/
rt_err_t dfs_filesystem_get_partition(struct dfs_partition* part, rt_uint8_t* buf, rt_uint32_t pindex) rt_err_t dfs_filesystem_get_partition(struct dfs_partition* part, rt_uint8_t* buf, rt_uint32_t pindex)
{ {
#define DPT_ADDRESS 0x1be /* device partition offset in Boot Sector */ #define DPT_ADDRESS 0x1be /* device partition offset in Boot Sector */
@ -146,8 +145,7 @@ rt_err_t dfs_filesystem_get_partition(struct dfs_partition* part, rt_uint8_t* bu
part->size = *(dpt+12) | *(dpt+13) << 8 | part->size = *(dpt+12) | *(dpt+13) << 8 |
*(dpt+14) << 16 | *(dpt+15) << 24; *(dpt+14) << 16 | *(dpt+15) << 24;
#ifdef RT_USING_FINSH rt_kprintf("found part[%d], begin: %d, size: ",
rt_kprintf("part[%d], begin: %d, size: ",
pindex, part->offset * 512); pindex, part->offset * 512);
if ( (part->size>>11) > 0 ) /* MB */ if ( (part->size>>11) > 0 ) /* MB */
{ {
@ -166,7 +164,6 @@ rt_err_t dfs_filesystem_get_partition(struct dfs_partition* part, rt_uint8_t* bu
{ {
rt_kprintf("%d%s",part->size>>1,"KB\r\n");/* KB */ rt_kprintf("%d%s",part->size>>1,"KB\r\n");/* KB */
} }
#endif
} }
else else
{ {
@ -176,32 +173,23 @@ rt_err_t dfs_filesystem_get_partition(struct dfs_partition* part, rt_uint8_t* bu
return result; return result;
} }
/* /**
+------------------------------------------------------------------------------ * this function will mount a file system on a specified path.
| Function : dfs_mount *
+------------------------------------------------------------------------------ * @param device_name the name of device which includes a file system.
| Description : mount a filesystem to specified path * @param filesystemtype the file system type
| * @param rwflag the read/write etc. flag.
| Parameters : device_name, the implementation of filesystem * @param data the privated data(parameter) for this file system.
| path, *
| filesystemtype, * @return 0 on successful or -1 on failed.
| rwflag, */
| data,
| Returns : the status of register
| 0 , successful
| -1, failed
+------------------------------------------------------------------------------
*/
int dfs_mount(const char* device_name, const char* path, int dfs_mount(const char* device_name, const char* path,
const char* filesystemtype, unsigned long rwflag, const const char* filesystemtype, unsigned long rwflag, const
void* data) void* data)
{ {
struct dfs_filesystem_operation* ops; const struct dfs_filesystem_operation* ops;
struct dfs_filesystem* fs; struct dfs_filesystem* fs;
char *fullpath=RT_NULL; char *fullpath=RT_NULL;
#ifdef DFS_USING_WORKDIR
char full_path[DFS_PATH_MAX + 1];
#endif
rt_device_t dev_id; rt_device_t dev_id;
int index; int index;
@ -240,19 +228,11 @@ int dfs_mount(const char* device_name, const char* path,
dfs_unlock(); dfs_unlock();
/* make full path for special file */ /* make full path for special file */
fullpath = (char*)path; fullpath = dfs_normalize_path(RT_NULL, path);
if ( fullpath[0] != '/') /* not an abstract path */ if ( fullpath == RT_NULL) /* not an abstract path */
{ {
#ifdef DFS_USING_WORKDIR
/* build full path */
fullpath = full_path;
dfs_lock();
build_fullpath(working_directory, path, fullpath);
dfs_unlock();
#else
rt_set_errno(-DFS_STATUS_ENOTDIR); rt_set_errno(-DFS_STATUS_ENOTDIR);
return -1; return -1;
#endif
} }
/* Check if the path exists or not, raw APIs call, fixme */ /* Check if the path exists or not, raw APIs call, fixme */
@ -260,12 +240,13 @@ int dfs_mount(const char* device_name, const char* path,
{ {
struct dfs_fd fd; struct dfs_fd fd;
if ( dfile_raw_open(&fd, fullpath, DFS_O_RDONLY | DFS_O_DIRECTORY) < 0 ) if ( dfs_file_open(&fd, fullpath, DFS_O_RDONLY | DFS_O_DIRECTORY) < 0 )
{ {
rt_free(fullpath);
rt_set_errno(-DFS_STATUS_ENOTDIR); rt_set_errno(-DFS_STATUS_ENOTDIR);
return -1; return -1;
} }
dfile_raw_close(&fd); dfs_file_close(&fd);
} }
/* check whether the file system mounted or not */ /* check whether the file system mounted or not */
@ -291,7 +272,7 @@ int dfs_mount(const char* device_name, const char* path,
/* register file system */ /* register file system */
fs = &(filesystem_table[index]); fs = &(filesystem_table[index]);
strncpy(fs->path, fullpath, strlen(fullpath)); fs->path = fullpath;
fs->ops = ops; fs->ops = ops;
fs->dev_id = dev_id; fs->dev_id = dev_id;
/* release filesystem_table lock */ /* release filesystem_table lock */
@ -308,6 +289,7 @@ int dfs_mount(const char* device_name, const char* path,
rt_memset(fs, 0, sizeof(struct dfs_filesystem)); rt_memset(fs, 0, sizeof(struct dfs_filesystem));
dfs_unlock(); dfs_unlock();
rt_free(fullpath);
rt_set_errno(-DFS_STATUS_ENOSYS); rt_set_errno(-DFS_STATUS_ENOSYS);
return -1; return -1;
} }
@ -323,6 +305,7 @@ int dfs_mount(const char* device_name, const char* path,
rt_memset(fs, 0, sizeof(struct dfs_filesystem)); rt_memset(fs, 0, sizeof(struct dfs_filesystem));
dfs_unlock(); dfs_unlock();
rt_free(fullpath);
return -1; return -1;
} }
@ -330,42 +313,28 @@ int dfs_mount(const char* device_name, const char* path,
err1: err1:
dfs_unlock(); dfs_unlock();
if (fullpath != RT_NULL) rt_free(fullpath);
return -1; return -1;
} }
/* /**
+------------------------------------------------------------------------------ * this function will umount a file system on specified path.
| Function : dfs_unmount *
+------------------------------------------------------------------------------ * @param specialfile the specified path which mounted a file system.
| Description : unmount a filesystem *
| * @return 0 on successful or -1 on failed.
| Parameters : */
| Returns : the status of register
| 0 , successful
| -1, failed
+------------------------------------------------------------------------------
*/
int dfs_unmount(const char *specialfile) int dfs_unmount(const char *specialfile)
{ {
char *fullpath; char *fullpath;
#ifdef DFS_USING_WORKDIR
char full_path[DFS_PATH_MAX + 1];
#endif
struct dfs_filesystem* fs = RT_NULL; struct dfs_filesystem* fs = RT_NULL;
fullpath = (char*)specialfile; fullpath = dfs_normalize_path(RT_NULL, specialfile);
if ( fullpath[0] != '/') if (fullpath == RT_NULL)
{ {
#ifdef DFS_USING_WORKDIR
/* build full path */
fullpath = full_path;
dfs_lock();
build_fullpath(working_directory, specialfile, fullpath);
dfs_unlock();
#else
rt_set_errno(-DFS_STATUS_ENOTDIR); rt_set_errno(-DFS_STATUS_ENOTDIR);
return -1; return -1;
#endif
} }
/* lock filesystem */ /* lock filesystem */
@ -385,11 +354,91 @@ int dfs_unmount(const char *specialfile)
rt_memset(fs, 0, sizeof(struct dfs_filesystem)); rt_memset(fs, 0, sizeof(struct dfs_filesystem));
dfs_unlock(); dfs_unlock();
rt_free(fullpath);
return 0; return 0;
err1: err1:
dfs_unlock(); dfs_unlock();
rt_free(fullpath);
return -1; return -1;
} }
/**
* make a file system on the special device
*
* @param fs_name, the file system name
* @param device_name, the special device name
*
* @return 0 on successful, otherwise failed.
*/
int dfs_mkfs(const char* fs_name, const char* device_name)
{
int index;
/* lock filesystem */
dfs_lock();
/* find the file system operations */
for (index = 0; index < DFS_FILESYSTEM_TYPES_MAX; index++)
{
if (filesystem_operation_table[index] != RT_NULL &&
strcmp(filesystem_operation_table[index]->name, fs_name) == 0)
{
/* find file system operation */
const struct dfs_filesystem_operation* ops = filesystem_operation_table[index];
dfs_unlock();
if (ops->mkfs != RT_NULL)
return ops->mkfs(device_name);
break;
}
}
dfs_unlock();
return -1;
}
/**
* this function will return the information about a mounted file system.
*
* @param path the path which mounted file system.
* @param buffer the buffer to save the returned information.
*
* @return 0 on successful, others on failed.
*/
int dfs_statfs(const char* path, struct dfs_statfs* buffer)
{
struct dfs_filesystem* fs;
fs = dfs_filesystem_lookup(path);
if (fs != NULL)
{
if (fs->ops->statfs!= RT_NULL)
return fs->ops->statfs(fs, buffer);
}
return -1;
}
#ifdef RT_USING_FINSH
#include <finsh.h>
void mkfs(const char* fs_name, const char* device_name)
{
dfs_mkfs(fs_name, device_name);
}
FINSH_FUNCTION_EXPORT(mkfs, make a file system);
void df(const char* path)
{
struct dfs_statfs buffer;
if (dfs_statfs(path, &buffer) == 0)
{
rt_kprintf("disk free: %d block[%d bytes per block]\n", buffer.f_bfree, buffer.f_bsize);
}
}
FINSH_FUNCTION_EXPORT(df, get disk free);
#endif

View File

@ -1,84 +0,0 @@
/*
+------------------------------------------------------------------------------
| Project : Device Filesystem
+------------------------------------------------------------------------------
| Copyright 2004, 2005 www.fayfayspace.org.
| All rights reserved.
|------------------------------------------------------------------------------
| File : dfs.c, the implementation of Device FileSystem
|------------------------------------------------------------------------------
| Chang Logs:
| Date Author Notes
| 2002-12-31 ffxz The first version.
| 2005-01-22 ffxz Clean up the code.
+------------------------------------------------------------------------------
*/
#include <dfs_def.h>
#include <dfs_config.h>
#include <dfs_fs.h>
#include <dfs_raw.h>
#include <string.h>
/* Global variables */
struct dfs_filesystem_operation* filesystem_operation_table[DFS_FILESYSTEM_TYPES_MAX + 1];
struct dfs_filesystem filesystem_table[DFS_FILESYSTEMS_MAX + 1];
/* device filesystem lock */
struct rt_mutex dlock;
#ifdef DFS_USING_WORKDIR
char working_directory[DFS_PATH_MAX + 1];
#endif
#ifdef DFS_USING_STDIO
struct dfs_fd fd_table[3 + DFS_FD_MAX];
#else
struct dfs_fd fd_table[DFS_FD_MAX];
#endif
/*
+------------------------------------------------------------------------------
| Function : dfs_init
+------------------------------------------------------------------------------
| Description : Inits the Device Filesystem
| Parameters : null
| Returns : null
+------------------------------------------------------------------------------
*/
void dfs_init()
{
int index;
/* clear filesystem operations table */
for (index = 0; index < DFS_FILESYSTEM_TYPES_MAX + 1; index++)
filesystem_operation_table[index] = RT_NULL;
/* create device filesystem lock */
rt_mutex_init(&dlock, "dlock", RT_IPC_FLAG_FIFO);
/* clear filesystem table */
rt_memset(filesystem_table, 0, sizeof(filesystem_table));
#ifdef DFS_USING_WORKDIR
/* set current working directory */
strcpy(working_directory, "/");
#endif
/* clean fd table */
rt_memset(fd_table, 0, sizeof(fd_table));
}
void dfs_lock()
{
rt_err_t result;
result = rt_mutex_take(&dlock, RT_WAITING_FOREVER);
RT_ASSERT(result == RT_EOK);
}
void dfs_unlock()
{
rt_mutex_release(&dlock);
}

View File

@ -1,32 +1,30 @@
/* /*
+------------------------------------------------------------------------------ * File : dfs_posix.c
| Project : Device Filesystem * This file is part of Device File System in RT-Thread RTOS
+------------------------------------------------------------------------------ * COPYRIGHT (C) 2004-2010, RT-Thread Development Team
| Copyright 2004, 2005 www.fayfayspace.org. *
| All rights reserved. * The license and distribution terms for this file may be
|------------------------------------------------------------------------------ * found in the file LICENSE in this distribution or at
| File : dfs_posix.c, the interface related implementations of Device FileSystem * http://www.rt-thread.org/license/LICENSE.
|------------------------------------------------------------------------------ *
| Chang Logs: * Change Logs:
| Date Author Notes * Date Author Notes
| 2009-05-27 Yi.qiu The first version. * 2009-05-27 Yi.qiu The first version
+------------------------------------------------------------------------------ */
*/
#include <string.h> #include <dfs.h>
#include <dfs_util.h>
#include <dfs_posix.h> #include <dfs_posix.h>
/* /**
+------------------------------------------------------------------------------ * this function is a POSIX compliant version, which will open a file and return
| Function : open * a file descriptor.
+------------------------------------------------------------------------------ *
| Description : * @param file the path name of file.
| * @param flags the file open flags.
| Parameters : * @param mode
| Returns : *
| * @return the non-negative integer on successful open, others for failed.
+------------------------------------------------------------------------------ */
*/
int open(const char *file, int flags, int mode) int open(const char *file, int flags, int mode)
{ {
int fd, result; int fd, result;
@ -37,10 +35,12 @@ int open(const char *file, int flags, int mode)
if (fd < 0) return -1; if (fd < 0) return -1;
d = fd_get(fd); d = fd_get(fd);
result = dfile_raw_open(d, file, flags); result = dfs_file_open(d, file, flags);
if (result < 0) if (result < 0)
{ {
rt_set_errno(result); rt_set_errno(result);
/* release the ref-count of fd */
fd_put(d); fd_put(d);
fd_put(d); fd_put(d);
@ -52,17 +52,14 @@ int open(const char *file, int flags, int mode)
return fd; return fd;
} }
/* /**
+------------------------------------------------------------------------------ * this function is a POSIX compliant version, which will close the open
| Function : close * file descriptor.
+------------------------------------------------------------------------------ *
| Description : * @param fd the file descriptor.
| *
| Parameters : * @return 0 on successful, -1 on failed.
| Returns : */
|
+------------------------------------------------------------------------------
*/
int close(int fd) int close(int fd)
{ {
int result; int result;
@ -75,8 +72,7 @@ int close(int fd)
return -1; return -1;
} }
result = dfile_raw_close(d); result = dfs_file_close(d);
fd_put(d);
fd_put(d); fd_put(d);
if (result < 0) if (result < 0)
@ -84,20 +80,21 @@ int close(int fd)
rt_set_errno(result); rt_set_errno(result);
return -1; return -1;
} }
fd_put(d);
return 0; return 0;
} }
/* /**
+------------------------------------------------------------------------------ * this function is a POSIX compliant version, which will read specified data buffer
| Function : read * length for an open file descriptor.
+------------------------------------------------------------------------------ *
| Description : * @param fd the file descriptor.
| * @param buf the buffer to save the read data.
| Parameters : * @param len the maximal length of data buffer
| Returns : *
| * @return the actual read data buffer length
+------------------------------------------------------------------------------ */
*/
int read(int fd, char *buf, int len) int read(int fd, char *buf, int len)
{ {
int result; int result;
@ -111,7 +108,7 @@ int read(int fd, char *buf, int len)
return -1; return -1;
} }
result = dfile_raw_read(d, buf, len); result = dfs_file_read(d, buf, len);
if (result < 0) if (result < 0)
{ {
rt_set_errno(result); rt_set_errno(result);
@ -125,17 +122,16 @@ int read(int fd, char *buf, int len)
return result; return result;
} }
/* /**
+------------------------------------------------------------------------------ * this function is a POSIX compliant version, which will write pecified data buffer
| Function : write * length for an open file descriptor.
+------------------------------------------------------------------------------ *
| Description : * @param fd the file descriptor
| * @param buf the data buffer to be written.
| Parameters : * @param len the data buffer length.
| Returns : *
| * @return the actual written data buffer length.
+------------------------------------------------------------------------------ */
*/
int write(int fd, char *buf, int len) int write(int fd, char *buf, int len)
{ {
int result; int result;
@ -149,7 +145,7 @@ int write(int fd, char *buf, int len)
return -1; return -1;
} }
result = dfile_raw_write(d, buf, len); result = dfs_file_write(d, buf, len);
if (result < 0) if (result < 0)
{ {
rt_set_errno(result); rt_set_errno(result);
@ -163,17 +159,16 @@ int write(int fd, char *buf, int len)
return result; return result;
} }
/* /**
+------------------------------------------------------------------------------ * this function is a POSIX compliant version, which will seek the offset for an
| Function : lseek * open file descriptor.
+------------------------------------------------------------------------------ *
| Description : * @param fd the file descriptor.
| * @param offset the offset to be seeked.
| Parameters : * @param dir the directory of seek.
| Returns : *
| * @return the current file position, or -1 on failed.
+------------------------------------------------------------------------------ */
*/
int lseek(int fd, int offset, int dir) int lseek(int fd, int offset, int dir)
{ {
int result; int result;
@ -200,7 +195,7 @@ int lseek(int fd, int offset, int dir)
break; break;
} }
result = dfile_raw_lseek(d, offset); result = dfs_file_lseek(d, offset);
if (result < 0) if (result < 0)
{ {
rt_set_errno(result); rt_set_errno(result);
@ -213,22 +208,22 @@ int lseek(int fd, int offset, int dir)
return offset; return offset;
} }
/* /**
+------------------------------------------------------------------------------ * this function is a POSIX compliant version, which will rename old file name to
| Function : rename * new file name.
+------------------------------------------------------------------------------ *
| Description : * @param old the old file name.
| * @param new the new file name.
| Parameters : *
| Returns : * @return 0 on successful, -1 on failed.
| *
+------------------------------------------------------------------------------ * 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, const char* new)
{ {
int result; int result;
result = dfile_raw_rename(old, new); result = dfs_file_rename(old, new);
if (result < 0) if (result < 0)
{ {
rt_set_errno(result); rt_set_errno(result);
@ -237,22 +232,19 @@ int rename(const char* old, const char* new )
return 0; return 0;
} }
/* /**
+------------------------------------------------------------------------------ * this function is a POSIX compliant version, which will unlink (remove) a
| Function : unlink * specified path file from file system.
+------------------------------------------------------------------------------ *
| Description : * @param pathname the specified path name to be unlinked.
| *
| Parameters : * @return 0 on successful, -1 on failed.
| Returns : */
|
+------------------------------------------------------------------------------
*/
int unlink(const char *pathname) int unlink(const char *pathname)
{ {
int result; int result;
result = dfile_raw_unlink(pathname); result = dfs_file_unlink(pathname);
if (result < 0) if (result < 0)
{ {
rt_set_errno(result); rt_set_errno(result);
@ -261,22 +253,19 @@ int unlink(const char *pathname)
return 0; return 0;
} }
/* /**
+------------------------------------------------------------------------------ * this function is a POSIX compliant version, which will get file information.
| Function : stat *
+------------------------------------------------------------------------------ * @param file the file name
| Description : * @param buf the the data buffer to save stat description.
| *
| Parameters : * @return 0 on successful, -1 on failed.
| Returns : */
|
+------------------------------------------------------------------------------
*/
int stat(const char *file, struct dfs_stat *buf) int stat(const char *file, struct dfs_stat *buf)
{ {
int result; int result;
result = dfile_raw_stat(file, (struct dfs_stat *)buf); result = dfs_file_stat(file, buf);
if (result < 0) if (result < 0)
{ {
rt_set_errno(result); rt_set_errno(result);
@ -285,17 +274,37 @@ int stat(const char *file, struct dfs_stat *buf)
return result; return result;
} }
/* /**
+------------------------------------------------------------------------------ * this function is a POSIX compliant version, which will return the
| Function : mkdir * information about a mounted file system.
+------------------------------------------------------------------------------ *
| Description : * @param path the path which mounted file system.
| * @param buf the buffer to save the returned information.
| Parameters : *
| Returns : * @return 0 on successful, others on failed.
| */
+------------------------------------------------------------------------------ int statfs(const char *path, struct dfs_statfs *buf)
*/ {
int result;
result = dfs_statfs(path, buf);
if (result < 0)
{
rt_set_errno(result);
return -1;
}
return result;
}
/**
* this function is a POSIX compliant version, which will make a directory
*
* @param path the directory path to be made.
* @param mode
*
* @return 0 on successful, others on failed.
*/
int mkdir (const char *path, rt_uint16_t mode) int mkdir (const char *path, rt_uint16_t mode)
{ {
int fd; int fd;
@ -305,9 +314,7 @@ int mkdir (const char *path, rt_uint16_t mode)
fd = fd_new(); fd = fd_new();
d = fd_get(fd); d = fd_get(fd);
result = dfile_raw_open(d, path, DFS_O_DIRECTORY | DFS_O_CREAT); result = dfs_file_open(d, path, DFS_O_DIRECTORY | DFS_O_CREAT);
fd_put(d);
fd_put(d);
if (result < 0) if (result < 0)
{ {
@ -315,25 +322,23 @@ int mkdir (const char *path, rt_uint16_t mode)
return -1; return -1;
} }
fd_put(d);
fd_put(d);
return 0; return 0;
} }
/* /**
+------------------------------------------------------------------------------ * this function is a POSIX compliant version, which will remove a directory.
| Function : rmdir *
+------------------------------------------------------------------------------ * @param pathname the path name to be removed.
| Description : *
| * @return 0 on sucessfull, others on failed.
| Parameters : */
| Returns :
|
+------------------------------------------------------------------------------
*/
int rmdir(const char *pathname) int rmdir(const char *pathname)
{ {
int result; int result;
result = dfile_raw_unlink(pathname); result = dfs_file_unlink(pathname);
if (result < 0) if (result < 0)
{ {
rt_set_errno(result); rt_set_errno(result);
@ -343,17 +348,13 @@ int rmdir(const char *pathname)
return 0; return 0;
} }
/* /**
+------------------------------------------------------------------------------ * this function is a POSIX compliant version, which will open a directory.
| Function : opendir *
+------------------------------------------------------------------------------ * @param name the path name to be open.
| Description : *
| * @return the DIR pointer of directory, NULL on open failed.
| Parameters : */
| Returns :
|
+------------------------------------------------------------------------------
*/
DIR* opendir(const char* name) DIR* opendir(const char* name)
{ {
struct dfs_fd* d; struct dfs_fd* d;
@ -367,14 +368,14 @@ DIR* opendir(const char* name)
if (fd == -1) { rt_kprintf("no fd\n"); return RT_NULL; } if (fd == -1) { rt_kprintf("no fd\n"); return RT_NULL; }
d = fd_get(fd); d = fd_get(fd);
result = dfile_raw_open(d, name, DFS_O_RDONLY | DFS_O_DIRECTORY); result = dfs_file_open(d, name, DFS_O_RDONLY | DFS_O_DIRECTORY);
if (result >= 0) if (result >= 0)
{ {
/* open successfully */ /* open successfully */
t = (DIR *) rt_malloc (sizeof(DIR)); t = (DIR *) rt_malloc (sizeof(DIR));
if (t == RT_NULL) if (t == RT_NULL)
{ {
dfile_raw_close(d); dfs_file_close(d);
fd_put(d); fd_put(d);
} }
else else
@ -394,17 +395,15 @@ DIR* opendir(const char* name)
return RT_NULL; return RT_NULL;
} }
/* /**
+------------------------------------------------------------------------------ * this function is a POSIX compliant version, which will return a pointer
| Function : readdir * to a dirent structure representing the next directory entry in the
+------------------------------------------------------------------------------ * directory stream.
| Description : *
| * @param d the directory stream pointer.
| Parameters : *
| Returns : * @return the next directory entry, NULL on the end of directory or failed.
| */
+------------------------------------------------------------------------------
*/
struct dfs_dirent* readdir(DIR *d) struct dfs_dirent* readdir(DIR *d)
{ {
int result; int result;
@ -419,7 +418,7 @@ struct dfs_dirent* readdir(DIR *d)
if (!d->num || (d->cur += ((struct dfs_dirent*)(d->buf + d->cur))->d_reclen) >= d->num) if (!d->num || (d->cur += ((struct dfs_dirent*)(d->buf + d->cur))->d_reclen) >= d->num)
{ {
result = dfile_raw_getdents(fd, (struct dfs_dirent*)d->buf, sizeof(d->buf) - 1); result = dfs_file_getdents(fd, (struct dfs_dirent*)d->buf, sizeof(d->buf) - 1);
if (result <= 0) if (result <= 0)
{ {
rt_set_errno(result); rt_set_errno(result);
@ -436,17 +435,14 @@ struct dfs_dirent* readdir(DIR *d)
return (struct dfs_dirent*)(d->buf+d->cur); return (struct dfs_dirent*)(d->buf+d->cur);
} }
/* /**
+------------------------------------------------------------------------------ * this function is a POSIX compliant version, which will return current
| Function : telldir * location in directory stream.
+------------------------------------------------------------------------------ *
| Description : * @param d the directory stream pointer.
| *
| Parameters : * @return the current location in directory stream.
| Returns : */
|
+------------------------------------------------------------------------------
*/
rt_off_t telldir(DIR *d) rt_off_t telldir(DIR *d)
{ {
struct dfs_fd* fd; struct dfs_fd* fd;
@ -465,17 +461,13 @@ rt_off_t telldir(DIR *d)
return result; return result;
} }
/* /**
+------------------------------------------------------------------------------ * this function is a POSIX compliant version, which will set position of
| Function : seekdir * next directory structure in the directory stream.
+------------------------------------------------------------------------------ *
| Description : * @param d the directory stream.
| * @param offset the offset in directory stream.
| Parameters : */
| Returns :
|
+------------------------------------------------------------------------------
*/
void seekdir(DIR *d, rt_off_t offset) void seekdir(DIR *d, rt_off_t offset)
{ {
struct dfs_fd* fd; struct dfs_fd* fd;
@ -487,21 +479,15 @@ void seekdir(DIR *d, rt_off_t offset)
return ; return ;
} }
if (dfile_raw_lseek(fd, offset) >= 0) d->num = d->cur = 0; if (dfs_file_lseek(fd, offset) >= 0) d->num = d->cur = 0;
fd_put(fd); fd_put(fd);
} }
/* /**
+------------------------------------------------------------------------------ * this function is a POSIX compliant version, which will reset directory stream.
| Function : rewinddir *
+------------------------------------------------------------------------------ * @param d the directory stream.
| Description : */
|
| Parameters :
| Returns :
|
+------------------------------------------------------------------------------
*/
void rewinddir(DIR *d) void rewinddir(DIR *d)
{ {
struct dfs_fd* fd; struct dfs_fd* fd;
@ -513,21 +499,18 @@ void rewinddir(DIR *d)
return ; return ;
} }
if (dfile_raw_lseek(fd, 0) >= 0) d->num = d->cur = 0; if (dfs_file_lseek(fd, 0) >= 0) d->num = d->cur = 0;
fd_put(fd); fd_put(fd);
} }
/* /**
+------------------------------------------------------------------------------ * this function is a POSIX compliant version, which will close a directory
| Function : closedir * stream.
+------------------------------------------------------------------------------ *
| Description : * @param d the directory stream.
| *
| Parameters : * @return 0 on successful, -1 on failed.
| Returns : */
|
+------------------------------------------------------------------------------
*/
int closedir(DIR* d) int closedir(DIR* d)
{ {
int result; int result;
@ -540,7 +523,7 @@ int closedir(DIR* d)
return -1; return -1;
} }
result = dfile_raw_close(fd); result = dfs_file_close(fd);
fd_put(fd); fd_put(fd);
fd_put(fd); fd_put(fd);
@ -554,60 +537,58 @@ int closedir(DIR* d)
else return 0; else return 0;
} }
/* #ifdef DFS_USING_WORKDIR
+------------------------------------------------------------------------------ /**
| Function : chdir * this function is a POSIX compliant version, which will change working directory.
+------------------------------------------------------------------------------ *
| Description : * @param path the path name to be changed to.
| *
| Parameters : * @return 0 on successful, -1 on failed.
| Returns : */
|
+------------------------------------------------------------------------------
*/
int chdir(const char *path) int chdir(const char *path)
{ {
char* fullpath, full_path[DFS_PATH_MAX + 1]; char* fullpath;
DIR* d;
if(path == RT_NULL || rt_strlen(path) > DFS_PATH_MAX) if(path == RT_NULL || rt_strlen(path) > DFS_PATH_MAX)
return -1; return -1;
fullpath = (char*)path; fullpath = dfs_normalize_path(NULL, path);
if ( fullpath[0] != '/' ) if (fullpath == RT_NULL)
return -1; /* build path failed */
dfs_lock();
d = opendir(fullpath);
if (d == RT_NULL)
{ {
/* build full path */ rt_free(fullpath);
fullpath = full_path; /* this is a not exist directory */
#ifdef DFS_USING_WORKDIR
dfs_lock();
build_fullpath(working_directory, path, fullpath);
strcpy(working_directory, fullpath);
dfs_unlock(); dfs_unlock();
#endif return -1;
}
else
{
#ifdef DFS_USING_WORKDIR
dfs_lock();
rt_strncpy(working_directory, path, strlen(path) + 1);
working_directory[strlen(path)] = '\0';
dfs_unlock();
#endif
} }
/* close directory stream */
closedir(d);
/* copy full path to working directory */
strncpy(working_directory, fullpath, DFS_PATH_MAX);
rt_free(fullpath); /* release normalize directory path name */
dfs_unlock();
return 0; return 0;
} }
#endif
/* /**
+------------------------------------------------------------------------------ * this function is a POSIX compliant version, which will return current
| Function : chdir * working directory.
+------------------------------------------------------------------------------ *
| Description : * @param buf the returned current directory.
| * @size the buffer size.
| Parameters : *
| Returns : * @return the returned current directory.
| */
+------------------------------------------------------------------------------
*/
char* getcwd(char *buf, rt_size_t size) char* getcwd(char *buf, rt_size_t size)
{ {
#ifdef DFS_USING_WORKDIR #ifdef DFS_USING_WORKDIR

View File

@ -1,684 +0,0 @@
/*
+------------------------------------------------------------------------------
| Project : Device Filesystem
+------------------------------------------------------------------------------
| Copyright 2004, 2005 www.fayfayspace.org.
| All rights reserved.
|------------------------------------------------------------------------------
| File : dfs_raw.c, the raw APIs of Device FileSystem
|------------------------------------------------------------------------------
| Chang Logs:
| Date Author Notes
| 2005-02-22 ffxz The first version.
+------------------------------------------------------------------------------
*/
#include <dfs_raw.h>
#include <dfs_util.h>
#include <string.h>
extern struct dfs_fd fd_table[];
#define NO_WORKING_DIR "system does not support working dir\n"
/*
+------------------------------------------------------------------------------
| Function : fd_new
+------------------------------------------------------------------------------
| Description :
|
| Parameters :
| Returns :
|
+------------------------------------------------------------------------------
*/
int fd_new(void)
{
struct dfs_fd* d;
int idx;
/* lock filesystem */
dfs_lock();
/* find an empty fd entry */
#ifdef DFS_USING_STDIO
for (idx = 3; idx < DFS_FD_MAX + 3 && fd_table[idx].ref_count > 0; idx++);
#else
for (idx = 0; idx < DFS_FD_MAX && fd_table[idx].ref_count > 0; idx++);
#endif
/* can't find an empty fd entry */
#ifdef DFS_USING_STDIO
if (idx == DFS_FD_MAX + 3)
#else
if (idx == DFS_FD_MAX)
#endif
{
idx = -1;
goto __result;
}
d = &(fd_table[idx]);
d->ref_count = 1;
__result:
dfs_unlock();
return idx;
}
/*
+------------------------------------------------------------------------------
| Function : fd_get
+------------------------------------------------------------------------------
| Description :
|
| Parameters :
| Returns :
|
+------------------------------------------------------------------------------
*/
struct dfs_fd* fd_get(int fd)
{
struct dfs_fd* d;
#ifdef DFS_USING_STDIO
if ( fd < 3 || fd > DFS_FD_MAX + 3) return RT_NULL;
#else
if ( fd < 0 || fd > DFS_FD_MAX ) return RT_NULL;
#endif
d = &fd_table[fd];
dfs_lock();
d->ref_count ++;
dfs_unlock();
return d;
}
/*
+------------------------------------------------------------------------------
| Function : fd_put
+------------------------------------------------------------------------------
| Description :
|
| Parameters :
| Returns :
|
+------------------------------------------------------------------------------
*/
void fd_put(struct dfs_fd* fd)
{
dfs_lock();
fd->ref_count --;
/* clear this fd entry */
if ( fd->ref_count == 0 )
{
rt_memset(fd, 0, sizeof(struct dfs_fd));
}
dfs_unlock();
};
/*
+------------------------------------------------------------------------------
| Function : dfile_raw_open
+------------------------------------------------------------------------------
| Description :
|
| Parameters :
| Returns :
|
+------------------------------------------------------------------------------
*/
int dfile_raw_open(struct dfs_fd* fd, const char *path, int flags)
{
struct dfs_filesystem* fs;
char *fullpath;
#ifdef DFS_USING_WORKDIR
char full_path[DFS_PATH_MAX + 1];
#endif
int fspathlen, result;
/* parameter check */
if ( fd == RT_NULL ) return -DFS_STATUS_EINVAL;
/* make sure we have an absolute path */
fullpath = (char*)path;
if ( fullpath[0] != '/')
{
#ifdef DFS_USING_WORKDIR
/* build full path */
fullpath = &full_path[0];
dfs_lock();
build_fullpath(working_directory, path, fullpath);
dfs_unlock();
#else
rt_kprintf(NO_WORKING_DIR);
return -1;
#endif
}
dfs_log(DFS_DEBUG_INFO, ("open file:%s", fullpath));
/* find filesystem */
fs = dfs_filesystem_lookup(fullpath);
if ( fs == RT_NULL ) return -DFS_STATUS_ENOENT;
dfs_log(DFS_DEBUG_INFO, ("open in filesystem:%s", fs->ops->name));
fd->fs = fs;
/* initilize the fd item */
fd->type = FT_REGULAR;
//fd->ref_count = 1;
fd->flags = flags;
fd->size = 0;
fd->pos = 0;
fspathlen = strlen(fs->path);
rt_memset(fd->path, 0, DFS_PATH_MAX + 1);
if (*(fullpath + fspathlen) != '/') strcpy(fd->path, "/");
strcat(fd->path, fullpath + fspathlen);
/* specific file system open routine */
if (fs->ops->open == RT_NULL)
{
/* clear fd */
rt_memset(fd->path, 0, DFS_PATH_MAX + 1);
fd->type = FT_REGULAR;
fd->ref_count = 0;
fd->fs = RT_NULL;
fd->flags = 0;
fd->size = 0;
fd->pos = 0;
fd->data = RT_NULL;
return -DFS_STATUS_ENOSYS;
}
if ((result = fs->ops->open(fd)) < 0)
{
/* clear fd */
fd->fs = RT_NULL;
fd->flags = 0;
fd->data = RT_NULL;
dfs_log(DFS_DEBUG_INFO, ("open failed"));
return result;
}
fd->flags |= DFS_F_OPEN;
if ( flags & DFS_O_DIRECTORY )
{
fd->type = FT_DIRECTORY;
fd->flags |= DFS_F_DIRECTORY;
}
dfs_log(DFS_DEBUG_INFO, ("open successful"));
return 0;
}
/*
+------------------------------------------------------------------------------
| Function : dfile_raw_close
+------------------------------------------------------------------------------
| Description :
|
| Parameters :
| Returns :
|
+------------------------------------------------------------------------------
*/
int dfile_raw_close(struct dfs_fd* fd)
{
int result = 0;
if (fd != RT_NULL && fd->fs->ops->close != RT_NULL) result = fd->fs->ops->close(fd);
/* close fd error, return */
if ( result < 0 ) return result;
fd->flags &= ~DFS_F_OPEN;
return result;
}
/*
+------------------------------------------------------------------------------
| Function : dfile_raw_ioctl
+------------------------------------------------------------------------------
| Description :
|
| Parameters :
| Returns :
|
+------------------------------------------------------------------------------
*/
int dfile_raw_ioctl(struct dfs_fd* fd, int cmd, void *args)
{
struct dfs_filesystem* fs;
if (fd == RT_NULL || fd->type != FT_REGULAR) return -DFS_STATUS_EINVAL;
fs = fd->fs;
#if 0 /* not support right now */
if (cmd == FILEGETSIZE)
{
if (args == RT_NULL) return -DFS_STATUS_EINVAL;
*((rt_uint32_t *) args) = fd->size;
return 0;
}
else if (cmd == FILEGETPOS)
{
if (args == RT_NULL) return -DFS_STATUS_EINVAL;
*((rt_uint32_t *) args) = fd->pos;
return 0;
}
#endif
if (fs->ops->ioctl != RT_NULL) return fs->ops->ioctl(fd, cmd, args);
return -DFS_STATUS_ENOSYS;
}
/*
+------------------------------------------------------------------------------
| Function : dfile_raw_read
+------------------------------------------------------------------------------
| Description :
|
| Parameters :
| Returns :
|
+------------------------------------------------------------------------------
*/
int dfile_raw_read(struct dfs_fd* fd, void *buf, rt_size_t len)
{
struct dfs_filesystem* fs;
int result = 0;
if (fd == RT_NULL) return -DFS_STATUS_EINVAL;
fs = (struct dfs_filesystem*) fd->fs;
if (fs->ops->read == RT_NULL) return -DFS_STATUS_ENOSYS;
if ( (result = fs->ops->read(fd, buf, len)) < 0 ) fd->flags |= DFS_F_EOF;
return result;
}
/*
+------------------------------------------------------------------------------
| Function : dfile_raw_getdents
+------------------------------------------------------------------------------
| Description :
|
| Parameters :
| Returns :
|
+------------------------------------------------------------------------------
*/
int dfile_raw_getdents(struct dfs_fd* fd, struct dfs_dirent* dirp, rt_size_t nbytes)
{
struct dfs_filesystem* fs;
/* parameter check */
if (fd == RT_NULL || fd->type != FT_DIRECTORY) return -DFS_STATUS_EINVAL;
fs = (struct dfs_filesystem*) fd->fs;
if (fs->ops->getdents != RT_NULL) return fs->ops->getdents(fd, dirp, nbytes);
return -DFS_STATUS_ENOSYS;
}
/*
+------------------------------------------------------------------------------
| Function : dfile_raw_unlink
+------------------------------------------------------------------------------
| Description :
|
| Parameters :
| Returns :
|
+------------------------------------------------------------------------------
*/
int dfile_raw_unlink(const char *path)
{
struct dfs_filesystem* fs;
char *fullpath, *real_path, search_path[DFS_PATH_MAX + 1];
#ifdef DFS_USING_WORKDIR
char full_path[DFS_PATH_MAX+1];
#endif
struct dfs_fd* fd;
int index, fspathlen;
/* Make sure we have an absolute path */
fullpath = (char*)path;
if ( fullpath[0] != '/')
{
#ifdef DFS_USING_WORKDIR
/* build full path */
fullpath = full_path;
dfs_lock();
build_fullpath(working_directory, path, fullpath);
dfs_unlock();
#else
rt_kprintf(NO_WORKING_DIR);
return -1;
#endif
}
if ( (fs = dfs_filesystem_lookup(fullpath)) == RT_NULL) return -DFS_STATUS_ENOENT;
/* Check whether file is already open */
dfs_lock();
for (index = 0; index < DFS_FD_MAX; index++)
{
fd = &(fd_table[index]);
if (fd->fs == RT_NULL) continue;
build_fullpath(fd->fs->path, fd->path, search_path);
if (strcmp(fullpath, search_path) == 0)
{
dfs_unlock();
return -DFS_STATUS_EEXIST;
}
}
dfs_unlock();
fspathlen = strlen(fs->path);
real_path = search_path;
rt_memset( real_path, 0, sizeof( real_path ) );
if (*(fullpath + fspathlen) != '/') strcpy(real_path, "/");
strcat(real_path, fullpath + fspathlen);
if (fs->ops->unlink != RT_NULL) return fs->ops->unlink(fs, real_path);
return -DFS_STATUS_ENOSYS;
}
/*
+------------------------------------------------------------------------------
| Function : dfile_raw_write
+------------------------------------------------------------------------------
| Description :
|
| Parameters :
| Returns :
|
+------------------------------------------------------------------------------
*/
int dfile_raw_write(struct dfs_fd* fd, const void *buf, rt_size_t len)
{
struct dfs_filesystem* fs = fd->fs;
if (fd == RT_NULL) return -DFS_STATUS_EINVAL;
if (fs->ops->write == RT_NULL) return -DFS_STATUS_ENOSYS;
return fs->ops->write(fd, buf, len);
}
/*
+------------------------------------------------------------------------------
| Function : dfile_raw_lseek
+------------------------------------------------------------------------------
| Description :
|
| Parameters :
| Returns :
|
+------------------------------------------------------------------------------
*/
int dfile_raw_lseek(struct dfs_fd* fd, rt_off_t offset)
{
struct dfs_filesystem* fs = fd->fs;
if (fd == RT_NULL) return -DFS_STATUS_EINVAL;
if (fs->ops->lseek == RT_NULL) return -DFS_STATUS_ENOSYS;
return fs->ops->lseek(fd, offset);
}
/*
+------------------------------------------------------------------------------
| Function : dfile_raw_stat
+------------------------------------------------------------------------------
| Description : get the file or directory stat description
|
| Parameters : path, the file or directory path
| buf, the stat description will be saved in it
| Returns :
|
+------------------------------------------------------------------------------
*/
int dfile_raw_stat(const char *path, struct dfs_stat *buf)
{
struct dfs_filesystem* fs;
char* fullpath, real_path[DFS_PATH_MAX + 1];
#ifdef DFS_USING_WORKDIR
char full_path[DFS_PATH_MAX + 1];
#endif
int fspathlen;
fullpath = (char*)path;
if ( fullpath[0] != '/' )
{
#ifdef DFS_USING_WORKDIR
/* build full path */
fullpath = full_path;
dfs_lock();
build_fullpath(working_directory, path, fullpath);
dfs_unlock();
#else
rt_kprintf(NO_WORKING_DIR);
return -1;
#endif
}
if ( (fs = dfs_filesystem_lookup(fullpath)) == RT_NULL )
{
dfs_log(DFS_DEBUG_ERROR, ("can't find mounted filesystem on this path:%s", fullpath));
return -DFS_STATUS_ENOENT;
}
fspathlen = strlen(fs->path);
rt_memset(real_path, 0, sizeof(real_path));
if (*(fullpath + fspathlen) != '/') real_path[0] = '/';
strcat(real_path, fullpath + fspathlen);
if (real_path[0] == '/' && real_path[1] == '\0')
{
/* it's the root directory */
buf->st_dev = 0;
buf->st_mode = DFS_S_IRUSR | DFS_S_IRGRP | DFS_S_IROTH |
DFS_S_IWUSR | DFS_S_IWGRP | DFS_S_IWOTH;
buf->st_mode |= DFS_S_IFDIR | DFS_S_IXUSR | DFS_S_IXGRP | DFS_S_IXOTH;
buf->st_size = 0;
buf->st_mtime = 0;
buf->st_blksize = 512;
return DFS_STATUS_OK;
}
if (fs->ops->stat == RT_NULL)
{
dfs_log(DFS_DEBUG_ERROR, ("the filesystem didn't implement this function"));
return -DFS_STATUS_ENOSYS;
}
return fs->ops->stat(fs, real_path, buf);
}
/*
+------------------------------------------------------------------------------
| Function : dfile_raw_rename
+------------------------------------------------------------------------------
| Description :
|
| Parameters :
| Returns :
|
+------------------------------------------------------------------------------
*/
int dfile_raw_rename(const char* oldpath, const char* newpath)
{
struct dfs_filesystem *oldfs, *newfs;
char *oldfullpath, *newfullpath;
#ifdef DFS_USING_WORKDIR
/* Change DFS_PATH_MAX to DFS_PATH_MAX + 1, yi.qiu@2008.09.23*/
char old_realpath[DFS_PATH_MAX + 1], new_realpath[DFS_PATH_MAX + 1];
#endif
oldfullpath = (char*)oldpath;
newfullpath = (char*)newpath;
if ( oldfullpath[0] != '/' )
{
#ifdef DFS_USING_WORKDIR
/* build full path */
oldfullpath = old_realpath;
dfs_lock();
build_fullpath(working_directory, oldpath, oldfullpath);
dfs_unlock();
#else
rt_kprintf(NO_WORKING_DIR);
return -1;
#endif
}
if ( newfullpath[0] != '/' )
{
#ifdef DFS_USING_WORKDIR
/* build full path */
newfullpath = new_realpath;
dfs_lock();
build_fullpath(working_directory, newpath, newfullpath);
dfs_unlock();
#else
rt_kprintf(NO_WORKING_DIR);
return -1;
#endif
}
if ( (oldfs = dfs_filesystem_lookup(oldfullpath)) == RT_NULL )
{
return -DFS_STATUS_ENOENT;
}
if ( (newfs = dfs_filesystem_lookup(newfullpath)) == RT_NULL )
{
return -DFS_STATUS_ENOENT;
}
if ( oldfs == newfs )
{
if ( oldfs->ops->rename == RT_NULL ) return -DFS_STATUS_ENOSYS;
return oldfs->ops->rename(oldfs, oldfullpath, newfullpath);
}
/* not at same file system, return EXDEV */
return -DFS_STATUS_EXDEV;
}
#ifdef RT_USING_FINSH
#include <finsh.h>
static char fullpath[256 + 1];
static struct dfs_fd fd;
static struct dfs_dirent dirent;
void ls(const char* pathname)
{
struct dfs_stat stat;
int length;
/* list directory */
if ( dfile_raw_open(&fd, pathname, DFS_O_DIRECTORY) == 0 )
{
rt_kprintf("Directory %s:\n", pathname);
do
{
rt_memset(&dirent, 0, sizeof(struct dfs_dirent));
length = dfile_raw_getdents(&fd, &dirent, sizeof(struct dfs_dirent));
if ( length > 0 )
{
rt_memset(&stat, 0, sizeof(struct dfs_stat));
/* build full path for each file */
if (pathname[strlen(pathname) - 1] != '/')
rt_snprintf(fullpath, sizeof(fullpath), "%s%c%s", pathname, '/', dirent.d_name);
else
rt_snprintf(fullpath, sizeof(fullpath), "%s%s", pathname, dirent.d_name);
dfile_raw_stat(fullpath, &stat);
if ( stat.st_mode & DFS_S_IFDIR )
{
rt_kprintf("%s\t\t<DIR>\n", dirent.d_name);
}
else
{
rt_kprintf("%s\t\t%lu\n", dirent.d_name, stat.st_size);
}
}
}while(length > 0);
dfile_raw_close(&fd);
}
else
{
rt_kprintf("No such directory\n");
}
}
FINSH_FUNCTION_EXPORT(ls, list directory contents)
static void mkdir(const char* pathname)
{
/* make a new directory */
if (dfile_raw_open(&fd, pathname, DFS_O_DIRECTORY | DFS_O_CREAT) == 0)
{
dfile_raw_close(&fd);
}
else rt_kprintf("Can't mkdir %s\n", pathname);
}
FINSH_FUNCTION_EXPORT(mkdir, make a directory)
void rm(const char* filename)
{
if (dfile_raw_unlink(filename) < 0)
{
rt_kprintf("Delete %s failed\n", filename);
}
}
FINSH_FUNCTION_EXPORT(rm, remove files or directories)
void cat(const char* filename)
{
rt_uint32_t length;
char buffer[81];
if (dfile_raw_open(&fd, filename, DFS_O_RDONLY) < 0)
{
rt_kprintf("Open %s failed\n", filename);
return;
}
do
{
rt_memset(buffer, 0, sizeof(buffer));
length = dfile_raw_read(&fd, buffer, 81);
if (length > 0)
{
rt_kprintf("%s", buffer);
}
}while (length > 0);
dfile_raw_close(&fd);
}
FINSH_FUNCTION_EXPORT(cat, print file)
#endif

View File

@ -1,295 +0,0 @@
/*
+------------------------------------------------------------------------------
| Project : Device Filesystem
+------------------------------------------------------------------------------
| Copyright 2004, 2005 www.fayfayspace.org.
| All rights reserved.
|------------------------------------------------------------------------------
| File : dfs_utl.c, some misc functions of Device FileSystem
|------------------------------------------------------------------------------
| Chang Logs:
| Date Author Notes
| 2005-01-26 ffxz The first version
+------------------------------------------------------------------------------
*/
#include <string.h>
#include <dfs_fs.h>
#include <dfs_util.h>
/*
+------------------------------------------------------------------------------
| Function : dir_name
+------------------------------------------------------------------------------
| Description : Gets the directory name in specified path string
|
| Parameters : path, the specified path string
| path_name, return the path name in this parameter
| len, the length of path name
| Returns : the status of path_name:
| 0 , successful
| -1, failed
| For Example :
| path, dir_name
| /, /
| /usr, /
| /usr/lib, /usr
| /usr/lib/, /usr
+------------------------------------------------------------------------------
*/
int dir_name(const char* path, char* dirname, int len)
{
int pos;
if ( path[0] == '/' && path[1] == '\0' )
{
*dirname++ = '/';
*dirname = '\0';
return 0;
}
pos = strlen(path);
while ( *(path + pos - 1) =='/' ) pos --;
while ( pos > 0 && *(path + pos - 1) != '/' ) pos --;
while ( pos > 0 && *(path + pos - 1) == '/' ) pos --;
if ( pos > len ) return -1;
if ( pos != 0)
{
memcpy(dirname, path, pos);
*(dirname+pos) = '\0';
}
else
{
*dirname++ = '/';
*dirname = '\0';
}
return 0;
}
/*
+------------------------------------------------------------------------------
| Function : file_name
+------------------------------------------------------------------------------
| Description : Gets the file name in specified path string
|
| Parameters : path, the specified path string
| filename, return the path name in this parameter
| len, the length of file name
| Returns : the status of file_name:
| 0 , successful
| -1, failed
| For Example :
| path, filename
| /, RT_NULL
| /usr, usr
| /usr/lib, lib
| /usr/lib/, lib
+------------------------------------------------------------------------------
*/
int file_name(const char* path, char* filename, int len)
{
int pos, size;
if ( path[0] == '/' && path[1] == '\0' )
{
*filename = '\0';
return 0;
}
pos = strlen(path);
while ( *(path + pos -1)=='/' ) pos --;
size = pos;
while ( *(path + pos -1) != '/' && pos > 0 ) pos --;
if ( size - pos > len ) return -1;
else size = size - pos ;
memcpy(filename, path + pos, size);
*(filename+size) = '\0';
return 0;
}
/*
+------------------------------------------------------------------------------
| Function : next_path_name
+------------------------------------------------------------------------------
| Description : Gets the next directory name from specified path string
|
| Parameters : path, the specified path string
| pos, the specified position of path string
| next, return the next path in this parameter
| Returns : the position of next directory item,
| -1, if pos reach in the end of the specified path string
| For Example :
+------------------------------------------------------------------------------
*/
int next_dir_name(const char* path, int pos, char* next)
{
const char* q;
if ( pos > strlen(path) || pos < 0|| path == RT_NULL ) return -1;
q = path + pos;
/* check for firt '/' */
while ( *q == '/' ) q++;
if ( *q == '\0' )
{
*next = '\0';
return -1;
}
while ( *q != '/' && *q != '\0' )
{
*next++ = *q++;
}
*next = '\0';
return q - path + 1;
}
/*
+------------------------------------------------------------------------------
| Function : build_fullpath
+------------------------------------------------------------------------------
| Description : make up the full path from directory and filename
|
| Parameters : path, the specified path string
| filename, return the path name in this parameter
| fullpath, the returned full path name
| Returns : null
+------------------------------------------------------------------------------
*/
void build_fullpath(const char *directory, const char *filename, char *fullpath)
{
char elem[DFS_PATH_MAX + 1];
int pos, start, len;
len = 0;
/* check parameters */
RT_ASSERT(directory != RT_NULL);
RT_ASSERT(filename != RT_NULL);
RT_ASSERT(fullpath != RT_NULL);
/* copy full of directory */
strncpy(fullpath, directory, DFS_PATH_MAX + 1);
for (pos = 0; filename[pos] != '\0'; )
{
/* strip '//' */
while (filename[pos] == '/') pos++;
/* get the filename element, save to elem array */
for (start = pos; filename[pos] != '/' && filename[pos] != '\0'; pos++)
len = pos - start + 1;
strncpy(elem, filename + start, DFS_PATH_MAX + 1);
/* clear the end of elem */
elem[(len < DFS_PATH_MAX + 1? len : DFS_PATH_MAX + 1)] = '\0';
/* strip '..' */
if (elem[0] == '.' && elem[1] == '.')
{
if (strlen(fullpath) == 0) strcpy(fullpath, ""); /* empty filename */
else if (fullpath[0] == '/' && fullpath[1] !='\0')
{
int i = strlen(fullpath);
while (fullpath[i - 1] == '/') i--;
while (i > 0 && fullpath[i - 1] != '/') i--;
fullpath[i] = '\0';
}
}
/* not '.', copy as normally */
else if (elem[0] != '.')
{
len = strlen(fullpath);
if (len == 0) strncpy(fullpath, elem, DFS_PATH_MAX + 1);
else if (fullpath[len - 1] == '/')
{
if (elem[0] == '/') strncat(fullpath, elem + 1, DFS_PATH_MAX + 1);
else strncat(fullpath, elem, DFS_PATH_MAX + 1);
}
else
{
if (elem[0] == '/') strcat(fullpath, elem);
else
{
strncat(fullpath, "/", DFS_PATH_MAX + 1);
strncat(fullpath + 1, elem, DFS_PATH_MAX + 1);
}
}
}
}
if ( fullpath[0] != '/' && fullpath[(len = strlen(fullpath)) - 1] == '/')
{
fullpath[len - 1] = '\0';
}
}
int str_is_prefix(const char* prefix, const char* str)
{
while ((*prefix) && (*str) && (*prefix == *str))
{
prefix ++;
str ++;
}
if (*prefix == 0) return 0;
return -1;
}
#if !defined(RT_USING_MINILIBC) && !defined(RT_USING_NEWLIB)
#if !defined(__ICCARM__)
char *strrchr(const char *t, int c)
{
register char ch;
register const char *l=0;
ch = c;
for (;;)
{
if ((*t == ch)) l=t;
if ((!*t)) return (char*)l; ++t;
}
}
#endif
#if defined (__ARMCC_VERSION) && (__ARMCC_VERSION / 10000 < 35)
int strncasecmp ( const char* s1, const char* s2, size_t len )
{
register unsigned int x2;
register unsigned int x1;
register const char* end = s1 + len;
while (1)
{
if ((s1 >= end) )
return 0;
x2 = *s2 - 'A'; if ((x2 < 26u)) x2 += 32;
x1 = *s1 - 'A'; if ((x1 < 26u)) x1 += 32;
s1++; s2++;
if ((x2 != x1))
break;
if ((x1 == (unsigned int)-'A'))
break;
}
return x1 - x2;
}
#endif /* end of __ARMCC_VERSION */
#endif