Use file_exists and dir_exists functions where appropriate, fix one wrong file descriptor check and one possible dir descriptor leak

git-svn-id: svn://svn.rockbox.org/rockbox/trunk@17147 a1c6a512-1295-4272-9138-f99709370657
This commit is contained in:
Nils Wallménius 2008-04-16 19:51:43 +00:00
parent d65930f972
commit a01996436d
8 changed files with 18 additions and 52 deletions

View file

@ -380,8 +380,6 @@ static bool check_bookmark(const char* bookmark)
/* ------------------------------------------------------------------------*/ /* ------------------------------------------------------------------------*/
bool bookmark_autoload(const char* file) bool bookmark_autoload(const char* file)
{ {
int fd;
if(global_settings.autoloadbookmark == BOOKMARK_NO) if(global_settings.autoloadbookmark == BOOKMARK_NO)
return false; return false;
@ -390,10 +388,10 @@ bool bookmark_autoload(const char* file)
{ {
return false; return false;
} }
fd = open(global_bookmark_file_name, O_RDONLY);
if(fd<0) if(!file_exists(global_bookmark_file_name))
return false; return false;
close(fd);
if(global_settings.autoloadbookmark == BOOKMARK_YES) if(global_settings.autoloadbookmark == BOOKMARK_YES)
{ {
return bookmark_load(global_bookmark_file_name, true); return bookmark_load(global_bookmark_file_name, true);
@ -1040,12 +1038,7 @@ bool bookmark_exist(void)
sizeof(global_temp_buffer)); sizeof(global_temp_buffer));
if (generate_bookmark_file_name(name)) if (generate_bookmark_file_name(name))
{ {
int fd=open(global_bookmark_file_name, O_RDONLY); exist = file_exists(global_bookmark_file_name);
if (fd >=0)
{
close(fd);
exist=true;
}
} }
} }

View file

@ -91,22 +91,20 @@ bool look_for_cuesheet_file(const char *trackpath, char *found_cue_path)
dot = strrchr(cuepath, '.'); dot = strrchr(cuepath, '.');
strcpy(dot, ".cue"); strcpy(dot, ".cue");
int fd = open(cuepath,O_RDONLY); if (!file_exists(cuepath))
if (fd < 0)
{ {
strcpy(cuepath, CUE_DIR); strcpy(cuepath, CUE_DIR);
strcat(cuepath, slash); strcat(cuepath, slash);
char *dot = strrchr(cuepath, '.'); char *dot = strrchr(cuepath, '.');
strcpy(dot, ".cue"); strcpy(dot, ".cue");
fd = open(cuepath,O_RDONLY); if (!file_exists(cuepath))
if (fd < 0)
{ {
if (found_cue_path) if (found_cue_path)
found_cue_path = NULL; found_cue_path = NULL;
return false; return false;
} }
} }
close(fd);
if (found_cue_path) if (found_cue_path)
strncpy(found_cue_path, cuepath, MAX_PATH); strncpy(found_cue_path, cuepath, MAX_PATH);
return true; return true;

View file

@ -46,7 +46,7 @@ int lang_load(const char *filename)
int fd = open(filename, O_RDONLY); int fd = open(filename, O_RDONLY);
int retcode=0; int retcode=0;
unsigned char lang_header[3]; unsigned char lang_header[3];
if(fd == -1) if(fd < 0)
return 1; return 1;
fsize = filesize(fd) - 2; fsize = filesize(fd) - 2;
if(fsize <= MAX_LANGUAGE_SIZE) { if(fsize <= MAX_LANGUAGE_SIZE) {

View file

@ -574,13 +574,10 @@ static void init(void)
#ifdef AUTOROCK #ifdef AUTOROCK
{ {
int fd;
static const char filename[] = PLUGIN_APPS_DIR "/autostart.rock"; static const char filename[] = PLUGIN_APPS_DIR "/autostart.rock";
fd = open(filename, O_RDONLY); if(file_exists(filename)) /* no complaint if it doesn't exist */
if(fd >= 0) /* no complaint if it doesn't exist */
{ {
close(fd);
plugin_load((char*)filename, NULL); /* start if it does */ plugin_load((char*)filename, NULL); /* start if it does */
} }
} }

View file

@ -783,14 +783,9 @@ static bool clipboard_pastedirectory(char *src, int srclen, char *target,
DIR *srcdir; DIR *srcdir;
int srcdirlen = strlen(src); int srcdirlen = strlen(src);
int targetdirlen = strlen(target); int targetdirlen = strlen(target);
int fd;
bool result = true; bool result = true;
/* Check if the target exists */ if (!file_exists(target)) {
fd = open(target, O_RDONLY);
close(fd);
if (fd < 0) {
if (!copy) { if (!copy) {
/* Just move the directory */ /* Just move the directory */
result = rename(src, target) == 0; result = rename(src, target) == 0;
@ -871,7 +866,6 @@ static bool clipboard_paste(void)
char target[MAX_PATH]; char target[MAX_PATH];
char *cwd, *nameptr; char *cwd, *nameptr;
bool success; bool success;
int target_fd;
unsigned char *lines[]={ID2P(LANG_REALLY_OVERWRITE)}; unsigned char *lines[]={ID2P(LANG_REALLY_OVERWRITE)};
struct text_message message={(char **)lines, 1}; struct text_message message={(char **)lines, 1};
@ -885,12 +879,8 @@ static bool clipboard_paste(void)
/* Final target is current directory plus name of selection */ /* Final target is current directory plus name of selection */
snprintf(target, sizeof(target), "%s%s", cwd[1] ? cwd : "", nameptr); snprintf(target, sizeof(target), "%s%s", cwd[1] ? cwd : "", nameptr);
/* Check if we're going to overwrite */
target_fd = open(target, O_RDONLY);
close(target_fd);
/* If the target existed but they choose not to overwite, exit */ /* If the target existed but they choose not to overwite, exit */
if (target_fd >= 0 && if (file_exists(target) &&
(gui_syncyesno_run(&message, NULL, NULL) == YESNO_NO)) { (gui_syncyesno_run(&message, NULL, NULL) == YESNO_NO)) {
return false; return false;
} }

View file

@ -64,10 +64,8 @@ static int initialize_catalog(void)
if (!initialized) if (!initialized)
{ {
DIR* dir;
bool default_dir = true; bool default_dir = true;
/* directory config is of the format: "dir: /path/to/dir" */ /* directory config is of the format: "dir: /path/to/dir" */
if (global_settings.playlist_catalog_dir[0]) if (global_settings.playlist_catalog_dir[0])
{ {
@ -82,11 +80,9 @@ static int initialize_catalog(void)
playlist_dir_length = strlen(playlist_dir); playlist_dir_length = strlen(playlist_dir);
dir = opendir(playlist_dir); if (dir_exists(playlist_dir))
if (dir)
{ {
playlist_dir_exists = true; playlist_dir_exists = true;
closedir(dir);
memset(most_recent_playlist, 0, sizeof(most_recent_playlist)); memset(most_recent_playlist, 0, sizeof(most_recent_playlist));
initialized = true; initialized = true;
} }

View file

@ -1211,9 +1211,9 @@ static int save_preset_list(void)
{ {
bool bad_file_name = true; bool bad_file_name = true;
if(!opendir(FMPRESET_PATH)) /* Check if there is preset folder */ if(!dir_exists(FMPRESET_PATH)) /* Check if there is preset folder */
mkdir(FMPRESET_PATH); mkdir(FMPRESET_PATH);
create_numbered_filename(filepreset, FMPRESET_PATH, "preset", create_numbered_filename(filepreset, FMPRESET_PATH, "preset",
".fmr", 2 IF_CNFN_NUM_(, NULL)); ".fmr", 2 IF_CNFN_NUM_(, NULL));

View file

@ -269,8 +269,7 @@ static int tree_voice_cb(int selected_item, void * data)
bool check_rockboxdir(void) bool check_rockboxdir(void)
{ {
DIR *dir = opendir(ROCKBOX_DIR); if(!dir_exists(ROCKBOX_DIR))
if(!dir)
{ /* No need to localise this message. { /* No need to localise this message.
If .rockbox is missing, it wouldn't work anyway */ If .rockbox is missing, it wouldn't work anyway */
int i; int i;
@ -282,7 +281,6 @@ bool check_rockboxdir(void)
gui_syncsplash(HZ*2, "Installation incomplete"); gui_syncsplash(HZ*2, "Installation incomplete");
return false; return false;
} }
closedir(dir);
return true; return true;
} }
@ -1086,10 +1084,8 @@ bool bookmark_play(char *resume_file, int index, int offset, int seed,
/* Playlist playback */ /* Playlist playback */
char* slash; char* slash;
/* check that the file exists */ /* check that the file exists */
int fd = open(resume_file, O_RDONLY); if(!file_exists(resume_file))
if(fd<0)
return false; return false;
close(fd);
slash = strrchr(resume_file,'/'); slash = strrchr(resume_file,'/');
if (slash) if (slash)
@ -1171,7 +1167,6 @@ static void say_filetype(int attr)
static int ft_play_dirname(char* name) static int ft_play_dirname(char* name)
{ {
int fd;
char dirname_mp3_filename[MAX_PATH+1]; char dirname_mp3_filename[MAX_PATH+1];
#if CONFIG_CODEC != SWCODEC #if CONFIG_CODEC != SWCODEC
@ -1185,15 +1180,12 @@ static int ft_play_dirname(char* name)
DEBUGF("Checking for %s\n", dirname_mp3_filename); DEBUGF("Checking for %s\n", dirname_mp3_filename);
fd = open(dirname_mp3_filename, O_RDONLY); if (!file_exists(dirname_mp3_filename))
if (fd < 0)
{ {
DEBUGF("Failed to find: %s\n", dirname_mp3_filename); DEBUGF("Failed to find: %s\n", dirname_mp3_filename);
return -1; return -1;
} }
close(fd);
DEBUGF("Found: %s\n", dirname_mp3_filename); DEBUGF("Found: %s\n", dirname_mp3_filename);
talk_file(dirname_mp3_filename, false); talk_file(dirname_mp3_filename, false);