diff options
author | Christian Persch <chpe@src.gnome.org> | 2021-11-13 11:03:48 +0100 |
---|---|---|
committer | Christian Persch <chpe@src.gnome.org> | 2021-11-18 23:53:23 +0100 |
commit | 5914c1d6e8a8dddbb176f37552a4efb27445b909 (patch) | |
tree | b1d89200a2b1d6c757a8c134cc0a5a661d630768 /glib/poppler-attachment.cc | |
parent | c3f1ece62ac52587308e44d3e170d864372875f2 (diff) |
glib: Add APIs to save to file descriptor
Diffstat (limited to 'glib/poppler-attachment.cc')
-rw-r--r-- | glib/poppler-attachment.cc | 46 |
1 files changed, 45 insertions, 1 deletions
diff --git a/glib/poppler-attachment.cc b/glib/poppler-attachment.cc index 3195483b..b44edded 100644 --- a/glib/poppler-attachment.cc +++ b/glib/poppler-attachment.cc @@ -227,7 +227,8 @@ static gboolean save_helper(const gchar *buf, gsize count, gpointer data, GError n = fwrite(buf, 1, count, f); if (n != count) { - g_set_error(error, G_FILE_ERROR, g_file_error_from_errno(errno), _("Error writing to image file: %s"), g_strerror(errno)); + int errsv = errno; + g_set_error(error, G_FILE_ERROR, g_file_error_from_errno(errsv), _("Error writing to image file: %s"), g_strerror(errsv)); return FALSE; } @@ -274,6 +275,49 @@ gboolean poppler_attachment_save(PopplerAttachment *attachment, const char *file return result; } +/** + * poppler_attachment_save_to_fd: + * @attachment: A #PopplerAttachment. + * @fd: a valid file descriptor open for writing + * @error: (allow-none): return location for error, or %NULL. + * + * Saves @attachment to a file referred to by @fd. If @error is set, %FALSE + * will be returned. Possible errors include those in the #G_FILE_ERROR domain + * and whatever the save function generates. + * Note that this function takes ownership of @fd; you must not operate on it + * again, nor close it. + * + * Return value: %TRUE, if the file successfully saved + * + * Since: 21.12.0 + **/ +gboolean poppler_attachment_save_to_fd(PopplerAttachment *attachment, int fd, GError **error) +{ + gboolean result; + FILE *f; + + g_return_val_if_fail(POPPLER_IS_ATTACHMENT(attachment), FALSE); + g_return_val_if_fail(fd != -1, FALSE); + g_return_val_if_fail(error == nullptr || *error == nullptr, FALSE); + + f = fdopen(fd, "wb"); + if (f == nullptr) { + int errsv = errno; + g_set_error(error, G_FILE_ERROR, g_file_error_from_errno(errsv), _("Failed to open FD %d for writing: %s"), fd, g_strerror(errsv)); + return FALSE; + } + + result = poppler_attachment_save_to_callback(attachment, save_helper, f, error); + + if (fclose(f) < 0) { + int errsv = errno; + g_set_error(error, G_FILE_ERROR, g_file_error_from_errno(errsv), _("Failed to close FD %d, all data may not have been saved: %s"), fd, g_strerror(errsv)); + return FALSE; + } + + return result; +} + #define BUF_SIZE 1024 /** |