use Lintian::Util qw(normalize_pkg_path); my $path = normalize_pkg_path('usr/bin/', '../lib/git-core/git-pull'); if (defined $path) { # ... } my (@paragraphs); eval { @paragraphs = read_dpkg_control_utf8('some/debian/ctrl/file'); }; if ($@) { # syntax error etc. die "ctrl/file: $@"; } foreach my $para (@paragraphs) { my $value = $para->{'some-field'}; if (defined $value) { # ... } }
Most subs are imported only on request.
If you have very large files (e.g. Packages_amd64), you almost certainly want ``visit_dpkg_paragraph''. Otherwise, one of the convenience functions are probably what you are looking for.
FLAGS (if given) is a bitmask of the DCTRL_* constants. Please refer to ``CONSTANTS'' for the list of constants and their meaning. The default value for FLAGS is 0.
If LINES is given, it should be a reference to an empty list. On return, LINES will be populated with a hashref for each paragraph (in the same order as the returned list). Each hashref will also have a special key "START-OF-PARAGRAPH" that gives the line number of the first field in that paragraph. These hashrefs will map the field name of the given paragraph to the line number where the field name appeared.
This is a convenience sub around ``visit_dpkg_paragraph'' and can therefore produce the same errors as it. Please see ``visit_dpkg_paragraph'' for the finer semantics of how the control file is parsed.
NB: parse_dpkg_control does not close the handle for the caller.
FLAGS (if given) is a bitmask of the DCTRL_* constants. Please refer to ``CONSTANTS'' for the list of constants and their meaning. The default value for FLAGS is 0.
If the file is empty (i.e. it contains no paragraphs), the method will contain an empty list. The deb822 contents may be inside a signed PGP message with a signature.
visit_dpkg_paragraph will require the PGP headers to be correct (if present) and require that the entire file is covered by the signature. However, it will not validate the signature (in fact, the contents of the PGP SIGNATURE part can be empty). The signature should be validated separately.
visit_dpkg_paragraph will pass paragraphs to CODE as they are completed. If CODE can process the paragraphs as they are seen, very large control files can be processed without keeping all the paragraphs in memory.
As a consequence of how the file is parsed, CODE may be passed a number of (valid) paragraphs before parsing is stopped due to a syntax error.
NB: visit_dpkg_paragraph does not close the handle for the caller.
CODE is expected to be a callable reference (e.g. a sub) and will be invoked as the following:
The return value of CODE is ignored.
If the CODE invokes die (or similar) the error is propagated to the caller.
On syntax errors, visit_dpkg_paragraph will call die with the following string:
"syntax error at line %d: %s\n"
Where %d is the line number of the issue and %s is one of:
Otherwise, this behaves like:
use autodie; open(my $fd, '<:encoding(UTF-8)', FILE); # or '<' my @p = parse_dpkg_control($fd, FLAGS, LINES); close($fd); return @p;
This goes without saying that may fail with any of the messages that ``parse_dpkg_control(HANDLE[, FLAGS[, LINES]])'' do. It can also emit autodie exceptions if open or close fails.
Basically, this is a fancy convenience for setting up an ar + tar pipe and passing said pipe to ``parse_dpkg_control(HANDLE[, FLAGS[, LINES]])''.
DEBFILE must be an ar file containing a ``control.tar.gz'' member, which in turn should contain a ``control'' file. If the ``control'' file is empty this will return an empty list.
Note: the control file is only expected to have a single paragraph and thus only the first is returned (in the unlikely case that there are more than one).
This function may fail with any of the messages that ``parse_dpkg_control'' do. It can also emit:
"cannot fork to unpack %s: %s\n"
Note: the control file is only expected to have a single paragraph and thus only the first is returned (in the unlikely case that there are more than one).
This function may fail with any of the messages that ``read_dpkg_control(FILE[, FLAGS[, LINES]])'' do.
May cause an exception if there are issues reading from the pipe.
Caveat: This will block until the pipe is closed from the ``write''-end, so only use it with pipes where the ``write''-end will eventually close their end by themselves (or something else will make them close it).
ALGO can be 'md5' or shaX, where X is any number supported by Digest::SHA (e.g. 'sha256').
This sub is a convenience wrapper around Digest::{MD5,SHA}.
ALGO can be 'md5' or shaX, where X is any number supported by Digest::SHA (e.g. 'sha256').
This sub is a convenience wrapper around Digest::{MD5,SHA}.
The list of whitelisted %ENV variables are:
PATH LC_ALL (*) TMPDIR
(*) LC_ALL is a special case as clean_env will change its value to either ``C.UTF-8'' or ``C'' (if CLOC is given and a truth value).
If the string does not appear to be a valid permission, it will cause a trappable error.
Examples:
# Good perm2oct('-rw-r--r--') == 0644 perm2oct('-rwxr-xr-x') == 0755 # Bad perm2oct('broken') # too short to be recognised perm2oct('-resurunet') # contains unknown permissions
OPTS, if given, is a hash reference with zero or more of the following key-value pairs:
On failure, this sub emits a trappable error.
Note: The handle may be a pipe from an external processes.
If multiple arguments are given, they will be merged into a single string (by join (' ', @_)). If only one argument is given it will be stringified and used directly.
The tool name should follow the same rules as check names. Particularly, third-party checks should namespace their tools in the same way they namespace their checks. E.g. ``python/some-helper''.
If the tool cannot be found, this sub will cause a trappable error.
@lines = map { strip } <$fd>;
In void context, the input argument will be modified so it can be used as a replacement for chomp in some cases:
while ( my $line = <$fd> ) { strip ($line); # $line no longer has any leading or trailing whitespace }
Otherwise, a copy of the string is returned:
while ( my $orig = <$fd> ) { my $stripped = strip ($orig); if ($stripped ne $orig) { # $orig had leading or/and trailing whitespace } }
Remove initial ./ by default
signal_number2name(2) eq 'INT'
As the name suggests, this is a path ``normalization'' rather than a true path resolution (for that use Cwd::realpath). Particularly, it assumes none of the path segments are symlinks.
normalize_pkg_path will return "q{}" (i.e. the empty string) if PATH is normalized to the root dir and "undef" if the path cannot be normalized without escaping the package root.
Examples:
normalize_pkg_path('usr/share/java/../../../usr/share/ant/file')
eq 'usr/share/ant/file'
normalize_pkg_path('usr/..') eq q{};
The following will return C<undef>: normalize_pkg_path('usr/bin/../../../../etc/passwd')
normalize_pkg_path will return "q{}" (i.e. the empty string) if the target is the root dir and "undef" if the path cannot be normalized without escaping the package root.
CAVEAT: This function is not always sufficient to test if it is safe to open a given symlink. Use is_ancestor_of for that. If you must use this function, remember to check that the target is not a symlink (or if it is, that it can be resolved safely).
Examples:
normalize_pkg_path('usr/share/java', '../ant/file') eq 'usr/share/ant/file' normalize_pkg_path('usr/share/java', '../../../usr/share/ant/file') normalize_pkg_path('usr/share/java', '/usr/share/ant/file') eq 'usr/share/ant/file' normalize_pkg_path('/usr/share/java', '/') eq q{}; normalize_pkg_path('/', 'usr/..') eq q{}; The following will return C<undef>: normalize_pkg_path('usr/bin', '../../../../etc/passwd') normalize_pkg_path('usr/bin', '/../etc/passwd')
The following values recognised (string checks are not case sensitive):
This function will resolve the paths; any failure to resolve the path will cause a trappable error.
The subroutine will continue to read from INHANDLE until it is exhausted or an error occurs (either during read or write). In case of errors, a trappable error will be raised. The handles are left open when the subroutine returns, caller must close them afterwards.
Caller should ensure that handles are using ``blocking'' I/O. The subroutine will use sysread and syswrite when reading and writing.
OPTS, if given, may contain the following key-value pairs:
Note the result is by design not deterministic to reduce the risk of all large packages being in the same run (e.g. like gcc-5 + gcc-5-cross + gcc-6 + gcc-6-cross).