r19250 - gnucash/trunk/src - Bug #615347 - Use enforced, consistent data file extension across supported platforms

Geert Janssens gjanssens at code.gnucash.org
Sat Jun 12 05:23:51 EDT 2010


Author: gjanssens
Date: 2010-06-12 05:23:51 -0400 (Sat, 12 Jun 2010)
New Revision: 19250
Trac: http://svn.gnucash.org/trac/changeset/19250

Modified:
   gnucash/trunk/src/backend/xml/gnc-backend-xml.c
   gnucash/trunk/src/core-utils/gnc-uri-utils.c
   gnucash/trunk/src/core-utils/gnc-uri-utils.h
   gnucash/trunk/src/gnome-utils/gnc-file.c
Log:
Bug #615347 - Use enforced, consistent data file extension across supported platforms

The choice has fallen on ".gnucash". This extension will be added to all filenames that don't have it already when a user chooses "Save As...". Obviously this is only done for files, not for database storage. The backup files will from now on also end in ".gnucash" instead of the previous ".xac". The code that removes old backup files scans for both extensions to ensure that backup files still available with the ".xac" extension are rotated as well.

Modified: gnucash/trunk/src/backend/xml/gnc-backend-xml.c
===================================================================
--- gnucash/trunk/src/backend/xml/gnc-backend-xml.c	2010-06-10 13:05:12 UTC (rev 19249)
+++ gnucash/trunk/src/backend/xml/gnc-backend-xml.c	2010-06-12 09:23:51 UTC (rev 19250)
@@ -374,7 +374,7 @@
 
    If make_backup is true, write out a time-stamped copy of the file
    into the same directory as the indicated file, with a filename of
-   "file.YYYYMMDDHHMMSS.xac" where YYYYMMDDHHMMSS is replaced with the
+   "file.YYYYMMDDHHMMSS.gnucash" where YYYYMMDDHHMMSS is replaced with the
    current year/month/day/hour/minute/second. */
 
 /* The variable buf_size must be a compile-time constant */
@@ -593,11 +593,7 @@
     }
 
     timestamp = xaccDateUtilGetStampNow ();
-    backup = g_new (char, strlen (datafile) + strlen (timestamp) + 6);
-    strcpy (backup, datafile);
-    strcat (backup, ".");
-    strcat (backup, timestamp);
-    strcat (backup, ".xac");
+    backup = g_strconcat( datafile, ".", timestamp, GNC_DATAFILE_EXT, NULL );
     g_free (timestamp);
 
     bkup_ret = gnc_int_link_or_make_backup(be, datafile, backup);
@@ -759,12 +755,17 @@
 
 /* ================================================================= */
 
+/* Determine whether the path refers to a gnucash created file
+ * We can filter this based on the file's extension.
+ * Currently this can be .gnucash, .log, .LNK or .xac
+ */
 static int
 gnc_xml_be_select_files (const gchar *d)
 {
     return (g_str_has_suffix(d, ".LNK") ||
-            g_str_has_suffix(d, ".xac") ||
-            g_str_has_suffix(d, ".log"));
+            g_str_has_suffix(d, ".xac") == 0 /* old datafile extension */ ||
+            g_str_has_suffix(d, GNC_DATAFILE_EXT) ||
+            g_str_has_suffix(d, GNC_LOGFILE_EXT));
 }
 
 static void
@@ -808,6 +809,7 @@
         char *name;
         int len;
 
+        /* Ensure we only evaluate gnucuash related files. */
         if (gnc_xml_be_select_files (dent) == 0)
             continue;
 
@@ -845,8 +847,6 @@
 
                 if (res
                         && res != name + pathlen + 1
-                        && (strcmp(res, ".xac") == 0
-                            || strcmp(res, ".log") == 0)
                         && file_time > 0
                         && days > be->file_retention_days)
                 {

Modified: gnucash/trunk/src/core-utils/gnc-uri-utils.c
===================================================================
--- gnucash/trunk/src/core-utils/gnc-uri-utils.c	2010-06-10 13:05:12 UTC (rev 19249)
+++ gnucash/trunk/src/core-utils/gnc-uri-utils.c	2010-06-12 09:23:51 UTC (rev 19250)
@@ -98,7 +98,7 @@
     *password = NULL;
     *path     = NULL;
 
-    g_return_if_fail( uri != 0 );
+    g_return_if_fail( uri != NULL );
 
     splituri = g_strsplit ( uri, "://", 2 );
     if ( splituri[1] == NULL )
@@ -324,3 +324,21 @@
 
     return newuri;
 }
+
+gchar *gnc_uri_add_extension ( const gchar *uri, const gchar *extension )
+{
+    g_return_val_if_fail( uri != 0, NULL );
+
+    /* Only add extension if the user provided the extension and the uri is
+     * file based.
+     */
+    if ( !extension || !gnc_uri_is_file_uri( uri ) )
+        return g_strdup( uri );
+
+    /* Don't add extension if it's already there */
+    if ( g_str_has_suffix( uri, extension ) )
+        return g_strdup( uri );
+
+    /* Ok, all tests passed, let's add the extension */
+    return g_strconcat( uri, extension, NULL );
+}

Modified: gnucash/trunk/src/core-utils/gnc-uri-utils.h
===================================================================
--- gnucash/trunk/src/core-utils/gnc-uri-utils.h	2010-06-10 13:05:12 UTC (rev 19249)
+++ gnucash/trunk/src/core-utils/gnc-uri-utils.h	2010-06-12 09:23:51 UTC (rev 19250)
@@ -39,6 +39,9 @@
 #ifndef GNCURIUTILS_H_
 #define GNCURIUTILS_H_
 
+#define GNC_DATAFILE_EXT ".gnucash"
+#define GNC_LOGFILE_EXT  ".log"
+
 /** Converts a uri in separate components.
  *
  *  Uri's can take any of the following forms:
@@ -207,6 +210,21 @@
  */
 gboolean gnc_uri_is_file_uri (const gchar *uri);
 
+/** Adds an extension to the uri if:
+ *  * the uri is not empty and file based
+ *  * doesn't already have the extension
+ *
+ *  @param uri The uri to process
+ *  @param extension The extension to add if missing. Note that the extension
+ *                   is added verbatim, so if a dot should be added, this
+ *                   should be part of the extension.
+ *
+ *  @return The uri, but garanteed to end with extension if the uri is file
+ *          based. Otherwise the uri is returned unmodified. Note that the
+ *          returned value should be freed with g_free when no longer needed.
+ */
+gchar *gnc_uri_add_extension ( const gchar *uri, const gchar *extension );
+
 #endif /* GNCURIUTILS_H_ */
 /** @} */
 /** @} */

Modified: gnucash/trunk/src/gnome-utils/gnc-file.c
===================================================================
--- gnucash/trunk/src/gnome-utils/gnc-file.c	2010-06-10 13:05:12 UTC (rev 19249)
+++ gnucash/trunk/src/gnome-utils/gnc-file.c	2010-06-12 09:23:51 UTC (rev 19250)
@@ -1080,6 +1080,7 @@
     QofSession *session;
     char *default_dir = NULL;        /* Default to last open */
     char *last;
+    char *norm_file;
     char *newfile;
     const char *oldfile;
     gchar *logpath = NULL;
@@ -1098,14 +1099,16 @@
 
     /* Convert user input into a normalized uri
      * Note that the normalized uri for internal use can have a password */
-    newfile = gnc_uri_normalize_uri ( filename, TRUE );
-    if (!newfile)
+    norm_file = gnc_uri_normalize_uri ( filename, TRUE );
+    if (!norm_file)
     {
         show_session_error (ERR_FILEIO_FILE_NOT_FOUND, filename,
                             GNC_FILE_DIALOG_SAVE);
         return;
     }
 
+    newfile = gnc_uri_add_extension (norm_file, GNC_DATAFILE_EXT);
+    g_free (norm_file);
     gnc_uri_get_components (newfile, &protocol, &hostname,
                             &port, &username, &password, &path);
 



More information about the gnucash-changes mailing list