2 * Copyright (C) 2002 Jorn Baayen
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2, or (at your option)
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 #include <sys/types.h>
30 #include <glib/gstdio.h>
32 #include <libgnomevfs/gnome-vfs-uri.h>
33 #include <libgnomevfs/gnome-vfs-utils.h>
34 #include <libgnomevfs/gnome-vfs-ops.h>
35 #include <libgnomevfs/gnome-vfs-xfer.h>
37 #include "ev-file-helpers.h"
39 static gchar *dot_dir = NULL;
40 static gchar *tmp_dir = NULL;
41 static gint count = 0;
44 ensure_dir_exists (const char *dir)
46 if (g_file_test (dir, G_FILE_TEST_IS_DIR))
49 if (g_mkdir (dir, 488) == 0)
53 return g_file_test (dir, G_FILE_TEST_IS_DIR);
55 g_warning ("Failed to create directory %s: %s", dir, strerror (errno));
62 if (dot_dir == NULL) {
65 dot_dir = g_build_filename (gnome_user_dir_get (),
69 exists = ensure_dir_exists (dot_dir);
80 if (tmp_dir == NULL) {
84 dirname = g_strdup_printf ("evince-%u", getpid ());
85 tmp_dir = g_build_filename (g_get_tmp_dir (),
90 exists = ensure_dir_exists (tmp_dir);
98 ev_file_helpers_init (void)
103 ev_file_helpers_shutdown (void)
116 ev_tmp_filename (const gchar *prefix)
119 gchar *filename = NULL;
122 if (filename != NULL)
125 basename = g_strdup_printf ("%s-%d",
126 prefix ? prefix : "document",
129 filename = g_build_filename (ev_tmp_dir (),
133 } while (g_file_test (filename, G_FILE_TEST_EXISTS));
139 ev_xfer_uri_simple (const char *from,
143 GnomeVFSResult result;
144 GnomeVFSURI *source_uri;
145 GnomeVFSURI *target_uri;
150 source_uri = gnome_vfs_uri_new (from);
151 target_uri = gnome_vfs_uri_new (to);
153 result = gnome_vfs_xfer_uri (source_uri, target_uri,
154 GNOME_VFS_XFER_DEFAULT | GNOME_VFS_XFER_FOLLOW_LINKS,
155 GNOME_VFS_XFER_ERROR_MODE_ABORT,
156 GNOME_VFS_XFER_OVERWRITE_MODE_REPLACE,
159 gnome_vfs_uri_unref (target_uri);
160 gnome_vfs_uri_unref (source_uri);
162 if (result != GNOME_VFS_OK)
166 gnome_vfs_result_to_string (result));
167 return (result == GNOME_VFS_OK);
171 /* Compressed files support */
172 #define BZIPCOMMAND "bzip2"
173 #define GZIPCOMMAND "gzip"
175 #define BUFFER_SIZE 1024
178 ev_file_uncompress (const gchar *uri,
179 EvCompressionType type,
183 gchar *uri_unc = NULL;
184 gchar *filename, *filename_unc;
188 g_return_val_if_fail (uri != NULL, NULL);
190 if (type == EV_COMPRESSION_NONE)
193 cmd = g_find_program_in_path ((type == EV_COMPRESSION_BZIP2) ? BZIPCOMMAND : GZIPCOMMAND);
198 filename = g_filename_from_uri (uri, NULL, NULL);
204 filename_unc = g_build_filename (ev_tmp_dir (), "evinceXXXXXX", NULL);
205 fd = g_mkstemp (filename_unc);
209 g_free (filename_unc);
216 argv[3] = filename_unc;
219 if (g_spawn_async_with_pipes (NULL, argv, NULL,
220 G_SPAWN_STDERR_TO_DEV_NULL,
222 NULL, &pout, NULL, error)) {
223 GIOChannel *in, *out;
224 gchar buf[BUFFER_SIZE];
225 GIOStatus read_st, write_st;
226 gsize bytes_read, bytes_written;
228 in = g_io_channel_unix_new (pout);
229 g_io_channel_set_encoding (in, NULL, NULL);
230 out = g_io_channel_unix_new (fd);
231 g_io_channel_set_encoding (out, NULL, NULL);
234 read_st = g_io_channel_read_chars (in, buf,
238 if (read_st == G_IO_STATUS_NORMAL) {
239 write_st = g_io_channel_write_chars (out, buf,
243 if (write_st == G_IO_STATUS_ERROR)
245 } else if (read_st == G_IO_STATUS_ERROR) {
248 } while (bytes_read > 0);
250 g_io_channel_unref (in);
251 g_io_channel_unref (out);
256 if (*error == NULL) {
257 uri_unc = g_filename_to_uri (filename_unc,
263 g_free (filename_unc);