root/src/filemanager/chown.c

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

DEFINITIONS

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

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