source: main/trunk/release-kits/shared/linux/wrapper.cpp

Last change on this file was 35632, checked in by davidb, 3 years ago

Tidy up on treatment of 'string', 'char *', and 'const char*'. It was noticed that g++ was generating a warning message (warning: deprecated conversion from string constant to ‘char*’) on a call to extract_bundled_file() where a string literal was being passed as an argument. As this is C++ code, the string literal is treated as a 'const char*', however it was being passed to a function expecting a 'char *' mean the function could technically alter it if it wanted to (not easy to do -- danger, danger -- if you're a string literalfg). Of course our function doesn't do that, but the compiler isn't to know that. Simplest fix is to change our function to work with a 'const char*'. This also means some other lines of code in wrapper.cpp calling extract_bundled_file() could then loose the typecast they had for passing in a string as (char *)s.c_str()

File size: 8.9 KB
Line 
1#include <iostream>
2#include <fstream>
3#include <iomanip>
4#include <string.h>
5#include <cstdlib>
6#include <sstream>
7using namespace std;
8
9// The following provides the externs for chdir() and getcwd()
10#include <unistd.h>
11//extern int chdir(const char *path);
12//extern char *getcwd(char *buf, size_t size);
13
14#ifndef CDROM
15#include "wrapper.h"
16#include "libsearch4j.h"
17bool extract_bundled_file( const char[], int, const char*, bool );
18#else
19bool prep_java( string wd );
20#endif
21
22void usage( char* progname, int exitvalue = -1 ) {
23 cerr
24 << "Usage: " << progname << " [OPTIONS]" << endl
25 << "OPTIONS: " << endl
26 << " -h, -help Display this usage message" << endl
27 << " -textonly Do a non-graphical command line installation" << endl
28 << " -extract Extract the jar file, this can " << endl;
29 #ifdef CDROM
30 cerr << " -wd PATH Look for the CD-ROM at PATH" << endl;
31 #endif
32
33 exit( exitvalue );
34}
35
36int main(int argc, char** argv) {
37
38 char wdc[1024];
39 getcwd(wdc, sizeof(wdc));
40 string wd = "";
41 wd.append( wdc );
42
43 string scratch = (getenv("TMPDIR") == NULL) ? "/tmp" : getenv("TMPDIR");
44 string tempdir = scratch + "/greenstone-installer.tmp"; //temporary directory where we will store extracted files
45 bool succeeded = false;
46 bool text_mode = false;
47
48 //parse arguments
49 string a;
50 for ( int i=1; i<argc; i++ ) {
51 a = argv[i];
52 if ( a.compare("-wd") == 0 ) {
53 wd = "";
54 wd.append( argv[++i] );
55 } else if ( a.compare("-h") == 0 || a.compare("-help") == 0 || a.compare("--help") == 0) {
56 usage( argv[0], 0 );
57 } else if ( a.compare("-textonly") == 0 || a.compare("-text") == 0 || a.compare("text") == 0 ) {
58 //cerr << "-text is deprecated, please use -extract and then run \"java -jar greenstone.jar text\"" << endl;
59 text_mode = true;
60#ifndef CDROM
61 } else if ( a.compare("-extract") == 0 || a.compare("--extract") || a.compare("-x")) {
62 cout << "----------------------------" << endl;
63 cout << "Extracting java installer..." << endl;
64 cout << "----------------------------" << endl;
65 bool result = extract_bundled_file( greenstonejar, sizeof(greenstonejar), "greenstone.jar", false);
66 if(result){
67 cout << "\nExtraction Complete" << endl;
68 cout << "You can now run \"java -jar greenstone.jar text\" to run the installer from the command line\n" << endl;
69 return 0;
70 }
71 else{
72 cerr << "\nThere was an error while extracting the java installer" << endl;
73 return 1;
74 }
75#endif
76 } else {
77 cerr << "Unrecognised option '" << a << "'" << endl;
78 usage(argv[0]);
79 }
80 }
81
82 #ifdef CDROM
83 //ensure working directory is correct
84 FILE* pFile;
85 pFile = fopen( (wd+"/Software/core/all/uninst.jar").c_str(), "r");
86 if ( pFile == NULL ) {
87 cerr << "Could not find cdrom at '" << wd << "'." << endl;
88 cerr << "Please enter the path the CDROM [/media/cdrom]: ";
89 getline(cin, wd);
90 if ( wd.compare("") == 0 ) {
91 wd = "/media/cdrom";
92 }
93 } else {
94 fclose(pFile);
95 }
96
97 pFile = fopen( (wd+"/Software/core/all/uninst.jar").c_str(), "r");
98 if ( pFile == NULL ) {
99 cerr << "Could not find cdrom at '"<< wd << "'." << endl;
100 cerr << "Please run the installer again" << endl;
101 exit(-1);
102 } else {
103 fclose(pFile);
104 }
105 #endif
106
107 //create the temp folder
108 cout << "Creating temp directory..." << endl;
109 succeeded = ( 0 == system( ("mkdir " + tempdir).c_str() ) );
110 if ( !succeeded ) {
111 cerr << "Failed to create the temp directory '" << tempdir << "'. " <<
112 "Check that it does not already exist. Also check that you have write " <<
113 "permissions to '" << scratch << "', or set the environment variable TMPDIR to " <<
114 "a directory where you do have write permissions." << endl <<
115 "Exiting" << endl;
116 return 1;
117 }
118
119 #ifndef CDROM
120
121 string jarfile = tempdir + "/greenstone.jar"; //where we will store the jar file
122 string javafile = tempdir + "/@java.installer@"; //where we will store the java tar file
123
124
125 //extract files
126 cout << "Extracting installer jar..." << endl;
127 succeeded = extract_bundled_file( greenstonejar, sizeof(greenstonejar), jarfile.c_str(), false);
128
129 #ifdef java_is_bundled
130 cout << "Preparing Greenstone installer..." << endl;
131 succeeded = extract_bundled_file( java, sizeof(java), javafile.c_str(), true ) && succeeded;
132 #endif
133
134 if ( !succeeded ) {
135 cerr << "Failed to extract one or more resources" << endl;
136 cerr << "This installer may not have sufficient file permissions to write it to disk" << endl;
137 cerr << "Or, the files may be corrupt or missing from this executable" << endl;
138 cerr << "Exiting" << endl;
139 return 1;
140 }
141
142 #endif
143
144 //change to the temp directory
145 chdir( tempdir.c_str() );
146
147
148 #ifndef CDROM
149 #ifdef java_is_bundled
150 succeeded = (system( "/bin/sh -c ./@java.installer@ > /dev/null" ) == 0);
151 succeeded = succeeded && ( system( "tar -xf jre.tar" ) == 0 );
152 if ( !succeeded ) {
153 cout << "Failed to extract the bundled java archive to the temp directory" << endl;
154 cout << "You need the tar program on your PATH" << endl;
155 cout << "Exiting" << endl;
156 return 1;
157 }
158 #endif
159
160 //check if an appropriate java is found
161 Jvm minimum; minimum.setVersionFromString( "@java.min.version@" );
162 string phint = "./@java.extracted@";
163 string hint = "";
164 Jvm foundJvm;
165 bool jvmFound = find( foundJvm, true, minimum, false, false, phint, hint, false );
166
167 //if the jvm was not found, report not found
168 if ( !jvmFound ) {
169
170 //did not find a good java
171 cout << "Greenstone requires java @java.min.version@ or greater." << endl;
172
173 //tell them if java is absent or just too old
174 Jvm tmpJvm;
175 if ( find( tmpJvm, false, minimum, false, false, phint, hint, false ) ) {
176 cout << "You have java, but it is too old." << endl;
177 } else {
178 cout << "Could not find java on your system." << endl;
179 }
180
181 cout << "Install java (@java.min.version@ or greater) and set JAVA_HOME or JRE_HOME, and try again" << endl;
182 #ifndef java_is_bundled
183 cout << "Or, download a greentsone3 installer with bundled java and use that instead of this one" << endl;
184 #endif
185
186 //if we have found it, launch the installer
187 } else {
188
189 cout << "Launching Installer ..." << endl;
190 int launch_exit_code = 0;
191 launch_exit_code = system( (foundJvm.getExecutable() + " -Xmx200M -jar greenstone.jar" + (text_mode?" text":"") ).c_str() );
192
193 //report how it went
194 if ( launch_exit_code == 0 ) {
195 cout << "Setup complete" << endl;
196 } else {
197 cout << "The installer exited with an error" << endl;
198 cout << "Greenstone may not be correctly installed" << endl;
199 }
200
201 }
202 #else
203 bool java_ready = prep_java( wd );
204 if ( java_ready ) {
205 string cmd = "./jre/bin/java -Dorig.dir=\"" + wd + "\" -jar " + wd + "/Java/Jars/linux.jar" + (text_mode?" text":"");
206 system( cmd.c_str() );
207 }
208 #endif
209
210 //change to the scratch dir for the following operation
211 chdir(scratch.c_str());
212
213 //delete the temp files
214 cout << "Deleting the temp directory" << endl;
215 system( ("rm -rf " + tempdir).c_str() );
216
217 return 0;
218
219}
220#ifndef CDROM
221bool extract_bundled_file( const char data[], int size, const char* filename, bool make_executable) {
222
223 if ( size == 0 ) return false;
224
225 //delete the file if it exists
226 remove( filename );
227
228 //open the file
229 fstream binary_file( filename, ios::out|ios::binary );
230 if ( !binary_file.good() ) {
231 binary_file.close();
232 return false;
233 }
234
235 //write the file
236 binary_file.write( data, size );
237 if ( !binary_file.good() ) {
238 binary_file.close();
239 return false;
240 }
241
242 //close the file
243 binary_file.close();
244 if ( !binary_file.good() ) {
245 binary_file.close();
246 return false;
247 }
248
249 if ( make_executable ) {
250 string command = "chmod a+x " + string(filename);
251 system( command.c_str() );
252 }
253
254 return true;
255
256}
257#else
258
259bool prep_java( string wd ) {
260
261
262 string jTestCmd = "./jre/bin/java -version >/dev/null 2>&1";
263
264 //try to extract straight off cd
265 string j1cmd = wd + "/Java/Linux/jre_bin && tar -xf jre.tar && " + jTestCmd;
266 int j1status = system(j1cmd.c_str());
267 system("/bin/rm -f jre.tar");
268
269 if (WEXITSTATUS(j1status)!=0) {
270 cerr << "Unable to extract java straight off CD-ROM." << endl;
271 cerr << "This is probably because your CD-ROM was mounted with 'noexec'." << endl;
272 cerr << "It can be changed by a system administrator" << endl << endl;
273 cerr << "Copying Java to local disk so it can be run" << endl;
274
275 string cdtar_home = wd + "/Java/Linux/";
276 string tcmd = "(cd \"" + cdtar_home + "\" ; tar cf - jre_bin )";
277 tcmd += "| tar xvf - ";
278 //tcmd += "| awk 'BEGIN{C=0}{C++; printf(\".\"); if (C%70==0) printf(\"\\n\");}END{printf(\"\\n\")}'";
279
280 int tstatus = system(tcmd.c_str());
281
282 if (WEXITSTATUS(tstatus)!=0) {
283 cerr << "Failed to copy Java to disk" << endl;
284 cerr << "Please make sure you have write permissions in the directory where you run the installer" << endl;
285 }
286 else {
287
288 string j2cmd = "./jre_bin && tar -xf jre.tar && " + jTestCmd;
289 int j2status = system(j2cmd.c_str());
290 system("/bin/rm -f jre.tar jre_bin");
291
292 if (WEXITSTATUS(j2status)!=0) {
293 cerr << "Unable to run copied Java" << endl;
294 return false;
295 }
296 }
297 }
298
299 // to get to here, full_java must be set (and working)
300
301 return true;
302}
303
304#endif
Note: See TracBrowser for help on using the repository browser.