From: Martin Matuska Date: Sat, 25 Jan 2020 13:26:59 +0000 (+0100) Subject: Unify unsupported entry file type error message in writer X-Git-Tag: v3.4.2~22 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=bcaa4a4d270544dfd5e0f661d7524ab94beda174;p=thirdparty%2Flibarchive.git Unify unsupported entry file type error message in writer Closes #1320 --- diff --git a/Makefile.am b/Makefile.am index ea31c5270..06c264420 100644 --- a/Makefile.am +++ b/Makefile.am @@ -228,6 +228,7 @@ libarchive_la_SOURCES= \ libarchive/archive_write_set_format_iso9660.c \ libarchive/archive_write_set_format_mtree.c \ libarchive/archive_write_set_format_pax.c \ + libarchive/archive_write_set_format_private.h \ libarchive/archive_write_set_format_raw.c \ libarchive/archive_write_set_format_shar.c \ libarchive/archive_write_set_format_ustar.c \ diff --git a/libarchive/CMakeLists.txt b/libarchive/CMakeLists.txt index ec775bb49..9389bbc9a 100644 --- a/libarchive/CMakeLists.txt +++ b/libarchive/CMakeLists.txt @@ -150,6 +150,7 @@ SET(libarchive_SOURCES archive_write_set_format_iso9660.c archive_write_set_format_mtree.c archive_write_set_format_pax.c + archive_write_set_format_private.h archive_write_set_format_raw.c archive_write_set_format_shar.c archive_write_set_format_ustar.c diff --git a/libarchive/archive_write_set_format.c b/libarchive/archive_write_set_format.c index 0f706231a..12de08077 100644 --- a/libarchive/archive_write_set_format.c +++ b/libarchive/archive_write_set_format.c @@ -36,6 +36,7 @@ __FBSDID("$FreeBSD: head/lib/libarchive/archive_write_set_format.c 201168 2009-1 #include "archive.h" #include "archive_private.h" +#include "archive_write_set_format_private.h" /* A table that maps format codes to functions. */ static const @@ -76,3 +77,47 @@ archive_write_set_format(struct archive *a, int code) archive_set_error(a, EINVAL, "No such format"); return (ARCHIVE_FATAL); } + +void +__archive_write_entry_filetype_unsupported(struct archive *a, + struct archive_entry *entry, const char *format) +{ + char *name = NULL; + + switch (archive_entry_filetype(entry)) { + /* + * All formats should be able to archive regular files (AE_IFREG) + */ + case AE_IFDIR: + name = "directories"; + break; + case AE_IFLNK: + name = "symbolic links"; + break; + case AE_IFCHR: + name = "character devices"; + break; + case AE_IFBLK: + name = "block devices"; + break; + case AE_IFIFO: + name = "named pipes"; + break; + case AE_IFSOCK: + name = "sockets"; + break; + default: + break; + } + + if (name != NULL) { + archive_set_error(a, ARCHIVE_ERRNO_FILE_FORMAT, + "%s: %s format cannot archive %s", + archive_entry_pathname(entry), format, name); + } else { + archive_set_error(a, ARCHIVE_ERRNO_FILE_FORMAT, + "%s: %s format cannot archive files with mode 0%lo", + archive_entry_pathname(entry), format, + (unsigned long)archive_entry_mode(entry)); + } +} diff --git a/libarchive/archive_write_set_format_7zip.c b/libarchive/archive_write_set_format_7zip.c index e577704b1..fb7697f65 100644 --- a/libarchive/archive_write_set_format_7zip.c +++ b/libarchive/archive_write_set_format_7zip.c @@ -52,6 +52,7 @@ __FBSDID("$FreeBSD$"); #include "archive_rb.h" #include "archive_string.h" #include "archive_write_private.h" +#include "archive_write_set_format_private.h" /* * Codec ID diff --git a/libarchive/archive_write_set_format_ar.c b/libarchive/archive_write_set_format_ar.c index 253cac82e..fc0de1e9f 100644 --- a/libarchive/archive_write_set_format_ar.c +++ b/libarchive/archive_write_set_format_ar.c @@ -42,6 +42,7 @@ __FBSDID("$FreeBSD: head/lib/libarchive/archive_write_set_format_ar.c 201108 200 #include "archive_entry.h" #include "archive_private.h" #include "archive_write_private.h" +#include "archive_write_set_format_private.h" struct ar_w { uint64_t entry_bytes_remaining; diff --git a/libarchive/archive_write_set_format_cpio.c b/libarchive/archive_write_set_format_cpio.c index 16cefad7b..729f9c775 100644 --- a/libarchive/archive_write_set_format_cpio.c +++ b/libarchive/archive_write_set_format_cpio.c @@ -43,6 +43,7 @@ __FBSDID("$FreeBSD: head/lib/libarchive/archive_write_set_format_cpio.c 201170 2 #include "archive_entry_locale.h" #include "archive_private.h" #include "archive_write_private.h" +#include "archive_write_set_format_private.h" static ssize_t archive_write_cpio_data(struct archive_write *, const void *buff, size_t s); diff --git a/libarchive/archive_write_set_format_cpio_newc.c b/libarchive/archive_write_set_format_cpio_newc.c index 2d923cc33..172fda62f 100644 --- a/libarchive/archive_write_set_format_cpio_newc.c +++ b/libarchive/archive_write_set_format_cpio_newc.c @@ -44,6 +44,7 @@ __FBSDID("$FreeBSD: head/lib/libarchive/archive_write_set_format_cpio_newc.c 201 #include "archive_entry_locale.h" #include "archive_private.h" #include "archive_write_private.h" +#include "archive_write_set_format_private.h" static ssize_t archive_write_newc_data(struct archive_write *, const void *buff, size_t s); diff --git a/libarchive/archive_write_set_format_gnutar.c b/libarchive/archive_write_set_format_gnutar.c index e7757c22b..ec29c5c41 100644 --- a/libarchive/archive_write_set_format_gnutar.c +++ b/libarchive/archive_write_set_format_gnutar.c @@ -46,6 +46,7 @@ __FBSDID("$FreeBSD: head/lib/libarchive/archive_write_set_format_gnu_tar.c 19157 #include "archive_entry_locale.h" #include "archive_private.h" #include "archive_write_private.h" +#include "archive_write_set_format_private.h" struct gnutar { uint64_t entry_bytes_remaining; @@ -534,17 +535,9 @@ archive_write_gnutar_header(struct archive_write *a, case AE_IFBLK: tartype = '4' ; break; case AE_IFDIR: tartype = '5' ; break; case AE_IFIFO: tartype = '6' ; break; - case AE_IFSOCK: - archive_set_error(&a->archive, - ARCHIVE_ERRNO_FILE_FORMAT, - "tar format cannot archive socket"); - ret = ARCHIVE_FAILED; - goto exit_write_header; - default: - archive_set_error(&a->archive, - ARCHIVE_ERRNO_FILE_FORMAT, - "tar format cannot archive this (mode=0%lo)", - (unsigned long)archive_entry_mode(entry)); + default: /* AE_IFSOCK and unknown */ + __archive_write_entry_filetype_unsupported( + &a->archive, entry, "gnutar"); ret = ARCHIVE_FAILED; goto exit_write_header; } diff --git a/libarchive/archive_write_set_format_pax.c b/libarchive/archive_write_set_format_pax.c index 7c5e63bb3..584f46ae0 100644 --- a/libarchive/archive_write_set_format_pax.c +++ b/libarchive/archive_write_set_format_pax.c @@ -43,6 +43,7 @@ __FBSDID("$FreeBSD: head/lib/libarchive/archive_write_set_format_pax.c 201162 20 #include "archive_entry_locale.h" #include "archive_private.h" #include "archive_write_private.h" +#include "archive_write_set_format_private.h" struct sparse_block { struct sparse_block *next; @@ -713,17 +714,9 @@ archive_write_pax_header(struct archive_write *a, } break; } - case AE_IFSOCK: - archive_set_error(&a->archive, - ARCHIVE_ERRNO_FILE_FORMAT, - "tar format cannot archive socket"); - return (ARCHIVE_FAILED); - default: - archive_set_error(&a->archive, - ARCHIVE_ERRNO_FILE_FORMAT, - "tar format cannot archive this (type=0%lo)", - (unsigned long) - archive_entry_filetype(entry_original)); + default: /* AE_IFSOCK and unknown */ + __archive_write_entry_filetype_unsupported( + &a->archive, entry_original, "pax"); return (ARCHIVE_FAILED); } } diff --git a/libarchive/archive_write_set_format_private.h b/libarchive/archive_write_set_format_private.h new file mode 100644 index 000000000..e20022755 --- /dev/null +++ b/libarchive/archive_write_set_format_private.h @@ -0,0 +1,42 @@ +/*- + * Copyright (c) 2020 Martin Matuska + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#ifndef ARCHIVE_WRITE_SET_FORMAT_PRIVATE_H_INCLUDED +#define ARCHIVE_WRITE_SET_FORMAT_PRIVATE_H_INCLUDED + +#ifndef __LIBARCHIVE_BUILD +#ifndef __LIBARCHIVE_TEST +#error This header is only to be used internally to libarchive. +#endif +#endif + +#include "archive.h" +#include "archive_entry.h" + +void __archive_write_entry_filetype_unsupported(struct archive *a, + struct archive_entry *entry, const char *format); +#endif diff --git a/libarchive/archive_write_set_format_shar.c b/libarchive/archive_write_set_format_shar.c index 600c88257..9e4931c95 100644 --- a/libarchive/archive_write_set_format_shar.c +++ b/libarchive/archive_write_set_format_shar.c @@ -42,6 +42,7 @@ __FBSDID("$FreeBSD: head/lib/libarchive/archive_write_set_format_shar.c 189438 2 #include "archive_entry.h" #include "archive_private.h" #include "archive_write_private.h" +#include "archive_write_set_format_private.h" struct shar { int dump; @@ -194,8 +195,8 @@ archive_write_shar_header(struct archive_write *a, struct archive_entry *entry) archive_entry_set_size(entry, 0); if (archive_entry_hardlink(entry) == NULL && archive_entry_symlink(entry) == NULL) { - archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, - "shar format cannot archive this"); + __archive_write_entry_filetype_unsupported( + &a->archive, entry, "shar"); return (ARCHIVE_WARN); } } diff --git a/libarchive/archive_write_set_format_ustar.c b/libarchive/archive_write_set_format_ustar.c index ad4ccb77e..e1fe974a1 100644 --- a/libarchive/archive_write_set_format_ustar.c +++ b/libarchive/archive_write_set_format_ustar.c @@ -44,6 +44,7 @@ __FBSDID("$FreeBSD: head/lib/libarchive/archive_write_set_format_ustar.c 191579 #include "archive_entry_locale.h" #include "archive_private.h" #include "archive_write_private.h" +#include "archive_write_set_format_private.h" struct ustar { uint64_t entry_bytes_remaining; @@ -609,16 +610,9 @@ __archive_write_format_header_ustar(struct archive_write *a, char h[512], case AE_IFBLK: h[USTAR_typeflag_offset] = '4' ; break; case AE_IFDIR: h[USTAR_typeflag_offset] = '5' ; break; case AE_IFIFO: h[USTAR_typeflag_offset] = '6' ; break; - case AE_IFSOCK: - archive_set_error(&a->archive, - ARCHIVE_ERRNO_FILE_FORMAT, - "tar format cannot archive socket"); - return (ARCHIVE_FAILED); - default: - archive_set_error(&a->archive, - ARCHIVE_ERRNO_FILE_FORMAT, - "tar format cannot archive this (mode=0%lo)", - (unsigned long)archive_entry_mode(entry)); + default: /* AE_IFSOCK and unknown */ + __archive_write_entry_filetype_unsupported( + &a->archive, entry, "ustar"); ret = ARCHIVE_FAILED; } } diff --git a/libarchive/archive_write_set_format_v7tar.c b/libarchive/archive_write_set_format_v7tar.c index 1fdaafd2a..599407144 100644 --- a/libarchive/archive_write_set_format_v7tar.c +++ b/libarchive/archive_write_set_format_v7tar.c @@ -44,6 +44,7 @@ __FBSDID("$FreeBSD$"); #include "archive_entry_locale.h" #include "archive_private.h" #include "archive_write_private.h" +#include "archive_write_set_format_private.h" struct v7tar { uint64_t entry_bytes_remaining; @@ -491,31 +492,11 @@ format_header_v7tar(struct archive_write *a, char h[512], case AE_IFLNK: h[V7TAR_typeflag_offset] = '2'; break; - case AE_IFCHR: - archive_set_error(&a->archive, - ARCHIVE_ERRNO_FILE_FORMAT, - "tar format cannot archive character device"); - return (ARCHIVE_FAILED); - case AE_IFBLK: - archive_set_error(&a->archive, - ARCHIVE_ERRNO_FILE_FORMAT, - "tar format cannot archive block device"); - return (ARCHIVE_FAILED); - case AE_IFIFO: - archive_set_error(&a->archive, - ARCHIVE_ERRNO_FILE_FORMAT, - "tar format cannot archive fifo"); - return (ARCHIVE_FAILED); - case AE_IFSOCK: - archive_set_error(&a->archive, - ARCHIVE_ERRNO_FILE_FORMAT, - "tar format cannot archive socket"); - return (ARCHIVE_FAILED); default: - archive_set_error(&a->archive, - ARCHIVE_ERRNO_FILE_FORMAT, - "tar format cannot archive this (mode=0%lo)", - (unsigned long)archive_entry_mode(entry)); + /* AE_IFBLK, AE_IFCHR, AE_IFIFO, AE_IFSOCK + * and unknown */ + __archive_write_entry_filetype_unsupported( + &a->archive, entry, "v7tar"); ret = ARCHIVE_FAILED; } } diff --git a/libarchive/archive_write_set_format_warc.c b/libarchive/archive_write_set_format_warc.c index 00f15ff9d..46b057341 100644 --- a/libarchive/archive_write_set_format_warc.c +++ b/libarchive/archive_write_set_format_warc.c @@ -48,6 +48,7 @@ __FBSDID("$FreeBSD$"); #include "archive_private.h" #include "archive_random_private.h" #include "archive_write_private.h" +#include "archive_write_set_format_private.h" struct warc_s { unsigned int omit_warcinfo:1; @@ -259,10 +260,8 @@ _warc_header(struct archive_write *a, struct archive_entry *entry) return (ARCHIVE_OK); } /* just resort to erroring as per Tim's advice */ - archive_set_error( - &a->archive, - ARCHIVE_ERRNO_FILE_FORMAT, - "WARC can only process regular files"); + __archive_write_entry_filetype_unsupported( + &a->archive, entry, "WARC"); return (ARCHIVE_FAILED); } diff --git a/libarchive/archive_write_set_format_zip.c b/libarchive/archive_write_set_format_zip.c index 917350d56..6d485295d 100644 --- a/libarchive/archive_write_set_format_zip.c +++ b/libarchive/archive_write_set_format_zip.c @@ -57,6 +57,7 @@ __FBSDID("$FreeBSD: head/lib/libarchive/archive_write_set_format_zip.c 201168 20 #include "archive_private.h" #include "archive_random_private.h" #include "archive_write_private.h" +#include "archive_write_set_format_private.h" #ifndef HAVE_ZLIB_H #include "archive_crc32.h" @@ -526,8 +527,8 @@ archive_write_zip_header(struct archive_write *a, struct archive_entry *entry) /* Ignore types of entries that we don't support. */ type = archive_entry_filetype(entry); if (type != AE_IFREG && type != AE_IFDIR && type != AE_IFLNK) { - archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, - "Filetype not supported"); + __archive_write_entry_filetype_unsupported( + &a->archive, entry, "zip"); return ARCHIVE_FAILED; };