Browse Source

dpkg: Add new --validate-<thing> commands

These commands make it possible to test if several of those <thing>s
have a valid syntax. The current list of supported things is «pkgname»,
«trigname», «archname» and «version».
Guillem Jover 7 years ago
parent
commit
4801f15933
5 changed files with 111 additions and 4 deletions
  1. 3 0
      debian/changelog
  2. 22 1
      man/dpkg.man
  3. 66 1
      src/enquiry.c
  4. 10 1
      src/main.c
  5. 10 1
      src/main.h

+ 3 - 0
debian/changelog

@@ -12,6 +12,9 @@ dpkg (1.18.16) UNRELEASED; urgency=medium
     needed on 32-bit architectures with many cores, which results in more
     than the userspace addressable memory, when using settings such as
     -z9 and/or -Sextreme in dpkg-deb. Closes: #846564
+  * Add new dpkg --validate-<thing> commands to validate the syntax of
+    various <thing>s, where the current list is «pkgname», «trigname»,
+    «archname» and «version».
   * Perl modules:
     - Whitelist DPKG_GENSYMBOLS_CHECK_LEVEL, DPKG_ROOT, DPKG_ADMINDIR and
       DPKG_DATADIR environment variables in Dpkg::Build::Info.

+ 22 - 1
man/dpkg.man

@@ -6,7 +6,7 @@
 .\" Copyright © 2000-2003 Adam Heath <doogie@debian.org>
 .\" Copyright © 2002 Josip Rodin
 .\" Copyright © 2004-2005 Scott James Remnant <keybuk@debian.org>
-.\" Copyright © 2006-2015 Guillem Jover <guillem@debian.org>
+.\" Copyright © 2006-2016 Guillem Jover <guillem@debian.org>
 .\" Copyright © 2007-2008 Ian Jackson <ijackson@chiark.greenend.org.uk>
 .\" Copyright © 2008-2011 Raphaël Hertzog <hertzog@debian.org>
 .\"
@@ -374,6 +374,27 @@ Supports multi-arch fields and semantics (since dpkg 1.16.2).
 Supports versioned \fBProvides\fP (since dpkg 1.17.11).
 .RE
 .TP
+.BI \-\-validate\- "thing string"
+Validate that the \fIthing\fP \fIstring\fP has a correct syntaxa
+(since dpkg 1.18.16).
+Returns 0 if the \fIstring\fP is valid, 1 if the \fIstring\fP is invalid but
+might be accepted in lax contexts, and 2 if the \fIstring\fP is invalid.
+The current list of validatable \fIthing\fPs is:
+.RS
+.TP
+.B pkgname
+Validates the given package name (since dpkg 1.18.16).
+.TP
+.B trigname
+Validates the given trigger name (since dpkg 1.18.16).
+.TP
+.B pkgname
+Validates the given architecture name (since dpkg 1.18.16).
+.TP
+.B version
+Validates the given version (since dpkg 1.18.16).
+.RE
+.TP
 .B \-\-compare\-versions \fIver1 op ver2\fP
 Compare version numbers, where \fIop\fP is a binary operator. \fBdpkg\fP
 returns true (\fB0\fP) if the specified condition is satisfied,

+ 66 - 1
src/enquiry.c

@@ -3,7 +3,7 @@
  * enquiry.c - status enquiry and listing options
  *
  * Copyright © 1995,1996 Ian Jackson <ijackson@chiark.greenend.org.uk>
- * Copyright © 2006, 2008-2015 Guillem Jover <guillem@debian.org>
+ * Copyright © 2006, 2008-2016 Guillem Jover <guillem@debian.org>
  * Copyright © 2011 Linaro Limited
  * Copyright © 2011 Raphaël Hertzog <hertzog@debian.org>
  *
@@ -40,6 +40,7 @@
 #include <dpkg/arch.h>
 #include <dpkg/pkg-array.h>
 #include <dpkg/pkg-show.h>
+#include <dpkg/triglib.h>
 #include <dpkg/string.h>
 #include <dpkg/options.h>
 
@@ -596,6 +597,70 @@ print_foreign_arches(const char *const *argv)
   return 0;
 }
 
+int
+validate_pkgname(const char *const *argv)
+{
+  const char *emsg;
+
+  if (!argv[0] || argv[1])
+    badusage(_("--%s takes one <pkgname> argument"), cipaction->olong);
+
+  emsg = pkg_name_is_illegal(argv[0]);
+  if (emsg)
+    ohshit(_("package name '%s' is invalid: %s"), argv[0], emsg);
+
+  return 0;
+}
+
+int
+validate_trigname(const char *const *argv)
+{
+  const char *emsg;
+
+  if (!argv[0] || argv[1])
+    badusage(_("--%s takes one <trigname> argument"), cipaction->olong);
+
+  emsg = trig_name_is_illegal(argv[0]);
+  if (emsg)
+    ohshit(_("trigger name '%s' is invalid: %s"), argv[0], emsg);
+
+  return 0;
+}
+
+int
+validate_archname(const char *const *argv)
+{
+  const char *emsg;
+
+  if (!argv[0] || argv[1])
+    badusage(_("--%s takes one <archname> argument"), cipaction->olong);
+
+  emsg = dpkg_arch_name_is_illegal(argv[0]);
+  if (emsg)
+    ohshit(_("architecture name '%s' is invalid: %s"), argv[0], emsg);
+
+  return 0;
+}
+
+int
+validate_version(const char *const *argv)
+{
+  struct dpkg_version version;
+  struct dpkg_error err;
+
+  if (!argv[0] || argv[1])
+    badusage(_("--%s takes one <version> argument"), cipaction->olong);
+
+  if (parseversion(&version, argv[0], &err) < 0) {
+    dpkg_error_print(&err, _("version '%s' has bad syntax"), argv[0]);
+    dpkg_error_destroy(&err);
+
+    return 1;
+  }
+
+  return 0;
+}
+
 int
 cmpversions(const char *const *argv)
 {

+ 10 - 1
src/main.c

@@ -3,7 +3,7 @@
  * main.c - main program
  *
  * Copyright © 1994,1995 Ian Jackson <ijackson@chiark.greenend.org.uk>
- * Copyright © 2006-2015 Guillem Jover <guillem@debian.org>
+ * Copyright © 2006-2016 Guillem Jover <guillem@debian.org>
  * Copyright © 2010 Canonical Ltd.
  *   written by Martin Pitt <martin.pitt@canonical.com>
  *
@@ -111,6 +111,7 @@ usage(const struct cmdinfo *ci, const char *value)
 "  --print-architecture             Print dpkg architecture.\n"
 "  --print-foreign-architectures    Print allowed foreign architectures.\n"
 "  --assert-<feature>               Assert support for the specified feature.\n"
+"  --validate-<thing> <string>      Validate a <thing>'s <string>.\n"
 "  --compare-versions <a> <op> <b>  Compare version numbers - see below.\n"
 "  --force-help                     Show help on forcing.\n"
 "  -Dh|--debug=help                 Show help on debugging.\n"
@@ -124,6 +125,10 @@ usage(const struct cmdinfo *ci, const char *value)
   printf(_(
 "Assertable features: support-predepends, working-epoch, long-filenames,\n"
 "  multi-conrep, multi-arch, versioned-provides.\n"
+"\n"));
+
+  printf(_(
+"Validatable things: pkgname, archname, trigname, version.\n"
 "\n"));
 
   printf(_(
@@ -704,6 +709,10 @@ static const struct cmdinfo cmdinfos[]= {
   ACTION( "print-architecture",              0,  act_printarch,            printarch   ),
   ACTION( "print-foreign-architectures",     0,  act_printforeignarches,   print_foreign_arches ),
   ACTION( "predep-package",                  0,  act_predeppackage,        predeppackage   ),
+  ACTION( "validate-pkgname",                0,  act_validate_pkgname,     validate_pkgname ),
+  ACTION( "validate-trigname",               0,  act_validate_trigname,    validate_trigname ),
+  ACTION( "validate-archname",               0,  act_validate_archname,    validate_archname ),
+  ACTION( "validate-version",                0,  act_validate_version,     validate_version ),
   ACTION( "compare-versions",                0,  act_cmpversions,          cmpversions     ),
 /*
   ACTION( "command-fd",                   'c', act_commandfd,   commandfd     ),

+ 10 - 1
src/main.h

@@ -3,7 +3,7 @@
  * main.h - external definitions for this program
  *
  * Copyright © 1995 Ian Jackson <ijackson@chiark.greenend.org.uk>
- * Copyright © 2006, 2008-2015 Guillem Jover <guillem@debian.org>
+ * Copyright © 2006, 2008-2016 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
@@ -110,6 +110,11 @@ enum action {
 	act_assertmultiarch,
 	act_assertverprovides,
 
+	act_validate_pkgname,
+	act_validate_trigname,
+	act_validate_archname,
+	act_validate_version,
+
 	act_audit,
 	act_unpackchk,
 	act_predeppackage,
@@ -177,6 +182,10 @@ int assertlongfilenames(const char *const *argv);
 int assertmulticonrep(const char *const *argv);
 int assertmultiarch(const char *const *argv);
 int assertverprovides(const char *const *argv);
+int validate_pkgname(const char *const *argv);
+int validate_trigname(const char *const *argv);
+int validate_archname(const char *const *argv);
+int validate_version(const char *const *argv);
 int predeppackage(const char *const *argv);
 int printarch(const char *const *argv);
 int printinstarch(const char *const *argv);