提交 44a2d720 编写于 作者: D dzzxzz

unify the components/dfs coding style according to the /documentation/coding_style_cn.txt

git-svn-id: https://rt-thread.googlecode.com/svn/trunk@1866 bbd45198-f89e-11dd-88c7-29a3b14d5316
上级 701df334
...@@ -73,7 +73,7 @@ static int elm_result_to_dfs(FRESULT result) ...@@ -73,7 +73,7 @@ static int elm_result_to_dfs(FRESULT result)
return status; return status;
} }
int dfs_elm_mount(struct dfs_filesystem* fs, unsigned long rwflag, const void* data) int dfs_elm_mount(struct dfs_filesystem *fs, unsigned long rwflag, const void *data)
{ {
FATFS *fat; FATFS *fat;
FRESULT result; FRESULT result;
...@@ -87,12 +87,13 @@ int dfs_elm_mount(struct dfs_filesystem* fs, unsigned long rwflag, const void* d ...@@ -87,12 +87,13 @@ int dfs_elm_mount(struct dfs_filesystem* fs, unsigned long rwflag, const void* d
break; break;
} }
} }
if (index == _VOLUMES) return -DFS_STATUS_ENOSPC; if (index == _VOLUMES)
return -DFS_STATUS_ENOSPC;
/* get device */ /* get device */
disk[index] = fs->dev_id; disk[index] = fs->dev_id;
fat = (FATFS *) rt_malloc(sizeof(FATFS)); fat = (FATFS *)rt_malloc(sizeof(FATFS));
if (fat == RT_NULL) if (fat == RT_NULL)
{ {
return -1; return -1;
...@@ -111,13 +112,13 @@ int dfs_elm_mount(struct dfs_filesystem* fs, unsigned long rwflag, const void* d ...@@ -111,13 +112,13 @@ int dfs_elm_mount(struct dfs_filesystem* fs, unsigned long rwflag, const void* d
return 0; return 0;
} }
int dfs_elm_unmount(struct dfs_filesystem* fs) int dfs_elm_unmount(struct dfs_filesystem *fs)
{ {
FATFS *fat; FATFS *fat;
FRESULT result; FRESULT result;
rt_uint32_t index; rt_uint32_t index;
fat = (FATFS*) fs->data; fat = (FATFS *)fs->data;
RT_ASSERT(fat != RT_NULL); RT_ASSERT(fat != RT_NULL);
...@@ -141,7 +142,7 @@ int dfs_elm_unmount(struct dfs_filesystem* fs) ...@@ -141,7 +142,7 @@ int dfs_elm_unmount(struct dfs_filesystem* fs)
return -DFS_STATUS_ENOENT; return -DFS_STATUS_ENOENT;
} }
int dfs_elm_mkfs(const char* device_name) int dfs_elm_mkfs(const char *device_name)
{ {
BYTE drv; BYTE drv;
rt_device_t dev; rt_device_t dev;
...@@ -156,7 +157,7 @@ int dfs_elm_mkfs(const char* device_name) ...@@ -156,7 +157,7 @@ int dfs_elm_mkfs(const char* device_name)
/* 1: no partition table */ /* 1: no partition table */
/* 0: auto selection of cluster size */ /* 0: auto selection of cluster size */
result = f_mkfs(drv, 1, 0); result = f_mkfs(drv, 1, 0);
if ( result != FR_OK) if (result != FR_OK)
{ {
rt_kprintf("format error\n"); rt_kprintf("format error\n");
return elm_result_to_dfs(result); return elm_result_to_dfs(result);
...@@ -171,7 +172,7 @@ int dfs_elm_mkfs(const char* device_name) ...@@ -171,7 +172,7 @@ int dfs_elm_mkfs(const char* device_name)
return -DFS_STATUS_EIO; return -DFS_STATUS_EIO;
} }
int dfs_elm_statfs(struct dfs_filesystem* fs, struct statfs *buf) int dfs_elm_statfs(struct dfs_filesystem *fs, struct statfs *buf)
{ {
FATFS *f; FATFS *f;
FRESULT res; FRESULT res;
...@@ -181,11 +182,12 @@ int dfs_elm_statfs(struct dfs_filesystem* fs, struct statfs *buf) ...@@ -181,11 +182,12 @@ int dfs_elm_statfs(struct dfs_filesystem* fs, struct statfs *buf)
RT_ASSERT(fs != RT_NULL); RT_ASSERT(fs != RT_NULL);
RT_ASSERT(buf != RT_NULL); RT_ASSERT(buf != RT_NULL);
f = (FATFS*) fs->data; f = (FATFS *)fs->data;
rt_snprintf(driver, sizeof(driver), "%d:", f->drv); rt_snprintf(driver, sizeof(driver), "%d:", f->drv);
res = f_getfree(driver, &fre_clust, &f); res = f_getfree(driver, &fre_clust, &f);
if (res) return elm_result_to_dfs(res); if (res)
return elm_result_to_dfs(res);
/* Get total sectors and free sectors */ /* Get total sectors and free sectors */
tot_sect = (f->n_fatent - 2) * f->csize; tot_sect = (f->n_fatent - 2) * f->csize;
...@@ -202,9 +204,9 @@ int dfs_elm_statfs(struct dfs_filesystem* fs, struct statfs *buf) ...@@ -202,9 +204,9 @@ int dfs_elm_statfs(struct dfs_filesystem* fs, struct statfs *buf)
return 0; return 0;
} }
int dfs_elm_open(struct dfs_fd* file) int dfs_elm_open(struct dfs_fd *file)
{ {
FIL* fd; FIL *fd;
BYTE mode; BYTE mode;
FRESULT result; FRESULT result;
char *drivers_fn; char *drivers_fn;
...@@ -215,9 +217,11 @@ int dfs_elm_open(struct dfs_fd* file) ...@@ -215,9 +217,11 @@ int dfs_elm_open(struct dfs_fd* file)
/* add path for ELM FatFS driver support */ /* add path for ELM FatFS driver support */
vol = elm_get_vol((FATFS *)file->fs->data); vol = elm_get_vol((FATFS *)file->fs->data);
if (vol < 0) return -DFS_STATUS_ENOENT; if (vol < 0)
return -DFS_STATUS_ENOENT;
drivers_fn = rt_malloc(256); drivers_fn = rt_malloc(256);
if (drivers_fn == RT_NULL) return -DFS_STATUS_ENOMEM; if (drivers_fn == RT_NULL)
return -DFS_STATUS_ENOMEM;
rt_snprintf(drivers_fn, 256, "%d:%s", vol, file->path); rt_snprintf(drivers_fn, 256, "%d:%s", vol, file->path);
#else #else
...@@ -267,17 +271,22 @@ int dfs_elm_open(struct dfs_fd* file) ...@@ -267,17 +271,22 @@ int dfs_elm_open(struct dfs_fd* file)
{ {
mode = FA_READ; mode = FA_READ;
if (file->flags & DFS_O_WRONLY) mode |= FA_WRITE; if (file->flags & DFS_O_WRONLY)
if ((file->flags & DFS_O_ACCMODE) & DFS_O_RDWR) mode |= FA_WRITE; mode |= FA_WRITE;
if ((file->flags & DFS_O_ACCMODE) & DFS_O_RDWR)
mode |= FA_WRITE;
/* Opens the file, if it is existing. If not, a new file is created. */ /* Opens the file, if it is existing. If not, a new file is created. */
if (file->flags & DFS_O_CREAT) mode |= FA_OPEN_ALWAYS; if (file->flags & DFS_O_CREAT)
mode |= FA_OPEN_ALWAYS;
/* Creates a new file. If the file is existing, it is truncated and overwritten. */ /* Creates a new file. If the file is existing, it is truncated and overwritten. */
if (file->flags & DFS_O_TRUNC) mode |= FA_CREATE_ALWAYS; if (file->flags & DFS_O_TRUNC)
mode |= FA_CREATE_ALWAYS;
/* Creates a new file. The function fails if the file is already existing. */ /* Creates a new file. The function fails if the file is already existing. */
if (file->flags & DFS_O_EXCL) mode |= FA_CREATE_NEW; if (file->flags & DFS_O_EXCL)
mode |= FA_CREATE_NEW;
/* allocate a fd */ /* allocate a fd */
fd = (FIL*)rt_malloc(sizeof(FIL)); fd = (FIL *)rt_malloc(sizeof(FIL));
if (fd == RT_NULL) if (fd == RT_NULL)
{ {
return -DFS_STATUS_ENOMEM; return -DFS_STATUS_ENOMEM;
...@@ -309,16 +318,16 @@ int dfs_elm_open(struct dfs_fd* file) ...@@ -309,16 +318,16 @@ int dfs_elm_open(struct dfs_fd* file)
return DFS_STATUS_OK; return DFS_STATUS_OK;
} }
int dfs_elm_close(struct dfs_fd* file) int dfs_elm_close(struct dfs_fd *file)
{ {
FRESULT result; FRESULT result;
result = FR_OK; result = FR_OK;
if (file->type == FT_DIRECTORY) if (file->type == FT_DIRECTORY)
{ {
DIR* dir; DIR *dir;
dir = (DIR*)(file->data); dir = (DIR *)(file->data);
RT_ASSERT(dir != RT_NULL); RT_ASSERT(dir != RT_NULL);
/* release memory */ /* release memory */
...@@ -326,8 +335,8 @@ int dfs_elm_close(struct dfs_fd* file) ...@@ -326,8 +335,8 @@ int dfs_elm_close(struct dfs_fd* file)
} }
else if (file->type == FT_REGULAR) else if (file->type == FT_REGULAR)
{ {
FIL* fd; FIL *fd;
fd = (FIL*)(file->data); fd = (FIL *)(file->data);
RT_ASSERT(fd != RT_NULL); RT_ASSERT(fd != RT_NULL);
result = f_close(fd); result = f_close(fd);
...@@ -341,14 +350,14 @@ int dfs_elm_close(struct dfs_fd* file) ...@@ -341,14 +350,14 @@ int dfs_elm_close(struct dfs_fd* file)
return elm_result_to_dfs(result); return elm_result_to_dfs(result);
} }
int dfs_elm_ioctl(struct dfs_fd* file, int cmd, void* args) int dfs_elm_ioctl(struct dfs_fd *file, int cmd, void *args)
{ {
return -DFS_STATUS_ENOSYS; return -DFS_STATUS_ENOSYS;
} }
int dfs_elm_read(struct dfs_fd* file, void* buf, rt_size_t len) int dfs_elm_read(struct dfs_fd *file, void *buf, rt_size_t len)
{ {
FIL* fd; FIL *fd;
FRESULT result; FRESULT result;
UINT byte_read; UINT byte_read;
...@@ -357,20 +366,21 @@ int dfs_elm_read(struct dfs_fd* file, void* buf, rt_size_t len) ...@@ -357,20 +366,21 @@ int dfs_elm_read(struct dfs_fd* file, void* buf, rt_size_t len)
return -DFS_STATUS_EISDIR; return -DFS_STATUS_EISDIR;
} }
fd = (FIL*)(file->data); fd = (FIL *)(file->data);
RT_ASSERT(fd != RT_NULL); RT_ASSERT(fd != RT_NULL);
result = f_read(fd, buf, len, &byte_read); result = f_read(fd, buf, len, &byte_read);
/* update position */ /* update position */
file->pos = fd->fptr; file->pos = fd->fptr;
if (result == FR_OK) return byte_read; if (result == FR_OK)
return byte_read;
return elm_result_to_dfs(result); return elm_result_to_dfs(result);
} }
int dfs_elm_write(struct dfs_fd* file, const void* buf, rt_size_t len) int dfs_elm_write(struct dfs_fd *file, const void *buf, rt_size_t len)
{ {
FIL* fd; FIL *fd;
FRESULT result; FRESULT result;
UINT byte_write; UINT byte_write;
...@@ -379,39 +389,40 @@ int dfs_elm_write(struct dfs_fd* file, const void* buf, rt_size_t len) ...@@ -379,39 +389,40 @@ int dfs_elm_write(struct dfs_fd* file, const void* buf, rt_size_t len)
return -DFS_STATUS_EISDIR; return -DFS_STATUS_EISDIR;
} }
fd = (FIL*)(file->data); fd = (FIL *)(file->data);
RT_ASSERT(fd != RT_NULL); RT_ASSERT(fd != RT_NULL);
result = f_write(fd, buf, len, &byte_write); result = f_write(fd, buf, len, &byte_write);
/* update position and file size */ /* update position and file size */
file->pos = fd->fptr; file->pos = fd->fptr;
file->size = fd->fsize; file->size = fd->fsize;
if (result == FR_OK) return byte_write; if (result == FR_OK)
return byte_write;
return elm_result_to_dfs(result); return elm_result_to_dfs(result);
} }
int dfs_elm_flush(struct dfs_fd* file) int dfs_elm_flush(struct dfs_fd *file)
{ {
FIL* fd; FIL *fd;
FRESULT result; FRESULT result;
fd = (FIL*)(file->data); fd = (FIL *)(file->data);
RT_ASSERT(fd != RT_NULL); RT_ASSERT(fd != RT_NULL);
result = f_sync(fd); result = f_sync(fd);
return elm_result_to_dfs(result); return elm_result_to_dfs(result);
} }
int dfs_elm_lseek(struct dfs_fd* file, rt_off_t offset) int dfs_elm_lseek(struct dfs_fd *file, rt_off_t offset)
{ {
FRESULT result = FR_OK; FRESULT result = FR_OK;
if (file->type == FT_REGULAR) if (file->type == FT_REGULAR)
{ {
FIL* fd; FIL *fd;
/* regular file type */ /* regular file type */
fd = (FIL*)(file->data); fd = (FIL *)(file->data);
RT_ASSERT(fd != RT_NULL); RT_ASSERT(fd != RT_NULL);
result = f_lseek(fd, offset); result = f_lseek(fd, offset);
...@@ -424,9 +435,9 @@ int dfs_elm_lseek(struct dfs_fd* file, rt_off_t offset) ...@@ -424,9 +435,9 @@ int dfs_elm_lseek(struct dfs_fd* file, rt_off_t offset)
else if (file->type == FT_DIRECTORY) else if (file->type == FT_DIRECTORY)
{ {
/* which is a directory */ /* which is a directory */
DIR* dir; DIR *dir;
dir = (DIR*)(file->data); dir = (DIR *)(file->data);
RT_ASSERT(dir != RT_NULL); RT_ASSERT(dir != RT_NULL);
result = f_seekdir(dir, offset / sizeof(struct dirent)); result = f_seekdir(dir, offset / sizeof(struct dirent));
...@@ -441,20 +452,21 @@ int dfs_elm_lseek(struct dfs_fd* file, rt_off_t offset) ...@@ -441,20 +452,21 @@ int dfs_elm_lseek(struct dfs_fd* file, rt_off_t offset)
return elm_result_to_dfs(result); return elm_result_to_dfs(result);
} }
int dfs_elm_getdents(struct dfs_fd* file, struct dirent* dirp, rt_uint32_t count) int dfs_elm_getdents(struct dfs_fd *file, struct dirent *dirp, rt_uint32_t count)
{ {
DIR* dir; DIR *dir;
FILINFO fno; FILINFO fno;
FRESULT result; FRESULT result;
rt_uint32_t index; rt_uint32_t index;
struct dirent* d; struct dirent *d;
dir = (DIR*)(file->data); dir = (DIR *)(file->data);
RT_ASSERT(dir != RT_NULL); RT_ASSERT(dir != RT_NULL);
/* make integer count */ /* make integer count */
count = (count / sizeof(struct dirent)) * sizeof(struct dirent); count = (count / sizeof(struct dirent)) * sizeof(struct dirent);
if ( count == 0 ) return -DFS_STATUS_EINVAL; if (count == 0)
return -DFS_STATUS_EINVAL;
#if _USE_LFN #if _USE_LFN
/* allocate long file name */ /* allocate long file name */
...@@ -470,7 +482,8 @@ int dfs_elm_getdents(struct dfs_fd* file, struct dirent* dirp, rt_uint32_t count ...@@ -470,7 +482,8 @@ int dfs_elm_getdents(struct dfs_fd* file, struct dirent* dirp, rt_uint32_t count
d = dirp + index; d = dirp + index;
result = f_readdir(dir, &fno); result = f_readdir(dir, &fno);
if (result != FR_OK || fno.fname[0] == 0) break; if (result != FR_OK || fno.fname[0] == 0)
break;
#if _USE_LFN #if _USE_LFN
fn = *fno.lfname? fno.lfname : fno.fname; fn = *fno.lfname? fno.lfname : fno.fname;
...@@ -479,15 +492,17 @@ int dfs_elm_getdents(struct dfs_fd* file, struct dirent* dirp, rt_uint32_t count ...@@ -479,15 +492,17 @@ int dfs_elm_getdents(struct dfs_fd* file, struct dirent* dirp, rt_uint32_t count
#endif #endif
d->d_type = DFS_DT_UNKNOWN; d->d_type = DFS_DT_UNKNOWN;
if (fno.fattrib & AM_DIR) d->d_type = DFS_DT_DIR; if (fno.fattrib & AM_DIR)
else d->d_type = DFS_DT_REG; d->d_type = DFS_DT_DIR;
else
d->d_type = DFS_DT_REG;
d->d_namlen = rt_strlen(fn); d->d_namlen = rt_strlen(fn);
d->d_reclen = (rt_uint16_t)sizeof(struct dirent); d->d_reclen = (rt_uint16_t)sizeof(struct dirent);
rt_strncpy(d->d_name, fn, rt_strlen(fn) + 1); rt_strncpy(d->d_name, fn, rt_strlen(fn) + 1);
index ++; index ++;
if ( index * sizeof(struct dirent) >= count ) if (index * sizeof(struct dirent) >= count)
break; break;
} }
...@@ -503,7 +518,7 @@ int dfs_elm_getdents(struct dfs_fd* file, struct dirent* dirp, rt_uint32_t count ...@@ -503,7 +518,7 @@ int dfs_elm_getdents(struct dfs_fd* file, struct dirent* dirp, rt_uint32_t count
return index * sizeof(struct dirent); return index * sizeof(struct dirent);
} }
int dfs_elm_unlink(struct dfs_filesystem* fs, const char* path) int dfs_elm_unlink(struct dfs_filesystem *fs, const char *path)
{ {
FRESULT result; FRESULT result;
...@@ -514,9 +529,11 @@ int dfs_elm_unlink(struct dfs_filesystem* fs, const char* path) ...@@ -514,9 +529,11 @@ int dfs_elm_unlink(struct dfs_filesystem* fs, const char* path)
/* add path for ELM FatFS driver support */ /* add path for ELM FatFS driver support */
vol = elm_get_vol((FATFS *)fs->data); vol = elm_get_vol((FATFS *)fs->data);
if (vol < 0) return -DFS_STATUS_ENOENT; if (vol < 0)
return -DFS_STATUS_ENOENT;
drivers_fn = rt_malloc(256); drivers_fn = rt_malloc(256);
if (drivers_fn == RT_NULL) return -DFS_STATUS_ENOMEM; if (drivers_fn == RT_NULL)
return -DFS_STATUS_ENOMEM;
rt_snprintf(drivers_fn, 256, "%d:%s", vol, path); rt_snprintf(drivers_fn, 256, "%d:%s", vol, path);
#else #else
...@@ -531,7 +548,7 @@ int dfs_elm_unlink(struct dfs_filesystem* fs, const char* path) ...@@ -531,7 +548,7 @@ int dfs_elm_unlink(struct dfs_filesystem* fs, const char* path)
return elm_result_to_dfs(result); return elm_result_to_dfs(result);
} }
int dfs_elm_rename(struct dfs_filesystem* fs, const char* oldpath, const char* newpath) int dfs_elm_rename(struct dfs_filesystem *fs, const char *oldpath, const char *newpath)
{ {
FRESULT result; FRESULT result;
...@@ -543,10 +560,12 @@ int dfs_elm_rename(struct dfs_filesystem* fs, const char* oldpath, const char* n ...@@ -543,10 +560,12 @@ int dfs_elm_rename(struct dfs_filesystem* fs, const char* oldpath, const char* n
/* add path for ELM FatFS driver support */ /* add path for ELM FatFS driver support */
vol = elm_get_vol((FATFS *)fs->data); vol = elm_get_vol((FATFS *)fs->data);
if (vol < 0) return -DFS_STATUS_ENOENT; if (vol < 0)
return -DFS_STATUS_ENOENT;
drivers_oldfn = rt_malloc(256); drivers_oldfn = rt_malloc(256);
if (drivers_oldfn == RT_NULL) return -DFS_STATUS_ENOMEM; if (drivers_oldfn == RT_NULL)
return -DFS_STATUS_ENOMEM;
drivers_newfn = newpath; drivers_newfn = newpath;
rt_snprintf(drivers_oldfn, 256, "%d:%s", vol, oldpath); rt_snprintf(drivers_oldfn, 256, "%d:%s", vol, oldpath);
...@@ -564,7 +583,7 @@ int dfs_elm_rename(struct dfs_filesystem* fs, const char* oldpath, const char* n ...@@ -564,7 +583,7 @@ int dfs_elm_rename(struct dfs_filesystem* fs, const char* oldpath, const char* n
return elm_result_to_dfs(result); return elm_result_to_dfs(result);
} }
int dfs_elm_stat(struct dfs_filesystem* fs, const char *path, struct stat *st) int dfs_elm_stat(struct dfs_filesystem *fs, const char *path, struct stat *st)
{ {
FILINFO file_info; FILINFO file_info;
FRESULT result; FRESULT result;
...@@ -577,9 +596,11 @@ int dfs_elm_stat(struct dfs_filesystem* fs, const char *path, struct stat *st) ...@@ -577,9 +596,11 @@ int dfs_elm_stat(struct dfs_filesystem* fs, const char *path, struct stat *st)
/* add path for ELM FatFS driver support */ /* add path for ELM FatFS driver support */
vol = elm_get_vol((FATFS *)fs->data); vol = elm_get_vol((FATFS *)fs->data);
if (vol < 0) return -DFS_STATUS_ENOENT; if (vol < 0)
return -DFS_STATUS_ENOENT;
drivers_fn = rt_malloc(256); drivers_fn = rt_malloc(256);
if (drivers_fn == RT_NULL) return -DFS_STATUS_ENOMEM; if (drivers_fn == RT_NULL)
return -DFS_STATUS_ENOMEM;
rt_snprintf(drivers_fn, 256, "%d:%s", vol, path); rt_snprintf(drivers_fn, 256, "%d:%s", vol, path);
#else #else
...@@ -600,7 +621,7 @@ int dfs_elm_stat(struct dfs_filesystem* fs, const char *path, struct stat *st) ...@@ -600,7 +621,7 @@ int dfs_elm_stat(struct dfs_filesystem* fs, const char *path, struct stat *st)
if (result == FR_OK) if (result == FR_OK)
{ {
/* convert to dfs stat structure */ /* convert to dfs stat structure */
st->st_dev = 0; st->st_dev = 0;
st->st_mode = DFS_S_IFREG | DFS_S_IRUSR | DFS_S_IRGRP | DFS_S_IROTH | st->st_mode = DFS_S_IFREG | DFS_S_IRUSR | DFS_S_IRGRP | DFS_S_IROTH |
DFS_S_IWUSR | DFS_S_IWGRP | DFS_S_IWOTH; DFS_S_IWUSR | DFS_S_IWGRP | DFS_S_IWOTH;
...@@ -659,19 +680,19 @@ int elm_init(void) ...@@ -659,19 +680,19 @@ int elm_init(void)
#include "diskio.h" #include "diskio.h"
/* Inidialize a Drive */ /* Inidialize a Drive */
DSTATUS disk_initialize (BYTE drv) DSTATUS disk_initialize(BYTE drv)
{ {
return 0; return 0;
} }
/* Return Disk Status */ /* Return Disk Status */
DSTATUS disk_status (BYTE drv) DSTATUS disk_status(BYTE drv)
{ {
return 0; return 0;
} }
/* Read Sector(s) */ /* Read Sector(s) */
DRESULT disk_read (BYTE drv, BYTE *buff, DWORD sector, BYTE count) DRESULT disk_read(BYTE drv, BYTE *buff, DWORD sector, BYTE count)
{ {
rt_size_t result; rt_size_t result;
rt_device_t device = disk[drv]; rt_device_t device = disk[drv];
...@@ -686,7 +707,7 @@ DRESULT disk_read (BYTE drv, BYTE *buff, DWORD sector, BYTE count) ...@@ -686,7 +707,7 @@ DRESULT disk_read (BYTE drv, BYTE *buff, DWORD sector, BYTE count)
} }
/* Write Sector(s) */ /* Write Sector(s) */
DRESULT disk_write (BYTE drv, const BYTE *buff, DWORD sector, BYTE count) DRESULT disk_write(BYTE drv, const BYTE *buff, DWORD sector, BYTE count)
{ {
rt_size_t result; rt_size_t result;
rt_device_t device = disk[drv]; rt_device_t device = disk[drv];
...@@ -701,11 +722,12 @@ DRESULT disk_write (BYTE drv, const BYTE *buff, DWORD sector, BYTE count) ...@@ -701,11 +722,12 @@ DRESULT disk_write (BYTE drv, const BYTE *buff, DWORD sector, BYTE count)
} }
/* Miscellaneous Functions */ /* Miscellaneous Functions */
DRESULT disk_ioctl (BYTE drv, BYTE ctrl, void *buff) DRESULT disk_ioctl(BYTE drv, BYTE ctrl, void *buff)
{ {
rt_device_t device = disk[drv]; rt_device_t device = disk[drv];
if (device == RT_NULL) return RES_ERROR; if (device == RT_NULL)
return RES_ERROR;
if (ctrl == GET_SECTOR_COUNT) if (ctrl == GET_SECTOR_COUNT)
{ {
...@@ -714,8 +736,9 @@ DRESULT disk_ioctl (BYTE drv, BYTE ctrl, void *buff) ...@@ -714,8 +736,9 @@ DRESULT disk_ioctl (BYTE drv, BYTE ctrl, void *buff)
rt_memset(&geometry, 0, sizeof(geometry)); rt_memset(&geometry, 0, sizeof(geometry));
rt_device_control(device, RT_DEVICE_CTRL_BLK_GETGEOME, &geometry); rt_device_control(device, RT_DEVICE_CTRL_BLK_GETGEOME, &geometry);
*(DWORD*)buff = geometry.sector_count; *(DWORD *)buff = geometry.sector_count;
if (geometry.sector_count == 0) return RES_ERROR; if (geometry.sector_count == 0)
return RES_ERROR;
} }
else if (ctrl == GET_SECTOR_SIZE) else if (ctrl == GET_SECTOR_SIZE)
{ {
...@@ -724,7 +747,7 @@ DRESULT disk_ioctl (BYTE drv, BYTE ctrl, void *buff) ...@@ -724,7 +747,7 @@ DRESULT disk_ioctl (BYTE drv, BYTE ctrl, void *buff)
rt_memset(&geometry, 0, sizeof(geometry)); rt_memset(&geometry, 0, sizeof(geometry));
rt_device_control(device, RT_DEVICE_CTRL_BLK_GETGEOME, &geometry); rt_device_control(device, RT_DEVICE_CTRL_BLK_GETGEOME, &geometry);
*(WORD*)buff = geometry.bytes_per_sector; *(WORD *)buff = geometry.bytes_per_sector;
} }
else if (ctrl == GET_BLOCK_SIZE) /* Get erase block size in unit of sectors (DWORD) */ else if (ctrl == GET_BLOCK_SIZE) /* Get erase block size in unit of sectors (DWORD) */
{ {
...@@ -733,19 +756,19 @@ DRESULT disk_ioctl (BYTE drv, BYTE ctrl, void *buff) ...@@ -733,19 +756,19 @@ DRESULT disk_ioctl (BYTE drv, BYTE ctrl, void *buff)
rt_memset(&geometry, 0, sizeof(geometry)); rt_memset(&geometry, 0, sizeof(geometry));
rt_device_control(device, RT_DEVICE_CTRL_BLK_GETGEOME, &geometry); rt_device_control(device, RT_DEVICE_CTRL_BLK_GETGEOME, &geometry);
*(DWORD*)buff = geometry.block_size/geometry.bytes_per_sector; *(DWORD *)buff = geometry.block_size/geometry.bytes_per_sector;
} }
return RES_OK; return RES_OK;
} }
rt_time_t get_fattime() rt_time_t get_fattime(void)
{ {
return 0; return 0;
} }
#if _FS_REENTRANT #if _FS_REENTRANT
int ff_cre_syncobj(BYTE drv, _SYNC_t* m) int ff_cre_syncobj(BYTE drv, _SYNC_t *m)
{ {
char name[8]; char name[8];
rt_mutex_t mutex; rt_mutex_t mutex;
...@@ -770,7 +793,8 @@ int ff_del_syncobj(_SYNC_t m) ...@@ -770,7 +793,8 @@ int ff_del_syncobj(_SYNC_t m)
int ff_req_grant(_SYNC_t m) int ff_req_grant(_SYNC_t m)
{ {
if (rt_mutex_take(m, _FS_TIMEOUT) == RT_EOK) return RT_TRUE; if (rt_mutex_take(m, _FS_TIMEOUT) == RT_EOK)
return RT_TRUE;
return RT_FALSE; return RT_FALSE;
} }
......
/*
* File : dfs_nfs.h
* This file is part of Device File System in RT-Thread RTOS
* COPYRIGHT (C) 2004-2011, RT-Thread Development Team
*
* The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at
* http://www.rt-thread.org/license/LICENSE.
*
* Change Logs:
* Date Author Notes
*/
#ifndef __NFS_H__ #ifndef __NFS_H__
#define __NFS_H__ #define __NFS_H__
......
/*
* File : dfs_romfs.c
* This file is part of Device File System in RT-Thread RTOS
* COPYRIGHT (C) 2004-2011, RT-Thread Development Team
*
* The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at
* http://www.rt-thread.org/license/LICENSE.
*
* Change Logs:
* Date Author Notes
*/
#include <rtthread.h> #include <rtthread.h>
#include <dfs.h> #include <dfs.h>
#include <dfs_fs.h> #include <dfs_fs.h>
#include "dfs_romfs.h" #include "dfs_romfs.h"
int dfs_romfs_mount(struct dfs_filesystem* fs, unsigned long rwflag, const void* data) int dfs_romfs_mount(struct dfs_filesystem *fs, unsigned long rwflag, const void *data)
{ {
struct romfs_dirent* root_dirent; struct romfs_dirent *root_dirent;
if (data == RT_NULL) return -DFS_STATUS_EIO; if (data == RT_NULL)
return -DFS_STATUS_EIO;
root_dirent = (struct romfs_dirent*)data; root_dirent = (struct romfs_dirent *)data;
fs->data = root_dirent; fs->data = root_dirent;
return DFS_STATUS_OK; return DFS_STATUS_OK;
} }
int dfs_romfs_unmount(struct dfs_filesystem* fs) int dfs_romfs_unmount(struct dfs_filesystem *fs)
{ {
return DFS_STATUS_OK; return DFS_STATUS_OK;
} }
int dfs_romfs_ioctl(struct dfs_fd* file, int cmd, void* args) int dfs_romfs_ioctl(struct dfs_fd *file, int cmd, void *args)
{ {
return -DFS_STATUS_EIO; return -DFS_STATUS_EIO;
} }
struct romfs_dirent* dfs_romfs_lookup(struct romfs_dirent* root_dirent, const char* path, rt_size_t *size) struct romfs_dirent *dfs_romfs_lookup(struct romfs_dirent *root_dirent, const char *path, rt_size_t *size)
{ {
rt_size_t index, found; rt_size_t index, found;
const char *subpath, *subpath_end; const char *subpath, *subpath_end;
struct romfs_dirent* dirent; struct romfs_dirent *dirent;
rt_size_t dirent_size; rt_size_t dirent_size;
if (path[0] == '/' && path[1] == '\0') if (path[0] == '/' && path[1] == '\0')
...@@ -39,15 +53,17 @@ struct romfs_dirent* dfs_romfs_lookup(struct romfs_dirent* root_dirent, const ch ...@@ -39,15 +53,17 @@ struct romfs_dirent* dfs_romfs_lookup(struct romfs_dirent* root_dirent, const ch
} }
/* goto root directy entries */ /* goto root directy entries */
dirent = (struct romfs_dirent*)root_dirent->data; dirent = (struct romfs_dirent *)root_dirent->data;
dirent_size = root_dirent->size; dirent_size = root_dirent->size;
/* get the end position of this subpath */ /* get the end position of this subpath */
subpath_end = path; subpath_end = path;
/* skip /// */ /* skip /// */
while (*subpath_end && *subpath_end == '/') subpath_end ++; while (*subpath_end && *subpath_end == '/')
subpath_end ++;
subpath = subpath_end; subpath = subpath_end;
while ((*subpath_end != '/') && *subpath_end) subpath_end ++; while ((*subpath_end != '/') && *subpath_end)
subpath_end ++;
while (dirent != RT_NULL) while (dirent != RT_NULL)
{ {
...@@ -61,9 +77,11 @@ struct romfs_dirent* dfs_romfs_lookup(struct romfs_dirent* root_dirent, const ch ...@@ -61,9 +77,11 @@ struct romfs_dirent* dfs_romfs_lookup(struct romfs_dirent* root_dirent, const ch
dirent_size = dirent[index].size; dirent_size = dirent[index].size;
/* skip /// */ /* skip /// */
while (*subpath_end && *subpath_end == '/') subpath_end ++; while (*subpath_end && *subpath_end == '/')
subpath_end ++;
subpath = subpath_end; subpath = subpath_end;
while ((*subpath_end != '/') && *subpath_end) subpath_end ++; while ((*subpath_end != '/') && *subpath_end)
subpath_end ++;
if (!(*subpath)) if (!(*subpath))
{ {
...@@ -81,24 +99,26 @@ struct romfs_dirent* dfs_romfs_lookup(struct romfs_dirent* root_dirent, const ch ...@@ -81,24 +99,26 @@ struct romfs_dirent* dfs_romfs_lookup(struct romfs_dirent* root_dirent, const ch
else else
{ {
/* return file dirent */ /* return file dirent */
if (subpath != RT_NULL) break; /* not the end of path */ if (subpath != RT_NULL)
break; /* not the end of path */
return &dirent[index]; return &dirent[index];
} }
} }
} }
if (!found) break; /* not found */ if (!found)
break; /* not found */
} }
/* not found */ /* not found */
return RT_NULL; return RT_NULL;
} }
int dfs_romfs_read(struct dfs_fd* file, void *buf, rt_size_t count) int dfs_romfs_read(struct dfs_fd *file, void *buf, rt_size_t count)
{ {
rt_size_t length; rt_size_t length;
struct romfs_dirent* dirent; struct romfs_dirent *dirent;
dirent = (struct romfs_dirent *)file->data; dirent = (struct romfs_dirent *)file->data;
RT_ASSERT(dirent != RT_NULL); RT_ASSERT(dirent != RT_NULL);
...@@ -117,7 +137,7 @@ int dfs_romfs_read(struct dfs_fd* file, void *buf, rt_size_t count) ...@@ -117,7 +137,7 @@ int dfs_romfs_read(struct dfs_fd* file, void *buf, rt_size_t count)
return length; return length;
} }
int dfs_romfs_lseek(struct dfs_fd* file, rt_off_t offset) int dfs_romfs_lseek(struct dfs_fd *file, rt_off_t offset)
{ {
if (offset <= file->size) if (offset <= file->size)
{ {
...@@ -128,30 +148,31 @@ int dfs_romfs_lseek(struct dfs_fd* file, rt_off_t offset) ...@@ -128,30 +148,31 @@ int dfs_romfs_lseek(struct dfs_fd* file, rt_off_t offset)
return -DFS_STATUS_EIO; return -DFS_STATUS_EIO;
} }
int dfs_romfs_close(struct dfs_fd* file) int dfs_romfs_close(struct dfs_fd *file)
{ {
file->data = RT_NULL; file->data = RT_NULL;
return DFS_STATUS_OK; return DFS_STATUS_OK;
} }
int dfs_romfs_open(struct dfs_fd* file) int dfs_romfs_open(struct dfs_fd *file)
{ {
rt_size_t size; rt_size_t size;
struct romfs_dirent* dirent; struct romfs_dirent *dirent;
struct romfs_dirent* root_dirent; struct romfs_dirent *root_dirent;
root_dirent = (struct romfs_dirent*)file->fs->data; root_dirent = (struct romfs_dirent *)file->fs->data;
if (file->flags & (DFS_O_CREAT | DFS_O_WRONLY | DFS_O_APPEND | DFS_O_TRUNC | DFS_O_RDWR)) if (file->flags & (DFS_O_CREAT | DFS_O_WRONLY | DFS_O_APPEND | DFS_O_TRUNC | DFS_O_RDWR))
return -DFS_STATUS_EINVAL; return -DFS_STATUS_EINVAL;
dirent = dfs_romfs_lookup(root_dirent, file->path, &size); dirent = dfs_romfs_lookup(root_dirent, file->path, &size);
if (dirent == RT_NULL) return -DFS_STATUS_ENOENT; if (dirent == RT_NULL)
return -DFS_STATUS_ENOENT;
/* entry is a directory file type */ /* entry is a directory file type */
if (dirent->type == ROMFS_DIRENT_DIR) if (dirent->type == ROMFS_DIRENT_DIR)
{ {
if (!(file->flags & DFS_O_DIRECTORY) ) if (!(file->flags & DFS_O_DIRECTORY))
return -DFS_STATUS_ENOENT; return -DFS_STATUS_ENOENT;
} }
else else
...@@ -168,16 +189,17 @@ int dfs_romfs_open(struct dfs_fd* file) ...@@ -168,16 +189,17 @@ int dfs_romfs_open(struct dfs_fd* file)
return DFS_STATUS_OK; return DFS_STATUS_OK;
} }
int dfs_romfs_stat(struct dfs_filesystem* fs, const char *path, struct stat *st) int dfs_romfs_stat(struct dfs_filesystem *fs, const char *path, struct stat *st)
{ {
rt_size_t size; rt_size_t size;
struct romfs_dirent* dirent; struct romfs_dirent *dirent;
struct romfs_dirent* root_dirent; struct romfs_dirent *root_dirent;
root_dirent = (struct romfs_dirent*)fs->data; root_dirent = (struct romfs_dirent *)fs->data;
dirent = dfs_romfs_lookup(root_dirent, path, &size); dirent = dfs_romfs_lookup(root_dirent, path, &size);
if (dirent == RT_NULL) return -DFS_STATUS_ENOENT; if (dirent == RT_NULL)
return -DFS_STATUS_ENOENT;
st->st_dev = 0; st->st_dev = 0;
st->st_mode = DFS_S_IFREG | DFS_S_IRUSR | DFS_S_IRGRP | DFS_S_IROTH | st->st_mode = DFS_S_IFREG | DFS_S_IRUSR | DFS_S_IRGRP | DFS_S_IROTH |
...@@ -196,22 +218,23 @@ int dfs_romfs_stat(struct dfs_filesystem* fs, const char *path, struct stat *st) ...@@ -196,22 +218,23 @@ int dfs_romfs_stat(struct dfs_filesystem* fs, const char *path, struct stat *st)
return DFS_STATUS_OK; return DFS_STATUS_OK;
} }
int dfs_romfs_getdents(struct dfs_fd* file, struct dirent* dirp, rt_uint32_t count) int dfs_romfs_getdents(struct dfs_fd *file, struct dirent *dirp, rt_uint32_t count)
{ {
rt_size_t index; rt_size_t index;
const char *name; const char *name;
struct dirent* d; struct dirent *d;
struct romfs_dirent *dirent, *sub_dirent; struct romfs_dirent *dirent, *sub_dirent;
dirent = (struct romfs_dirent*) file->data; dirent = (struct romfs_dirent *)file->data;
RT_ASSERT(dirent->type == ROMFS_DIRENT_DIR); RT_ASSERT(dirent->type == ROMFS_DIRENT_DIR);
/* enter directory */ /* enter directory */
dirent = (struct romfs_dirent*) dirent->data; dirent = (struct romfs_dirent *)dirent->data;
/* make integer count */ /* make integer count */
count = (count / sizeof(struct dirent)); count = (count / sizeof(struct dirent));
if ( count == 0 ) return -DFS_STATUS_EINVAL; if (count == 0)
return -DFS_STATUS_EINVAL;
index = 0; index = 0;
for (index = 0; index < count && file->pos < file->size; index ++) for (index = 0; index < count && file->pos < file->size; index ++)
......
/*
* File : dfs_romfs.h
* This file is part of Device File System in RT-Thread RTOS
* COPYRIGHT (C) 2004-2011, RT-Thread Development Team
*
* The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at
* http://www.rt-thread.org/license/LICENSE.
*
* Change Logs:
* Date Author Notes
*/
#ifndef __DFS_ROMFS_H__ #ifndef __DFS_ROMFS_H__
#define __DFS_ROMFS_H__ #define __DFS_ROMFS_H__
......
/* /*
* File : dfs.h * File : dfs.h
* This file is part of Device File System in RT-Thread RTOS * This file is part of Device File System in RT-Thread RTOS
* COPYRIGHT (C) 2004-2010, RT-Thread Development Team * COPYRIGHT (C) 2004-2011, RT-Thread Development Team
* *
* The license and distribution terms for this file may be * The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at * found in the file LICENSE in this distribution or at
...@@ -21,14 +21,14 @@ ...@@ -21,14 +21,14 @@
extern "C" { extern "C" {
#endif #endif
char* dfs_normalize_path(const char* directory, const char* filename); char *dfs_normalize_path(const char *directory, const char *filename);
const char* dfs_subdir(const char* directory, const char* filename); const char *dfs_subdir(const char *directory, const char *filename);
/* FD APIs */ /* FD APIs */
int fd_new(void); int fd_new(void);
struct dfs_fd* fd_get(int fd); struct dfs_fd *fd_get(int fd);
void fd_put(struct dfs_fd* fd); void fd_put(struct dfs_fd *fd);
int fd_is_open(const char* pathname); int fd_is_open(const char *pathname);
#ifdef __cplusplus #ifdef __cplusplus
} }
......
/* /*
* File : dfs_def.h * File : dfs_def.h
* This file is part of Device File System in RT-Thread RTOS * This file is part of Device File System in RT-Thread RTOS
* COPYRIGHT (C) 2004-2010, RT-Thread Development Team * COPYRIGHT (C) 2004-2011, RT-Thread Development Team
* *
* The license and distribution terms for this file may be * The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at * found in the file LICENSE in this distribution or at
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
* 2004-10-14 Beranard Clean up the code. * 2004-10-14 Beranard Clean up the code.
* 2005-01-22 Beranard 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__
...@@ -277,11 +278,11 @@ struct dirent ...@@ -277,11 +278,11 @@ struct dirent
/* file descriptor */ /* file descriptor */
struct dfs_fd struct dfs_fd
{ {
char* path; /* 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 */
struct dfs_filesystem* fs; /* Resident file system */ struct dfs_filesystem *fs; /* Resident file system */
rt_uint32_t flags; /* Descriptor flags */ rt_uint32_t flags; /* Descriptor flags */
rt_size_t size; /* Size in bytes */ rt_size_t size; /* Size in bytes */
......
/* /*
+------------------------------------------------------------------------------ * File : dfs_elm.h
| Project : Device Filesystem * This file is part of Device File System in RT-Thread RTOS
+------------------------------------------------------------------------------ * COPYRIGHT (C) 2008-2011, 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_efs.h * http://www.rt-thread.org/license/LICENSE.
|------------------------------------------------------------------------------ *
| Chang Logs: * Change Logs:
| Date Author Notes * Date Author Notes
| 2010-02-06 Bernard Add elm_init function declaration * 2010-02-06 Bernard Add elm_init function declaration
+------------------------------------------------------------------------------ */
*/
#ifndef __DFS_ELM_H__ #ifndef __DFS_ELM_H__
#define __DFS_ELM_H__ #define __DFS_ELM_H__
......
/* /*
+------------------------------------------------------------------------------ * File : dfs_file.c
| Project : Device Filesystem * This file is part of Device File System in RT-Thread RTOS
+------------------------------------------------------------------------------ * COPYRIGHT (C) 2004-2011, 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_raw.h, the raw APIs of Device FileSystem * http://www.rt-thread.org/license/LICENSE.
|------------------------------------------------------------------------------ *
| Chang Logs: * Change Logs:
| Date Author Notes * Date Author Notes
| 2005-01-26 ffxz The first version * 2005-01-26 Bernard The first version.
+------------------------------------------------------------------------------ */
*/
#ifndef __DFS_RAW_H__ #ifndef __DFS_RAW_H__
#define __DFS_RAW_H__ #define __DFS_RAW_H__
...@@ -20,16 +19,16 @@ ...@@ -20,16 +19,16 @@
#include <dfs.h> #include <dfs.h>
#include <dfs_fs.h> #include <dfs_fs.h>
int dfs_file_open(struct dfs_fd* fd, const char *path, int flags); int dfs_file_open(struct dfs_fd *fd, const char *path, int flags);
int dfs_file_close(struct dfs_fd* fd); int dfs_file_close(struct dfs_fd *fd);
int dfs_file_ioctl(struct dfs_fd* fd, int cmd, void *args); int dfs_file_ioctl(struct dfs_fd *fd, int cmd, void *args);
int dfs_file_read(struct dfs_fd* fd, void *buf, rt_size_t len); int dfs_file_read(struct dfs_fd *fd, void *buf, rt_size_t len);
int dfs_file_getdents(struct dfs_fd* fd, struct dirent* dirp, rt_size_t nbytes); int dfs_file_getdents(struct dfs_fd *fd, struct dirent *dirp, rt_size_t nbytes);
int dfs_file_unlink(const char *path); int dfs_file_unlink(const char *path);
int dfs_file_write(struct dfs_fd* fd, const void *buf, rt_size_t len); int dfs_file_write(struct dfs_fd *fd, const void *buf, rt_size_t len);
int dfs_file_lseek(struct dfs_fd* fd, rt_off_t offset); int dfs_file_lseek(struct dfs_fd *fd, rt_off_t offset);
int dfs_file_stat(const char *path, struct stat *buf); int dfs_file_stat(const char *path, struct stat *buf);
int dfs_file_rename(const char* oldpath, const char* newpath); int dfs_file_rename(const char *oldpath, const char *newpath);
#endif #endif
/* /*
* File : dfs_fs.h * File : dfs_fs.h
* This file is part of Device File System in RT-Thread RTOS * This file is part of Device File System in RT-Thread RTOS
* COPYRIGHT (C) 2004-2010, RT-Thread Development Team * COPYRIGHT (C) 2004-2011, RT-Thread Development Team
* *
* The license and distribution terms for this file may be * The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at * found in the file LICENSE in this distribution or at
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
* Date Author Notes * Date Author Notes
* 2005-02-22 Bernard 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,25 +27,25 @@ struct dfs_filesystem_operation ...@@ -26,25 +27,25 @@ struct dfs_filesystem_operation
char *name; char *name;
/* mount and unmount file system */ /* 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);
/* make a file system */ /* make a file system */
int (*mkfs) (const char* device_name); int (*mkfs) (const char *device_name);
int (*statfs) (struct dfs_filesystem* fs, struct statfs *buf); int (*statfs) (struct dfs_filesystem *fs, struct statfs *buf);
int (*open) (struct dfs_fd* fd); 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 (*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 dirent* dirp, rt_uint32_t count); int (*getdents) (struct dfs_fd *fd, struct dirent *dirp, rt_uint32_t count);
int (*unlink) (struct dfs_filesystem* fs, const char* pathname); int (*unlink) (struct dfs_filesystem *fs, const char *pathname);
int (*stat) (struct dfs_filesystem* fs, const char* filename, struct stat* buf); int (*stat) (struct dfs_filesystem *fs, const char *filename, struct stat *buf);
int (*rename) (struct dfs_filesystem* fs, const char* oldpath, const char* newpath); int (*rename) (struct dfs_filesystem *fs, const char *oldpath, const char *newpath);
}; };
/* Mounted file system */ /* Mounted file system */
...@@ -52,8 +53,8 @@ struct dfs_filesystem ...@@ -52,8 +53,8 @@ struct dfs_filesystem
{ {
rt_device_t dev_id; /* Attached device */ rt_device_t dev_id; /* Attached device */
char* path; /* File system mount point */ char *path; /* File system mount point */
const struct dfs_filesystem_operation* ops; /* Operations for file system type */ const struct dfs_filesystem_operation *ops; /* Operations for file system type */
void *data; /* Specific file system data */ void *data; /* Specific file system data */
}; };
...@@ -67,23 +68,23 @@ struct dfs_partition ...@@ -67,23 +68,23 @@ struct dfs_partition
rt_sem_t lock; rt_sem_t lock;
}; };
int dfs_register(const 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);
int dfs_mount(const char* device_name, const char* path, int dfs_mount(const char *device_name, const char *path,
const char* filesystemtype, rt_uint32_t rwflag, const const char *filesystemtype, rt_uint32_t rwflag, const
void* data); void *data);
int dfs_unmount(const char *specialfile); int dfs_unmount(const char *specialfile);
/* extern variable */ /* extern variable */
extern const 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 statfs* buffer); int dfs_statfs(const char *path, struct statfs *buffer);
#endif #endif
/* /*
* File : dfs_init.h
+------------------------------------------------------------------------------ * This file is part of Device File System in RT-Thread RTOS
* COPYRIGHT (C) 2004-2011, RT-Thread Development Team
| Project : Device Filesystem *
* The license and distribution terms for this file may be
+------------------------------------------------------------------------------ * found in the file LICENSE in this distribution or at
* http://www.rt-thread.org/license/LICENSE.
| Copyright 2004, 2005 www.fayfayspace.org. *
* Change Logs:
| All rights reserved. * Date Author Notes
* 2005-02-21 Bernard The first version.
|------------------------------------------------------------------------------ */
| File : dfs_init.h, the initilization definitions of Device FileSystem
|------------------------------------------------------------------------------
| Chang Logs:
| Date Author Notes
| 2005-02-21 ffxz The first version.
+------------------------------------------------------------------------------
*/
#ifndef __DFS_INIT_H__ #ifndef __DFS_INIT_H__
#define __DFS_INIT_H__ #define __DFS_INIT_H__
......
/* /*
* File : dfs_def.h * File : dfs_def.h
* This file is part of Device File System in RT-Thread RTOS * This file is part of Device File System in RT-Thread RTOS
* COPYRIGHT (C) 2004-2010, RT-Thread Development Team * COPYRIGHT (C) 2004-2011, RT-Thread Development Team
* *
* The license and distribution terms for this file may be * The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at * found in the file LICENSE in this distribution or at
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
* 2010-07-18 Bernard add stat and statfs structure definitions. * 2010-07-18 Bernard add stat and statfs structure definitions.
* 2011-05-16 Yi.qiu Change parameter name of rename, "new" is C++ key word. * 2011-05-16 Yi.qiu Change parameter name of rename, "new" is C++ key word.
*/ */
#ifndef __DFS_POSIX_H__ #ifndef __DFS_POSIX_H__
#define __DFS_POSIX_H__ #define __DFS_POSIX_H__
...@@ -82,9 +83,9 @@ typedef struct ...@@ -82,9 +83,9 @@ typedef struct
} DIR; } DIR;
/* directory api*/ /* directory api*/
int mkdir (const char *path, mode_t mode); int mkdir(const char *path, mode_t mode);
DIR* opendir(const char* name); DIR *opendir(const char *name);
struct dirent* readdir(DIR *d); struct dirent *readdir(DIR *d);
long telldir(DIR *d); long telldir(DIR *d);
void seekdir(DIR *d, off_t offset); void seekdir(DIR *d, off_t offset);
void rewinddir(DIR *d); void rewinddir(DIR *d);
......
/* /*
* File : dfs.c * File : dfs.c
* This file is part of Device File System in RT-Thread RTOS * This file is part of Device File System in RT-Thread RTOS
* COPYRIGHT (C) 2004-2010, RT-Thread Development Team * COPYRIGHT (C) 2004-2011, RT-Thread Development Team
* *
* The license and distribution terms for this file may be * The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at * found in the file LICENSE in this distribution or at
...@@ -10,7 +10,6 @@ ...@@ -10,7 +10,6 @@
* Change Logs: * Change Logs:
* Date Author Notes * Date Author Notes
* 2005-02-22 Bernard The first version. * 2005-02-22 Bernard The first version.
* 2010-07-16
*/ */
#include <dfs.h> #include <dfs.h>
...@@ -20,7 +19,7 @@ ...@@ -20,7 +19,7 @@
#define NO_WORKING_DIR "system does not support working dir\n" #define NO_WORKING_DIR "system does not support working dir\n"
/* Global variables */ /* Global variables */
const struct dfs_filesystem_operation* filesystem_operation_table[DFS_FILESYSTEM_TYPES_MAX]; const struct dfs_filesystem_operation *filesystem_operation_table[DFS_FILESYSTEM_TYPES_MAX];
struct dfs_filesystem filesystem_table[DFS_FILESYSTEMS_MAX]; struct dfs_filesystem filesystem_table[DFS_FILESYSTEMS_MAX];
/* device filesystem lock */ /* device filesystem lock */
...@@ -44,7 +43,7 @@ struct dfs_fd fd_table[DFS_FD_MAX]; ...@@ -44,7 +43,7 @@ struct dfs_fd fd_table[DFS_FD_MAX];
/** /**
* this function will initialize device file system. * this function will initialize device file system.
*/ */
void dfs_init() void dfs_init(void)
{ {
/* clear filesystem operations table */ /* clear filesystem operations table */
rt_memset(filesystem_operation_table, 0, sizeof(filesystem_operation_table)); rt_memset(filesystem_operation_table, 0, sizeof(filesystem_operation_table));
...@@ -68,7 +67,7 @@ void dfs_init() ...@@ -68,7 +67,7 @@ void dfs_init()
* *
* @note please don't invoke it on ISR. * @note please don't invoke it on ISR.
*/ */
void dfs_lock() void dfs_lock(void)
{ {
rt_err_t result; rt_err_t result;
...@@ -84,7 +83,7 @@ void dfs_lock() ...@@ -84,7 +83,7 @@ void dfs_lock()
* *
* @note please don't invoke it on ISR. * @note please don't invoke it on ISR.
*/ */
void dfs_unlock() void dfs_unlock(void)
{ {
rt_mutex_release(&fslock); rt_mutex_release(&fslock);
} }
...@@ -97,7 +96,7 @@ void dfs_unlock() ...@@ -97,7 +96,7 @@ void dfs_unlock()
*/ */
int fd_new(void) int fd_new(void)
{ {
struct dfs_fd* d; struct dfs_fd *d;
int idx; int idx;
/* lock filesystem */ /* lock filesystem */
...@@ -138,14 +137,16 @@ __result: ...@@ -138,14 +137,16 @@ __result:
* @return NULL on on this file descriptor or the file descriptor structure * @return NULL on on this file descriptor or the file descriptor structure
* pointer. * pointer.
*/ */
struct dfs_fd* fd_get(int fd) struct dfs_fd *fd_get(int fd)
{ {
struct dfs_fd* d; struct dfs_fd *d;
#ifdef DFS_USING_STDIO #ifdef DFS_USING_STDIO
if ( fd < 3 || fd > DFS_FD_MAX + 3) return RT_NULL; if (fd < 3 || fd > DFS_FD_MAX + 3)
return RT_NULL;
#else #else
if ( fd < 0 || fd > DFS_FD_MAX ) return RT_NULL; if (fd < 0 || fd > DFS_FD_MAX)
return RT_NULL;
#endif #endif
dfs_lock(); dfs_lock();
...@@ -163,13 +164,13 @@ struct dfs_fd* fd_get(int fd) ...@@ -163,13 +164,13 @@ struct dfs_fd* fd_get(int fd)
* *
* This function will put the file descriptor. * This function will put the file descriptor.
*/ */
void fd_put(struct dfs_fd* fd) void fd_put(struct dfs_fd *fd)
{ {
dfs_lock(); dfs_lock();
fd->ref_count --; fd->ref_count --;
/* clear this fd entry */ /* clear this fd entry */
if ( fd->ref_count == 0 ) if (fd->ref_count == 0)
{ {
rt_memset(fd, 0, sizeof(struct dfs_fd)); rt_memset(fd, 0, sizeof(struct dfs_fd));
} }
...@@ -185,12 +186,12 @@ void fd_put(struct dfs_fd* fd) ...@@ -185,12 +186,12 @@ void fd_put(struct dfs_fd* fd)
* *
* @return 0 on file has been open successfully, -1 on open failed. * @return 0 on file has been open successfully, -1 on open failed.
*/ */
int fd_is_open(const char* pathname) int fd_is_open(const char *pathname)
{ {
char *fullpath; char *fullpath;
unsigned int index; unsigned int index;
struct dfs_filesystem* fs; struct dfs_filesystem *fs;
struct dfs_fd* fd; struct dfs_fd *fd;
fullpath = dfs_normalize_path(RT_NULL, pathname); fullpath = dfs_normalize_path(RT_NULL, pathname);
if (fullpath != RT_NULL) if (fullpath != RT_NULL)
...@@ -207,16 +208,17 @@ int fd_is_open(const char* pathname) ...@@ -207,16 +208,17 @@ int fd_is_open(const char* pathname)
/* get file path name under mounted file system */ /* get file path name under mounted file system */
if (fs->path[0] == '/' && fs->path[1] == '\0') if (fs->path[0] == '/' && fs->path[1] == '\0')
mountpath = fullpath; mountpath = fullpath;
else mountpath = fullpath + strlen(fs->path); else
mountpath = fullpath + strlen(fs->path);
dfs_lock(); dfs_lock();
for (index = 0; index < DFS_FD_MAX; index++) for (index = 0; index < DFS_FD_MAX; index++)
{ {
fd = &(fd_table[index]); fd = &(fd_table[index]);
if (fd->fs == RT_NULL) continue; if (fd->fs == RT_NULL)
continue;
if (fd->fs == fs && if (fd->fs == fs && strcmp(fd->path, mountpath) == 0)
strcmp(fd->path, mountpath) == 0)
{ {
/* found file in file descriptor table */ /* found file in file descriptor table */
rt_free(fullpath); rt_free(fullpath);
...@@ -240,9 +242,9 @@ int fd_is_open(const char* pathname) ...@@ -240,9 +242,9 @@ int fd_is_open(const char* pathname)
* *
* @return the subdir pointer in filename * @return the subdir pointer in filename
*/ */
const char* dfs_subdir(const char* directory, const char* filename) const char *dfs_subdir(const char *directory, const char *filename)
{ {
const char* dir; const char *dir;
if (strlen(directory) == strlen(filename)) /* it's a same path */ if (strlen(directory) == strlen(filename)) /* it's a same path */
return RT_NULL; return RT_NULL;
...@@ -263,7 +265,7 @@ const char* dfs_subdir(const char* directory, const char* filename) ...@@ -263,7 +265,7 @@ const char* dfs_subdir(const char* directory, const char* filename)
* *
* @return the built full file path (absoluted path) * @return the built full file path (absoluted path)
*/ */
char* dfs_normalize_path(const char* directory, const char* filename) char *dfs_normalize_path(const char *directory, const char *filename)
{ {
char *fullpath; char *fullpath;
char *dst0, *dst, *src; char *dst0, *dst, *src;
...@@ -311,7 +313,8 @@ char* dfs_normalize_path(const char* directory, const char* filename) ...@@ -311,7 +313,8 @@ char* dfs_normalize_path(const char* directory, const char* filename)
/* './' case */ /* './' case */
src += 2; src += 2;
while ((*src == '/') && (*src != '\0')) src ++; while ((*src == '/') && (*src != '\0'))
src ++;
continue; continue;
} }
else if (src[1] == '.') else if (src[1] == '.')
...@@ -327,37 +330,47 @@ char* dfs_normalize_path(const char* directory, const char* filename) ...@@ -327,37 +330,47 @@ char* dfs_normalize_path(const char* directory, const char* filename)
/* '../' case */ /* '../' case */
src += 3; src += 3;
while ((*src == '/') && (*src != '\0')) src ++; while ((*src == '/') && (*src != '\0'))
src ++;
goto up_one; goto up_one;
} }
} }
} }
/* copy up the next '/' and erase all '/' */ /* copy up the next '/' and erase all '/' */
while ((c = *src++) != '\0' && c != '/') *dst ++ = c; while ((c = *src++) != '\0' && c != '/')
*dst ++ = c;
if (c == '/') if (c == '/')
{ {
*dst ++ = '/'; *dst ++ = '/';
while (c == '/') c = *src++; while (c == '/')
c = *src++;
src --; src --;
} }
else if (!c) break; else if (!c)
break;
continue; continue;
up_one: up_one:
dst --; dst --;
if (dst < dst0) { rt_free(fullpath); return NULL;} if (dst < dst0)
while (dst0 < dst && dst[-1] != '/') dst --; {
rt_free(fullpath);
return NULL;
}
while (dst0 < dst && dst[-1] != '/')
dst --;
} }
*dst = '\0'; *dst = '\0';
/* remove '/' in the end of path if exist */ /* remove '/' in the end of path if exist */
dst --; dst --;
if ((dst != fullpath) && (*dst == '/')) *dst = '\0'; if ((dst != fullpath) && (*dst == '/'))
*dst = '\0';
return fullpath; return fullpath;
} }
......
/* /*
* File : dfs_file.c * File : dfs_file.c
* This file is part of Device File System in RT-Thread RTOS * This file is part of Device File System in RT-Thread RTOS
* COPYRIGHT (C) 2004-2010, RT-Thread Development Team * COPYRIGHT (C) 2004-2011, RT-Thread Development Team
* *
* The license and distribution terms for this file may be * The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at * found in the file LICENSE in this distribution or at
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
* Date Author Notes * Date Author Notes
* 2005-02-22 Bernard The first version. * 2005-02-22 Bernard The first version.
*/ */
#include <dfs.h> #include <dfs.h>
#include <dfs_file.h> #include <dfs_file.h>
...@@ -28,14 +29,15 @@ ...@@ -28,14 +29,15 @@
* *
* @return 0 on successful, -1 on failed. * @return 0 on successful, -1 on failed.
*/ */
int dfs_file_open(struct dfs_fd* fd, const char *path, int flags) int dfs_file_open(struct dfs_fd *fd, const char *path, int flags)
{ {
struct dfs_filesystem* fs; struct dfs_filesystem *fs;
char *fullpath; char *fullpath;
int result; int result;
/* parameter check */ /* parameter check */
if ( fd == RT_NULL ) return -DFS_STATUS_EINVAL; if (fd == RT_NULL)
return -DFS_STATUS_EINVAL;
/* make sure we have an absolute path */ /* make sure we have an absolute path */
fullpath = dfs_normalize_path(RT_NULL, path); fullpath = dfs_normalize_path(RT_NULL, path);
...@@ -48,7 +50,7 @@ int dfs_file_open(struct dfs_fd* fd, const char *path, int flags) ...@@ -48,7 +50,7 @@ int dfs_file_open(struct dfs_fd* fd, const char *path, int flags)
/* find filesystem */ /* find filesystem */
fs = dfs_filesystem_lookup(fullpath); fs = dfs_filesystem_lookup(fullpath);
if ( fs == RT_NULL ) if (fs == RT_NULL)
{ {
rt_free(fullpath); /* release path */ rt_free(fullpath); /* release path */
return -DFS_STATUS_ENOENT; return -DFS_STATUS_ENOENT;
...@@ -92,7 +94,7 @@ int dfs_file_open(struct dfs_fd* fd, const char *path, int flags) ...@@ -92,7 +94,7 @@ int dfs_file_open(struct dfs_fd* fd, const char *path, int flags)
} }
fd->flags |= DFS_F_OPEN; fd->flags |= DFS_F_OPEN;
if ( flags & DFS_O_DIRECTORY ) if (flags & DFS_O_DIRECTORY)
{ {
fd->type = FT_DIRECTORY; fd->type = FT_DIRECTORY;
fd->flags |= DFS_F_DIRECTORY; fd->flags |= DFS_F_DIRECTORY;
...@@ -109,14 +111,16 @@ int dfs_file_open(struct dfs_fd* fd, const char *path, int flags) ...@@ -109,14 +111,16 @@ int dfs_file_open(struct dfs_fd* fd, const char *path, int flags)
* *
* @return 0 on successful, -1 on failed. * @return 0 on successful, -1 on failed.
*/ */
int dfs_file_close(struct dfs_fd* fd) int dfs_file_close(struct dfs_fd *fd)
{ {
int result = 0; int result = 0;
if (fd != RT_NULL && fd->fs->ops->close != RT_NULL) result = fd->fs->ops->close(fd); if (fd != RT_NULL && fd->fs->ops->close != RT_NULL)
result = fd->fs->ops->close(fd);
/* close fd error, return */ /* close fd error, return */
if ( result < 0 ) return result; if (result < 0)
return result;
rt_free(fd->path); rt_free(fd->path);
rt_memset(fd, 0, sizeof(struct dfs_fd)); rt_memset(fd, 0, sizeof(struct dfs_fd));
...@@ -133,14 +137,16 @@ int dfs_file_close(struct dfs_fd* fd) ...@@ -133,14 +137,16 @@ int dfs_file_close(struct dfs_fd* fd)
* *
* @return 0 on successful, -1 on failed. * @return 0 on successful, -1 on failed.
*/ */
int dfs_file_ioctl(struct dfs_fd* fd, int cmd, void *args) int dfs_file_ioctl(struct dfs_fd *fd, int cmd, void *args)
{ {
struct dfs_filesystem* fs; struct dfs_filesystem *fs;
if (fd == RT_NULL || fd->type != FT_REGULAR) return -DFS_STATUS_EINVAL; if (fd == RT_NULL || fd->type != FT_REGULAR)
return -DFS_STATUS_EINVAL;
fs = fd->fs; fs = fd->fs;
if (fs->ops->ioctl != RT_NULL) return fs->ops->ioctl(fd, cmd, args); if (fs->ops->ioctl != RT_NULL)
return fs->ops->ioctl(fd, cmd, args);
return -DFS_STATUS_ENOSYS; return -DFS_STATUS_ENOSYS;
} }
...@@ -154,17 +160,20 @@ int dfs_file_ioctl(struct dfs_fd* fd, int cmd, void *args) ...@@ -154,17 +160,20 @@ int dfs_file_ioctl(struct dfs_fd* fd, int cmd, void *args)
* *
* @return the actual read data bytes or 0 on end of file or failed. * @return the actual read data bytes or 0 on end of file or failed.
*/ */
int dfs_file_read(struct dfs_fd* fd, void *buf, rt_size_t len) int dfs_file_read(struct dfs_fd *fd, void *buf, rt_size_t len)
{ {
struct dfs_filesystem* fs; struct dfs_filesystem *fs;
int result = 0; int result = 0;
if (fd == RT_NULL) return -DFS_STATUS_EINVAL; if (fd == RT_NULL)
return -DFS_STATUS_EINVAL;
fs = (struct dfs_filesystem*) fd->fs; fs = (struct dfs_filesystem *)fd->fs;
if (fs->ops->read == RT_NULL) return -DFS_STATUS_ENOSYS; if (fs->ops->read == RT_NULL)
return -DFS_STATUS_ENOSYS;
if ( (result = fs->ops->read(fd, buf, len)) < 0 ) fd->flags |= DFS_F_EOF; if ((result = fs->ops->read(fd, buf, len)) < 0)
fd->flags |= DFS_F_EOF;
return result; return result;
} }
...@@ -178,15 +187,17 @@ int dfs_file_read(struct dfs_fd* fd, void *buf, rt_size_t len) ...@@ -178,15 +187,17 @@ int dfs_file_read(struct dfs_fd* fd, void *buf, rt_size_t len)
* *
* @return the read dirent, others on failed. * @return the read dirent, others on failed.
*/ */
int dfs_file_getdents(struct dfs_fd* fd, struct dirent* dirp, rt_size_t nbytes) int dfs_file_getdents(struct dfs_fd *fd, struct dirent *dirp, rt_size_t nbytes)
{ {
struct dfs_filesystem* fs; struct dfs_filesystem *fs;
/* parameter check */ /* parameter check */
if (fd == RT_NULL || fd->type != FT_DIRECTORY) return -DFS_STATUS_EINVAL; if (fd == RT_NULL || fd->type != FT_DIRECTORY)
return -DFS_STATUS_EINVAL;
fs = (struct dfs_filesystem*) fd->fs; fs = (struct dfs_filesystem *)fd->fs;
if (fs->ops->getdents != RT_NULL) return fs->ops->getdents(fd, dirp, nbytes); if (fs->ops->getdents != RT_NULL)
return fs->ops->getdents(fd, dirp, nbytes);
return -DFS_STATUS_ENOSYS; return -DFS_STATUS_ENOSYS;
} }
...@@ -202,19 +213,19 @@ int dfs_file_unlink(const char *path) ...@@ -202,19 +213,19 @@ int dfs_file_unlink(const char *path)
{ {
int result; int result;
char *fullpath; char *fullpath;
struct dfs_filesystem* fs; struct dfs_filesystem *fs;
result = DFS_STATUS_OK; result = DFS_STATUS_OK;
/* Make sure we have an absolute path */ /* Make sure we have an absolute path */
fullpath = dfs_normalize_path(RT_NULL, path); fullpath = dfs_normalize_path(RT_NULL, path);
if ( fullpath == RT_NULL) if (fullpath == RT_NULL)
{ {
return -DFS_STATUS_EINVAL; return -DFS_STATUS_EINVAL;
} }
/* get filesystem */ /* get filesystem */
if ( (fs = dfs_filesystem_lookup(fullpath)) == RT_NULL) if ((fs = dfs_filesystem_lookup(fullpath)) == RT_NULL)
{ {
result = -DFS_STATUS_ENOENT; result = -DFS_STATUS_ENOENT;
goto __exit; goto __exit;
...@@ -250,14 +261,16 @@ __exit: ...@@ -250,14 +261,16 @@ __exit:
* *
* @return the actual written data length. * @return the actual written data length.
*/ */
int dfs_file_write(struct dfs_fd* fd, const void *buf, rt_size_t len) int dfs_file_write(struct dfs_fd *fd, const void *buf, rt_size_t len)
{ {
struct dfs_filesystem* fs; struct dfs_filesystem *fs;
if (fd == RT_NULL) return -DFS_STATUS_EINVAL; if (fd == RT_NULL)
return -DFS_STATUS_EINVAL;
fs = fd->fs; fs = fd->fs;
if (fs->ops->write == RT_NULL) return -DFS_STATUS_ENOSYS; if (fs->ops->write == RT_NULL)
return -DFS_STATUS_ENOSYS;
return fs->ops->write(fd, buf, len); return fs->ops->write(fd, buf, len);
} }
...@@ -269,14 +282,16 @@ int dfs_file_write(struct dfs_fd* fd, const void *buf, rt_size_t len) ...@@ -269,14 +282,16 @@ int dfs_file_write(struct dfs_fd* fd, const void *buf, rt_size_t len)
* *
* @return 0 on successful, -1 on failed. * @return 0 on successful, -1 on failed.
*/ */
int dfs_file_flush(struct dfs_fd* fd) int dfs_file_flush(struct dfs_fd *fd)
{ {
struct dfs_filesystem* fs; struct dfs_filesystem *fs;
if (fd == RT_NULL) return -DFS_STATUS_EINVAL; if (fd == RT_NULL)
return -DFS_STATUS_EINVAL;
fs = fd->fs; fs = fd->fs;
if (fs->ops->flush == RT_NULL) return -DFS_STATUS_ENOSYS; if (fs->ops->flush == RT_NULL)
return -DFS_STATUS_ENOSYS;
return fs->ops->flush(fd); return fs->ops->flush(fd);
} }
...@@ -289,13 +304,15 @@ int dfs_file_flush(struct dfs_fd* fd) ...@@ -289,13 +304,15 @@ int dfs_file_flush(struct dfs_fd* fd)
* *
* @return the current position after seek. * @return the current position after seek.
*/ */
int dfs_file_lseek(struct dfs_fd* fd, rt_off_t offset) int dfs_file_lseek(struct dfs_fd *fd, rt_off_t offset)
{ {
int result; int result;
struct dfs_filesystem* fs = fd->fs; struct dfs_filesystem *fs = fd->fs;
if (fd == RT_NULL) return -DFS_STATUS_EINVAL; if (fd == RT_NULL)
if (fs->ops->lseek == RT_NULL) return -DFS_STATUS_ENOSYS; return -DFS_STATUS_EINVAL;
if (fs->ops->lseek == RT_NULL)
return -DFS_STATUS_ENOSYS;
result = fs->ops->lseek(fd, offset); result = fs->ops->lseek(fd, offset);
...@@ -317,11 +334,11 @@ int dfs_file_lseek(struct dfs_fd* fd, rt_off_t offset) ...@@ -317,11 +334,11 @@ int dfs_file_lseek(struct dfs_fd* fd, rt_off_t offset)
int dfs_file_stat(const char *path, struct stat *buf) int dfs_file_stat(const char *path, struct stat *buf)
{ {
int result; int result;
char* fullpath; char *fullpath;
struct dfs_filesystem* fs; struct dfs_filesystem *fs;
fullpath = dfs_normalize_path(RT_NULL, path); fullpath = dfs_normalize_path(RT_NULL, path);
if ( fullpath == RT_NULL ) if (fullpath == RT_NULL)
{ {
return -1; return -1;
} }
...@@ -339,7 +356,7 @@ int dfs_file_stat(const char *path, struct stat *buf) ...@@ -339,7 +356,7 @@ int dfs_file_stat(const char *path, struct stat *buf)
/* it's the root directory */ /* it's the root directory */
buf->st_dev = 0; buf->st_dev = 0;
buf->st_mode = DFS_S_IRUSR | DFS_S_IRGRP | DFS_S_IROTH | buf->st_mode = DFS_S_IRUSR | DFS_S_IRGRP | DFS_S_IROTH |
DFS_S_IWUSR | DFS_S_IWGRP | DFS_S_IWOTH; 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_mode |= DFS_S_IFDIR | DFS_S_IXUSR | DFS_S_IXGRP | DFS_S_IXOTH;
...@@ -378,7 +395,7 @@ int dfs_file_stat(const char *path, struct stat *buf) ...@@ -378,7 +395,7 @@ int dfs_file_stat(const char *path, struct stat *buf)
* *
* @return 0 on successful, -1 on failed. * @return 0 on successful, -1 on failed.
*/ */
int dfs_file_rename(const char* oldpath, const char* newpath) int dfs_file_rename(const char *oldpath, const char *newpath)
{ {
int result; int result;
struct dfs_filesystem *oldfs, *newfs; struct dfs_filesystem *oldfs, *newfs;
...@@ -388,34 +405,34 @@ int dfs_file_rename(const char* oldpath, const char* newpath) ...@@ -388,34 +405,34 @@ int dfs_file_rename(const char* oldpath, const char* newpath)
newfullpath = RT_NULL; newfullpath = RT_NULL;
oldfullpath = dfs_normalize_path(RT_NULL, oldpath); oldfullpath = dfs_normalize_path(RT_NULL, oldpath);
if ( oldfullpath == RT_NULL ) if (oldfullpath == RT_NULL)
{ {
result = -DFS_STATUS_ENOENT; result = -DFS_STATUS_ENOENT;
goto __exit; goto __exit;
} }
newfullpath = dfs_normalize_path(RT_NULL, newpath); newfullpath = dfs_normalize_path(RT_NULL, newpath);
if ( newfullpath == RT_NULL ) if (newfullpath == RT_NULL)
{ {
result = -DFS_STATUS_ENOENT; result = -DFS_STATUS_ENOENT;
goto __exit; goto __exit;
} }
if ( (oldfs = dfs_filesystem_lookup(oldfullpath)) == RT_NULL ) if ((oldfs = dfs_filesystem_lookup(oldfullpath)) == RT_NULL)
{ {
result = -DFS_STATUS_ENOENT; result = -DFS_STATUS_ENOENT;
goto __exit; goto __exit;
} }
if ( (newfs = dfs_filesystem_lookup(newfullpath)) == RT_NULL ) if ((newfs = dfs_filesystem_lookup(newfullpath)) == RT_NULL)
{ {
result = -DFS_STATUS_ENOENT; result = -DFS_STATUS_ENOENT;
goto __exit; goto __exit;
} }
if ( oldfs == newfs ) if (oldfs == newfs)
{ {
if ( oldfs->ops->rename == RT_NULL ) if (oldfs->ops->rename == RT_NULL)
{ {
result = -DFS_STATUS_ENOSYS; result = -DFS_STATUS_ENOSYS;
goto __exit; goto __exit;
...@@ -440,7 +457,7 @@ __exit: ...@@ -440,7 +457,7 @@ __exit:
static struct dfs_fd fd; static struct dfs_fd fd;
static struct dirent dirent; static struct dirent dirent;
void ls(const char* pathname) void ls(const char *pathname)
{ {
struct stat stat; struct stat stat;
int length; int length;
...@@ -459,24 +476,25 @@ void ls(const char* pathname) ...@@ -459,24 +476,25 @@ void ls(const char* pathname)
} }
else else
{ {
path = (char*)pathname; path = (char *)pathname;
} }
/* list directory */ /* list directory */
if ( dfs_file_open(&fd, path, DFS_O_DIRECTORY) == 0 ) if (dfs_file_open(&fd, path, DFS_O_DIRECTORY) == 0)
{ {
rt_kprintf("Directory %s:\n", path); rt_kprintf("Directory %s:\n", path);
do do
{ {
rt_memset(&dirent, 0, sizeof(struct dirent)); rt_memset(&dirent, 0, sizeof(struct dirent));
length = dfs_file_getdents(&fd, &dirent, sizeof(struct dirent)); length = dfs_file_getdents(&fd, &dirent, sizeof(struct dirent));
if ( length > 0 ) if (length > 0)
{ {
rt_memset(&stat, 0, sizeof(struct stat)); rt_memset(&stat, 0, sizeof(struct stat));
/* build full path for each file */ /* build full path for each file */
fullpath = dfs_normalize_path(path, dirent.d_name); fullpath = dfs_normalize_path(path, dirent.d_name);
if (fullpath == RT_NULL) break; if (fullpath == RT_NULL)
break;
if (dfs_file_stat(fullpath, &stat) == 0) if (dfs_file_stat(fullpath, &stat) == 0)
{ {
...@@ -502,11 +520,12 @@ void ls(const char* pathname) ...@@ -502,11 +520,12 @@ void ls(const char* pathname)
{ {
rt_kprintf("No such directory\n"); rt_kprintf("No such directory\n");
} }
if (pathname == RT_NULL) rt_free(path); if (pathname == RT_NULL)
rt_free(path);
} }
FINSH_FUNCTION_EXPORT(ls, list directory contents) FINSH_FUNCTION_EXPORT(ls, list directory contents)
void rm(const char* filename) void rm(const char *filename)
{ {
if (dfs_file_unlink(filename) < 0) if (dfs_file_unlink(filename) < 0)
{ {
...@@ -541,7 +560,7 @@ void cat(const char* filename) ...@@ -541,7 +560,7 @@ void cat(const char* filename)
FINSH_FUNCTION_EXPORT(cat, print file) FINSH_FUNCTION_EXPORT(cat, print file)
#define BUF_SZ 4096 #define BUF_SZ 4096
void copy(const char* src, const char* dst) void copy(const char *src, const char *dst)
{ {
struct dfs_fd src_fd; struct dfs_fd src_fd;
rt_uint8_t *block_ptr; rt_uint8_t *block_ptr;
......
/* /*
* File : dfs_fs.c * File : dfs_fs.c
* This file is part of Device File System in RT-Thread RTOS * This file is part of Device File System in RT-Thread RTOS
* COPYRIGHT (C) 2004-2010, RT-Thread Development Team * COPYRIGHT (C) 2004-2011, RT-Thread Development Team
* *
* The license and distribution terms for this file may be * The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at * found in the file LICENSE in this distribution or at
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
* 2010-06-30 Bernard Optimize for RT-Thread RTOS * 2010-06-30 Bernard Optimize for RT-Thread RTOS
* 2011-03-12 Bernard fix the filesystem lookup issue. * 2011-03-12 Bernard fix the filesystem lookup issue.
*/ */
#include <dfs_fs.h> #include <dfs_fs.h>
#include <dfs_file.h> #include <dfs_file.h>
...@@ -28,43 +29,43 @@ ...@@ -28,43 +29,43 @@
* *
* @return 0 on sucessful, -1 on failed. * @return 0 on sucessful, -1 on failed.
*/ */
int dfs_register(const struct dfs_filesystem_operation* ops) int dfs_register(const struct dfs_filesystem_operation *ops)
{ {
int index, result; int index, result;
result = 0; result = 0;
/* lock filesystem */ /* lock filesystem */
dfs_lock(); dfs_lock();
/* check if this filesystem was already registered */ /* check if this filesystem was already registered */
for (index = 0; index < DFS_FILESYSTEM_TYPES_MAX; index++) for (index = 0; index < DFS_FILESYSTEM_TYPES_MAX; index++)
{ {
if (filesystem_operation_table[index] != RT_NULL && if (filesystem_operation_table[index] != RT_NULL &&
strcmp(filesystem_operation_table[index]->name, ops->name) == 0) strcmp(filesystem_operation_table[index]->name, ops->name) == 0)
{ {
result = -1; result = -1;
goto err; goto err;
} }
} }
/* find out an empty filesystem type entry */ /* find out an empty filesystem type entry */
for (index = 0; index < DFS_FILESYSTEM_TYPES_MAX && filesystem_operation_table[index] != RT_NULL; for (index = 0; index < DFS_FILESYSTEM_TYPES_MAX && filesystem_operation_table[index] != RT_NULL;
index++) ; index++) ;
/* filesystem type table full */ /* filesystem type table full */
if (index == DFS_FILESYSTEM_TYPES_MAX) if (index == DFS_FILESYSTEM_TYPES_MAX)
{ {
result = -1; result = -1;
goto err; goto err;
} }
/* save the filesystem's operations */ /* save the filesystem's operations */
filesystem_operation_table[index] = ops; filesystem_operation_table[index] = ops;
err: err:
dfs_unlock(); dfs_unlock();
return result; return result;
} }
/** /**
...@@ -75,42 +76,44 @@ err: ...@@ -75,42 +76,44 @@ err:
* @return the found file system or NULL if no file system mounted on * @return the found file system or NULL if no file system mounted on
* specified path * specified path
*/ */
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;
rt_uint32_t index, fspath, prefixlen; rt_uint32_t index, fspath, prefixlen;
fs = RT_NULL; fs = RT_NULL;
prefixlen = 0; prefixlen = 0;
/* lock filesystem */ /* lock filesystem */
dfs_lock(); dfs_lock();
/* lookup it in the filesystem table */ /* lookup it in the filesystem table */
for (index = 0; index < DFS_FILESYSTEMS_MAX; index++) for (index = 0; index < DFS_FILESYSTEMS_MAX; index++)
{ {
if (filesystem_table[index].path == RT_NULL) continue; if (filesystem_table[index].path == RT_NULL)
continue;
else else
{ {
fspath = strlen(filesystem_table[index].path); fspath = strlen(filesystem_table[index].path);
if (fspath < prefixlen) continue; if (fspath < prefixlen)
continue;
} }
if ((filesystem_table[index].ops != RT_NULL) && if ((filesystem_table[index].ops != RT_NULL) &&
(strncmp(filesystem_table[index].path, path, fspath) == 0)) (strncmp(filesystem_table[index].path, path, fspath) == 0))
{ {
/* check next path separator */ /* check next path separator */
if ( fspath > 1 && (strlen(path) > fspath) && if (fspath > 1 && (strlen(path) > fspath) && (path[fspath] != '/'))
(path[fspath] != '/')) continue; continue;
fs = &filesystem_table[index]; fs = &filesystem_table[index];
prefixlen = fspath; prefixlen = fspath;
} }
} }
dfs_unlock(); dfs_unlock();
return fs; return fs;
} }
/** /**
...@@ -122,69 +125,66 @@ struct dfs_filesystem* dfs_filesystem_lookup(const char *path) ...@@ -122,69 +125,66 @@ struct dfs_filesystem* dfs_filesystem_lookup(const char *path)
* *
* @return RT_EOK on successful or -RT_ERROR on failed. * @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 */
#define DPT_ITEM_SIZE 16 /* partition item size */ #define DPT_ITEM_SIZE 16 /* partition item size */
rt_uint8_t* dpt; rt_uint8_t *dpt;
rt_uint8_t type; rt_uint8_t type;
rt_err_t result; rt_err_t result;
RT_ASSERT(part != RT_NULL); RT_ASSERT(part != RT_NULL);
RT_ASSERT(buf != RT_NULL); RT_ASSERT(buf != RT_NULL);
result = RT_EOK; result = RT_EOK;
dpt = buf + DPT_ADDRESS + pindex * DPT_ITEM_SIZE; dpt = buf + DPT_ADDRESS + pindex * DPT_ITEM_SIZE;
if ((*dpt != 0x80) && (*dpt != 0x00)) if ((*dpt != 0x80) && (*dpt != 0x00))
{ {
/* which is not a partition table */ /* which is not a partition table */
result = -RT_ERROR; result = -RT_ERROR;
return result; return result;
} }
/* get partition type */ /* get partition type */
type = *(dpt+4); type = *(dpt+4);
if (type != 0) if (type != 0)
{ {
/* set partition type */ /* set partition type */
part->type = type; part->type = type;
/* get partition offset and size */ /* get partition offset and size */
part->offset = *(dpt+ 8) | *(dpt+ 9) << 8 | part->offset = *(dpt+8) | *(dpt+9)<<8 | *(dpt+10)<<16 | *(dpt+11)<<24;
*(dpt+10) << 16 | *(dpt+11) << 24; part->size = *(dpt+12) | *(dpt+13)<<8 | *(dpt+14)<<16 | *(dpt+15)<<24;
part->size = *(dpt+12) | *(dpt+13) << 8 |
*(dpt+14) << 16 | *(dpt+15) << 24; rt_kprintf("found part[%d], begin: %d, size: ", pindex, part->offset*512);
if ((part->size>>11) > 0) /* MB */
rt_kprintf("found part[%d], begin: %d, size: ", {
pindex, part->offset * 512); unsigned int part_size;
if ( (part->size>>11) > 0 ) /* MB */ part_size = part->size >> 11;/* MB */
{ if ((part_size>>10) > 0) /* GB */
unsigned int part_size; {
part_size = part->size>>11;/* MB */ rt_kprintf("%d.%d%s",part_size>>10,part_size&0x3FF,"GB\r\n");/* GB */
if ( (part_size>>10) > 0) /* GB */ }
{ else
rt_kprintf("%d.%d%s",part_size>>10,part_size&0x3FF,"GB\r\n");/* GB */ {
} rt_kprintf("%d.%d%s",part_size,(part->size>>1)&0x3FF,"MB\r\n");/* MB */
else }
{ }
rt_kprintf("%d.%d%s",part_size,(part->size>>1)&0x3FF,"MB\r\n");/* MB */ else
} {
} rt_kprintf("%d%s",part->size>>1,"KB\r\n");/* KB */
else }
{ }
rt_kprintf("%d%s",part->size>>1,"KB\r\n");/* KB */ else
} {
} result = -RT_ERROR;
else }
{
result = -RT_ERROR; return result;
}
return result;
} }
/** /**
...@@ -198,17 +198,17 @@ rt_err_t dfs_filesystem_get_partition(struct dfs_partition* part, rt_uint8_t* bu ...@@ -198,17 +198,17 @@ rt_err_t dfs_filesystem_get_partition(struct dfs_partition* part, rt_uint8_t* bu
* *
* @return 0 on successful or -1 on failed. * @return 0 on successful or -1 on 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)
{ {
const 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;
rt_device_t dev_id; rt_device_t dev_id;
int index; int index;
/* open specific device */ /* open specific device */
if (device_name != RT_NULL) if (device_name != RT_NULL)
{ {
dev_id = rt_device_find(device_name); dev_id = rt_device_find(device_name);
...@@ -225,112 +225,117 @@ int dfs_mount(const char* device_name, const char* path, ...@@ -225,112 +225,117 @@ int dfs_mount(const char* device_name, const char* path,
dev_id = RT_NULL; dev_id = RT_NULL;
} }
/* find out specific filesystem */ /* find out specific filesystem */
dfs_lock(); dfs_lock();
for ( index = 0; index < DFS_FILESYSTEM_TYPES_MAX; index++ ) for (index = 0; index < DFS_FILESYSTEM_TYPES_MAX; index++)
{ {
if (strcmp(filesystem_operation_table[index]->name, filesystemtype) == 0)break; if (strcmp(filesystem_operation_table[index]->name, filesystemtype) == 0)
} break;
}
/* can't find filesystem */
if ( index == DFS_FILESYSTEM_TYPES_MAX ) /* can't find filesystem */
{ if (index == DFS_FILESYSTEM_TYPES_MAX)
rt_set_errno(-DFS_STATUS_ENODEV); {
dfs_unlock(); rt_set_errno(-DFS_STATUS_ENODEV);
return -1; dfs_unlock();
} return -1;
ops = filesystem_operation_table[index]; }
dfs_unlock(); ops = filesystem_operation_table[index];
dfs_unlock();
/* make full path for special file */
/* make full path for special file */
fullpath = dfs_normalize_path(RT_NULL, path); fullpath = dfs_normalize_path(RT_NULL, path);
if ( fullpath == RT_NULL) /* not an abstract path */ if (fullpath == RT_NULL) /* not an abstract path */
{ {
rt_set_errno(-DFS_STATUS_ENOTDIR); rt_set_errno(-DFS_STATUS_ENOTDIR);
return -1; return -1;
} }
/* Check if the path exists or not, raw APIs call, fixme */ /* Check if the path exists or not, raw APIs call, fixme */
if ( (strcmp(fullpath, "/") != 0) && (strcmp(fullpath, "/dev") != 0) ) if ((strcmp(fullpath, "/") != 0) && (strcmp(fullpath, "/dev") != 0))
{ {
struct dfs_fd fd; struct dfs_fd fd;
if ( dfs_file_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_free(fullpath);
rt_set_errno(-DFS_STATUS_ENOTDIR); rt_set_errno(-DFS_STATUS_ENOTDIR);
return -1; return -1;
} }
dfs_file_close(&fd); dfs_file_close(&fd);
} }
/* check whether the file system mounted or not */ /* check whether the file system mounted or not */
dfs_lock(); dfs_lock();
for (index =0; index < DFS_FILESYSTEMS_MAX; index++) for (index =0; index < DFS_FILESYSTEMS_MAX; index++)
{ {
if ( filesystem_table[index].ops != RT_NULL && if (filesystem_table[index].ops != RT_NULL &&
strcmp(filesystem_table[index].path, path) == 0 ) strcmp(filesystem_table[index].path, path) == 0)
{ {
rt_set_errno(-DFS_STATUS_EINVAL); rt_set_errno(-DFS_STATUS_EINVAL);
goto err1; goto err1;
} }
} }
/* find out en empty filesystem table entry */ /* find out en empty filesystem table entry */
for (index = 0; index < DFS_FILESYSTEMS_MAX && filesystem_table[index].ops != RT_NULL; for (index = 0; index < DFS_FILESYSTEMS_MAX && filesystem_table[index].ops != RT_NULL;
index++) ; index++) ;
if ( index == DFS_FILESYSTEMS_MAX ) /* can't find en empty filesystem table entry */ if (index == DFS_FILESYSTEMS_MAX) /* can't find en empty filesystem table entry */
{ {
rt_set_errno(-DFS_STATUS_ENOSPC); rt_set_errno(-DFS_STATUS_ENOSPC);
goto err1; goto err1;
} }
/* register file system */ /* register file system */
fs = &(filesystem_table[index]); fs = &(filesystem_table[index]);
fs->path = 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 */
dfs_unlock(); dfs_unlock();
/* open device, but do not check the status of device */ /* open device, but do not check the status of device */
if (dev_id != RT_NULL) rt_device_open(fs->dev_id, RT_DEVICE_OFLAG_RDWR); if (dev_id != RT_NULL)
rt_device_open(fs->dev_id, RT_DEVICE_OFLAG_RDWR);
if (ops->mount == RT_NULL) /* there is no mount implementation */
{ if (ops->mount == RT_NULL) /* there is no mount implementation */
if (dev_id != RT_NULL) rt_device_close(dev_id); {
dfs_lock(); if (dev_id != RT_NULL)
/* clear filesystem table entry */ rt_device_close(dev_id);
rt_memset(fs, 0, sizeof(struct dfs_filesystem)); dfs_lock();
/* clear filesystem table entry */
rt_memset(fs, 0, sizeof(struct dfs_filesystem));
dfs_unlock(); dfs_unlock();
rt_free(fullpath); rt_free(fullpath);
rt_set_errno(-DFS_STATUS_ENOSYS); rt_set_errno(-DFS_STATUS_ENOSYS);
return -1; return -1;
} }
/* call mount of this filesystem */ /* call mount of this filesystem */
else if (ops->mount(fs, rwflag, data) < 0) else if (ops->mount(fs, rwflag, data) < 0)
{ {
/* close device */ /* close device */
if (dev_id != RT_NULL) rt_device_close(fs->dev_id); if (dev_id != RT_NULL)
rt_device_close(fs->dev_id);
/* mount failed */
/* mount failed */
dfs_lock(); dfs_lock();
/* clear filesystem table entry */ /* clear filesystem table entry */
rt_memset(fs, 0, sizeof(struct dfs_filesystem)); rt_memset(fs, 0, sizeof(struct dfs_filesystem));
dfs_unlock(); dfs_unlock();
rt_free(fullpath); rt_free(fullpath);
return -1; return -1;
} }
return 0; return 0;
err1: err1:
dfs_unlock(); dfs_unlock();
if (fullpath != RT_NULL) rt_free(fullpath); if (fullpath != RT_NULL)
rt_free(fullpath);
return -1; return -1;
} }
/** /**
...@@ -342,42 +347,42 @@ err1: ...@@ -342,42 +347,42 @@ err1:
*/ */
int dfs_unmount(const char *specialfile) int dfs_unmount(const char *specialfile)
{ {
char *fullpath; char *fullpath;
struct dfs_filesystem* fs = RT_NULL; struct dfs_filesystem *fs = RT_NULL;
fullpath = dfs_normalize_path(RT_NULL, specialfile); fullpath = dfs_normalize_path(RT_NULL, specialfile);
if (fullpath == RT_NULL) if (fullpath == RT_NULL)
{ {
rt_set_errno(-DFS_STATUS_ENOTDIR); rt_set_errno(-DFS_STATUS_ENOTDIR);
return -1; return -1;
} }
/* lock filesystem */ /* lock filesystem */
dfs_lock(); dfs_lock();
fs = dfs_filesystem_lookup(fullpath); fs = dfs_filesystem_lookup(fullpath);
if (fs != RT_NULL && fs->ops->unmount != RT_NULL && fs->ops->unmount(fs) < 0) if (fs != RT_NULL && fs->ops->unmount != RT_NULL && fs->ops->unmount(fs) < 0)
{ {
goto err1; goto err1;
} }
/* close device, but do not check the status of device */ /* close device, but do not check the status of device */
if (fs->dev_id != RT_NULL) if (fs->dev_id != RT_NULL)
rt_device_close(fs->dev_id); rt_device_close(fs->dev_id);
/* clear this filesystem table entry */ /* clear this filesystem table entry */
rt_memset(fs, 0, sizeof(struct dfs_filesystem)); rt_memset(fs, 0, sizeof(struct dfs_filesystem));
dfs_unlock(); dfs_unlock();
rt_free(fullpath); rt_free(fullpath);
return 0; return 0;
err1: err1:
dfs_unlock(); dfs_unlock();
rt_free(fullpath); rt_free(fullpath);
return -1; return -1;
} }
/** /**
...@@ -388,18 +393,18 @@ err1: ...@@ -388,18 +393,18 @@ err1:
* *
* @return 0 on successful, otherwise failed. * @return 0 on successful, otherwise failed.
*/ */
int dfs_mkfs(const char* fs_name, const char* device_name) int dfs_mkfs(const char *fs_name, const char *device_name)
{ {
int index; int index;
/* lock filesystem */ /* lock filesystem */
dfs_lock(); dfs_lock();
/* find the file system operations */ /* find the file system operations */
for (index = 0; index < DFS_FILESYSTEM_TYPES_MAX; index++) for (index = 0; index < DFS_FILESYSTEM_TYPES_MAX; index++)
{ {
if (filesystem_operation_table[index] != RT_NULL && if (filesystem_operation_table[index] != RT_NULL &&
strcmp(filesystem_operation_table[index]->name, fs_name) == 0) strcmp(filesystem_operation_table[index]->name, fs_name) == 0)
{ {
/* find file system operation */ /* find file system operation */
const struct dfs_filesystem_operation* ops = filesystem_operation_table[index]; const struct dfs_filesystem_operation* ops = filesystem_operation_table[index];
dfs_unlock(); dfs_unlock();
...@@ -408,8 +413,8 @@ int dfs_mkfs(const char* fs_name, const char* device_name) ...@@ -408,8 +413,8 @@ int dfs_mkfs(const char* fs_name, const char* device_name)
return ops->mkfs(device_name); return ops->mkfs(device_name);
break; break;
} }
} }
dfs_unlock(); dfs_unlock();
return -1; return -1;
...@@ -423,9 +428,9 @@ int dfs_mkfs(const char* fs_name, const char* device_name) ...@@ -423,9 +428,9 @@ int dfs_mkfs(const char* fs_name, const char* device_name)
* *
* @return 0 on successful, others on failed. * @return 0 on successful, others on failed.
*/ */
int dfs_statfs(const char* path, struct statfs* buffer) int dfs_statfs(const char *path, struct statfs *buffer)
{ {
struct dfs_filesystem* fs; struct dfs_filesystem *fs;
fs = dfs_filesystem_lookup(path); fs = dfs_filesystem_lookup(path);
if (fs != NULL) if (fs != NULL)
...@@ -439,13 +444,13 @@ int dfs_statfs(const char* path, struct statfs* buffer) ...@@ -439,13 +444,13 @@ int dfs_statfs(const char* path, struct statfs* buffer)
#ifdef RT_USING_FINSH #ifdef RT_USING_FINSH
#include <finsh.h> #include <finsh.h>
void mkfs(const char* fs_name, const char* device_name) void mkfs(const char *fs_name, const char *device_name)
{ {
dfs_mkfs(fs_name, device_name); dfs_mkfs(fs_name, device_name);
} }
FINSH_FUNCTION_EXPORT(mkfs, make a file system); FINSH_FUNCTION_EXPORT(mkfs, make a file system);
void df(const char* path) void df(const char *path)
{ {
struct statfs buffer; struct statfs buffer;
......
/* /*
* File : dfs_posix.c * File : dfs_posix.c
* This file is part of Device File System in RT-Thread RTOS * This file is part of Device File System in RT-Thread RTOS
* COPYRIGHT (C) 2004-2010, RT-Thread Development Team * COPYRIGHT (C) 2004-2011, RT-Thread Development Team
* *
* The license and distribution terms for this file may be * The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at * found in the file LICENSE in this distribution or at
...@@ -33,11 +33,12 @@ ...@@ -33,11 +33,12 @@
int open(const char *file, int flags, int mode) int open(const char *file, int flags, int mode)
{ {
int fd, result; int fd, result;
struct dfs_fd* d; struct dfs_fd *d;
/* allocate a fd */ /* allocate a fd */
fd = fd_new(); fd = fd_new();
if (fd < 0) return -1; if (fd < 0)
return -1;
d = fd_get(fd); d = fd_get(fd);
result = dfs_file_open(d, file, flags); result = dfs_file_open(d, file, flags);
...@@ -68,7 +69,7 @@ int open(const char *file, int flags, int mode) ...@@ -68,7 +69,7 @@ int open(const char *file, int flags, int mode)
int close(int fd) int close(int fd)
{ {
int result; int result;
struct dfs_fd* d; struct dfs_fd *d;
d = fd_get(fd); d = fd_get(fd);
if (d == RT_NULL) if (d == RT_NULL)
...@@ -103,7 +104,7 @@ int close(int fd) ...@@ -103,7 +104,7 @@ int close(int fd)
int read(int fd, void *buf, size_t len) int read(int fd, void *buf, size_t len)
{ {
int result; int result;
struct dfs_fd* d; struct dfs_fd *d;
/* get the fd */ /* get the fd */
d = fd_get(fd); d = fd_get(fd);
...@@ -140,7 +141,7 @@ int read(int fd, void *buf, size_t len) ...@@ -140,7 +141,7 @@ int read(int fd, void *buf, size_t len)
int write(int fd, const void *buf, size_t len) int write(int fd, const void *buf, size_t len)
{ {
int result; int result;
struct dfs_fd* d; struct dfs_fd *d;
/* get the fd */ /* get the fd */
d = fd_get(fd); d = fd_get(fd);
...@@ -177,7 +178,7 @@ int write(int fd, const void *buf, size_t len) ...@@ -177,7 +178,7 @@ int write(int fd, const void *buf, size_t len)
off_t lseek(int fd, off_t offset, int whence) off_t lseek(int fd, off_t offset, int whence)
{ {
int result; int result;
struct dfs_fd* d; struct dfs_fd *d;
d = fd_get(fd); d = fd_get(fd);
if (d == RT_NULL) if (d == RT_NULL)
...@@ -200,7 +201,7 @@ off_t lseek(int fd, off_t offset, int whence) ...@@ -200,7 +201,7 @@ off_t lseek(int fd, off_t offset, int whence)
break; break;
} }
if( offset < 0 ) if(offset < 0)
{ {
rt_set_errno(DFS_STATUS_EINVAL); rt_set_errno(DFS_STATUS_EINVAL);
return -1; return -1;
...@@ -229,7 +230,7 @@ off_t lseek(int fd, off_t offset, int whence) ...@@ -229,7 +230,7 @@ off_t lseek(int fd, off_t offset, int whence)
* *
* note: the old and new file name must be belong to a same file system. * 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;
...@@ -292,7 +293,7 @@ int stat(const char *file, struct stat *buf) ...@@ -292,7 +293,7 @@ int stat(const char *file, struct stat *buf)
*/ */
int fstat(int fildes, struct stat *buf) int fstat(int fildes, struct stat *buf)
{ {
struct dfs_fd* d; struct dfs_fd *d;
/* get the fd */ /* get the fd */
d = fd_get(fildes); d = fd_get(fildes);
...@@ -303,7 +304,7 @@ int fstat(int fildes, struct stat *buf) ...@@ -303,7 +304,7 @@ int fstat(int fildes, struct stat *buf)
} }
/* it's the root directory */ /* it's the root directory */
buf->st_dev = 0; buf->st_dev = 0;
buf->st_mode = DFS_S_IFREG | DFS_S_IRUSR | DFS_S_IRGRP | DFS_S_IROTH | buf->st_mode = DFS_S_IFREG | DFS_S_IRUSR | DFS_S_IRGRP | DFS_S_IROTH |
DFS_S_IWUSR | DFS_S_IWGRP | DFS_S_IWOTH; DFS_S_IWUSR | DFS_S_IWGRP | DFS_S_IWOTH;
...@@ -356,11 +357,15 @@ int statfs(const char *path, struct statfs *buf) ...@@ -356,11 +357,15 @@ int statfs(const char *path, struct statfs *buf)
int mkdir (const char *path, mode_t mode) int mkdir (const char *path, mode_t mode)
{ {
int fd; int fd;
struct dfs_fd* d; struct dfs_fd *d;
int result; int result;
fd = fd_new(); fd = fd_new();
if (fd == -1) { rt_kprintf("no fd\n"); return -1; } if (fd == -1)
{
rt_kprintf("no fd\n");
return -1;
}
d = fd_get(fd); d = fd_get(fd);
...@@ -410,17 +415,21 @@ int rmdir(const char *pathname) ...@@ -410,17 +415,21 @@ int rmdir(const char *pathname)
* *
* @return the DIR pointer of directory, NULL on open failed. * @return the DIR pointer of directory, NULL on open failed.
*/ */
DIR* opendir(const char* name) DIR *opendir(const char *name)
{ {
struct dfs_fd* d; struct dfs_fd *d;
int fd, result; int fd, result;
DIR* t; DIR *t;
t = RT_NULL; t = RT_NULL;
/* allocate a fd */ /* allocate a fd */
fd = fd_new(); fd = fd_new();
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 = dfs_file_open(d, name, DFS_O_RDONLY | DFS_O_DIRECTORY); result = dfs_file_open(d, name, DFS_O_RDONLY | DFS_O_DIRECTORY);
...@@ -459,10 +468,10 @@ DIR* opendir(const char* name) ...@@ -459,10 +468,10 @@ DIR* opendir(const char* name)
* *
* @return the next directory entry, NULL on the end of directory or failed. * @return the next directory entry, NULL on the end of directory or failed.
*/ */
struct dirent* readdir(DIR *d) struct dirent *readdir(DIR *d)
{ {
int result; int result;
struct dfs_fd* fd; struct dfs_fd *fd;
fd = fd_get(d->fd); fd = fd_get(d->fd);
if (fd == RT_NULL) if (fd == RT_NULL)
...@@ -488,7 +497,7 @@ struct dirent* readdir(DIR *d) ...@@ -488,7 +497,7 @@ struct dirent* readdir(DIR *d)
} }
fd_put(fd); fd_put(fd);
return (struct dirent*)(d->buf+d->cur); return (struct dirent *)(d->buf+d->cur);
} }
/** /**
...@@ -501,7 +510,7 @@ struct dirent* readdir(DIR *d) ...@@ -501,7 +510,7 @@ struct dirent* readdir(DIR *d)
*/ */
long telldir(DIR *d) long telldir(DIR *d)
{ {
struct dfs_fd* fd; struct dfs_fd *fd;
long result; long result;
fd = fd_get(d->fd); fd = fd_get(d->fd);
...@@ -526,7 +535,7 @@ long telldir(DIR *d) ...@@ -526,7 +535,7 @@ long telldir(DIR *d)
*/ */
void seekdir(DIR *d, off_t offset) void seekdir(DIR *d, off_t offset)
{ {
struct dfs_fd* fd; struct dfs_fd *fd;
fd = fd_get(d->fd); fd = fd_get(d->fd);
if (fd == RT_NULL) if (fd == RT_NULL)
...@@ -536,7 +545,8 @@ void seekdir(DIR *d, off_t offset) ...@@ -536,7 +545,8 @@ void seekdir(DIR *d, off_t offset)
} }
/* seek to the offset position of directory */ /* seek to the offset position of directory */
if (dfs_file_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);
} }
...@@ -547,7 +557,7 @@ void seekdir(DIR *d, off_t offset) ...@@ -547,7 +557,7 @@ void seekdir(DIR *d, off_t offset)
*/ */
void rewinddir(DIR *d) void rewinddir(DIR *d)
{ {
struct dfs_fd* fd; struct dfs_fd *fd;
fd = fd_get(d->fd); fd = fd_get(d->fd);
if (fd == RT_NULL) if (fd == RT_NULL)
...@@ -557,7 +567,8 @@ void rewinddir(DIR *d) ...@@ -557,7 +567,8 @@ void rewinddir(DIR *d)
} }
/* seek to the beginning of directory */ /* seek to the beginning of directory */
if (dfs_file_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);
} }
...@@ -569,10 +580,10 @@ void rewinddir(DIR *d) ...@@ -569,10 +580,10 @@ void rewinddir(DIR *d)
* *
* @return 0 on successful, -1 on failed. * @return 0 on successful, -1 on failed.
*/ */
int closedir(DIR* d) int closedir(DIR *d)
{ {
int result; int result;
struct dfs_fd* fd; struct dfs_fd *fd;
fd = fd_get(d->fd); fd = fd_get(d->fd);
if (fd == RT_NULL) if (fd == RT_NULL)
...@@ -605,10 +616,10 @@ int closedir(DIR* d) ...@@ -605,10 +616,10 @@ int closedir(DIR* d)
*/ */
int chdir(const char *path) int chdir(const char *path)
{ {
char* fullpath; char *fullpath;
DIR* d; DIR *d;
if(path == RT_NULL) if (path == RT_NULL)
{ {
dfs_lock(); dfs_lock();
rt_kprintf("%s\n", working_directory); rt_kprintf("%s\n", working_directory);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册