Skip to content

Commit

Permalink
use a common function for opening regular files
Browse files Browse the repository at this point in the history
Fixes #514
  • Loading branch information
derselbst committed Apr 18, 2019
1 parent b817232 commit 7f11a9b
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 33 deletions.
8 changes: 1 addition & 7 deletions src/midi/fluid_midi.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,7 @@ int fluid_is_midifile(const char *filename)

do
{
if(!fluid_file_test(filename, G_FILE_TEST_IS_REGULAR))
{
return retcode;
}

// file seems to exist and is a regular file or a symlink to such
if((fp = FLUID_FOPEN(filename, "rb")) == NULL)
if((fp = fluid_file_open(filename, NULL)) == NULL)
{
return retcode;
}
Expand Down
2 changes: 1 addition & 1 deletion src/sfloader/fluid_defsfont.c
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ int fluid_defsfont_load(fluid_defsfont_t *defsfont, const fluid_file_callbacks_t

if(sfdata == NULL)
{
FLUID_LOG(FLUID_ERR, "Couldn't load soundfont file");
/* error message already printed */
return FLUID_FAILED;
}

Expand Down
8 changes: 1 addition & 7 deletions src/sfloader/fluid_sffile.c
Original file line number Diff line number Diff line change
Expand Up @@ -340,13 +340,7 @@ int fluid_is_soundfont(const char *filename)

do
{
if(!fluid_file_test(filename, G_FILE_TEST_IS_REGULAR))
{
return retcode;
}

// file seems to exist and is a regular file or a symlink to such
if((fp = FLUID_FOPEN(filename, "rb")) == NULL)
if((fp = fluid_file_open(filename, NULL)) == NULL)
{
return retcode;
}
Expand Down
22 changes: 5 additions & 17 deletions src/sfloader/fluid_sfont.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,14 @@

void *default_fopen(const char *path)
{
FILE* handle;
const char* msg;
FILE* handle = fluid_file_open(path, &msg);

if(!fluid_file_test(path, G_FILE_TEST_EXISTS))
if(handle == NULL)
{
FLUID_LOG(FLUID_ERR, "fluid_sfloader_load(): Unable to load non-existent file. ('%s')", path);
return NULL;
}

if(!fluid_file_test(path, G_FILE_TEST_IS_REGULAR))
{
FLUID_LOG(FLUID_ERR, "fluid_sfloader_load(): Refusing to load non-regular file! ('%s')", path);
return NULL;
FLUID_LOG(FLUID_ERR, "fluid_sfloader_load(): Failed to open '%s': %s", path, msg);
}

if((handle = FLUID_FOPEN(path, "rb")) == NULL)
{
FLUID_LOG(FLUID_ERR, "fluid_sfloader_load(): Specified file does not exists or insufficient permissions to open it! ('%s')", path);
return NULL;
}


return handle;
}

Expand Down
33 changes: 33 additions & 0 deletions src/utils/fluid_sys.c
Original file line number Diff line number Diff line change
Expand Up @@ -1610,3 +1610,36 @@ void delete_fluid_server_socket(fluid_server_socket_t *server_socket)
}

#endif // NETWORK_SUPPORT

FILE* fluid_file_open(const char* path, const char** errMsg)
{
static const char ErrExist[] = "File does not exist.";
static const char ErrRegular[] = "File is not regular, refusing to open it.";
static const char ErrNull[] = "File does not exists or insufficient permissions to open it.";

FILE* handle = NULL;

if(!g_file_test(path, G_FILE_TEST_EXISTS))
{
if(errMsg != NULL)
{
*errMsg = ErrExist;
}
}
else if(!g_file_test(path, G_FILE_TEST_IS_REGULAR))
{
if(errMsg != NULL)
{
*errMsg = ErrRegular;
}
}
else if((handle = FLUID_FOPEN(path, "rb")) == NULL)
{
if(errMsg != NULL)
{
*errMsg = ErrNull;
}
}

return handle;
}
2 changes: 1 addition & 1 deletion src/utils/fluid_sys.h
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ fluid_ostream_t fluid_socket_get_ostream(fluid_socket_t sock);
typedef GStatBuf fluid_stat_buf_t;
#endif

#define fluid_file_test g_file_test
FILE* fluid_file_open(const char* filename, const char** errMsg);

/* Profiling */
#if WITH_PROFILING
Expand Down

0 comments on commit 7f11a9b

Please sign in to comment.