fix sdcard.c dfs_fs.c

git-svn-id: https://rt-thread.googlecode.com/svn/trunk@106 bbd45198-f89e-11dd-88c7-29a3b14d5316
This commit is contained in:
wuyangyong 2009-10-18 12:32:10 +00:00
parent d3c6a6abb6
commit 597015a3fe
2 changed files with 3400 additions and 3382 deletions

File diff suppressed because it is too large Load Diff

View File

@ -32,34 +32,34 @@
*/
int dfs_register(struct dfs_filesystem_operation* ops)
{
int index;
int index;
rt_sem_take(filesystem_operation_table_lock, RT_WAITING_FOREVER);
rt_sem_take(filesystem_operation_table_lock, RT_WAITING_FOREVER);
/* check if this filesystem was already registered */
for (index = 0; index < DFS_FILESYSTEM_TYPES_MAX; index++)
{
if (filesystem_operation_table[index] != RT_NULL &&
strcmp(filesystem_operation_table[index]->name, ops->name) == 0)
goto err;
}
/* check if this filesystem was already registered */
for (index = 0; index < DFS_FILESYSTEM_TYPES_MAX; index++)
{
if (filesystem_operation_table[index] != RT_NULL &&
strcmp(filesystem_operation_table[index]->name, ops->name) == 0)
goto err;
}
/* find out an empty filesystem type entry */
for (index = 0; index < DFS_FILESYSTEM_TYPES_MAX && filesystem_operation_table[index] != RT_NULL;
index++) ;
/* find out an empty filesystem type entry */
for (index = 0; index < DFS_FILESYSTEM_TYPES_MAX && filesystem_operation_table[index] != RT_NULL;
index++) ;
/* filesystem type table full */
if (index == DFS_FILESYSTEM_TYPES_MAX) goto err;
/* filesystem type table full */
if (index == DFS_FILESYSTEM_TYPES_MAX) goto err;
/* save the filesystem's operations */
filesystem_operation_table[index] = ops;
/* save the filesystem's operations */
filesystem_operation_table[index] = ops;
rt_sem_release(filesystem_operation_table_lock);
return 0;
rt_sem_release(filesystem_operation_table_lock);
return 0;
err:
rt_sem_release(filesystem_operation_table_lock);
return -1;
rt_sem_release(filesystem_operation_table_lock);
return -1;
}
/*
@ -74,31 +74,31 @@ err:
*/
struct dfs_filesystem* dfs_filesystem_lookup(const char *path)
{
struct dfs_filesystem* fs;
rt_uint32_t index, fspath, prefixlen;
struct dfs_filesystem* fs;
rt_uint32_t index, fspath, prefixlen;
fs = RT_NULL;
prefixlen = 0;
fs = RT_NULL;
prefixlen = 0;
/* lock filesystem table */
rt_sem_take(filesystem_table_lock, RT_WAITING_FOREVER);
/* lock filesystem table */
rt_sem_take(filesystem_table_lock, RT_WAITING_FOREVER);
/* lookup it in the filesystem table */
for (index = 0; index < DFS_FILESYSTEMS_MAX + 1; index++)
{
fspath = strlen(filesystem_table[index].path);
if (fspath < prefixlen) continue;
/* lookup it in the filesystem table */
for (index = 0; index < DFS_FILESYSTEMS_MAX + 1; index++)
{
fspath = strlen(filesystem_table[index].path);
if (fspath < prefixlen) continue;
if ((filesystem_table[index].ops != RT_NULL) &&
str_is_prefix(filesystem_table[index].path, path) == 0)
{
fs = &filesystem_table[index];
prefixlen = fspath;
}
}
if ((filesystem_table[index].ops != RT_NULL) &&
str_is_prefix(filesystem_table[index].path, path) == 0)
{
fs = &filesystem_table[index];
prefixlen = fspath;
}
}
rt_sem_release(filesystem_table_lock);
return fs;
rt_sem_release(filesystem_table_lock);
return fs;
}
rt_err_t dfs_filesystem_get_partition(struct dfs_partition* part, rt_uint8_t* buf, rt_uint32_t pindex)
@ -106,49 +106,66 @@ rt_err_t dfs_filesystem_get_partition(struct dfs_partition* part, rt_uint8_t* bu
#define DPT_ADDRESS 0x1be /* device partition offset in Boot Sector */
#define DPT_ITEM_SIZE 16 /* partition item size */
rt_uint8_t* dpt;
rt_uint8_t type;
rt_err_t result;
rt_uint8_t* dpt;
rt_uint8_t type;
rt_err_t result;
RT_ASSERT(part != RT_NULL);
RT_ASSERT(buf != RT_NULL);
RT_ASSERT(part != 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))
{
/* which is not a partition table */
result = -RT_ERROR;
return result;
}
if ((*dpt != 0x80) && (*dpt != 0x00))
{
/* which is not a partition table */
result = -RT_ERROR;
return result;
}
/* get partition type */
type = *(dpt+4);
/* get partition type */
type = *(dpt+4);
if (type != 0)
{
/* set partition type */
part->type = type;
if (type != 0)
{
/* set partition type */
part->type = type;
/* get partition offset and size */
part->offset = *(dpt+ 8) | *(dpt+ 9) << 8 |
*(dpt+10) << 16 | *(dpt+11) << 24;
part->size = *(dpt+12) | *(dpt+13) << 8 |
*(dpt+14) << 16 | *(dpt+15) << 24;
/* get partition offset and size */
part->offset = *(dpt+ 8) | *(dpt+ 9) << 8 |
*(dpt+10) << 16 | *(dpt+11) << 24;
part->size = *(dpt+12) | *(dpt+13) << 8 |
*(dpt+14) << 16 | *(dpt+15) << 24;
#ifdef RT_USING_FINSH
rt_kprintf("part[%d], begin: %d, size: %d\n",
pindex, part->offset * 512, part->size* 512);
rt_kprintf("part[%d], begin: %d, size: ",
pindex, part->offset * 512);
if ( (part->size>>11) > 0 ) /* MB */
{
unsigned int part_size;
part_size = part->size>>11;/* MB */
if ( (part_size>>10) > 0) /* GB */
{
rt_kprintf("%d.%d%s",part_size>>10,part_size&0x3FF,"GB\r\n");/* GB */
}
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 */
}
#endif
}
else
{
result = -RT_ERROR;
}
}
else
{
result = -RT_ERROR;
}
return result;
return result;
}
/*
@ -168,138 +185,138 @@ rt_err_t dfs_filesystem_get_partition(struct dfs_partition* part, rt_uint8_t* bu
+------------------------------------------------------------------------------
*/
int dfs_mount(const char* device_name, const char* path,
const char* filesystemtype, unsigned long rwflag, const
void* data)
const char* filesystemtype, unsigned long rwflag, const
void* data)
{
struct dfs_filesystem_operation* ops;
struct dfs_filesystem* fs;
char *fullpath=RT_NULL;
struct dfs_filesystem_operation* ops;
struct dfs_filesystem* fs;
char *fullpath=RT_NULL;
#ifdef RT_USING_WORKDIR
char full_path[DFS_PATH_MAX + 1];
char full_path[DFS_PATH_MAX + 1];
#endif
rt_device_t dev_id;
int index;
rt_device_t dev_id;
int index;
/* open specific device */
dev_id = rt_device_find(device_name);
if (dev_id == RT_NULL)
{
/* no this device */
rt_set_errno(-DFS_STATUS_ENODEV);
return -1;
}
/* open specific device */
dev_id = rt_device_find(device_name);
if (dev_id == RT_NULL)
{
/* no this device */
rt_set_errno(-DFS_STATUS_ENODEV);
return -1;
}
/* find out specific filesystem */
rt_sem_take(filesystem_operation_table_lock, RT_WAITING_FOREVER);
for ( index = 0; index < DFS_FILESYSTEM_TYPES_MAX; index++ )
{
if (strcmp(filesystem_operation_table[index]->name, filesystemtype) == 0)break;
}
/* find out specific filesystem */
rt_sem_take(filesystem_operation_table_lock, RT_WAITING_FOREVER);
for ( index = 0; index < DFS_FILESYSTEM_TYPES_MAX; index++ )
{
if (strcmp(filesystem_operation_table[index]->name, filesystemtype) == 0)break;
}
/* can't find filesystem */
if ( index == DFS_FILESYSTEM_TYPES_MAX )
{
rt_set_errno(-DFS_STATUS_ENODEV);
rt_sem_release(filesystem_operation_table_lock);
return -1;
}
ops = filesystem_operation_table[index];
rt_sem_release(filesystem_operation_table_lock);
/* can't find filesystem */
if ( index == DFS_FILESYSTEM_TYPES_MAX )
{
rt_set_errno(-DFS_STATUS_ENODEV);
rt_sem_release(filesystem_operation_table_lock);
return -1;
}
ops = filesystem_operation_table[index];
rt_sem_release(filesystem_operation_table_lock);
/* make full path for special file */
fullpath = (char*)path;
if ( fullpath[0] != '/') /* not an abstract path */
{
/* make full path for special file */
fullpath = (char*)path;
if ( fullpath[0] != '/') /* not an abstract path */
{
#ifdef RT_USING_WORKDIR
/* build full path */
fullpath = full_path;
rt_sem_take(working_directory_lock, RT_WAITING_FOREVER);
build_fullpath(working_directory, path, fullpath);
rt_sem_release(working_directory_lock);
/* build full path */
fullpath = full_path;
rt_sem_take(working_directory_lock, RT_WAITING_FOREVER);
build_fullpath(working_directory, path, fullpath);
rt_sem_release(working_directory_lock);
#else
rt_set_errno(-DFS_STATUS_ENOTDIR);
return -1;
rt_set_errno(-DFS_STATUS_ENOTDIR);
return -1;
#endif
}
}
/* Check if the path exists or not, raw APIs call, fixme */
if ( (strcmp(fullpath, "/") != 0) && (strcmp(fullpath, "/dev") != 0) )
{
struct dfs_fd fd;
/* Check if the path exists or not, raw APIs call, fixme */
if ( (strcmp(fullpath, "/") != 0) && (strcmp(fullpath, "/dev") != 0) )
{
struct dfs_fd fd;
if ( dfile_raw_open(&fd, fullpath, DFS_O_RDONLY | DFS_O_DIRECTORY) < 0 )
{
rt_set_errno(-DFS_STATUS_ENOTDIR);
return -1;
}
dfile_raw_close(&fd);
}
if ( dfile_raw_open(&fd, fullpath, DFS_O_RDONLY | DFS_O_DIRECTORY) < 0 )
{
rt_set_errno(-DFS_STATUS_ENOTDIR);
return -1;
}
dfile_raw_close(&fd);
}
/* check whether the file system mounted or not */
rt_sem_take(filesystem_table_lock, RT_WAITING_FOREVER);
for (index =0; index < DFS_FILESYSTEMS_MAX; index++)
{
if ( filesystem_table[index].ops != RT_NULL &&
strcmp(filesystem_table[index].path, path) == 0 )
{
rt_set_errno(-DFS_STATUS_EINVAL);
goto err1;
}
}
/* check whether the file system mounted or not */
rt_sem_take(filesystem_table_lock, RT_WAITING_FOREVER);
for (index =0; index < DFS_FILESYSTEMS_MAX; index++)
{
if ( filesystem_table[index].ops != RT_NULL &&
strcmp(filesystem_table[index].path, path) == 0 )
{
rt_set_errno(-DFS_STATUS_EINVAL);
goto err1;
}
}
/* find out en empty filesystem table entry */
for (index = 0; index < DFS_FILESYSTEMS_MAX && filesystem_table[index].ops != RT_NULL;
index++) ;
if ( index == DFS_FILESYSTEMS_MAX ) /* can't find en empty filesystem table entry */
{
rt_set_errno(-DFS_STATUS_EMMOUNT);
goto err1;
}
/* find out en empty filesystem table entry */
for (index = 0; index < DFS_FILESYSTEMS_MAX && filesystem_table[index].ops != RT_NULL;
index++) ;
if ( index == DFS_FILESYSTEMS_MAX ) /* can't find en empty filesystem table entry */
{
rt_set_errno(-DFS_STATUS_EMMOUNT);
goto err1;
}
/* register file system */
fs = &(filesystem_table[index]);
strncpy(fs->path, fullpath, strlen(fullpath));
fs->ops = ops;
fs->dev_id = dev_id;
/* release filesystem_table lock */
rt_sem_release(filesystem_table_lock);
/* register file system */
fs = &(filesystem_table[index]);
strncpy(fs->path, fullpath, strlen(fullpath));
fs->ops = ops;
fs->dev_id = dev_id;
/* release filesystem_table lock */
rt_sem_release(filesystem_table_lock);
/* open device, but do not check the status of device */
rt_device_open(fs->dev_id, RT_DEVICE_OFLAG_RDWR);
/* open device, but do not check the status of device */
rt_device_open(fs->dev_id, RT_DEVICE_OFLAG_RDWR);
if ( ops->mount == RT_NULL ) /* there is no mount implementation */
{
rt_sem_take(filesystem_table_lock, RT_WAITING_FOREVER);
if ( ops->mount == RT_NULL ) /* there is no mount implementation */
{
rt_sem_take(filesystem_table_lock, RT_WAITING_FOREVER);
/* clear filesystem table entry */
rt_memset(fs, 0, sizeof(struct dfs_filesystem));
/* clear filesystem table entry */
rt_memset(fs, 0, sizeof(struct dfs_filesystem));
rt_sem_release(filesystem_table_lock);
rt_sem_release(filesystem_table_lock);
rt_set_errno(-DFS_STATUS_ENOSYS);
return -1;
}
/* call mount of this filesystem */
else if ( ops->mount(fs) < 0 )
{
/* mount failed */
rt_sem_take(filesystem_table_lock, RT_WAITING_FOREVER);
rt_set_errno(-DFS_STATUS_ENOSYS);
return -1;
}
/* call mount of this filesystem */
else if ( ops->mount(fs) < 0 )
{
/* mount failed */
rt_sem_take(filesystem_table_lock, RT_WAITING_FOREVER);
/* close device */
rt_device_close(fs->dev_id);
/* close device */
rt_device_close(fs->dev_id);
/* clear filesystem table entry */
rt_memset(fs, 0, sizeof(struct dfs_filesystem));
/* clear filesystem table entry */
rt_memset(fs, 0, sizeof(struct dfs_filesystem));
rt_sem_release(filesystem_table_lock);
return -1;
}
rt_sem_release(filesystem_table_lock);
return -1;
}
return 0;
return 0;
err1:
rt_sem_release(filesystem_table_lock);
return -1;
rt_sem_release(filesystem_table_lock);
return -1;
}
/*
@ -316,47 +333,47 @@ err1:
*/
int dfs_unmount(const char *specialfile)
{
char *fullpath;
char *fullpath;
#ifdef RT_USING_WORKDIR
char full_path[DFS_PATH_MAX + 1];
char full_path[DFS_PATH_MAX + 1];
#endif
struct dfs_filesystem* fs = RT_NULL;
struct dfs_filesystem* fs = RT_NULL;
fullpath = (char*)specialfile;
if ( fullpath[0] != '/')
{
fullpath = (char*)specialfile;
if ( fullpath[0] != '/')
{
#ifdef RT_USING_WORKDIR
/* build full path */
fullpath = full_path;
rt_sem_take(working_directory_lock, RT_WAITING_FOREVER);
build_fullpath(working_directory, specialfile, fullpath);
rt_sem_release(working_directory_lock);
/* build full path */
fullpath = full_path;
rt_sem_take(working_directory_lock, RT_WAITING_FOREVER);
build_fullpath(working_directory, specialfile, fullpath);
rt_sem_release(working_directory_lock);
#else
rt_set_errno(-DFS_STATUS_ENOTDIR);
return -1;
rt_set_errno(-DFS_STATUS_ENOTDIR);
return -1;
#endif
}
}
/* lock filesystem table */
rt_sem_take(filesystem_table_lock, RT_WAITING_FOREVER);
/* lock filesystem table */
rt_sem_take(filesystem_table_lock, RT_WAITING_FOREVER);
fs = dfs_filesystem_lookup(fullpath);
if (fs != RT_NULL && fs->ops->unmount != RT_NULL && fs->ops->unmount(fs) < 0)
{
goto err1;
}
fs = dfs_filesystem_lookup(fullpath);
if (fs != RT_NULL && fs->ops->unmount != RT_NULL && fs->ops->unmount(fs) < 0)
{
goto err1;
}
/* close device, but do not check the status of device */
rt_device_close(fs->dev_id);
/* close device, but do not check the status of device */
rt_device_close(fs->dev_id);
/* clear this filesystem table entry */
rt_memset(fs, 0, sizeof(struct dfs_filesystem));
/* clear this filesystem table entry */
rt_memset(fs, 0, sizeof(struct dfs_filesystem));
rt_sem_release(filesystem_table_lock);
rt_sem_release(filesystem_table_lock);
return 0;
return 0;
err1:
rt_sem_release(filesystem_table_lock);
return -1;
rt_sem_release(filesystem_table_lock);
return -1;
}