gnucash maint: Multiple changes pushed

John Ralls jralls at code.gnucash.org
Tue Sep 14 12:38:47 EDT 2021


Updated	 via  https://github.com/Gnucash/gnucash/commit/574f567b (commit)
	 via  https://github.com/Gnucash/gnucash/commit/d7eae066 (commit)
	from  https://github.com/Gnucash/gnucash/commit/8e4c4948 (commit)



commit 574f567b625d3a175ba153bb1c63a0d14e8dd75a
Author: John Ralls <jralls at ceridwen.us>
Date:   Tue Sep 14 09:32:12 2021 -0700

    Refactor GncXmlBackend::get_file_lock.
    
    Moving all of the error-handling to it for better clarity and
    simplicity. Drop the unnecessary stat call, open will fail with
    EEXIST if there's already a lock file. Provide specific messages
    for file system failures.

diff --git a/libgnucash/backend/xml/gnc-xml-backend.cpp b/libgnucash/backend/xml/gnc-xml-backend.cpp
index 8b8a3059f..5afe3fa0a 100644
--- a/libgnucash/backend/xml/gnc-xml-backend.cpp
+++ b/libgnucash/backend/xml/gnc-xml-backend.cpp
@@ -149,14 +149,7 @@ GncXmlBackend::session_begin(QofSession* session, const char* new_uri,
 
     /* Set the lock file */
     m_lockfile = m_fullpath + ".LCK";
-    auto locked = get_file_lock();
-    if (mode == SESSION_BREAK_LOCK && !locked)
-    {
-        // Don't pass on locked or readonly errors.
-        QofBackendError berror = get_error();
-        if (!(berror == ERR_BACKEND_LOCKED || berror == ERR_BACKEND_READONLY))
-            set_error(berror);
-    }
+    get_file_lock(mode);
 }
 
 void
@@ -626,50 +619,45 @@ GncXmlBackend::link_or_make_backup (const std::string& orig,
     return true;
 }
 
-bool
-GncXmlBackend::get_file_lock ()
+void
+GncXmlBackend::get_file_lock (SessionOpenMode mode)
 {
-    GStatBuf statbuf;
-#ifndef G_OS_WIN32
-    char* pathbuf = NULL, *tmpbuf = NULL;
-    size_t pathbuf_size = 0;
-#endif
-    QofBackendError be_err;
-
-    auto rc = g_stat (m_lockfile.c_str(), &statbuf);
-    if (!rc)
-    {
-        /* oops .. file is locked by another user  .. */
-        set_error(ERR_BACKEND_LOCKED);
-        m_lockfile.clear();
-        return false;
-    }
-
     m_lockfd = g_open (m_lockfile.c_str(), O_RDWR | O_CREAT | O_EXCL ,
-                         S_IRUSR | S_IWUSR);
+                       S_IRUSR | S_IWUSR);
     if (m_lockfd == -1)
     {
+        QofBackendError be_err{ERR_BACKEND_NO_ERR};
         /* oops .. we can't create the lockfile .. */
         switch (errno)
         {
         case EACCES:
+            set_message("Unable to create lockfile, make sure that you have write access to the directory.");
+            be_err = ERR_BACKEND_READONLY;
+            break;
+
         case EROFS:
+            set_message("Unable to create lockfile, data file is on a read-only filesystem.");
+            be_err = ERR_BACKEND_READONLY;
+            break;
         case ENOSPC:
+            set_message("Unable to create lockfile, no space on filesystem.");
             be_err = ERR_BACKEND_READONLY;
             break;
-        default:
+        case EEXIST:
             be_err = ERR_BACKEND_LOCKED;
             break;
-        }
-        if (errno != EEXIST) // Can't lock, but not because the file is locked
+        default: 
             PWARN ("Unable to create the lockfile %s: %s",
                    m_lockfile.c_str(), strerror(errno));
-        set_error(be_err);
-        m_lockfile.clear();
-        return false;
+            set_message("Lockfile creation failed. Please see the tracefile for details.");
+            be_err = ERR_FILEIO_FILE_LOCKERR;
+        }
+        if (!(mode == SESSION_BREAK_LOCK && be_err == ERR_BACKEND_LOCKED))
+        {
+            set_error(be_err);
+            m_lockfile.clear();
+        }
     }
-
-    return true;
 }
 
 bool
diff --git a/libgnucash/backend/xml/gnc-xml-backend.hpp b/libgnucash/backend/xml/gnc-xml-backend.hpp
index ab98a7b61..662ec8e41 100644
--- a/libgnucash/backend/xml/gnc-xml-backend.hpp
+++ b/libgnucash/backend/xml/gnc-xml-backend.hpp
@@ -49,7 +49,7 @@ public:
 
 private:
     bool save_may_clobber_data();
-    bool get_file_lock();
+    void get_file_lock(SessionOpenMode);
     bool link_or_make_backup(const std::string& orig, const std::string& bkup);
     bool backup_file();
     bool write_to_file(bool make_backup);

commit d7eae0664fb4cf7f3411c8d3b47cef32ad2346fc
Author: John Ralls <jralls at ceridwen.us>
Date:   Sun Sep 12 13:29:10 2021 -0700

    [gz_open] Use compress and write consistent with calling functions.

diff --git a/libgnucash/backend/xml/io-gncxml-v2.cpp b/libgnucash/backend/xml/io-gncxml-v2.cpp
index 955a49002..e26bc6269 100644
--- a/libgnucash/backend/xml/io-gncxml-v2.cpp
+++ b/libgnucash/backend/xml/io-gncxml-v2.cpp
@@ -95,7 +95,7 @@ typedef struct
     gint fd;
     gchar* filename;
     gchar* perms;
-    gboolean compress;
+    gboolean write;
 } gz_thread_params_t;
 
 /* Callback structure */
@@ -125,8 +125,8 @@ gnc_v2_book_version_string;        /* see gnc-book-xml-v2 */
 
 /* Forward declarations */
 static FILE* try_gz_open (const char* filename, const char* perms,
-                          gboolean use_gzip,
-                          gboolean compress);
+                          gboolean compress,
+                          gboolean write);
 static gboolean is_gzipped_file (const gchar* name);
 static gboolean wait_for_gzip (FILE* file);
 
@@ -1433,7 +1433,7 @@ gz_thread_func (gz_thread_params_t* params)
         goto cleanup_gz_thread_func;
     }
 
-    if (params->compress)
+    if (params->write)
     {
         while (success)
         {
@@ -1514,13 +1514,13 @@ cleanup_gz_thread_func:
 }
 
 static FILE*
-try_gz_open (const char* filename, const char* perms, gboolean use_gzip,
-             gboolean compress)
+try_gz_open (const char* filename, const char* perms, gboolean compress,
+             gboolean write)
 {
     if (strstr (filename, ".gz.") != NULL) /* its got a temp extension */
-        use_gzip = TRUE;
+        compress = TRUE;
 
-    if (!use_gzip)
+    if (!compress)
         return g_fopen (filename, perms);
 
     {
@@ -1554,10 +1554,10 @@ try_gz_open (const char* filename, const char* perms, gboolean use_gzip,
         }
 
         params = g_new (gz_thread_params_t, 1);
-        params->fd = filedes[compress ? 0 : 1];
+        params->fd = filedes[write ? 0 : 1];
         params->filename = g_strdup (filename);
         params->perms = g_strdup (perms);
-        params->compress = compress;
+        params->write = write;
 
         thread = g_thread_new ("xml_thread", (GThreadFunc) gz_thread_func,
                                params);
@@ -1573,7 +1573,7 @@ try_gz_open (const char* filename, const char* perms, gboolean use_gzip,
             return g_fopen (filename, perms);
         }
 
-        if (compress)
+        if (write)
             file = fdopen (filedes[1], "w");
         else
             file = fdopen (filedes[0], "r");



Summary of changes:
 libgnucash/backend/xml/gnc-xml-backend.cpp | 58 ++++++++++++------------------
 libgnucash/backend/xml/gnc-xml-backend.hpp |  2 +-
 libgnucash/backend/xml/io-gncxml-v2.cpp    | 22 ++++++------
 3 files changed, 35 insertions(+), 47 deletions(-)



More information about the gnucash-changes mailing list