]> www.fi.muni.cz Git - evince.git/blob - libdocument/ev-file-helpers.c
Don't leak the input stream.
[evince.git] / libdocument / ev-file-helpers.c
1 /*
2  *  Copyright (C) 2002 Jorn Baayen
3  *
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)
7  *  any later version.
8  *
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.
13  *
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.
17  *
18  *  $Id$
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include <stdlib.h>
26 #include <sys/types.h>
27 #include <unistd.h>
28 #include <string.h>
29 #include <glib.h>
30 #include <glib/gstdio.h>
31 #include <errno.h>
32
33 #include "ev-file-helpers.h"
34
35 static gchar *tmp_dir = NULL;
36 static gint   count = 0;
37
38 gboolean
39 ev_dir_ensure_exists (const gchar *dir,
40                       int          mode)
41 {
42         if (g_mkdir_with_parents (dir, mode) == 0)
43                 return TRUE;
44
45         if (errno == EEXIST)
46                 return g_file_test (dir, G_FILE_TEST_IS_DIR);
47         
48         g_warning ("Failed to create directory %s: %s", dir, g_strerror (errno));
49         return FALSE;
50 }
51
52 const gchar *
53 ev_tmp_dir (void)
54 {
55         if (tmp_dir == NULL) {
56                 gboolean exists;
57                 gchar   *dirname, *prgname;
58
59                 prgname = g_get_prgname ();
60                 dirname = g_strdup_printf ("%s-%u", prgname ? prgname : "unknown", getpid ());
61                 tmp_dir = g_build_filename (g_get_tmp_dir (),
62                                             dirname,
63                                             NULL);
64                 g_free (dirname);
65
66                 exists = ev_dir_ensure_exists (tmp_dir, 0700);
67                 g_assert (exists);
68         }
69
70         return tmp_dir;
71 }
72
73 void
74 _ev_file_helpers_init (void)
75 {
76 }
77
78 void
79 _ev_file_helpers_shutdown (void)
80 {       
81         if (tmp_dir != NULL)    
82                 g_rmdir (tmp_dir);
83
84         g_free (tmp_dir);
85         tmp_dir = NULL;
86 }
87
88 GFile *
89 ev_tmp_file_get (const gchar *prefix)
90 {
91         gchar *path;
92         GFile *file;
93
94         path = ev_tmp_filename (prefix);
95         file = g_file_new_for_path (path);
96         
97         g_free (path);
98         
99         return file;
100 }
101
102 gchar * 
103 ev_tmp_filename (const gchar *prefix)
104 {
105         gchar *basename;
106         gchar *filename = NULL;
107
108         do {
109                 if (filename != NULL)
110                         g_free (filename);
111                         
112                 basename = g_strdup_printf ("%s-%d",
113                                             prefix ? prefix : "document",
114                                             count ++);
115                 
116                 filename = g_build_filename (ev_tmp_dir (),
117                                              basename, NULL);
118                 
119                 g_free (basename);
120         } while (g_file_test (filename, G_FILE_TEST_EXISTS));
121                         
122         return filename;
123 }
124
125 /* Remove a local temp file created by evince */
126 void
127 ev_tmp_filename_unlink (const gchar *filename)
128 {
129         const gchar *tempdir;
130         
131         if (!filename)
132                 return;
133
134         tempdir = g_get_tmp_dir ();
135         if (g_ascii_strncasecmp (filename, tempdir, strlen (tempdir)) == 0) {
136                 g_unlink (filename);
137         }
138 }
139
140 void
141 ev_tmp_file_unlink (GFile *file)
142 {
143         gboolean res;
144         GError  *error = NULL;
145
146         if (!file)
147                 return;
148         
149         res = g_file_delete (file, NULL, &error);
150         if (!res) {
151                 char *uri;
152                 
153                 uri = g_file_get_uri (file);
154                 g_warning ("Unable to delete temp file %s: %s\n", uri, error->message);
155                 g_free (uri);
156                 g_error_free (error);
157         }
158 }
159
160 void
161 ev_tmp_uri_unlink (const gchar *uri)
162 {
163         GFile *file;
164         
165         if (!uri)
166                 return;
167         
168         file = g_file_new_for_uri (uri);
169         if (!g_file_is_native (file)) {
170                 g_warning ("Attempting to delete non native uri: %s\n", uri);
171                 g_object_unref (file);
172                 return;
173         }
174         
175         ev_tmp_file_unlink (file);
176         g_object_unref (file);
177 }
178
179 gboolean
180 ev_xfer_uri_simple (const char *from,
181                     const char *to,
182                     GError     **error)
183 {
184         GFile *source_file;
185         GFile *target_file;
186         GError *ioerror = NULL;
187         gboolean result;
188         
189         if (!from)
190                 return FALSE;
191         
192         source_file = g_file_new_for_uri (from);
193         target_file = g_file_new_for_uri (to);
194         
195         result = g_file_copy (source_file, target_file,
196 #if GLIB_CHECK_VERSION(2,19,0)
197                               G_FILE_COPY_TARGET_DEFAULT_PERMS |
198 #endif
199                               G_FILE_COPY_OVERWRITE,
200                               NULL, NULL, NULL, &ioerror);
201
202         g_object_unref (target_file);
203         g_object_unref (source_file);
204     
205         if (!result) {
206                 g_propagate_error (error, ioerror);
207         }
208         return result;
209
210 }
211
212 static gchar *
213 get_mime_type_from_uri (const gchar *uri, GError **error)
214 {
215         GFile     *file;
216         GFileInfo *file_info;
217         gchar     *mime_type;
218
219         file = g_file_new_for_uri (uri);
220         file_info = g_file_query_info (file,
221                                        G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
222                                        0, NULL, error);
223         g_object_unref (file);
224
225         if (file_info == NULL)
226                 return NULL;
227
228         mime_type = g_content_type_get_mime_type (
229                         g_file_info_get_content_type (file_info));
230         g_object_unref (file_info);
231
232         return mime_type;
233 }
234
235 static gchar *
236 get_mime_type_from_data (const gchar *uri, GError **error)
237 {
238         GFile            *file;
239         GFileInputStream *input_stream;
240         gssize            size_read;
241         guchar            buffer[1024];
242         gboolean          retval;
243
244         file = g_file_new_for_uri (uri);
245         
246         input_stream = g_file_read (file, NULL, error);
247         if (!input_stream) {
248                 g_object_unref (file);
249                 return NULL;
250         }
251
252         size_read = g_input_stream_read (G_INPUT_STREAM (input_stream),
253                                          buffer, sizeof (buffer), NULL, error);
254         if (size_read == -1) {
255                 g_object_unref (input_stream);
256                 g_object_unref (file);
257                 return NULL;
258         }
259
260         retval = g_input_stream_close (G_INPUT_STREAM (input_stream), NULL, error);
261
262         g_object_unref (input_stream);
263         g_object_unref (file);
264         if (!retval)
265                 return NULL;
266
267         return g_content_type_guess (NULL, /* no filename */
268                                      buffer, size_read,
269                                      NULL);
270 }
271
272 /**
273  * ev_file_get_mime_type:
274  * @uri: the URI
275  * @fast: whether to use fast MIME type detection
276  * @error: a #GError location to store an error, or %NULL
277  *
278  * Note: on unknown MIME types, this may return NULL without @error
279  * being filled in.
280  * 
281  * Returns: a newly allocated string with the MIME type of the file at
282  *   @uri, or %NULL on error or if the MIME type could not be determined
283  */
284 gchar *
285 ev_file_get_mime_type (const gchar *uri,
286                        gboolean     fast,
287                        GError     **error)
288 {
289         return fast ? get_mime_type_from_uri (uri, error) : get_mime_type_from_data (uri, error);
290 }
291
292 /* Compressed files support */
293 #define BZIPCOMMAND "bzip2"
294 #define GZIPCOMMAND "gzip"
295 #define N_ARGS      4
296 #define BUFFER_SIZE 1024
297
298 static gchar *
299 compression_run (const gchar       *uri,
300                  EvCompressionType  type,
301                  gboolean           compress, 
302                  GError           **error)
303 {
304         gchar *argv[N_ARGS];
305         gchar *uri_dst = NULL;
306         gchar *filename, *filename_dst;
307         gchar *cmd;
308         gint   fd, pout;
309
310         if (type == EV_COMPRESSION_NONE)
311                 return NULL;
312
313         cmd = g_find_program_in_path ((type == EV_COMPRESSION_BZIP2) ? BZIPCOMMAND : GZIPCOMMAND);
314         if (!cmd)
315                 return NULL;
316
317         filename = g_filename_from_uri (uri, NULL, NULL);
318         if (!filename) {
319                 g_free (cmd);
320                 return NULL;
321         }
322         
323         filename_dst = g_build_filename (ev_tmp_dir (), "evinceXXXXXX", NULL);
324         fd = g_mkstemp (filename_dst);
325         if (fd < 0) {
326                 g_free (cmd);
327                 g_free (filename);
328                 g_free (filename_dst);
329                 return NULL;
330         }
331
332         argv[0] = cmd;
333         argv[1] = compress ? "-c" : "-cd";
334         argv[2] = filename;
335         argv[3] = NULL;
336
337         if (g_spawn_async_with_pipes (NULL, argv, NULL,
338                                       G_SPAWN_STDERR_TO_DEV_NULL,
339                                       NULL, NULL, NULL,
340                                       NULL, &pout, NULL, error)) {
341                 GIOChannel *in, *out;
342                 gchar buf[BUFFER_SIZE];
343                 GIOStatus read_st, write_st;
344                 gsize bytes_read, bytes_written;
345
346                 in = g_io_channel_unix_new (pout);
347                 g_io_channel_set_encoding (in, NULL, NULL);
348                 out = g_io_channel_unix_new (fd);
349                 g_io_channel_set_encoding (out, NULL, NULL);
350
351                 do {
352                         read_st = g_io_channel_read_chars (in, buf,
353                                                            BUFFER_SIZE,
354                                                            &bytes_read,
355                                                            error);
356                         if (read_st == G_IO_STATUS_NORMAL) {
357                                 write_st = g_io_channel_write_chars (out, buf,
358                                                                      bytes_read,
359                                                                      &bytes_written,
360                                                                      error);
361                                 if (write_st == G_IO_STATUS_ERROR)
362                                         break;
363                         } else if (read_st == G_IO_STATUS_ERROR) {
364                                 break;
365                         }
366                 } while (bytes_read > 0);
367
368                 g_io_channel_unref (in);
369                 g_io_channel_unref (out);
370         }
371
372         close (fd);
373
374         if (*error == NULL) {
375                 uri_dst = g_filename_to_uri (filename_dst, NULL, NULL);
376         }
377
378         g_free (cmd);
379         g_free (filename);
380         g_free (filename_dst);
381
382         return uri_dst;
383 }
384
385 gchar *
386 ev_file_uncompress (const gchar       *uri,
387                     EvCompressionType  type,
388                     GError           **error)
389 {
390         g_return_val_if_fail (uri != NULL, NULL);
391
392         return compression_run (uri, type, FALSE, error);
393 }
394
395 gchar *
396 ev_file_compress (const gchar       *uri,
397                   EvCompressionType  type,
398                   GError           **error)
399 {
400         g_return_val_if_fail (uri != NULL, NULL);
401
402         return compression_run (uri, type, TRUE, error);
403 }