From 0ad1fb11651bc875ad13c9def8a0436305f3b834 Mon Sep 17 00:00:00 2001 From: "Dirk-Jan C. Binnema" Date: Wed, 1 Sep 2010 23:35:34 +0300 Subject: [PATCH] * update mu extract implementation, support --overwrite and --targetdir (WIP, needs testing) --- src/mu-cmd-extract.c | 20 ++++++++++++++------ src/mu-config.c | 2 ++ src/mu-config.h | 1 + src/mu-msg-part.c | 8 ++++---- 4 files changed, 21 insertions(+), 10 deletions(-) diff --git a/src/mu-cmd-extract.c b/src/mu-cmd-extract.c index 74417668..18838c0e 100644 --- a/src/mu-cmd-extract.c +++ b/src/mu-cmd-extract.c @@ -48,8 +48,8 @@ save_numbered_parts (MuMsg *msg, MuConfigOptions *opts) break; } - /* FIXME: targetdir, overwrite */ - if (!mu_msg_mime_part_save (msg, idx, NULL, TRUE)) { + if (!mu_msg_mime_part_save + (msg, idx, opts->targetdir, opts->overwrite)) { g_warning ("failed to save MIME-part %d", idx); rv = FALSE; break; @@ -66,6 +66,8 @@ struct _SaveData { gboolean attachments_only; gboolean result; guint saved_num; + const gchar* targetdir; + gboolean overwrite; }; typedef struct _SaveData SaveData; @@ -89,7 +91,8 @@ save_part_if (MuMsgPart *part, SaveData *sd) g_ascii_strcasecmp (part->type, "multipart") == 0) return; - sd->result = mu_msg_mime_part_save (sd->msg, part->index, NULL, TRUE); + sd->result = mu_msg_mime_part_save (sd->msg, part->index, + sd->targetdir, sd->overwrite); if (!sd->result) g_warning ("failed to save MIME-part %u", part->index); else @@ -98,7 +101,8 @@ save_part_if (MuMsgPart *part, SaveData *sd) } static gboolean -save_certain_parts (MuMsg *msg, gboolean attachments_only) +save_certain_parts (MuMsg *msg, gboolean attachments_only, const gchar *targetdir, + gboolean overwrite) { SaveData sd; @@ -106,6 +110,8 @@ save_certain_parts (MuMsg *msg, gboolean attachments_only) sd.result = TRUE; sd.saved_num = 0; sd.attachments_only = attachments_only; + sd.overwrite = overwrite; + sd.targetdir = targetdir; mu_msg_msg_part_foreach (msg, (MuMsgPartForeachFunc)save_part_if, @@ -138,9 +144,11 @@ save_parts (const char *path, MuConfigOptions *opts) if (opts->parts) rv = save_numbered_parts (msg, opts); else if (opts->save_attachments) /* all attachments */ - rv = save_certain_parts (msg, TRUE); + rv = save_certain_parts (msg, TRUE, + opts->targetdir, opts->overwrite); else if (opts->save_all) /* all parts */ - rv = save_certain_parts (msg, FALSE); + rv = save_certain_parts (msg, FALSE, + opts->targetdir, opts->overwrite); else g_assert_not_reached (); diff --git a/src/mu-config.c b/src/mu-config.c index 7a415003..556e9394 100644 --- a/src/mu-config.c +++ b/src/mu-config.c @@ -218,6 +218,8 @@ config_options_group_extract (MuConfigOptions *opts) "save specific parts", NULL}, {"target-dir", 0, 0, G_OPTION_ARG_FILENAME, &opts->targetdir, "target directory for saving", NULL}, + {"overwrite", 0, 0, G_OPTION_ARG_NONE, &opts->overwrite, + "overwrite existing files", NULL}, { NULL, 0, 0, 0, NULL, NULL, NULL } }; diff --git a/src/mu-config.h b/src/mu-config.h index b7fb6100..77b0f6f8 100644 --- a/src/mu-config.h +++ b/src/mu-config.h @@ -65,6 +65,7 @@ struct _MuConfigOptions { gboolean *save_attachments; /* extract all attachment parts */ gchar *parts; /* comma-sep'd list of parts to save */ char *targetdir; /* where to save the attachments */ + gboolean overwrite; /* should we overwrite same-named files */ }; typedef struct _MuConfigOptions MuConfigOptions; diff --git a/src/mu-msg-part.c b/src/mu-msg-part.c index 7ff11380..2b39c545 100644 --- a/src/mu-msg-part.c +++ b/src/mu-msg-part.c @@ -100,7 +100,7 @@ save_part (GMimeObject *part, const char *filename, int fd, rv; GMimeDataWrapper *wrapper; GMimeStream *stream; - + rv = TRUE; fd = mu_util_create_writeable_fd (filename, targetdir, overwrite); @@ -110,7 +110,7 @@ save_part (GMimeObject *part, const char *filename, errno != 0 ? strerror(errno) : ""); return FALSE; } - + stream = g_mime_stream_fs_new (fd); if (!stream) { g_critical ("%s: failed to create stream", __FUNCTION__); @@ -119,7 +119,7 @@ save_part (GMimeObject *part, const char *filename, } g_mime_stream_fs_set_owner (GMIME_STREAM_FS(stream), - TRUE); /* GMimeStream will close fd */ + TRUE); /* GMimeStream will close the fd */ wrapper = g_mime_part_get_content_object (GMIME_PART(part)); if (!wrapper) { @@ -180,7 +180,7 @@ mu_msg_mime_part_save (MuMsg *msg, int wanted_idx, spd.wanted_idx = wanted_idx; spd.targetdir = targetdir; spd.overwrite = overwrite; - spd.stream = FALSE; + spd.stream = FALSE; /* not used yet */ spd.result = FALSE; /* get something fairly unique for building a filename;