more
[metze/wireshark/wip.git] / ui / failure_message.c
1 /* failure_message.c
2  * Routines to print various "standard" failure messages used in multiple
3  * places
4  *
5  * Wireshark - Network traffic analyzer
6  * By Gerald Combs <gerald@wireshark.org>
7  * Copyright 1998 Gerald Combs
8  *
9  * SPDX-License-Identifier: GPL-2.0-or-later
10  */
11
12 #include "config.h"
13
14 #include <string.h>
15 #include <errno.h>
16
17 #include <wiretap/wtap.h>
18 #include <wsutil/filesystem.h>
19 #include <wsutil/cmdarg_err.h>
20
21 #include "ui/failure_message.h"
22
23 static char *
24 input_file_description(const char *fname)
25 {
26     char *fstring;
27
28     if (strcmp(fname, "-") == 0) {
29         /* We're reading from the standard input */
30         fstring = g_strdup("standard input");
31     } else {
32         /* We're reading from a file */
33         fstring = g_strdup_printf("file \"%s\"", fname);
34     }
35     return fstring;
36 }
37
38 static char *
39 output_file_description(const char *fname)
40 {
41     char *fstring;
42
43     if (strcmp(fname, "-") == 0) {
44         /* We're writing to to the standard output */
45         fstring = g_strdup("standard output");
46     } else {
47         /* We're writing to a file */
48         fstring = g_strdup_printf("file \"%s\"", fname);
49     }
50     return fstring;
51 }
52
53 /*
54  * Error message for a failed attempt to open a capture file for reading.
55  * "progname" is the name of the program trying to open the file;
56  * "filename" is the name of the file being opened; "err" is assumed
57  * to be a UNIX-style errno or a WTAP_ERR_ value; "err_info" is assumed
58  * to be a string giving further information for some WTAP_ERR_ values.
59  */
60 void
61 cfile_open_failure_message(const char *progname, const char *filename,
62                            int err, gchar *err_info)
63 {
64     if (err < 0) {
65         /*
66          * Wiretap error.
67          * Get a string that describes what we're opening.
68          */
69         char *file_description = input_file_description(filename);
70
71         switch (err) {
72
73         case WTAP_ERR_NOT_REGULAR_FILE:
74             cmdarg_err("The %s is a \"special file\" or socket or other non-regular file.",
75                        file_description);
76             break;
77
78         case WTAP_ERR_RANDOM_OPEN_PIPE:
79             cmdarg_err("The %s is a pipe or FIFO; %s can't read pipe or FIFO files in two-pass mode.",
80                        file_description, progname);
81             break;
82
83         case WTAP_ERR_FILE_UNKNOWN_FORMAT:
84             cmdarg_err("The %s isn't a capture file in a format %s understands.",
85                        file_description, progname);
86             break;
87
88         case WTAP_ERR_UNSUPPORTED:
89             cmdarg_err("The %s contains record data that %s doesn't support.\n"
90                        "(%s)",
91                        file_description, progname,
92                        err_info != NULL ? err_info : "no information supplied");
93             g_free(err_info);
94             break;
95
96         case WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED:
97             cmdarg_err("The %s is a capture for a network type that %s doesn't support.",
98                        file_description, progname);
99             break;
100
101         case WTAP_ERR_BAD_FILE:
102             cmdarg_err("The %s appears to be damaged or corrupt.\n"
103                        "(%s)",
104                        file_description,
105                        err_info != NULL ? err_info : "no information supplied");
106             g_free(err_info);
107             break;
108
109         case WTAP_ERR_CANT_OPEN:
110             cmdarg_err("The %s could not be opened for some unknown reason.",
111                        file_description);
112             break;
113
114         case WTAP_ERR_SHORT_READ:
115             cmdarg_err("The %s appears to have been cut short in the middle of a packet or other data.",
116                        file_description);
117             break;
118
119         case WTAP_ERR_DECOMPRESS:
120             cmdarg_err("The %s cannot be decompressed; it may be damaged or corrupt."
121                        "(%s)",
122                        file_description,
123                        err_info != NULL ? err_info : "no information supplied");
124             g_free(err_info);
125             break;
126
127         case WTAP_ERR_DECOMPRESSION_NOT_SUPPORTED:
128             cmdarg_err("The %s cannot be decompressed; it is compressed in a way that we don't support."
129                        "(%s)",
130                        file_description,
131                        err_info != NULL ? err_info : "no information supplied");
132             g_free(err_info);
133             break;
134
135         default:
136             cmdarg_err("The %s could not be opened: %s.",
137                        file_description,
138                        wtap_strerror(err));
139             break;
140         }
141         g_free(file_description);
142     } else
143         cmdarg_err(file_open_error_message(err, FALSE), filename);
144 }
145
146 /*
147  * Error message for a failed attempt to open a capture file for writing.
148  * "progname" is the name of the program trying to open the file;
149  * "filename" is the name of the file being opened; "err" is assumed
150  * to be a UNIX-style errno or a WTAP_ERR_ value; "file_type_subtype" is
151  * a WTAP_FILE_TYPE_SUBTYPE_ value for the type and subtype of file being
152  * opened.
153  */
154 void
155 cfile_dump_open_failure_message(const char *progname, const char *filename,
156                                 int err, int file_type_subtype)
157 {
158     if (err < 0) {
159         /*
160          * Wiretap error.
161          * Get a string that describes what we're opening.
162          */
163         char *file_description = input_file_description(filename);
164
165         switch (err) {
166
167         case WTAP_ERR_NOT_REGULAR_FILE:
168             cmdarg_err("The %s is a \"special file\" or socket or other non-regular file.",
169                        file_description);
170             break;
171
172         case WTAP_ERR_CANT_WRITE_TO_PIPE:
173             cmdarg_err("The %s is a pipe, and \"%s\" capture files can't be written to a pipe.",
174                        file_description,
175                        wtap_file_type_subtype_short_string(file_type_subtype));
176             break;
177
178         case WTAP_ERR_UNWRITABLE_FILE_TYPE:
179             cmdarg_err("%s doesn't support writing capture files in that format.",
180                        progname);
181             break;
182
183         case WTAP_ERR_UNWRITABLE_ENCAP:
184             cmdarg_err("The capture file being read can't be written as a \"%s\" file.",
185                        wtap_file_type_subtype_short_string(file_type_subtype));
186             break;
187
188         case WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED:
189             cmdarg_err("The capture file being read can't be written as a \"%s\" file.",
190                        wtap_file_type_subtype_short_string(file_type_subtype));
191             break;
192
193         case WTAP_ERR_CANT_OPEN:
194             cmdarg_err("The %s could not be created for some unknown reason.",
195                        file_description);
196             break;
197
198         case WTAP_ERR_SHORT_WRITE:
199             cmdarg_err("A full header couldn't be written to the %s.",
200                        file_description);
201             break;
202
203         case WTAP_ERR_COMPRESSION_NOT_SUPPORTED:
204             cmdarg_err("This file type cannot be written as a compressed file.");
205             break;
206
207         default:
208             cmdarg_err("The %s could not be created: %s.",
209                        file_description,
210                        wtap_strerror(err));
211             break;
212         }
213         g_free(file_description);
214     } else
215         cmdarg_err(file_open_error_message(err, TRUE), filename);
216 }
217
218 /*
219  * Error message for a failed attempt to read from a capture file.
220  * "progname" is the name of the program trying to open the file;
221  * "filename" is the name of the file being opened; "err" is assumed
222  * to be a UNIX-style errno or a WTAP_ERR_ value; "err_info" is assumed
223  * to be a string giving further information for some WTAP_ERR_ values.
224  */
225 void
226 cfile_read_failure_message(const char *progname, const char *filename,
227                            int err, gchar *err_info)
228 {
229     char *file_string;
230
231     /* Get a string that describes what we're reading from */
232     file_string = input_file_description(filename);
233
234     switch (err) {
235
236     case WTAP_ERR_UNSUPPORTED:
237         cmdarg_err("The %s contains record data that %s doesn't support.\n"
238                    "(%s)",
239                    file_string, progname,
240                    err_info != NULL ? err_info : "no information supplied");
241         g_free(err_info);
242         break;
243
244     case WTAP_ERR_SHORT_READ:
245         cmdarg_err("The %s appears to have been cut short in the middle of a packet.",
246                    file_string);
247         break;
248
249     case WTAP_ERR_BAD_FILE:
250         cmdarg_err("The %s appears to be damaged or corrupt.\n"
251                    "(%s)",
252                    file_string,
253                    err_info != NULL ? err_info : "no information supplied");
254         g_free(err_info);
255         break;
256
257     case WTAP_ERR_DECOMPRESS:
258         cmdarg_err("The %s cannot be decompressed; it may be damaged or corrupt.\n"
259                    "(%s)",
260                    file_string,
261                    err_info != NULL ? err_info : "no information supplied");
262         g_free(err_info);
263         break;
264
265     case WTAP_ERR_DECOMPRESSION_NOT_SUPPORTED:
266         cmdarg_err("The %s cannot be decompressed; it is compressed in a way that we don't support.\n"
267                    "(%s)",
268                    file_string,
269                    err_info != NULL ? err_info : "no information supplied");
270         g_free(err_info);
271         break;
272
273     default:
274         cmdarg_err("An error occurred while reading the %s: %s.",
275                    file_string, wtap_strerror(err));
276         break;
277     }
278     g_free(file_string);
279 }
280
281 /*
282  * Error message for a failed attempt to write to a capture file.
283  * "progname" is the name of the program trying to open the file;
284  * "in_filename" is the name of the file from which the record
285  * being written came; "out_filename" is the name of the file to
286  * which we're writing; "err" is assumed "err" is assumed to be a
287  * UNIX-style errno or a WTAP_ERR_ value; "err_info" is assumed to be
288  * a string giving further information for some WTAP_ERR_ values;
289  * "framenum" is the frame number of the record on which the error
290  * occurred; "file_type_subtype" is a WTAP_FILE_TYPE_SUBTYPE_ value
291  * for the type and subtype of file being written.
292  */
293 void
294 cfile_write_failure_message(const char *progname, const char *in_filename,
295                             const char *out_filename, int err, gchar *err_info,
296                             guint32 framenum, int file_type_subtype)
297 {
298     char *in_file_string;
299     char *in_frame_string;
300     char *out_file_string;
301
302     /* Get a string that describes what we're reading from */
303     if (in_filename == NULL) {
304         in_frame_string = g_strdup("");
305     } else {
306         in_file_string = input_file_description(in_filename);
307         in_frame_string = g_strdup_printf(" %u of %s", framenum,
308                                           in_file_string);
309         g_free(in_file_string);
310     }
311
312     /* Get a string that describes what we're writing to */
313     out_file_string = output_file_description(out_filename);
314
315     switch (err) {
316
317     case WTAP_ERR_UNWRITABLE_ENCAP:
318         /*
319          * This is a problem with the particular frame we're writing
320          * and the file type and subtype we're writing; note that,
321          * and report the frame number and file type/subtype.
322          */
323         cmdarg_err("Frame%s has a network type that can't be saved in a \"%s\" file.",
324                    in_frame_string,
325                    wtap_file_type_subtype_short_string(file_type_subtype));
326         break;
327
328     case WTAP_ERR_PACKET_TOO_LARGE:
329         /*
330          * This is a problem with the particular frame we're writing
331          * and the file type and subtype we're writing; note that,
332          * and report the frame number and file type/subtype.
333          */
334         cmdarg_err("Frame%s is larger than %s supports in a \"%s\" file.",
335                    in_frame_string, progname,
336                    wtap_file_type_subtype_short_string(file_type_subtype));
337         break;
338
339     case WTAP_ERR_UNWRITABLE_REC_TYPE:
340         /*
341          * This is a problem with the particular record we're writing
342          * and the file type and subtype we're writing; note that,
343          * and report the record number and file type/subtype.
344          */
345         cmdarg_err("Record%s has a record type that can't be saved in a \"%s\" file.",
346                    in_frame_string,
347                    wtap_file_type_subtype_short_string(file_type_subtype));
348         break;
349
350     case WTAP_ERR_UNWRITABLE_REC_DATA:
351         /*
352          * This is a problem with the particular record we're writing
353          * and the file type and subtype we're writing; note that,
354          * and report the record number and file type/subtype.
355          */
356         cmdarg_err("Record%s has data that can't be saved in a \"%s\" file.\n"
357                    "(%s)",
358                    in_frame_string,
359                    wtap_file_type_subtype_short_string(file_type_subtype),
360                    err_info != NULL ? err_info : "no information supplied");
361         g_free(err_info);
362         break;
363
364     case ENOSPC:
365         cmdarg_err("Not all the packets could be written to the %s because there is "
366                    "no space left on the file system.",
367                    out_file_string);
368         break;
369
370 #ifdef EDQUOT
371     case EDQUOT:
372         cmdarg_err("Not all the packets could be written to the %s because you are "
373                    "too close to, or over your disk quota.",
374                    out_file_string);
375   break;
376 #endif
377
378     case WTAP_ERR_SHORT_WRITE:
379         cmdarg_err("A full write couldn't be done to the %s.",
380                    out_file_string);
381         break;
382
383     default:
384         cmdarg_err("An error occurred while writing to the %s: %s.",
385                    out_file_string, wtap_strerror(err));
386         break;
387     }
388     g_free(in_frame_string);
389     g_free(out_file_string);
390 }
391
392 /*
393  * Error message for a failed attempt to close a capture file.
394  * "filename" is the name of the file being closed; "err" is assumed
395  * to be a UNIX-style errno or a WTAP_ERR_ value.
396  *
397  * When closing a capture file:
398  *
399  *    some information in the file that can't be determined until
400  *    all packets have been written might be written to the file
401  *    (such as a table of the file offsets of all packets);
402  *
403  *    data buffered in the low-level file writing code might be
404  *    flushed to the file;
405  *
406  *    for remote file systems, data written to the file but not
407  *    yet sent to the server might be sent to the server or, if
408  *    that data was sent asynchronously, "out of space", "disk
409  *    quota exceeded", or "I/O error" indications might have
410  *    been received but not yet delivered, and the close operation
411  *    could deliver them;
412  *
413  * so we have to check for write errors here.
414  */
415 void
416 cfile_close_failure_message(const char *filename, int err)
417 {
418     char *file_string;
419
420     /* Get a string that describes what we're writing to */
421     file_string = output_file_description(filename);
422
423     switch (err) {
424
425     case ENOSPC:
426         cmdarg_err("Not all the packets could be written to the %s because there is "
427                    "no space left on the file system.",
428                    file_string);
429     break;
430
431 #ifdef EDQUOT
432     case EDQUOT:
433         cmdarg_err("Not all the packets could be written to the %s because you are "
434                    "too close to, or over your disk quota.",
435                    file_string);
436   break;
437 #endif
438
439     case WTAP_ERR_CANT_CLOSE:
440         cmdarg_err("The %s couldn't be closed for some unknown reason.",
441                    file_string);
442         break;
443
444     case WTAP_ERR_SHORT_WRITE:
445         cmdarg_err("A full write couldn't be done to the %s.",
446                    file_string);
447         break;
448
449     default:
450         cmdarg_err("An error occurred while closing the file %s: %s.",
451                    file_string, wtap_strerror(err));
452         break;
453     }
454     g_free(file_string);
455 }