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_bg_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-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 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 (const Widget * h)
     /* [previous][next][first][last][top][bottom][index][help]  */
 152 {
 153     int y = 3;
 154     int x = 7 + GW * 2;
 155 
 156     tty_setcolor (COLOR_NORMAL);
 157 
 158     widget_gotoyx (h, y + 0, x);
 159     tty_print_string (_("Name"));
 160     widget_gotoyx (h, y + 2, x);
 161     tty_print_string (_("Owner name"));
 162     widget_gotoyx (h, y + 4, x);
 163     tty_print_string (_("Group name"));
 164     widget_gotoyx (h, y + 6, x);
 165     tty_print_string (_("Size"));
 166     widget_gotoyx (h, y + 8, x);
 167     tty_print_string (_("Permission"));
 168 }
 169 
 170 /* --------------------------------------------------------------------------------------------- */
 171 
 172 static cb_ret_t
 173 chown_bg_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
     /* [previous][next][first][last][top][bottom][index][help]  */
 174 {
 175     switch (msg)
 176     {
 177     case MSG_DRAW:
 178         frame_callback (w, NULL, MSG_DRAW, 0, NULL);
 179         chown_refresh (WIDGET (w->owner));
 180         return MSG_HANDLED;
 181 
 182     default:
 183         return frame_callback (w, sender, msg, parm, data);
 184     }
 185 }
 186 
 187 /* --------------------------------------------------------------------------------------------- */
 188 
 189 static WDialog *
 190 chown_init (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 191 {
 192     int single_set;
 193     WDialog *ch_dlg;
 194     WGroup *g;
 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, NULL, NULL,
 206                     "[Chown]", _("Chown command"));
 207     g = GROUP (ch_dlg);
 208 
 209     /* draw background */
 210     ch_dlg->bg->callback = chown_bg_callback;
 211 
 212     group_add_widget (g, groupbox_new (2, 3, GH, GW, _("User name")));
 213     l_user = listbox_new (3, 4, GH - 2, GW - 2, FALSE, NULL);
 214     group_add_widget (g, l_user);
 215     /* add field for unknown names (numbers) */
 216     listbox_add_item (l_user, LISTBOX_APPEND_AT_END, 0, _("<Unknown user>"), NULL, FALSE);
 217     /* get and put user names in the listbox */
 218     setpwent ();
 219     while ((l_pass = getpwent ()) != NULL)
 220         listbox_add_item (l_user, LISTBOX_APPEND_SORTED, 0, l_pass->pw_name, NULL, FALSE);
 221     endpwent ();
 222 
 223     group_add_widget (g, groupbox_new (2, 4 + GW, GH, GW, _("Group name")));
 224     l_group = listbox_new (3, 5 + GW, GH - 2, GW - 2, FALSE, NULL);
 225     group_add_widget (g, l_group);
 226     /* add field for unknown names (numbers) */
 227     listbox_add_item (l_group, LISTBOX_APPEND_AT_END, 0, _("<Unknown group>"), NULL, FALSE);
 228     /* get and put group names in the listbox */
 229     setgrent ();
 230     while ((l_grp = getgrent ()) != NULL)
 231         listbox_add_item (l_group, LISTBOX_APPEND_SORTED, 0, l_grp->gr_name, NULL, FALSE);
 232     endgrent ();
 233 
 234     group_add_widget (g, groupbox_new (2, 5 + GW * 2, GH, GW, _("File")));
 235     /* add widgets for the file information */
 236     for (i = 0; i < LABELS; i++)
 237     {
 238         chown_label[i].l = label_new (chown_label[i].y, 7 + GW * 2, "");
 239         group_add_widget (g, chown_label[i].l);
 240     }
 241 
 242     if (single_set == 0)
 243     {
 244         int x;
 245 
 246         group_add_widget (g, hline_new (lines - chown_but[0].y - 1, -1, -1));
 247 
 248         y = lines - chown_but[0].y;
 249         x = (cols - blen) / 2;
 250 
 251         for (i = 0; i < BUTTONS - 2; i++)
 252         {
 253             group_add_widget (g, button_new (y, x, chown_but[i].ret_cmd, chown_but[i].flags,
 254                                              chown_but[i].text, NULL));
 255             x += chown_but[i].len + 1;
 256         }
 257     }
 258 
 259     i = BUTTONS - 2;
 260     y = lines - chown_but[i].y;
 261     group_add_widget (g, hline_new (y - 1, -1, -1));
 262     group_add_widget (g, button_new (y, WIDGET (ch_dlg)->cols / 2 - chown_but[i].len,
 263                                      chown_but[i].ret_cmd, chown_but[i].flags, chown_but[i].text,
 264                                      NULL));
 265     i++;
 266     group_add_widget (g, button_new (y, WIDGET (ch_dlg)->cols / 2 + 1, chown_but[i].ret_cmd,
 267                                      chown_but[i].flags, chown_but[i].text, NULL));
 268 
 269     /* select first listbox */
 270     widget_select (WIDGET (l_user));
 271 
 272     return ch_dlg;
 273 }
 274 
 275 /* --------------------------------------------------------------------------------------------- */
 276 
 277 static void
 278 chown_done (gboolean need_update)
     /* [previous][next][first][last][top][bottom][index][help]  */
 279 {
 280     if (need_update)
 281         update_panels (UP_OPTIMIZE, UP_KEEPSEL);
 282     repaint_screen ();
 283 }
 284 
 285 /* --------------------------------------------------------------------------------------------- */
 286 
 287 static const char *
 288 next_file (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 289 {
 290     while (!current_panel->dir.list[current_file].f.marked)
 291         current_file++;
 292 
 293     return current_panel->dir.list[current_file].fname;
 294 }
 295 
 296 /* --------------------------------------------------------------------------------------------- */
 297 
 298 static gboolean
 299 try_chown (const vfs_path_t * p, uid_t u, gid_t g)
     /* [previous][next][first][last][top][bottom][index][help]  */
 300 {
 301     while (mc_chown (p, u, g) == -1 && !ignore_all)
 302     {
 303         int my_errno = errno;
 304         int result;
 305         char *msg;
 306 
 307         msg =
 308             g_strdup_printf (_("Cannot chown \"%s\"\n%s"), x_basename (vfs_path_as_str (p)),
 309                              unix_error_string (my_errno));
 310         result =
 311             query_dialog (MSG_ERROR, msg, D_ERROR, 4, _("&Ignore"), _("Ignore &all"), _("&Retry"),
 312                           _("&Cancel"));
 313         g_free (msg);
 314 
 315         switch (result)
 316         {
 317         case 0:
 318             /* try next file */
 319             return TRUE;
 320 
 321         case 1:
 322             ignore_all = TRUE;
 323             /* try next file */
 324             return TRUE;
 325 
 326         case 2:
 327             /* retry this file */
 328             break;
 329 
 330         case 3:
 331         default:
 332             /* stop remain files processing */
 333             return FALSE;
 334         }
 335     }
 336 
 337     return TRUE;
 338 }
 339 
 340 /* --------------------------------------------------------------------------------------------- */
 341 
 342 static gboolean
 343 do_chown (const vfs_path_t * p, uid_t u, gid_t g)
     /* [previous][next][first][last][top][bottom][index][help]  */
 344 {
 345     gboolean ret;
 346 
 347     ret = try_chown (p, u, g);
 348 
 349     do_file_mark (current_panel, current_file, 0);
 350 
 351     return ret;
 352 }
 353 
 354 /* --------------------------------------------------------------------------------------------- */
 355 
 356 static void
 357 apply_chowns (vfs_path_t * vpath, uid_t u, gid_t g)
     /* [previous][next][first][last][top][bottom][index][help]  */
 358 {
 359     gboolean ok;
 360 
 361     if (!do_chown (vpath, u, g))
 362         return;
 363 
 364     do
 365     {
 366         const char *fname;
 367         struct stat sf;
 368 
 369         fname = next_file ();
 370         vpath = vfs_path_from_str (fname);
 371         ok = (mc_stat (vpath, &sf) == 0);
 372 
 373         if (!ok)
 374         {
 375             /* if current file was deleted outside mc -- try next file */
 376             /* decrease current_panel->marked */
 377             do_file_mark (current_panel, current_file, 0);
 378 
 379             /* try next file */
 380             ok = TRUE;
 381         }
 382         else
 383             ok = do_chown (vpath, u, g);
 384 
 385         vfs_path_free (vpath);
 386     }
 387     while (ok && current_panel->marked != 0);
 388 }
 389 
 390 /* --------------------------------------------------------------------------------------------- */
 391 /*** public functions ****************************************************************************/
 392 /* --------------------------------------------------------------------------------------------- */
 393 
 394 void
 395 chown_cmd (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 396 {
 397     gboolean need_update;
 398     gboolean end_chown;
 399 
 400     chown_i18n ();
 401 
 402     current_file = 0;
 403     ignore_all = FALSE;
 404 
 405     do
 406     {                           /* do while any files remaining */
 407         vfs_path_t *vpath;
 408         WDialog *ch_dlg;
 409         struct stat sf_stat;
 410         const char *fname;
 411         int result;
 412         char buffer[BUF_TINY];
 413         uid_t new_user = (uid_t) (-1);
 414         gid_t new_group = (gid_t) (-1);
 415 
 416         do_refresh ();
 417 
 418         need_update = FALSE;
 419         end_chown = FALSE;
 420 
 421         if (current_panel->marked != 0)
 422             fname = next_file ();       /* next marked file */
 423         else
 424             fname = selection (current_panel)->fname;   /* single file */
 425 
 426         vpath = vfs_path_from_str (fname);
 427 
 428         if (mc_stat (vpath, &sf_stat) != 0)
 429         {
 430             vfs_path_free (vpath);
 431             break;
 432         }
 433 
 434         ch_dlg = chown_init ();
 435 
 436         /* select in listboxes */
 437         listbox_select_entry (l_user, listbox_search_text (l_user, get_owner (sf_stat.st_uid)));
 438         listbox_select_entry (l_group, listbox_search_text (l_group, get_group (sf_stat.st_gid)));
 439 
 440         chown_label (0, str_trunc (fname, GW - 4));
 441         chown_label (1, str_trunc (get_owner (sf_stat.st_uid), GW - 4));
 442         chown_label (2, str_trunc (get_group (sf_stat.st_gid), GW - 4));
 443         size_trunc_len (buffer, GW - 4, sf_stat.st_size, 0, panels_options.kilobyte_si);
 444         chown_label (3, buffer);
 445         chown_label (4, string_perm (sf_stat.st_mode));
 446 
 447         result = dlg_run (ch_dlg);
 448 
 449         switch (result)
 450         {
 451         case B_CANCEL:
 452             end_chown = TRUE;
 453             break;
 454 
 455         case B_ENTER:
 456         case B_SETALL:
 457             {
 458                 struct group *grp;
 459                 struct passwd *user;
 460                 char *text;
 461 
 462                 listbox_get_current (l_group, &text, NULL);
 463                 grp = getgrnam (text);
 464                 if (grp != NULL)
 465                     new_group = grp->gr_gid;
 466                 listbox_get_current (l_user, &text, NULL);
 467                 user = getpwnam (text);
 468                 if (user != NULL)
 469                     new_user = user->pw_uid;
 470                 if (result == B_ENTER)
 471                 {
 472                     if (current_panel->marked <= 1)
 473                     {
 474                         /* single or last file */
 475                         if (mc_chown (vpath, new_user, new_group) == -1)
 476                             message (D_ERROR, MSG_ERROR, _("Cannot chown \"%s\"\n%s"),
 477                                      fname, unix_error_string (errno));
 478                         end_chown = TRUE;
 479                     }
 480                     else if (!try_chown (vpath, new_user, new_group))
 481                     {
 482                         /* stop multiple files processing */
 483                         result = B_CANCEL;
 484                         end_chown = TRUE;
 485                     }
 486                 }
 487                 else
 488                 {
 489                     apply_chowns (vpath, new_user, new_group);
 490                     end_chown = TRUE;
 491                 }
 492 
 493                 need_update = TRUE;
 494                 break;
 495             }
 496 
 497         case B_SETUSR:
 498             {
 499                 struct passwd *user;
 500                 char *text;
 501 
 502                 listbox_get_current (l_user, &text, NULL);
 503                 user = getpwnam (text);
 504                 if (user != NULL)
 505                 {
 506                     new_user = user->pw_uid;
 507                     apply_chowns (vpath, new_user, new_group);
 508                     need_update = TRUE;
 509                     end_chown = TRUE;
 510                 }
 511                 break;
 512             }
 513 
 514         case B_SETGRP:
 515             {
 516                 struct group *grp;
 517                 char *text;
 518 
 519                 listbox_get_current (l_group, &text, NULL);
 520                 grp = getgrnam (text);
 521                 if (grp != NULL)
 522                 {
 523                     new_group = grp->gr_gid;
 524                     apply_chowns (vpath, new_user, new_group);
 525                     need_update = TRUE;
 526                     end_chown = TRUE;
 527                 }
 528                 break;
 529             }
 530 
 531         default:
 532             break;
 533         }
 534 
 535         if (current_panel->marked != 0 && result != B_CANCEL)
 536         {
 537             do_file_mark (current_panel, current_file, 0);
 538             need_update = TRUE;
 539         }
 540 
 541         vfs_path_free (vpath);
 542 
 543         dlg_destroy (ch_dlg);
 544     }
 545     while (current_panel->marked != 0 && !end_chown);
 546 
 547     chown_done (need_update);
 548 }
 549 
 550 /* --------------------------------------------------------------------------------------------- */

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