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

View file

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

View file

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

View file

@ -574,13 +574,10 @@ static void init(void)
#ifdef AUTOROCK
{
int fd;
static const char filename[] = PLUGIN_APPS_DIR "/autostart.rock";
fd = open(filename, O_RDONLY);
if(fd >= 0) /* no complaint if it doesn't exist */
if(file_exists(filename)) /* no complaint if it doesn't exist */
{
close(fd);
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;
int srcdirlen = strlen(src);
int targetdirlen = strlen(target);
int fd;
bool result = true;
/* Check if the target exists */
fd = open(target, O_RDONLY);
close(fd);
if (fd < 0) {
if (!file_exists(target)) {
if (!copy) {
/* Just move the directory */
result = rename(src, target) == 0;
@ -871,7 +866,6 @@ static bool clipboard_paste(void)
char target[MAX_PATH];
char *cwd, *nameptr;
bool success;
int target_fd;
unsigned char *lines[]={ID2P(LANG_REALLY_OVERWRITE)};
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 */
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 (target_fd >= 0 &&
if (file_exists(target) &&
(gui_syncyesno_run(&message, NULL, NULL) == YESNO_NO)) {
return false;
}

View file

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

View file

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