root/src/filemanager/achown.c

/* [previous][next][first][last][top][bottom][index][help]  */

DEFINITIONS

This source file includes following definitions.
  1. advanced_chown_init
  2. inc_flag_pos
  3. dec_flag_pos
  4. set_perm_by_flags
  5. get_perm
  6. get_mode
  7. update_permissions
  8. update_ownership
  9. print_flags
  10. advanced_chown_refresh
  11. advanced_chown_info_update
  12. update_mode
  13. perm_button_callback
  14. perm_button_mouse_callback
  15. perm_button_new
  16. chl_callback
  17. user_group_button_cb
  18. advanced_chown_bg_callback
  19. advanced_chown_callback
  20. advanced_chown_dlg_create
  21. advanced_chown_done
  22. next_file
  23. try_advanced_chown
  24. do_advanced_chown
  25. apply_advanced_chowns
  26. advanced_chown_cmd

   1 /*
   2    Chown-advanced command -- for the Midnight Commander
   3 
   4    Copyright (C) 1994-2020
   5    Free Software Foundation, Inc.
   6 
   7    This file is part of the Midnight Commander.
   8 
   9    The Midnight Commander is free software: you can redistribute it
  10    and/or modify it under the terms of the GNU General Public License as
  11    published by the Free Software Foundation, either version 3 of the License,
  12    or (at your option) any later version.
  13 
  14    The Midnight Commander is distributed in the hope that it will be useful,
  15    but WITHOUT ANY WARRANTY; without even the implied warranty of
  16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17    GNU General Public License for more details.
  18 
  19    You should have received a copy of the GNU General Public License
  20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  21  */
  22 
  23 /** \file achown.c
  24  *  \brief Source: Contains functions for advanced chowning
  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/tty/key.h"        /* XCTRL and ALT macros */
  42 #include "lib/skin.h"
  43 #include "lib/vfs/vfs.h"
  44 #include "lib/strutil.h"
  45 #include "lib/util.h"
  46 #include "lib/widget.h"
  47 
  48 #include "midnight.h"           /* current_panel */
  49 
  50 #include "achown.h"
  51 
  52 /*** global variables ****************************************************************************/
  53 
  54 /*** file scope macro definitions ****************************************************************/
  55 
  56 #define BX 5
  57 #define BY 5
  58 
  59 #define BUTTONS      9
  60 #define BUTTONS_PERM 5
  61 
  62 #define B_SETALL B_USER
  63 #define B_SKIP   (B_USER + 1)
  64 
  65 /*** file scope type declarations ****************************************************************/
  66 
  67 /*** file scope variables ************************************************************************/
  68 
  69 static struct
  70 {
  71     unsigned long id;
  72     int ret_cmd;
  73     button_flags_t flags;
  74     int x;
  75     int len;
  76     const char *text;
  77 } advanced_chown_but[BUTTONS] =
  78 {
  79     /* *INDENT-OFF* */
  80     { 0, B_ENTER,   NARROW_BUTTON,  3, 0, "   " },
  81     { 0, B_ENTER,   NARROW_BUTTON, 11, 0, "   " },
  82     { 0, B_ENTER,   NARROW_BUTTON, 19, 0, "   " },
  83     { 0, B_ENTER,   NARROW_BUTTON, 29, 0, ""    },
  84     { 0, B_ENTER,   NARROW_BUTTON, 47, 0, ""    },
  85 
  86     { 0, B_SETALL,  NORMAL_BUTTON,  0, 0, N_("Set &all") },
  87     { 0, B_SKIP,    NORMAL_BUTTON,  0, 0, N_("S&kip")    },
  88     { 0, B_ENTER,  DEFPUSH_BUTTON,  0, 0, N_("&Set")     },
  89     { 0, B_CANCEL,  NORMAL_BUTTON,  0, 0, N_("&Cancel")  }
  90     /* *INDENT-ON* */
  91 };
  92 
  93 static int current_file;
  94 static gboolean ignore_all;
  95 
  96 static WButton *b_att[3];       /* permission */
  97 static WButton *b_user, *b_group;       /* owner */
  98 static WLabel *l_filename;
  99 static WLabel *l_mode;
 100 
 101 static int flag_pos;
 102 static int x_toggle;
 103 static char ch_flags[11];
 104 static const char ch_perm[] = "rwx";
 105 static mode_t ch_cmode;
 106 static struct stat sf_stat;
 107 
 108 /* --------------------------------------------------------------------------------------------- */
 109 /*** file scope functions ************************************************************************/
 110 /* --------------------------------------------------------------------------------------------- */
 111 
 112 static void
 113 advanced_chown_init (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 114 {
 115     static gboolean i18n = FALSE;
 116     int i;
 117 
 118     if (i18n)
 119         return;
 120 
 121     i18n = TRUE;
 122 
 123     for (i = BUTTONS_PERM; i < BUTTONS; i++)
 124     {
 125 #ifdef ENABLE_NLS
 126         advanced_chown_but[i].text = _(advanced_chown_but[i].text);
 127 #endif /* ENABLE_NLS */
 128 
 129         advanced_chown_but[i].len = str_term_width1 (advanced_chown_but[i].text) + 3;
 130         if (advanced_chown_but[i].flags == DEFPUSH_BUTTON)
 131             advanced_chown_but[i].len += 2;     /* "<>" */
 132     }
 133 
 134 }
 135 
 136 /* --------------------------------------------------------------------------------------------- */
 137 
 138 static cb_ret_t
 139 inc_flag_pos (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 140 {
 141     if (flag_pos == 10)
 142     {
 143         flag_pos = 0;
 144         return MSG_NOT_HANDLED;
 145     }
 146 
 147     flag_pos++;
 148 
 149     return flag_pos % 3 == 0 ? MSG_NOT_HANDLED : MSG_HANDLED;
 150 }
 151 
 152 /* --------------------------------------------------------------------------------------------- */
 153 
 154 static cb_ret_t
 155 dec_flag_pos (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 156 {
 157     if (flag_pos == 0)
 158     {
 159         flag_pos = 10;
 160         return MSG_NOT_HANDLED;
 161     }
 162 
 163     flag_pos--;
 164 
 165     return (flag_pos + 1) % 3 == 0 ? MSG_NOT_HANDLED : MSG_HANDLED;
 166 }
 167 
 168 /* --------------------------------------------------------------------------------------------- */
 169 
 170 static void
 171 set_perm_by_flags (char *s, int f_p)
     /* [previous][next][first][last][top][bottom][index][help]  */
 172 {
 173     int i;
 174 
 175     for (i = 0; i < 3; i++)
 176     {
 177         if (ch_flags[f_p + i] == '+')
 178             s[i] = ch_perm[i];
 179         else if (ch_flags[f_p + i] == '-')
 180             s[i] = '-';
 181         else
 182             s[i] = (ch_cmode & (1 << (8 - f_p - i))) != 0 ? ch_perm[i] : '-';
 183     }
 184 }
 185 
 186 /* --------------------------------------------------------------------------------------------- */
 187 
 188 static mode_t
 189 get_perm (char *s, int base)
     /* [previous][next][first][last][top][bottom][index][help]  */
 190 {
 191     mode_t m = 0;
 192 
 193     m |= (s[0] == '-') ? 0 :
 194         ((s[0] == '+') ? (mode_t) (1 << (base + 2)) : (1 << (base + 2)) & ch_cmode);
 195 
 196     m |= (s[1] == '-') ? 0 :
 197         ((s[1] == '+') ? (mode_t) (1 << (base + 1)) : (1 << (base + 1)) & ch_cmode);
 198 
 199     m |= (s[2] == '-') ? 0 : ((s[2] == '+') ? (mode_t) (1 << base) : (1 << base) & ch_cmode);
 200 
 201     return m;
 202 }
 203 
 204 /* --------------------------------------------------------------------------------------------- */
 205 
 206 static mode_t
 207 get_mode (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 208 {
 209     mode_t m;
 210 
 211     m = ch_cmode ^ (ch_cmode & 0777);
 212     m |= get_perm (ch_flags, 6);
 213     m |= get_perm (ch_flags + 3, 3);
 214     m |= get_perm (ch_flags + 6, 0);
 215 
 216     return m;
 217 }
 218 
 219 /* --------------------------------------------------------------------------------------------- */
 220 
 221 static void
 222 update_permissions (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 223 {
 224     set_perm_by_flags (b_att[0]->text.start, 0);
 225     set_perm_by_flags (b_att[1]->text.start, 3);
 226     set_perm_by_flags (b_att[2]->text.start, 6);
 227 }
 228 
 229 /* --------------------------------------------------------------------------------------------- */
 230 
 231 static void
 232 update_ownership (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 233 {
 234     button_set_text (b_user, get_owner (sf_stat.st_uid));
 235     button_set_text (b_group, get_group (sf_stat.st_gid));
 236 }
 237 
 238 /* --------------------------------------------------------------------------------------------- */
 239 
 240 static void
 241 print_flags (const WDialog * h)
     /* [previous][next][first][last][top][bottom][index][help]  */
 242 {
 243     int i;
 244 
 245     tty_setcolor (COLOR_NORMAL);
 246 
 247     for (i = 0; i < 3; i++)
 248     {
 249         widget_gotoyx (h, BY + 1, advanced_chown_but[0].x + 6 + i);
 250         tty_print_char (ch_flags[i]);
 251     }
 252 
 253     for (i = 0; i < 3; i++)
 254     {
 255         widget_gotoyx (h, BY + 1, advanced_chown_but[1].x + 6 + i);
 256         tty_print_char (ch_flags[i + 3]);
 257     }
 258 
 259     for (i = 0; i < 3; i++)
 260     {
 261         widget_gotoyx (h, BY + 1, advanced_chown_but[2].x + 6 + i);
 262         tty_print_char (ch_flags[i + 6]);
 263     }
 264 
 265     update_permissions ();
 266 
 267     for (i = 0; i < 15; i++)
 268     {
 269         widget_gotoyx (h, BY + 1, advanced_chown_but[3].x + 6 + i);
 270         tty_print_char (ch_flags[9]);
 271     }
 272     for (i = 0; i < 15; i++)
 273     {
 274         widget_gotoyx (h, BY + 1, advanced_chown_but[4].x + 6 + i);
 275         tty_print_char (ch_flags[10]);
 276     }
 277 }
 278 
 279 /* --------------------------------------------------------------------------------------------- */
 280 
 281 static void
 282 advanced_chown_refresh (WDialog * h)
     /* [previous][next][first][last][top][bottom][index][help]  */
 283 {
 284     tty_setcolor (COLOR_NORMAL);
 285 
 286     widget_gotoyx (h, BY - 1, advanced_chown_but[0].x + 5);
 287     tty_print_string (_("owner"));
 288     widget_gotoyx (h, BY - 1, advanced_chown_but[1].x + 5);
 289     tty_print_string (_("group"));
 290     widget_gotoyx (h, BY - 1, advanced_chown_but[2].x + 5);
 291     tty_print_string (_("other"));
 292 
 293     widget_gotoyx (h, BY - 1, advanced_chown_but[3].x + 5);
 294     tty_print_string (_("owner"));
 295     widget_gotoyx (h, BY - 1, advanced_chown_but[4].x + 5);
 296     tty_print_string (_("group"));
 297 
 298     widget_gotoyx (h, BY + 1, 3);
 299     tty_print_string (_("Flag"));
 300     print_flags (h);
 301 }
 302 
 303 /* --------------------------------------------------------------------------------------------- */
 304 
 305 static void
 306 advanced_chown_info_update (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 307 {
 308     /* mode */
 309     label_set_textv (l_mode, _("Permissions (octal): %o"), get_mode ());
 310 
 311     /* permissions */
 312     update_permissions ();
 313 }
 314 
 315 /* --------------------------------------------------------------------------------------------- */
 316 
 317 static void
 318 update_mode (WGroup * g)
     /* [previous][next][first][last][top][bottom][index][help]  */
 319 {
 320     print_flags (DIALOG (g));
 321     advanced_chown_info_update ();
 322     widget_set_state (WIDGET (g->current->data), WST_FOCUSED, TRUE);
 323 }
 324 
 325 /* --------------------------------------------------------------------------------------------- */
 326 
 327 static cb_ret_t
 328 perm_button_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
     /* [previous][next][first][last][top][bottom][index][help]  */
 329 {
 330     WButton *b = BUTTON (w);
 331     WGroup *g = w->owner;
 332     int i = 0;
 333     int f_pos;
 334 
 335     /* one of permission buttons */
 336     if (b == b_att[0])
 337         f_pos = 0;
 338     else if (b == b_att[1])
 339         f_pos = 1;
 340     else                        /* if (w == b_att [1] */
 341         f_pos = 2;
 342 
 343     switch (msg)
 344     {
 345     case MSG_FOCUS:
 346         if (b->hotpos == -1)
 347             b->hotpos = 0;
 348 
 349         flag_pos = f_pos * 3 + b->hotpos;
 350         return MSG_HANDLED;
 351 
 352     case MSG_KEY:
 353         switch (parm)
 354         {
 355         case '*':
 356             parm = '=';
 357             MC_FALLTHROUGH;
 358 
 359         case '-':
 360         case '=':
 361         case '+':
 362             flag_pos = f_pos * 3 + b->hotpos;
 363             ch_flags[flag_pos] = parm;
 364             update_mode (g);
 365             send_message (w, NULL, MSG_KEY, KEY_RIGHT, NULL);
 366             if (b->hotpos == 2)
 367                 group_select_next_widget (g);
 368             break;
 369 
 370         case XCTRL ('f'):
 371         case KEY_RIGHT:
 372             {
 373                 cb_ret_t ret;
 374 
 375                 ret = inc_flag_pos ();
 376                 b->hotpos = flag_pos % 3;
 377                 return ret;
 378             }
 379 
 380         case XCTRL ('b'):
 381         case KEY_LEFT:
 382             {
 383                 cb_ret_t ret;
 384 
 385                 ret = dec_flag_pos ();
 386                 b->hotpos = flag_pos % 3;
 387                 return ret;
 388             }
 389 
 390         case 'x':
 391             i++;
 392             MC_FALLTHROUGH;
 393 
 394         case 'w':
 395             i++;
 396             MC_FALLTHROUGH;
 397 
 398         case 'r':
 399             b->hotpos = i;
 400             MC_FALLTHROUGH;
 401 
 402         case ' ':
 403             i = b->hotpos;
 404 
 405             flag_pos = f_pos * 3 + i;
 406             if (b->text.start[flag_pos % 3] == '-')
 407                 ch_flags[flag_pos] = '+';
 408             else
 409                 ch_flags[flag_pos] = '-';
 410             update_mode (w->owner);
 411             break;
 412 
 413         case '4':
 414             i++;
 415             MC_FALLTHROUGH;
 416 
 417         case '2':
 418             i++;
 419             MC_FALLTHROUGH;
 420 
 421         case '1':
 422             b->hotpos = i;
 423             flag_pos = f_pos * 3 + i;
 424             ch_flags[flag_pos] = '=';
 425             update_mode (g);
 426             break;
 427 
 428         default:
 429             break;
 430         }
 431         /* continue key handling in the dialog level */
 432         return MSG_NOT_HANDLED;
 433 
 434     default:
 435         return button_default_callback (w, sender, msg, parm, data);
 436     }
 437 }
 438 
 439 /* --------------------------------------------------------------------------------------------- */
 440 
 441 static void
 442 perm_button_mouse_callback (Widget * w, mouse_msg_t msg, mouse_event_t * event)
     /* [previous][next][first][last][top][bottom][index][help]  */
 443 {
 444     switch (msg)
 445     {
 446     case MSG_MOUSE_DOWN:
 447         /* place cursor on flag that is being modified */
 448         BUTTON (w)->hotpos = CLAMP (event->x - 1, 0, 2);
 449         MC_FALLTHROUGH;
 450 
 451     default:
 452         button_mouse_default_callback (w, msg, event);
 453         break;
 454     }
 455 }
 456 
 457 /* --------------------------------------------------------------------------------------------- */
 458 
 459 static WButton *
 460 perm_button_new (int y, int x, int action, button_flags_t flags, const char *text,
     /* [previous][next][first][last][top][bottom][index][help]  */
 461                  bcback_fn callback)
 462 {
 463     WButton *b;
 464     Widget *w;
 465 
 466     /* create base button using native API */
 467     b = button_new (y, x, action, flags, text, callback);
 468     w = WIDGET (b);
 469 
 470     /* we don't want HOTKEY */
 471     widget_want_hotkey (w, FALSE);
 472 
 473     w->callback = perm_button_callback;
 474     w->mouse_callback = perm_button_mouse_callback;
 475 
 476     return b;
 477 }
 478 
 479 /* --------------------------------------------------------------------------------------------- */
 480 
 481 static cb_ret_t
 482 chl_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
     /* [previous][next][first][last][top][bottom][index][help]  */
 483 {
 484     switch (msg)
 485     {
 486     case MSG_KEY:
 487         switch (parm)
 488         {
 489         case KEY_LEFT:
 490         case KEY_RIGHT:
 491             {
 492                 WDialog *h = DIALOG (w);
 493 
 494                 h->ret_value = parm;
 495                 dlg_stop (h);
 496             }
 497         default:
 498             break;
 499         }
 500         MC_FALLTHROUGH;
 501 
 502     default:
 503         return dlg_default_callback (w, sender, msg, parm, data);
 504     }
 505 }
 506 
 507 /* --------------------------------------------------------------------------------------------- */
 508 
 509 static int
 510 user_group_button_cb (WButton * button, int action)
     /* [previous][next][first][last][top][bottom][index][help]  */
 511 {
 512     Widget *w = WIDGET (button);
 513     int f_pos;
 514     gboolean chl_end;
 515 
 516     (void) action;
 517 
 518     if (button == b_user)
 519         f_pos = BUTTONS_PERM - 2;
 520     else if (button == b_group)
 521         f_pos = BUTTONS_PERM - 1;
 522     else
 523         return 0;               /* do nothing */
 524 
 525     do
 526     {
 527         WGroup *g = w->owner;
 528         WDialog *h = DIALOG (g);
 529         Widget *wh = WIDGET (h);
 530 
 531         gboolean is_owner = (f_pos == BUTTONS_PERM - 2);
 532         const char *title;
 533         int lxx, b_pos;
 534         WDialog *chl_dlg;
 535         WListbox *chl_list;
 536         int result;
 537         int fe;
 538         struct passwd *chl_pass;
 539         struct group *chl_grp;
 540 
 541         chl_end = FALSE;
 542 
 543         if (is_owner)
 544         {
 545             title = _("owner");
 546             lxx = WIDGET (b_user)->x + 1;
 547         }
 548         else
 549         {
 550             title = _("group");
 551             lxx = WIDGET (b_group)->x + 1;
 552         }
 553 
 554         chl_dlg =
 555             dlg_create (TRUE, wh->y - 1, lxx, wh->lines + 2, 17, WPOS_KEEP_DEFAULT, TRUE,
 556                         dialog_colors, chl_callback, NULL, "[Advanced Chown]", title);
 557 
 558         /* get new listboxes */
 559         chl_list =
 560             listbox_new (1, 1, WIDGET (chl_dlg)->lines - 2, WIDGET (chl_dlg)->cols - 2, FALSE,
 561                          NULL);
 562         listbox_add_item (chl_list, LISTBOX_APPEND_AT_END, 0, "<Unknown>", NULL, FALSE);
 563         if (is_owner)
 564         {
 565             /* get and put user names in the listbox */
 566             setpwent ();
 567             while ((chl_pass = getpwent ()) != NULL)
 568                 listbox_add_item (chl_list, LISTBOX_APPEND_SORTED, 0, chl_pass->pw_name, NULL,
 569                                   FALSE);
 570             endpwent ();
 571             fe = listbox_search_text (chl_list, get_owner (sf_stat.st_uid));
 572         }
 573         else
 574         {
 575             /* get and put group names in the listbox */
 576             setgrent ();
 577             while ((chl_grp = getgrent ()) != NULL)
 578                 listbox_add_item (chl_list, LISTBOX_APPEND_SORTED, 0, chl_grp->gr_name, NULL,
 579                                   FALSE);
 580             endgrent ();
 581             fe = listbox_search_text (chl_list, get_group (sf_stat.st_gid));
 582         }
 583 
 584         listbox_select_entry (chl_list, fe);
 585 
 586         b_pos = chl_list->pos;
 587         group_add_widget (GROUP (chl_dlg), chl_list);
 588 
 589         result = dlg_run (chl_dlg);
 590 
 591         if (result != B_CANCEL)
 592         {
 593             if (b_pos != chl_list->pos)
 594             {
 595                 gboolean ok = FALSE;
 596                 char *text;
 597 
 598                 listbox_get_current (chl_list, &text, NULL);
 599                 if (is_owner)
 600                 {
 601                     chl_pass = getpwnam (text);
 602                     if (chl_pass != NULL)
 603                     {
 604                         sf_stat.st_uid = chl_pass->pw_uid;
 605                         ok = TRUE;
 606                     }
 607                 }
 608                 else
 609                 {
 610                     chl_grp = getgrnam (text);
 611                     if (chl_grp != NULL)
 612                     {
 613                         sf_stat.st_gid = chl_grp->gr_gid;
 614                         ok = TRUE;
 615                     }
 616                 }
 617 
 618                 if (!ok)
 619                     group_select_current_widget (g);
 620                 else
 621                 {
 622                     ch_flags[f_pos + 6] = '+';
 623                     update_ownership ();
 624                     group_select_current_widget (g);
 625                     print_flags (h);
 626                 }
 627             }
 628 
 629             if (result == KEY_LEFT)
 630             {
 631                 if (!is_owner)
 632                     chl_end = TRUE;
 633                 group_select_prev_widget (g);
 634                 f_pos--;
 635             }
 636             else if (result == KEY_RIGHT)
 637             {
 638                 if (is_owner)
 639                     chl_end = TRUE;
 640                 group_select_next_widget (g);
 641                 f_pos++;
 642             }
 643         }
 644 
 645         /* Here we used to redraw the window */
 646         dlg_destroy (chl_dlg);
 647     }
 648     while (chl_end);
 649 
 650     return 0;
 651 }
 652 
 653 /* --------------------------------------------------------------------------------------------- */
 654 
 655 static cb_ret_t
 656 advanced_chown_bg_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
     /* [previous][next][first][last][top][bottom][index][help]  */
 657 {
 658     switch (msg)
 659     {
 660     case MSG_DRAW:
 661         frame_callback (w, NULL, MSG_DRAW, 0, NULL);
 662         advanced_chown_refresh (DIALOG (w->owner));
 663         advanced_chown_info_update ();
 664         return MSG_HANDLED;
 665 
 666     default:
 667         return frame_callback (w, sender, msg, parm, data);
 668     }
 669 }
 670 
 671 /* --------------------------------------------------------------------------------------------- */
 672 
 673 static cb_ret_t
 674 advanced_chown_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
     /* [previous][next][first][last][top][bottom][index][help]  */
 675 {
 676     WGroup *g = GROUP (w);
 677     int i = 0;
 678 
 679     switch (msg)
 680     {
 681     case MSG_KEY:
 682         switch (parm)
 683         {
 684         case ALT ('x'):
 685             i++;
 686             MC_FALLTHROUGH;
 687 
 688         case ALT ('w'):
 689             i++;
 690             MC_FALLTHROUGH;
 691 
 692         case ALT ('r'):
 693             parm = i + 3;
 694             for (i = 0; i < 3; i++)
 695                 ch_flags[i * 3 + parm - 3] = (x_toggle & (1 << parm)) ? '-' : '+';
 696             x_toggle ^= (1 << parm);
 697             update_mode (g);
 698             widget_draw (w);
 699             break;
 700 
 701         case XCTRL ('x'):
 702             i++;
 703             MC_FALLTHROUGH;
 704 
 705         case XCTRL ('w'):
 706             i++;
 707             MC_FALLTHROUGH;
 708 
 709         case XCTRL ('r'):
 710             parm = i;
 711             for (i = 0; i < 3; i++)
 712                 ch_flags[i * 3 + parm] = (x_toggle & (1 << parm)) ? '-' : '+';
 713             x_toggle ^= (1 << parm);
 714             update_mode (g);
 715             widget_draw (w);
 716             break;
 717 
 718         default:
 719             break;
 720         }
 721         return MSG_NOT_HANDLED;
 722 
 723     default:
 724         return dlg_default_callback (w, sender, msg, parm, data);
 725     }
 726 }
 727 
 728 /* --------------------------------------------------------------------------------------------- */
 729 
 730 static WDialog *
 731 advanced_chown_dlg_create (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 732 {
 733     gboolean single_set;
 734     WDialog *ch_dlg;
 735     WGroup *ch_grp;
 736     int lines = 12;
 737     int cols = 74;
 738     int i;
 739     int y;
 740 
 741     memset (ch_flags, '=', 11);
 742     flag_pos = 0;
 743     x_toggle = 070;
 744 
 745     single_set = (current_panel->marked < 2);
 746     if (!single_set)
 747         lines += 2;
 748 
 749     ch_dlg =
 750         dlg_create (TRUE, 0, 0, lines, cols, WPOS_CENTER, FALSE, dialog_colors,
 751                     advanced_chown_callback, NULL, "[Advanced Chown]", _("Chown advanced command"));
 752     ch_grp = GROUP (ch_dlg);
 753 
 754     /* draw background */
 755     ch_dlg->bg->callback = advanced_chown_bg_callback;
 756 
 757     l_filename = label_new (2, 3, "");
 758     group_add_widget (ch_grp, l_filename);
 759 
 760     group_add_widget (ch_grp, hline_new (3, -1, -1));
 761 
 762 #define XTRACT(i,y,cb) y, BX+advanced_chown_but[i].x, \
 763         advanced_chown_but[i].ret_cmd, advanced_chown_but[i].flags, \
 764         (advanced_chown_but[i].text), cb
 765     b_att[0] = perm_button_new (XTRACT (0, BY, NULL));
 766     advanced_chown_but[0].id = group_add_widget (ch_grp, b_att[0]);
 767     b_att[1] = perm_button_new (XTRACT (1, BY, NULL));
 768     advanced_chown_but[1].id = group_add_widget (ch_grp, b_att[1]);
 769     b_att[2] = perm_button_new (XTRACT (2, BY, NULL));
 770     advanced_chown_but[2].id = group_add_widget (ch_grp, b_att[2]);
 771     b_user = button_new (XTRACT (3, BY, user_group_button_cb));
 772     advanced_chown_but[3].id = group_add_widget (ch_grp, b_user);
 773     b_group = button_new (XTRACT (4, BY, user_group_button_cb));
 774     advanced_chown_but[4].id = group_add_widget (ch_grp, b_group);
 775 
 776     l_mode = label_new (BY + 2, 3, "");
 777     group_add_widget (ch_grp, l_mode);
 778 
 779     y = BY + 3;
 780     if (!single_set)
 781     {
 782         i = BUTTONS_PERM;
 783         group_add_widget (ch_grp, hline_new (y++, -1, -1));
 784         advanced_chown_but[i].id = group_add_widget (ch_grp,
 785                                                      button_new (y,
 786                                                                  WIDGET (ch_dlg)->cols / 2 -
 787                                                                  advanced_chown_but[i].len,
 788                                                                  advanced_chown_but[i].ret_cmd,
 789                                                                  advanced_chown_but[i].flags,
 790                                                                  advanced_chown_but[i].text, NULL));
 791         i++;
 792         advanced_chown_but[i].id = group_add_widget (ch_grp,
 793                                                      button_new (y, WIDGET (ch_dlg)->cols / 2 + 1,
 794                                                                  advanced_chown_but[i].ret_cmd,
 795                                                                  advanced_chown_but[i].flags,
 796                                                                  advanced_chown_but[i].text, NULL));
 797         y++;
 798     }
 799 
 800     i = BUTTONS_PERM + 2;
 801     group_add_widget (ch_grp, hline_new (y++, -1, -1));
 802     advanced_chown_but[i].id = group_add_widget (ch_grp,
 803                                                  button_new (y,
 804                                                              WIDGET (ch_dlg)->cols / 2 -
 805                                                              advanced_chown_but[i].len,
 806                                                              advanced_chown_but[i].ret_cmd,
 807                                                              advanced_chown_but[i].flags,
 808                                                              advanced_chown_but[i].text, NULL));
 809     i++;
 810     advanced_chown_but[i].id = group_add_widget (ch_grp,
 811                                                  button_new (y, WIDGET (ch_dlg)->cols / 2 + 1,
 812                                                              advanced_chown_but[i].ret_cmd,
 813                                                              advanced_chown_but[i].flags,
 814                                                              advanced_chown_but[i].text, NULL));
 815 
 816     widget_select (WIDGET (b_att[0]));
 817 
 818     return ch_dlg;
 819 }
 820 
 821 /* --------------------------------------------------------------------------------------------- */
 822 
 823 static void
 824 advanced_chown_done (gboolean need_update)
     /* [previous][next][first][last][top][bottom][index][help]  */
 825 {
 826     if (need_update)
 827         update_panels (UP_OPTIMIZE, UP_KEEPSEL);
 828     repaint_screen ();
 829 }
 830 
 831 /* --------------------------------------------------------------------------------------------- */
 832 
 833 static const char *
 834 next_file (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 835 {
 836     while (!current_panel->dir.list[current_file].f.marked)
 837         current_file++;
 838 
 839     return current_panel->dir.list[current_file].fname;
 840 }
 841 
 842 /* --------------------------------------------------------------------------------------------- */
 843 
 844 static gboolean
 845 try_advanced_chown (const vfs_path_t * p, mode_t m, uid_t u, gid_t g)
     /* [previous][next][first][last][top][bottom][index][help]  */
 846 {
 847     int chmod_result;
 848     const char *fname;
 849 
 850     fname = x_basename (vfs_path_as_str (p));
 851 
 852     while ((chmod_result = mc_chmod (p, m)) == -1 && !ignore_all)
 853     {
 854         int my_errno = errno;
 855         int result;
 856         char *msg;
 857 
 858         msg = g_strdup_printf (_("Cannot chmod \"%s\"\n%s"), fname, unix_error_string (my_errno));
 859         result =
 860             query_dialog (MSG_ERROR, msg, D_ERROR, 4, _("&Ignore"), _("Ignore &all"), _("&Retry"),
 861                           _("&Cancel"));
 862         g_free (msg);
 863 
 864         switch (result)
 865         {
 866         case 0:
 867             /* call mc_chown() only, if mc_chmod() didn't fail */
 868             return TRUE;
 869 
 870         case 1:
 871             ignore_all = TRUE;
 872             /* call mc_chown() only, if mc_chmod() didn't fail */
 873             return TRUE;
 874 
 875         case 2:
 876             /* retry chmod of this file */
 877             break;
 878 
 879         case 3:
 880         default:
 881             /* stop remain files processing */
 882             return FALSE;
 883         }
 884     }
 885 
 886     /* call mc_chown() only, if mc_chmod didn't fail */
 887     while (chmod_result != -1 && mc_chown (p, u, g) == -1 && !ignore_all)
 888     {
 889         int my_errno = errno;
 890         int result;
 891         char *msg;
 892 
 893         msg = g_strdup_printf (_("Cannot chown \"%s\"\n%s"), fname, unix_error_string (my_errno));
 894         result =
 895             query_dialog (MSG_ERROR, msg, D_ERROR, 4, _("&Ignore"), _("Ignore &all"), _("&Retry"),
 896                           _("&Cancel"));
 897         g_free (msg);
 898 
 899         switch (result)
 900         {
 901         case 0:
 902             /* try next file */
 903             return TRUE;
 904 
 905         case 1:
 906             ignore_all = TRUE;
 907             /* try next file */
 908             return TRUE;
 909 
 910         case 2:
 911             /* retry chown of this file */
 912             break;
 913 
 914         case 3:
 915         default:
 916             /* stop remain files processing */
 917             return FALSE;
 918         }
 919     }
 920 
 921     return TRUE;
 922 
 923 }
 924 
 925 /* --------------------------------------------------------------------------------------------- */
 926 
 927 static gboolean
 928 do_advanced_chown (const vfs_path_t * p, mode_t m, uid_t u, gid_t g)
     /* [previous][next][first][last][top][bottom][index][help]  */
 929 {
 930     gboolean ret;
 931 
 932     ret = try_advanced_chown (p, m, u, g);
 933 
 934     do_file_mark (current_panel, current_file, 0);
 935 
 936     return ret;
 937 }
 938 
 939  /* --------------------------------------------------------------------------------------------- */
 940 
 941 static void
 942 apply_advanced_chowns (vfs_path_t * vpath, struct stat *sf)
     /* [previous][next][first][last][top][bottom][index][help]  */
 943 {
 944     gid_t a_gid = sf->st_gid;
 945     uid_t a_uid = sf->st_uid;
 946     gboolean ok;
 947 
 948     if (!do_advanced_chown
 949         (vpath, get_mode (), (ch_flags[9] == '+') ? a_uid : (uid_t) (-1),
 950          (ch_flags[10] == '+') ? a_gid : (gid_t) (-1)))
 951         return;
 952 
 953     do
 954     {
 955         const char *fname;
 956 
 957         fname = next_file ();
 958         vpath = vfs_path_from_str (fname);
 959         ok = (mc_stat (vpath, sf) == 0);
 960 
 961         if (!ok)
 962         {
 963             /* if current file was deleted outside mc -- try next file */
 964             /* decrease current_panel->marked */
 965             do_file_mark (current_panel, current_file, 0);
 966 
 967             /* try next file */
 968             ok = TRUE;
 969         }
 970         else
 971         {
 972             ch_cmode = sf->st_mode;
 973 
 974             ok = do_advanced_chown (vpath, get_mode (),
 975                                     (ch_flags[9] == '+') ? a_uid : (uid_t) (-1),
 976                                     (ch_flags[10] == '+') ? a_gid : (gid_t) (-1));
 977         }
 978 
 979         vfs_path_free (vpath);
 980     }
 981     while (ok && current_panel->marked != 0);
 982 }
 983 
 984 /* --------------------------------------------------------------------------------------------- */
 985 /*** public functions ****************************************************************************/
 986 /* --------------------------------------------------------------------------------------------- */
 987 
 988 void
 989 advanced_chown_cmd (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 990 {
 991     gboolean need_update;
 992     gboolean end_chown;
 993 
 994     /* Number of files at startup */
 995     int files_on_begin;
 996 
 997     files_on_begin = MAX (1, current_panel->marked);
 998 
 999     advanced_chown_init ();
1000 
1001     current_file = 0;
1002     ignore_all = FALSE;
1003 
1004     do
1005     {                           /* do while any files remaining */
1006         vfs_path_t *vpath;
1007         WDialog *ch_dlg;
1008         const char *fname;
1009         int result;
1010         int file_idx;
1011 
1012         do_refresh ();
1013 
1014         need_update = FALSE;
1015         end_chown = FALSE;
1016 
1017         if (current_panel->marked != 0)
1018             fname = next_file ();       /* next marked file */
1019         else
1020             fname = selection (current_panel)->fname;   /* single file */
1021 
1022         vpath = vfs_path_from_str (fname);
1023 
1024         if (mc_stat (vpath, &sf_stat) != 0)
1025         {
1026             vfs_path_free (vpath);
1027             break;
1028         }
1029 
1030         ch_cmode = sf_stat.st_mode;
1031 
1032         ch_dlg = advanced_chown_dlg_create ();
1033 
1034         file_idx = files_on_begin == 1 ? 1 : (files_on_begin - current_panel->marked + 1);
1035         label_set_textv (l_filename, "%s (%d/%d)",
1036                          str_fit_to_term (fname, WIDGET (ch_dlg)->cols - 20, J_LEFT_FIT),
1037                          file_idx, files_on_begin);
1038         update_ownership ();
1039 
1040         result = dlg_run (ch_dlg);
1041 
1042         switch (result)
1043         {
1044         case B_CANCEL:
1045             end_chown = TRUE;
1046             break;
1047 
1048         case B_ENTER:
1049             if (current_panel->marked <= 1)
1050             {
1051                 /* single or last file */
1052                 if (mc_chmod (vpath, get_mode ()) == -1)
1053                     message (D_ERROR, MSG_ERROR, _("Cannot chmod \"%s\"\n%s"),
1054                              fname, unix_error_string (errno));
1055                 /* call mc_chown only, if mc_chmod didn't fail */
1056                 else if (mc_chown
1057                          (vpath, (ch_flags[9] == '+') ? sf_stat.st_uid : (uid_t) (-1),
1058                           (ch_flags[10] == '+') ? sf_stat.st_gid : (gid_t) (-1)) == -1)
1059                     message (D_ERROR, MSG_ERROR, _("Cannot chown \"%s\"\n%s"), fname,
1060                              unix_error_string (errno));
1061 
1062                 end_chown = TRUE;
1063             }
1064             else if (!try_advanced_chown
1065                      (vpath, get_mode (), (ch_flags[9] == '+') ? sf_stat.st_uid : (uid_t) (-1),
1066                       (ch_flags[10] == '+') ? sf_stat.st_gid : (gid_t) (-1)))
1067             {
1068                 /* stop multiple files processing */
1069                 result = B_CANCEL;
1070                 end_chown = TRUE;
1071             }
1072 
1073             need_update = TRUE;
1074             break;
1075 
1076         case B_SETALL:
1077             apply_advanced_chowns (vpath, &sf_stat);
1078             need_update = TRUE;
1079             end_chown = TRUE;
1080             break;
1081 
1082         case B_SKIP:
1083         default:
1084             break;
1085         }
1086 
1087         if (current_panel->marked != 0 && result != B_CANCEL)
1088         {
1089             do_file_mark (current_panel, current_file, 0);
1090             need_update = TRUE;
1091         }
1092 
1093         vfs_path_free (vpath);
1094 
1095         dlg_destroy (ch_dlg);
1096     }
1097     while (current_panel->marked != 0 && !end_chown);
1098 
1099     advanced_chown_done (need_update);
1100 }
1101 
1102 /* --------------------------------------------------------------------------------------------- */

/* [previous][next][first][last][top][bottom][index][help]  */