diff --git a/ChangeLog b/ChangeLog index 4b7600278d..5086093c11 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,15 @@ +2003-01-04 David Hampton + + * src/backend/file/io-gncxml-v2.c: Pipe the output through zlib if + the user requested compression. This request is either setting a + option in the preferences (for all files), or naming the + particular data file with a .gz extension. #88509 + + * src/backend/file/gnc-backend-file.c: + * src/gnome/top-level.c: + * src/app-utils/prefs.scm: Create a 'file compression' option and + pass the data into the backend. + 2003-01-04 Derek Atkkins * add a set of business accounts to the setup druid diff --git a/src/app-utils/prefs.scm b/src/app-utils/prefs.scm index f74b46f743..380c595122 100644 --- a/src/app-utils/prefs.scm +++ b/src/app-utils/prefs.scm @@ -511,6 +511,12 @@ without one.") (N_ "General") (N_ "No account list setup on new file") "j" (N_ "Don't popup the new account list dialog when you choose \"New File\" from the \"File\" menu") #f)) +(gnc:register-configuration-option + (gnc:make-simple-boolean-option + (N_ "General") (N_ "Use file compression") + "k" (N_ "Compress the data file.") + #f)) + (gnc:register-configuration-option (gnc:make-number-range-option (N_ "General") (N_ "Days to retain log files") diff --git a/src/backend/file/gnc-backend-file.c b/src/backend/file/gnc-backend-file.c index 6777439f02..98f73db5bd 100644 --- a/src/backend/file/gnc-backend-file.c +++ b/src/backend/file/gnc-backend-file.c @@ -60,6 +60,7 @@ typedef enum } GNCBookFileType; static int file_retention_days = 0; +static gboolean file_compression = FALSE; static void gnc_file_be_load_from_file(Backend *, GNCBook *); @@ -78,10 +79,10 @@ gnc_file_be_set_retention_days (int days) file_retention_days = days; } -int -gnc_file_be_get_retention_days (void) +void +gnc_file_be_set_compression (gboolean compress) { - return file_retention_days; + file_compression = compress; } static void @@ -657,7 +658,7 @@ gnc_file_be_write_to_file(FileBackend *be, gboolean make_backup) } } - if(gnc_book_write_to_xml_file_v2(book, tmp_name)) + if(gnc_book_write_to_xml_file_v2(book, tmp_name, file_compression)) { /* Record the file's permissions before unlinking it */ rc = stat(datafile, &statbuf); diff --git a/src/backend/file/io-gncxml-v2.c b/src/backend/file/io-gncxml-v2.c index 482f169c5b..857a8e6f03 100644 --- a/src/backend/file/io-gncxml-v2.c +++ b/src/backend/file/io-gncxml-v2.c @@ -24,6 +24,8 @@ #include #include #include +#include +#include #include "gnc-book-p.h" #include "gnc-engine.h" @@ -1165,15 +1167,69 @@ gnc_book_write_accounts_to_xml_filehandle_v2(Backend *be, GNCBook *book, FILE *o return TRUE; } +#define BUFLEN 4096 + +static FILE * +try_gz_open (const char *filename, const char *perms, gboolean use_gzip) +{ + char buffer[BUFLEN]; + unsigned bytes; + int filedes[2]; + gzFile *out; + pid_t pid; + + if (strstr(filename, ".gz.") != NULL) /* its got a temp extension */ + use_gzip = TRUE; + + if (!use_gzip) + return fopen(filename, perms); + +//gboolean +//gnc_lookup_boolean_option( const char *section, +// const char *name, +// gboolean default_value) +// + + if (pipe(filedes) < 0) { + PWARN("Pipe call failed. Opening uncompressed file."); + return fopen(filename, perms); + } + + pid = fork(); + switch (pid) { + case -1: + PWARN("Fork call failed. Opening uncompressed file."); + return fopen(filename, perms); + + case 0: /* child */ + close(filedes[1]); + out = gzopen(filename, perms); + if (out == NULL) { + PWARN("child gzopen failed\n"); + exit(0); + } + while ((bytes = read(filedes[0], buffer, BUFLEN)) > 0) + gzwrite(out, buffer, bytes); + gzclose(out); + _exit(0); + + default: /* parent */ + sleep(2); + close(filedes[0]); + return fdopen(filedes[1], "w"); + } +} + gboolean gnc_book_write_to_xml_file_v2( GNCBook *book, - const char *filename) + const char *filename, + gboolean compress) { FILE *out; - out = fopen(filename, "w"); - if (out == NULL) + out = try_gz_open(filename, "w", compress); + if (out == NULL) { return FALSE; } diff --git a/src/backend/file/io-gncxml-v2.h b/src/backend/file/io-gncxml-v2.h index 4387706f93..3f2c05ae2a 100644 --- a/src/backend/file/io-gncxml-v2.h +++ b/src/backend/file/io-gncxml-v2.h @@ -126,7 +126,7 @@ gboolean gnc_session_load_from_xml_file_v2(GNCSession *session); /* write all book info to a file */ gboolean gnc_book_write_to_xml_filehandle_v2(GNCBook *book, FILE *fh); -gboolean gnc_book_write_to_xml_file_v2(GNCBook *book, const char *filename); +gboolean gnc_book_write_to_xml_file_v2(GNCBook *book, const char *filename, gboolean compress); /* write just the commodities and accounts to a file */ gboolean gnc_book_write_accounts_to_xml_filehandle_v2(Backend *be, GNCBook *book, FILE *fh); diff --git a/src/backend/gnc-backend-api.h b/src/backend/gnc-backend-api.h index c800bce704..c70a63e9eb 100644 --- a/src/backend/gnc-backend-api.h +++ b/src/backend/gnc-backend-api.h @@ -28,6 +28,6 @@ Backend * gnc_backend_new (void); void gnc_file_be_set_retention_days (int days); -int gnc_file_be_get_retention_days (void); +void gnc_file_be_set_compression (gboolean compress); #endif diff --git a/src/gnome/top-level.c b/src/gnome/top-level.c index 1a1b92b23a..edaeec7941 100644 --- a/src/gnome/top-level.c +++ b/src/gnome/top-level.c @@ -86,6 +86,8 @@ static void gnc_configure_auto_decimal_places_cb(gpointer); static void gnc_configure_auto_decimal_places(void); static void gnc_configure_file_be_retention_days_cb(gpointer); static void gnc_configure_file_be_retention_days(void); +static void gnc_configure_file_be_compression_cb(gpointer); +static void gnc_configure_file_be_compression(void); static void gnc_configure_register_font_cb(gpointer); static void gnc_configure_register_font(void); static void gnc_configure_register_hint_font_cb(gpointer); @@ -110,6 +112,7 @@ static SCM negative_color_callback_id = SCM_UNDEFINED; static SCM auto_decimal_callback_id = SCM_UNDEFINED; static SCM auto_decimal_places_callback_id = SCM_UNDEFINED; static SCM log_retention_days_callback_id = SCM_UNDEFINED; +static SCM compression_callback_id = SCM_UNDEFINED; static SCM register_font_callback_id = SCM_UNDEFINED; static SCM register_hint_font_callback_id = SCM_UNDEFINED; @@ -376,6 +379,12 @@ gnc_gui_init (SCM command_line) NULL, "General", "Days to retain log files"); + gnc_configure_file_be_compression(); + compression_callback_id = + gnc_register_option_change_callback(gnc_configure_file_be_compression_cb, + NULL, "General", + "Use file compression"); + gnc_configure_register_font(); register_font_callback_id = gnc_register_option_change_callback(gnc_configure_register_font_cb, @@ -850,7 +859,7 @@ gnc_configure_auto_decimal_places (void) /* gnc_configure_file_be_retention_days_cb * Callback called when options change - - * sets auto decimal places option. + * sets days retained for the file backend. * * Args: Nothing * Returns: Nothing @@ -862,7 +871,7 @@ gnc_configure_file_be_retention_days_cb (gpointer not_used) } /* gnc_configure_file_be_retention_days - * Pass the global value for the auto decimal places range to the engine. + * Pass the global value for the number of days to retain files to the file backend. * * Args: Nothing * Returns: Nothing @@ -873,9 +882,33 @@ gnc_configure_file_be_retention_days (void) gnc_file_be_set_retention_days (gnc_lookup_number_option("General", "Days to retain log files", 0)); - } +/* gnc_configure_file_be_retention_days_cb + * Callback called when options change - + * sets days retained for the file backend. + * + * Args: Nothing + * Returns: Nothing + */ +static void +gnc_configure_file_be_compression_cb (gpointer not_used) +{ + gnc_configure_file_be_compression (); +} + +/* gnc_configure_file_be_retention_days + * Pass the global value for the number of days to retain files to the file backend. + * + * Args: Nothing + * Returns: Nothing + */ +static void +gnc_configure_file_be_compression (void) +{ + gnc_file_be_set_compression + (gnc_lookup_boolean_option("General", "Use file compression", FALSE)); +} /* gnc_configure_register_font_cb * Callback called when options change -