2 //hmmm: anything related to _stub_size should be kept, but that is where
3 // we need a redundant search mechanism that can't be fooled so easily
4 // by modifying exe; make a pattern that will be found and is the first
5 // place to start looking for manifest.
7 /*****************************************************************************\
9 * Name : bundle_creator *
10 * Author : Chris Koeritz *
12 *******************************************************************************
13 * Copyright (c) 2006-$now By Author. This program is free software; you can *
14 * redistribute it and/or modify it under the terms of the GNU General Public *
15 * License as published by the Free Software Foundation; either version 2 of *
16 * the License or (at your option) any later version. This is online at: *
17 * http://www.fsf.org/copyleft/gpl.html *
18 * Please send any updates to: fred@gruntose.com *
19 \*****************************************************************************/
21 #include "common_bundle.h"
23 #include <application/hoople_main.h>
24 #include <application/command_line.h>
25 #include <basis/array.h>
26 #include <basis/byte_array.h>
27 #include <basis/environment.h>
28 #include <configuration/application_configuration.h>
29 #include <configuration/ini_configurator.h>
30 #include <configuration/variable_tokenizer.h>
31 #include <filesystem/byte_filer.h>
32 #include <filesystem/directory.h>
33 #include <filesystem/filename.h>
34 #include <filesystem/file_time.h>
35 #include <loggers/console_logger.h>
36 #include <loggers/file_logger.h>
37 #include <processes/launch_process.h>
38 #include <structures/static_memory_gremlin.h>
39 #include <structures/string_table.h>
40 #include <textual/byte_formatter.h>
41 #include <textual/list_parsing.h>
42 #include <textual/parser_bits.h>
43 #include <timely/time_stamp.h>
52 using namespace application;
53 using namespace basis;
54 using namespace configuration;
55 using namespace filesystem;
56 using namespace loggers;
57 using namespace filesystem;
58 using namespace processes;
59 using namespace structures;
60 using namespace textual;
61 using namespace timely;
63 const int CHUNKING_SIZE = 256 * KILOBYTE;
64 // we'll read this big a chunk from a source file at a time.
66 const astring SUBVERSION_FOLDER = ".svn";
67 // we don't want to include this in a bundle.
69 #define BASE_LOG(to_print) program_wide_logger::get().log(to_print, ALWAYS_PRINT)
70 #define LOG(to_print) CLASS_EMERGENCY_LOG(program_wide_logger::get(), to_print)
72 //#define DEBUG_BUNDLER
73 // uncomment for noisy debugging version.
75 // returns the "retval" and mentions that this is a failure at "where".
76 #define FAIL_RETURN(retval, where) { \
77 LOG(astring("failure in ") + where + a_sprintf(", exit=%d", retval)); \
81 ////////////////////////////////////////////////////////////////////////////
83 bool true_value(const astring &value)
84 { return (!value.equal_to("0")) && (!value.equal_to("false")); }
86 ////////////////////////////////////////////////////////////////////////////
88 // this structure overrides the manifest_chunk by providing a source string.
90 struct bundled_chunk : manifest_chunk
92 astring _source; //!< where the file comes from on the source system.
93 virtual ~bundled_chunk() {}
96 ////////////////////////////////////////////////////////////////////////////
98 // main bundler class.
100 class bundle_creator : public application_shell
104 : application_shell(),
105 _app_name(filename(_global_argv[0]).basename()),
106 _bundle(NIL), _stub_size(0), _keyword() {}
108 virtual ~bundle_creator() {
112 DEFINE_CLASS_NAME("bundle_creator");
113 virtual int execute();
114 int print_instructions();
116 astring determine_stub_file_and_validate();
117 //!< returns the stub file location if it could be successfully located.
119 int open_output_file();
120 //!< prepares the output file to be written into.
121 /*!< non-zero return indicates an error. */
124 //!< reads manifest definition specifying files in the bundle.
125 /*!< creates the list of bundle pieces. */
127 int write_stub_and_toc();
128 //!< stuffs the unpacker stub into output file and table of contents.
130 int bundle_sources();
131 //!< reads all of the input files and dumps them into the bundle.
134 //!< puts finishing touches on the output file and closes it.
137 //!< writes the offset position into the output file.
138 /*!< this happens at the specially marked location (muftiloc). */
140 int patch_recursive_target(const astring &source, const astring &target,
142 //!< processes the recursive target specified in "curr".
143 /*!< the manifest_index tells the function where the external caller
144 is currently working on the manifest. new items will appear just after
147 int recurse_into_dir(const astring &source, const astring &target,
149 //!< adds all files from "source" to our list, recurses on dirs.
151 int patch_wildcard_target(const astring &source, const astring &target,
153 //!< processes the wildcard bearing target specified in "curr".
154 /*!< any new source items will get dropped on the end of the manifest. */
156 int add_files_here(directory &dirndl, const astring &source,
157 const astring &target, int manifest_index);
158 //!< takes all the files found in "source" and adds them to manifest.
160 bool get_file_size(const astring &file, un_int &size, byte_array ×tamp);
161 //!< returns the file "size" and "timestamp" found for "file".
164 astring _app_name; //!< application name for this program.
165 astring _output_file; //!< what bundle file to create.
166 astring _manifest_file; //!< the manifest of what's included in bundle.
167 array<bundled_chunk> _manifest_list; //!< the parsed list of contents.
168 byte_filer *_bundle; //!< points at the bundled output file.
169 int _stub_size; //!< where the TOC will be located.
170 astring _keyword; // set if we were given a keyword on cmd line.
173 ////////////////////////////////////////////////////////////////////////////
175 int bundle_creator::print_instructions()
177 BASE_LOG(a_sprintf("\
178 %s: This program needs two parameters on the command line.\n\
179 The -o flag must point at the bundled output file to create. The -m flag\n\
180 must point at a valid manifest file that defines what will be packed into\n\
181 the output file. See the example manifest in the bundler example\n\
182 (in setup_src/bundle_example) for more information on the required file\n\
188 int bundle_creator::execute()
192 BASE_LOG(astring("starting file bundling at ") + time_stamp::notarize(false));
194 command_line cmds(_global_argc, _global_argv);
196 if (cmds.get_value('?', temp)) return print_instructions();
197 if (cmds.get_value("?", temp)) return print_instructions();
198 if (!cmds.get_value('o', _output_file)) return print_instructions();
199 if (!cmds.get_value('m', _manifest_file)) return print_instructions();
201 if (filename(_output_file).exists()) {
202 BASE_LOG(a_sprintf("\
203 %s: The output file already exists. Please move it out of\n\
204 the way; this program will not overwrite existing files.\n",
209 if (!filename(_manifest_file).exists()) {
210 BASE_LOG(a_sprintf("\
211 %s: The manifest file does not exist. This program cannot do anything\n\
212 without a valid packing manifest.\n", _app_name.s()));
216 // test this early on so we don't waste time uselessly.
217 astring stub_file_okay = determine_stub_file_and_validate();
218 if (!stub_file_okay) {
219 BASE_LOG(a_sprintf("\
220 %s: The unpacking stub file does not exist (check binaries folder).\n\
221 Abandoning bundling process.\n", _app_name.s()));
225 // make sure we snag any keyword that was passed on the command line.
226 cmds.get_value("keyword", _keyword);
228 // first step is to provide some built-in variables that can be used to
229 // make the manifests less platform specific. this doesn't really help
230 // if you bundle it on linux and try to run it on windows. but either
231 // platform's resources can easily be made into a bundle with the same
234 environment::set("EXE_END", ""); // executable file ending.
235 environment::set("DLL_START", "lib"); // dll file prefix.
236 environment::set("DLL_END", ".so"); // dll file ending.
238 environment::set("EXE_END", ".exe");
239 environment::set("DLL_START", "");
240 environment::set("DLL_END", ".dll");
244 if ( (ret = read_manifest()) ) FAIL_RETURN(ret, "reading manifest");
245 // read manifest to build list of what's what.
246 if ( (ret = open_output_file()) ) FAIL_RETURN(ret, "opening output file");
247 // open up our output file for the bundled chunks.
248 if ( (ret = write_stub_and_toc()) ) FAIL_RETURN(ret, "writing stub and TOC");
249 // writes the stub unpacker application and the table of contents to the
251 if ( (ret = bundle_sources()) ) FAIL_RETURN(ret, "bundling source files");
252 // stuff all the source files into the output bundle.
253 if ( (ret = finalize_file()) ) FAIL_RETURN(ret, "finalizing file");
254 // finishes with the file and closes it up.
255 if ( (ret = write_offset()) ) FAIL_RETURN(ret, "writing offset");
256 // stores the offset of the TOC into the output file in a special location
257 // that is delineated by a known keyword (muftiloc) and which should only
258 // exist in the file in one location.
263 int bundle_creator::open_output_file()
265 FUNCDEF("open_output_file");
266 _bundle = new byte_filer(_output_file, "wb");
267 if (!_bundle->good()) {
268 LOG(astring("failed to open the output file: ") + _output_file);
274 bool bundle_creator::get_file_size(const astring &infile, un_int &size,
275 byte_array &time_stamp)
277 FUNCDEF("get_file_size");
279 // access the source file to get its size.
280 byte_filer source_file(infile, "rb");
281 if (!source_file.good()) {
282 LOG(astring("could not access the file for size check: ") + infile);
285 size = int(source_file.length());
286 file_time tim(infile);
287 tim.pack(time_stamp);
291 int bundle_creator::add_files_here(directory &dirndl, const astring &source,
292 const astring &target, int manifest_index)
294 FUNCDEF("add_files_here");
295 for (int i = 0; i < dirndl.files().length(); i++) {
296 astring curry = dirndl.files()[i];
297 // skip .svn folders and contents.
298 if (curry.contains(SUBVERSION_FOLDER)) continue;
299 //hmmm: this could be a much nicer generalized file exclusion list.
301 //LOG(astring("file is: ") + curry);
302 bundled_chunk new_guy;
303 new_guy._source = source + "/" + curry; // the original full path to it.
304 new_guy._payload = target + "/" + curry;
305 new_guy._keywords = _manifest_list[manifest_index]._keywords;
306 // copy the flags from the parent, so we don't forget options.
307 new_guy._flags = _manifest_list[manifest_index]._flags;
308 // remove some flags that make no sense for the new guy.
309 new_guy._flags &= ~RECURSIVE_SRC;
311 //LOG(a_sprintf("adding: source=%s targ=%s", new_guy._source.s(), new_guy._payload.s()));
312 bool okaysize = get_file_size(new_guy._source, new_guy._size, new_guy.c_filetime);
313 if (!okaysize || (new_guy._size < 0) ) {
314 LOG(astring("failed to get file size for ") + new_guy._source);
318 _manifest_list.insert(manifest_index + 1, 1);
319 _manifest_list[manifest_index + 1] = new_guy;
324 int bundle_creator::recurse_into_dir(const astring &source,
325 const astring &target, int manifest_index)
327 // FUNCDEF("recurse_into_dir");
328 //LOG(astring("src=") + source + " dest=" + target);
330 // we won't include the subversion folder.
331 if (source.contains(SUBVERSION_FOLDER)) return 0;
333 string_array dirs; // culled from the directory listing.
335 // don't pay for the directory object on the recursive invocation stack;
336 // just have what we need on the stack (the directory list).
337 directory dirndl(source);
338 //check dir for goodness!
339 int ret = add_files_here(dirndl, source, target, manifest_index);
340 // add in just the files that were found.
342 // this is a failure, but the function complains about it already.
345 dirs = dirndl.directories();
348 //LOG("now scanning directories...");
350 // now scan across the directories we found.
351 for (int i = 0; i < dirs.length(); i++) {
353 //LOG(astring("curr dir is ") + s);
354 int ret = recurse_into_dir(source + "/" + s, target + "/"
355 + s, manifest_index);
356 if (ret != 0) return ret; // bail out.
362 int bundle_creator::patch_recursive_target(const astring &source,
363 const astring &target, int manifest_index)
365 // FUNCDEF("patch_recursive_target");
366 //LOG(astring("patch recurs src=") + source + " targ=" + target);
367 return recurse_into_dir(source, target, manifest_index);
370 int bundle_creator::patch_wildcard_target(const astring &source,
371 const astring &target, int manifest_index)
373 // FUNCDEF("patch_wildcard_target");
374 // find the last slash. the rest is our wildcard component.
375 int src_end = source.end();
376 int slash_indy = source.find('/', src_end, true);
377 astring real_source = source.substring(0, slash_indy - 1);
378 astring wild_pat = source.substring(slash_indy + 1, src_end);
379 //BASE_LOG(astring("got src=") + real_source + " wildpat=" + wild_pat);
381 directory dirndl(real_source, wild_pat.s());
382 //check dir for goodness!
383 int ret = add_files_here(dirndl, real_source, target, manifest_index);
385 // this is a failure, but the function complains about it already.
392 int bundle_creator::read_manifest()
394 FUNCDEF("read_manifest");
395 ini_configurator ini(_manifest_file, configurator::RETURN_ONLY);
397 bool worked = ini.get_section("toc", toc);
399 LOG(astring("failed to read TOC section in manifest:\n") + _manifest_file
400 + "\ndoes that file exist?");
404 //hmmm: make a class member.
405 file_logger noisy_logfile(application_configuration::make_logfile_name
406 ("bundle_creator_activity.log"));
407 noisy_logfile.log(astring('-', 76));
408 noisy_logfile.log(astring("Bundling starts at ") + time_stamp::notarize(false));
410 // add enough items in the list for our number of sections.
411 _manifest_list.insert(0, toc.symbols());
412 astring value; // temporary string used below.
413 int final_return = 0; // if non-zero, an error occurred.
415 #define BAIL(retval) \
416 final_return = retval; \
418 _manifest_list.zap(i, i); \
422 for (int i = 0; i < toc.symbols(); i++) {
423 // read all the info in this section and store it into our list.
424 astring section_name = toc.name(i);
425 section_name.strip_spaces(astring::FROM_FRONT);
426 if (section_name[0] == '#') {
427 //hmmm: this looks a bit familiar from bail macro above. abstract out?
429 _manifest_list.zap(i, i);
431 continue; // skip comments.
434 // check for any keywords on the section. these are still needed for
435 // variables, which otherwise would skip the rest of the field checks.
436 if (ini.get(section_name, "keyword", value)) {
437 ///LOG(astring("into keyword processing--value held is ") + value);
439 bool worked = list_parsing::parse_csv_line(value, keys);
441 LOG(astring("failed to parse keywords for section ")
442 + section_name + " in " + _manifest_file);
445 ///LOG(astring("parsed list is ") + keys.text_form());
446 _manifest_list[i]._keywords = keys;
448 list_parsing::create_csv_line(_manifest_list[i]._keywords, dumped);
449 noisy_logfile.log(section_name + " keywords: " + dumped);
452 if (ini.get(section_name, "variable", value)) {
453 // this is a variable assignment. it is the only thing we care about
454 // for this section, so the rest is ignored.
455 variable_tokenizer zohre;
457 if (zohre.symbols() < 1) {
458 LOG(astring("failed to parse a variable statement from ") + value);
461 _manifest_list[i]._flags = SET_VARIABLE; // not orred, just this.
462 // set the two parts of our variable.
463 _manifest_list[i]._payload = zohre.table().name(0);
464 _manifest_list[i]._parms = zohre.table()[0];
465 BASE_LOG(astring("will set ") + _manifest_list[i]._payload + " = "
466 + _manifest_list[i]._parms);
467 astring new_value = parser_bits::substitute_env_vars(_manifest_list[i]._parms);
468 environment::set(_manifest_list[i]._payload, new_value);
471 BASE_LOG(astring("** variable ") + _manifest_list[i]._payload + " should have value=" + new_value);
472 BASE_LOG(astring("** variable ") + _manifest_list[i]._payload + " now does have value=" + environment::get(_manifest_list[i]._payload));
476 } else if (ini.get(section_name, "assert_defined", value)) {
477 // they are just asking for a variable test, to see if a variable
478 // that the installer needs is actually defined at unpacking time.
479 _manifest_list[i]._payload = value;
480 _manifest_list[i]._flags = TEST_VARIABLE_DEFINED;
481 BASE_LOG(astring("will test ") + _manifest_list[i]._payload + " is "
482 + "defined at unpacking time.");
486 if (!ini.get(section_name, "source", _manifest_list[i]._source)) {
487 // check whether they told us not to pack and it's executable.
488 bool okay_to_omit_source = false;
490 if (ini.get(section_name, "no_pack", value)
491 && ini.get(section_name, "exec_target", value2) ) {
492 if (true_value(value) && true_value(value2)) {
493 // this type of section doesn't need source declared.
494 okay_to_omit_source = true;
497 if (!okay_to_omit_source) {
498 LOG(astring("failed to read the source entry for section ")
499 + section_name + " in " + _manifest_file);
503 // fix meshugener backslashes so we can count on the slash direction.
504 _manifest_list[i]._source.replace_all('\\', '/');
506 if (!ini.get(section_name, "target", _manifest_list[i]._payload)) {
507 // check whether they told us not to pack and it's executable.
508 bool okay_to_omit_target = false;
510 if (ini.get(section_name, "no_pack", value)
511 && ini.get(section_name, "exec_source", value2) ) {
512 if (true_value(value) && true_value(value2)) {
513 // this type of section doesn't need target declared.
514 okay_to_omit_target = true;
517 if (!okay_to_omit_target) {
518 LOG(astring("failed to read the target entry for section ")
519 + section_name + " in " + _manifest_file);
523 // fix backslashes in target also.
524 _manifest_list[i]._payload.replace_all('\\', '/');
526 // capture any parameters they have specified for exec or other options.
527 if (ini.get(section_name, "parms", value)) {
528 _manifest_list[i]._parms = value;
530 BASE_LOG(astring("got parms for ") + section_name + " as: " + value);
532 if (value[0] != '"') {
533 // repair the string if we're running on windows.
534 _manifest_list[i]._parms = astring("\"") + value + "\"";
536 noisy_logfile.log(section_name + " parms: " + _manifest_list[i]._parms);
539 // check for the ignore errors flag.
540 if (ini.get(section_name, "error_okay", value)) {
541 if (true_value(value))
542 _manifest_list[i]._flags |= IGNORE_ERRORS;
545 // see if they are saying not to overwrite the target file.
546 if (ini.get(section_name, "no_replace", value)) {
547 if (true_value(value))
548 _manifest_list[i]._flags |= NO_OVERWRITE;
551 // test whether they are saying not to complain about a failure with
552 // our normal pop-up dialog (on winders).
553 if (ini.get(section_name, "quiet", value)) {
554 if (true_value(value))
555 _manifest_list[i]._flags |= QUIET_FAILURE;
558 // did they want a backup of the original to be made, instead of
559 // just overwriting the file?
560 if (ini.get(section_name, "make_backup", value)) {
561 if (true_value(value))
562 _manifest_list[i]._flags |= MAKE_BACKUP_FILE;
565 // look for our recursion flag.
566 if (ini.get(section_name, "recurse", value)) {
567 if (true_value(value))
568 _manifest_list[i]._flags |= RECURSIVE_SRC;
570 // the options here are only appropriate when the target is NOT set to
573 if (ini.get(section_name, "no_pack", value)) {
574 // allow either side to not be required if this is an executable.
575 if (true_value(value))
576 _manifest_list[i]._flags |= OMIT_PACKING;
579 // check if they have specified a source side executable.
580 if (ini.get(section_name, "exec_source", value)) {
581 if (true_value(value)) {
582 _manifest_list[i]._flags |= SOURCE_EXECUTE;
585 // check if they have specified a target side executable. this is
586 // mutually exclusive with a source side exec.
587 if (ini.get(section_name, "exec_target", value)) {
588 if (true_value(value))
589 _manifest_list[i]._flags |= TARGET_EXECUTE;
594 // replace environment variables in the source now...
595 _manifest_list[i]._source = parser_bits::substitute_env_vars
596 (_manifest_list[i]._source, false);
598 // look for wildcards in the source.
599 int indy = _manifest_list[i]._source.find("*");
601 // see if they specified a keyword on the command line and if this matches.
602 // if not we need to abandon this item.
603 if (!!_keyword && !_manifest_list[i]._keywords.member(_keyword)) {
604 // their keyword choice didn't match what we were told to use.
605 noisy_logfile.log(astring("skipping ") + _manifest_list[i]._payload
606 + " file check; doesn't match keyword \"" + _keyword + "\"");
610 // we only access the source file here if it's finalized. we can't do
611 // this if the target is supposed to be recursive or if it's got a wildcard
613 if (!(_manifest_list[i]._flags & RECURSIVE_SRC) && negative(indy)
614 && !(_manifest_list[i]._flags & OMIT_PACKING) ) {
615 // access the source file to get its size.
616 byte_filer source_file(_manifest_list[i]._source, "rb");
617 if (!source_file.good()) {
618 LOG(astring("could not access the source file for bundling: ")
619 + _manifest_list[i]._source);
622 bool okaysize = get_file_size(_manifest_list[i]._source,
623 _manifest_list[i]._size, _manifest_list[i].c_filetime);
624 if (!okaysize || (_manifest_list[i]._size < 0) ) {
625 // this is a failure, but the function complains about it already.
631 // patch the manifest list for wildcards and recursive sources.
632 for (int i = 0; i < _manifest_list.length(); i++) {
633 bundled_chunk curr = _manifest_list[i];
635 if (!!_keyword && !curr._keywords.member(_keyword)) {
636 // this item's keyword doesn't match the one we were given, so skip it.
637 noisy_logfile.log(astring("zapping entry for ") + curr._payload
638 + "; doesn't match keyword \"" + _keyword + "\"");
639 _manifest_list.zap(i, i);
640 i--; // skip back since we eliminated an index.
644 if (curr._flags & SET_VARIABLE) {
645 // we're done working on this.
647 } else if (curr._flags & TEST_VARIABLE_DEFINED) {
648 // this also requires no further effort.
650 } else if (curr._flags & RECURSIVE_SRC) {
651 // handle a recursive style target.
652 int star_indy = curr._source.find("*");
653 if (non_negative(star_indy)) {
654 // this is currently illegal. we don't allow recursion + wildcards.
655 LOG(astring("illegal combination of recursion and wildcard: ")
659 // handle the recursive guy.
660 int ret = patch_recursive_target(curr._source, curr._payload, i);
662 LOG(astring("failed during packing of recursive source: ")
666 // take this item out of the picture, since all contents got included.
667 _manifest_list.zap(i, i);
668 i--; // skip back since we eliminated an index.
670 } else if (curr._flags & SOURCE_EXECUTE) {
671 // we have massaged the current manifest chunk as much as we can, so now
672 // we will execute the source item if that was specified.
673 BASE_LOG(astring("launching ") + curr._source);
675 curr._parms = parser_bits::substitute_env_vars(curr._parms, false);
676 BASE_LOG(astring("\tparameters ") + curr._parms);
678 BASE_LOG(astring('-', 76));
680 basis::un_int retval = launch_process::run(curr._source, curr._parms,
681 launch_process::AWAIT_APP_EXIT, kid);
683 LOG(astring("failed to launch process, source=") + curr._source
684 + ", with parms " + curr._parms);
685 if (! (curr._flags & IGNORE_ERRORS) ) {
689 BASE_LOG(astring('-', 76));
690 if (curr._flags & OMIT_PACKING) {
691 // this one shouldn't be included in the package.
692 _manifest_list.zap(i, i);
693 i--; // skip back since we eliminated an index.
697 // check for a wildcard.
698 int star_indy = curr._source.find("*");
699 if (negative(star_indy)) continue; // simple targets are boring.
700 // this does have a wildcard in it. let's make sure it's in the right
701 // place for a wildcard in our scheme.
702 int slash_indy = curr._source.find('/', curr._source.end(), true);
703 if (star_indy < slash_indy) {
704 BASE_LOG(astring("illegal wildcard placement in ") + curr._source);
705 BASE_LOG(astring(" (the wildcard must be in the last component of the path)"));
708 // handle the wildcarded source.
709 int ret = patch_wildcard_target(curr._source, curr._payload, i);
711 LOG(astring("failed during packing of wildcarded source: ")
715 _manifest_list.zap(i, i);
716 i--; // skip back since we eliminated an index.
723 // we had a successful run so we can print this stuff out.
724 LOG("read the following info from manifest:");
725 for (int i = 0; i < _manifest_list.length(); i++) {
726 bundled_chunk &curr = _manifest_list[i];
727 BASE_LOG(a_sprintf("(%d) size %d, %s => %s", i, curr._size,
728 curr._source.s(), curr._payload.s()));
736 astring bundle_creator::determine_stub_file_and_validate()
738 FUNCDEF("determine_stub_file_and_validate");
739 // define our location to find the unpacking stub program.
740 //hmmm: make this a command line parameter.
742 astring stub_filename("unpacker_stub");
745 astring stub_filename("unpacker_stub.exe");
747 astring repo_dir = "$PRODUCTION_DIR";
748 astring stub_file = parser_bits::substitute_env_vars
749 (repo_dir + "/binaries/" + stub_filename, false);
750 if (!filename(stub_file).exists()) {
751 // we needed to find that to build the bundle.
752 LOG(astring("could not find unpacking stub file at: ") + stub_file);
753 return astring::empty_string();
758 int bundle_creator::write_stub_and_toc()
760 FUNCDEF("write_stub_and_toc");
762 astring stub_file = determine_stub_file_and_validate();
763 if (!stub_file) return 1;
765 // make sure the stub is accessible.
766 byte_filer stubby(stub_file, "rb");
767 if (!stubby.good()) {
768 FAIL_RETURN(80, astring("could not read the unpacking stub at: ") + stub_file);
770 _stub_size = int(stubby.length()); // get the stub size for later reference.
771 byte_array whole_stub;
772 stubby.read(whole_stub, _stub_size + 100);
774 _bundle->write(whole_stub);
776 byte_array packed_toc_len;
777 structures::obscure_attach(packed_toc_len, _manifest_list.length());
778 int ret = _bundle->write(packed_toc_len);
780 LOG(astring("could not write the TOC length to the bundle: ")
785 // dump out the manifest list in our defined format.
786 for (int i = 0; i < _manifest_list.length(); i++) {
787 bundled_chunk &curr = _manifest_list[i];
788 //LOG(a_sprintf("flag %d is %d", i, curr._flags));
791 if (_bundle->write(chunk) <= 0) {
792 LOG(a_sprintf("could not write item #%d [%s] to the bundle: ", i,
802 int bundle_creator::bundle_sources()
804 FUNCDEF("bundle_sources");
805 // go through all the source files and append them to the bundled output.
806 file_logger noisy_logfile(application_configuration::make_logfile_name
807 ("bundle_creator_activity.log"));
808 for (int i = 0; i < _manifest_list.length(); i++) {
809 bundled_chunk &curr = _manifest_list[i];
811 if (curr._flags & SET_VARIABLE) {
812 // all we need to do is keep this in the manifest.
813 noisy_logfile.log(astring("bundling: variable setting ") + curr._payload
814 + "=" + curr._parms);
816 } else if (curr._flags & TEST_VARIABLE_DEFINED) {
817 // just remember to test this when running the unpack.
818 noisy_logfile.log(astring("bundling: test variable ") + curr._payload
821 } else if (curr._flags & OMIT_PACKING) {
822 // this one shouldn't be included in the package.
826 noisy_logfile.log(astring("bundling: ") + curr._source);
827 byte_filer source(curr._source, "rb");
828 if (!source.good()) {
829 LOG(a_sprintf("could not read item #%d for the bundle: \"", i)
830 + curr._source + "\"");
834 byte_array compressed(256 * KILOBYTE); // expand the buffer to start with.
835 byte_array temp; // temporary read buffer.
837 // chew on the file a chunk at a time. this allows us to easily handle
838 // arbitrarily large files rather than reading their entirety into memory.
839 int total_written = 0;
841 int ret = source.read(temp, CHUNKING_SIZE);
843 LOG(a_sprintf("failed while reading item #%d: ", i) + curr._source);
846 total_written += ret; // add in what we expect to write.
847 // skip compressing if there's no data.
849 bool null_chunk = false;
854 compressed.reset(int(0.1 * ret) + ret + KILOBYTE);
855 // provide some extra space as per zlib instructions. we're giving it
856 // way more than they request.
857 destlen = compressed.length();
858 // pack the chunks first so we can know sizes needed.
859 int comp_ret = compress(compressed.access(), &destlen, temp.observe(),
861 if (comp_ret != Z_OK) {
862 LOG(a_sprintf("failed while compressing item #%d: ", i)
866 compressed.zap(destlen, compressed.length() - 1);
868 byte_array just_sizes;
869 structures::obscure_attach(just_sizes, temp.length());
870 // add in the real size.
871 structures::obscure_attach(just_sizes, int(destlen));
872 // add in the packed size.
873 ret = _bundle->write(just_sizes);
875 LOG(a_sprintf("failed while writing sizes for item #%d: ", i)
880 ret = _bundle->write(compressed);
882 LOG(a_sprintf("failed while writing item #%d: ", i) + curr._source);
884 } else if (ret != compressed.length()) {
885 LOG(a_sprintf("wrote different size for item #%d (tried %d, "
886 "wrote %d): ", i, compressed.length(), ret) + curr._source);
890 } while (!source.eof());
891 //hmmm: very common code to above size writing.
892 byte_array just_sizes;
893 structures::obscure_attach(just_sizes, -1);
894 structures::obscure_attach(just_sizes, -1);
895 int ret = _bundle->write(just_sizes);
897 LOG(a_sprintf("failed while writing sentinel of item #%d: ", i)
902 if (total_written != curr._size) {
903 LOG(a_sprintf("size (%d) disagrees with initial size (%d) for "
904 "item #%d: ", total_written, curr._size, i) + curr._source);
907 noisy_logfile.log(astring("Bundling run ends at ") + time_stamp::notarize(false));
908 noisy_logfile.log(astring('-', 76));
913 int bundle_creator::finalize_file()
919 int bundle_creator::write_offset()
921 // FUNCDEF("write_offset");
922 byte_filer bun(_output_file, "r+b"); // open the file for updating.
924 astring magic_string("muftiloc"); // our sentinel string.
925 astring temp_string; // data from the file.
928 // find the telltale text in the file.
929 bool found_it = false; // we'll set this to true if we see the string.
930 int location = 0; // where the sentinel's end is.
931 for (int i = 0; i < magic_string.length(); i++) {
932 int ret = bun.read(temp_string, 1);
934 if (temp_string[0] != magic_string[i]) break; // no match.
935 if (i == magic_string.end()) {
936 // we found a match to our string!
938 location = int(bun.tell());
939 //LOG(a_sprintf("found the sentinel in the file! posn=%d", location));
942 if (!found_it) continue; // keep reading.
944 byte_array packed_offset;
945 structures::obscure_attach(packed_offset, _stub_size);
946 //LOG(astring("pattern of len is:\n") + byte_format::text_dump(packed_offset));
947 // write the offset into the current position, which should be just after
948 // the sentinel's location.
949 bun.write(packed_offset);
950 //LOG(a_sprintf("wrote manifest offset before posn=%d", bun.tell()));
951 break; // done with looking for that pattern.
953 bun.close(); // completely finished now.
955 chmod(_output_file.s(), 0766);
956 // make sure it's an executable file when we're done with it.
958 BASE_LOG(astring("done file bundling at ") + time_stamp::notarize(false));
963 ////////////////////////////////////////////////////////////////////////////
965 HOOPLE_MAIN(bundle_creator, )
967 #ifdef __BUILD_STATIC_APPLICATION__
968 // static dependencies found by buildor_gen_deps.sh:
969 #include <application/application_shell.cpp>
970 #include <application/command_line.cpp>
971 #include <basis/astring.cpp>
972 #include <basis/common_outcomes.cpp>
973 #include <basis/environment.cpp>
974 #include <basis/mutex.cpp>
975 #include <basis/utf_conversion.cpp>
976 #include <configuration/application_configuration.cpp>
977 #include <configuration/configurator.cpp>
978 #include <configuration/ini_configurator.cpp>
979 #include <configuration/ini_parser.cpp>
980 #include <configuration/table_configurator.cpp>
981 #include <configuration/variable_tokenizer.cpp>
982 #include <filesystem/byte_filer.cpp>
983 #include <filesystem/directory.cpp>
984 #include <filesystem/filename.cpp>
985 #include <filesystem/file_time.cpp>
986 #include <loggers/combo_logger.cpp>
987 #include <loggers/console_logger.cpp>
988 #include <loggers/critical_events.cpp>
989 #include <loggers/file_logger.cpp>
990 #include <loggers/program_wide_logger.cpp>
991 #include <processes/launch_process.cpp>
992 #include <structures/bit_vector.cpp>
993 #include <structures/checksums.cpp>
994 #include <structures/object_packers.cpp>
995 #include <structures/static_memory_gremlin.cpp>
996 #include <structures/string_hasher.cpp>
997 #include <structures/string_table.cpp>
998 #include <structures/version_record.cpp>
999 #include <textual/byte_formatter.cpp>
1000 #include <textual/list_parsing.cpp>
1001 #include <textual/parser_bits.cpp>
1002 #include <textual/string_manipulation.cpp>
1003 #include <timely/earth_time.cpp>
1004 #include <timely/time_control.cpp>
1005 #include <timely/time_stamp.cpp>
1006 #endif // __BUILD_STATIC_APPLICATION__