If the attachment has already been downloaded, it uses the existing file and doesn't try to create it again. This prevents a problem that happened when the user tried to open an attachment for the second time, but the existing file was read-only and couldn't be overwritten.
pmo-trunk-r3785
return NULL;
}
- /* try to write the file there */
filepath = g_strconcat (tmpdir, "/", orig_name, NULL);
- fd = g_open (filepath, O_CREAT|O_WRONLY|O_TRUNC, 0644);
- if (fd == -1) {
- g_warning ("%s: failed to create '%s': %s",
- __FUNCTION__, filepath, g_strerror(errno));
+ /* don't overwrite if it already exists, even if it is writable */
+ if (modest_maemo_utils_file_exists (filepath)) {
+ if (path!=NULL) {
+ *path = filepath;
+ } else {
+ g_free (filepath);
+ }
g_free (tmpdir);
- g_free (filepath);
return NULL;
+ } else {
+ /* try to write the file there */
+ fd = g_open (filepath, O_CREAT|O_WRONLY|O_TRUNC, 0644);
+ if (fd == -1) {
+ g_warning ("%s: failed to create '%s': %s",
+ __FUNCTION__, filepath, g_strerror(errno));
+ g_free (filepath);
+ g_free (tmpdir);
+ return NULL;
+ }
}
g_free (tmpdir);
* @orig_name: a string with the original name of the extension, or %NULL
* @hash_base: if %NULL, subdir will be random. If not, it will be a hash
* of this.
- * @path: a string with the created file path
+ * @path: a string with the created file path.
*
* Creates a temporary fs stream, in a random subdir of /tmp or /var/tmp.
*
- * Returns: a #TnyFsStream, or %NULL if operation failed.
+ * Returns: a #TnyFsStream, or %NULL if operation failed. Note that it is
+ * possible that the file already exists but it is not writable. In that case,
+ * the function would return NULL and @path would contain its path.
*/
TnyFsStream *modest_maemo_utils_create_temp_stream (const gchar *orig_name, const gchar *hash_base, gchar **path);
if (!TNY_IS_MSG (mime_part)) {
gchar *filepath = NULL;
const gchar *att_filename = tny_mime_part_get_filename (mime_part);
+ const gchar *content_type;
TnyFsStream *temp_stream = NULL;
temp_stream = modest_maemo_utils_create_temp_stream (att_filename, attachment_uid,
&filepath);
- if (temp_stream) {
- const gchar *content_type;
+ if (temp_stream != NULL) {
content_type = tny_mime_part_get_content_type (mime_part);
tny_mime_part_decode_to_stream (mime_part, TNY_STREAM (temp_stream));
g_free (filepath);
/* NOTE: files in the temporary area will be automatically
* cleaned after some time if they are no longer in use */
+ } else if (filepath != NULL) {
+ /* the file may already exist but it isn't writable,
+ * let's try to open it anyway */
+ content_type = tny_mime_part_get_content_type (mime_part);
+ modest_platform_activate_file (filepath, content_type);
+ g_free (filepath);
}
} else {
/* message attachment */