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