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