Add wait_for_gzip to avoid reading from a file that is still being

written to by a child process, a race condition at the end of the xml
import druid.


git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@14235 57a11ea4-9604-0410-9ed3-97b8803252fd
This commit is contained in:
Andreas Köhler 2006-05-29 07:08:28 +00:00
parent de37603ecc
commit 2bb9dc0c8f
2 changed files with 34 additions and 0 deletions
ChangeLog
src/backend/file

View File

@ -1,3 +1,9 @@
2006-05-29 Andreas Köhler <andi5.py@gmx.net>
* src/backend/file/io-gncxml-v2.c: Add wait_for_gzip to avoid
reading from a file that is still being written to by a child
process, a race condition at the end of the xml import druid.
2006-05-28 David Hampton <hampton@employees.org>
* src/pixmaps/gnucash_splash.png: New splash screen from Joshua

View File

@ -28,6 +28,7 @@
#include <unistd.h>
#include <zlib.h>
#include <errno.h>
#include <sys/wait.h>
#include "gnc-engine.h"
#include "gnc-pricedb-p.h"
@ -51,6 +52,8 @@
static QofLogModule log_module = GNC_MOD_IO;
static pid_t gzip_child_pid = 0;
/* Callback structure */
struct file_backend {
gboolean ok;
@ -1282,6 +1285,10 @@ try_gz_open (const char *filename, const char *perms, gboolean use_gzip,
int filedes[2];
pid_t pid;
/* avoid reading from file that is still being written to
by a child process */
g_assert(gzip_child_pid == 0);
if (pipe(filedes) < 0) {
PWARN("Pipe call failed. Opening uncompressed file.");
return fopen(filename, perms);
@ -1320,6 +1327,10 @@ try_gz_open (const char *filename, const char *perms, gboolean use_gzip,
}
default: /* parent */
if (compress) {
/* the calling code must wait_for_gzip() */
gzip_child_pid = pid;
}
sleep(2);
if (compress) {
close(filedes[0]);
@ -1335,6 +1346,20 @@ try_gz_open (const char *filename, const char *perms, gboolean use_gzip,
#endif
}
static gboolean
wait_for_gzip()
{
pid_t retval;
if (gzip_child_pid == 0)
return TRUE;
retval = waitpid(gzip_child_pid, NULL, WUNTRACED);
gzip_child_pid = 0;
return retval != -1;
}
gboolean
gnc_book_write_to_xml_file_v2(
QofBook *book,
@ -1358,6 +1383,9 @@ gnc_book_write_to_xml_file_v2(
return FALSE;
}
if (compress)
return wait_for_gzip();
return TRUE;
}