Skip to content

Commit

Permalink
Primer for config file merging
Browse files Browse the repository at this point in the history
Signed-off-by: Philippe De Swert <philippe.deswert@jollamobile.com>
  • Loading branch information
philippedeswert committed Mar 30, 2013
1 parent df6665e commit 1bf3220
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 2 deletions.
3 changes: 2 additions & 1 deletion configure.ac
Expand Up @@ -20,7 +20,7 @@ test_gcc_flag() {

# We use gnu99 instead of c99 because many have interpreted the standard
# in a way that int64_t isn't defined on non-64 bit platforms.
CFLAGS="-std=gnu99 -Wall -W -Wextra -pedantic -pipe -Wformat -Wold-style-definition -Wdeclaration-after-statement -Wfloat-equal -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wendif-labels -Wpointer-arith -Wcast-align -Wwrite-strings -Winline -Wno-unused-parameter"
CFLAGS="-Os -std=gnu99 -Wall -W -Wextra -pedantic -pipe -Wformat -Wold-style-definition -Wdeclaration-after-statement -Wfloat-equal -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wendif-labels -Wpointer-arith -Wcast-align -Wwrite-strings -Winline -Wno-unused-parameter -finline-small-functions"

AC_ARG_ENABLE([debug], AS_HELP_STRING([--enable-debug],[Enable debug @<:@default=false@:>@]),
[case "${enableval}" in
Expand Down Expand Up @@ -145,6 +145,7 @@ echo "

Compiler: ${CC}
CFLAGS: ${CFLAGS}
LDFLAGS: ${LDFLAGS}

Debug enabled: ${debug}
"
Expand Down
53 changes: 53 additions & 0 deletions src/usb_moded-config.c
Expand Up @@ -286,3 +286,56 @@ int set_network_setting(const char *config, const char *setting)

#endif

int conf_file_merge(void)
{
GDir *confdir;
struct stat fileinfo, dir;
const gchar *filename;
gchar *keyfile_string = NULL;
GKeyFile *settingsfile;
int ret = 0, test = 0;

confdir = g_dir_open(CONFIG_FILE_DIR, 0, NULL);
if(!confdir)
{
log_debug("No configuration. Creating defaults.\n");
create_conf_file();
return (ret);
}

if (stat(FS_MOUNT_CONFIG_FILE, &fileinfo) == -1)
{
/* conf file not created yet, merge all */
goto merge;
}

/* config file is created, so the dir must exists */
stat(CONFIG_FILE_DIR, &dir);

/* st_mtime is changed by file modifications, st_mtime of a directory is changed by the creation or deletion of files in that directory.
So if the st_mtime of the config file is equal to the directory time we can be sure the config is untouched and we do not need
to re-merge the config.
*/
if(fileinfo.st_mtime == dir.st_mtime)
return 0;

merge:
/* check each ini file and get contents */
while((filename = g_dir_read_name(confdir)) != NULL)
{
settingsfile = g_key_file_new();
/* load contents of file, if it fails skip to next one */
test = g_key_file_load_from_file(settingsfile, filename, G_KEY_FILE_NONE, NULL);
if(!test)
break;
keyfile_string = g_strconcat(keyfile_string, g_key_file_to_data(settingsfile, NULL, NULL), NULL);
g_key_file_free(settingsfile);
}

/* write out merged config file */
/* g_file_set_contents returns 1 on succes, this function returns 0 */
ret = !g_file_set_contents(FS_MOUNT_CONFIG_FILE, keyfile_string, -1, NULL);

g_dir_close(confdir);
return(ret);
}
2 changes: 2 additions & 0 deletions src/usb_moded-config.h
Expand Up @@ -78,3 +78,5 @@ const char * get_network_interface(void);
const char * get_network_gateway(void);

const char * get_soft_connect_path(void);

int conf_file_merge(void);
2 changes: 1 addition & 1 deletion src/usb_moded.c
Expand Up @@ -416,7 +416,7 @@ inline const char * get_usb_mode(void)
* @param module The module name for the requested mode
*
*/
inline void set_usb_module(const char *module)
void set_usb_module(const char *module)
{
free(current_mode.module);
current_mode.module = strdup(module);
Expand Down

0 comments on commit 1bf3220

Please sign in to comment.