Browse Source

libdpkg: Switch compression strategy code from strings to enums

Make those values global, because they could be shared by different
compressors, because for validation purposes there's no point in making
them compressor specific as that's too late in case the compressor has
not been specified yet. And finally using enums instead of strings
allows to more easily handle the different strategy values.
Guillem Jover 14 years ago
parent
commit
d630b04733
5 changed files with 42 additions and 37 deletions
  1. 1 1
      dpkg-deb/build.c
  2. 10 2
      dpkg-deb/main.c
  3. 20 32
      lib/dpkg/compress.c
  4. 10 2
      lib/dpkg/compress.h
  5. 1 0
      lib/dpkg/libdpkg.map

+ 1 - 1
dpkg-deb/build.c

@@ -479,8 +479,8 @@ do_build(const char *const *argv)
     struct compress_params params;
 
     params.type = compressor_type_gzip;
+    params.strategy = compressor_strategy_none;
     params.level = 9;
-    params.strategy = NULL;
 
     compress_filter(&params, p1[0], gzfd, _("control member"));
     exit(0);

+ 10 - 2
dpkg-deb/main.c

@@ -142,8 +142,8 @@ int oldformatflag = 0;
 int opt_verbose = 0;
 struct compress_params compress_params = {
   .type = compressor_type_gzip,
+  .strategy = compressor_strategy_none,
   .level = -1,
-  .strategy = NULL,
 };
 
 static void
@@ -163,6 +163,14 @@ set_compress_level(const struct cmdinfo *cip, const char *value)
   compress_params.level = level;
 }
 
+static void
+set_compress_strategy(const struct cmdinfo *cip, const char *value)
+{
+  compress_params.strategy = compressor_get_strategy(value);
+  if (compress_params.strategy == compressor_strategy_unknown)
+    ohshit(_("unknown compression strategy '%s'!"), value);
+}
+
 static void
 setcompresstype(const struct cmdinfo *cip, const char *value)
 {
@@ -192,7 +200,7 @@ static const struct cmdinfo cmdinfos[]= {
   { "nocheck",       0,   0, &nocheckflag,   NULL,         NULL,          1 },
   { NULL,            'z', 1, NULL,           NULL,         set_compress_level },
   { NULL,            'Z', 1, NULL,           NULL,         setcompresstype  },
-  { NULL,            'S', 1, NULL,           &compress_params.strategy, NULL },
+  { NULL,            'S', 1, NULL,           NULL,         set_compress_strategy },
   { "showformat",    0,   1, NULL,           &showformat,  NULL             },
   { "help",          'h', 0, NULL,           NULL,         usage            },
   { "version",       0,   0, NULL,           NULL,         printversion     },

+ 20 - 32
lib/dpkg/compress.c

@@ -4,7 +4,7 @@
  *
  * Copyright © 2000 Wichert Akkerman <wakkerma@debian.org>
  * Copyright © 2004 Scott James Remnant <scott@netsplit.com>
- * Copyright © 2006-2010 Guillem Jover <guillem@debian.org>
+ * Copyright © 2006-2012 Guillem Jover <guillem@debian.org>
  *
  * This is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -78,8 +78,6 @@ struct compressor {
 	const char *name;
 	const char *extension;
 	int default_level;
-	bool (*check_params)(struct compress_params *params,
-	                     struct dpkg_error *err);
 	void (*fixup_params)(struct compress_params *params);
 	void (*compress)(int fd_in, int fd_out, struct compress_params *params,
 	                 const char *desc);
@@ -90,17 +88,6 @@ struct compressor {
  * No compressor (pass-through).
  */
 
-static bool
-check_none_params(struct compress_params *params, struct dpkg_error *err)
-{
-	if (params->strategy) {
-		dpkg_put_warn(err, _("unknown compression strategy"));
-		return false;
-	}
-
-	return true;
-}
-
 static void
 fixup_none_params(struct compress_params *params)
 {
@@ -122,7 +109,6 @@ static const struct compressor compressor_none = {
 	.name = "none",
 	.extension = "",
 	.default_level = 0,
-	.check_params = check_none_params,
 	.fixup_params = fixup_none_params,
 	.compress = compress_none,
 	.decompress = decompress_none,
@@ -242,7 +228,6 @@ static const struct compressor compressor_gzip = {
 	.name = "gzip",
 	.extension = ".gz",
 	.default_level = 9,
-	.check_params = check_none_params,
 	.fixup_params = fixup_gzip_params,
 	.compress = compress_gzip,
 	.decompress = decompress_gzip,
@@ -367,7 +352,6 @@ static const struct compressor compressor_bzip2 = {
 	.name = "bzip2",
 	.extension = ".bz2",
 	.default_level = 9,
-	.check_params = check_none_params,
 	.fixup_params = fixup_bzip2_params,
 	.compress = compress_bzip2,
 	.decompress = decompress_bzip2,
@@ -379,17 +363,6 @@ static const struct compressor compressor_bzip2 = {
 
 #define XZ		"xz"
 
-static bool
-check_xz_params(struct compress_params *params, struct dpkg_error *err)
-{
-	if (params->strategy && strcmp(params->strategy, "extreme") != 0) {
-		dpkg_put_warn(err, _("unknown compression strategy"));
-		return false;
-	}
-
-	return true;
-}
-
 static void
 decompress_xz(int fd_in, int fd_out, const char *desc)
 {
@@ -402,7 +375,7 @@ compress_xz(int fd_in, int fd_out, struct compress_params *params, const char *d
 	char combuf[6];
 	const char *strategy;
 
-	if (params->strategy && strcmp(params->strategy, "extreme") == 0)
+	if (params->strategy == compressor_strategy_extreme)
 		strategy = "-e";
 	else
 		strategy = NULL;
@@ -415,7 +388,6 @@ static const struct compressor compressor_xz = {
 	.name = "xz",
 	.extension = ".xz",
 	.default_level = 6,
-	.check_params = check_xz_params,
 	.fixup_params = fixup_none_params,
 	.compress = compress_xz,
 	.decompress = decompress_xz,
@@ -444,7 +416,6 @@ static const struct compressor compressor_lzma = {
 	.name = "lzma",
 	.extension = ".lzma",
 	.default_level = 6,
-	.check_params = check_none_params,
 	.fixup_params = fixup_none_params,
 	.compress = compress_lzma,
 	.decompress = decompress_lzma,
@@ -503,10 +474,27 @@ compressor_find_by_extension(const char *extension)
 	return compressor_type_unknown;
 }
 
+enum compressor_strategy
+compressor_get_strategy(const char *name)
+{
+	if (strcmp(name, "extreme") == 0)
+		return compressor_strategy_extreme;
+
+	return compressor_strategy_unknown;
+}
+
 bool
 compressor_check_params(struct compress_params *params, struct dpkg_error *err)
 {
-	return compressor(params->type)->check_params(params, err);
+	if (params->strategy == compressor_strategy_none)
+		return true;
+
+	if (params->type == compressor_type_xz &&
+	    params->strategy == compressor_strategy_extreme)
+		return true;
+
+	dpkg_put_error(err, _("unknown compression strategy"));
+	return false;
 }
 
 static void

+ 10 - 2
lib/dpkg/compress.h

@@ -3,7 +3,7 @@
  * compress.h - compression support functions
  *
  * Copyright © 2004 Scott James Remnant <scott@netsplit.com>
- * Copyright © 2006-2009 Guillem Jover <guillem@debian.org>
+ * Copyright © 2006-2012 Guillem Jover <guillem@debian.org>
  *
  * This is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -44,9 +44,15 @@ enum compressor_type {
 	compressor_type_lzma,
 };
 
+enum compressor_strategy {
+	compressor_strategy_unknown = -1,
+	compressor_strategy_none,
+	compressor_strategy_extreme,
+};
+
 struct compress_params {
 	enum compressor_type type;
-	const char *strategy;
+	enum compressor_strategy strategy;
 	int level;
 };
 
@@ -55,6 +61,8 @@ enum compressor_type compressor_find_by_extension(const char *name);
 
 const char *compressor_get_extension(enum compressor_type type);
 
+enum compressor_strategy compressor_get_strategy(const char *name);
+
 bool compressor_check_params(struct compress_params *params,
                              struct dpkg_error *err);
 

+ 1 - 0
lib/dpkg/libdpkg.map

@@ -137,6 +137,7 @@ LIBDPKG_PRIVATE {
 	compressor_find_by_name;
 	compressor_find_by_extension;
 	compressor_get_extension;
+	compressor_get_strategy;
 	compressor_check_params;
 	compress_filter;
 	decompress_filter;