23 #include <sys/types.h>
29 #include <glib/gstdio.h>
30 #include <glib/gi18n-lib.h>
54 g_return_val_if_fail (dir != NULL,
FALSE);
57 if (g_mkdir_with_parents (dir, mode) == 0)
61 if (errsv == EEXIST && g_file_test (dir, G_FILE_TEST_IS_DIR))
64 display_name = g_filename_display_name (dir);
65 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv),
66 "Failed to create directory '%s': %s",
67 display_name, g_strerror (errsv));
68 g_free (display_name);
90 prgname = g_get_prgname ();
91 dirname = g_strdup_printf (
"%s-%u", prgname ? prgname :
"unknown", getpid ());
92 tmp_dir = g_build_filename (g_get_tmp_dir (), dirname, NULL);
140 name = g_build_filename (tmp, tmpl, NULL);
141 fd = g_mkstemp (name);
146 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv),
147 _(
"Failed to create a temporary file: %s"),
163 int fd = GPOINTER_TO_INT (fdptr);
190 file = g_file_new_for_path (file_name);
193 g_object_set_data_full (G_OBJECT (file),
"ev-mkstemp-fd",
194 GINT_TO_POINTER (fd), (GDestroyNotify)
close_fd_cb);
230 static const char letters[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
231 static const int NLETTERS =
sizeof (letters) - 1;
232 static int counter = 0;
239 XXXXXX = g_strrstr (tmpl,
"XXXXXX");
241 if (!XXXXXX || strncmp (XXXXXX,
"XXXXXX", 6))
248 g_get_current_time (&tv);
249 value = (tv.tv_usec ^ tv.tv_sec) + counter++;
251 for (count = 0; count < 100; value += 7777, ++count)
256 XXXXXX[0] = letters[v % NLETTERS];
258 XXXXXX[1] = letters[v % NLETTERS];
260 XXXXXX[2] = letters[v % NLETTERS];
262 XXXXXX[3] = letters[v % NLETTERS];
264 XXXXXX[4] = letters[v % NLETTERS];
266 XXXXXX[5] = letters[v % NLETTERS];
269 if (g_mkdir (tmpl, 0700) == 0)
304 name = g_build_filename (tmp, tmpl, NULL);
308 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv),
309 _(
"Failed to create a temporary directory: %s"),
329 if (g_str_has_prefix (filename,
tmp_dir)) {
338 GError *error = NULL;
343 res = g_file_delete (file, NULL, &error);
347 uri = g_file_get_uri (file);
348 g_warning (
"Unable to delete temp file %s: %s\n", uri, error->message);
350 g_error_free (error);
362 file = g_file_new_for_uri (uri);
363 if (!g_file_is_native (file)) {
364 g_warning (
"Attempting to delete non native uri: %s\n", uri);
365 g_object_unref (file);
370 g_object_unref (file);
379 if (!g_file_is_native (file))
382 path = g_file_get_path (file);
386 retval = g_str_has_prefix (path, g_get_tmp_dir ());
414 g_return_val_if_fail (to != NULL,
TRUE);
416 source_file = g_file_new_for_uri (from);
417 target_file = g_file_new_for_uri (to);
419 result = g_file_copy (source_file, target_file,
420 G_FILE_COPY_TARGET_DEFAULT_PERMS |
421 G_FILE_COPY_OVERWRITE,
422 NULL, NULL, NULL, error);
424 g_object_unref (target_file);
425 g_object_unref (source_file);
452 g_return_val_if_fail (from != NULL,
FALSE);
453 g_return_val_if_fail (to != NULL,
FALSE);
455 source_file = g_file_new_for_uri (from);
456 target_file = g_file_new_for_uri (to);
458 result = g_file_copy_attributes (source_file, target_file,
459 G_FILE_COPY_ALL_METADATA |
460 G_FILE_COPY_TARGET_DEFAULT_PERMS,
463 g_object_unref (target_file);
464 g_object_unref (source_file);
473 GFileInfo *file_info;
474 const gchar *content_type;
475 gchar *mime_type = NULL;
477 file = g_file_new_for_uri (uri);
478 file_info = g_file_query_info (file,
479 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
481 g_object_unref (file);
483 if (file_info == NULL)
486 content_type = g_file_info_get_content_type (file_info);
487 if (content_type != NULL) {
488 mime_type = g_content_type_get_mime_type (content_type);
490 if (mime_type == NULL) {
491 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
492 _(
"Unknown MIME Type"));
495 g_object_unref (file_info);
503 GFileInputStream *input_stream;
508 gchar *mime_type = NULL;
510 file = g_file_new_for_uri (uri);
512 input_stream = g_file_read (file, NULL, error);
514 g_object_unref (file);
518 size_read = g_input_stream_read (G_INPUT_STREAM (input_stream),
519 buffer,
sizeof (buffer), NULL, error);
520 if (size_read == -1) {
521 g_object_unref (input_stream);
522 g_object_unref (file);
526 retval = g_input_stream_close (G_INPUT_STREAM (input_stream), NULL, error);
528 g_object_unref (input_stream);
529 g_object_unref (file);
533 content_type = g_content_type_guess (NULL,
536 if (content_type == NULL) {
537 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
538 _(
"Unknown MIME Type"));
546 if (strcmp (content_type,
"*") == 0) {
547 g_free (content_type);
552 mime_type = g_content_type_get_mime_type (content_type);
553 g_free (content_type);
555 if (mime_type == NULL) {
556 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
557 _(
"Unknown MIME Type"));
590 #define BUFFER_SIZE 1024
599 gchar *uri_dst = NULL;
600 gchar *filename, *filename_dst = NULL;
612 g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
613 "Failed to find the \"%s\" command in the search path.",
618 filename = g_filename_from_uri (uri, NULL, error);
624 fd =
ev_mkstemp (
"comp.XXXXXX", &filename_dst, error);
633 argv[1] = compress ? (
char *)
"-c" : (
char *)
"-cd";
637 if (g_spawn_async_with_pipes (NULL, argv, NULL,
638 G_SPAWN_STDERR_TO_DEV_NULL,
640 NULL, &pout, NULL, &err)) {
641 GIOChannel *in, *out;
643 GIOStatus read_st, write_st;
644 gsize bytes_read, bytes_written;
646 in = g_io_channel_unix_new (pout);
647 g_io_channel_set_encoding (in, NULL, NULL);
648 out = g_io_channel_unix_new (fd);
649 g_io_channel_set_encoding (out, NULL, NULL);
652 read_st = g_io_channel_read_chars (in, buf,
656 if (read_st == G_IO_STATUS_NORMAL) {
657 write_st = g_io_channel_write_chars (out, buf,
661 if (write_st == G_IO_STATUS_ERROR)
663 }
else if (read_st == G_IO_STATUS_ERROR) {
666 }
while (bytes_read > 0);
668 g_io_channel_unref (in);
669 g_io_channel_unref (out);
675 g_propagate_error (error, err);
677 uri_dst = g_filename_to_uri (filename_dst, NULL, error);
682 g_free (filename_dst);
710 g_return_val_if_fail (uri != NULL, NULL);
739 g_return_val_if_fail (uri != NULL, NULL);