Scan directories using GIO.
[rox-filer.git] / ROX-Filer / src / run.c
blob3d2c5f75591d80179a6ceb10634c4e2b0bd5f51a
1 /*
2 * ROX-Filer, filer for the ROX desktop project
3 * Copyright (C) 2006, Thomas Leonard and others (see changelog for details).
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the Free
7 * Software Foundation; either version 2 of the License, or (at your option)
8 * any later version.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
17 * Place, Suite 330, Boston, MA 02111-1307 USA
20 /* run.c */
22 #include "config.h"
24 #include <gio/gio.h>
25 #include <errno.h>
26 #include <string.h>
27 #include <sys/param.h>
29 #include "global.h"
31 #include "run.h"
32 #include "support.h"
33 #include "gui_support.h"
34 #include "filer.h"
35 #include "display.h"
36 #include "main.h"
37 #include "type.h"
38 #include "dir.h"
39 #include "diritem.h"
40 #include "action.h"
41 #include "icon.h"
42 #include "choices.h"
44 /* Static prototypes */
45 static void write_data(gpointer data, gint fd, GdkInputCondition cond);
46 static gboolean follow_symlink(GFile *path,
47 FilerWindow *filer_window,
48 FilerWindow *src_window);
49 static gboolean open_file(const guchar *path, MIME_type *type);
50 static void open_mountpoint(const guchar *full_path, DirItem *item,
51 FilerWindow *filer_window, FilerWindow *src_window,
52 gboolean edit);
53 static gboolean run_desktop(const char *full_path,
54 const char **args, const char *dir);
55 static gboolean type_open(const char *path, MIME_type *type);
57 typedef struct _PipedData PipedData;
59 struct _PipedData
61 guchar *data;
62 gint tag;
63 gulong sent;
64 gulong length;
68 /****************************************************************
69 * EXTERNAL INTERFACE *
70 ****************************************************************/
73 /* An application has been double-clicked (or run in some other way) */
74 void run_app(const char *path)
76 GString *apprun;
77 const char *argv[] = {NULL, NULL};
79 apprun = g_string_new(path);
80 argv[0] = g_string_append(apprun, "/AppRun")->str;
82 rox_spawn(home_dir, argv);
84 g_string_free(apprun, TRUE);
87 /* Execute this program, passing all the URIs in the list as arguments.
88 * URIs that are files on the local machine will be passed as simple
89 * pathnames. The uri_list should be freed after this function returns.
91 void run_with_files(const char *path, GList *uri_list)
93 const char **argv;
94 int argc = 0, i;
95 struct stat info;
96 MIME_type *type;
98 if (stat(path, &info))
100 delayed_error(_("Program %s not found - deleted?"), path);
101 return;
104 argv = g_malloc(sizeof(char *) * (g_list_length(uri_list) + 2));
106 if (S_ISDIR(info.st_mode))
107 argv[argc++] = make_path(path, "AppRun");
108 else
109 argv[argc++] = path;
111 while (uri_list)
113 const EscapedPath *uri = uri_list->data;
114 char *local;
116 local = get_local_path(uri);
117 if (local)
118 argv[argc++] = local;
119 else
120 argv[argc++] = unescape_uri(uri);
121 uri_list = uri_list->next;
124 argv[argc++] = NULL;
126 type = type_from_path(argv[0]);
127 if (type && type == application_x_desktop)
129 run_desktop(argv[0], argv + 1, home_dir);
131 else
133 rox_spawn(home_dir, argv);
136 for (i = 1; i < argc; i++)
137 g_free((gchar *) argv[i]);
138 g_free(argv);
141 /* Run the program as '<path> -', piping the data to it via stdin.
142 * You can g_free() the data as soon as this returns.
144 void run_with_data(const char *path, gpointer data, gulong length)
146 const char *argv[] = {NULL, "-", NULL};
147 struct stat info;
148 int fds[2];
149 PipedData *pd;
151 if (stat(path, &info))
153 delayed_error(_("Program %s not found - deleted?"), path);
154 return;
157 if (S_ISDIR(info.st_mode))
158 argv[0] = make_path(path, "AppRun");
159 else
160 argv[0] = path;
162 if (pipe(fds))
164 delayed_error("pipe: %s", g_strerror(errno));
165 return;
167 close_on_exec(fds[1], TRUE);
168 close_on_exec(fds[0], TRUE);
170 switch (fork())
172 case -1:
173 delayed_error("fork: %s", g_strerror(errno));
174 close(fds[1]);
175 break;
176 case 0:
177 /* We are the child */
178 chdir(home_dir);
179 if (dup2(fds[0], 0) == -1)
180 g_warning("dup2() failed: %s\n",
181 g_strerror(errno));
182 else
184 close_on_exec(0, FALSE);
185 if (execv(argv[0], (char **) argv))
186 g_warning("execv(%s) failed: %s\n",
187 argv[0], g_strerror(errno));
189 _exit(1);
190 default:
191 /* We are the parent */
192 set_blocking(fds[1], FALSE);
193 pd = g_new(PipedData, 1);
194 pd->data = g_malloc(length);
195 memcpy(pd->data, data, length);
196 pd->length = length;
197 pd->sent = 0;
198 pd->tag = gdk_input_add_full(fds[1], GDK_INPUT_WRITE,
199 write_data, pd, NULL);
200 break;
203 close(fds[0]);
206 /* Splits args into an argument vector, and runs the program. Must be
207 * executable.
209 void run_with_args(const char *path, DirItem *item, const char *args)
211 GError *error = NULL;
212 gchar **argv = NULL;
213 int n_args = 0;
215 if (item->base_type != TYPE_DIRECTORY && item->base_type != TYPE_FILE)
217 delayed_error("Arguments (%s) given for non-executable item %s",
218 args, path);
219 return;
222 if (!g_shell_parse_argv(args, &n_args, &argv, &error))
224 delayed_error("Failed to parse argument string '%s':\n%s",
225 args, error->message);
226 g_error_free(error);
227 return;
230 g_return_if_fail(argv != NULL);
231 g_return_if_fail(error == NULL);
233 argv = g_realloc(argv, (n_args + 2) * sizeof(gchar *));
234 memmove(argv + 1, argv, (n_args + 1) * sizeof(gchar *));
236 if (item->base_type == TYPE_DIRECTORY)
237 argv[0] = g_strconcat(path, "/AppRun", NULL);
238 else
239 argv[0] = g_strdup(path);
241 rox_spawn(home_dir, (const gchar **) argv);
243 g_strfreev(argv);
246 /* Load a file, open a directory or run an application. Or, if 'edit' is set:
247 * edit a file, open an application, follow a symlink or mount a device.
249 * filer_window is the window to use for displaying a directory.
250 * NULL will always use a new directory when needed.
251 * src_window is the window to copy options from, or NULL.
253 * Returns TRUE on success.
255 gboolean run_diritem(const guchar *full_path,
256 DirItem *item,
257 FilerWindow *filer_window,
258 FilerWindow *src_window,
259 gboolean edit)
261 GFile *gfile;
262 gboolean retval;
264 gfile = g_file_new_for_path(full_path);
265 retval = run_diritem_gfile(gfile, item, filer_window, src_window, edit);
266 g_object_unref(gfile);
268 return retval;
271 gboolean run_diritem_gfile(GFile *path,
272 DirItem *item,
273 FilerWindow *filer_window,
274 FilerWindow *src_window,
275 gboolean edit)
277 const char *full_path = "TODO";
279 if (item->flags & ITEM_FLAG_SYMLINK && edit)
280 return follow_symlink(path, filer_window, src_window);
282 switch (item->base_type)
284 case TYPE_DIRECTORY:
285 if (item->flags & ITEM_FLAG_APPDIR && !edit)
287 run_app(full_path);
288 return TRUE;
291 if (item->flags & ITEM_FLAG_MOUNT_POINT)
293 open_mountpoint(full_path, item,
294 filer_window, src_window, edit);
296 else if (filer_window)
297 filer_change_to(filer_window, full_path, NULL);
298 else
299 filer_opendir_gfile(path, src_window, NULL);
300 return TRUE;
301 case TYPE_FILE:
302 if (EXECUTABLE_FILE(item) && !edit)
304 const char *argv[] = {NULL, NULL};
305 guchar *dir = filer_window
306 ? filer_window->sym_path
307 : NULL;
309 if (item->mime_type == application_x_desktop)
310 return run_desktop(full_path,
311 NULL, dir);
312 else
313 argv[0] = full_path;
315 return rox_spawn(dir, argv) != 0;
318 return open_file(full_path, edit ? text_plain
319 : item->mime_type);
320 case TYPE_ERROR:
321 delayed_error(_("File doesn't exist, or I can't "
322 "access it: %s"), full_path);
323 return FALSE;
324 default:
325 delayed_error(
326 _("I don't know how to open '%s'"), full_path);
327 return FALSE;
331 /* Attempt to open this item */
332 gboolean run_by_path(const guchar *full_path)
334 gboolean retval;
335 DirItem *item;
337 /* XXX: Loads an image - wasteful */
338 item = diritem_new("");
339 diritem_restat(full_path, item, NULL);
340 retval = run_diritem(full_path, item, NULL, NULL, FALSE);
341 diritem_free(item);
343 return retval;
346 /* Attempt to open this item */
347 gboolean run_by_gfile(GFile *path)
349 gboolean retval;
350 DirItem *item;
352 /* XXX: Loads an image - wasteful */
353 item = diritem_new("");
354 diritem_restat_gfile(path, item);
355 retval = run_diritem_gfile(path, item, NULL, NULL, FALSE);
356 diritem_free(item);
358 return retval;
361 /* Convert uri to path and call run_by_path() */
362 gboolean run_by_uri(const gchar *uri, gchar **errmsg)
364 gboolean retval;
365 gchar *scheme;
366 gchar *cmd;
368 scheme=get_uri_scheme((EscapedPath *) uri);
369 if(!scheme)
371 *errmsg=g_strdup_printf(_("'%s' is not a valid URI"),
372 uri);
373 return FALSE;
376 if(strcmp(scheme, "file")==0 || strcmp(scheme, "trash")==0) {
377 GFile *gfile;
378 gfile = g_file_new_for_uri(uri);
380 retval = run_by_gfile(gfile);
381 g_object_unref(gfile);
383 if(!retval)
384 *errmsg=g_strdup_printf(_("%s not accessable"), uri);
386 } else if((cmd=choices_find_xdg_path_load(scheme, "URI", SITE))) {
387 DirItem *item;
389 item=diritem_new(scheme);
390 diritem_restat(cmd, item, NULL);
392 run_with_args(cmd, item, uri);
393 retval=TRUE; /* we hope... */
395 diritem_free(item);
396 g_free(cmd);
398 } else {
399 retval=FALSE;
400 *errmsg=g_strdup_printf(_("%s: no handler for %s"),
401 uri, scheme);
404 g_free(scheme);
406 return retval;
409 /* Open dir/Help, or show a message if missing */
410 void show_help_files(const char *dir)
412 const char *help_dir;
414 help_dir = make_path(dir, "Help");
416 if (file_exists(help_dir))
417 filer_opendir(help_dir, NULL, NULL);
418 else
419 info_message(
420 _("Application:\n"
421 "This is an application directory - you can "
422 "run it as a program, or open it (hold down "
423 "Shift while you open it). Most applications provide "
424 "their own help here, but this one doesn't."));
427 /* Open a directory viewer showing this file, and wink it */
428 void open_to_show(const guchar *path)
430 FilerWindow *new;
431 guchar *dir, *slash;
433 g_return_if_fail(path != NULL);
435 dir = g_strdup(path);
436 slash = strrchr(dir, '/');
437 if (slash == dir || !slash)
439 /* Item in the root (or root itself!) */
440 new = filer_opendir("/", NULL, NULL);
441 if (new && dir[1])
442 display_set_autoselect(new, dir + 1);
444 else
446 *slash = '\0';
447 new = filer_opendir(dir, NULL, NULL);
448 if (new)
450 if (slash[1] == '.')
451 display_set_hidden(new, TRUE);
452 display_set_autoselect(new, slash + 1);
456 g_free(dir);
459 /* Invoked using -x, this indicates that the filesystem has been modified
460 * and we should look at this item again.
462 void examine(const guchar *path)
464 struct stat info;
466 if (mc_stat(path, &info) != 0)
468 /* Deleted? Do a paranoid update of everything... */
469 filer_check_mounted(path);
471 else
473 /* Update directory containing this item... */
474 dir_check_this(path);
476 /* If this is itself a directory then rescan its contents... */
477 if (S_ISDIR(info.st_mode))
478 refresh_dirs(path);
480 /* If it's on the pinboard or a panel, update the icon... */
481 icons_may_update(path);
485 /****************************************************************
486 * INTERNAL FUNCTIONS *
487 ****************************************************************/
490 static void write_data(gpointer data, gint fd, GdkInputCondition cond)
492 PipedData *pd = (PipedData *) data;
494 while (pd->sent < pd->length)
496 int sent;
498 sent = write(fd, pd->data + pd->sent, pd->length - pd->sent);
500 if (sent < 0)
502 if (errno == EAGAIN)
503 return;
504 delayed_error(_("Could not send data to program: %s"),
505 g_strerror(errno));
506 goto finish;
509 pd->sent += sent;
512 finish:
513 g_source_remove(pd->tag);
514 g_free(pd->data);
515 g_free(pd);
516 close(fd);
519 static char *g_file_readlink(GFile *symlink, GError **error)
521 GFileInfo *info;
522 char *target;
524 info = g_file_query_info(symlink, G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, NULL, error);
525 if (*error)
526 return NULL;
528 target = (char *) g_file_info_get_symlink_target(info);
529 if (target)
530 target = g_strdup(target);
532 g_object_unref(info);
534 return target;
537 /* Follow the link 'full_path' and display it in filer_window, or a
538 * new window if that is NULL.
540 static gboolean follow_symlink(GFile *path,
541 FilerWindow *filer_window,
542 FilerWindow *src_window)
544 char *target, *leafname;
545 GFile *resolved, *new_dir;
546 GError *error = NULL;
548 target = g_file_readlink(path, &error);
550 if (error)
552 delayed_error(_("Could not read link: %s"), error->message);
553 g_error_free(error);
554 return FALSE;
557 resolved = g_file_resolve_relative_path(path, target);
558 g_free(target);
559 target = NULL;
561 new_dir = g_file_get_parent(resolved);
562 leafname = g_file_get_basename(resolved);
563 g_object_unref(resolved);
565 if (filer_window)
566 filer_change_to(filer_window, g_file_get_path(new_dir), leafname); /* XXX : free */
567 else
569 FilerWindow *new;
571 new = filer_opendir_gfile(new_dir, src_window, NULL);
572 if (new)
573 display_set_autoselect(new, leafname);
576 g_object_unref(new_dir);
577 g_free(leafname);
579 return TRUE;
582 /* Load this file into an appropriate editor */
583 static gboolean open_file(const guchar *path, MIME_type *type)
585 g_return_val_if_fail(type != NULL, FALSE);
587 if (type_open(path, type))
588 return TRUE;
590 report_error(
591 _("No run action specified for files of this type (%s/%s) - "
592 "you can set a run action by choosing `Set Run Action' "
593 "from the File menu, or you can just drag the file to an "
594 "application.%s"),
595 type->media_type,
596 type->subtype,
597 type->executable ? _("\n\nNote: If this is a computer program which "
598 "you want to run, you need to set the execute bit "
599 "by choosing Permissions from the File menu.")
600 : "");
602 return FALSE;
605 /* Called like run_diritem, when a mount-point is opened */
606 static void open_mountpoint(const guchar *full_path, DirItem *item,
607 FilerWindow *filer_window, FilerWindow *src_window,
608 gboolean edit)
610 gboolean mounted = (item->flags & ITEM_FLAG_MOUNTED) != 0;
612 if (mounted == edit)
614 GList *paths;
616 paths = g_list_prepend(NULL, (gpointer) full_path);
617 action_mount(paths, filer_window == NULL, !mounted, -1);
618 g_list_free(paths);
619 if (filer_window && !mounted)
620 filer_change_to(filer_window, full_path, NULL);
622 else
624 if (filer_window)
625 filer_change_to(filer_window, full_path, NULL);
626 else
627 filer_opendir(full_path, src_window, NULL);
631 /* full_path is a .desktop file. Execute the application, using the Exec line
632 * from the file.
633 * Returns TRUE on success.
635 static gboolean run_desktop(const char *full_path,
636 const char **args,
637 const char *dir)
639 GError *error = NULL;
640 char *exec = NULL;
641 char *terminal = NULL;
642 char *req_dir = NULL;
643 gint argc = 0;
644 gchar **argv = NULL;
645 GPtrArray *expanded = NULL;
646 gboolean inserted_args = FALSE;
647 int i;
648 gboolean success = FALSE;
650 get_values_from_desktop_file(full_path,
651 &error,
652 "Desktop Entry", "Exec", &exec,
653 "Desktop Entry", "Terminal", &terminal,
654 "Desktop Entry", "Path", &req_dir,
655 NULL);
656 if (error)
658 delayed_error("Failed to parse .desktop file '%s':\n%s",
659 full_path, error->message);
660 goto err;
663 if (!exec)
665 delayed_error("Can't find Exec command in .desktop file '%s'",
666 full_path);
667 goto err;
670 if (!g_shell_parse_argv(exec, &argc, &argv, &error))
672 delayed_error("Failed to parse '%s' from '%s':\n%s",
673 exec, full_path, error->message);
674 goto err;
677 expanded = g_ptr_array_new();
679 if (terminal && g_strcasecmp(terminal, "true") == 0) {
680 g_ptr_array_add(expanded, g_strdup("xterm"));
681 g_ptr_array_add(expanded, g_strdup("-e"));
684 for (i = 0; i < argc; i++)
686 const char *src = argv[i];
688 if (src[0] == '%' && src[1] != '\0' && src[2] == '\0')
690 /* We should treat these four differently. */
691 if (src[1] == 'f' || src[1] == 'F' ||
692 src[1] == 'u' || src[1] == 'U')
694 int j;
695 for (j = 0; args && args[j]; j++)
696 g_ptr_array_add(expanded, g_strdup(args[j]));
697 inserted_args = TRUE;
699 else
701 delayed_error("Unsupported escape character in '%s' in '%s'",
702 exec, full_path);
703 goto err;
706 else
708 g_ptr_array_add(expanded, g_strdup(src));
711 if (!inserted_args)
713 /* Many .desktop files don't include a % expansion. In that case
714 * add the arguments here.
716 int j;
717 for (j = 0; args && args[j]; j++)
718 g_ptr_array_add(expanded, g_strdup(args[j]));
720 g_ptr_array_add(expanded, NULL);
722 if(req_dir)
723 dir = req_dir;
725 success = rox_spawn(dir, (const gchar **) expanded->pdata);
726 err:
727 if (error != NULL)
728 g_error_free(error);
729 if (exec != NULL)
730 g_free(exec);
731 if (terminal != NULL)
732 g_free(terminal);
733 if (req_dir != NULL)
734 g_free(req_dir);
735 if (argv != NULL)
736 g_strfreev(argv);
737 if (expanded != NULL)
739 g_ptr_array_foreach(expanded, (GFunc) g_free, NULL);
740 g_ptr_array_free(expanded, TRUE);
743 return success;
746 /* Returns FALSE is no run action is set for this type. */
747 static gboolean type_open(const char *path, MIME_type *type)
749 gchar *argv[] = {NULL, NULL, NULL};
750 char *open;
751 struct stat info;
753 argv[1] = (char *) path;
755 open = handler_for(type);
756 if (!open)
757 return FALSE;
759 if (stat(open, &info))
761 report_error("stat(%s): %s", open, g_strerror(errno));
762 g_free(open);
763 return TRUE;
766 if (info.st_mode & S_IWOTH)
768 gchar *choices_dir;
769 GList *paths;
771 report_error(_("Executable '%s' is world-writeable! Refusing "
772 "to run. Please change the permissions now (this "
773 "problem may have been caused by a bug in earlier "
774 "versions of the filer).\n\n"
775 "Having (non-symlink) run actions world-writeable "
776 "means that other people who use your computer can "
777 "replace your run actions with malicious versions.\n\n"
778 "If you trust everyone who could write to these files "
779 "then you needn't worry. Otherwise, you should check, "
780 "or even just delete, all the existing run actions."),
781 open);
782 choices_dir = g_path_get_dirname(open);
783 paths = g_list_append(NULL, choices_dir);
784 action_chmod(paths, TRUE, _("go-w (Fix security problem)"));
785 g_free(choices_dir);
786 g_list_free(paths);
787 g_free(open);
788 return TRUE;
791 if (S_ISDIR(info.st_mode))
793 argv[0] = g_strconcat(open, "/AppRun", NULL);
794 rox_spawn(home_dir, (const gchar **) argv);
796 else if (type_get_type(open) == application_x_desktop)
798 argv[0] = open;
799 run_desktop(open, (const char **) (argv + 1), home_dir);
801 else
803 argv[0] = open;
804 rox_spawn(home_dir, (const gchar **) argv);
807 if (argv[0] != open)
808 g_free(argv[0]);
810 g_free(open);
812 return TRUE;