View source with raw comments or as raw
    1/*  Part of SWI-Prolog
    2
    3    Author:        Jan Wielemaker
    4    E-mail:        J.Wielemaker@vu.nl
    5    WWW:           http://www.swi-prolog.org
    6    Copyright (c)  2008-2013, University of Amsterdam
    7                              VU University Amsterdam
    8    All rights reserved.
    9
   10    Redistribution and use in source and binary forms, with or without
   11    modification, are permitted provided that the following conditions
   12    are met:
   13
   14    1. Redistributions of source code must retain the above copyright
   15       notice, this list of conditions and the following disclaimer.
   16
   17    2. Redistributions in binary form must reproduce the above copyright
   18       notice, this list of conditions and the following disclaimer in
   19       the documentation and/or other materials provided with the
   20       distribution.
   21
   22    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   23    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   24    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   25    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   26    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   27    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   28    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   29    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   30    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   31    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   32    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   33    POSSIBILITY OF SUCH DAMAGE.
   34*/
   35
   36:- module(prolog_hotfix,
   37          [ load_hotfixes/1             % +Directory
   38          ]).   39:- use_module(library(lists)).   40:- use_module(library(option)).   41:- use_module(library(readutil)).   42:- use_module(library(apply)).   43:- use_module(library(prolog_source)).

Load hotfixes into executables

This library was developed to deal with hotfixing products that are distributed as a Prolog saved state. It assumes the vendor is willing to distribute hotfixes as Prolog source files. These files are placed into a directory. The predicate load_hotfixes/1 replaces files that are loaded into the saved state.

Resolution of the file to load is based on the module if the hotfix file provides a module. If the hotfix file is not a module file and there are multiple loaded source files with the same name from different directories, the hotfix directory must create the minimal directory structure to make the paths unique. If omitted, this library will prompt the user for the file that must be replaced.

author
- Jan Wielemaker */
To be done
- This could be extended in several ways:
  • Load hotfixes from a (encrypted) zip file
  • Use digital signatures and load over HTTP
  • Replace individual predicates
 load_hotfixes(+Dir) is det
Load all hotfixes that have not yet been applied into the current state.
   74load_hotfixes(Dir) :-
   75    absolute_file_name(Dir, DirPath,
   76                       [ file_type(directory),
   77                         access(read)
   78                       ]),
   79    phrase(prolog_source_files([DirPath]), Files),
   80    ensure_dirsep(DirPath, Common),
   81    maplist(apply_hotfix(Common), Files).
 prolog_source_files(+Dirs)// is det
Find all Prolog source files in the given directory.
   88prolog_source_files([]) --> !.
   89prolog_source_files([H|T]) -->
   90    !,
   91    prolog_source_files(H),
   92    prolog_source_files(T).
   93prolog_source_files(F) -->
   94    { exists_file(F),
   95      file_name_extension(_, Ext, F),
   96      user:prolog_file_type(Ext, prolog)
   97    },
   98    !,
   99    [F].
  100prolog_source_files(Dir) -->
  101    { exists_directory(Dir),
  102      !,
  103      atom_concat(Dir, '/*', Pattern),
  104      expand_file_name(Pattern, Members)
  105    },
  106    prolog_source_files(Members).
  107prolog_source_files(_) -->
  108    [].
 apply_hotfix(+HotfixDir, +File) is det
Locate the hotfix and load it if it is newer. First step to find the file we must replace is using the module name, as these are guaranteed to be unique in the Prolog process. If that fails, we use the filename, but now we can get multiple files with the same name loaded from different directories as candidates. Finally, if no file matches, we load the file into the user module.
  121apply_hotfix(_HotfixDir, File) :-
  122    file_module(File, Module),
  123    module_property(Module, file(Loaded)),
  124    '$time_source_file'(Loaded, Time, _Type),
  125    !,
  126    time_file(File, HotfixTime),
  127    (   HotfixTime =\= Time
  128    ->  load_hotfix(File, Loaded)
  129    ;   true
  130    ).
  131apply_hotfix(HotfixDir, File) :-
  132    atom_concat(HotfixDir, Local, File),
  133    atom_concat(/, Local, SlashLocal),
  134    findall(Loaded-Time,
  135            (   '$time_source_file'(Loaded, Time, user),
  136                sub_atom(Loaded, _, _, 0, SlashLocal)
  137            ),
  138            Pairs),
  139    Pairs \== [],
  140    !,
  141    (   Pairs = [Loaded-Time]
  142    ->  true
  143    ;   select_file_to_reload(Pairs, Local, Loaded-Time)
  144    ),
  145    time_file(File, HotfixTime),
  146    (   HotfixTime =\= Time
  147    ->  load_hotfix(File, Loaded)
  148    ;   true
  149    ).
  150apply_hotfix(_HotfixDir, File) :-
  151    user:consult(File).
 ensure_dirsep(+Dir, -DirSlash) is det
  156ensure_dirsep(Dir0, Dir) :-
  157    (   sub_atom(Dir0, _, _, 0, /)
  158    ->  Dir = Dir0
  159    ;   atom_concat(Dir0, /, Dir)
  160    ).
 load_hotfix(+HotfixFile, +Loaded) is det
Reload the HotfixFile, pretending we are reloading Loaded.
See also
- make:reload_file/1
  169load_hotfix(File, Loaded) :-
  170    time_file(File, Modified),
  171    setup_call_cleanup(
  172        open(File, read, In),
  173        load_hotfix_from_stream(Loaded, In, Modified),
  174        close(In)).
  175
  176load_hotfix_from_stream(Loaded, In, Modified) :-
  177    FixOptions = [ stream(In),
  178                   modified(Modified),
  179                   register(false)
  180                 ],
  181    set_stream(In, file_name(Loaded)),
  182    findall(M-Opts,
  183            source_file_property(Loaded, load_context(M, _, Opts)),
  184            Modules),
  185    (   Modules = [First-OptsFirst|Rest]
  186    ->  merge_options(FixOptions, OptsFirst, FirstOptions),
  187        load_stream(First:Loaded, FirstOptions),
  188        forall(member(Context-Opts, Rest),
  189               ( merge_options([if(not_loaded)|FirstOptions], Opts, ORest),
  190                 load_stream(Context:Loaded, ORest)
  191               ))
  192    ;   load_stream(user:Loaded, FixOptions)
  193    ).
  194
  195load_stream(Source, Options) :-
  196    option(stream(In), Options),
  197    setup_call_cleanup(
  198        stream_property(In, position(Pos)),
  199        load_files(Source, Options),
  200        set_stream_position(In, Pos)).
 select_file_to_reload(+Pairs, +Local, -Pair) is det
  204select_file_to_reload(Pairs, Local, Pair) :-
  205    format(user_error,
  206           'Hotfix ~w matches multiple loaded files.~n~n',
  207           [Local]),
  208    forall(nth1(I, Pairs, File-_),
  209           format(user_error, '~t~d~6| ~w~n', [I, File])),
  210    repeat,
  211       format(user_error, '~nPlease select (\'s\' skips hotfix)? ', []),
  212       read_line_to_codes(user_input, Line),
  213       (   Line == end_of_file
  214       ->  halt(1)
  215       ;   atom_codes(s, Line)
  216       ->  !, fail
  217       ;   catch(number_codes(N, Line), _, fail)
  218       ),
  219       nth1(N, Pairs, Pair),
  220    !.
 file_module(+File, -Module) is semidet
True if Module is the module defined in File.
  226file_module(File, Module) :-
  227    catch(file_module_guarded(File, Module), _, fail).
  228
  229file_module_guarded(File, Module) :-
  230    setup_call_cleanup(
  231        prolog_open_source(File, In),
  232        prolog_read_source_term(In, _, Expanded, []),
  233        prolog_close_source(In)),
  234    Expanded = (:- module(Module, _))