From b6cff0a1866b39256530f60afb6e7b7e10a68d3e Mon Sep 17 00:00:00 2001 From: Bob Copeland Date: Fri, 29 Aug 2008 10:30:20 -0400 Subject: [PATCH] Encapsulate mount-time variables in a single structure --- gstfs.c | 89 +++++++++++++++++++++++++++++++++++++---------------------------- 1 file changed, 51 insertions(+), 38 deletions(-) diff --git a/gstfs.c b/gstfs.c index 5d6fe55..48b40d1 100644 --- a/gstfs.c +++ b/gstfs.c @@ -16,19 +16,20 @@ #define min(a,b) ((a)<(b)?(a):(b)) #define max(a,b) ((a)>(b)?(a):(b)) -/* protects file_cache, cache_lru accesses */ -static pthread_mutex_t cache_mutex = PTHREAD_MUTEX_INITIALIZER; -static GHashTable *file_cache; /* cache of transcoded audio */ -static GQueue *cache_lru; /* queue of items in LRU order */ -static int max_cache_entries = 50; - -static char *get_source_path(const char *filename); - -char *source_mount = "ogg"; -char *src_ext = "ogg", *dest_ext = "mp3"; +/* per-mount options and data structures */ +struct gstfs_mount_info +{ + pthread_mutex_t cache_mutex; /* protects file_cache, cache_lru accesses */ + GHashTable *file_cache; /* cache of transcoded audio */ + GQueue *cache_lru; /* queue of items in LRU order */ + int max_cache_entries; /* max # of entries in the cache */ + char *source_mount; /* directory we are mirroring */ + char *src_ext; /* extension of files we transcode */ + char *dest_ext; /* extension of target files */ +}; /* This stuff is stored into file_cache by filename */ -struct gst_file_info +struct gstfs_file_info { char *filename; /* hash key */ char *source_filename; /* filename in other mount */ @@ -39,18 +40,24 @@ struct gst_file_info GList *list_node; /* pointer for cache_lru */ }; -struct gst_file_info *get_file_info(const char *filename) + +static struct gstfs_mount_info mount_info; + +static char *get_source_path(const char *filename); + + +struct gstfs_file_info *get_file_info(const char *filename) { - struct gst_file_info *fi; + struct gstfs_file_info *fi; - fi = calloc(1, sizeof(struct gst_file_info)); + fi = calloc(1, sizeof(struct gstfs_file_info)); fi->filename = g_strdup(filename); fi->source_filename = get_source_path(filename); pthread_mutex_init(&fi->mutex, NULL); return fi; } -void put_file_info(struct gst_file_info *fi) +void put_file_info(struct gstfs_file_info *fi) { g_free(fi->filename); g_free(fi->source_filename); @@ -71,7 +78,7 @@ char *replace_ext(char *filename, char *search, char *replace) int is_target_type(const char *filename) { char *ext = strrchr(filename, '.'); - return (ext && strcmp(ext+1, dest_ext) == 0); + return (ext && strcmp(ext+1, mount_info.dest_ext) == 0); } /* @@ -84,12 +91,13 @@ int is_target_type(const char *filename) */ static void expire_cache() { - struct gst_file_info *fi; + struct gstfs_file_info *fi; - while (g_queue_get_length(cache_lru) > max_cache_entries) + while (g_queue_get_length(mount_info.cache_lru) > + mount_info.max_cache_entries) { - fi = (struct gst_file_info *) g_queue_pop_head(cache_lru); - g_hash_table_remove(file_cache, fi); + fi = (struct gstfs_file_info *) g_queue_pop_head(mount_info.cache_lru); + g_hash_table_remove(mount_info.file_cache, fi); put_file_info(fi); } } @@ -100,35 +108,35 @@ static void expire_cache() * * If it isn't a mirror file, return NULL. */ -static struct gst_file_info *gstfs_lookup(const char *path) +static struct gstfs_file_info *gstfs_lookup(const char *path) { - struct gst_file_info *ret; + struct gstfs_file_info *ret; if (!is_target_type(path)) return NULL; - pthread_mutex_lock(&cache_mutex); - ret = g_hash_table_lookup(file_cache, path); + pthread_mutex_lock(&mount_info.cache_mutex); + ret = g_hash_table_lookup(mount_info.file_cache, path); if (!ret) { ret = get_file_info(path); if (!ret) goto out; - g_hash_table_replace(file_cache, ret->filename, ret); + g_hash_table_replace(mount_info.file_cache, ret->filename, ret); } // move to end of LRU if (ret->list_node) - g_queue_unlink(cache_lru, ret->list_node); + g_queue_unlink(mount_info.cache_lru, ret->list_node); - g_queue_push_tail(cache_lru, ret); - ret->list_node = cache_lru->tail; + g_queue_push_tail(mount_info.cache_lru, ret); + ret->list_node = mount_info.cache_lru->tail; expire_cache(); out: - pthread_mutex_unlock(&cache_mutex); + pthread_mutex_unlock(&mount_info.cache_mutex); return ret; } @@ -136,8 +144,8 @@ static char *get_source_path(const char *filename) { char *source; - source = g_strdup_printf("%s%s", source_mount, filename); - source = replace_ext(source, dest_ext, src_ext); + source = g_strdup_printf("%s%s", mount_info.source_mount, filename); + source = replace_ext(source, mount_info.dest_ext, mount_info.src_ext); return source; } @@ -157,7 +165,7 @@ int gstfs_getattr(const char *path, struct stat *stbuf) { int ret = 0; char *source_path; - struct gst_file_info *converted; + struct gstfs_file_info *converted; source_path = get_source_path(path); @@ -172,7 +180,7 @@ int gstfs_getattr(const char *path, struct stat *stbuf) static int read_cb(char *buf, size_t size, void *data) { - struct gst_file_info *info = (struct gst_file_info *) data; + struct gstfs_file_info *info = (struct gstfs_file_info *) data; size_t newsz = info->len + size; @@ -192,7 +200,7 @@ static int read_cb(char *buf, size_t size, void *data) int gstfs_read(const char *path, char *buf, size_t size, off_t offset, struct fuse_file_info *fi) { - struct gst_file_info *info = gstfs_lookup(path); + struct gstfs_file_info *info = gstfs_lookup(path); size_t count = 0; if (!info) @@ -217,7 +225,7 @@ out: int gstfs_open(const char *path, struct fuse_file_info *fi) { - struct gst_file_info *info = gstfs_lookup(path); + struct gstfs_file_info *info = gstfs_lookup(path); if (!info) return -ENOENT; @@ -243,7 +251,7 @@ int gstfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler, while ((dirent = readdir(dir))) { char *s = g_strdup(dirent->d_name); - s = replace_ext(s, src_ext, dest_ext); + s = replace_ext(s, mount_info.src_ext, mount_info.dest_ext); filler(buf, s, NULL, 0); g_free(s); @@ -263,8 +271,13 @@ static struct fuse_operations gstfs_ops = { int main(int argc, char *argv[]) { - file_cache = g_hash_table_new(g_str_hash, g_str_equal); - cache_lru = g_queue_new(); + pthread_mutex_init(&mount_info.cache_mutex, NULL); + mount_info.file_cache = g_hash_table_new(g_str_hash, g_str_equal); + mount_info.cache_lru = g_queue_new(); + mount_info.max_cache_entries = 50; + mount_info.source_mount = "ogg"; + mount_info.src_ext = "ogg"; + mount_info.dest_ext = "mp3"; gst_init(&argc, &argv); return fuse_main(argc, argv, &gstfs_ops, NULL); -- 2.11.4.GIT