From d87b037efe7d001902c0cde992e1633ff9f70061 Mon Sep 17 00:00:00 2001 From: Robert Kukla Date: Wed, 21 Nov 2007 21:28:27 +0000 Subject: consolidate the 3 file_exists() functions into one; use the version that explicitly uses dircache; give dir_exists() the same treatment for consistency git-svn-id: svn://svn.rockbox.org/rockbox/trunk@15742 a1c6a512-1295-4272-9138-f99709370657 --- apps/misc.c | 32 +++++++++++++++++++++++++++++++ apps/misc.h | 4 ++++ apps/plugin.c | 3 +++ apps/plugin.h | 6 +++++- apps/plugins/shortcuts/shortcuts.h | 3 --- apps/plugins/shortcuts/shortcuts_append.c | 4 ++-- apps/plugins/shortcuts/shortcuts_common.c | 32 ------------------------------- apps/plugins/shortcuts/shortcuts_view.c | 4 ++-- apps/recorder/albumart.c | 21 +------------------- apps/tagcache.c | 6 +----- 10 files changed, 50 insertions(+), 65 deletions(-) diff --git a/apps/misc.c b/apps/misc.c index 4af97afb5e..a8710c312f 100644 --- a/apps/misc.c +++ b/apps/misc.c @@ -1092,3 +1092,35 @@ char* strrsplt(char* str, int c) return s; } +/* Test file existence, using dircache of possible */ +bool file_exists(const char *file) +{ + int fd; + + if (!file || strlen(file) <= 0) + return false; + +#ifdef HAVE_DIRCACHE + if (dircache_is_enabled()) + return (dircache_get_entry_ptr(file) != NULL); +#endif + + fd = open(file, O_RDONLY); + if (fd < 0) + return false; + close(fd); + return true; +} + +bool dir_exists(const char *path) +{ + DIR* d = opendir(path); + bool retval; + if (d != NULL) { + closedir(d); + retval = true; + } else { + retval = false; + } + return retval; +} diff --git a/apps/misc.h b/apps/misc.h index ab0e592aa0..590ccf013f 100644 --- a/apps/misc.h +++ b/apps/misc.h @@ -115,4 +115,8 @@ int hex_to_rgb(const char* hex); char* strrsplt(char* str, int c); +bool file_exists(const char *file); +bool dir_exists(const char *path); + + #endif /* MISC_H */ diff --git a/apps/plugin.c b/apps/plugin.c index 145d30b41b..9b620b1280 100644 --- a/apps/plugin.c +++ b/apps/plugin.c @@ -522,6 +522,9 @@ static const struct plugin_api rockbox_api = { #ifdef PROC_NEEDS_CACHEALIGN align_buffer, #endif + + file_exists, + dir_exists, }; int plugin_load(const char* plugin, void* parameter) diff --git a/apps/plugin.h b/apps/plugin.h index 9123af44ec..0aacd4e5fa 100644 --- a/apps/plugin.h +++ b/apps/plugin.h @@ -113,7 +113,7 @@ #define PLUGIN_MAGIC 0x526F634B /* RocK */ /* increase this every time the api struct changes */ -#define PLUGIN_API_VERSION 89 +#define PLUGIN_API_VERSION 90 /* update this to latest version if a change to the api struct breaks backwards compatibility (and please take the opportunity to sort in any @@ -645,6 +645,10 @@ struct plugin_api { #ifdef PROC_NEEDS_CACHEALIGN size_t (*align_buffer)(void **start, size_t size, size_t align); #endif + + bool (*file_exists)(const char *file); + bool (*dir_exists)(const char *path); + }; /* plugin header */ diff --git a/apps/plugins/shortcuts/shortcuts.h b/apps/plugins/shortcuts/shortcuts.h index 6ccb320a36..34f200d193 100644 --- a/apps/plugins/shortcuts/shortcuts.h +++ b/apps/plugins/shortcuts/shortcuts.h @@ -79,9 +79,6 @@ bool remove_entry(sc_file_t *file, int entry_idx); /* Checks whether the index is a valid one for the file. */ bool is_valid_index(sc_file_t *file, int entry_idx); -bool file_exists(char *filename); /* Does the specified file exist? */ -bool dir_exists(char *path); /* Does the specified dir exist? */ - #ifdef SC_DEBUG void print_file(sc_file_t *file); diff --git a/apps/plugins/shortcuts/shortcuts_append.c b/apps/plugins/shortcuts/shortcuts_append.c index bc287234dd..afd03929ae 100644 --- a/apps/plugins/shortcuts/shortcuts_append.c +++ b/apps/plugins/shortcuts/shortcuts_append.c @@ -70,9 +70,9 @@ enum plugin_status plugin_start(struct plugin_api* api, void* void_parameter) * if it's a dir and then file (not vice versa) since * open() can also open a dir */ found = true; - if (dir_exists(parameter)) { + if (rb->dir_exists(parameter)) { its_a_dir = true; - } else if (file_exists(parameter)) { + } else if (rb->file_exists(parameter)) { its_a_dir = false; } else { found = false; diff --git a/apps/plugins/shortcuts/shortcuts_common.c b/apps/plugins/shortcuts/shortcuts_common.c index 4a097f0a0d..91e3084e10 100644 --- a/apps/plugins/shortcuts/shortcuts_common.c +++ b/apps/plugins/shortcuts/shortcuts_common.c @@ -360,35 +360,3 @@ void write_entry_to_file(int fd, sc_entry_t *entry) } rb->fdprintf(fd, "\n"); } - - -bool file_exists(char *filename) -{ - int fd = rb->open(filename, O_RDONLY); - bool retval; - if (fd >= 0) { - rb->close(fd); - retval = true; - } else { - retval = false; - } - DEBUGF("Checked existence of the file '%s': %s\n", - filename, (retval ? "found" : "NOT FOUND")); - return retval; -} - - -bool dir_exists(char *path) -{ - DIR* d = rb->opendir(path); - bool retval; - if (d != NULL) { - rb->closedir(d); - retval = true; - } else { - retval = false; - } - DEBUGF("Checked existence of the dir '%s': %s\n", - path, (retval ? "found" : "NOT FOUND")); - return retval; -} diff --git a/apps/plugins/shortcuts/shortcuts_view.c b/apps/plugins/shortcuts/shortcuts_view.c index f61177f37a..4ef1bbc816 100644 --- a/apps/plugins/shortcuts/shortcuts_view.c +++ b/apps/plugins/shortcuts/shortcuts_view.c @@ -166,10 +166,10 @@ bool goto_entry(char *file_or_dir) char *what; if (is_dir) { what = "Directory"; - exists = dir_exists(file_or_dir); + exists = rb->dir_exists(file_or_dir); } else { what = "File"; - exists = file_exists(file_or_dir); + exists = rb->file_exists(file_or_dir); } if (!exists) { diff --git a/apps/recorder/albumart.c b/apps/recorder/albumart.c index d99280ce85..6aaf3885d7 100644 --- a/apps/recorder/albumart.c +++ b/apps/recorder/albumart.c @@ -26,6 +26,7 @@ #include "buffering.h" #include "dircache.h" #include "debug.h" +#include "misc.h" /* Strip filename from a full path @@ -89,26 +90,6 @@ static char* strip_extension(char* buf, int buf_size, const char* file) return buf; } -/* Test file existence, using dircache of possible */ -static bool file_exists(const char *file) -{ - int fd; - - if (!file || strlen(file) <= 0) - return false; - -#ifdef HAVE_DIRCACHE - if (dircache_is_enabled()) - return (dircache_get_entry_ptr(file) != NULL); -#endif - - fd = open(file, O_RDONLY); - if (fd < 0) - return false; - close(fd); - return true; -} - /* Make sure part of path only contain chars valid for a FAT32 long name. * Double quotes are replaced with single quotes, other unsupported chars * are replaced with an underscore. diff --git a/apps/tagcache.c b/apps/tagcache.c index 91220a3517..2882aa8b00 100644 --- a/apps/tagcache.c +++ b/apps/tagcache.c @@ -3807,12 +3807,8 @@ static bool check_dir(const char *dirname) /* check for a database.ignore file */ snprintf(newpath, MAX_PATH, "%s/database.ignore", dirname); - len = open(newpath, O_RDONLY); - if (len >= 0) - { - close(len); + if(file_exists(newpath)) return false; - } /* Recursively scan the dir. */ #ifdef __PCTOOL__ -- cgit v1.2.3