Commit 229ccd19 authored by xpetrak2's avatar xpetrak2
Browse files

Tweak of paths and configuration responsibilities

parent 54fae336
......@@ -21,152 +21,38 @@
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "autoconfig.h"
#include "configuration.h"
#include "log.h"
#include "paths.h"
/**
* Set home directory.
* Create a directory, if doesn't exist yet.
*
* @param homedir Path to homedir.
* @param dir_path Path to the directory.
* @param mode Directory's permissions.
*
* @return 0 Path successfully set.
* @return 1 Home directory retrieval failure.
* @return 0 Success.
* @return 1 Failure.
*/
static int set_homedir(char **homedir)
{
*homedir = getenv("HOME");
if (!*homedir || *homedir[0] == '\0') {
log_error("Can not find home directory");
return 1;
}
return 0;
}
/**
* Set paths to config and data directories.
*
* @param config_dir Config directory.
* @param data_dir Data directory.
*
* @return 0 Paths successfully set.
* @return 1 Allocation failure.
*/
static int set_directories(char **config_dir, char **data_dir)
{
char *homedir = NULL,
*tmpchar = NULL;
size_t tmpsize;
/* configuration directory */
tmpchar = getenv("XDG_CONFIG_HOME");
if (!tmpchar || tmpchar[0] == '\0') {
if (set_homedir(&homedir)) {
return 1;
}
tmpsize = strlen(homedir);
*config_dir = (char *) malloc(tmpsize +
sizeof("/.config/" PACKAGE_NAME
"/"));
if (!*config_dir) {
log_error("Setting configdir");
return 1;
}
strcpy(*config_dir, homedir);
strcpy(*config_dir + tmpsize, "/.config/" PACKAGE_NAME "/");
} else {
tmpsize = strlen(tmpchar);
*config_dir = (char *) malloc(tmpsize +
sizeof("/" PACKAGE_NAME "/"));
if (!*config_dir) {
log_error("Setting configdir");
return 1;
}
strcpy(*config_dir, tmpchar);
strcpy(*config_dir + tmpsize, "/" PACKAGE_NAME "/");
}
/* data directory */
tmpchar = getenv("XDG_DATA_HOME");
if (!tmpchar || tmpchar[0] == '\0') {
if (!homedir && set_homedir(&homedir)) {
free(config_dir);
return 1;
}
tmpsize = strlen(homedir);
*data_dir = (char *) malloc(tmpsize +
sizeof("/.local/share/" PACKAGE_NAME
"/"));
if (!*data_dir) {
log_error("Setting datadir");
free(config_dir);
return 1;
}
strcpy(*data_dir, homedir);
strcpy(*data_dir + tmpsize, "/.local/share/" PACKAGE_NAME "/");
} else {
tmpsize = strlen(tmpchar);
*data_dir = (char *) malloc(tmpsize +
sizeof("/" PACKAGE_NAME "/"));
if (!*data_dir) {
log_error("Setting datadir");
free(config_dir);
return 1;
}
strcpy(*data_dir, tmpchar);
strcpy(*data_dir + tmpsize, "/" PACKAGE_NAME "/");
}
return 0;
}
/**
* Creates directories if they don't exist yet.
*
* @param config_dir Config directory.
* @param data_dir Data directory.
*
* @return 0 Directories created.
* @return 1 Creating directories failed.
*/
static int create_dirs(const char *config_dir, const char *data_dir)
static int create_dir(const char *dir_path, mode_t mode)
{
struct stat buffer;
/* configuration directory */
if (stat(config_dir, &buffer)) {
if (errno == ENOENT) {
/* create */
if (mkdir(config_dir, S_IRWXU)) {
log_error("Could not create configuration "
"directory %s", config_dir);
return 1;
} else {
log_debug("create_dirs - created configuration "
"directory %s", config_dir);
}
} else {
log_error("Could not open configuration "
"directory %s", config_dir);
return 1;
}
}
/* data directory */
if (stat(data_dir, &buffer)) {
if (stat(dir_path, &buffer)) {
if (errno == ENOENT) {
/* create */
if (mkdir(data_dir, S_IRWXU)) {
log_error("Could not create "
"data directory %s", data_dir);
if (mkdir(dir_path, mode)) {
log_error("Could not create directory %s",
dir_path);
return 1;
} else {
log_debug("create_dirs - created data "
"directory %s", data_dir);
log_debug("create_dir - directory %s created",
dir_path);
}
} else {
log_error("Could not open data directory %s", data_dir);
log_error("Could not open directory %s", dir_path);
return 1;
}
}
......@@ -175,18 +61,18 @@ static int create_dirs(const char *config_dir, const char *data_dir)
}
/**
* Create needed directories and fetch their paths into params.
* Create needed directories.
*
* @param config_dir Config directory.
* @param data_dir Data directory.
* @param paths Create directories for dir paths in here.
*
* @return 0 Directories created.
* @return 1 Directories setup failure.
* @return 0 Directories created.
* @return 1 Failure.
*/
int setup_directories(char **config_dir, char **data_dir)
int create_dirs(const filepaths_t *paths)
{
if (set_directories(config_dir, data_dir) ||
create_dirs(*config_dir, *data_dir)) {
if (create_dir(paths->config_dir, S_IRWXU) ||
create_dir(paths->data_dir, S_IRWXU)) {
log_error("Creating directories");
return 1;
}
......
......@@ -19,6 +19,8 @@
#ifndef CONFIGURATION_H
#define CONFIGURATION_H
int setup_directories(char **config_dir, char **data_dir);
#include "paths.h"
int create_dirs(const filepaths_t *paths);
#endif /* CONFIGURATION_H */
......@@ -18,6 +18,7 @@
#include <event2/event.h>
#include "configuration.h"
#include "global_state.h"
#include "linkedlist.h"
#include "log.h"
......@@ -51,6 +52,9 @@ void global_state_clear(global_state_t *global_state)
* Initialize global state variables.
*
* @param global_state The global state.
*
* @return 0 Successfully initialized.
* @return 1 Failure.
*/
int global_state_init(global_state_t *global_state)
{
......@@ -59,6 +63,10 @@ int global_state_init(global_state_t *global_state)
log_error("Initializing paths to needed files/dirs");
return 1;
}
if (create_dirs(&global_state->filepaths)) {
log_error("Creating directories");
return 1;
}
linkedlist_init(&global_state->identities);
if (!(global_state->true_identity = identity_generate(0x00))) {
......
......@@ -20,12 +20,86 @@
#include <stdlib.h>
#include <string.h>
#include "configuration.h"
#include "autoconfig.h"
#include "log.h"
#include "paths.h"
#define HOSTS_FILE_NAME "hosts"
static int set_dir_path(const char *location,
const char *dir_name,
char **dir_path);
/**
* Set path to home directory.
*
* @param homedir Path to home directory.
*
* @return 0 Path successfully set.
* @return 1 Home directory retrieval failure.
*/
static int set_homedir(char **homedir)
{
*homedir = getenv("HOME");
if (!*homedir || *homedir[0] == '\0') {
log_error("Can not find home directory");
return 1;
}
return 0;
}
/**
* Set path to config directory.
*
* @param config_dir Path to config directory.
*
* @return 0 Successfully set.
* @return 1 Failure.
*/
static int set_config_dir_path(char **config_dir)
{
char *homedir = NULL,
*tmpchar = NULL;
tmpchar = getenv("XDG_CONFIG_HOME");
if (!tmpchar || tmpchar[0] == '\0') {
if (set_homedir(&homedir)) {
return 1;
}
return set_dir_path(homedir,
"/.config/" PACKAGE,
config_dir);
}
return set_dir_path(tmpchar, "/" PACKAGE, config_dir);
}
/**
* Set path to data directory.
*
* @param data_dir Path to data directory.
*
* @return 0 Successfully set.
* @return 1 Failure.
*/
static int set_data_dir_path(char **data_dir)
{
char *homedir = NULL,
*tmpchar = NULL;
tmpchar = getenv("XDG_DATA_HOME");
if (!tmpchar || tmpchar[0] == '\0') {
if (set_homedir(&homedir)) {
return 1;
}
return set_dir_path(homedir,
"/.local/share/" PACKAGE,
data_dir);
}
return set_dir_path(tmpchar, "/" PACKAGE, data_dir);
}
/**
* Sets path to hosts file.
*
......@@ -51,6 +125,36 @@ static int set_hosts_path(char *data_dir, char **hosts)
return 0;
}
/**
* Set path to a directory.
*
* @param location Directory's location.
* @param dir_name Directory's name.
* @param dir_path Path to the directory.
*
* @return 0 Successfully set.
* @return 1 Failure.
*/
static int set_dir_path(const char *location,
const char *dir_name,
char **dir_path)
{
*dir_path = (char *) malloc((strlen(location) + strlen(dir_name)) *
sizeof(char) + sizeof("/"));
if (!*dir_path) {
log_error("Setting directory path for %s", dir_name);
return 1;
}
strcpy(*dir_path, location);
strcat(*dir_path, dir_name);
strcat(*dir_path, "/");
return 0;
}
/**
* Initializes a filepaths_t instance.
*
......@@ -61,12 +165,15 @@ static int set_hosts_path(char *data_dir, char **hosts)
*/
int setup_paths(filepaths_t *filepaths)
{
if (setup_directories(&filepaths->config_dir,
&filepaths->data_dir)) {
if (set_config_dir_path(&filepaths->config_dir) ||
set_data_dir_path(&filepaths->data_dir)) {
log_error("Setting directory paths failed");
return 1;
}
set_hosts_path(filepaths->data_dir, &filepaths->hosts);
if (set_hosts_path(filepaths->data_dir, &filepaths->hosts)) {
return 1;
}
return 0;
}
......
......@@ -21,10 +21,8 @@
/** Paths to needed files and directories. */
typedef struct s_filepaths {
/**< Path to config directory. */
char *config_dir;
/**< Path to data directory. */
char *data_dir;
char *config_dir; /**< Config directory. */
char *data_dir; /**< Data directory. */
/**< Path to file with addresses of hosts. */
char *hosts;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment