root/src/filemanager/chown.c

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

DEFINITIONS

This source file includes following definitions.
  1. chown_i18n
  2. chown_refresh
  3. chown_callback
  4. chown_init
  5. chown_done
  6. next_file
  7. try_chown
  8. do_chown
  9. apply_chowns
  10. chown_cmd

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

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