Check if file exists during runtime #562.
authorDaniel Carl <danielcarl@gmx.de>
Mon, 3 Jun 2019 21:18:04 +0000 (23:18 +0200)
committerDaniel Carl <danielcarl@gmx.de>
Mon, 3 Jun 2019 22:25:37 +0000 (00:25 +0200)
Do not consider file to be existent which was on startup. This allows
also to benenfit from files created by other vimb instances spawned
later.

src/file-storage.c
tests/test-file-storage.c

index 925f669..a924482 100644 (file)
@@ -54,18 +54,12 @@ FileStorage *file_storage_new(const char *dir, const char *filename, int mode)
 
     /* 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) {
@@ -81,9 +75,7 @@ FileStorage *file_storage_new(const char *dir, const char *filename, int mode)
 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);
         }
@@ -111,7 +103,7 @@ gboolean file_storage_append(FileStorage *storage, const char *format, ...)
         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);
@@ -135,9 +127,7 @@ char **file_storage_get_lines(FileStorage *storage)
     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) {
index 28859b2..9cea872 100644 (file)
@@ -84,13 +84,18 @@ static void test_ephemeral_with_file(void)
     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);