]> www.fi.muni.cz Git - evince.git/blob - libdocument/ev-file-helpers.c
NULL safety. (get_mime_type_from_data): Return the MIME type, not the
[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         const gchar *content_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         content_type = g_file_info_get_content_type (file_info);
229         g_object_unref (file_info);
230
231         if (!content_type)
232                 return NULL;
233
234         return g_content_type_get_mime_type (content_type);
235 }
236
237 static gchar *
238 get_mime_type_from_data (const gchar *uri, GError **error)
239 {
240         GFile            *file;
241         GFileInputStream *input_stream;
242         gssize            size_read;
243         guchar            buffer[1024];
244         gboolean          retval;
245         gchar            *content_type, *mime_type;
246
247         file = g_file_new_for_uri (uri);
248         
249         input_stream = g_file_read (file, NULL, error);
250         if (!input_stream) {
251                 g_object_unref (file);
252                 return NULL;
253         }
254
255         size_read = g_input_stream_read (G_INPUT_STREAM (input_stream),
256                                          buffer, sizeof (buffer), NULL, error);
257         if (size_read == -1) {
258                 g_object_unref (input_stream);
259                 g_object_unref (file);
260                 return NULL;
261         }
262
263         retval = g_input_stream_close (G_INPUT_STREAM (input_stream), NULL, error);
264
265         g_object_unref (input_stream);
266         g_object_unref (file);
267         if (!retval)
268                 return NULL;
269
270         content_type = g_content_type_guess (NULL, /* no filename */
271                                              buffer, size_read,
272                                              NULL);
273         if (!content_type)
274                 return NULL;
275
276         mime_type = g_content_type_get_mime_type (content_type);
277         g_free (content_type);
278         return mime_type;
279 }
280
281 /**
282  * ev_file_get_mime_type:
283  * @uri: the URI
284  * @fast: whether to use fast MIME type detection
285  * @error: a #GError location to store an error, or %NULL
286  *
287  * Note: on unknown MIME types, this may return NULL without @error
288  * being filled in.
289  * 
290  * Returns: a newly allocated string with the MIME type of the file at
291  *   @uri, or %NULL on error or if the MIME type could not be determined
292  */
293 gchar *
294 ev_file_get_mime_type (const gchar *uri,
295                        gboolean     fast,
296                        GError     **error)
297 {
298         return fast ? get_mime_type_from_uri (uri, error) : get_mime_type_from_data (uri, error);
299 }
300
301 /* Compressed files support */
302 #define BZIPCOMMAND "bzip2"
303 #define GZIPCOMMAND "gzip"
304 #define N_ARGS      4
305 #define BUFFER_SIZE 1024
306
307 static gchar *
308 compression_run (const gchar       *uri,
309                  EvCompressionType  type,
310                  gboolean           compress, 
311                  GError           **error)
312 {
313         gchar *argv[N_ARGS];
314         gchar *uri_dst = NULL;
315         gchar *filename, *filename_dst;
316         gchar *cmd;
317         gint   fd, pout;
318
319         if (type == EV_COMPRESSION_NONE)
320                 return NULL;
321
322         cmd = g_find_program_in_path ((type == EV_COMPRESSION_BZIP2) ? BZIPCOMMAND : GZIPCOMMAND);
323         if (!cmd)
324                 return NULL;
325
326         filename = g_filename_from_uri (uri, NULL, NULL);
327         if (!filename) {
328                 g_free (cmd);
329                 return NULL;
330         }
331         
332         filename_dst = g_build_filename (ev_tmp_dir (), "evinceXXXXXX", NULL);
333         fd = g_mkstemp (filename_dst);
334         if (fd < 0) {
335                 g_free (cmd);
336                 g_free (filename);
337                 g_free (filename_dst);
338                 return NULL;
339         }
340
341         argv[0] = cmd;
342         argv[1] = compress ? "-c" : "-cd";
343         argv[2] = filename;
344         argv[3] = NULL;
345
346         if (g_spawn_async_with_pipes (NULL, argv, NULL,
347                                       G_SPAWN_STDERR_TO_DEV_NULL,
348                                       NULL, NULL, NULL,
349                                       NULL, &pout, NULL, error)) {
350                 GIOChannel *in, *out;
351                 gchar buf[BUFFER_SIZE];
352                 GIOStatus read_st, write_st;
353                 gsize bytes_read, bytes_written;
354
355                 in = g_io_channel_unix_new (pout);
356                 g_io_channel_set_encoding (in, NULL, NULL);
357                 out = g_io_channel_unix_new (fd);
358                 g_io_channel_set_encoding (out, NULL, NULL);
359
360                 do {
361                         read_st = g_io_channel_read_chars (in, buf,
362                                                            BUFFER_SIZE,
363                                                            &bytes_read,
364                                                            error);
365                         if (read_st == G_IO_STATUS_NORMAL) {
366                                 write_st = g_io_channel_write_chars (out, buf,
367                                                                      bytes_read,
368                                                                      &bytes_written,
369                                                                      error);
370                                 if (write_st == G_IO_STATUS_ERROR)
371                                         break;
372                         } else if (read_st == G_IO_STATUS_ERROR) {
373                                 break;
374                         }
375                 } while (bytes_read > 0);
376
377                 g_io_channel_unref (in);
378                 g_io_channel_unref (out);
379         }
380
381         close (fd);
382
383         if (*error == NULL) {
384                 uri_dst = g_filename_to_uri (filename_dst, NULL, NULL);
385         }
386
387         g_free (cmd);
388         g_free (filename);
389         g_free (filename_dst);
390
391         return uri_dst;
392 }
393
394 gchar *
395 ev_file_uncompress (const gchar       *uri,
396                     EvCompressionType  type,
397                     GError           **error)
398 {
399         g_return_val_if_fail (uri != NULL, NULL);
400
401         return compression_run (uri, type, FALSE, error);
402 }
403
404 gchar *
405 ev_file_compress (const gchar       *uri,
406                   EvCompressionType  type,
407                   GError           **error)
408 {
409         g_return_val_if_fail (uri != NULL, NULL);
410
411         return compression_run (uri, type, TRUE, error);
412 }