1
1
package com .devonfw .tools .ide .io ;
2
2
3
- import com .devonfw .tools .ide .cli .CliException ;
4
- import com .devonfw .tools .ide .context .IdeContext ;
5
- import com .devonfw .tools .ide .os .SystemInfoImpl ;
6
- import com .devonfw .tools .ide .process .ProcessContext ;
7
- import com .devonfw .tools .ide .url .model .file .UrlChecksum ;
8
- import com .devonfw .tools .ide .util .DateTimeUtil ;
9
- import com .devonfw .tools .ide .util .FilenameUtil ;
10
- import com .devonfw .tools .ide .util .HexUtil ;
11
-
12
- import org .apache .commons .compress .archivers .ArchiveEntry ;
13
- import org .apache .commons .compress .archivers .ArchiveInputStream ;
14
- import org .apache .commons .compress .archivers .tar .TarArchiveEntry ;
15
- import org .apache .commons .compress .archivers .tar .TarArchiveInputStream ;
16
- import org .apache .commons .compress .archivers .zip .ZipArchiveInputStream ;
17
-
18
3
import java .io .BufferedOutputStream ;
19
4
import java .io .FileInputStream ;
20
5
import java .io .FileOutputStream ;
47
32
import java .util .function .Predicate ;
48
33
import java .util .stream .Stream ;
49
34
35
+ import org .apache .commons .compress .archivers .ArchiveEntry ;
36
+ import org .apache .commons .compress .archivers .ArchiveInputStream ;
37
+ import org .apache .commons .compress .archivers .tar .TarArchiveEntry ;
38
+ import org .apache .commons .compress .archivers .tar .TarArchiveInputStream ;
39
+ import org .apache .commons .compress .archivers .zip .ZipArchiveInputStream ;
40
+
41
+ import com .devonfw .tools .ide .cli .CliException ;
42
+ import com .devonfw .tools .ide .context .IdeContext ;
43
+ import com .devonfw .tools .ide .os .SystemInfoImpl ;
44
+ import com .devonfw .tools .ide .process .ProcessContext ;
45
+ import com .devonfw .tools .ide .url .model .file .UrlChecksum ;
46
+ import com .devonfw .tools .ide .util .DateTimeUtil ;
47
+ import com .devonfw .tools .ide .util .FilenameUtil ;
48
+ import com .devonfw .tools .ide .util .HexUtil ;
49
+
50
50
/**
51
51
* Implementation of {@link FileAccess}.
52
52
*/
@@ -280,10 +280,12 @@ public void copy(Path source, Path target, FileCopyMode mode) {
280
280
if (fileOnly ) {
281
281
this .context .debug ("Copying file {} to {}" , source , target );
282
282
if (Files .isDirectory (target )) {
283
- // if we want to copy "file.txt" to the existing folder "path/to/folder/" in a shell this will copy "file.txt" into that folder
283
+ // if we want to copy "file.txt" to the existing folder "path/to/folder/" in a shell this will copy "file.txt"
284
+ // into that folder
284
285
// with Java NIO the raw copy method will fail as we cannot copy the file to the path of the target folder
285
286
// even worse if FileCopyMode is override the target folder ("path/to/folder/") would be deleted and the result
286
- // of our "file.txt" would later appear in "path/to/folder". To prevent such bugs we append the filename to target
287
+ // of our "file.txt" would later appear in "path/to/folder". To prevent such bugs we append the filename to
288
+ // target
287
289
target = target .resolve (source .getFileName ());
288
290
}
289
291
} else {
@@ -355,7 +357,7 @@ private void deleteLinkIfExists(Path path) throws IOException {
355
357
*
356
358
* @param source the {@link Path} to adapt.
357
359
* @param targetLink the {@link Path} used to calculate the relative path to the {@code source} if {@code relative} is
358
- * set to {@code true}.
360
+ * set to {@code true}.
359
361
* @param relative the {@code relative} flag.
360
362
* @return the adapted {@link Path}.
361
363
* @see FileAccessImpl#symlink(Path, Path, boolean)
@@ -413,7 +415,8 @@ private void createWindowsJunction(Path source, Path targetLink) {
413
415
} catch (IOException e ) {
414
416
throw new IllegalStateException (
415
417
"Since Windows junctions are used, the source must be an absolute path. The transformation of the passed "
416
- + "source (" + source + ") to an absolute path failed." , e );
418
+ + "source (" + source + ") to an absolute path failed." ,
419
+ e );
417
420
}
418
421
419
422
} else {
@@ -435,9 +438,8 @@ public void symlink(Path source, Path targetLink, boolean relative) {
435
438
try {
436
439
adaptedSource = adaptPath (source , targetLink , relative );
437
440
} catch (IOException e ) {
438
- throw new IllegalStateException (
439
- "Failed to adapt source for source (" + source + ") target (" + targetLink + ") and relative (" + relative
440
- + ")" , e );
441
+ throw new IllegalStateException ("Failed to adapt source for source (" + source + ") target (" + targetLink
442
+ + ") and relative (" + relative + ")" , e );
441
443
}
442
444
this .context .trace ("Creating {} symbolic link {} pointing to {}" , adaptedSource .isAbsolute () ? "" : "relative" ,
443
445
targetLink , adaptedSource );
@@ -451,7 +453,7 @@ public void symlink(Path source, Path targetLink, boolean relative) {
451
453
try {
452
454
Files .createSymbolicLink (targetLink , adaptedSource );
453
455
} catch (FileSystemException e ) {
454
- if (this . context . getSystemInfo () .isWindows ()) {
456
+ if (SystemInfoImpl . INSTANCE .isWindows ()) {
455
457
this .context .info ("Due to lack of permissions, Microsoft's mklink with junction had to be used to create "
456
458
+ "a Symlink. See https://github.com/devonfw/IDEasy/blob/main/documentation/symlinks.asciidoc for "
457
459
+ "further details. Error was: " + e .getMessage ());
@@ -460,9 +462,8 @@ public void symlink(Path source, Path targetLink, boolean relative) {
460
462
throw new RuntimeException (e );
461
463
}
462
464
} catch (IOException e ) {
463
- throw new IllegalStateException (
464
- "Failed to create a " + (adaptedSource .isAbsolute () ? "" : "relative" ) + "symbolic link " + targetLink
465
- + " pointing to " + source , e );
465
+ throw new IllegalStateException ("Failed to create a " + (adaptedSource .isAbsolute () ? "" : "relative" )
466
+ + "symbolic link " + targetLink + " pointing to " + source , e );
466
467
}
467
468
}
468
469
@@ -505,13 +506,14 @@ public Path createTempDir(String name) {
505
506
public void extract (Path archiveFile , Path targetDir , Consumer <Path > postExtractHook , boolean extract ) {
506
507
507
508
if (Files .isDirectory (archiveFile )) {
508
- Path properInstallDir = archiveFile ; //getProperInstallationSubDirOf(archiveFile, archiveFile);
509
+ Path properInstallDir = archiveFile ; // getProperInstallationSubDirOf(archiveFile, archiveFile);
509
510
if (extract ) {
510
511
this .context .warning ("Found directory for download at {} hence copying without extraction!" , archiveFile );
511
512
copy (properInstallDir , targetDir , FileCopyMode .COPY_TREE_OVERRIDE_TREE );
512
513
} else {
513
514
move (properInstallDir , targetDir );
514
515
}
516
+ postExtractHook (postExtractHook , properInstallDir );
515
517
return ;
516
518
} else if (!extract ) {
517
519
mkdirs (targetDir );
@@ -551,26 +553,31 @@ public void extract(Path archiveFile, Path targetDir, Consumer<Path> postExtract
551
553
}
552
554
}
553
555
Path properInstallDir = getProperInstallationSubDirOf (tmpDir , archiveFile );
556
+ postExtractHook (postExtractHook , properInstallDir );
557
+ move (properInstallDir , targetDir );
558
+ delete (tmpDir );
559
+ }
560
+
561
+ private void postExtractHook (Consumer <Path > postExtractHook , Path properInstallDir ) {
562
+
554
563
if (postExtractHook != null ) {
555
564
postExtractHook .accept (properInstallDir );
556
565
}
557
- move (properInstallDir , targetDir );
558
- delete (tmpDir );
559
566
}
560
567
561
568
/**
562
569
* @param path the {@link Path} to start the recursive search from.
563
570
* @return the deepest subdir {@code s} of the passed path such that all directories between {@code s} and the passed
564
- * path (including {@code s}) are the sole item in their respective directory and {@code s} is not named "bin".
571
+ * path (including {@code s}) are the sole item in their respective directory and {@code s} is not named
572
+ * "bin".
565
573
*/
566
574
private Path getProperInstallationSubDirOf (Path path , Path archiveFile ) {
567
575
568
576
try (Stream <Path > stream = Files .list (path )) {
569
577
Path [] subFiles = stream .toArray (Path []::new );
570
578
if (subFiles .length == 0 ) {
571
- throw new CliException (
572
- "The downloaded package " + archiveFile + " seems to be empty as you can check in the extracted folder "
573
- + path );
579
+ throw new CliException ("The downloaded package " + archiveFile
580
+ + " seems to be empty as you can check in the extracted folder " + path );
574
581
} else if (subFiles .length == 1 ) {
575
582
String filename = subFiles [0 ].getFileName ().toString ();
576
583
if (!filename .equals (IdeContext .FOLDER_BIN ) && !filename .equals (IdeContext .FOLDER_CONTENTS )
@@ -598,7 +605,7 @@ public void extractTar(Path file, Path targetDir, TarCompression compression) {
598
605
599
606
/**
600
607
* @param permissions The integer as returned by {@link TarArchiveEntry#getMode()} that represents the file
601
- * permissions of a file on a Unix file system.
608
+ * permissions of a file on a Unix file system.
602
609
* @return A String representing the file permissions. E.g. "rwxrwxr-x" or "rw-rw-r--"
603
610
*/
604
611
public static String generatePermissionString (int permissions ) {
0 commit comments