4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2003, the ROX-Filer team.
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19 * Place, Suite 330, Boston, MA 02111-1307 USA
22 /* action.c - code for handling the filer action windows.
23 * These routines generally fork() and talk to us via pipes.
31 #include <sys/param.h>
44 #include "gui_support.h"
49 #include "modechange.h"
55 /* Parent->Child messages are one character each:
57 * Y/N Yes/No button clicked
58 * F Force deletion of non-writeable items
60 * E Entry text changed
64 typedef struct _GUIside GUIside
;
65 typedef void ActionChild(gpointer data
);
66 typedef void ForDirCB(const char *path
, const char *dest_path
);
70 ABox
*abox
; /* The action window widget */
72 int from_child
; /* File descriptor */
74 int input_tag
; /* gdk_input_add() */
75 pid_t child
; /* Process ID */
76 int errors
; /* Number of errors so far */
77 gboolean show_info
; /* For Disk Usage */
79 guchar
**default_string
; /* Changed when the entry changes */
80 void (*entry_string_func
)(GtkWidget
*widget
,
81 const guchar
*string
);
86 /* These don't need to be in a structure because we fork() before
89 static gboolean mount_open_dir
= FALSE
;
90 static int from_parent
= 0;
91 static FILE *to_parent
= NULL
;
92 static gboolean quiet
= FALSE
;
93 static GString
*message
= NULL
;
94 static const char *action_dest
= NULL
;
95 static const char *action_leaf
= NULL
;
96 static void (*action_do_func
)(const char *source
, const char *dest
);
97 static double size_tally
; /* For Disk Usage */
98 static unsigned long dir_counter
; /* For Disk Usage */
99 static unsigned long file_counter
; /* For Disk Usage */
101 static struct mode_change
*mode_change
= NULL
; /* For Permissions */
102 static FindCondition
*find_condition
= NULL
; /* For Find */
104 /* Only used by child */
105 static gboolean o_force
= FALSE
;
106 static gboolean o_brief
= FALSE
;
107 static gboolean o_recurse
= FALSE
;
108 static gboolean o_newer
= FALSE
;
110 static Option o_action_copy
, o_action_move
, o_action_link
;
111 static Option o_action_delete
, o_action_mount
;
112 static Option o_action_force
, o_action_brief
, o_action_recurse
;
113 static Option o_action_newer
;
115 /* Whenever the text in these boxes is changed we store a copy of the new
116 * string to be used as the default next time.
118 static guchar
*last_chmod_string
= NULL
;
119 static guchar
*last_find_string
= NULL
;
121 /* Set to one of the above before forking. This may change over a call to
122 * reply(). It is reset to NULL once the text is parsed.
124 static guchar
*new_entry_string
= NULL
;
126 /* Static prototypes */
127 static void send_done(void);
128 static void send_check_path(const gchar
*path
);
129 static void send_mount_path(const gchar
*path
);
130 static gboolean
printf_send(const char *msg
, ...);
131 static gboolean
send(void);
132 static gboolean
send_error(void);
133 static gboolean
send_dir(const char *dir
);
134 static gboolean
read_exact(int source
, char *buffer
, ssize_t len
);
135 static void do_mount(const guchar
*path
, gboolean mount
);
136 static gboolean
printf_reply(int fd
, gboolean ignore_quiet
,
137 const char *msg
, ...);
138 static gboolean
remove_pinned_ok(GList
*paths
);
143 /* This is called whenever the user edits the entry box (if any) - send the
146 static void entry_changed(GtkEditable
*entry
, GUIside
*gui_side
)
150 g_return_if_fail(gui_side
->default_string
!= NULL
);
152 text
= gtk_editable_get_chars(entry
, 0, -1);
154 if (gui_side
->entry_string_func
)
155 gui_side
->entry_string_func(GTK_WIDGET(entry
), text
);
157 g_free(*(gui_side
->default_string
));
158 *(gui_side
->default_string
) = text
; /* Gets text's ref */
160 if (!gui_side
->to_child
)
163 fputc('E', gui_side
->to_child
);
164 fputs(text
, gui_side
->to_child
);
165 fputc('\n', gui_side
->to_child
);
166 fflush(gui_side
->to_child
);
169 void show_condition_help(gpointer data
)
174 help
= gtk_dialog_new_with_buttons(
175 _("Find expression reference"),
177 GTK_STOCK_CLOSE
, GTK_RESPONSE_CANCEL
,
179 gtk_dialog_set_default_response(GTK_DIALOG(help
), GTK_RESPONSE_CANCEL
);
181 text
= gtk_label_new(NULL
);
182 gtk_misc_set_padding(GTK_MISC(text
), 2, 2);
183 gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(help
)->vbox
), text
);
184 gtk_label_set_selectable(GTK_LABEL(text
), TRUE
);
185 gtk_label_set_markup(GTK_LABEL(text
), _(
186 "<u>Quick Start</u>\n"
187 "Just put the name of the file you're looking for in single quotes:\n"
188 "<b>'index.html'</b> (to find a file called 'index.html')\n"
191 "<b>'*.htm', '*.html'</b> (finds HTML files)\n"
192 "<b>IsDir 'lib'</b> (finds directories called 'lib')\n"
193 "<b>IsReg 'core'</b> (finds a regular file called 'core')\n"
194 "<b>! (IsDir, IsReg)</b> (is neither a directory nor a regular file)\n"
195 "<b>mtime after 1 day ago and size > 1Mb</b> (big, and recently modified)\n"
196 "<b>'CVS' prune, isreg</b> (a regular file not in CVS)\n"
197 "<b>IsReg system(grep -q fred \"%\")</b> (contains the word 'fred')\n"
199 "<u>Simple Tests</u>\n"
200 "<b>IsReg, IsLink, IsDir, IsChar, IsBlock, IsDev, IsPipe, IsSocket, IsDoor</b> "
202 "<b>IsSUID, IsSGID, IsSticky, IsReadable, IsWriteable, IsExecutable</b> "
204 "<b>IsEmpty, IsMine</b>\n"
205 "A pattern in single quotes is a shell-style wildcard pattern to match. If it\n"
206 "contains a slash then the match is against the full path; otherwise it is\n"
207 "against the leafname only.\n"
209 "<u>Comparisons</u>\n"
210 "<b><, <=, =, !=, >, >=, After, Before</b> (compare two values)\n"
211 "<b>5 bytes, 1Kb, 2Mb, 3Gb</b> (file sizes)\n"
212 "<b>2 secs|mins|hours|days|weeks|years ago|hence</b> (times)\n"
213 "<b>atime, ctime, mtime, now, size, inode, nlinks, uid, gid, blocks</b> "
217 "<b>system(command)</b> (true if 'command' returns with a zero exit status;\n"
218 "a % in 'command' is replaced with the path of the current file)\n"
219 "<b>prune</b> (false, and prevents searching the contents of a directory)."));
221 g_signal_connect(help
, "response",
222 G_CALLBACK(gtk_widget_destroy
), NULL
);
224 gtk_widget_show_all(help
);
227 static void show_chmod_help(gpointer data
)
232 help
= gtk_dialog_new_with_buttons(
233 _("Find expression reference"),
235 GTK_STOCK_CLOSE
, GTK_RESPONSE_CANCEL
,
237 gtk_dialog_set_default_response(GTK_DIALOG(help
), GTK_RESPONSE_CANCEL
);
239 text
= gtk_label_new(NULL
);
240 gtk_misc_set_padding(GTK_MISC(text
), 2, 2);
241 gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(help
)->vbox
), text
);
242 gtk_label_set_selectable(GTK_LABEL(text
), TRUE
);
243 gtk_label_set_markup(GTK_LABEL(text
), _(
244 "Normally, you can just select a command from the menu (click \n"
245 "on the arrow beside the command box). Sometimes, you need more...\n"
247 "The format of a command is: <b>CHANGE, CHANGE, ...</b>\n"
248 "Each <b>CHANGE</b> is: <b>WHO HOW PERMISSIONS</b>\n"
249 "<b>WHO</b> is some combination of <b>u</b>, <b>g</b> and <b>o</b> which "
250 "determines whether to\n"
251 "change the permissions for the User (owner), Group or Others.\n"
252 "<b>HOW</b> is <b>+</b>, <b>-</b> or <b>=</b> to add, remove or set "
253 "exactly the permissions.\n"
254 "<b>PERMISSIONS</b> is some combination of the letters <b>rwxXstugo</b>\n"
256 "Bracketed text and spaces are ignored.\n"
259 "<b>u+rw</b>: the file owner gains read and write permission\n"
260 "<b>g=u</b>: the group permissions are set to be the same as the user's\n"
261 "<b>o=u-w</b>: others get the same permissions as the owner, but without "
263 "<b>a+x</b>: <b>a</b>ll get execute/access permission - same as <b>ugo+x</b>\n"
264 "<b>a+X</b>: directories become accessable by everyone; files which were\n"
265 "executable by anyone become executable by everyone\n"
266 "<b>u+rw, go+r</b>: two commands at once!\n"
267 "<b>u+s</b>: set the SetUID bit - often has no effect on script files\n"
268 "<b>755</b>: set the permissions directly\n"
270 "See the chmod(1) man page for full details."));
272 g_signal_connect(help
, "response",
273 G_CALLBACK(gtk_widget_destroy
), NULL
);
275 gtk_widget_show_all(help
);
278 static void process_message(GUIside
*gui_side
, const gchar
*buffer
)
280 ABox
*abox
= gui_side
->abox
;
283 abox_ask(abox
, buffer
+ 1);
284 else if (*buffer
== 's')
285 dir_check_this(buffer
+ 1); /* Update this item */
286 else if (*buffer
== '=')
287 abox_add_filename(abox
, buffer
+ 1);
288 else if (*buffer
== '#')
289 abox_clear_results(abox
);
290 else if (*buffer
== 'X')
292 filer_close_recursive(buffer
+ 1);
293 /* Let child know it's safe to continue... */
294 fputc('X', gui_side
->to_child
);
295 fflush(gui_side
->to_child
);
297 else if (*buffer
== 'm' || *buffer
== 'M')
299 /* Mount / major changes to this path */
303 mount_user_mount(buffer
+ 1);
305 filer_check_mounted(buffer
+ 1);
307 else if (*buffer
== '/')
308 abox_set_current_object(abox
, buffer
+ 1);
309 else if (*buffer
== 'o')
310 filer_opendir(buffer
+ 1, NULL
, NULL
);
311 else if (*buffer
== '!')
314 abox_log(abox
, buffer
+ 1, "error");
317 abox_log(abox
, buffer
+ 1, NULL
);
320 /* Called when the child sends us a message */
321 static void message_from_child(gpointer data
,
323 GdkInputCondition condition
)
326 GUIside
*gui_side
= (GUIside
*) data
;
327 ABox
*abox
= gui_side
->abox
;
328 GtkTextBuffer
*text_buffer
;
330 text_buffer
= gtk_text_view_get_buffer(GTK_TEXT_VIEW(abox
->log
));
332 if (read_exact(source
, buf
, 4))
338 message_len
= strtol(buf
, NULL
, 16);
339 buffer
= g_malloc(message_len
+ 1);
340 if (message_len
> 0 && read_exact(source
, buffer
, message_len
))
342 buffer
[message_len
] = '\0';
343 process_message(gui_side
, buffer
);
347 g_printerr("Child died in the middle of a message.\n");
350 if (gui_side
->abort_attempts
)
351 abox_log(abox
, _("\nProcess terminated.\n"), "error");
353 /* The child is dead */
356 fclose(gui_side
->to_child
);
357 gui_side
->to_child
= NULL
;
358 close(gui_side
->from_child
);
359 g_source_remove(gui_side
->input_tag
);
360 abox_cancel_ask(gui_side
->abox
);
362 if (gui_side
->errors
)
366 if (gui_side
->errors
== 1)
367 report
= g_strdup(_("There was one error.\n"));
369 report
= g_strdup_printf(_("There were %d errors.\n"),
372 gtk_text_buffer_insert_at_cursor(text_buffer
, report
, -1);
376 else if (gui_side
->show_info
== FALSE
)
377 gtk_widget_destroy(GTK_WIDGET(gui_side
->abox
));
380 /* Scans src_dir, calling cb(item, dest_path) for each item */
381 static void for_dir_contents(ForDirCB
*cb
,
383 const char *dest_path
)
387 GList
*list
= NULL
, *next
;
389 d
= mc_opendir(src_dir
);
392 /* Message displayed is "ERROR reading 'path': message" */
393 printf_send("!%s '%s': %s\n", _("ERROR reading"),
394 src_dir
, g_strerror(errno
));
400 while ((ent
= mc_readdir(d
)))
402 if (ent
->d_name
[0] == '.' && (ent
->d_name
[1] == '\0'
403 || (ent
->d_name
[1] == '.' && ent
->d_name
[2] == '\0')))
405 list
= g_list_prepend(list
, g_strdup(make_path(src_dir
,
410 for (next
= list
; next
; next
= next
->next
)
412 cb((char *) next
->data
, dest_path
);
419 /* Read this many bytes into the buffer. TRUE on success. */
420 static gboolean
read_exact(int source
, char *buffer
, ssize_t len
)
425 got
= read(source
, buffer
, len
);
434 static void send_done(void)
436 printf_send(_("'\nDone\n"));
439 /* Notify the filer that this item has been updated */
440 static void send_check_path(const gchar
*path
)
442 printf_send("s%s", path
);
445 /* Notify the filer that this whole subtree has changed (eg, been unmounted) */
446 static void send_mount_path(const gchar
*path
)
448 printf_send("m%s", path
);
451 /* Send a message to the filer process. The first character indicates the
452 * type of the message.
454 static gboolean
printf_send(const char *msg
, ...)
460 tmp
= g_strdup_vprintf(msg
, args
);
463 g_string_assign(message
, tmp
);
469 /* Send 'message' to our parent process. TRUE on success. */
470 static gboolean
send(void)
475 g_return_val_if_fail(message
->len
< 0xffff, FALSE
);
477 sprintf(len_buffer
, "%04x", message
->len
);
478 fwrite(len_buffer
, 1, 4, to_parent
);
479 len
= fwrite(message
->str
, 1, message
->len
, to_parent
);
481 return len
== (ssize_t
) message
->len
;
484 /* Set the directory indicator at the top of the window */
485 static gboolean
send_dir(const char *dir
)
487 return printf_send("/%s", dir
);
490 static gboolean
send_error(void)
492 return printf_send("!%s: %s\n", _("ERROR"), g_strerror(errno
));
495 static void response(GtkDialog
*dialog
, gint response
, GUIside
*gui_side
)
499 if (!gui_side
->to_child
)
502 if (response
== GTK_RESPONSE_YES
)
504 else if (response
== GTK_RESPONSE_NO
)
509 fputc(code
, gui_side
->to_child
);
510 fflush(gui_side
->to_child
);
513 static void flag_toggled(ABox
*abox
, gint flag
, GUIside
*gui_side
)
515 if (!gui_side
->to_child
)
518 fputc(flag
, gui_side
->to_child
);
519 fflush(gui_side
->to_child
);
522 static void read_new_entry_text(void)
528 new = g_string_new(NULL
);
532 len
= read(from_parent
, &c
, 1);
535 fprintf(stderr
, "read() error: %s\n",
537 _exit(1); /* Parent died? */
542 g_string_append_c(new, c
);
545 g_free(new_entry_string
);
546 new_entry_string
= new->str
;
547 g_string_free(new, FALSE
);
550 static void process_flag(char flag
)
561 o_recurse
= !o_recurse
;
570 read_new_entry_text();
573 printf_send("!ERROR: Bad message '%c'\n", flag
);
578 /* If the parent has sent any flag toggles, read them */
579 static void check_flags(void)
590 FD_SET(from_parent
, &set
);
593 got
= select(from_parent
+ 1, &set
, NULL
, NULL
, &tv
);
596 g_error("select() failed: %s\n", g_strerror(errno
));
600 got
= read(from_parent
, &retval
, 1);
602 g_error("read() error: %s\n", g_strerror(errno
));
604 process_flag(retval
);
608 /* Read until the user sends a reply. If ignore_quiet is TRUE then
609 * the user MUST click Yes or No, else treat quiet on as Yes.
610 * If the user needs prompting then does send().
612 static gboolean
printf_reply(int fd
, gboolean ignore_quiet
,
613 const char *msg
, ...)
620 if (quiet
&& !ignore_quiet
)
624 tmp
= g_strdup_vprintf(msg
, args
);
627 g_string_assign(message
, tmp
);
634 len
= read(fd
, &retval
, 1);
637 fprintf(stderr
, "read() error: %s\n",
639 _exit(1); /* Parent died? */
645 printf_send("' %s\n", _("Yes"));
648 printf_send("' %s\n", _("No"));
651 process_flag(retval
);
657 static void abort_operation(GtkWidget
*widget
, gpointer data
)
659 GUIside
*gui_side
= (GUIside
*) data
;
663 if (gui_side
->abort_attempts
== 0)
665 abox_log(ABOX(widget
),
666 _("\nAsking child process to terminate...\n"),
668 kill(-gui_side
->child
, SIGTERM
);
672 abox_log(ABOX(widget
),
673 _("\nTrying to KILL run-away process...\n"),
675 kill(-gui_side
->child
, SIGKILL
);
676 kill(-gui_side
->child
, SIGCONT
);
678 gui_side
->abort_attempts
++;
681 gtk_widget_destroy(widget
);
684 static void destroy_action_window(GtkWidget
*widget
, gpointer data
)
686 GUIside
*gui_side
= (GUIside
*) data
;
690 kill(-gui_side
->child
, SIGTERM
);
691 fclose(gui_side
->to_child
);
692 close(gui_side
->from_child
);
693 g_source_remove(gui_side
->input_tag
);
701 /* Create two pipes, fork() a child and return a pointer to a GUIside struct
702 * (NULL on failure). The child calls func().
704 static GUIside
*start_action(GtkWidget
*abox
, ActionChild
*func
, gpointer data
,
705 int force
, int brief
, int recurse
, int newer
)
708 int filedes
[4]; /* 0 and 2 are for reading */
711 struct sigaction act
;
715 report_error("pipe: %s", g_strerror(errno
));
716 gtk_widget_destroy(abox
);
720 if (pipe(filedes
+ 2))
724 report_error("pipe: %s", g_strerror(errno
));
725 gtk_widget_destroy(abox
);
729 autoq
= gtk_toggle_button_get_active(
730 GTK_TOGGLE_BUTTON(ABOX(abox
)->quiet
));
741 report_error("fork: %s", g_strerror(errno
));
742 gtk_widget_destroy(abox
);
745 /* We are the child */
747 /* Create a new process group */
752 dir_drop_all_dnotifies();
754 /* Reset the SIGCHLD handler */
755 act
.sa_handler
= SIG_DFL
;
756 sigemptyset(&act
.sa_mask
);
758 sigaction(SIGCHLD
, &act
, NULL
);
760 message
= g_string_new(NULL
);
763 to_parent
= fdopen(filedes
[1], "wb");
764 from_parent
= filedes
[2];
770 /* We are the parent */
773 gui_side
= g_new(GUIside
, 1);
774 gui_side
->from_child
= filedes
[0];
775 gui_side
->to_child
= fdopen(filedes
[3], "wb");
776 gui_side
->child
= child
;
777 gui_side
->errors
= 0;
778 gui_side
->show_info
= FALSE
;
779 gui_side
->default_string
= NULL
;
780 gui_side
->entry_string_func
= NULL
;
781 gui_side
->abort_attempts
= 0;
783 gui_side
->abox
= ABOX(abox
);
784 g_signal_connect(abox
, "destroy",
785 G_CALLBACK(destroy_action_window
), gui_side
);
787 g_signal_connect(abox
, "response", G_CALLBACK(response
), gui_side
);
788 g_signal_connect(abox
, "flag_toggled",
789 G_CALLBACK(flag_toggled
), gui_side
);
790 g_signal_connect(abox
, "abort_operation",
791 G_CALLBACK(abort_operation
), gui_side
);
793 gui_side
->input_tag
= gtk_input_add_full(gui_side
->from_child
,
796 NULL
, gui_side
, NULL
);
801 /* ACTIONS ON ONE ITEM */
803 /* These may call themselves recursively, or ask questions, etc */
805 /* Updates the global size_tally, file_counter and dir_counter */
806 static void do_usage(const char *src_path
, const char *unused
)
812 if (mc_lstat(src_path
, &info
))
814 printf_send("'%s:\n", src_path
);
817 else if (S_ISREG(info
.st_mode
) || S_ISLNK(info
.st_mode
))
820 size_tally
+= info
.st_size
;
822 else if (S_ISDIR(info
.st_mode
))
825 if (printf_reply(from_parent
, FALSE
,
826 _("?Count contents of %s?"), src_path
))
829 safe_path
= g_strdup(src_path
);
830 for_dir_contents(do_usage
, safe_path
, safe_path
);
838 /* dest_path is the dir containing src_path */
839 static void do_delete(const char *src_path
, const char *unused
)
847 if (mc_lstat(src_path
, &info
))
853 write_prot
= S_ISLNK(info
.st_mode
) ? FALSE
854 : access(src_path
, W_OK
) != 0;
855 if (write_prot
|| !quiet
)
857 if (!printf_reply(from_parent
, write_prot
&& !o_force
,
858 _("?Delete %s'%s'?"),
859 write_prot
? _("WRITE-PROTECTED ") : "",
864 printf_send(_("'Deleting '%s'\n"), src_path
);
866 safe_path
= g_strdup(src_path
);
868 if (S_ISDIR(info
.st_mode
))
870 for_dir_contents(do_delete
, safe_path
, safe_path
);
871 if (rmdir(safe_path
))
877 printf_send(_("'Directory '%s' deleted\n"), safe_path
);
878 send_mount_path(safe_path
);
880 else if (unlink(src_path
))
884 send_check_path(safe_path
);
885 if (strcmp(g_basename(safe_path
), ".DirIcon") == 0)
888 dir
= g_path_get_dirname(safe_path
);
889 send_check_path(dir
);
897 /* path is the item to check. If is is a directory then we may recurse
898 * (unless prune is used).
900 static void do_find(const char *path
, const char *unused
)
908 if (!printf_reply(from_parent
, FALSE
, _("?Check '%s'?"), path
))
914 if (new_entry_string
)
916 find_condition_free(find_condition
);
917 find_condition
= find_compile(new_entry_string
);
918 null_g_free(&new_entry_string
);
924 printf_send(_("!Invalid find condition - "
925 "change it and try again\n"));
926 if (!printf_reply(from_parent
, TRUE
,
927 _("?Check '%s'?"), path
))
931 if (mc_lstat(path
, &info
.stats
))
934 printf_send(_("'(while checking '%s')\n"), path
);
938 info
.fullpath
= path
;
939 time(&info
.now
); /* XXX: Not for each check! */
941 info
.leaf
= g_basename(path
);
943 if (find_test_condition(find_condition
, &info
))
944 printf_send("=%s", path
);
946 if (S_ISDIR(info
.stats
.st_mode
) && !info
.prune
)
949 safe_path
= g_strdup(path
);
950 for_dir_contents(do_find
, safe_path
, safe_path
);
955 /* Like mode_compile(), but ignores spaces and bracketed bits */
956 static struct mode_change
*nice_mode_compile(const char *mode_string
,
957 unsigned int masked_ops
)
961 struct mode_change
*retval
= NULL
;
963 new = g_string_new(NULL
);
965 for (; *mode_string
; mode_string
++)
967 if (*mode_string
== '(')
969 if (*mode_string
== ')')
977 if (brackets
== 0 && *mode_string
!= ' ')
978 g_string_append_c(new, *mode_string
);
982 retval
= mode_compile(new->str
, masked_ops
);
983 g_string_free(new, TRUE
);
987 static void do_chmod(const char *path
, const char *unused
)
994 if (mc_lstat(path
, &info
))
999 if (S_ISLNK(info
.st_mode
))
1004 if (!printf_reply(from_parent
, FALSE
,
1005 _("?Change permissions of '%s'?"), path
))
1009 printf_send(_("'Changing permissions of '%s'\n"), path
);
1013 if (new_entry_string
)
1016 mode_free(mode_change
);
1017 mode_change
= nice_mode_compile(new_entry_string
,
1019 null_g_free(&new_entry_string
);
1026 _("!Invalid mode command - change it and try again\n"));
1027 if (!printf_reply(from_parent
, TRUE
,
1028 _("?Change permissions of '%s'?"), path
))
1032 if (mc_lstat(path
, &info
))
1037 if (S_ISLNK(info
.st_mode
))
1040 new_mode
= mode_adjust(info
.st_mode
, mode_change
);
1041 if (chmod(path
, new_mode
))
1047 send_check_path(path
);
1049 if (S_ISDIR(info
.st_mode
))
1051 send_mount_path(path
);
1056 safe_path
= g_strdup(path
);
1057 for_dir_contents(do_chmod
, safe_path
, safe_path
);
1063 /* We want to copy 'object' into directory 'dir'. If 'action_leaf'
1064 * is set then that is the new leafname, otherwise the leafname stays
1067 static const char *make_dest_path(const char *object
, const char *dir
)
1075 leaf
= strrchr(object
, '/');
1077 leaf
= object
; /* Error? */
1082 return make_path(dir
, leaf
);
1085 /* If action_leaf is not NULL it specifies the new leaf name */
1086 static void do_copy2(const char *path
, const char *dest
)
1088 const char *dest_path
;
1090 struct stat dest_info
;
1094 dest_path
= make_dest_path(path
, dest
);
1096 if (mc_lstat(path
, &info
))
1102 if (mc_lstat(dest_path
, &dest_info
) == 0)
1107 merge
= S_ISDIR(info
.st_mode
) && S_ISDIR(dest_info
.st_mode
);
1109 if (!merge
&& o_newer
&& info
.st_mtime
> dest_info
.st_mtime
)
1111 /* Newer; keep going */
1115 if (!printf_reply(from_parent
, TRUE
,
1116 _("?'%s' already exists - %s?"),
1118 merge
? _("merge contents")
1125 if (S_ISDIR(dest_info
.st_mode
))
1126 err
= rmdir(dest_path
);
1128 err
= unlink(dest_path
);
1133 if (errno
!= ENOENT
)
1135 printf_send(_("'Trying copy anyway...\n"));
1141 if (!printf_reply(from_parent
, FALSE
,
1142 _("?Copy %s as %s?"), path
, dest_path
))
1146 printf_send(_("'Copying %s as %s\n"), path
, dest_path
);
1148 if (S_ISDIR(info
.st_mode
))
1150 mode_t mode
= info
.st_mode
;
1151 char *safe_path
, *safe_dest
;
1152 struct stat dest_info
;
1155 safe_path
= g_strdup(path
);
1156 safe_dest
= g_strdup(dest_path
);
1158 exists
= !mc_lstat(dest_path
, &dest_info
);
1160 if (exists
&& !S_ISDIR(dest_info
.st_mode
))
1161 printf_send(_("!ERROR: Destination already exists, "
1162 "but is not a directory\n"));
1163 else if (exists
== FALSE
&& mkdir(dest_path
, 0700 | mode
))
1168 /* (just been created then) */
1169 send_check_path(dest_path
);
1172 for_dir_contents(do_copy2
, safe_path
, safe_dest
);
1173 /* Note: dest_path now invalid... */
1179 /* We may have created the directory with
1180 * more permissions than the source so that
1181 * we could write to it... change it back now.
1183 if (chmod(safe_dest
, mode
))
1185 /* Some filesystems don't support
1186 * SetGID and SetUID bits. Ignore
1193 /* Also, try to preserve the timestamps */
1194 utb
.actime
= info
.st_atime
;
1195 utb
.modtime
= info
.st_mtime
;
1197 utime(safe_dest
, &utb
);
1204 else if (S_ISLNK(info
.st_mode
))
1208 /* Not all versions of cp(1) can make symlinks,
1209 * so we special-case it.
1212 target
= readlink_dup(path
);
1215 if (symlink(target
, dest_path
))
1218 send_check_path(dest_path
);
1229 error
= copy_file(path
, dest_path
);
1233 printf_send(_("!%s\nFailed to copy '%s'\n"),
1238 send_check_path(dest_path
);
1242 /* If action_leaf is not NULL it specifies the new leaf name */
1243 static void do_move2(const char *path
, const char *dest
)
1245 const char *dest_path
;
1246 const char *argv
[] = {"mv", "-f", NULL
, NULL
, NULL
};
1253 dest_path
= make_dest_path(path
, dest
);
1255 is_dir
= mc_lstat(path
, &info2
) == 0 && S_ISDIR(info2
.st_mode
);
1257 if (access(dest_path
, F_OK
) == 0)
1262 if (mc_lstat(dest_path
, &info
))
1268 if (!is_dir
&& o_newer
&& info2
.st_mtime
> info
.st_mtime
)
1270 /* Newer; keep going */
1272 else if (!printf_reply(from_parent
, TRUE
,
1273 _("?'%s' already exists - overwrite?"),
1277 if (S_ISDIR(info
.st_mode
))
1278 err
= rmdir(dest_path
);
1280 err
= unlink(dest_path
);
1285 if (errno
!= ENOENT
)
1287 printf_send(_("'Trying move anyway...\n"));
1292 if (!printf_reply(from_parent
, FALSE
,
1293 _("?Move %s as %s?"), path
, dest_path
))
1297 printf_send(_("'Moving %s as %s\n"), path
, dest_path
);
1300 argv
[3] = dest_path
;
1302 err
= fork_exec_wait(argv
);
1305 printf_send(_("!%s\nFailed to move %s as %s\n"),
1306 err
, path
, dest_path
);
1311 send_check_path(dest_path
);
1314 send_mount_path(path
);
1316 send_check_path(path
);
1320 /* Copy path to dest.
1321 * Check that path not copied into itself.
1323 static void do_copy(const char *path
, const char *dest
)
1325 if (is_sub_dir(make_dest_path(path
, dest
), path
))
1326 printf_send(_("!ERROR: Can't copy object into itself\n"));
1329 do_copy2(path
, dest
);
1330 send_check_path(dest
);
1334 /* Move path to dest.
1335 * Check that path not moved into itself.
1337 static void do_move(const char *path
, const char *dest
)
1339 if (is_sub_dir(make_dest_path(path
, dest
), path
))
1341 _("!ERROR: Can't move/rename object into itself\n"));
1344 do_move2(path
, dest
);
1345 send_check_path(dest
);
1349 static void do_link(const char *path
, const char *dest
)
1351 const char *dest_path
;
1355 dest_path
= make_dest_path(path
, dest
);
1358 printf_send(_("'Linking %s as %s\n"), path
, dest_path
);
1359 else if (!printf_reply(from_parent
, FALSE
,
1360 _("?Link %s as %s?"), path
, dest_path
))
1363 if (symlink(path
, dest_path
))
1366 send_check_path(dest_path
);
1369 /* Mount/umount this item (depending on 'mount') */
1370 static void do_mount(const guchar
*path
, gboolean mount
)
1372 const char *argv
[3] = {NULL
, NULL
, NULL
};
1377 argv
[0] = mount
? "mount" : "umount";
1381 printf_send(mount
? _("'Mounting %s\n")
1382 : _("'Unmounting %s\n"),
1384 else if (!printf_reply(from_parent
, FALSE
,
1385 mount
? _("?Mount %s?")
1386 : _("?Unmount %s?"),
1393 /* Need to close all sub-directories now, or we
1394 * can't unmount if dnotify is used.
1396 printf_send("X%s", path
);
1397 /* Wait until it's safe... */
1398 read(from_parent
, &c
, 1);
1399 g_return_if_fail(c
== 'X');
1402 err
= fork_exec_wait(argv
);
1406 _("!%s\nMount failed\n") :
1407 _("!%s\nUnmount failed\n"), err
);
1412 printf_send("M%s", path
);
1413 if (mount
&& mount_open_dir
)
1414 printf_send("o%s", path
);
1418 /* CHILD MAIN LOOPS */
1420 /* After forking, the child calls one of these functions */
1422 /* We use a double for total size in order to count beyond 4Gb */
1423 static void usage_cb(gpointer data
)
1425 GList
*paths
= (GList
*) data
;
1426 double total_size
= 0;
1428 dir_counter
= file_counter
= 0;
1430 for (; paths
; paths
= paths
->next
)
1432 guchar
*path
= (guchar
*) paths
->data
;
1438 do_usage(path
, NULL
);
1440 printf_send("'%s: %s\n",
1442 format_double_size(size_tally
));
1443 total_size
+= size_tally
;
1446 g_string_printf(message
, _("'\nTotal: %s ("),
1447 format_double_size(total_size
));
1450 g_string_append_printf(message
,
1451 "%ld %s%s", file_counter
,
1452 file_counter
== 1 ? _("file") : _("files"),
1453 dir_counter
? ", " : ")\n");
1455 if (file_counter
== 0 && dir_counter
== 0)
1456 g_string_append(message
, _("no directories)\n"));
1457 else if (dir_counter
)
1458 g_string_append_printf(message
,
1459 "%ld %s)\n", dir_counter
,
1460 dir_counter
== 1 ? _("directory")
1461 : _("directories"));
1466 #ifdef DO_MOUNT_POINTS
1467 static void mount_cb(gpointer data
)
1469 GList
*paths
= (GList
*) data
;
1470 gboolean mount_points
= FALSE
;
1472 for (; paths
; paths
= paths
->next
)
1474 guchar
*path
= (guchar
*) paths
->data
;
1477 target
= readlink_dup(path
);
1481 if (mount_is_mounted(target
, NULL
, NULL
))
1483 mount_points
= TRUE
;
1484 do_mount(target
, FALSE
); /* Unmount */
1486 else if (g_hash_table_lookup(fstab_mounts
, target
))
1488 mount_points
= TRUE
;
1489 do_mount(target
, TRUE
); /* Mount */
1499 printf_send(_("!No mount points selected!\n"));
1503 /* (use g_dirname() instead?) */
1504 static guchar
*dirname(guchar
*path
)
1508 slash
= strrchr(path
, '/');
1509 g_return_val_if_fail(slash
!= NULL
, g_strdup(path
));
1512 return g_strndup(path
, slash
- path
);
1513 return g_strdup("/");
1516 static void delete_cb(gpointer data
)
1518 GList
*paths
= (GList
*) data
;
1520 for (; paths
; paths
= paths
->next
)
1522 guchar
*path
= (guchar
*) paths
->data
;
1525 dir
= dirname(path
);
1528 do_delete(path
, dir
);
1536 static void find_cb(gpointer data
)
1538 GList
*all_paths
= (GList
*) data
;
1543 for (paths
= all_paths
; paths
; paths
= paths
->next
)
1545 guchar
*path
= (guchar
*) paths
->data
;
1549 do_find(path
, NULL
);
1552 if (!printf_reply(from_parent
, TRUE
,
1553 _("?Another search?")))
1561 static void chmod_cb(gpointer data
)
1563 GList
*paths
= (GList
*) data
;
1565 for (; paths
; paths
= paths
->next
)
1567 guchar
*path
= (guchar
*) paths
->data
;
1572 if (mc_stat(path
, &info
) != 0)
1574 else if (S_ISLNK(info
.st_mode
))
1575 printf_send(_("!'%s' is a symbolic link\n"),
1578 do_chmod(path
, NULL
);
1584 static void list_cb(gpointer data
)
1586 GList
*paths
= (GList
*) data
;
1588 for (; paths
; paths
= paths
->next
)
1590 send_dir((char *) paths
->data
);
1592 action_do_func((char *) paths
->data
, action_dest
);
1598 /* EXTERNAL INTERFACE */
1600 void action_find(GList
*paths
)
1607 report_error(_("You need to select some items "
1608 "to search through"));
1612 if (!last_find_string
)
1613 last_find_string
= g_strdup("'core'");
1615 new_entry_string
= last_find_string
;
1617 abox
= abox_new(_("Find"), FALSE
);
1618 gui_side
= start_action(abox
, find_cb
, paths
,
1619 o_action_force
.int_value
,
1620 o_action_brief
.int_value
,
1621 o_action_recurse
.int_value
,
1622 o_action_newer
.int_value
);
1626 abox_add_results(ABOX(abox
));
1628 gui_side
->default_string
= &last_find_string
;
1629 abox_add_entry(ABOX(abox
), last_find_string
,
1630 new_help_button(show_condition_help
, NULL
));
1631 g_signal_connect(ABOX(abox
)->entry
, "changed",
1632 G_CALLBACK(entry_changed
), gui_side
);
1633 set_find_string_colour(ABOX(abox
)->entry
, last_find_string
);
1635 gui_side
->show_info
= TRUE
;
1636 gui_side
->entry_string_func
= set_find_string_colour
;
1638 number_of_windows
++;
1639 gtk_widget_show_all(abox
);
1642 /* Count disk space used by selected items */
1643 void action_usage(GList
*paths
)
1650 report_error(_("You need to select some items to count"));
1654 abox
= abox_new(_("Disk Usage"), TRUE
);
1656 gui_side
= start_action(abox
, usage_cb
, paths
,
1657 o_action_force
.int_value
,
1658 o_action_brief
.int_value
,
1659 o_action_recurse
.int_value
,
1660 o_action_newer
.int_value
);
1664 gui_side
->show_info
= TRUE
;
1666 number_of_windows
++;
1668 gtk_widget_show(abox
);
1671 /* Mount/unmount listed items (paths).
1672 * Free the list after this function returns.
1673 * If open_dir is TRUE and the dir is successfully mounted, open it.
1674 * quiet can be -1 for default.
1676 void action_mount(GList
*paths
, gboolean open_dir
, int quiet
)
1678 #ifdef DO_MOUNT_POINTS
1683 quiet
= o_action_mount
.int_value
;
1685 mount_open_dir
= open_dir
;
1687 abox
= abox_new(_("Mount / Unmount"), quiet
);
1688 gui_side
= start_action(abox
, mount_cb
, paths
,
1689 o_action_force
.int_value
,
1690 o_action_brief
.int_value
,
1691 o_action_recurse
.int_value
,
1692 o_action_newer
.int_value
);
1696 number_of_windows
++;
1697 gtk_widget_show(abox
);
1700 _("ROX-Filer does not yet support mount points on your "
1702 #endif /* DO_MOUNT_POINTS */
1705 /* Delete these paths */
1706 void action_delete(GList
*paths
)
1711 if (!remove_pinned_ok(paths
))
1714 abox
= abox_new(_("Delete"), o_action_delete
.int_value
);
1715 gui_side
= start_action(abox
, delete_cb
, paths
,
1716 o_action_force
.int_value
,
1717 o_action_brief
.int_value
,
1718 o_action_recurse
.int_value
,
1719 o_action_newer
.int_value
);
1723 abox_add_flag(ABOX(abox
),
1724 _("Force"), _("Don't confirm deletion of non-writeable items"),
1725 'F', o_action_force
.int_value
);
1726 abox_add_flag(ABOX(abox
),
1727 _("Brief"), _("Only log directories being deleted"),
1728 'B', o_action_brief
.int_value
);
1730 number_of_windows
++;
1731 gtk_widget_show(abox
);
1734 /* Change the permissions of the selected items */
1735 void action_chmod(GList
*paths
, gboolean force_recurse
, const char *action
)
1739 static GList
*presets
= NULL
;
1740 gboolean recurse
= force_recurse
|| o_action_recurse
.int_value
;
1744 report_error(_("You need to select the items "
1745 "whose permissions you want to change"));
1751 presets
= g_list_append(presets
, (gchar
*)
1752 _("a+x (Make executable/searchable)"));
1753 presets
= g_list_append(presets
, (gchar
*)
1754 _("a-x (Make non-executable/non-searchable)"));
1755 presets
= g_list_append(presets
, (gchar
*)
1756 _("u+rw (Give owner read+write)"));
1757 presets
= g_list_append(presets
, (gchar
*)
1758 _("go-rwx (Private - owner access only)"));
1759 presets
= g_list_append(presets
, (gchar
*)
1760 _("go=u-w (Public access, not write)"));
1763 if (!last_chmod_string
)
1764 last_chmod_string
= g_strdup((guchar
*) presets
->data
);
1767 new_entry_string
= g_strdup(action
);
1769 new_entry_string
= g_strdup(last_chmod_string
);
1771 abox
= abox_new(_("Permissions"), FALSE
);
1772 gui_side
= start_action(abox
, chmod_cb
, paths
,
1773 o_action_force
.int_value
,
1774 o_action_brief
.int_value
,
1776 o_action_newer
.int_value
);
1781 abox_add_flag(ABOX(abox
),
1782 _("Brief"), _("Don't list processed files"),
1783 'B', o_action_brief
.int_value
);
1784 abox_add_flag(ABOX(abox
),
1785 _("Recurse"), _("Also change contents of subdirectories"),
1788 gui_side
->default_string
= &last_chmod_string
;
1789 abox_add_combo(ABOX(abox
), presets
, new_entry_string
,
1790 new_help_button(show_chmod_help
, NULL
));
1792 g_signal_connect(ABOX(abox
)->entry
, "changed",
1793 G_CALLBACK(entry_changed
), gui_side
);
1795 g_signal_connect_swapped(gui_side
->entry
, "activate",
1796 G_CALLBACK(gtk_button_clicked
),
1800 number_of_windows
++;
1801 gtk_widget_show(abox
);
1804 null_g_free(&new_entry_string
);
1807 /* If leaf is NULL then the copy has the same name as the original.
1808 * quiet can be -1 for default.
1810 void action_copy(GList
*paths
, const char *dest
, const char *leaf
, int quiet
)
1816 quiet
= o_action_copy
.int_value
;
1820 action_do_func
= do_copy
;
1822 abox
= abox_new(_("Copy"), quiet
);
1823 gui_side
= start_action(abox
, list_cb
, paths
,
1824 o_action_force
.int_value
,
1825 o_action_brief
.int_value
,
1826 o_action_recurse
.int_value
,
1827 o_action_newer
.int_value
);
1831 abox_add_flag(ABOX(abox
),
1833 _("Only over-write if source is newer than destination."),
1834 'W', o_action_newer
.int_value
);
1836 number_of_windows
++;
1837 gtk_widget_show(abox
);
1840 /* If leaf is NULL then the file is not renamed.
1841 * quiet can be -1 for default.
1843 void action_move(GList
*paths
, const char *dest
, const char *leaf
, int quiet
)
1849 quiet
= o_action_move
.int_value
;
1853 action_do_func
= do_move
;
1855 abox
= abox_new(_("Move"), quiet
);
1856 gui_side
= start_action(abox
, list_cb
, paths
,
1857 o_action_force
.int_value
,
1858 o_action_brief
.int_value
,
1859 o_action_recurse
.int_value
,
1860 o_action_newer
.int_value
);
1864 abox_add_flag(ABOX(abox
),
1866 _("Only over-write if source is newer than destination."),
1867 'W', o_action_newer
.int_value
);
1868 number_of_windows
++;
1869 gtk_widget_show(abox
);
1872 /* If leaf is NULL then the link will have the same name */
1873 /* XXX: No quiet option here? */
1874 void action_link(GList
*paths
, const char *dest
, const char *leaf
)
1881 action_do_func
= do_link
;
1883 abox
= abox_new(_("Link"), o_action_link
.int_value
);
1884 gui_side
= start_action(abox
, list_cb
, paths
,
1885 o_action_force
.int_value
,
1886 o_action_brief
.int_value
,
1887 o_action_recurse
.int_value
,
1888 o_action_newer
.int_value
);
1892 number_of_windows
++;
1893 gtk_widget_show(abox
);
1896 void action_init(void)
1898 option_add_int(&o_action_copy
, "action_copy", 1);
1899 option_add_int(&o_action_move
, "action_move", 1);
1900 option_add_int(&o_action_link
, "action_link", 1);
1901 option_add_int(&o_action_delete
, "action_delete", 0);
1902 option_add_int(&o_action_mount
, "action_mount", 1);
1903 option_add_int(&o_action_force
, "action_force", FALSE
);
1904 option_add_int(&o_action_brief
, "action_brief", FALSE
);
1905 option_add_int(&o_action_recurse
, "action_recurse", FALSE
);
1906 option_add_int(&o_action_newer
, "action_newer", FALSE
);
1911 /* Check to see if any of the selected items (or their children) are
1912 * on the pinboard or panel. If so, ask for confirmation.
1914 * TRUE if it's OK to lose them.
1916 static gboolean
remove_pinned_ok(GList
*paths
)
1918 GList
*ask
= NULL
, *next
;
1923 for (; paths
; paths
= paths
->next
)
1925 guchar
*path
= (guchar
*) paths
->data
;
1927 if (icons_require(path
))
1929 if (++ask_n
> MAX_ASK
)
1931 ask
= g_list_append(ask
, path
);
1938 if (ask_n
> MAX_ASK
)
1940 message
= g_string_new(_("Deleting items such as "));
1943 else if (ask_n
== 1)
1944 message
= g_string_new(_("Deleting the item "));
1946 message
= g_string_new(_("Deleting the items "));
1949 for (next
= ask
; next
; next
= next
->next
)
1951 guchar
*path
= (guchar
*) next
->data
;
1954 leaf
= strrchr(path
, '/');
1960 g_string_append_c(message
, '`');
1961 g_string_append(message
, leaf
);
1962 g_string_append_c(message
, '\'');
1964 if (i
== ask_n
- 1 && i
> 0)
1965 g_string_append(message
, _(" and "));
1967 g_string_append(message
, ", ");
1973 message
= g_string_append(message
,
1974 _(" will affect some items on the pinboard "
1975 "or panel - really delete it?"));
1978 if (ask_n
> MAX_ASK
)
1979 message
= g_string_append_c(message
, ',');
1980 message
= g_string_append(message
,
1981 _(" will affect some items on the pinboard "
1982 "or panel - really delete them?"));
1985 retval
= confirm(message
->str
, GTK_STOCK_DELETE
, NULL
);
1987 g_string_free(message
, TRUE
);
1992 void set_find_string_colour(GtkWidget
*widget
, const guchar
*string
)
1994 FindCondition
*cond
;
1996 cond
= find_compile(string
);
1997 entry_set_error(widget
, !cond
);
1999 find_condition_free(cond
);