From 54e77664fafc4448852acd8e3c0d5a61b6bc897d Mon Sep 17 00:00:00 2001 From: "bernard.xiong@gmail.com" Date: Sun, 31 Oct 2010 13:52:58 +0000 Subject: [PATCH] romfs and dev is usable. fix directory adjustment in ls function. git-svn-id: https://rt-thread.googlecode.com/svn/trunk@1035 bbd45198-f89e-11dd-88c7-29a3b14d5316 --- components/dfs/filesystems/devfs/devfs.c | 6 +- components/dfs/filesystems/nfs/dfs_nfs.c | 2 +- components/dfs/filesystems/romfs/dfs_romfs.c | 92 ++++++++++++++------ components/dfs/filesystems/romfs/dfs_romfs.h | 1 + components/dfs/filesystems/romfs/mkromfs.py | 60 ++++++++++--- components/dfs/src/dfs_file.c | 2 +- 6 files changed, 115 insertions(+), 48 deletions(-) diff --git a/components/dfs/filesystems/devfs/devfs.c b/components/dfs/filesystems/devfs/devfs.c index af44bbb23..98f2146f6 100644 --- a/components/dfs/filesystems/devfs/devfs.c +++ b/components/dfs/filesystems/devfs/devfs.c @@ -82,7 +82,7 @@ int dfs_device_fs_close(struct dfs_fd* file) RT_ASSERT(file != RT_NULL); - if (file->flags & FT_DIRECTORY) + if (file->type == FT_DIRECTORY) { struct device_dirent *root_dirent; @@ -225,10 +225,10 @@ int dfs_device_fs_getdents(struct dfs_fd* file, struct dirent* dirp, rt_uint32_t RT_ASSERT(root_dirent != RT_NULL); /* make integer count */ - count = (count / sizeof(struct dirent)) * sizeof(struct dirent); + count = (count / sizeof(struct dirent)); if ( count == 0 ) return -DFS_STATUS_EINVAL; - for (index = 0; index < count/sizeof(struct dirent) && index + root_dirent->read_index < root_dirent->device_count; + for (index = 0; index < count && index + root_dirent->read_index < root_dirent->device_count; index ++) { object = (rt_object_t)root_dirent->devices[root_dirent->read_index + index]; diff --git a/components/dfs/filesystems/nfs/dfs_nfs.c b/components/dfs/filesystems/nfs/dfs_nfs.c index 836f4f1d2..bf01607e5 100644 --- a/components/dfs/filesystems/nfs/dfs_nfs.c +++ b/components/dfs/filesystems/nfs/dfs_nfs.c @@ -1017,7 +1017,7 @@ int nfs_getdents(struct dfs_fd* file, struct dirent* dirp, rt_uint32_t count) name = nfs_readdir(nfs, dir); if (name == RT_NULL) break; - d->d_type &= DFS_DT_REG; + d->d_type = DFS_DT_REG; d->d_namlen = rt_strlen(name); d->d_reclen = (rt_uint16_t)sizeof(struct dirent); diff --git a/components/dfs/filesystems/romfs/dfs_romfs.c b/components/dfs/filesystems/romfs/dfs_romfs.c index efd74e238..934d1dd85 100644 --- a/components/dfs/filesystems/romfs/dfs_romfs.c +++ b/components/dfs/filesystems/romfs/dfs_romfs.c @@ -25,15 +25,22 @@ int dfs_romfs_ioctl(struct dfs_fd* file, int cmd, void* args) return -DFS_STATUS_EIO; } -struct romfs_dirent* dfs_romfs_lookup(struct romfs_dirent* root_dirent, const char* path) +struct romfs_dirent* dfs_romfs_lookup(struct romfs_dirent* root_dirent, const char* path, rt_size_t *size) { - rt_size_t index; + rt_size_t index, found; const char *subpath, *subpath_end; struct romfs_dirent* dirent; + rt_size_t dirent_size; - dirent = root_dirent; + if (path[0] == '/' && path[1] == '\0') + { + *size = root_dirent->size; + return root_dirent; + } - if (path[0] == '/' && path[1] == '\0') return dirent; + /* goto root directy entries */ + dirent = (struct romfs_dirent*)root_dirent->data; + dirent_size = root_dirent->size; /* get the end position of this subpath */ subpath_end = path; @@ -44,26 +51,42 @@ struct romfs_dirent* dfs_romfs_lookup(struct romfs_dirent* root_dirent, const ch while (dirent != RT_NULL) { + found = 0; + /* search in folder */ - for (index = 0; index < dirent->size; index ++) + for (index = 0; index < dirent_size; index ++) { if (rt_strncmp(dirent[index].name, subpath, (subpath_end - subpath)) == 0) { + dirent_size = dirent[index].size; + /* skip /// */ while (*subpath_end && *subpath_end == '/') subpath_end ++; subpath = subpath_end; while ((*subpath_end != '/') && *subpath_end) subpath_end ++; - if (!(*subpath)) return dirent; + if (!(*subpath)) + { + *size = dirent_size; + return &dirent[index]; + } if (dirent[index].type == ROMFS_DIRENT_DIR) { + /* enter directory */ dirent = (struct romfs_dirent*)dirent[index].data; + found = 1; break; } - else return dirent; + else + { + /* return file dirent */ + return &dirent[index]; + } } } + + if (!found) break; /* not found */ } /* not found */ @@ -86,6 +109,9 @@ int dfs_romfs_read(struct dfs_fd* file, void *buf, rt_size_t count) if (length > 0) memcpy(buf, &(dirent->data[file->pos]), length); + /* update file current position */ + file->pos += length; + return length; } @@ -108,42 +134,44 @@ int dfs_romfs_close(struct dfs_fd* file) int dfs_romfs_open(struct dfs_fd* file) { - struct romfs_dirent* root_dirent; + rt_size_t size; struct romfs_dirent* dirent; + struct romfs_dirent* root_dirent; 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)) return -DFS_STATUS_EINVAL; - dirent = dfs_romfs_lookup(root_dirent, file->path); + dirent = dfs_romfs_lookup(root_dirent, file->path, &size); if (dirent == RT_NULL) return -DFS_STATUS_ENOENT; - if (file->flags & DFS_O_DIRECTORY) - file->data = dirent; - - file->size = dirent->size; + file->data = dirent; + file->size = size; file->pos = 0; + 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) { - struct romfs_dirent* root_dirent; + rt_size_t size; struct romfs_dirent* dirent; + struct romfs_dirent* root_dirent; root_dirent = (struct romfs_dirent*)fs->data; - dirent = dfs_romfs_lookup(root_dirent, path); + dirent = dfs_romfs_lookup(root_dirent, path, &size); if (dirent == RT_NULL) return -DFS_STATUS_ENOENT; st->st_dev = 0; st->st_mode = DFS_S_IFREG | DFS_S_IRUSR | DFS_S_IRGRP | DFS_S_IROTH | DFS_S_IWUSR | DFS_S_IWGRP | DFS_S_IWOTH; + if (dirent->type == ROMFS_DIRENT_DIR) { - st->st_mode &= ~DFS_S_IFREG; - st->st_mode |= DFS_S_IFDIR | DFS_S_IXUSR | DFS_S_IXGRP | DFS_S_IXOTH; + st->st_mode &= ~DFS_S_IFREG; + st->st_mode |= DFS_S_IFDIR | DFS_S_IXUSR | DFS_S_IXGRP | DFS_S_IXOTH; } st->st_size = dirent->size; @@ -153,22 +181,25 @@ int dfs_romfs_stat(struct dfs_filesystem* fs, const char *path, struct _stat *st 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; const char *name; - struct _dirent* d; + struct dirent* d; struct romfs_dirent *dirent, *sub_dirent; dirent = (struct romfs_dirent*) file->data; + RT_ASSERT(dirent->type == ROMFS_DIRENT_DIR); + /* enter directory */ + dirent = (struct romfs_dirent*) dirent->data; + /* make integer count */ - count = (count / sizeof(struct _dirent)) * sizeof(struct _dirent); + count = (count / sizeof(struct dirent)); if ( count == 0 ) return -DFS_STATUS_EINVAL; index = 0; - sub_dirent = &dirent[file->pos]; - for (index = 0; index < count; index ++) + for (index = 0; index < count && file->pos < file->size; index ++) { d = dirp + index; @@ -176,17 +207,20 @@ int dfs_romfs_getdents(struct dfs_fd* file, struct _dirent* dirp, rt_uint32_t co name = sub_dirent->name; /* fill dirent */ - d->d_type &= DFS_DT_REG; + if (sub_dirent->type == ROMFS_DIRENT_DIR) + d->d_type = DFS_DT_DIR; + else + d->d_type = DFS_DT_REG; + d->d_namlen = rt_strlen(name); - d->d_reclen = (rt_uint16_t)sizeof(struct _dirent); - rt_strncpy(d->d_name, name, rt_strlen(name) + 1); + d->d_reclen = (rt_uint16_t)sizeof(struct dirent); + rt_strncpy(d->d_name, name, rt_strlen(name) + 1); /* move to next position */ ++ file->pos; - if (file->pos > file->size) break; } - - return index * sizeof(struct _dirent); + + return index * sizeof(struct dirent); } static const struct dfs_filesystem_operation _romfs = diff --git a/components/dfs/filesystems/romfs/dfs_romfs.h b/components/dfs/filesystems/romfs/dfs_romfs.h index 8b4b0bd5c..6a3f7ac1c 100644 --- a/components/dfs/filesystems/romfs/dfs_romfs.h +++ b/components/dfs/filesystems/romfs/dfs_romfs.h @@ -16,5 +16,6 @@ struct romfs_dirent }; int dfs_romfs_init(void); +extern const struct romfs_dirent romfs_root; #endif diff --git a/components/dfs/filesystems/romfs/mkromfs.py b/components/dfs/filesystems/romfs/mkromfs.py index ccf0929ff..5ae6500ae 100644 --- a/components/dfs/filesystems/romfs/mkromfs.py +++ b/components/dfs/filesystems/romfs/mkromfs.py @@ -3,22 +3,24 @@ import os import string basename = '' +output = '' def mkromfs_output(out): - print '%s' % out, + # print '%s' % out, + output.write(out) def mkromfs_file(filename, arrayname): - f = open(filename, "rb") + f = file(filename, "rb") arrayname = arrayname.replace('.', '_') mkromfs_output('const static unsigned char %s[] = {\n' % arrayname) count = 0 while True: byte = f.read(1) - - if not byte: + + if len(byte) != 1: break - + mkromfs_output('0x%02x,' % ord(byte)) count = count + 1 @@ -26,7 +28,11 @@ def mkromfs_file(filename, arrayname): count = 0 mkromfs_output('\n') - mkromfs_output('};\n\n') + if count == 0: + mkromfs_output('};\n\n') + else: + mkromfs_output('\n};\n\n') + f.close() def mkromfs_dir(dirname, is_root = False): @@ -37,7 +43,10 @@ def mkromfs_dir(dirname, is_root = False): for item in list: fullpath = os.path.join(path, item) if os.path.isdir(fullpath): - mkromfs_dir(fullpath) + # if it is an empty directory, ignore it + l = os.listdir(fullpath) + if len(l): + mkromfs_dir(fullpath) # make for files for item in list: @@ -52,16 +61,16 @@ def mkromfs_dir(dirname, is_root = False): dir = subpath.split('\\') direntname = string.join(dir, '_') if is_root: - mkromfs_output('const struct romfs_dirent romfs_root[] = {\n') + mkromfs_output('const struct romfs_dirent _root_dirent[] = {\n') else: mkromfs_output(('const static struct romfs_dirent %s[] = {\n' % direntname)) for item in list: fullpath = os.path.join(path, item) - fn = fullpath[len(dirname):] - if fn[0] == '\\': - fn = fn[1:] - fn = fn.replace('\\', '/') + fn = fullpath[len(dirname):] + if fn[0] == '\\': + fn = fn[1:] + fn = fn.replace('\\', '/') subpath = fullpath[len(basename):] items = subpath.split('\\') @@ -71,10 +80,30 @@ def mkromfs_dir(dirname, is_root = False): if subpath[0] == '/': subpath = subpath[1:] + if not os.path.isfile(fullpath): + l = os.listdir(fullpath) + if len(l): + mkromfs_output(('\t{ROMFS_DIRENT_DIR, "%s", %s, sizeof(%s)/sizeof(%s[0])},\n' % (fn, item_name, item_name, item_name))) + else: + mkromfs_output(('\t{ROMFS_DIRENT_DIR, "%s", RT_NULL, 0},\n' % fn)) + + for item in list: + fullpath = os.path.join(path, item) + fn = fullpath[len(dirname):] + if fn[0] == '\\': + fn = fn[1:] + fn = fn.replace('\\', '/') + + subpath = fullpath[len(basename):] + items = subpath.split('\\') + item_name = string.join(items, '_') + item_name = item_name.replace('.', '_') + subpath = subpath.replace('\\', '/') + if subpath[0] == '/': + subpath = subpath[1:] + if os.path.isfile(fullpath): mkromfs_output(('\t{ROMFS_DIRENT_FILE, "%s", %s, sizeof(%s)},\n' % (fn, item_name, item_name))) - else: - mkromfs_output(('\t{ROMFS_DIRENT_DIR, "%s", %s, sizeof(%s)/sizeof(%s[0])},\n' % (fn, item_name, item_name, item_name))) mkromfs_output('};\n\n') @@ -86,5 +115,8 @@ if __name__ == "__main__": print "Usage: %s " % sys.argv[0] raise SystemExit + output = file(filename, 'wt') mkromfs_output("#include \n\n") mkromfs_dir(basename, is_root = True) + + mkromfs_output("const struct romfs_dirent romfs_root = {ROMFS_DIRENT_DIR, \"/\", _root_dirent, sizeof(_root_dirent)/sizeof(_root_dirent[0])};\n\n") diff --git a/components/dfs/src/dfs_file.c b/components/dfs/src/dfs_file.c index a1b19941e..88a9feacf 100644 --- a/components/dfs/src/dfs_file.c +++ b/components/dfs/src/dfs_file.c @@ -470,7 +470,7 @@ void ls(const char* pathname) if (dfs_file_stat(fullpath, &stat) == 0) { - if ( stat.st_mode & DFS_S_IFDIR ) + if ( DFS_S_ISDIR(stat.st_mode)) { rt_kprintf("%s\t\t\n", dirent.d_name); } -- GitLab