Permalink
Browse files

Fix issue 237.

Properly set a clear error message when archive_{write,read}_set_options failed.
  • Loading branch information...
ggcueroad committed Feb 13, 2012
1 parent 1ecb1bb commit f30dc311bafff055bbcfcb272834566e15fce4dd
@@ -38,6 +38,7 @@ _archive_set_option(struct archive *a,
int magic, const char *fn, option_handler use_option)
{
const char *mp, *op, *vp;
+ int r;
archive_check_magic(a, magic, ARCHIVE_STATE_NEW, fn);
@@ -47,10 +48,24 @@ _archive_set_option(struct archive *a,
if (op == NULL && vp == NULL)
return (ARCHIVE_OK);
- if (op == NULL)
+ if (op == NULL) {
+ archive_set_error(a, ARCHIVE_ERRNO_MISC, "Empty option");
return (ARCHIVE_FAILED);
+ }
- return use_option(a, mp, op, vp);
+ r = use_option(a, mp, op, vp);
+ if (r == ARCHIVE_WARN - 1) {
+ archive_set_error(a, ARCHIVE_ERRNO_MISC,
+ "Unknown module name: `%s'", mp);
+ return (ARCHIVE_FAILED);
+ }
+ if (r == ARCHIVE_WARN) {
+ archive_set_error(a, ARCHIVE_ERRNO_MISC,
+ "Undefined option: `%s%s%s%s%s%s'",
+ vp?"":"!", mp?mp:"", mp?":":"", op, vp?"=":"", vp?vp:"");
+ return (ARCHIVE_FAILED);
+ }
+ return (r);
}
int
@@ -102,6 +117,25 @@ _archive_set_options(struct archive *a, const char *options,
free(data);
return (ARCHIVE_FATAL);
}
+ if (r == ARCHIVE_FAILED && mod != NULL) {
+ free(data);
+ return (ARCHIVE_FAILED);
+ }
+ if (r == ARCHIVE_WARN - 1) {
+ /* The module name is wrong. */
+ archive_set_error(a, ARCHIVE_ERRNO_MISC,
+ "Unknown module name: `%s'", mod);
+ free(data);
+ return (ARCHIVE_FAILED);
+ }
+ if (r == ARCHIVE_WARN) {
+ /* The option name is wrong. No-one used this. */
+ archive_set_error(a, ARCHIVE_ERRNO_MISC,
+ "Undefined option: `%s%s%s'",
+ mod?mod:"", mod?":":"", opt);
+ free(data);
+ return (ARCHIVE_FAILED);
+ }
if (r == ARCHIVE_OK)
anyok = 1;
else
@@ -78,7 +78,7 @@ archive_set_format_option(struct archive *_a, const char *m, const char *o,
struct archive_read *a = (struct archive_read *)_a;
struct archive_format_descriptor *format;
size_t i;
- int r, rv = ARCHIVE_FAILED;
+ int r, rv = ARCHIVE_WARN;
for (i = 0; i < sizeof(a->formats)/sizeof(a->formats[0]); i++) {
format = &a->formats[i];
@@ -102,6 +102,10 @@ archive_set_format_option(struct archive *_a, const char *m, const char *o,
if (r == ARCHIVE_OK)
rv = ARCHIVE_OK;
}
+ /* If the format name didn't match, return a special code for
+ * _archive_set_option[s]. */
+ if (rv == ARCHIVE_WARN && m != NULL)
+ rv = ARCHIVE_WARN - 1;
return (rv);
}
@@ -112,7 +116,7 @@ archive_set_filter_option(struct archive *_a, const char *m, const char *o,
struct archive_read *a = (struct archive_read *)_a;
struct archive_read_filter *filter;
struct archive_read_filter_bidder *bidder;
- int r, rv = ARCHIVE_FAILED;
+ int r, rv = ARCHIVE_WARN;
for (filter = a->filter; filter != NULL; filter = filter->upstream) {
bidder = filter->bidder;
@@ -135,6 +139,10 @@ archive_set_filter_option(struct archive *_a, const char *m, const char *o,
if (r == ARCHIVE_OK)
rv = ARCHIVE_OK;
}
+ /* If the filter name didn't match, return a special code for
+ * _archive_set_option[s]. */
+ if (rv == ARCHIVE_WARN && m != NULL)
+ rv = ARCHIVE_WARN - 1;
return (rv);
}
@@ -1,5 +1,5 @@
/*-
- * Copyright (c) 2010-2011 Michihiro NAKAJIMA
+ * Copyright (c) 2010-2012 Michihiro NAKAJIMA
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -478,11 +478,13 @@ archive_read_format_cab_options(struct archive_read *a,
else
ret = ARCHIVE_FATAL;
}
- } else
- archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
- "cab: unknown keyword ``%s''", key);
+ return (ret);
+ }
- return (ret);
+ /* Note: The "warn" return is just to inform the options
+ * supervisor that we didn't handle it. It will generate
+ * a suitable error if no one used this option. */
+ return (ARCHIVE_WARN);
}
static int
@@ -1,6 +1,6 @@
/*-
* Copyright (c) 2003-2007 Tim Kientzle
- * Copyright (c) 2010-2011 Michihiro NAKAJIMA
+ * Copyright (c) 2010-2012 Michihiro NAKAJIMA
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -325,7 +325,7 @@ archive_read_format_cpio_options(struct archive_read *a,
if (strcmp(key, "compat-2x") == 0) {
/* Handle filnames as libarchive 2.x */
cpio->init_default_conversion = (val != NULL)?1:0;
- ret = ARCHIVE_OK;
+ return (ARCHIVE_OK);
} else if (strcmp(key, "hdrcharset") == 0) {
if (val == NULL || val[0] == 0)
archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
@@ -339,11 +339,13 @@ archive_read_format_cpio_options(struct archive_read *a,
else
ret = ARCHIVE_FATAL;
}
- } else
- archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
- "cpio: unknown keyword ``%s''", key);
+ return (ret);
+ }
- return (ret);
+ /* Note: The "warn" return is just to inform the options
+ * supervisor that we didn't handle it. It will generate
+ * a suitable error if no one used this option. */
+ return (ARCHIVE_WARN);
}
static int
@@ -1,5 +1,5 @@
/*-
- * Copyright (c) 2008-2011 Michihiro NAKAJIMA
+ * Copyright (c) 2008-2012 Michihiro NAKAJIMA
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -445,11 +445,13 @@ archive_read_format_lha_options(struct archive_read *a,
else
ret = ARCHIVE_FATAL;
}
- } else
- archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
- "lha: unknown keyword ``%s''", key);
+ return (ret);
+ }
- return (ret);
+ /* Note: The "warn" return is just to inform the options
+ * supervisor that we didn't handle it. It will generate
+ * a suitable error if no one used this option. */
+ return (ARCHIVE_WARN);
}
static int
@@ -757,11 +757,13 @@ archive_read_format_rar_options(struct archive_read *a,
else
ret = ARCHIVE_FATAL;
}
- } else
- archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
- "rar: unknown keyword ``%s''", key);
-
- return (ret);
+ return (ret);
+ }
+
+ /* Note: The "warn" return is just to inform the options
+ * supervisor that we didn't handle it. It will generate
+ * a suitable error if no one used this option. */
+ return (ARCHIVE_WARN);
}
static int
@@ -1,6 +1,6 @@
/*-
* Copyright (c) 2003-2007 Tim Kientzle
- * Copyright (c) 2011 Michihiro NAKAJIMA
+ * Copyright (c) 2011-2012 Michihiro NAKAJIMA
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -369,7 +369,7 @@ archive_read_format_tar_options(struct archive_read *a,
/* Handle UTF-8 filnames as libarchive 2.x */
tar->compat_2x = (val != NULL)?1:0;
tar->init_default_conversion = tar->compat_2x;
- ret = ARCHIVE_OK;
+ return (ARCHIVE_OK);
} else if (strcmp(key, "hdrcharset") == 0) {
if (val == NULL || val[0] == 0)
archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
@@ -383,11 +383,13 @@ archive_read_format_tar_options(struct archive_read *a,
else
ret = ARCHIVE_FATAL;
}
- } else
- archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
- "tar: unknown keyword ``%s''", key);
+ return (ret);
+ }
- return (ret);
+ /* Note: The "warn" return is just to inform the options
+ * supervisor that we didn't handle it. It will generate
+ * a suitable error if no one used this option. */
+ return (ARCHIVE_WARN);
}
/* utility function- this exists to centralize the logic of tracking
@@ -1,6 +1,6 @@
/*-
* Copyright (c) 2004 Tim Kientzle
- * Copyright (c) 2011 Michihiro NAKAJIMA
+ * Copyright (c) 2011-2012 Michihiro NAKAJIMA
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -509,7 +509,7 @@ archive_read_format_zip_options(struct archive_read *a,
if (strcmp(key, "compat-2x") == 0) {
/* Handle filnames as libarchive 2.x */
zip->init_default_conversion = (val != NULL) ? 1 : 0;
- ret = ARCHIVE_OK;
+ return (ARCHIVE_OK);
} else if (strcmp(key, "hdrcharset") == 0) {
if (val == NULL || val[0] == 0)
archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
@@ -524,11 +524,13 @@ archive_read_format_zip_options(struct archive_read *a,
} else
ret = ARCHIVE_FATAL;
}
- } else
- archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
- "zip: unknown keyword ``%s''", key);
+ return (ret);
+ }
- return (ret);
+ /* Note: The "warn" return is just to inform the options
+ * supervisor that we didn't handle it. It will generate
+ * a suitable error if no one used this option. */
+ return (ARCHIVE_WARN);
}
static int
@@ -216,6 +216,9 @@ archive_compressor_bzip2_options(struct archive_write_filter *f,
return (ARCHIVE_OK);
}
+ /* Note: The "warn" return is just to inform the options
+ * supervisor that we didn't handle it. It will generate
+ * a suitable error if no one used this option. */
return (ARCHIVE_WARN);
}
@@ -230,6 +230,10 @@ archive_compressor_gzip_options(struct archive_write_filter *f, const char *key,
data->compression_level = value[0] - '0';
return (ARCHIVE_OK);
}
+
+ /* Note: The "warn" return is just to inform the options
+ * supervisor that we didn't handle it. It will generate
+ * a suitable error if no one used this option. */
return (ARCHIVE_WARN);
}
@@ -1,6 +1,6 @@
/*-
- * Copyright (c) 2009,2010 Michihiro NAKAJIMA
* Copyright (c) 2003-2010 Tim Kientzle
+ * Copyright (c) 2009-2012 Michihiro NAKAJIMA
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -375,6 +375,9 @@ archive_compressor_xz_options(struct archive_write_filter *f,
return (ARCHIVE_OK);
}
+ /* Note: The "warn" return is just to inform the options
+ * supervisor that we didn't handle it. It will generate
+ * a suitable error if no one used this option. */
return (ARCHIVE_WARN);
}
@@ -413,7 +413,10 @@ _7z_options(struct archive_write *a, const char *key, const char *value)
return (ARCHIVE_OK);
}
- return (ARCHIVE_FAILED);
+ /* Note: The "warn" return is just to inform the options
+ * supervisor that we didn't handle it. It will generate
+ * a suitable error if no one used this option. */
+ return (ARCHIVE_WARN);
}
static int
@@ -147,11 +147,13 @@ archive_write_cpio_options(struct archive_write *a, const char *key,
else
ret = ARCHIVE_FATAL;
}
- } else
- archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
- "%s: unknown keyword ``%s''", a->format_name, key);
+ return (ret);
+ }
- return (ret);
+ /* Note: The "warn" return is just to inform the options
+ * supervisor that we didn't handle it. It will generate
+ * a suitable error if no one used this option. */
+ return (ARCHIVE_WARN);
}
/*
@@ -154,11 +154,13 @@ archive_write_newc_options(struct archive_write *a, const char *key,
else
ret = ARCHIVE_FATAL;
}
- } else
- archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
- "%s: unknown keyword ``%s''", a->format_name, key);
+ return (ret);
+ }
- return (ret);
+ /* Note: The "warn" return is just to inform the options
+ * supervisor that we didn't handle it. It will generate
+ * a suitable error if no one used this option. */
+ return (ARCHIVE_WARN);
}
static struct archive_string_conv *
@@ -1,7 +1,7 @@
/*-
* Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
* Author: Jonas Gastal <jgastal@profusion.mobi>
- * Copyright (c) 2011 Michihiro NAKAJIMA
+ * Copyright (c) 2011-2012 Michihiro NAKAJIMA
*
* All rights reserved.
*
@@ -177,7 +177,8 @@ archive_write_set_format_gnutar(struct archive *_a)
gnutar = (struct gnutar *)calloc(1, sizeof(*gnutar));
if (gnutar == NULL) {
- archive_set_error(&a->archive, ENOMEM, "Can't allocate gnutar data");
+ archive_set_error(&a->archive, ENOMEM,
+ "Can't allocate gnutar data");
return (ARCHIVE_FATAL);
}
a->format_data = gnutar;
@@ -213,11 +214,13 @@ archive_write_gnutar_options(struct archive_write *a, const char *key,
else
ret = ARCHIVE_FATAL;
}
- } else
- archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
- "%s: unknown keyword ``%s''", a->format_name, key);
+ return (ret);
+ }
- return (ret);
+ /* Note: The "warn" return is just to inform the options
+ * supervisor that we didn't handle it. It will generate
+ * a suitable error if no one used this option. */
+ return (ARCHIVE_WARN);
}
static int
Oops, something went wrong.

0 comments on commit f30dc31

Please sign in to comment.