* update mu extract implementation, support --overwrite and --targetdir (WIP,

needs testing)
This commit is contained in:
Dirk-Jan C. Binnema 2010-09-01 23:35:34 +03:00
parent 364620170c
commit 0ad1fb1165
4 changed files with 21 additions and 10 deletions

View File

@ -48,8 +48,8 @@ save_numbered_parts (MuMsg *msg, MuConfigOptions *opts)
break; break;
} }
/* FIXME: targetdir, overwrite */ if (!mu_msg_mime_part_save
if (!mu_msg_mime_part_save (msg, idx, NULL, TRUE)) { (msg, idx, opts->targetdir, opts->overwrite)) {
g_warning ("failed to save MIME-part %d", idx); g_warning ("failed to save MIME-part %d", idx);
rv = FALSE; rv = FALSE;
break; break;
@ -66,6 +66,8 @@ struct _SaveData {
gboolean attachments_only; gboolean attachments_only;
gboolean result; gboolean result;
guint saved_num; guint saved_num;
const gchar* targetdir;
gboolean overwrite;
}; };
typedef struct _SaveData SaveData; typedef struct _SaveData SaveData;
@ -89,7 +91,8 @@ save_part_if (MuMsgPart *part, SaveData *sd)
g_ascii_strcasecmp (part->type, "multipart") == 0) g_ascii_strcasecmp (part->type, "multipart") == 0)
return; 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) if (!sd->result)
g_warning ("failed to save MIME-part %u", part->index); g_warning ("failed to save MIME-part %u", part->index);
else else
@ -98,7 +101,8 @@ save_part_if (MuMsgPart *part, SaveData *sd)
} }
static gboolean 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; SaveData sd;
@ -106,6 +110,8 @@ save_certain_parts (MuMsg *msg, gboolean attachments_only)
sd.result = TRUE; sd.result = TRUE;
sd.saved_num = 0; sd.saved_num = 0;
sd.attachments_only = attachments_only; sd.attachments_only = attachments_only;
sd.overwrite = overwrite;
sd.targetdir = targetdir;
mu_msg_msg_part_foreach (msg, mu_msg_msg_part_foreach (msg,
(MuMsgPartForeachFunc)save_part_if, (MuMsgPartForeachFunc)save_part_if,
@ -138,9 +144,11 @@ save_parts (const char *path, MuConfigOptions *opts)
if (opts->parts) if (opts->parts)
rv = save_numbered_parts (msg, opts); rv = save_numbered_parts (msg, opts);
else if (opts->save_attachments) /* all attachments */ 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 */ else if (opts->save_all) /* all parts */
rv = save_certain_parts (msg, FALSE); rv = save_certain_parts (msg, FALSE,
opts->targetdir, opts->overwrite);
else else
g_assert_not_reached (); g_assert_not_reached ();

View File

@ -218,6 +218,8 @@ config_options_group_extract (MuConfigOptions *opts)
"save specific parts", NULL}, "save specific parts", NULL},
{"target-dir", 0, 0, G_OPTION_ARG_FILENAME, &opts->targetdir, {"target-dir", 0, 0, G_OPTION_ARG_FILENAME, &opts->targetdir,
"target directory for saving", NULL}, "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 } { NULL, 0, 0, 0, NULL, NULL, NULL }
}; };

View File

@ -65,6 +65,7 @@ struct _MuConfigOptions {
gboolean *save_attachments; /* extract all attachment parts */ gboolean *save_attachments; /* extract all attachment parts */
gchar *parts; /* comma-sep'd list of parts to save */ gchar *parts; /* comma-sep'd list of parts to save */
char *targetdir; /* where to save the attachments */ char *targetdir; /* where to save the attachments */
gboolean overwrite; /* should we overwrite same-named files */
}; };
typedef struct _MuConfigOptions MuConfigOptions; typedef struct _MuConfigOptions MuConfigOptions;

View File

@ -100,7 +100,7 @@ save_part (GMimeObject *part, const char *filename,
int fd, rv; int fd, rv;
GMimeDataWrapper *wrapper; GMimeDataWrapper *wrapper;
GMimeStream *stream; GMimeStream *stream;
rv = TRUE; rv = TRUE;
fd = mu_util_create_writeable_fd (filename, targetdir, fd = mu_util_create_writeable_fd (filename, targetdir,
overwrite); overwrite);
@ -110,7 +110,7 @@ save_part (GMimeObject *part, const char *filename,
errno != 0 ? strerror(errno) : ""); errno != 0 ? strerror(errno) : "");
return FALSE; return FALSE;
} }
stream = g_mime_stream_fs_new (fd); stream = g_mime_stream_fs_new (fd);
if (!stream) { if (!stream) {
g_critical ("%s: failed to create stream", __FUNCTION__); 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), 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)); wrapper = g_mime_part_get_content_object (GMIME_PART(part));
if (!wrapper) { if (!wrapper) {
@ -180,7 +180,7 @@ mu_msg_mime_part_save (MuMsg *msg, int wanted_idx,
spd.wanted_idx = wanted_idx; spd.wanted_idx = wanted_idx;
spd.targetdir = targetdir; spd.targetdir = targetdir;
spd.overwrite = overwrite; spd.overwrite = overwrite;
spd.stream = FALSE; spd.stream = FALSE; /* not used yet */
spd.result = FALSE; spd.result = FALSE;
/* get something fairly unique for building a filename; /* get something fairly unique for building a filename;