dfs: refine code

decrease indentation to make cleaner code
fix mkfs bug when there is no mkfs implementation
This commit is contained in:
prife 2013-12-19 00:01:52 +08:00
parent 712d9428ed
commit 7fd6d17d5b
1 changed files with 69 additions and 116 deletions

View File

@ -103,18 +103,15 @@ struct dfs_filesystem *dfs_filesystem_lookup(const char *path)
/* 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) if ((filesystem_table[index].path == RT_NULL)
|| (filesystem_table[index].ops == RT_NULL))
continue;
fspath = strlen(filesystem_table[index].path);
if ((fspath < prefixlen)
|| (strncmp(filesystem_table[index].path, path, fspath) != 0))
continue; continue;
else
{
fspath = strlen(filesystem_table[index].path);
if (fspath < prefixlen)
continue;
}
if ((filesystem_table[index].ops != RT_NULL) &&
(strncmp(filesystem_table[index].path, path, fspath) == 0))
{
/* check next path separator */ /* check next path separator */
if (fspath > 1 && (strlen(path) > fspath) && (path[fspath] != '/')) if (fspath > 1 && (strlen(path) > fspath) && (path[fspath] != '/'))
continue; continue;
@ -122,7 +119,6 @@ struct dfs_filesystem *dfs_filesystem_lookup(const char *path)
fs = &filesystem_table[index]; fs = &filesystem_table[index];
prefixlen = fspath; prefixlen = fspath;
} }
}
dfs_unlock(); dfs_unlock();
@ -147,64 +143,42 @@ rt_err_t dfs_filesystem_get_partition(struct dfs_partition *part,
rt_uint8_t *dpt; rt_uint8_t *dpt;
rt_uint8_t type; rt_uint8_t type;
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;
dpt = buf + DPT_ADDRESS + pindex * DPT_ITEM_SIZE; dpt = buf + DPT_ADDRESS + pindex * DPT_ITEM_SIZE;
/* check if it is a valid partition table */
if ((*dpt != 0x80) && (*dpt != 0x00)) if ((*dpt != 0x80) && (*dpt != 0x00))
{ return -RT_ERROR;
/* which is not a partition table */
result = -RT_ERROR;
return result;
}
/* get partition type */ /* get partition type */
type = *(dpt+4); type = *(dpt+4);
if (type == 0)
return -RT_ERROR;
if (type != 0) /* set partition information
{ * size is the number of 512-Byte */
/* set partition type */
part->type = type; part->type = type;
/* get partition offset and size */
part->offset = *(dpt+8) | *(dpt+9)<<8 | *(dpt+10)<<16 | *(dpt+11)<<24; 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; part->size = *(dpt+12) | *(dpt+13)<<8 | *(dpt+14)<<16 | *(dpt+15)<<24;
rt_kprintf("found part[%d], begin: %d, size: ", rt_kprintf("found part[%d], begin: %d, size: ",
pindex, part->offset*512); pindex, part->offset*512);
if ((part->size>>11) > 0) /* MB */ if ((part->size>>11) == 0)
rt_kprintf("%d%s",part->size>>1,"KB\n"); /* KB */
else
{ {
unsigned int part_size; unsigned int part_size;
part_size = part->size >> 11;/* MB */ part_size = part->size >> 11; /* MB */
if ((part_size>>10) > 0) /* GB */ if ((part_size>>10) == 0)
{ rt_kprintf("%d.%d%s",part_size,(part->size>>1)&0x3FF,"MB\n");
/* GB */
rt_kprintf("%d.%d%s",part_size>>10,part_size&0x3FF,"GB\r\n");
}
else else
{ rt_kprintf("%d.%d%s",part_size>>10,part_size&0x3FF,"GB\n");
/* MB */
rt_kprintf("%d.%d%s",part_size,(part->size>>1)&0x3FF,"MB\r\n");
}
}
else
{
/* KB */
rt_kprintf("%d%s",part->size>>1,"KB\r\n");
}
}
else
{
result = -RT_ERROR;
} }
return result; return RT_EOK;
} }
/** /**
@ -231,22 +205,17 @@ int dfs_mount(const char *device_name,
int index, free_index; int index, free_index;
/* open specific device */ /* open specific device */
if (device_name != RT_NULL) if (device_name == RT_NULL)
{ {
dev_id = rt_device_find(device_name); /* which is a non-device filesystem mount */
if (dev_id == RT_NULL) dev_id = NULL;
}
else if ((dev_id = rt_device_find(device_name)) == RT_NULL)
{ {
/* no this device */ /* no this device */
rt_set_errno(-DFS_STATUS_ENODEV); rt_set_errno(-DFS_STATUS_ENODEV);
return -1; return -1;
} }
}
else
{
/* which is a non-device filesystem mount */
dev_id = RT_NULL;
}
/* find out specific filesystem */ /* find out specific filesystem */
dfs_lock(); dfs_lock();
@ -264,17 +233,22 @@ int dfs_mount(const char *device_name,
if (index == DFS_FILESYSTEM_TYPES_MAX) if (index == DFS_FILESYSTEM_TYPES_MAX)
{ {
rt_set_errno(-DFS_STATUS_ENODEV); rt_set_errno(-DFS_STATUS_ENODEV);
return -1; return -1;
} }
/* check if there is mount implementation */
ops = filesystem_operation_table[index]; ops = filesystem_operation_table[index];
if ((ops == NULL) || (ops->mount == NULL))
{
rt_set_errno(-DFS_STATUS_ENOSYS);
return -1;
}
/* 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;
} }
@ -293,8 +267,8 @@ int dfs_mount(const char *device_name,
dfs_file_close(&fd); dfs_file_close(&fd);
} }
free_index = DFS_FILESYSTEMS_MAX;
/* check whether the file system mounted or not */ /* check whether the file system mounted or not */
free_index = DFS_FILESYSTEMS_MAX;
dfs_lock(); dfs_lock();
for (index = 0; index < DFS_FILESYSTEMS_MAX; index ++) for (index = 0; index < DFS_FILESYSTEMS_MAX; index ++)
{ {
@ -304,6 +278,7 @@ int dfs_mount(const char *device_name,
if (free_index == DFS_FILESYSTEMS_MAX) if (free_index == DFS_FILESYSTEMS_MAX)
free_index = index; free_index = index;
} }
/* check if the PATH is mounted */
else if (strcmp(filesystem_table[index].path, path) == 0) else if (strcmp(filesystem_table[index].path, path) == 0)
{ {
rt_set_errno(-DFS_STATUS_EINVAL); rt_set_errno(-DFS_STATUS_EINVAL);
@ -330,23 +305,8 @@ int dfs_mount(const char *device_name,
if (dev_id != RT_NULL) if (dev_id != RT_NULL)
rt_device_open(fs->dev_id, RT_DEVICE_OFLAG_RDWR); rt_device_open(fs->dev_id, RT_DEVICE_OFLAG_RDWR);
/* there is no mount implementation */
if (ops->mount == RT_NULL)
{
if (dev_id != RT_NULL)
rt_device_close(dev_id);
dfs_lock();
/* clear filesystem table entry */
rt_memset(fs, 0, sizeof(struct dfs_filesystem));
dfs_unlock();
rt_free(fullpath);
rt_set_errno(-DFS_STATUS_ENOSYS);
return -1;
}
/* call mount of this filesystem */ /* call mount of this filesystem */
else if (ops->mount(fs, rwflag, data) < 0) if (ops->mount(fs, rwflag, data) < 0)
{ {
/* close device */ /* close device */
if (dev_id != RT_NULL) if (dev_id != RT_NULL)
@ -356,18 +316,13 @@ int dfs_mount(const char *device_name,
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(); goto err1;
rt_free(fullpath);
return -1;
} }
return 0; return 0;
err1: err1:
dfs_unlock(); dfs_unlock();
if (fullpath != RT_NULL)
rt_free(fullpath); rt_free(fullpath);
return -1; return -1;
@ -437,12 +392,10 @@ err1:
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;
rt_device_t dev_id; rt_device_t dev_id = RT_NULL;
/* check device name, and it should not be NULL */ /* check device name, and it should not be NULL */
if (device_name == RT_NULL) if (device_name != RT_NULL)
dev_id = RT_NULL;
else
dev_id = rt_device_find(device_name); dev_id = rt_device_find(device_name);
if (dev_id == RT_NULL) if (dev_id == RT_NULL)
@ -458,18 +411,22 @@ int dfs_mkfs(const char *fs_name, const char *device_name)
{ {
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)
break;
}
dfs_unlock();
if (index < DFS_FILESYSTEM_TYPES_MAX)
{ {
/* 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(); if (ops->mkfs == RT_NULL)
{
rt_set_errno(-DFS_STATUS_ENOSYS);
return -1;
}
if (ops->mkfs != RT_NULL)
return ops->mkfs(dev_id); return ops->mkfs(dev_id);
break;
} }
}
dfs_unlock();
rt_kprintf("Can not find the file system which named as %s.\n", fs_name); rt_kprintf("Can not find the file system which named as %s.\n", fs_name);
return -1; return -1;
@ -538,11 +495,7 @@ int df(const char *path)
long long cap; long long cap;
struct statfs buffer; struct statfs buffer;
if (path == RT_NULL) result = dfs_statfs(path ? path : RT_NULL, &buffer);
result = dfs_statfs("/", &buffer);
else
result = dfs_statfs(path, &buffer);
if (result != 0) if (result != 0)
{ {
rt_kprintf("dfs_statfs failed.\n"); rt_kprintf("dfs_statfs failed.\n");