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 |
fileType.contains("universal binary") || |
559 |
fileType.contains("shell script")) { |
560 |
boolean exist = false; |
561 |
try { |
562 |
exist = new File(filePath).exists(); |
563 |
} catch (Exception e) { |
564 |
} |
565 |
if(exist) { |
566 |
fileExecutables.add(filePath); |
567 |
//System.out.println("Executable [/usr/bin/file]: " + filePath); |
568 |
} |
569 |
|
570 |
} |
571 |
} |
572 |
} |
573 |
List <String> allExecutables = new ArrayList<String> (); |
574 |
allExecutables.addAll(fileExecutables); |
575 |
for(String s : executableList) { |
576 |
if(!allExecutables.contains(s)) { |
577 |
//System.out.println("Executable [ interpreter ]: " + s); |
578 |
allExecutables.add(s); |
579 |
} |
580 |
} |
581 |
chmod(allExecutables); |
582 |
} |
583 |
} |
584 |
|
585 |
private void chmod(List <String> executables) { |
586 |
if(executables.isEmpty()) { |
587 |
return ; |
588 |
} |
589 |
if(System.getProperty("java.version").startsWith("1.5")) { |
590 |
// Find chmod |
591 |
File chmod = new File("/bin/chmod"); // NOI18N |
592 |
if (!chmod.isFile()) { |
593 |
chmod = new File("/usr/bin/chmod"); // NOI18N |
594 |
} |
595 |
if (chmod.isFile()) { |
596 |
List <String> commands = new ArrayList<String>(executables.size() + 2); |
597 |
commands.add(chmod.getAbsolutePath()); |
598 |
commands.add("a+x"); // NOI18N |
599 |
commands.addAll(executables); |
600 |
try { |
601 |
System.out.println("Executing command : " + commands); |
602 |
new ProcessBuilder(commands).start().waitFor(); |
603 |
} catch (Exception e) { |
604 |
e.printStackTrace(); |
605 |
} |
606 |
} |
607 |
} else { |
608 |
// Determine if java.io.File.setExecutable method is supported |
609 |
try { |
610 |
Method setExecutableMethod = File.class.getMethod("setExecutable", Boolean.TYPE, Boolean.TYPE); |
611 |
for(String s : executables) { |
612 |
setExecutableMethod.invoke(new File(s), true, false); |
613 |
} |
614 |
} catch (Exception e) { |
615 |
e.printStackTrace(); |
616 |
} |
617 |
} |
618 |
} |
619 |
|
620 |
private String readCommandOutput(List<String> command) { |
621 |
ProcessBuilder builder = new ProcessBuilder(command); |
622 |
boolean doRun = true; |
623 |
StringBuilder sb = new StringBuilder(); |
624 |
byte[] bytes = new byte[8192]; |
625 |
int c = 0; |
626 |
|
627 |
try { |
628 |
Process process = builder.start(); |
629 |
while (doRun) { |
630 |
try { |
631 |
Thread.sleep(20); |
632 |
} catch (InterruptedException e) { |
633 |
} |
634 |
try { |
635 |
process.exitValue(); |
636 |
doRun = false; |
637 |
} catch (IllegalThreadStateException e) { |
638 |
; // do nothing - the process is still running |
639 |
} |
640 |
InputStream is = process.getInputStream(); |
641 |
while ((c = is.read(bytes)) != -1) { |
642 |
sb.append(new String(bytes, 0, c)); |
643 |
} |
644 |
} |
645 |
return sb.toString(); |
646 |
} catch (IOException e) { |
647 |
e.printStackTrace(); |
648 |
return new String(); |
649 |
} |
650 |
} |
535 |
|
651 |
|
536 |
/** Quotes string correctly, eg. removes all quotes from the string and adds |
652 |
/** Quotes string correctly, eg. removes all quotes from the string and adds |
537 |
* just one at the start and |
653 |
* just one at the start and |
Lines 564-571
Link Here
|
564 |
* @param progressVal The current progress bar value. If this is |
680 |
* @param progressVal The current progress bar value. If this is |
565 |
* negative, we don't want to update the progress bar. |
681 |
* negative, we don't want to update the progress bar. |
566 |
*/ |
682 |
*/ |
567 |
private long copyStreams( InputStream src, OutputStream dest, |
683 |
private long copyStreams( InputStream src, File destFile, |
568 |
long progressVal ) throws java.io.IOException { |
684 |
long progressVal, List <String> list, List <String> executableList ) throws java.io.IOException { |
|
|
685 |
OutputStream dest = new FileOutputStream(destFile); |
686 |
list.add(destFile.getAbsolutePath()); |
569 |
|
687 |
|
570 |
BufferedInputStream bsrc = new BufferedInputStream( src ); |
688 |
BufferedInputStream bsrc = new BufferedInputStream( src ); |
571 |
BufferedOutputStream bdest = new BufferedOutputStream( dest ); |
689 |
BufferedOutputStream bdest = new BufferedOutputStream( dest ); |
Lines 574-583
Link Here
|
574 |
|
692 |
|
575 |
int c; |
693 |
int c; |
576 |
byte [] bytes = new byte [8192]; |
694 |
byte [] bytes = new byte [8192]; |
577 |
|
695 |
boolean read = false; |
578 |
try { |
696 |
try { |
579 |
while( ( c = bsrc.read(bytes) ) != -1 ) { |
697 |
while( ( c = bsrc.read(bytes) ) != -1 ) { |
580 |
bdest.write(bytes, 0, c); |
698 |
bdest.write(bytes, 0, c); |
|
|
699 |
if (!read) { |
700 |
read = true; |
701 |
if (c >= 4) { // length of ELF header and min length of "#!/X" string |
702 |
if (bytes[0] == '\177' && |
703 |
bytes[1] == 'E' && |
704 |
bytes[2] == 'L' && |
705 |
bytes[3] == 'F') { |
706 |
executableList.add(destFile.getAbsolutePath()); |
707 |
} else if (bytes[0] == '#' && bytes[1] == '!') { |
708 |
String s = new String(bytes, 0, c); |
709 |
String[] array = s.split("(?:\r\n|\n|\r)"); |
710 |
|
711 |
if (array.length > 0) { |
712 |
//read the first line only |
713 |
//allow lines like "#! /bin/sh" |
714 |
if (array[0].replaceAll("#!(\\s)+/", "#!/").startsWith("#!/")) { |
715 |
executableList.add(destFile.getAbsolutePath()); |
716 |
} |
717 |
} |
718 |
} |
719 |
} |
720 |
} |
581 |
count+=c; |
721 |
count+=c; |
582 |
if ( count > 8500 ) { |
722 |
if ( count > 8500 ) { |
583 |
if (progressVal >= 0) { |
723 |
if (progressVal >= 0) { |