source: release-kits/shared/linux/wrapper.cpp@ 19027

Last change on this file since 19027 was 19027, checked in by oranfry, 15 years ago

use the official tmp directory so the installer works off non writable media

File size: 4.5 KB
Line 
1#include <iostream>
2#include <fstream>
3#include <iomanip>
4#include <cstdlib>
5
6using namespace std;
7
8#include "wrapper.h"
9#include "libsearch4j.h"
10
11bool extract_bundled_file( const char[], int, char*, bool );
12
13int main(int argc, char** argv) {
14
15 string scratch = (getenv("TMPDIR") == NULL) ? "/tmp" : getenv("TMPDIR");
16 string tempdir = scratch + "/@[email protected]"; //temporary directory where we will store extracted files
17 string jarfile = tempdir + "/@[email protected]"; //where we will store the jar file
18 string javafile = tempdir + "/@java.installer@"; //where we will store the java tar file
19 bool succeeded = false;
20 bool textMode = false;
21
22 //parse arguments
23 for ( int i=0; i<argc; i++ ) {
24 if ( strcmp( argv[i], "text" ) == 0 ) {
25 textMode = true;
26 }
27 }
28
29
30 //create the temp folder
31 cout << "Creating temp directory..." << endl;
32 succeeded = ( 0 == system( ("mkdir " + tempdir).c_str() ) );
33 if ( !succeeded ) {
34 cout << "Failed to write to create the temp directory '" << tempdir << "'" << endl;
35 cout << "Check that it does not already exist and try again" << endl;
36 cout << "Exiting" << endl;
37 return 1;
38 }
39
40 //extract files
41 cout << "Extracting installer jar..." << endl;
42 succeeded = extract_bundled_file( @installer.name@jar, sizeof(@installer.name@jar), (char*)jarfile.c_str(), false);
43
44 #ifdef java_is_bundled
45 cout << "Preparing Greenstone installer..." << endl;
46 succeeded = extract_bundled_file( java, sizeof(java), (char*)javafile.c_str(), true ) && succeeded;
47 #endif
48
49 if ( !succeeded ) {
50 cout << "Failed to extract one or more resources" << endl;
51 cout << "This installer may not have sufficient file permissions to write it to disk" << endl;
52 cout << "Or, the files may be corrupt or missing from this executable" << endl;
53 cout << "Exiting" << endl;
54 return 1;
55 }
56
57 //change to the temp directory
58 chdir( tempdir.c_str() );
59
60 #ifdef java_is_bundled
61 succeeded = (system( "/bin/sh -c ./@java.installer@ > /dev/null" ) == 0);
62 succeeded = succeeded && ( system( "tar -xf jre.tar" ) == 0 );
63 if ( !succeeded ) {
64 cout << "Failed to extract the bundled java archive to the temp directory" << endl;
65 cout << "You need the tar program on your PATH" << endl;
66 cout << "Exiting" << endl;
67 return 1;
68 }
69 #endif
70
71 //check if an appropriate java is found
72 Jvm minimum; minimum.setVersionFromString( "@java.min.version@" );
73 string phint = "./@java.extracted@";
74 string hint = "";
75 Jvm foundJvm;
76 bool jvmFound = find( foundJvm, true, minimum, false, false, phint, hint, false );
77
78 //if the jvm was not found, report not found
79 if ( !jvmFound ) {
80
81 //did not find a good java
82 cout << "Greenstone requires java @java.min.version@ or greater." << endl;
83
84 //tell them if java is absent or just too old
85 Jvm tmpJvm;
86 if ( find( tmpJvm, false, minimum, false, false, phint, hint, false ) ) {
87 cout << "You have java, but it is too old." << endl;
88 } else {
89 cout << "Could not find java on your system." << endl;
90 }
91
92 cout << "Install java (@java.min.version@ or greater) and set JAVA_HOME or JRE_HOME, and try again" << endl;
93 #ifndef java_is_bundled
94 cout << "Or, download a greentsone3 installer with bundled java and use that instead of this one" << endl;
95 #endif
96
97 //if we have found it, launch the installer
98 } else {
99
100 cout << "Launching Installer ..." << endl;
101 int launch_exit_code = 0;
102 launch_exit_code = system( (foundJvm.getExecutable() + " -Xmx85M -jar @[email protected]" + (textMode?" text":"") ).c_str() );
103
104 //report how it went
105 if ( launch_exit_code == 0 ) {
106 cout << "Setup complete" << endl;
107 } else {
108 cout << "The installer exited with an error" << endl;
109 cout << "Greenstone may not be correctly installed" << endl;
110 }
111
112 }
113
114 //change to the scratch dir for the following operation
115 chdir(scratch);
116
117 //delete the temp files
118 cout << "Deleting the temp directory" << endl;
119 system( ("rm -rf " + tempdir).c_str() );
120
121 return 0;
122
123}
124
125bool extract_bundled_file( const char data[], int size, char* filename, bool make_executable) {
126
127 if ( size == 0 ) return false;
128
129 //delete the file if it exists
130 remove( filename );
131
132 //open the file
133 fstream binary_file( filename, ios::out|ios::binary );
134 if ( !binary_file.good() ) {
135 binary_file.close();
136 return false;
137 }
138
139 //write the file
140 binary_file.write( data, size );
141 if ( !binary_file.good() ) {
142 binary_file.close();
143 return false;
144 }
145
146 //close the file
147 binary_file.close();
148 if ( !binary_file.good() ) {
149 binary_file.close();
150 return false;
151 }
152
153 if ( make_executable ) {
154 string command = "chmod a+x " + string(filename);
155 system( command.c_str() );
156 }
157
158 return true;
159
160}
Note: See TracBrowser for help on using the repository browser.