root/src/filemanager/achown.c

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

DEFINITIONS

This source file includes following definitions.
  1. advanced_chown_i18n
  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_init
  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_i18n (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     char buffer[BUF_SMALL];
 309 
 310     /* mode */
 311     g_snprintf (buffer, sizeof (buffer), "Permissions (octal): %o", get_mode ());
 312     label_set_text (l_mode, buffer);
 313 
 314     /* permissions */
 315     update_permissions ();
 316 }
 317 
 318 /* --------------------------------------------------------------------------------------------- */
 319 
 320 static void
 321 update_mode (WGroup * g)
     /* [previous][next][first][last][top][bottom][index][help]  */
 322 {
 323     print_flags (DIALOG (g));
 324     advanced_chown_info_update ();
 325     widget_set_state (WIDGET (g->current->data), WST_FOCUSED, TRUE);
 326 }
 327 
 328 /* --------------------------------------------------------------------------------------------- */
 329 
 330 static cb_ret_t
 331 perm_button_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
     /* [previous][next][first][last][top][bottom][index][help]  */
 332 {
 333     WButton *b = BUTTON (w);
 334     WGroup *g = w->owner;
 335     int i = 0;
 336     int f_pos;
 337 
 338     /* one of permission buttons */
 339     if (b == b_att[0])
 340         f_pos = 0;
 341     else if (b == b_att[1])
 342         f_pos = 1;
 343     else                        /* if (w == b_att [1] */
 344         f_pos = 2;
 345 
 346     switch (msg)
 347     {
 348     case MSG_FOCUS:
 349         if (b->hotpos == -1)
 350             b->hotpos = 0;
 351 
 352         flag_pos = f_pos * 3 + b->hotpos;
 353         return MSG_HANDLED;
 354 
 355     case MSG_KEY:
 356         switch (parm)
 357         {
 358         case '*':
 359             parm = '=';
 360             MC_FALLTHROUGH;
 361 
 362         case '-':
 363         case '=':
 364         case '+':
 365             flag_pos = f_pos * 3 + b->hotpos;
 366             ch_flags[flag_pos] = parm;
 367             update_mode (g);
 368             send_message (w, NULL, MSG_KEY, KEY_RIGHT, NULL);
 369             if (b->hotpos == 2)
 370                 group_select_next_widget (g);
 371             break;
 372 
 373         case XCTRL ('f'):
 374         case KEY_RIGHT:
 375             {
 376                 cb_ret_t ret;
 377 
 378                 ret = inc_flag_pos ();
 379                 b->hotpos = flag_pos % 3;
 380                 return ret;
 381             }
 382 
 383         case XCTRL ('b'):
 384         case KEY_LEFT:
 385             {
 386                 cb_ret_t ret;
 387 
 388                 ret = dec_flag_pos ();
 389                 b->hotpos = flag_pos % 3;
 390                 return ret;
 391             }
 392 
 393         case 'x':
 394             i++;
 395             MC_FALLTHROUGH;
 396 
 397         case 'w':
 398             i++;
 399             MC_FALLTHROUGH;
 400 
 401         case 'r':
 402             b->hotpos = i;
 403             MC_FALLTHROUGH;
 404 
 405         case ' ':
 406             i = b->hotpos;
 407 
 408             flag_pos = f_pos * 3 + i;
 409             if (b->text.start[flag_pos % 3] == '-')
 410                 ch_flags[flag_pos] = '+';
 411             else
 412                 ch_flags[flag_pos] = '-';
 413             update_mode (w->owner);
 414             break;
 415 
 416         case '4':
 417             i++;
 418             MC_FALLTHROUGH;
 419 
 420         case '2':
 421             i++;
 422             MC_FALLTHROUGH;
 423 
 424         case '1':
 425             b->hotpos = i;
 426             flag_pos = f_pos * 3 + i;
 427             ch_flags[flag_pos] = '=';
 428             update_mode (g);
 429             break;
 430 
 431         default:
 432             break;
 433         }
 434         /* continue key handling in the dialog level */
 435         return MSG_NOT_HANDLED;
 436 
 437     default:
 438         return button_default_callback (w, sender, msg, parm, data);
 439     }
 440 }
 441 
 442 /* --------------------------------------------------------------------------------------------- */
 443 
 444 static void
 445 perm_button_mouse_callback (Widget * w, mouse_msg_t msg, mouse_event_t * event)
     /* [previous][next][first][last][top][bottom][index][help]  */
 446 {
 447     switch (msg)
 448     {
 449     case MSG_MOUSE_DOWN:
 450         /* place cursor on flag that is being modified */
 451         BUTTON (w)->hotpos = CLAMP (event->x - 1, 0, 2);
 452         MC_FALLTHROUGH;
 453 
 454     default:
 455         button_mouse_default_callback (w, msg, event);
 456         break;
 457     }
 458 }
 459 
 460 /* --------------------------------------------------------------------------------------------- */
 461 
 462 static WButton *
 463 perm_button_new (int y, int x, int action, button_flags_t flags, const char *text,
     /* [previous][next][first][last][top][bottom][index][help]  */
 464                  bcback_fn callback)
 465 {
 466     WButton *b;
 467     Widget *w;
 468 
 469     /* create base button using native API */
 470     b = button_new (y, x, action, flags, text, callback);
 471     w = WIDGET (b);
 472 
 473     /* we don't want HOTKEY */
 474     widget_want_hotkey (w, FALSE);
 475 
 476     w->callback = perm_button_callback;
 477     w->mouse_callback = perm_button_mouse_callback;
 478 
 479     return b;
 480 }
 481 
 482 /* --------------------------------------------------------------------------------------------- */
 483 
 484 static cb_ret_t
 485 chl_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
     /* [previous][next][first][last][top][bottom][index][help]  */
 486 {
 487     switch (msg)
 488     {
 489     case MSG_KEY:
 490         switch (parm)
 491         {
 492         case KEY_LEFT:
 493         case KEY_RIGHT:
 494             {
 495                 WDialog *h = DIALOG (w);
 496 
 497                 h->ret_value = parm;
 498                 dlg_stop (h);
 499             }
 500         default:
 501             break;
 502         }
 503         MC_FALLTHROUGH;
 504 
 505     default:
 506         return dlg_default_callback (w, sender, msg, parm, data);
 507     }
 508 }
 509 
 510 /* --------------------------------------------------------------------------------------------- */
 511 
 512 static int
 513 user_group_button_cb (WButton * button, int action)
     /* [previous][next][first][last][top][bottom][index][help]  */
 514 {
 515     Widget *w = WIDGET (button);
 516     int f_pos;
 517     gboolean chl_end;
 518 
 519     (void) action;
 520 
 521     if (button == b_user)
 522         f_pos = BUTTONS_PERM - 2;
 523     else if (button == b_group)
 524         f_pos = BUTTONS_PERM - 1;
 525     else
 526         return 0;               /* do nothing */
 527 
 528     do
 529     {
 530         WGroup *g = w->owner;
 531         WDialog *h = DIALOG (h);
 532         Widget *wh = WIDGET (h);
 533 
 534         gboolean is_owner = (f_pos == BUTTONS_PERM - 2);
 535         const char *title;
 536         int lxx, b_pos;
 537         WDialog *chl_dlg;
 538         WListbox *chl_list;
 539         int result;
 540         int fe;
 541         struct passwd *chl_pass;
 542         struct group *chl_grp;
 543 
 544         chl_end = FALSE;
 545 
 546         if (is_owner)
 547         {
 548             title = _("owner");
 549             lxx = WIDGET (b_user)->x + 1;
 550         }
 551         else
 552         {
 553             title = _("group");
 554             lxx = WIDGET (b_group)->x + 1;
 555         }
 556 
 557         chl_dlg =
 558             dlg_create (TRUE, wh->y - 1, lxx, wh->lines + 2, 17, WPOS_KEEP_DEFAULT, TRUE,
 559                         dialog_colors, chl_callback, NULL, "[Advanced Chown]", title);
 560 
 561         /* get new listboxes */
 562         chl_list =
 563             listbox_new (1, 1, WIDGET (chl_dlg)->lines - 2, WIDGET (chl_dlg)->cols - 2, FALSE,
 564                          NULL);
 565         listbox_add_item (chl_list, LISTBOX_APPEND_AT_END, 0, "<Unknown>", NULL, FALSE);
 566         if (is_owner)
 567         {
 568             /* get and put user names in the listbox */
 569             setpwent ();
 570             while ((chl_pass = getpwent ()) != NULL)
 571                 listbox_add_item (chl_list, LISTBOX_APPEND_SORTED, 0, chl_pass->pw_name, NULL,
 572                                   FALSE);
 573             endpwent ();
 574             fe = listbox_search_text (chl_list, get_owner (sf_stat.st_uid));
 575         }
 576         else
 577         {
 578             /* get and put group names in the listbox */
 579             setgrent ();
 580             while ((chl_grp = getgrent ()) != NULL)
 581                 listbox_add_item (chl_list, LISTBOX_APPEND_SORTED, 0, chl_grp->gr_name, NULL,
 582                                   FALSE);
 583             endgrent ();
 584             fe = listbox_search_text (chl_list, get_group (sf_stat.st_gid));
 585         }
 586 
 587         listbox_select_entry (chl_list, fe);
 588 
 589         b_pos = chl_list->pos;
 590         group_add_widget (GROUP (chl_dlg), chl_list);
 591 
 592         result = dlg_run (chl_dlg);
 593 
 594         if (result != B_CANCEL)
 595         {
 596             if (b_pos != chl_list->pos)
 597             {
 598                 gboolean ok = FALSE;
 599                 char *text;
 600 
 601                 listbox_get_current (chl_list, &text, NULL);
 602                 if (is_owner)
 603                 {
 604                     chl_pass = getpwnam (text);
 605                     if (chl_pass != NULL)
 606                     {
 607                         sf_stat.st_uid = chl_pass->pw_uid;
 608                         ok = TRUE;
 609                     }
 610                 }
 611                 else
 612                 {
 613                     chl_grp = getgrnam (text);
 614                     if (chl_grp != NULL)
 615                     {
 616                         sf_stat.st_gid = chl_grp->gr_gid;
 617                         ok = TRUE;
 618                     }
 619                 }
 620 
 621                 if (!ok)
 622                     group_select_current_widget (g);
 623                 else
 624                 {
 625                     ch_flags[f_pos + 6] = '+';
 626                     update_ownership ();
 627                     group_select_current_widget (g);
 628                     print_flags (h);
 629                 }
 630             }
 631 
 632             if (result == KEY_LEFT)
 633             {
 634                 if (!is_owner)
 635                     chl_end = TRUE;
 636                 group_select_prev_widget (g);
 637                 f_pos--;
 638             }
 639             else if (result == KEY_RIGHT)
 640             {
 641                 if (is_owner)
 642                     chl_end = TRUE;
 643                 group_select_next_widget (g);
 644                 f_pos++;
 645             }
 646         }
 647 
 648         /* Here we used to redraw the window */
 649         dlg_destroy (chl_dlg);
 650     }
 651     while (chl_end);
 652 
 653     return 0;
 654 }
 655 
 656 /* --------------------------------------------------------------------------------------------- */
 657 
 658 static cb_ret_t
 659 advanced_chown_bg_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
     /* [previous][next][first][last][top][bottom][index][help]  */
 660 {
 661     switch (msg)
 662     {
 663     case MSG_DRAW:
 664         frame_callback (w, NULL, MSG_DRAW, 0, NULL);
 665         advanced_chown_refresh (DIALOG (w->owner));
 666         advanced_chown_info_update ();
 667         return MSG_HANDLED;
 668 
 669     default:
 670         return frame_callback (w, sender, msg, parm, data);
 671     }
 672 }
 673 
 674 /* --------------------------------------------------------------------------------------------- */
 675 
 676 static cb_ret_t
 677 advanced_chown_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
     /* [previous][next][first][last][top][bottom][index][help]  */
 678 {
 679     WGroup *g = GROUP (w);
 680     int i = 0;
 681 
 682     switch (msg)
 683     {
 684     case MSG_KEY:
 685         switch (parm)
 686         {
 687         case ALT ('x'):
 688             i++;
 689             MC_FALLTHROUGH;
 690 
 691         case ALT ('w'):
 692             i++;
 693             MC_FALLTHROUGH;
 694 
 695         case ALT ('r'):
 696             parm = i + 3;
 697             for (i = 0; i < 3; i++)
 698                 ch_flags[i * 3 + parm - 3] = (x_toggle & (1 << parm)) ? '-' : '+';
 699             x_toggle ^= (1 << parm);
 700             update_mode (g);
 701             widget_draw (w);
 702             break;
 703 
 704         case XCTRL ('x'):
 705             i++;
 706             MC_FALLTHROUGH;
 707 
 708         case XCTRL ('w'):
 709             i++;
 710             MC_FALLTHROUGH;
 711 
 712         case XCTRL ('r'):
 713             parm = i;
 714             for (i = 0; i < 3; i++)
 715                 ch_flags[i * 3 + parm] = (x_toggle & (1 << parm)) ? '-' : '+';
 716             x_toggle ^= (1 << parm);
 717             update_mode (g);
 718             widget_draw (w);
 719             break;
 720 
 721         default:
 722             break;
 723         }
 724         return MSG_NOT_HANDLED;
 725 
 726     default:
 727         return dlg_default_callback (w, sender, msg, parm, data);
 728     }
 729 }
 730 
 731 /* --------------------------------------------------------------------------------------------- */
 732 
 733 static WDialog *
 734 advanced_chown_init (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 735 {
 736     gboolean single_set;
 737     WDialog *ch_dlg;
 738     WGroup *ch_grp;
 739     int lines = 12;
 740     int cols = 74;
 741     int i;
 742     int y;
 743 
 744     memset (ch_flags, '=', 11);
 745     flag_pos = 0;
 746     x_toggle = 070;
 747 
 748     single_set = (current_panel->marked < 2);
 749     if (!single_set)
 750         lines += 2;
 751 
 752     ch_dlg =
 753         dlg_create (TRUE, 0, 0, lines, cols, WPOS_CENTER, FALSE, dialog_colors,
 754                     advanced_chown_callback, NULL, "[Advanced Chown]", _("Chown advanced command"));
 755     ch_grp = GROUP (ch_dlg);
 756 
 757     /* draw background */
 758     ch_dlg->bg->callback = advanced_chown_bg_callback;
 759 
 760     l_filename = label_new (2, 3, "");
 761     group_add_widget (ch_grp, l_filename);
 762 
 763     group_add_widget (ch_grp, hline_new (3, -1, -1));
 764 
 765 #define XTRACT(i,y,cb) y, BX+advanced_chown_but[i].x, \
 766         advanced_chown_but[i].ret_cmd, advanced_chown_but[i].flags, \
 767         (advanced_chown_but[i].text), cb
 768     b_att[0] = perm_button_new (XTRACT (0, BY, NULL));
 769     advanced_chown_but[0].id = group_add_widget (ch_grp, b_att[0]);
 770     b_att[1] = perm_button_new (XTRACT (1, BY, NULL));
 771     advanced_chown_but[1].id = group_add_widget (ch_grp, b_att[1]);
 772     b_att[2] = perm_button_new (XTRACT (2, BY, NULL));
 773     advanced_chown_but[2].id = group_add_widget (ch_grp, b_att[2]);
 774     b_user = button_new (XTRACT (3, BY, user_group_button_cb));
 775     advanced_chown_but[3].id = group_add_widget (ch_grp, b_user);
 776     b_group = button_new (XTRACT (4, BY, user_group_button_cb));
 777     advanced_chown_but[4].id = group_add_widget (ch_grp, b_group);
 778 
 779     l_mode = label_new (BY + 2, 3, "");
 780     group_add_widget (ch_grp, l_mode);
 781 
 782     y = BY + 3;
 783     if (!single_set)
 784     {
 785         i = BUTTONS_PERM;
 786         group_add_widget (ch_grp, hline_new (y++, -1, -1));
 787         advanced_chown_but[i].id = group_add_widget (ch_grp,
 788                                                      button_new (y,
 789                                                                  WIDGET (ch_dlg)->cols / 2 -
 790                                                                  advanced_chown_but[i].len,
 791                                                                  advanced_chown_but[i].ret_cmd,
 792                                                                  advanced_chown_but[i].flags,
 793                                                                  advanced_chown_but[i].text, NULL));
 794         i++;
 795         advanced_chown_but[i].id = group_add_widget (ch_grp,
 796                                                      button_new (y, WIDGET (ch_dlg)->cols / 2 + 1,
 797                                                                  advanced_chown_but[i].ret_cmd,
 798                                                                  advanced_chown_but[i].flags,
 799                                                                  advanced_chown_but[i].text, NULL));
 800         y++;
 801     }
 802 
 803     i = BUTTONS_PERM + 2;
 804     group_add_widget (ch_grp, hline_new (y++, -1, -1));
 805     advanced_chown_but[i].id = group_add_widget (ch_grp,
 806                                                  button_new (y,
 807                                                              WIDGET (ch_dlg)->cols / 2 -
 808                                                              advanced_chown_but[i].len,
 809                                                              advanced_chown_but[i].ret_cmd,
 810                                                              advanced_chown_but[i].flags,
 811                                                              advanced_chown_but[i].text, NULL));
 812     i++;
 813     advanced_chown_but[i].id = group_add_widget (ch_grp,
 814                                                  button_new (y, WIDGET (ch_dlg)->cols / 2 + 1,
 815                                                              advanced_chown_but[i].ret_cmd,
 816                                                              advanced_chown_but[i].flags,
 817                                                              advanced_chown_but[i].text, NULL));
 818 
 819     widget_select (WIDGET (b_att[0]));
 820 
 821     return ch_dlg;
 822 }
 823 
 824 /* --------------------------------------------------------------------------------------------- */
 825 
 826 static void
 827 advanced_chown_done (gboolean need_update)
     /* [previous][next][first][last][top][bottom][index][help]  */
 828 {
 829     if (need_update)
 830         update_panels (UP_OPTIMIZE, UP_KEEPSEL);
 831     repaint_screen ();
 832 }
 833 
 834 /* --------------------------------------------------------------------------------------------- */
 835 
 836 static const char *
 837 next_file (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 838 {
 839     while (!current_panel->dir.list[current_file].f.marked)
 840         current_file++;
 841 
 842     return current_panel->dir.list[current_file].fname;
 843 }
 844 
 845 /* --------------------------------------------------------------------------------------------- */
 846 
 847 static gboolean
 848 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]  */
 849 {
 850     int chmod_result;
 851     const char *fname;
 852 
 853     fname = x_basename (vfs_path_as_str (p));
 854 
 855     while ((chmod_result = mc_chmod (p, m)) == -1 && !ignore_all)
 856     {
 857         int my_errno = errno;
 858         int result;
 859         char *msg;
 860 
 861         msg = g_strdup_printf (_("Cannot chmod \"%s\"\n%s"), fname, unix_error_string (my_errno));
 862         result =
 863             query_dialog (MSG_ERROR, msg, D_ERROR, 4, _("&Ignore"), _("Ignore &all"), _("&Retry"),
 864                           _("&Cancel"));
 865         g_free (msg);
 866 
 867         switch (result)
 868         {
 869         case 0:
 870             /* call mc_chown() only, if mc_chmod() didn't fail */
 871             return TRUE;
 872 
 873         case 1:
 874             ignore_all = TRUE;
 875             /* call mc_chown() only, if mc_chmod() didn't fail */
 876             return TRUE;
 877 
 878         case 2:
 879             /* retry chmod of this file */
 880             break;
 881 
 882         case 3:
 883         default:
 884             /* stop remain files processing */
 885             return FALSE;
 886         }
 887     }
 888 
 889     /* call mc_chown() only, if mc_chmod didn't fail */
 890     while (chmod_result != -1 && mc_chown (p, u, g) == -1 && !ignore_all)
 891     {
 892         int my_errno = errno;
 893         int result;
 894         char *msg;
 895 
 896         msg = g_strdup_printf (_("Cannot chown \"%s\"\n%s"), fname, unix_error_string (my_errno));
 897         result =
 898             query_dialog (MSG_ERROR, msg, D_ERROR, 4, _("&Ignore"), _("Ignore &all"), _("&Retry"),
 899                           _("&Cancel"));
 900         g_free (msg);
 901 
 902         switch (result)
 903         {
 904         case 0:
 905             /* try next file */
 906             return TRUE;
 907 
 908         case 1:
 909             ignore_all = TRUE;
 910             /* try next file */
 911             return TRUE;
 912 
 913         case 2:
 914             /* retry chown of this file */
 915             break;
 916 
 917         case 3:
 918         default:
 919             /* stop remain files processing */
 920             return FALSE;
 921         }
 922     }
 923 
 924     return TRUE;
 925 
 926 }
 927 
 928 /* --------------------------------------------------------------------------------------------- */
 929 
 930 static gboolean
 931 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]  */
 932 {
 933     gboolean ret;
 934 
 935     ret = try_advanced_chown (p, m, u, g);
 936 
 937     do_file_mark (current_panel, current_file, 0);
 938 
 939     return ret;
 940 }
 941 
 942  /* --------------------------------------------------------------------------------------------- */
 943 
 944 static void
 945 apply_advanced_chowns (vfs_path_t * vpath, struct stat *sf)
     /* [previous][next][first][last][top][bottom][index][help]  */
 946 {
 947     gid_t a_gid = sf->st_gid;
 948     uid_t a_uid = sf->st_uid;
 949     gboolean ok;
 950 
 951     if (!do_advanced_chown
 952         (vpath, get_mode (), (ch_flags[9] == '+') ? a_uid : (uid_t) (-1),
 953          (ch_flags[10] == '+') ? a_gid : (gid_t) (-1)))
 954         return;
 955 
 956     do
 957     {
 958         const char *fname;
 959 
 960         fname = next_file ();
 961         vpath = vfs_path_from_str (fname);
 962         ok = (mc_stat (vpath, sf) == 0);
 963 
 964         if (!ok)
 965         {
 966             /* if current file was deleted outside mc -- try next file */
 967             /* decrease current_panel->marked */
 968             do_file_mark (current_panel, current_file, 0);
 969 
 970             /* try next file */
 971             ok = TRUE;
 972         }
 973         else
 974         {
 975             ch_cmode = sf->st_mode;
 976 
 977             ok = do_advanced_chown (vpath, get_mode (),
 978                                     (ch_flags[9] == '+') ? a_uid : (uid_t) (-1),
 979                                     (ch_flags[10] == '+') ? a_gid : (gid_t) (-1));
 980         }
 981 
 982         vfs_path_free (vpath);
 983     }
 984     while (ok && current_panel->marked != 0);
 985 }
 986 
 987 /* --------------------------------------------------------------------------------------------- */
 988 /*** public functions ****************************************************************************/
 989 /* --------------------------------------------------------------------------------------------- */
 990 
 991 void
 992 advanced_chown_cmd (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 993 {
 994     gboolean need_update;
 995     gboolean end_chown;
 996 
 997     /* Number of files at startup */
 998     int files_on_begin;
 999 
1000     files_on_begin = MAX (1, current_panel->marked);
1001 
1002     advanced_chown_i18n ();
1003 
1004     current_file = 0;
1005     ignore_all = FALSE;
1006 
1007     do
1008     {                           /* do while any files remaining */
1009         vfs_path_t *vpath;
1010         WDialog *ch_dlg;
1011         const char *fname;
1012         int result;
1013         int file_idx;
1014         char buffer[BUF_MEDIUM];
1015 
1016         do_refresh ();
1017 
1018         need_update = FALSE;
1019         end_chown = FALSE;
1020 
1021         if (current_panel->marked != 0)
1022             fname = next_file ();       /* next marked file */
1023         else
1024             fname = selection (current_panel)->fname;   /* single file */
1025 
1026         vpath = vfs_path_from_str (fname);
1027 
1028         if (mc_stat (vpath, &sf_stat) != 0)
1029         {
1030             vfs_path_free (vpath);
1031             break;
1032         }
1033 
1034         ch_cmode = sf_stat.st_mode;
1035 
1036         ch_dlg = advanced_chown_init ();
1037 
1038         file_idx = files_on_begin == 1 ? 1 : (files_on_begin - current_panel->marked + 1);
1039         g_snprintf (buffer, sizeof (buffer), "%s (%d/%d)",
1040                     str_fit_to_term (fname, WIDGET (ch_dlg)->cols - 20, J_LEFT_FIT),
1041                     file_idx, files_on_begin);
1042         label_set_text (l_filename, buffer);
1043         update_ownership ();
1044 
1045         result = dlg_run (ch_dlg);
1046 
1047         switch (result)
1048         {
1049         case B_CANCEL:
1050             end_chown = TRUE;
1051             break;
1052 
1053         case B_ENTER:
1054             if (current_panel->marked <= 1)
1055             {
1056                 /* single or last file */
1057                 if (mc_chmod (vpath, get_mode ()) == -1)
1058                     message (D_ERROR, MSG_ERROR, _("Cannot chmod \"%s\"\n%s"),
1059                              fname, unix_error_string (errno));
1060                 /* call mc_chown only, if mc_chmod didn't fail */
1061                 else if (mc_chown
1062                          (vpath, (ch_flags[9] == '+') ? sf_stat.st_uid : (uid_t) (-1),
1063                           (ch_flags[10] == '+') ? sf_stat.st_gid : (gid_t) (-1)) == -1)
1064                     message (D_ERROR, MSG_ERROR, _("Cannot chown \"%s\"\n%s"), fname,
1065                              unix_error_string (errno));
1066 
1067                 end_chown = TRUE;
1068             }
1069             else if (!try_advanced_chown
1070                      (vpath, get_mode (), (ch_flags[9] == '+') ? sf_stat.st_uid : (uid_t) (-1),
1071                       (ch_flags[10] == '+') ? sf_stat.st_gid : (gid_t) (-1)))
1072             {
1073                 /* stop multiple files processing */
1074                 result = B_CANCEL;
1075                 end_chown = TRUE;
1076             }
1077 
1078             need_update = TRUE;
1079             break;
1080 
1081         case B_SETALL:
1082             apply_advanced_chowns (vpath, &sf_stat);
1083             need_update = TRUE;
1084             end_chown = TRUE;
1085             break;
1086 
1087         case B_SKIP:
1088         default:
1089             break;
1090         }
1091 
1092         if (current_panel->marked != 0 && result != B_CANCEL)
1093         {
1094             do_file_mark (current_panel, current_file, 0);
1095             need_update = TRUE;
1096         }
1097 
1098         vfs_path_free (vpath);
1099 
1100         dlg_destroy (ch_dlg);
1101     }
1102     while (current_panel->marked != 0 && !end_chown);
1103 
1104     advanced_chown_done (need_update);
1105 }
1106 
1107 /* --------------------------------------------------------------------------------------------- */

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