+ if (g_strstr_len (orig_name, strlen(orig_name), "/") != NULL) {
+ g_warning ("%s: filename contains '/' character(s) (%s)",
+ __FUNCTION__, orig_name);
+ return NULL;
+ }
+
+ /* make a random subdir under /tmp or /var/tmp */
+ if (hash_base != NULL) {
+ hash_number = g_str_hash (hash_base);
+ } else {
+ hash_number = (guint) random ();
+ }
+ tmpdir = g_strdup_printf ("%s/%u", g_get_tmp_dir (), hash_number);
+ if ((g_access (tmpdir, R_OK) == -1) && (g_mkdir (tmpdir, 0755) == -1)) {
+ g_warning ("%s: failed to create dir '%s': %s",
+ __FUNCTION__, tmpdir, g_strerror(errno));
+ g_free (tmpdir);
+ return NULL;
+ }
+
+ filepath = g_strconcat (tmpdir, "/", orig_name, NULL);
+ /* 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);
+ 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);
+
+ if (path)
+ *path = filepath;
+
+ return TNY_FS_STREAM (tny_fs_stream_new (fd));