summaryrefslogblamecommitdiffstats
path: root/src/server/globals.c
blob: d84e3f7905c65d6b07e0518e79d83ad7b39891c1 (plain) (tree)
1
2
3
4
5
6
7
8
9
                    

                   
                     
                   

                   
 
                        

                            
                  























                                                                                                                                                                                                                               
#include "globals.h"
#include "ini.h"
#include "memlog.h"
#include "../types.h"
#include <stddef.h>
#include <string.h>
#include <stdlib.h>

char *_configDir = NULL;
char *_basePath = NULL;
int _vmdkLegacyMode = FALSE;
int _shutdown = 0;

#define SAVE_TO_VAR_STR(ss, kk) do { if (strcmp(section, #ss) == 0 && strcmp(key, #kk) == 0) { if (_ ## kk != NULL) free(_ ## kk); _ ## kk = strdup(value); } } while (0)
#define SAVE_TO_VAR_BOOL(ss, kk) do { if (strcmp(section, #ss) == 0 && strcmp(key, #kk) == 0) _ ## kk = atoi(value) != 0 || strcmp(value, "true") == 0 || strcmp(value, "True") == 0 || strcmp(value, "TRUE") == 0; } while (0)
#define SAVE_TO_VAR_INT(ss, kk) do { if (strcmp(section, #ss) == 0 && strcmp(key, #kk) == 0) _ ## kk = atoi(value); } while (0)

static int ini_handler(void *custom, const char* section, const char* key, const char* value)
{
	SAVE_TO_VAR_STR( dnbd3, basePath );
	SAVE_TO_VAR_BOOL( dnbd3, vmdkLegacyMode );
	return TRUE;
}

void globals_loadConfig()
{
	char *name = NULL;
	asprintf( &name, "%s/%s", _configDir, CONFIG_FILENAME );
	if ( name == NULL ) return;
	ini_parse( name, &ini_handler, NULL );
	free( name );
	if ( _basePath != NULL && _basePath[0] != '/' ) {
		memlogf( "[ERROR] _basePath must be absolute!" );
		exit( EXIT_FAILURE );
	}
}