(root)/
tar-1.35/
lib/
paxerror.c
       1  /* Miscellaneous error functions
       2  
       3     Copyright (C) 2005, 2007, 2023 Free Software Foundation, Inc.
       4  
       5     This program is free software; you can redistribute it and/or modify it
       6     under the terms of the GNU General Public License as published by the
       7     Free Software Foundation; either version 3, or (at your option) any later
       8     version.
       9  
      10     This program is distributed in the hope that it will be useful, but
      11     WITHOUT ANY WARRANTY; without even the implied warranty of
      12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
      13     Public License for more details.
      14  
      15     You should have received a copy of the GNU General Public License along
      16     with this program; if not, write to the Free Software Foundation, Inc.,
      17     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
      18  
      19  #include <system.h>
      20  #include <paxlib.h>
      21  #include <quote.h>
      22  #include <quotearg.h>
      23  
      24  void (*error_hook) (void);
      25  
      26  /* Decode MODE from its binary form in a stat structure, and encode it
      27     into a 9-byte string STRING, terminated with a NUL.  */
      28  
      29  void
      30  pax_decode_mode (mode_t mode, char *string)
      31  {
      32    *string++ = mode & S_IRUSR ? 'r' : '-';
      33    *string++ = mode & S_IWUSR ? 'w' : '-';
      34    *string++ = (mode & S_ISUID
      35  	       ? (mode & S_IXUSR ? 's' : 'S')
      36  	       : (mode & S_IXUSR ? 'x' : '-'));
      37    *string++ = mode & S_IRGRP ? 'r' : '-';
      38    *string++ = mode & S_IWGRP ? 'w' : '-';
      39    *string++ = (mode & S_ISGID
      40  	       ? (mode & S_IXGRP ? 's' : 'S')
      41  	       : (mode & S_IXGRP ? 'x' : '-'));
      42    *string++ = mode & S_IROTH ? 'r' : '-';
      43    *string++ = mode & S_IWOTH ? 'w' : '-';
      44    *string++ = (mode & S_ISVTX
      45  	       ? (mode & S_IXOTH ? 't' : 'T')
      46  	       : (mode & S_IXOTH ? 'x' : '-'));
      47    *string = '\0';
      48  }
      49  
      50  /* Report an error associated with the system call CALL and the
      51     optional name NAME.  */
      52  void
      53  call_arg_error (char const *call, char const *name)
      54  {
      55    int e = errno;
      56    /* TRANSLATORS: %s after `Cannot' is a function name, e.g. `Cannot open'.
      57       Directly translating this to another language will not work, first because
      58       %s itself is not translated.
      59       Translate it as `%s: Function %s failed'. */
      60    ERROR ((0, e, _("%s: Cannot %s"), quotearg_colon (name), call));
      61  }
      62  
      63  /* Report a fatal error associated with the system call CALL and
      64     the optional file name NAME.  */
      65  void
      66  call_arg_fatal (char const *call, char const *name)
      67  {
      68    int e = errno;
      69    /* TRANSLATORS: %s after `Cannot' is a function name, e.g. `Cannot open'.
      70       Directly translating this to another language will not work, first because
      71       %s itself is not translated.
      72       Translate it as `%s: Function %s failed'. */
      73    FATAL_ERROR ((0, e, _("%s: Cannot %s"), quotearg_colon (name),  call));
      74  }
      75  
      76  /* Report a warning associated with the system call CALL and
      77     the optional file name NAME.  */
      78  void
      79  call_arg_warn (char const *call, char const *name)
      80  {
      81    int e = errno;
      82    /* TRANSLATORS: %s after `Cannot' is a function name, e.g. `Cannot open'.
      83       Directly translating this to another language will not work, first because
      84       %s itself is not translated.
      85       Translate it as `%s: Function %s failed'. */
      86    WARN ((0, e, _("%s: Warning: Cannot %s"), quotearg_colon (name), call));
      87  }
      88  
      89  void
      90  chmod_error_details (char const *name, mode_t mode)
      91  {
      92    int e = errno;
      93    char buf[10];
      94    pax_decode_mode (mode, buf);
      95    ERROR ((0, e, _("%s: Cannot change mode to %s"),
      96  	  quotearg_colon (name), buf));
      97  }
      98  
      99  void
     100  chown_error_details (char const *name, uid_t uid, gid_t gid)
     101  {
     102    uintmax_t u = uid, g = gid;
     103    int e = errno;
     104    ERROR ((0, e, _("%s: Cannot change ownership to uid %ju, gid %ju"),
     105  	  quotearg_colon (name), u, g));
     106  }
     107  
     108  void
     109  close_error (char const *name)
     110  {
     111    call_arg_error ("close", name);
     112  }
     113  
     114  void
     115  close_warn (char const *name)
     116  {
     117    call_arg_warn ("close", name);
     118  }
     119  
     120  void
     121  exec_fatal (char const *name)
     122  {
     123    call_arg_fatal ("exec", name);
     124  }
     125  
     126  void
     127  link_error (char const *target, char const *source)
     128  {
     129    int e = errno;
     130    ERROR ((0, e, _("%s: Cannot hard link to %s"),
     131  	  quotearg_colon (source), quote_n (1, target)));
     132  }
     133  
     134  void
     135  mkdir_error (char const *name)
     136  {
     137    call_arg_error ("mkdir", name);
     138  }
     139  
     140  void
     141  mkfifo_error (char const *name)
     142  {
     143    call_arg_error ("mkfifo", name);
     144  }
     145  
     146  void
     147  mknod_error (char const *name)
     148  {
     149    call_arg_error ("mknod", name);
     150  }
     151  
     152  void
     153  open_error (char const *name)
     154  {
     155    call_arg_error ("open", name);
     156  }
     157  
     158  void
     159  open_fatal (char const *name)
     160  {
     161    call_arg_fatal ("open", name);
     162  }
     163  
     164  void
     165  open_warn (char const *name)
     166  {
     167    call_arg_warn ("open", name);
     168  }
     169  
     170  void
     171  read_error (char const *name)
     172  {
     173    call_arg_error ("read", name);
     174  }
     175  
     176  void
     177  read_error_details (char const *name, off_t offset, size_t size)
     178  {
     179    intmax_t off = offset;
     180    int e = errno;
     181    ERROR ((0, e,
     182  	  ngettext ("%s: Read error at byte %jd, while reading %zu byte",
     183  		    "%s: Read error at byte %jd, while reading %zu bytes",
     184  		    size),
     185  	  quotearg_colon (name), off, size));
     186  }
     187  
     188  void
     189  read_warn_details (char const *name, off_t offset, size_t size)
     190  {
     191    intmax_t off = offset;
     192    int e = errno;
     193    WARN ((0, e,
     194  	 ngettext ("%s: Warning: Read error at byte %jd, while reading %zu byte",
     195  		   "%s: Warning: Read error at byte %jd, while reading %zu bytes",
     196  		   size),
     197  	 quotearg_colon (name), off, size));
     198  }
     199  
     200  void
     201  read_fatal (char const *name)
     202  {
     203    call_arg_fatal ("read", name);
     204  }
     205  
     206  void
     207  read_fatal_details (char const *name, off_t offset, size_t size)
     208  {
     209    intmax_t off = offset;
     210    int e = errno;
     211    FATAL_ERROR ((0, e,
     212  		ngettext ("%s: Read error at byte %jd, while reading %zu byte",
     213  			  "%s: Read error at byte %jd, while reading %zu bytes",
     214  			  size),
     215  		quotearg_colon (name), off, size));
     216  }
     217  
     218  void
     219  readlink_error (char const *name)
     220  {
     221    call_arg_error ("readlink", name);
     222  }
     223  
     224  void
     225  readlink_warn (char const *name)
     226  {
     227    call_arg_warn ("readlink", name);
     228  }
     229  
     230  void
     231  rmdir_error (char const *name)
     232  {
     233    call_arg_error ("rmdir", name);
     234  }
     235  
     236  void
     237  savedir_error (char const *name)
     238  {
     239    call_arg_error ("savedir", name);
     240  }
     241  
     242  void
     243  savedir_warn (char const *name)
     244  {
     245    call_arg_warn ("savedir", name);
     246  }
     247  
     248  void
     249  seek_error (char const *name)
     250  {
     251    call_arg_error ("seek", name);
     252  }
     253  
     254  void
     255  seek_error_details (char const *name, off_t offset)
     256  {
     257    intmax_t off = offset;
     258    int e = errno;
     259    ERROR ((0, e, _("%s: Cannot seek to %jd"), quotearg_colon (name), off));
     260  }
     261  
     262  void
     263  seek_warn (char const *name)
     264  {
     265    call_arg_warn ("seek", name);
     266  }
     267  
     268  void
     269  seek_warn_details (char const *name, off_t offset)
     270  {
     271    intmax_t off = offset;
     272    int e = errno;
     273    WARN ((0, e, _("%s: Warning: Cannot seek to %jd"),
     274  	 quotearg_colon (name), off));
     275  }
     276  
     277  void
     278  symlink_error (char const *contents, char const *name)
     279  {
     280    int e = errno;
     281    ERROR ((0, e, _("%s: Cannot create symlink to %s"),
     282  	  quotearg_colon (name), quote_n (1, contents)));
     283  }
     284  
     285  void
     286  stat_fatal (char const *name)
     287  {
     288    call_arg_fatal ("stat", name);
     289  }
     290  
     291  void
     292  stat_error (char const *name)
     293  {
     294    call_arg_error ("stat", name);
     295  }
     296  
     297  void
     298  stat_warn (char const *name)
     299  {
     300    call_arg_warn ("stat", name);
     301  }
     302  
     303  void
     304  truncate_error (char const *name)
     305  {
     306    call_arg_error ("truncate", name);
     307  }
     308  
     309  void
     310  truncate_warn (char const *name)
     311  {
     312    call_arg_warn ("truncate", name);
     313  }
     314  
     315  void
     316  unlink_error (char const *name)
     317  {
     318    call_arg_error ("unlink", name);
     319  }
     320  
     321  void
     322  utime_error (char const *name)
     323  {
     324    call_arg_error ("utime", name);
     325  }
     326  
     327  void
     328  waitpid_error (char const *name)
     329  {
     330    call_arg_error ("waitpid", name);
     331  }
     332  
     333  void
     334  write_error (char const *name)
     335  {
     336    call_arg_error ("write", name);
     337  }
     338  
     339  void
     340  write_error_details (char const *name, size_t status, size_t size)
     341  {
     342    if (status == 0)
     343      write_error (name);
     344    else
     345      ERROR ((0, 0,
     346  	    ngettext ("%s: Wrote only %zu of %zu byte",
     347  		      "%s: Wrote only %zu of %zu bytes",
     348  		      size),
     349  	    name, status, size));
     350  }
     351  
     352  void
     353  chdir_fatal (char const *name)
     354  {
     355    call_arg_fatal ("chdir", name);
     356  }