[xml backend] Extract functions to make gz_trhead_func more readable.

This commit is contained in:
John Ralls 2022-03-01 12:08:59 -08:00
parent 25181a6c01
commit 67e8c317da

View File

@ -1387,66 +1387,54 @@ gnc_book_write_accounts_to_xml_filehandle_v2 (QofBackend* qof_be, QofBook* book,
return success; return success;
} }
#define BUFLEN 4096
/* Compress or decompress function that is to be run in a separate thread.
* Returns 1 on success or 0 otherwise, stuffed into a pointer type. */
static gpointer
gz_thread_func (gz_thread_params_t* params)
{
gchar buffer[BUFLEN];
gssize bytes;
gint gzval;
gzFile file;
gint success = 1;
#ifdef G_OS_WIN32 #ifdef G_OS_WIN32
{ static inline gzFile
gchar* conv_name = g_win32_locale_filename_from_utf8 (params->filename); gzopen_win32 (const char* filename, const char* perms)
gchar* perms; {
gzFile file;
char* new_perms = nullptr;
char* conv_name = g_win32_locale_filename_from_utf8 (filename);
if (!conv_name) if (!conv_name)
{ {
g_warning ("Could not convert '%s' to system codepage", g_warning ("Could not convert '%s' to system codepage",
params->filename); filename);
success = 0; success = 0;
goto cleanup_gz_thread_func; return nullptr;
} }
if (strchr (params->perms, 'b')) if (strchr (perms, 'b'))
perms = g_strdup (params->perms); new_perms = g_strdup (perms);
else else
perms = g_strdup_printf ("%cb%s", *params->perms, params->perms + 1); new_perms = g_strdup_printf ("%cb%s", *perms, perms + 1);
file = gzopen (conv_name, perms); file = gzopen (conv_name, new_perms);
g_free (perms); g_free (new_perms);
g_free (conv_name); g_free (conv_name);
} return file;
#else /* !G_OS_WIN32 */ }
file = gzopen (params->filename, params->perms); #endif
#endif /* G_OS_WIN32 */
if (file == NULL) constexpr uint32_t BUFLEN{4096};
{
g_warning ("Child threads gzopen failed"); static inline bool
success = 0; gz_thread_write (gzFile file, gz_thread_params_t* params)
goto cleanup_gz_thread_func; {
} bool success = true;
gchar buffer[BUFLEN];
if (params->write)
{
while (success) while (success)
{ {
bytes = read (params->fd, buffer, BUFLEN); auto bytes = read (params->fd, buffer, BUFLEN);
if (bytes > 0) if (bytes > 0)
{ {
if (gzwrite (file, buffer, bytes) <= 0) if (gzwrite (file, buffer, bytes) <= 0)
{ {
gint errnum; gint errnum;
const gchar* error = gzerror (file, &errnum); auto error = gzerror (file, &errnum);
g_warning ("Could not write the compressed file '%s'. The error is: '%s' (%d)", g_warning ("Could not write the compressed file '%s'. The error is: '%s' (%d)",
params->filename, error, errnum); params->filename, error, errnum);
success = 0; success = false;
} }
} }
else if (bytes == 0) else if (bytes == 0)
@ -1458,28 +1446,34 @@ gz_thread_func (gz_thread_params_t* params)
{ {
g_warning ("Could not read from pipe. The error is '%s' (errno %d)", g_warning ("Could not read from pipe. The error is '%s' (errno %d)",
g_strerror (errno) ? g_strerror (errno) : "", errno); g_strerror (errno) ? g_strerror (errno) : "", errno);
success = 0; success = false;
} }
} }
} return success;
else }
{
#if COMPILER(MSVC)
#define WRITE_FN _write
#else
#define WRITE_FN write
#endif
static inline bool
gz_thread_read (gzFile file, gz_thread_params_t* params)
{
bool success = true;
gchar buffer[BUFLEN];
while (success) while (success)
{ {
gzval = gzread (file, buffer, BUFLEN); auto gzval = gzread (file, buffer, BUFLEN);
if (gzval > 0) if (gzval > 0)
{ {
if ( if (WRITE_FN (params->fd, buffer, gzval) < 0)
#if COMPILER(MSVC)
_write
#else
write
#endif
(params->fd, buffer, gzval) < 0)
{ {
g_warning ("Could not write to pipe. The error is '%s' (%d)", g_warning ("Could not write to pipe. The error is '%s' (%d)",
g_strerror (errno) ? g_strerror (errno) : "", errno); g_strerror (errno) ? g_strerror (errno) : "", errno);
success = 0; success = false;
} }
} }
else if (gzval == 0) else if (gzval == 0)
@ -1492,16 +1486,47 @@ gz_thread_func (gz_thread_params_t* params)
const gchar* error = gzerror (file, &errnum); const gchar* error = gzerror (file, &errnum);
g_warning ("Could not read from compressed file '%s'. The error is: '%s' (%d)", g_warning ("Could not read from compressed file '%s'. The error is: '%s' (%d)",
params->filename, error, errnum); params->filename, error, errnum);
success = false;
}
}
return success;
}
/* Compress or decompress function that is to be run in a separate thread.
* Returns 1 on success or 0 otherwise, stuffed into a pointer type. */
static gpointer
gz_thread_func (gz_thread_params_t* params)
{
gint gzval;
bool success = true;
#ifdef G_OS_WIN32
auto file = gzopen_win32 (params->filename, params->perms);
#else /* !G_OS_WIN32 */
auto file = gzopen (params->filename, params->perms);
#endif /* G_OS_WIN32 */
if (!file)
{
g_warning ("Child threads gzopen failed");
success = 0; success = 0;
goto cleanup_gz_thread_func;
} }
if (params->write)
{
success = gz_thread_write (file, params);
} }
else
{
success = gz_thread_read (file, params);
} }
if ((gzval = gzclose (file)) != Z_OK) if ((gzval = gzclose (file)) != Z_OK)
{ {
g_warning ("Could not close the compressed file '%s' (errnum %d)", g_warning ("Could not close the compressed file '%s' (errnum %d)",
params->filename, gzval); params->filename, gzval);
success = 0; success = false;
} }
cleanup_gz_thread_func: cleanup_gz_thread_func: