/* Built the full path out of dir and given file name. */
fullpath = g_build_filename(dir, filename, NULL);
- if (g_file_test(fullpath, G_FILE_TEST_IS_REGULAR)) {
- storage->file_path = fullpath;
- } else if (mode) {
+ if (!g_file_test(fullpath, G_FILE_TEST_IS_REGULAR) && mode) {
/* If create option was given - create the file. */
fclose(fopen(fullpath, "a"));
-
- storage->file_path = fullpath;
g_chmod(fullpath, mode);
- } else {
- storage->file_path = NULL;
- g_free(fullpath);
}
+ storage->file_path = fullpath;
/* Use gstring as storage in case when the file is used read only. */
if (storage->readonly) {
void file_storage_free(FileStorage *storage)
{
if (storage) {
- if (storage->file_path) {
- g_free(storage->file_path);
- }
+ g_free(storage->file_path);
if (storage->str) {
g_string_free(storage->str, TRUE);
}
va_end(args);
return TRUE;
}
- if (storage->file_path && (f = fopen(storage->file_path, "a+"))) {
+ if ((f = fopen(storage->file_path, "a+"))) {
flock(fileno(f), LOCK_EX);
va_start(args, format);
vfprintf(f, format, args);
char *content = NULL;
char **lines = NULL;
- if (storage->file_path) {
- content = util_get_file_contents(storage->file_path, NULL);
- }
+ g_file_get_contents(storage->file_path, &content, NULL, NULL);
if (storage->str && storage->str->len) {
if (content) {
gboolean result;
file_path = g_build_filename(pwd, existing_file, NULL);
- result = g_file_set_contents(file_path, "one\n", -1, NULL);
- g_assert_true(result);
s = file_storage_new(pwd, existing_file, 0);
g_assert_nonnull(s);
+ /* file does not exists yet */
+ lines = file_storage_get_lines(s);
+ g_assert_cmpint(g_strv_length(lines), ==, 0);
+ g_strfreev(lines);
+
/* empty file storage but file with two lines */
+ result = g_file_set_contents(file_path, "one\n", -1, NULL);
+ g_assert_true(result);
lines = file_storage_get_lines(s);
g_assert_cmpint(g_strv_length(lines), ==, 2);
g_strfreev(lines);