From e034f287bf3e1fa7228a73cfb96118f2bf9e2bdb Mon Sep 17 00:00:00 2001 From: Vreixo Formoso Date: Sun, 25 Nov 2007 19:54:13 +0100 Subject: [PATCH] Implement IsoFilesystem to deal with local filesystem. --- src/fs_local.c | 55 +++++++++++++++++++++++++++++++++++++++++++++----- src/fsource.c | 13 ++++++++++++ src/fsource.h | 25 +++++++++++++++++++++-- 3 files changed, 86 insertions(+), 7 deletions(-) diff --git a/src/fs_local.c b/src/fs_local.c index 960d412..ac69b0d 100644 --- a/src/fs_local.c +++ b/src/fs_local.c @@ -238,9 +238,7 @@ int lfs_readdir(IsoFileSource *src, IsoFileSource **child) /* create the new FileSrc */ ret = iso_file_source_new_lfs(path, child); - if (ret < 0) { - free(path); - } + free(path); return ret; } default: @@ -316,7 +314,7 @@ void lfs_free(IsoFileSource *src) * @return * 1 success, < 0 error */ -int iso_file_source_new_lfs(char *path, IsoFileSource **src) +int iso_file_source_new_lfs(const char *path, IsoFileSource **src) { IsoFileSource *lfs_src; _LocalFsFileSource *data; @@ -337,7 +335,7 @@ int iso_file_source_new_lfs(char *path, IsoFileSource **src) } /* fill struct */ - data->path = path; + data->path = strdup(path); data->openned = 0; lfs_src->refcount = 1; @@ -356,4 +354,51 @@ int iso_file_source_new_lfs(char *path, IsoFileSource **src) return ISO_SUCCESS; } +static +int lfs_get_root(IsoFilesystem *fs, IsoFileSource **root) +{ + if (fs == NULL || root == NULL) { + return ISO_NULL_POINTER; + } + return iso_file_source_new_lfs("/", root); +} +static +int lfs_get_by_path(IsoFilesystem *fs, const char *path, IsoFileSource **file) +{ + if (fs == NULL || path == NULL || file == NULL) { + return ISO_NULL_POINTER; + } + return iso_file_source_new_lfs(path, file); +} + +static +void lfs_fs_free(IsoFilesystem *fs) +{ + /* nothing to do */ + return; +} + +int iso_local_filesystem_new(IsoFilesystem **fs) +{ + IsoFilesystem *lfs; + + if (fs == NULL) { + return ISO_NULL_POINTER; + } + + lfs = malloc(sizeof(IsoFilesystem)); + if (lfs == NULL) { + return ISO_OUT_OF_MEM; + } + + /* fill struct */ + lfs->refcount = 1; + lfs->data = NULL; /* we don't need private data */ + lfs->get_root = lfs_get_root; + lfs->get_by_path = lfs_get_by_path; + lfs->free = lfs_fs_free; + + *fs = lfs; + return ISO_SUCCESS; +} diff --git a/src/fsource.c b/src/fsource.c index 4588aea..4f97a8a 100644 --- a/src/fsource.c +++ b/src/fsource.c @@ -21,3 +21,16 @@ void iso_file_source_unref(IsoFileSource *src) free(src); } } + +void iso_filesystem_ref(IsoFilesystem *fs) +{ + ++fs->refcount; +} + +void iso_filesystem_unref(IsoFilesystem *fs) +{ + if (--fs->refcount == 0) { + fs->free(fs); + free(fs); + } +} diff --git a/src/fsource.h b/src/fsource.h index ea27a55..7eb9efb 100644 --- a/src/fsource.h +++ b/src/fsource.h @@ -31,9 +31,19 @@ struct Iso_Filesystem * @return * 1 on success, < 0 on error */ - int (*get_root)(IsoFilesystem *fs, IsoFileSource *root); + int (*get_root)(IsoFilesystem *fs, IsoFileSource **root); + /** + * + * + */ + int (*get_by_path)(IsoFilesystem *fs, const char *path, + IsoFileSource **file); + void (*free)(IsoFilesystem *fs); + + int refcount; + void *data; }; struct Iso_File_Source @@ -177,6 +187,17 @@ void iso_file_source_unref(IsoFileSource *src); * While this is usually called by corresponding method in IsoFilesystem * object, for local filesystem it is legal to call this directly. */ -int iso_file_source_new_lfs(char *path, IsoFileSource **src); +int iso_file_source_new_lfs(const char *path, IsoFileSource **src); + +void iso_filesystem_ref(IsoFilesystem *fs); +void iso_filesystem_unref(IsoFilesystem *fs); + +/** + * Create a new IsoFilesystem to deal with local filesystem. + * + * @return + * 1 sucess, < 0 error + */ +int iso_local_filesystem_new(IsoFilesystem **fs); #endif /*LIBISO_FSOURCE_H_*/