3
0
mirror of https://git.kernel.org/pub/scm/network/wireless/iwd.git synced 2024-11-26 10:39:23 +01:00

storage: Add support for utilizing STATE_DIRECTORY environment

This commit is contained in:
Marcel Holtmann 2019-09-08 19:18:48 +02:00
parent 45d5c67cb4
commit 8706be6698
3 changed files with 86 additions and 13 deletions

View File

@ -507,6 +507,7 @@ fail_netdev:
dbus_exit(); dbus_exit();
l_dbus_destroy(dbus); l_dbus_destroy(dbus);
storage_cleanup_dirs();
fail_dbus: fail_dbus:
l_genl_unref(genl); l_genl_unref(genl);
fail_genl: fail_genl:

View File

@ -46,6 +46,11 @@
#define STORAGE_DIR_MODE (S_IRUSR | S_IWUSR | S_IXUSR) #define STORAGE_DIR_MODE (S_IRUSR | S_IWUSR | S_IXUSR)
#define STORAGE_FILE_MODE (S_IRUSR | S_IWUSR) #define STORAGE_FILE_MODE (S_IRUSR | S_IWUSR)
#define KNOWN_FREQ_FILENAME ".known_network.freq"
static char *storage_path = NULL;
static char *storage_hotspot_path = NULL;
static int create_dirs(const char *filename) static int create_dirs(const char *filename)
{ {
struct stat st; struct stat st;
@ -167,19 +172,80 @@ error_create_dirs:
bool storage_create_dirs(void) bool storage_create_dirs(void)
{ {
if (create_dirs(DAEMON_STORAGEDIR "/")) { const char *state_dir;
l_error("Failed to create " DAEMON_STORAGEDIR "/"); char **state_dirs;
state_dir = getenv("STATE_DIRECTORY");
if (!state_dir)
state_dir = DAEMON_STORAGEDIR;
l_debug("Using state directory %s", state_dir);
state_dirs = l_strsplit(state_dir, ':');
if (!state_dirs[0]) {
l_strv_free(state_dirs);
return false; return false;
} }
if (create_dirs(DAEMON_STORAGEDIR "/hotspot/")) { storage_path = l_strdup(state_dirs[0]);
l_error("Failed to create " DAEMON_STORAGEDIR "/hotspot/"); storage_hotspot_path = l_strdup_printf("%s/hotspot", state_dirs[0]);
l_strv_free(state_dirs);
if (create_dirs(storage_path)) {
l_error("Failed to create %s", storage_path);
return false;
}
if (create_dirs(storage_hotspot_path)) {
l_error("Failed to create %s", storage_hotspot_path);
return false; return false;
} }
return true; return true;
} }
void storage_cleanup_dirs(void)
{
l_free(storage_path);
l_free(storage_hotspot_path);
}
char *storage_get_path(const char *format, ...)
{
va_list args;
char *fmt, *str;
if (!format)
return l_strdup(storage_path);
fmt = l_strdup_printf("%s/%s", storage_path, format);
va_start(args, format);
str = l_strdup_vprintf(fmt, args);
va_end(args);
l_free(fmt);
return str;
}
char *storage_get_hotspot_path(const char *format, ...)
{
va_list args;
char *fmt, *str;
if (!format)
return l_strdup(storage_hotspot_path);
fmt = l_strdup_printf("%s/%s", storage_hotspot_path, format);
va_start(args, format);
str = l_strdup_vprintf(fmt, args);
va_end(args);
l_free(fmt);
return str;
}
char *storage_get_network_file_path(enum security type, const char *ssid) char *storage_get_network_file_path(enum security type, const char *ssid)
{ {
char *path; char *path;
@ -193,14 +259,10 @@ char *storage_get_network_file_path(enum security type, const char *ssid)
if (*c) { if (*c) {
hex = l_util_hexstring((const unsigned char *) ssid, hex = l_util_hexstring((const unsigned char *) ssid,
strlen(ssid)); strlen(ssid));
path = storage_get_path("/=%s.%s", hex, security_to_str(type));
path = l_strdup_printf(DAEMON_STORAGEDIR "/=%s.%s", hex,
security_to_str(type));
l_free(hex); l_free(hex);
} else } else
path = l_strdup_printf(DAEMON_STORAGEDIR "/%s.%s", ssid, path = storage_get_path("/%s.%s", ssid, security_to_str(type));
security_to_str(type));
return path; return path;
} }
@ -327,32 +389,39 @@ int storage_network_remove(enum security type, const char *ssid)
return ret < 0 ? -errno : 0; return ret < 0 ? -errno : 0;
} }
static const char *known_freq_file_path =
DAEMON_STORAGEDIR "/.known_network.freq";
struct l_settings *storage_known_frequencies_load(void) struct l_settings *storage_known_frequencies_load(void)
{ {
struct l_settings *known_freqs; struct l_settings *known_freqs;
char *known_freq_file_path;
known_freqs = l_settings_new(); known_freqs = l_settings_new();
known_freq_file_path = storage_get_path("/%", KNOWN_FREQ_FILENAME);
if (!l_settings_load_from_file(known_freqs, known_freq_file_path)) { if (!l_settings_load_from_file(known_freqs, known_freq_file_path)) {
l_settings_free(known_freqs); l_settings_free(known_freqs);
known_freqs = NULL; known_freqs = NULL;
} }
l_free(known_freq_file_path);
return known_freqs; return known_freqs;
} }
void storage_known_frequencies_sync(struct l_settings *known_freqs) void storage_known_frequencies_sync(struct l_settings *known_freqs)
{ {
char *known_freq_file_path;
char *data; char *data;
size_t len; size_t len;
if (!known_freqs) if (!known_freqs)
return; return;
known_freq_file_path = storage_get_path("/%", KNOWN_FREQ_FILENAME);
data = l_settings_to_data(known_freqs, &len); data = l_settings_to_data(known_freqs, &len);
write_file(data, len, "%s", known_freq_file_path); write_file(data, len, "%s", known_freq_file_path);
l_free(data); l_free(data);
l_free(known_freq_file_path);
} }

View File

@ -32,6 +32,9 @@ ssize_t write_file(const void *buffer, size_t len, const char *path_fmt, ...)
__attribute__((format(printf, 3, 4))); __attribute__((format(printf, 3, 4)));
bool storage_create_dirs(void); bool storage_create_dirs(void);
void storage_cleanup_dirs(void);
char *storage_get_path(const char *format, ...);
char *storage_get_hotspot_path(const char *format, ...);
const char *storage_network_ssid_from_path(const char *path, const char *storage_network_ssid_from_path(const char *path,
enum security *type); enum security *type);