Lines 42-47
Link Here
|
42 |
package org.netbeans.updater; |
42 |
package org.netbeans.updater; |
43 |
|
43 |
|
44 |
import java.io.*; |
44 |
import java.io.*; |
|
|
45 |
import java.lang.reflect.Method; |
45 |
import java.util.*; |
46 |
import java.util.*; |
46 |
import java.util.jar.*; |
47 |
import java.util.jar.*; |
47 |
|
48 |
|
Lines 354-359
Link Here
|
354 |
try { |
355 |
try { |
355 |
jarFile = new JarFile (nbm); |
356 |
jarFile = new JarFile (nbm); |
356 |
Enumeration entries = jarFile.entries(); |
357 |
Enumeration entries = jarFile.entries(); |
|
|
358 |
List <String> list = new ArrayList <String> (jarFile.size()); |
359 |
List <String> executableList = new ArrayList <String> (); |
357 |
while( entries.hasMoreElements() ) { |
360 |
while( entries.hasMoreElements() ) { |
358 |
JarEntry entry = (JarEntry) entries.nextElement(); |
361 |
JarEntry entry = (JarEntry) entries.nextElement(); |
359 |
checkStop(); |
362 |
checkStop(); |
Lines 375-388
Link Here
|
375 |
File bckFile = new File( getBackupDirectory (cluster), entry.getName() ); |
378 |
File bckFile = new File( getBackupDirectory (cluster), entry.getName() ); |
376 |
bckFile.getParentFile ().mkdirs (); |
379 |
bckFile.getParentFile ().mkdirs (); |
377 |
// System.out.println("Backing up" ); // NOI18N |
380 |
// System.out.println("Backing up" ); // NOI18N |
378 |
copyStreams( new FileInputStream( destFile ), new FileOutputStream( bckFile ), -1 ); |
381 |
copyStreams( new FileInputStream( destFile ), bckFile, -1, list, executableList ); |
379 |
if (!destFile.delete() && isWindows()) { |
382 |
if (!destFile.delete() && isWindows()) { |
380 |
trickyDeleteOnWindows(destFile); |
383 |
trickyDeleteOnWindows(destFile); |
381 |
} |
384 |
} |
382 |
} else { |
385 |
} else { |
383 |
destFile.getParentFile ().mkdirs (); |
386 |
destFile.getParentFile ().mkdirs (); |
384 |
} |
387 |
} |
385 |
bytesRead = copyStreams( jarFile.getInputStream( entry ), new FileOutputStream( destFile ), bytesRead ); |
388 |
bytesRead = copyStreams( jarFile.getInputStream( entry ), destFile, bytesRead, list, executableList ); |
386 |
UpdaterFrame.setProgressValue( bytesRead ); |
389 |
UpdaterFrame.setProgressValue( bytesRead ); |
387 |
} |
390 |
} |
388 |
} else if ( entry.getName().startsWith( UPDATE_MAIN_DIR )&& |
391 |
} else if ( entry.getName().startsWith( UPDATE_MAIN_DIR )&& |
Lines 392-401
Link Here
|
392 |
entry.getName().substring(UPDATE_MAIN_DIR.length() + 1) ); |
395 |
entry.getName().substring(UPDATE_MAIN_DIR.length() + 1) ); |
393 |
destFile.getParentFile ().mkdirs (); |
396 |
destFile.getParentFile ().mkdirs (); |
394 |
hasMainClass = true; |
397 |
hasMainClass = true; |
395 |
bytesRead = copyStreams( jarFile.getInputStream( entry ), new FileOutputStream( destFile ), bytesRead ); |
398 |
bytesRead = copyStreams( jarFile.getInputStream( entry ), destFile, bytesRead, list, executableList ); |
396 |
UpdaterFrame.setProgressValue( bytesRead ); |
399 |
UpdaterFrame.setProgressValue( bytesRead ); |
397 |
} |
400 |
} |
398 |
} |
401 |
} |
|
|
402 |
fixPermissions(list,executableList); |
399 |
if ( hasMainClass ) { |
403 |
if ( hasMainClass ) { |
400 |
MainConfig mconfig = new MainConfig (getMainDirString (cluster) + UpdateTracking.FILE_SEPARATOR + JVM_PARAMS_FILE, cluster); |
404 |
MainConfig mconfig = new MainConfig (getMainDirString (cluster) + UpdateTracking.FILE_SEPARATOR + JVM_PARAMS_FILE, cluster); |
401 |
if (mconfig.isValid()) { |
405 |
if (mconfig.isValid()) { |
Lines 532-537
Link Here
|
532 |
private String getMainDirString (File activeCluster) { |
536 |
private String getMainDirString (File activeCluster) { |
533 |
return getMainDirectory (activeCluster).getPath (); |
537 |
return getMainDirectory (activeCluster).getPath (); |
534 |
} |
538 |
} |
|
|
539 |
|
540 |
|
541 |
private void fixPermissions(List<String> list, List <String> executableList) { |
542 |
if (!isWindows() && !list.isEmpty()) { |
543 |
List<String> commands = new ArrayList<String>(); |
544 |
commands.add("/usr/bin/file"); |
545 |
commands.addAll(list); |
546 |
|
547 |
String filesInfo = readCommandOutput(commands); |
548 |
List<String> fileExecutables = new ArrayList<String>(); |
549 |
for (String s : filesInfo.split("(?:\r\n|\n|\r)")) {//split by lines |
550 |
int colonIndex = s.indexOf(":"); |
551 |
if (colonIndex != -1) { |
552 |
String filePath = s.substring(0, colonIndex).trim(); |
553 |
String fileType = s.substring(colonIndex + 1); |
554 |
if (fileType.contains("executable") || |
555 |
fileType.contains("dynamic lib") || |
556 |
fileType.contains("shared object") || |
557 |
fileType.contains("shared library")) { |
558 |
fileExecutables.add(filePath); |
559 |
//System.out.println("Executable [/usr/bin/file]: " + filePath); |
560 |
} |
561 |
} |
562 |
} |
563 |
List <String> allExecutables = new ArrayList<String> (); |
564 |
allExecutables.addAll(fileExecutables); |
565 |
for(String s : executableList) { |
566 |
if(!allExecutables.contains(s)) { |
567 |
//System.out.println("Executable [ interpreter ]: " + s); |
568 |
allExecutables.add(s); |
569 |
} |
570 |
} |
571 |
chmod(allExecutables); |
572 |
} |
573 |
} |
574 |
|
575 |
private void chmod(List <String> executables) { |
576 |
if(executables.isEmpty()) { |
577 |
return ; |
578 |
} |
579 |
if(System.getProperty("java.version").startsWith("1.5")) { |
580 |
// Find chmod |
581 |
File chmod = new File("/bin/chmod"); // NOI18N |
582 |
if (!chmod.isFile()) { |
583 |
chmod = new File("/usr/bin/chmod"); // NOI18N |
584 |
} |
585 |
if (chmod.isFile()) { |
586 |
List <String> commands = new ArrayList<String>(executables.size() + 2); |
587 |
commands.add(chmod.getAbsolutePath()); |
588 |
commands.add("a+x"); // NOI18N |
589 |
commands.addAll(executables); |
590 |
try { |
591 |
System.out.println("Executing command : " + commands); |
592 |
new ProcessBuilder(commands).start().waitFor(); |
593 |
} catch (Exception e) { |
594 |
e.printStackTrace(); |
595 |
} |
596 |
} |
597 |
} else { |
598 |
// Determine if java.io.File.setExecutable method is supported |
599 |
try { |
600 |
Method setExecutableMethod = File.class.getMethod("setExecutable", new Class[] {Boolean.class, Boolean.class}); |
601 |
for(String s : executables) { |
602 |
setExecutableMethod.invoke(new File(s), true, false); |
603 |
} |
604 |
} catch (Exception e) { |
605 |
e.printStackTrace(); |
606 |
} |
607 |
} |
608 |
} |
609 |
|
610 |
private String readCommandOutput(List<String> command) { |
611 |
ProcessBuilder builder = new ProcessBuilder(command); |
612 |
boolean doRun = true; |
613 |
StringBuilder sb = new StringBuilder(); |
614 |
byte[] bytes = new byte[8192]; |
615 |
int c = 0; |
616 |
|
617 |
try { |
618 |
Process process = builder.start(); |
619 |
while (doRun) { |
620 |
try { |
621 |
Thread.sleep(20); |
622 |
} catch (InterruptedException e) { |
623 |
} |
624 |
try { |
625 |
process.exitValue(); |
626 |
doRun = false; |
627 |
} catch (IllegalThreadStateException e) { |
628 |
; // do nothing - the process is still running |
629 |
} |
630 |
InputStream is = process.getInputStream(); |
631 |
while ((c = is.read(bytes)) != -1) { |
632 |
sb.append(new String(bytes, 0, c)); |
633 |
} |
634 |
} |
635 |
return sb.toString(); |
636 |
} catch (IOException e) { |
637 |
e.printStackTrace(); |
638 |
return new String(); |
639 |
} |
640 |
} |
535 |
|
641 |
|
536 |
/** Quotes string correctly, eg. removes all quotes from the string and adds |
642 |
/** Quotes string correctly, eg. removes all quotes from the string and adds |
537 |
* just one at the start and |
643 |
* just one at the start and |
Lines 564-571
Link Here
|
564 |
* @param progressVal The current progress bar value. If this is |
670 |
* @param progressVal The current progress bar value. If this is |
565 |
* negative, we don't want to update the progress bar. |
671 |
* negative, we don't want to update the progress bar. |
566 |
*/ |
672 |
*/ |
567 |
private long copyStreams( InputStream src, OutputStream dest, |
673 |
private long copyStreams( InputStream src, File destFile, |
568 |
long progressVal ) throws java.io.IOException { |
674 |
long progressVal, List <String> list, List <String> executableList ) throws java.io.IOException { |
|
|
675 |
OutputStream dest = new FileOutputStream(destFile); |
676 |
list.add(destFile.getAbsolutePath()); |
569 |
|
677 |
|
570 |
BufferedInputStream bsrc = new BufferedInputStream( src ); |
678 |
BufferedInputStream bsrc = new BufferedInputStream( src ); |
571 |
BufferedOutputStream bdest = new BufferedOutputStream( dest ); |
679 |
BufferedOutputStream bdest = new BufferedOutputStream( dest ); |
Lines 574-583
Link Here
|
574 |
|
682 |
|
575 |
int c; |
683 |
int c; |
576 |
byte [] bytes = new byte [8192]; |
684 |
byte [] bytes = new byte [8192]; |
577 |
|
685 |
boolean read = false; |
578 |
try { |
686 |
try { |
579 |
while( ( c = bsrc.read(bytes) ) != -1 ) { |
687 |
while( ( c = bsrc.read(bytes) ) != -1 ) { |
580 |
bdest.write(bytes, 0, c); |
688 |
bdest.write(bytes, 0, c); |
|
|
689 |
if (!read) { |
690 |
read = true; |
691 |
if (c >= 4) { // length of ELF header and min length of "#!/X" string |
692 |
if (bytes[0] == '\177' && |
693 |
bytes[1] == 'E' && |
694 |
bytes[2] == 'L' && |
695 |
bytes[3] == 'F') { |
696 |
executableList.add(destFile.getAbsolutePath()); |
697 |
} else if (bytes[0] == '#' && bytes[1] == '!') { |
698 |
String s = new String(bytes, 0, c); |
699 |
String[] array = s.split("(?:\r\n|\n|\r)"); |
700 |
|
701 |
if (array.length > 0) { |
702 |
//read the first line only |
703 |
//allow lines like "#! /bin/sh" |
704 |
if (array[0].replaceAll("#!(\\s)+/", "#!/").startsWith("#!/")) { |
705 |
executableList.add(destFile.getAbsolutePath()); |
706 |
} |
707 |
} |
708 |
} |
709 |
} |
710 |
} |
581 |
count+=c; |
711 |
count+=c; |
582 |
if ( count > 8500 ) { |
712 |
if ( count > 8500 ) { |
583 |
if (progressVal >= 0) { |
713 |
if (progressVal >= 0) { |