View source with raw comments or as raw
    1/*  Part of SWI-Prolog
    2
    3    Author:        Jan Wielemaker and Matt Lilley
    4    E-mail:        J.Wielemaker@cs.vu.nl
    5    WWW:           http://www.swi-prolog.org
    6    Copyright (c)  2012-2019, VU University Amsterdam
    7    All rights reserved.
    8
    9    Redistribution and use in source and binary forms, with or without
   10    modification, are permitted provided that the following conditions
   11    are met:
   12
   13    1. Redistributions of source code must retain the above copyright
   14       notice, this list of conditions and the following disclaimer.
   15
   16    2. Redistributions in binary form must reproduce the above copyright
   17       notice, this list of conditions and the following disclaimer in
   18       the documentation and/or other materials provided with the
   19       distribution.
   20
   21    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   22    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   23    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   24    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   25    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   26    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   27    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   28    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   29    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   30    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   31    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   32    POSSIBILITY OF SUCH DAMAGE.
   33*/
   34
   35:- module(archive,
   36          [ archive_open/3,             % +Stream, -Archive, +Options
   37            archive_open/4,             % +Stream, +Mode, -Archive, +Options
   38            archive_create/3,           % +OutputFile, +InputFileList, +Options
   39            archive_close/1,            % +Archive
   40            archive_property/2,         % +Archive, ?Property
   41            archive_next_header/2,      % +Archive, -Name
   42            archive_open_entry/2,       % +Archive, -EntryStream
   43            archive_header_property/2,  % +Archive, ?Property
   44            archive_set_header_property/2,      % +Archive, +Property
   45            archive_extract/3,          % +Archive, +Dir, +Options
   46
   47            archive_entries/2,          % +Archive, -Entries
   48            archive_data_stream/3,      % +Archive, -DataStream, +Options
   49            archive_foldl/4             % :Goal, +Archive, +State0, -State
   50          ]).   51:- autoload(library(error),
   52	    [existence_error/2,domain_error/2,must_be/2]).   53:- autoload(library(filesex),
   54	    [directory_file_path/3,make_directory_path/1]).   55:- autoload(library(lists),[member/2]).   56:- autoload(library(option),[option/3,option/2]).   57
   58:- meta_predicate
   59    archive_foldl(4, +, +, -).

Access several archive formats

This library uses libarchive to access a variety of archive formats. The following example lists the entries in an archive:

list_archive(File) :-
    setup_call_cleanup(
        archive_open(File, Archive, []),
        (   repeat,
            (   archive_next_header(Archive, Path)
            ->  format('~w~n', [Path]),
                fail
            ;   !
            )
        ),
        archive_close(Archive)).

Here is an alternative way of doing this, using archive_foldl/4, a higher level predicate.

list_archive2(File) :-
    list_archive(File, Headers),
    maplist(writeln, Headers).

list_archive2(File, Headers) :-
    archive_foldl(add_header, File, Headers, []).

add_header(Path, _, [Path|Paths], Paths).

Here is another example which counts the files in the archive and prints file type information, also using archive_foldl/4:

print_entry(Path, Handle, Cnt0, Cnt1) :-
    archive_header_property(Handle, filetype(Type)),
    format('File ~w is of type ~w~n', [Path, Type]),
    Cnt1 is Cnt0 + 1.

list_archive_headers(File) :-
    archive_foldl(print_entry, File, 0, FileCount),
    format('We have ~w files', [FileCount]).
See also
- https://github.com/libarchive/libarchive/ */
  111:- use_foreign_library(foreign(archive4pl)).
 archive_open(+Data, -Archive, +Options) is det
Wrapper around archive_open/4 that opens the archive in read mode.
  117archive_open(Stream, Archive, Options) :-
  118    archive_open(Stream, read, Archive, Options).
  119
  120:- predicate_options(archive_open/4, 4,
  121                     [ close_parent(boolean),
  122                       filter(oneof([all,bzip2,compress,gzip,grzip,lrzip,
  123                                     lzip,lzma,lzop,none,rpm,uu,xz])),
  124                       format(oneof([all,'7zip',ar,cab,cpio,empty,gnutar,
  125                                     iso9660,lha,mtree,rar,raw,tar,xar,zip]))
  126                     ]).  127:- predicate_options(archive_create/3, 3,
  128                     [ directory(atom),
  129                       pass_to(archive_open/4, 4)
  130                     ]).
 archive_open(+Data, +Mode, -Archive, +Options) is det
Open the archive in Data and unify Archive with a handle to the opened archive. Data is either a file or a stream that contains a valid archive. Mode is either read or write. Details are controlled by Options. Typically, the option close_parent(true) is used to close an entry stream if the archive is closed using archive_close/1. For other options when reading, the defaults are typically fine - for writing, a valid format and optional filters must be specified. The option format(raw) must be used to process compressed streams that do not contain explicit entries (e.g., gzip'ed data) unambibuously. The raw format creates a pseudo archive holding a single member named data.
close_parent(+Boolean)
If this option is true (default false), Data stream is closed if archive_close/1 is called on Archive.
compression(+Compression)
Synomym for filter(Compression). Deprecated.
filter(+Filter)
Support the indicated filter. This option may be used multiple times to support multiple filters. In read mode, If no filter options are provided, all is assumed. In write mode, none is assumed. Supported values are all, bzip2, compress, gzip, grzip, lrzip, lzip, lzma, lzop, none, rpm, uu and xz. The value all is default for read, none for write.
format(+Format)
Support the indicated format. This option may be used multiple times to support multiple formats in read mode. In write mode, you must supply a single format. If no format options are provided, all is assumed for read mode. Note that all does not include raw and mtree. To open both archive and non-archive files, both format(all) and format(raw) and/or format(mtree) must be specified. Supported values are: all, 7zip, ar, cab, cpio, empty, gnutar, iso9660, lha, mtree, rar, raw, tar, xar and zip. The value all is default for read.

Note that the actually supported compression types and formats may vary depending on the version and installation options of the underlying libarchive library. This predicate raises a domain or permission error if the (explicitly) requested format or filter is not supported.

Errors
- domain_error(filter, Filter) if the requested filter is invalid (e.g., all for writing).
- domain_error(format, Format) if the requested format type is not supported.
- permission_error(set, filter, Filter) if the requested filter is not supported.
  187archive_open(stream(Stream), Mode, Archive, Options) :-
  188    !,
  189    archive_open_stream(Stream, Mode, Archive, Options).
  190archive_open(Stream, Mode, Archive, Options) :-
  191    is_stream(Stream),
  192    !,
  193    archive_open_stream(Stream, Mode, Archive, Options).
  194archive_open(File, Mode, Archive, Options) :-
  195    open(File, Mode, Stream, [type(binary)]),
  196    catch(archive_open_stream(Stream, Mode, Archive, [close_parent(true)|Options]),
  197          E, (close(Stream, [force(true)]), throw(E))).
 archive_close(+Archive) is det
Close the archive. If close_parent(true) was specified in archive_open/4, the underlying entry stream is closed too. If there is an entry opened with archive_open_entry/2, actually closing the archive is delayed until the stream associated with the entry is closed. This can be used to open a stream to an archive entry without having to worry about closing the archive:
archive_open_named(ArchiveFile, EntryName, Stream) :-
    archive_open(ArchiveFile, Archive, []),
    archive_next_header(Archive, EntryName),
    archive_open_entry(Archive, Stream),
    archive_close(Archive).
 archive_property(+Handle, ?Property) is nondet
True when Property is a property of the archive Handle. Defined properties are:
filters(List)
True when the indicated filters are applied before reaching the archive format.
  227archive_property(Handle, Property) :-
  228    defined_archive_property(Property),
  229    Property =.. [Name,Value],
  230    archive_property(Handle, Name, Value).
  231
  232defined_archive_property(filter(_)).
 archive_next_header(+Handle, -Name) is semidet
Forward to the next entry of the archive for which Name unifies with the pathname of the entry. Fails silently if the end of the archive is reached before success. Name is typically specified if a single entry must be accessed and unbound otherwise. The following example opens a Prolog stream to a given archive entry. Note that Stream must be closed using close/1 and the archive must be closed using archive_close/1 after the data has been used. See also setup_call_cleanup/3.
open_archive_entry(ArchiveFile, EntryName, Stream) :-
    open(ArchiveFile, read, In, [type(binary)]),
    archive_open(In, Archive, [close_parent(true)]),
    archive_next_header(Archive, EntryName),
    archive_open_entry(Archive, Stream).
Errors
- permission_error(next_header, archive, Handle) if a previously opened entry is not closed.
 archive_open_entry(+Archive, -Stream) is det
Open the current entry as a stream. Stream must be closed. If the stream is not closed before the next call to archive_next_header/2, a permission error is raised.
 archive_set_header_property(+Archive, +Property)
Set Property of the current header. Write-mode only. Defined properties are:
filetype(-Type)
Type is one of file, link, socket, character_device, block_device, directory or fifo. It appears that this library can also return other values. These are returned as an integer.
mtime(-Time)
True when entry was last modified at time.
size(-Bytes)
True when entry is Bytes long.
link_target(-Target)
Target for a link. Currently only supported for symbolic links.
 archive_header_property(+Archive, ?Property)
True when Property is a property of the current header. Defined properties are:
filetype(-Type)
Type is one of file, link, socket, character_device, block_device, directory or fifo. It appears that this library can also return other values. These are returned as an integer.
mtime(-Time)
True when entry was last modified at time.
size(-Bytes)
True when entry is Bytes long.
link_target(-Target)
Target for a link. Currently only supported for symbolic links.
format(-Format)
Provides the name of the archive format applicable to the current entry. The returned value is the lowercase version of the output of archive_format_name().
permissions(-Integer)
True when entry has the indicated permission mask.
  306archive_header_property(Archive, Property) :-
  307    (   nonvar(Property)
  308    ->  true
  309    ;   header_property(Property)
  310    ),
  311    archive_header_prop_(Archive, Property).
  312
  313header_property(filetype(_)).
  314header_property(mtime(_)).
  315header_property(size(_)).
  316header_property(link_target(_)).
  317header_property(format(_)).
  318header_property(permissions(_)).
 archive_extract(+ArchiveFile, +Dir, +Options)
Extract files from the given archive into Dir. Supported options:
remove_prefix(+Prefix)
Strip Prefix from all entries before extracting. If Prefix is a list, then each prefix is tried in order, succeding at the first one that matches. If no prefixes match, an error is reported. If Prefix is an atom, then that prefix is removed.
exclude(+ListOfPatterns)
Ignore members that match one of the given patterns. Patterns are handed to wildcard_match/2.
include(+ListOfPatterns)
Include members that match one of the given patterns. Patterns are handed to wildcard_match/2. The exclude options takes preference if a member matches both the include and the exclude option.
Errors
- existence_error(directory, Dir) if Dir does not exist or is not a directory.
- domain_error(path_prefix(Prefix), Path) if a path in the archive does not start with Prefix
To be done
- Add options
  346archive_extract(Archive, Dir, Options) :-
  347    (   exists_directory(Dir)
  348    ->  true
  349    ;   existence_error(directory, Dir)
  350    ),
  351    setup_call_cleanup(
  352        archive_open(Archive, Handle, Options),
  353        extract(Handle, Dir, Options),
  354        archive_close(Handle)).
  355
  356extract(Archive, Dir, Options) :-
  357    archive_next_header(Archive, Path),
  358    !,
  359    option(include(InclPatterns), Options, ['*']),
  360    option(exclude(ExclPatterns), Options, []),
  361    (   archive_header_property(Archive, filetype(file)),
  362        \+ matches(ExclPatterns, Path),
  363        matches(InclPatterns, Path)
  364    ->  archive_header_property(Archive, permissions(Perm)),
  365        remove_prefix(Options, Path, ExtractPath),
  366        directory_file_path(Dir, ExtractPath, Target),
  367        file_directory_name(Target, FileDir),
  368        make_directory_path(FileDir),
  369        setup_call_cleanup(
  370            archive_open_entry(Archive, In),
  371            setup_call_cleanup(
  372                open(Target, write, Out, [type(binary)]),
  373                copy_stream_data(In, Out),
  374                close(Out)),
  375            close(In)),
  376        set_permissions(Perm, Target)
  377    ;   true
  378    ),
  379    extract(Archive, Dir, Options).
  380extract(_, _, _).
 matches(+Patterns, +Path) is semidet
True when Path matches a pattern in Patterns.
  386matches([], _Path) :-
  387    !,
  388    fail.
  389matches(Patterns, Path) :-
  390    split_string(Path, "/", "/", Parts),
  391    member(Segment, Parts),
  392    Segment \== "",
  393    member(Pattern, Patterns),
  394    wildcard_match(Pattern, Segment),
  395    !.
  396
  397remove_prefix(Options, Path, ExtractPath) :-
  398    (   option(remove_prefix(Remove), Options)
  399    ->  (   is_list(Remove)
  400        ->  (   member(P, Remove),
  401                atom_concat(P, ExtractPath, Path)
  402            ->  true
  403            ;   domain_error(path_prefix(Remove), Path)
  404            )
  405        ;   (   atom_concat(Remove, ExtractPath, Path)
  406            ->  true
  407            ;   domain_error(path_prefix(Remove), Path)
  408            )
  409        )
  410    ;   ExtractPath = Path
  411    ).
 set_permissions(+Perm:integer, +Target:atom)
Restore the permissions. Currently only restores the executable permission.
  418set_permissions(Perm, Target) :-
  419    Perm /\ 0o100 =\= 0,
  420    !,
  421    '$mark_executable'(Target).
  422set_permissions(_, _).
  423
  424
  425                 /*******************************
  426                 *    HIGH LEVEL PREDICATES     *
  427                 *******************************/
 archive_entries(+Archive, -Paths) is det
True when Paths is a list of pathnames appearing in Archive.
  433archive_entries(Archive, Paths) :-
  434    setup_call_cleanup(
  435        archive_open(Archive, Handle, []),
  436        contents(Handle, Paths),
  437        archive_close(Handle)).
  438
  439contents(Handle, [Path|T]) :-
  440    archive_next_header(Handle, Path),
  441    !,
  442    contents(Handle, T).
  443contents(_, []).
 archive_data_stream(+Archive, -DataStream, +Options) is nondet
True when DataStream is a stream to a data object inside Archive. This predicate transparently unpacks data inside possibly nested archives, e.g., a tar file inside a zip file. It applies the appropriate decompression filters and thus ensures that Prolog reads the plain data from DataStream. DataStream must be closed after the content has been processed. Backtracking opens the next member of the (nested) archive. This predicate processes the following options:
meta_data(-Data:list(dict))
If provided, Data is unified with a list of filters applied to the (nested) archive to open the current DataStream. The first element describes the outermost archive. Each Data dict contains the header properties (archive_header_property/2) as well as the keys:
filters(Filters:list(atom))
Filter list as obtained from archive_property/2
name(Atom)
Name of the entry.

Non-archive files are handled as pseudo-archives that hold a single stream. This is implemented by using archive_open/3 with the options [format(all),format(raw)].

  472archive_data_stream(Archive, DataStream, Options) :-
  473    option(meta_data(MetaData), Options, _),
  474    archive_content(Archive, DataStream, MetaData, []).
  475
  476archive_content(Archive, Entry, [EntryMetadata|PipeMetadataTail], PipeMetadata2) :-
  477    archive_property(Archive, filter(Filters)),
  478    repeat,
  479    (   archive_next_header(Archive, EntryName)
  480    ->  findall(EntryProperty,
  481                archive_header_property(Archive, EntryProperty),
  482                EntryProperties),
  483        dict_create(EntryMetadata, archive_meta_data,
  484                    [ filters(Filters),
  485                      name(EntryName)
  486                    | EntryProperties
  487                    ]),
  488        (   EntryMetadata.filetype == file
  489        ->  archive_open_entry(Archive, Entry0),
  490            (   EntryName == data,
  491                EntryMetadata.format == raw
  492            ->  % This is the last entry in this nested branch.
  493                % We therefore close the choicepoint created by repeat/0.
  494                % Not closing this choicepoint would cause
  495                % archive_next_header/2 to throw an exception.
  496                !,
  497                PipeMetadataTail = PipeMetadata2,
  498                Entry = Entry0
  499            ;   PipeMetadataTail = PipeMetadata1,
  500                open_substream(Entry0,
  501                               Entry,
  502                               PipeMetadata1,
  503                               PipeMetadata2)
  504            )
  505        ;   fail
  506        )
  507    ;   !,
  508        fail
  509    ).
  510
  511open_substream(In, Entry, ArchiveMetadata, PipeTailMetadata) :-
  512    setup_call_cleanup(
  513        archive_open(stream(In),
  514                     Archive,
  515                     [ close_parent(true),
  516                       format(all),
  517                       format(raw)
  518                     ]),
  519        archive_content(Archive, Entry, ArchiveMetadata, PipeTailMetadata),
  520        archive_close(Archive)).
 archive_create(+OutputFile, +InputFiles, +Options) is det
Convenience predicate to create an archive in OutputFile with data from a list of InputFiles and the given Options.

Besides options supported by archive_open/4, the following options are supported:

directory(+Directory)
Changes the directory before adding input files. If this is specified, paths of input files must be relative to Directory and archived files will not have Directory as leading path. This is to simulate -C option of the tar program.
format(+Format)
Write mode supports the following formats: `7zip`, cpio, gnutar, iso9660, xar and zip. Note that a particular installation may support only a subset of these, depending on the configuration of libarchive.
  544archive_create(OutputFile, InputFiles, Options) :-
  545    must_be(list(text), InputFiles),
  546    option(directory(BaseDir), Options, '.'),
  547    setup_call_cleanup(
  548        archive_open(OutputFile, write, Archive, Options),
  549        archive_create_1(Archive, BaseDir, BaseDir, InputFiles, top),
  550        archive_close(Archive)).
  551
  552archive_create_1(_, _, _, [], _) :- !.
  553archive_create_1(Archive, Base, Current, ['.'|Files], sub) :-
  554    !,
  555    archive_create_1(Archive, Base, Current, Files, sub).
  556archive_create_1(Archive, Base, Current, ['..'|Files], Where) :-
  557    !,
  558    archive_create_1(Archive, Base, Current, Files, Where).
  559archive_create_1(Archive, Base, Current, [File|Files], Where) :-
  560    directory_file_path(Current, File, Filename),
  561    archive_create_2(Archive, Base, Filename),
  562    archive_create_1(Archive, Base, Current, Files, Where).
  563
  564archive_create_2(Archive, Base, Directory) :-
  565    exists_directory(Directory),
  566    !,
  567    entry_name(Base, Directory, Directory0),
  568    archive_next_header(Archive, Directory0),
  569    time_file(Directory, Time),
  570    archive_set_header_property(Archive, mtime(Time)),
  571    archive_set_header_property(Archive, filetype(directory)),
  572    archive_open_entry(Archive, EntryStream),
  573    close(EntryStream),
  574    directory_files(Directory, Files),
  575    archive_create_1(Archive, Base, Directory, Files, sub).
  576archive_create_2(Archive, Base, Filename) :-
  577    entry_name(Base, Filename, Filename0),
  578    archive_next_header(Archive, Filename0),
  579    size_file(Filename, Size),
  580    time_file(Filename, Time),
  581    archive_set_header_property(Archive, size(Size)),
  582    archive_set_header_property(Archive, mtime(Time)),
  583    setup_call_cleanup(
  584        archive_open_entry(Archive, EntryStream),
  585        setup_call_cleanup(
  586            open(Filename, read, DataStream, [type(binary)]),
  587            copy_stream_data(DataStream, EntryStream),
  588            close(DataStream)),
  589        close(EntryStream)).
  590
  591entry_name('.', Name, Name) :- !.
  592entry_name(Base, Name, EntryName) :-
  593    directory_file_path(Base, EntryName, Name).
 archive_foldl(:Goal, +Archive, +State0, -State)
Operates like foldl/4 but for the entries in the archive. For each member of the archive, Goal called as `call(:Goal, +Path, +Handle, +S0, -S1). Here, S0 is current state of the accumulator (starting with State0) and S1 is the next state of the accumulator, producing State after the last member of the archive.
Arguments:
Archive- File name or stream to be given to archive_open/[3,4].
See also
- archive_header_property/2, archive_open/4.
  607archive_foldl(Goal, Archive, State0, State) :-
  608    setup_call_cleanup(
  609        archive_open(Archive, Handle, [close_parent(true)]),
  610        archive_foldl_(Goal, Handle, State0, State),
  611        archive_close(Handle)
  612    ).
  613
  614archive_foldl_(Goal, Handle, State0, State) :-
  615    (   archive_next_header(Handle, Path)
  616    ->  call(Goal, Path, Handle, State0, State1),
  617        archive_foldl_(Goal, Handle, State1, State)
  618    ;   State = State0
  619    ).
  620
  621
  622		 /*******************************
  623		 *           MESSAGES		*
  624		 *******************************/
  625
  626:- multifile prolog:error_message//1.  627
  628prolog:error_message(archive_error(Code, Message)) -->
  629    [ 'Archive error (code ~p): ~w'-[Code, Message] ]