mirror of
https://git.pwmt.org/pwmt/zathura.git
synced 2025-01-16 20:07:52 +01:00
database-plain: Lock file when reading
This commit is contained in:
parent
b0007bf2de
commit
554e6eb70c
1 changed files with 27 additions and 5 deletions
|
@ -295,25 +295,47 @@ zathura_db_read_key_file_from_file(char* path)
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* open file */
|
||||||
|
int fd = open(path, O_RDWR);
|
||||||
|
if (fd == -1) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
GKeyFile* key_file = g_key_file_new();
|
GKeyFile* key_file = g_key_file_new();
|
||||||
if (key_file == NULL) {
|
if (key_file == NULL) {
|
||||||
|
close(fd);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* read config file */
|
||||||
|
file_lock_set(fd, F_WRLCK);
|
||||||
|
char* content = girara_file_read_from_fd(fd);
|
||||||
|
if (content == NULL) {
|
||||||
|
file_lock_set(fd, F_UNLCK);
|
||||||
|
close(fd);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
file_lock_set(fd, F_UNLCK);
|
||||||
|
|
||||||
|
close(fd);
|
||||||
|
|
||||||
|
/* parse config file */
|
||||||
GError* error = NULL;
|
GError* error = NULL;
|
||||||
if (g_key_file_load_from_file(key_file, path,
|
if (g_key_file_load_from_data(key_file, content, strlen(content),
|
||||||
G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS, &error) ==
|
G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS, &error) ==
|
||||||
FALSE) {
|
FALSE) {
|
||||||
|
|
||||||
if (error->code != 1) /* ignore empty file */ {
|
if (error->code != 1) /* ignore empty file */ {
|
||||||
|
free(content);
|
||||||
g_key_file_free(key_file);
|
g_key_file_free(key_file);
|
||||||
return NULL;
|
|
||||||
g_error_free(error);
|
g_error_free(error);
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
g_error_free(error);
|
g_error_free(error);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
free(content);
|
||||||
|
|
||||||
return key_file;
|
return key_file;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue