Move disk path routines to disk module
Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/commit/e2fcc8fa Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/e2fcc8fa Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/e2fcc8fa Branch: refs/heads/develop Commit: e2fcc8fa8d536c943b39ed2955f66e5dc8c875ae Parents: 4333192 Author: Fabio Utzig <[email protected]> Authored: Tue Jan 10 22:35:31 2017 -0200 Committer: Fabio Utzig <[email protected]> Committed: Thu Jan 12 10:51:46 2017 -0200 ---------------------------------------------------------------------- fs/disk/include/disk/disk.h | 2 ++ fs/disk/src/disk.c | 58 ++++++++++++++++++++++++++++++++++++++++ fs/fatfs/src/mynewt_glue.c | 39 +++------------------------ fs/fs/include/fs/fs.h | 3 --- fs/fs/src/fs_file.c | 28 +------------------ 5 files changed, 65 insertions(+), 65 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/e2fcc8fa/fs/disk/include/disk/disk.h ---------------------------------------------------------------------- diff --git a/fs/disk/include/disk/disk.h b/fs/disk/include/disk/disk.h index 49b9dde..a8695e8 100644 --- a/fs/disk/include/disk/disk.h +++ b/fs/disk/include/disk/disk.h @@ -46,6 +46,8 @@ struct disk_ops { int disk_register(const char *disk_name, const char *fs_name, struct disk_ops *dops); struct disk_ops *disk_ops_for(const char *disk_name); char *disk_fs_for(const char *disk_name); +char *disk_name_from_path(const char *path); +char *disk_filepath_from_path(const char *path); #ifdef __cplusplus } http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/e2fcc8fa/fs/disk/src/disk.c ---------------------------------------------------------------------- diff --git a/fs/disk/src/disk.c b/fs/disk/src/disk.c index 846c44d..550dade 100644 --- a/fs/disk/src/disk.c +++ b/fs/disk/src/disk.c @@ -96,3 +96,61 @@ disk_fs_for(const char *disk_name) return fs_name; } + +char * +disk_name_from_path(const char *path) +{ + char *colon; + uint8_t len; + char *disk; + + colon = (char *) path; + while (*colon && *colon != ':') { + colon++; + } + + if (*colon != ':') { + return NULL; + } + + len = colon - path; + disk = malloc(len + 1); + if (!disk) { + return NULL; + } + memcpy(disk, path, len); + disk[len] = '\0'; + + return disk; +} + +/** + * @brief Returns the path with the disk prefix removed (if found) + * + * Paths should be given in the form disk<number>:/path. This routine + * will parse and return only the path, removing the disk information. + */ +char * +disk_filepath_from_path(const char *path) +{ + char *colon; + char *filepath; + size_t len; + + colon = (char *) path; + while (*colon && *colon != ':') { + colon++; + } + + if (*colon != ':') { + filepath = strdup(path); + } else { + colon++; + len = strlen(colon); + filepath = malloc(len); + memcpy(filepath, colon, len); + filepath[len] = '\0'; + } + + return filepath; +} http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/e2fcc8fa/fs/fatfs/src/mynewt_glue.c ---------------------------------------------------------------------- diff --git a/fs/fatfs/src/mynewt_glue.c b/fs/fatfs/src/mynewt_glue.c index c622ffb..189ca8a 100644 --- a/fs/fatfs/src/mynewt_glue.c +++ b/fs/fatfs/src/mynewt_glue.c @@ -190,37 +190,6 @@ drivenumber_from_disk(char *disk_name) return disk_number; } -/** - * @brief Returns the path with the disk prefix removed (if found) - * - * Paths should be given in the form disk<number>:/path. This routine - * will parse and return only the path, removing the disk information. - */ -char * -filepath_from_path(const char *path) -{ - char *colon; - char *filepath; - size_t len; - - colon = (char *) path; - while (*colon && *colon != ':') { - colon++; - } - - if (*colon != ':') { - filepath = strdup(path); - } else { - colon++; - len = strlen(colon); - filepath = malloc(len); - memcpy(filepath, colon, len); - filepath[len] = '\0'; - } - - return filepath; -} - static int fatfs_open(const char *path, uint8_t access_flags, struct fs_file **out_fs_file) { @@ -261,9 +230,9 @@ fatfs_open(const char *path, uint8_t access_flags, struct fs_file **out_fs_file) mode |= FA_CREATE_ALWAYS; } - disk = disk_from_path(path); + disk = disk_name_from_path(path); number = drivenumber_from_disk(disk); - filepath = filepath_from_path(path); + filepath = disk_filepath_from_path(path); sprintf(drivepath, "%d:%s", number, filepath); free(filepath); @@ -409,9 +378,9 @@ fatfs_opendir(const char *path, struct fs_dir **out_fs_dir) goto out; } - disk = disk_from_path(path); + disk = disk_name_from_path(path); number = drivenumber_from_disk(disk); - filepath = filepath_from_path(path); + filepath = disk_filepath_from_path(path); sprintf(drivepath, "%d:%s", number, filepath); free(filepath); http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/e2fcc8fa/fs/fs/include/fs/fs.h ---------------------------------------------------------------------- diff --git a/fs/fs/include/fs/fs.h b/fs/fs/include/fs/fs.h index f823b32..b44f624 100644 --- a/fs/fs/include/fs/fs.h +++ b/fs/fs/include/fs/fs.h @@ -53,9 +53,6 @@ int fs_dirent_name(const struct fs_dirent *, size_t max_len, char *out_name, uint8_t *out_name_len); int fs_dirent_is_dir(const struct fs_dirent *); -/* Helper functions */ -char *disk_from_path(const char *path); - /* * File access flags. */ http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/e2fcc8fa/fs/fs/src/fs_file.c ---------------------------------------------------------------------- diff --git a/fs/fs/src/fs_file.c b/fs/fs/src/fs_file.c index 5ad28cd..305a3c4 100644 --- a/fs/fs/src/fs_file.c +++ b/fs/fs/src/fs_file.c @@ -149,32 +149,6 @@ safe_fs_ops_for(const char *fs_name) return fops; } -char *disk_from_path(const char *path) -{ - char *colon; - uint8_t len; - char *disk; - - colon = (char *) path; - while (*colon && *colon != ':') { - colon++; - } - - if (*colon != ':') { - return NULL; - } - - len = colon - path; - disk = malloc(len + 1); - if (!disk) { - return NULL; - } - memcpy(disk, path, len); - disk[len] = '\0'; - - return disk; -} - struct fs_ops * fops_from_filename(const char *filename) { @@ -182,7 +156,7 @@ fops_from_filename(const char *filename) char *fs_name = NULL; struct fs_ops *unique; - disk = disk_from_path(filename); + disk = disk_name_from_path(filename); if (disk) { fs_name = disk_fs_for(disk); } else {
