re-mark 1.29b-2 as not yet uploaded (merge madness!)
[debian/tar] / lib / paxerror.c
1 /* Miscellaneous error functions
2
3    Copyright (C) 2005, 2007 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   int e = errno;
103   ERROR ((0, e, _("%s: Cannot change ownership to uid %lu, gid %lu"),
104           quotearg_colon (name), (unsigned long) uid, (unsigned long) gid));
105 }
106
107 void
108 close_error (char const *name)
109 {
110   call_arg_error ("close", name);
111 }
112
113 void
114 close_warn (char const *name)
115 {
116   call_arg_warn ("close", name);
117 }
118
119 void
120 exec_fatal (char const *name)
121 {
122   call_arg_fatal ("exec", name);
123 }
124
125 void
126 link_error (char const *target, char const *source)
127 {
128   int e = errno;
129   ERROR ((0, e, _("%s: Cannot hard link to %s"),
130           quotearg_colon (source), quote_n (1, target)));
131 }
132
133 void
134 mkdir_error (char const *name)
135 {
136   call_arg_error ("mkdir", name);
137 }
138
139 void
140 mkfifo_error (char const *name)
141 {
142   call_arg_error ("mkfifo", name);
143 }
144
145 void
146 mknod_error (char const *name)
147 {
148   call_arg_error ("mknod", name);
149 }
150
151 void
152 open_error (char const *name)
153 {
154   call_arg_error ("open", name);
155 }
156
157 void
158 open_fatal (char const *name)
159 {
160   call_arg_fatal ("open", name);
161 }
162
163 void
164 open_warn (char const *name)
165 {
166   call_arg_warn ("open", name);
167 }
168
169 void
170 read_error (char const *name)
171 {
172   call_arg_error ("read", name);
173 }
174
175 void
176 read_error_details (char const *name, off_t offset, size_t size)
177 {
178   char buf[UINTMAX_STRSIZE_BOUND];
179   int e = errno;
180   ERROR ((0, e,
181           ngettext ("%s: Read error at byte %s, while reading %lu byte",
182                     "%s: Read error at byte %s, while reading %lu bytes",
183                     size),
184           quotearg_colon (name), STRINGIFY_BIGINT (offset, buf),
185           (unsigned long) size));
186 }
187
188 void
189 read_warn_details (char const *name, off_t offset, size_t size)
190 {
191   char buf[UINTMAX_STRSIZE_BOUND];
192   int e = errno;
193   WARN ((0, e,
194          ngettext ("%s: Warning: Read error at byte %s, while reading %lu byte",
195                    "%s: Warning: Read error at byte %s, while reading %lu bytes",
196                    size),
197          quotearg_colon (name), STRINGIFY_BIGINT (offset, buf),
198          (unsigned long) size));
199 }
200
201 void
202 read_fatal (char const *name)
203 {
204   call_arg_fatal ("read", name);
205 }
206
207 void
208 read_fatal_details (char const *name, off_t offset, size_t size)
209 {
210   char buf[UINTMAX_STRSIZE_BOUND];
211   int e = errno;
212   FATAL_ERROR ((0, e,
213                 ngettext ("%s: Read error at byte %s, while reading %lu byte",
214                           "%s: Read error at byte %s, while reading %lu bytes",
215                           size),
216                 quotearg_colon (name), STRINGIFY_BIGINT (offset, buf),
217                 (unsigned long) size));
218 }
219
220 void
221 readlink_error (char const *name)
222 {
223   call_arg_error ("readlink", name);
224 }
225
226 void
227 readlink_warn (char const *name)
228 {
229   call_arg_warn ("readlink", name);
230 }
231
232 void
233 rmdir_error (char const *name)
234 {
235   call_arg_error ("rmdir", name);
236 }
237
238 void
239 savedir_error (char const *name)
240 {
241   call_arg_error ("savedir", name);
242 }
243
244 void
245 savedir_warn (char const *name)
246 {
247   call_arg_warn ("savedir", name);
248 }
249
250 void
251 seek_error (char const *name)
252 {
253   call_arg_error ("seek", name);
254 }
255
256 void
257 seek_error_details (char const *name, off_t offset)
258 {
259   char buf[UINTMAX_STRSIZE_BOUND];
260   int e = errno;
261   ERROR ((0, e, _("%s: Cannot seek to %s"),
262           quotearg_colon (name),
263           STRINGIFY_BIGINT (offset, buf)));
264 }
265
266 void
267 seek_warn (char const *name)
268 {
269   call_arg_warn ("seek", name);
270 }
271
272 void
273 seek_warn_details (char const *name, off_t offset)
274 {
275   char buf[UINTMAX_STRSIZE_BOUND];
276   int e = errno;
277   WARN ((0, e, _("%s: Warning: Cannot seek to %s"),
278          quotearg_colon (name),
279          STRINGIFY_BIGINT (offset, buf)));
280 }
281
282 void
283 symlink_error (char const *contents, char const *name)
284 {
285   int e = errno;
286   ERROR ((0, e, _("%s: Cannot create symlink to %s"),
287           quotearg_colon (name), quote_n (1, contents)));
288 }
289
290 void
291 stat_fatal (char const *name)
292 {
293   call_arg_fatal ("stat", name);
294 }
295
296 void
297 stat_error (char const *name)
298 {
299   call_arg_error ("stat", name);
300 }
301
302 void
303 stat_warn (char const *name)
304 {
305   call_arg_warn ("stat", name);
306 }
307
308 void
309 truncate_error (char const *name)
310 {
311   call_arg_error ("truncate", name);
312 }
313
314 void
315 truncate_warn (char const *name)
316 {
317   call_arg_warn ("truncate", name);
318 }
319
320 void
321 unlink_error (char const *name)
322 {
323   call_arg_error ("unlink", name);
324 }
325
326 void
327 utime_error (char const *name)
328 {
329   call_arg_error ("utime", name);
330 }
331
332 void
333 waitpid_error (char const *name)
334 {
335   call_arg_error ("waitpid", name);
336 }
337
338 void
339 write_error (char const *name)
340 {
341   call_arg_error ("write", name);
342 }
343
344 void
345 write_error_details (char const *name, size_t status, size_t size)
346 {
347   if (status == 0)
348     write_error (name);
349   else
350     ERROR ((0, 0,
351             ngettext ("%s: Wrote only %lu of %lu byte",
352                       "%s: Wrote only %lu of %lu bytes",
353                       size),
354             name, (unsigned long int) status, (unsigned long int) size));
355 }
356
357 void
358 chdir_fatal (char const *name)
359 {
360   call_arg_fatal ("chdir", name);
361 }