This source file includes following definitions.
- chown_init
- chown_refresh
- chown_bg_callback
- chown_dlg_create
- chown_done
- next_file
- try_chown
- do_chown
- apply_chowns
- chown_cmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 #include <config.h>
28
29 #include <errno.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <pwd.h>
33 #include <grp.h>
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <unistd.h>
37
38 #include "lib/global.h"
39
40 #include "lib/tty/tty.h"
41 #include "lib/skin.h"
42 #include "lib/vfs/vfs.h"
43 #include "lib/strutil.h"
44 #include "lib/util.h"
45 #include "lib/widget.h"
46
47 #include "src/setup.h"
48
49 #include "cmd.h"
50
51
52
53
54
55 #define GH 12
56 #define GW 21
57
58 #define BUTTONS 5
59
60 #define B_SETALL B_USER
61 #define B_SETUSR (B_USER + 1)
62 #define B_SETGRP (B_USER + 2)
63
64 #define LABELS 5
65
66 #define chown_label(n,txt) label_set_text (chown_label [n].l, txt)
67
68
69
70
71
72
73
74 static struct
75 {
76 int ret_cmd;
77 button_flags_t flags;
78 int y;
79 int len;
80 const char *text;
81 } chown_but[BUTTONS] =
82 {
83
84 { B_SETALL, NORMAL_BUTTON, 5, 0, N_("Set &all") },
85 { B_SETGRP, NORMAL_BUTTON, 5, 0, N_("Set &groups") },
86 { B_SETUSR, NORMAL_BUTTON, 5, 0, N_("Set &users") },
87 { B_ENTER, DEFPUSH_BUTTON, 3, 0, N_("&Set") },
88 { B_CANCEL, NORMAL_BUTTON, 3, 0, N_("&Cancel") }
89
90 };
91
92
93 static int blen = 0;
94
95 static struct
96 {
97 int y;
98 WLabel *l;
99 } chown_label[LABELS] =
100 {
101
102 { 4, NULL },
103 { 6, NULL },
104 { 8, NULL },
105 { 10, NULL },
106 { 12, NULL }
107
108 };
109
110 static int current_file;
111 static gboolean ignore_all;
112
113 static WListbox *l_user, *l_group;
114
115
116
117
118
119 static void
120 chown_init (void)
121 {
122 static gboolean i18n = FALSE;
123 int i;
124
125 if (i18n)
126 return;
127
128 i18n = TRUE;
129
130 #ifdef ENABLE_NLS
131 for (i = 0; i < BUTTONS; i++)
132 chown_but[i].text = _(chown_but[i].text);
133 #endif
134
135 for (i = 0; i < BUTTONS; i++)
136 {
137 chown_but[i].len = str_term_width1 (chown_but[i].text) + 3;
138 if (chown_but[i].flags == DEFPUSH_BUTTON)
139 chown_but[i].len += 2;
140
141 if (i < BUTTONS - 2)
142 blen += chown_but[i].len;
143 }
144
145 blen += 2;
146 }
147
148
149
150 static void
151 chown_refresh (const Widget * h)
152 {
153 int y = 3;
154 int x = 7 + GW * 2;
155
156 tty_setcolor (COLOR_NORMAL);
157
158 widget_gotoyx (h, y + 0, x);
159 tty_print_string (_("Name"));
160 widget_gotoyx (h, y + 2, x);
161 tty_print_string (_("Owner name"));
162 widget_gotoyx (h, y + 4, x);
163 tty_print_string (_("Group name"));
164 widget_gotoyx (h, y + 6, x);
165 tty_print_string (_("Size"));
166 widget_gotoyx (h, y + 8, x);
167 tty_print_string (_("Permission"));
168 }
169
170
171
172 static cb_ret_t
173 chown_bg_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
174 {
175 switch (msg)
176 {
177 case MSG_DRAW:
178 frame_callback (w, NULL, MSG_DRAW, 0, NULL);
179 chown_refresh (WIDGET (w->owner));
180 return MSG_HANDLED;
181
182 default:
183 return frame_callback (w, sender, msg, parm, data);
184 }
185 }
186
187
188
189 static WDialog *
190 chown_dlg_create (WPanel * panel)
191 {
192 int single_set;
193 WDialog *ch_dlg;
194 WGroup *g;
195 int lines, cols;
196 int i, y;
197 struct passwd *l_pass;
198 struct group *l_grp;
199
200 single_set = (panel->marked < 2) ? 3 : 0;
201 lines = GH + 4 + (single_set != 0 ? 2 : 4);
202 cols = GW * 3 + 2 + 6;
203
204 ch_dlg =
205 dlg_create (TRUE, 0, 0, lines, cols, WPOS_CENTER, FALSE, dialog_colors, NULL, NULL,
206 "[Chown]", _("Chown command"));
207 g = GROUP (ch_dlg);
208
209
210 ch_dlg->bg->callback = chown_bg_callback;
211
212 group_add_widget (g, groupbox_new (2, 3, GH, GW, _("User name")));
213 l_user = listbox_new (3, 4, GH - 2, GW - 2, FALSE, NULL);
214 group_add_widget (g, l_user);
215
216 listbox_add_item (l_user, LISTBOX_APPEND_AT_END, 0, _("<Unknown user>"), NULL, FALSE);
217
218 setpwent ();
219 while ((l_pass = getpwent ()) != NULL)
220 listbox_add_item (l_user, LISTBOX_APPEND_SORTED, 0, l_pass->pw_name, NULL, FALSE);
221 endpwent ();
222
223 group_add_widget (g, groupbox_new (2, 4 + GW, GH, GW, _("Group name")));
224 l_group = listbox_new (3, 5 + GW, GH - 2, GW - 2, FALSE, NULL);
225 group_add_widget (g, l_group);
226
227 listbox_add_item (l_group, LISTBOX_APPEND_AT_END, 0, _("<Unknown group>"), NULL, FALSE);
228
229 setgrent ();
230 while ((l_grp = getgrent ()) != NULL)
231 listbox_add_item (l_group, LISTBOX_APPEND_SORTED, 0, l_grp->gr_name, NULL, FALSE);
232 endgrent ();
233
234 group_add_widget (g, groupbox_new (2, 5 + GW * 2, GH, GW, _("File")));
235
236 for (i = 0; i < LABELS; i++)
237 {
238 chown_label[i].l = label_new (chown_label[i].y, 7 + GW * 2, NULL);
239 group_add_widget (g, chown_label[i].l);
240 }
241
242 if (single_set == 0)
243 {
244 int x;
245
246 group_add_widget (g, hline_new (lines - chown_but[0].y - 1, -1, -1));
247
248 y = lines - chown_but[0].y;
249 x = (cols - blen) / 2;
250
251 for (i = 0; i < BUTTONS - 2; i++)
252 {
253 group_add_widget (g, button_new (y, x, chown_but[i].ret_cmd, chown_but[i].flags,
254 chown_but[i].text, NULL));
255 x += chown_but[i].len + 1;
256 }
257 }
258
259 i = BUTTONS - 2;
260 y = lines - chown_but[i].y;
261 group_add_widget (g, hline_new (y - 1, -1, -1));
262 group_add_widget (g, button_new (y, WIDGET (ch_dlg)->rect.cols / 2 - chown_but[i].len,
263 chown_but[i].ret_cmd, chown_but[i].flags, chown_but[i].text,
264 NULL));
265 i++;
266 group_add_widget (g, button_new (y, WIDGET (ch_dlg)->rect.cols / 2 + 1, chown_but[i].ret_cmd,
267 chown_but[i].flags, chown_but[i].text, NULL));
268
269
270 widget_select (WIDGET (l_user));
271
272 return ch_dlg;
273 }
274
275
276
277 static void
278 chown_done (gboolean need_update)
279 {
280 if (need_update)
281 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
282 repaint_screen ();
283 }
284
285
286
287 static const GString *
288 next_file (const WPanel * panel)
289 {
290 while (panel->dir.list[current_file].f.marked == 0)
291 current_file++;
292
293 return panel->dir.list[current_file].fname;
294 }
295
296
297
298 static gboolean
299 try_chown (const vfs_path_t * p, uid_t u, gid_t g)
300 {
301 const char *fname = NULL;
302
303 while (mc_chown (p, u, g) == -1 && !ignore_all)
304 {
305 int my_errno = errno;
306 int result;
307 char *msg;
308
309 if (fname == NULL)
310 fname = x_basename (vfs_path_as_str (p));
311 msg = g_strdup_printf (_("Cannot chown \"%s\"\n%s"), fname, unix_error_string (my_errno));
312 result =
313 query_dialog (MSG_ERROR, msg, D_ERROR, 4, _("&Ignore"), _("Ignore &all"), _("&Retry"),
314 _("&Cancel"));
315 g_free (msg);
316
317 switch (result)
318 {
319 case 0:
320
321 return TRUE;
322
323 case 1:
324 ignore_all = TRUE;
325
326 return TRUE;
327
328 case 2:
329
330 break;
331
332 case 3:
333 default:
334
335 return FALSE;
336 }
337 }
338
339 return TRUE;
340 }
341
342
343
344 static gboolean
345 do_chown (WPanel * panel, const vfs_path_t * p, uid_t u, gid_t g)
346 {
347 gboolean ret;
348
349 ret = try_chown (p, u, g);
350
351 do_file_mark (panel, current_file, 0);
352
353 return ret;
354 }
355
356
357
358 static void
359 apply_chowns (WPanel * panel, vfs_path_t * vpath, uid_t u, gid_t g)
360 {
361 gboolean ok;
362
363 if (!do_chown (panel, vpath, u, g))
364 return;
365
366 do
367 {
368 const GString *fname;
369 struct stat sf;
370
371 fname = next_file (panel);
372 vpath = vfs_path_from_str (fname->str);
373 ok = (mc_stat (vpath, &sf) == 0);
374
375 if (!ok)
376 {
377
378
379 do_file_mark (panel, current_file, 0);
380
381
382 ok = TRUE;
383 }
384 else
385 ok = do_chown (panel, vpath, u, g);
386
387 vfs_path_free (vpath, TRUE);
388 }
389 while (ok && panel->marked != 0);
390 }
391
392
393
394
395
396 void
397 chown_cmd (WPanel * panel)
398 {
399 gboolean need_update;
400 gboolean end_chown;
401
402 chown_init ();
403
404 current_file = 0;
405 ignore_all = FALSE;
406
407 do
408 {
409 vfs_path_t *vpath;
410 WDialog *ch_dlg;
411 struct stat sf_stat;
412 const GString *fname;
413 int result;
414 char buffer[BUF_TINY];
415 uid_t new_user = (uid_t) (-1);
416 gid_t new_group = (gid_t) (-1);
417
418 do_refresh ();
419
420 need_update = FALSE;
421 end_chown = FALSE;
422
423 if (panel->marked != 0)
424 fname = next_file (panel);
425 else
426 fname = panel_current_entry (panel)->fname;
427
428 vpath = vfs_path_from_str (fname->str);
429
430 if (mc_stat (vpath, &sf_stat) != 0)
431 {
432 vfs_path_free (vpath, TRUE);
433 break;
434 }
435
436 ch_dlg = chown_dlg_create (panel);
437
438
439 listbox_set_current (l_user, listbox_search_text (l_user, get_owner (sf_stat.st_uid)));
440 listbox_set_current (l_group, listbox_search_text (l_group, get_group (sf_stat.st_gid)));
441
442 chown_label (0, str_trunc (fname->str, GW - 4));
443 chown_label (1, str_trunc (get_owner (sf_stat.st_uid), GW - 4));
444 chown_label (2, str_trunc (get_group (sf_stat.st_gid), GW - 4));
445 size_trunc_len (buffer, GW - 4, sf_stat.st_size, 0, panels_options.kilobyte_si);
446 chown_label (3, buffer);
447 chown_label (4, string_perm (sf_stat.st_mode));
448
449 result = dlg_run (ch_dlg);
450
451 switch (result)
452 {
453 case B_CANCEL:
454 end_chown = TRUE;
455 break;
456
457 case B_ENTER:
458 case B_SETALL:
459 {
460 struct group *grp;
461 struct passwd *user;
462 char *text;
463
464 listbox_get_current (l_group, &text, NULL);
465 grp = getgrnam (text);
466 if (grp != NULL)
467 new_group = grp->gr_gid;
468 listbox_get_current (l_user, &text, NULL);
469 user = getpwnam (text);
470 if (user != NULL)
471 new_user = user->pw_uid;
472 if (result == B_ENTER)
473 {
474 if (panel->marked <= 1)
475 {
476
477 if (mc_chown (vpath, new_user, new_group) == -1)
478 message (D_ERROR, MSG_ERROR, _("Cannot chown \"%s\"\n%s"),
479 fname->str, unix_error_string (errno));
480 end_chown = TRUE;
481 }
482 else if (!try_chown (vpath, new_user, new_group))
483 {
484
485 result = B_CANCEL;
486 end_chown = TRUE;
487 }
488 }
489 else
490 {
491 apply_chowns (panel, vpath, new_user, new_group);
492 end_chown = TRUE;
493 }
494
495 need_update = TRUE;
496 break;
497 }
498
499 case B_SETUSR:
500 {
501 struct passwd *user;
502 char *text;
503
504 listbox_get_current (l_user, &text, NULL);
505 user = getpwnam (text);
506 if (user != NULL)
507 {
508 new_user = user->pw_uid;
509 apply_chowns (panel, vpath, new_user, new_group);
510 need_update = TRUE;
511 end_chown = TRUE;
512 }
513 break;
514 }
515
516 case B_SETGRP:
517 {
518 struct group *grp;
519 char *text;
520
521 listbox_get_current (l_group, &text, NULL);
522 grp = getgrnam (text);
523 if (grp != NULL)
524 {
525 new_group = grp->gr_gid;
526 apply_chowns (panel, vpath, new_user, new_group);
527 need_update = TRUE;
528 end_chown = TRUE;
529 }
530 break;
531 }
532
533 default:
534 break;
535 }
536
537 if (panel->marked != 0 && result != B_CANCEL)
538 {
539 do_file_mark (panel, current_file, 0);
540 need_update = TRUE;
541 }
542
543 vfs_path_free (vpath, TRUE);
544
545 widget_destroy (WIDGET (ch_dlg));
546 }
547 while (panel->marked != 0 && !end_chown);
548
549 chown_done (need_update);
550 }
551
552