• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

devonfw / IDEasy / 9907372175

12 Jul 2024 11:49AM UTC coverage: 61.142% (-0.02%) from 61.162%
9907372175

push

github

hohwille
fixed tests

1997 of 3595 branches covered (55.55%)

Branch coverage included in aggregate %.

5296 of 8333 relevant lines covered (63.55%)

2.8 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

55.37
cli/src/main/java/com/devonfw/tools/ide/io/FileAccessImpl.java
1
package com.devonfw.tools.ide.io;
2

3
import java.io.BufferedOutputStream;
4
import java.io.FileInputStream;
5
import java.io.FileOutputStream;
6
import java.io.IOException;
7
import java.io.InputStream;
8
import java.io.OutputStream;
9
import java.net.InetSocketAddress;
10
import java.net.Proxy;
11
import java.net.ProxySelector;
12
import java.net.URI;
13
import java.net.http.HttpClient;
14
import java.net.http.HttpClient.Redirect;
15
import java.net.http.HttpRequest;
16
import java.net.http.HttpResponse;
17
import java.nio.file.FileSystemException;
18
import java.nio.file.Files;
19
import java.nio.file.LinkOption;
20
import java.nio.file.NoSuchFileException;
21
import java.nio.file.Path;
22
import java.nio.file.attribute.BasicFileAttributes;
23
import java.nio.file.attribute.PosixFilePermission;
24
import java.nio.file.attribute.PosixFilePermissions;
25
import java.security.DigestInputStream;
26
import java.security.MessageDigest;
27
import java.security.NoSuchAlgorithmException;
28
import java.time.LocalDateTime;
29
import java.util.ArrayList;
30
import java.util.Iterator;
31
import java.util.List;
32
import java.util.Set;
33
import java.util.function.Consumer;
34
import java.util.function.Function;
35
import java.util.function.Predicate;
36
import java.util.stream.Stream;
37

38
import org.apache.commons.compress.archivers.ArchiveEntry;
39
import org.apache.commons.compress.archivers.ArchiveInputStream;
40
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
41
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
42
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
43

44
import com.devonfw.tools.ide.cli.CliException;
45
import com.devonfw.tools.ide.context.IdeContext;
46
import com.devonfw.tools.ide.os.SystemInfoImpl;
47
import com.devonfw.tools.ide.process.ProcessContext;
48
import com.devonfw.tools.ide.url.model.file.UrlChecksum;
49
import com.devonfw.tools.ide.util.DateTimeUtil;
50
import com.devonfw.tools.ide.util.FilenameUtil;
51
import com.devonfw.tools.ide.util.HexUtil;
52

53
/**
54
 * Implementation of {@link FileAccess}.
55
 */
56
public class FileAccessImpl implements FileAccess {
1✔
57

58
  private static final String WINDOWS_FILE_LOCK_DOCUMENTATION_PAGE = "https://github.com/devonfw/IDEasy/blob/main/documentation/windows-file-lock.adoc";
59

60
  private static final String WINDOWS_FILE_LOCK_WARNING =
61
      "On Windows, file operations could fail due to file locks. Please ensure the files in the moved directory are not in use. For further details, see: \n"
62
          + WINDOWS_FILE_LOCK_DOCUMENTATION_PAGE;
63

64
  private final IdeContext context;
65

66
  /**
67
   * The constructor.
68
   *
69
   * @param context the {@link IdeContext} to use.
70
   */
71
  public FileAccessImpl(IdeContext context) {
72

73
    super();
2✔
74
    this.context = context;
3✔
75
  }
1✔
76

77
  private HttpClient createHttpClient(String url) {
78

79
    HttpClient.Builder builder = HttpClient.newBuilder().followRedirects(Redirect.ALWAYS);
4✔
80
    Proxy proxy = this.context.getProxyContext().getProxy(url);
6✔
81
    if (proxy != Proxy.NO_PROXY) {
3!
82
      this.context.info("Downloading through proxy: " + proxy);
×
83
      InetSocketAddress proxyAddress = (InetSocketAddress) proxy.address();
×
84
      builder.proxy(ProxySelector.of(proxyAddress));
×
85
    }
86
    return builder.build();
3✔
87
  }
88

89
  @Override
90
  public void download(String url, Path target) {
91

92
    this.context.info("Trying to download {} from {}", target.getFileName(), url);
15✔
93
    mkdirs(target.getParent());
4✔
94
    try {
95
      if (url.startsWith("http")) {
4!
96

97
        HttpRequest request = HttpRequest.newBuilder().uri(URI.create(url)).GET().build();
7✔
98
        HttpClient client = createHttpClient(url);
4✔
99
        HttpResponse<InputStream> response = client.send(request, HttpResponse.BodyHandlers.ofInputStream());
5✔
100

101
        if (response.statusCode() == 200) {
4!
102
          downloadFileWithProgressBar(url, target, response);
5✔
103
        }
104
      } else if (url.startsWith("ftp") || url.startsWith("sftp")) {
1!
105
        throw new IllegalArgumentException("Unsupported download URL: " + url);
×
106
      } else {
107
        Path source = Path.of(url);
×
108
        if (isFile(source)) {
×
109
          // network drive
110
          copyFileWithProgressBar(source, target);
×
111
        } else {
112
          throw new IllegalArgumentException("Download path does not point to a downloadable file: " + url);
×
113
        }
114
      }
115
    } catch (Exception e) {
×
116
      throw new IllegalStateException("Failed to download file from URL " + url + " to " + target, e);
×
117
    }
1✔
118
  }
1✔
119

120
  /**
121
   * Downloads a file while showing a {@link IdeProgressBar}.
122
   *
123
   * @param url the url to download.
124
   * @param target Path of the target directory.
125
   * @param response the {@link HttpResponse} to use.
126
   */
127
  private void downloadFileWithProgressBar(String url, Path target, HttpResponse<InputStream> response) throws IOException {
128

129
    long contentLength = response.headers().firstValueAsLong("content-length").orElse(0);
7✔
130
    if (contentLength == 0) {
4!
131
      this.context.warning("Content-Length was not provided by download source : {} using fallback for the progress bar which will be inaccurate.", url);
×
132
      contentLength = 10000000;
×
133
    }
134

135
    byte[] data = new byte[1024];
3✔
136
    boolean fileComplete = false;
2✔
137
    int count;
138

139
    try (InputStream body = response.body();
4✔
140
        FileOutputStream fileOutput = new FileOutputStream(target.toFile());
6✔
141
        BufferedOutputStream bufferedOut = new BufferedOutputStream(fileOutput, data.length);
7✔
142
        IdeProgressBar pb = this.context.prepareProgressBar("Downloading", contentLength)) {
6✔
143
      while (!fileComplete) {
2✔
144
        count = body.read(data);
4✔
145
        if (count <= 0) {
2✔
146
          fileComplete = true;
3✔
147
        } else {
148
          bufferedOut.write(data, 0, count);
5✔
149
          pb.stepBy(count);
5✔
150
        }
151
      }
152
    } catch (Exception e) {
×
153
      throw new RuntimeException(e);
×
154
    }
1✔
155
  }
1✔
156

157
  /**
158
   * Copies a file while displaying a progress bar
159
   *
160
   * @param source Path of file to copy
161
   * @param target Path of target directory
162
   */
163
  private void copyFileWithProgressBar(Path source, Path target) throws IOException {
164

165
    try (InputStream in = new FileInputStream(source.toFile()); OutputStream out = new FileOutputStream(target.toFile())) {
×
166

167
      long size = source.toFile().length();
×
168
      byte[] buf = new byte[1024];
×
169
      int readBytes;
170

171
      try (IdeProgressBar pb = this.context.prepareProgressBar("Copying", size)) {
×
172
        while ((readBytes = in.read(buf)) > 0) {
×
173
          out.write(buf, 0, readBytes);
×
174
          pb.stepByOne();
×
175
        }
176
      } catch (Exception e) {
×
177
        throw new RuntimeException(e);
×
178
      }
×
179
    }
180
  }
×
181

182
  @Override
183
  public void mkdirs(Path directory) {
184

185
    if (Files.isDirectory(directory)) {
5✔
186
      return;
1✔
187
    }
188
    this.context.trace("Creating directory {}", directory);
10✔
189
    try {
190
      Files.createDirectories(directory);
5✔
191
    } catch (IOException e) {
×
192
      throw new IllegalStateException("Failed to create directory " + directory, e);
×
193
    }
1✔
194
  }
1✔
195

196
  @Override
197
  public boolean isFile(Path file) {
198

199
    if (!Files.exists(file)) {
×
200
      this.context.trace("File {} does not exist", file);
×
201
      return false;
×
202
    }
203
    if (Files.isDirectory(file)) {
×
204
      this.context.trace("Path {} is a directory but a regular file was expected", file);
×
205
      return false;
×
206
    }
207
    return true;
×
208
  }
209

210
  @Override
211
  public boolean isExpectedFolder(Path folder) {
212

213
    if (Files.isDirectory(folder)) {
5!
214
      return true;
×
215
    }
216
    this.context.warning("Expected folder was not found at {}", folder);
10✔
217
    return false;
2✔
218
  }
219

220
  @Override
221
  public String checksum(Path file) {
222

223
    try {
224
      MessageDigest md = MessageDigest.getInstance(UrlChecksum.HASH_ALGORITHM);
×
225
      byte[] buffer = new byte[1024];
×
226
      try (InputStream is = Files.newInputStream(file); DigestInputStream dis = new DigestInputStream(is, md)) {
×
227
        int read = 0;
×
228
        while (read >= 0) {
×
229
          read = dis.read(buffer);
×
230
        }
231
      } catch (Exception e) {
×
232
        throw new IllegalStateException("Failed to read and hash file " + file, e);
×
233
      }
×
234
      byte[] digestBytes = md.digest();
×
235
      String checksum = HexUtil.toHexString(digestBytes);
×
236
      return checksum;
×
237
    } catch (NoSuchAlgorithmException e) {
×
238
      throw new IllegalStateException("No such hash algorithm " + UrlChecksum.HASH_ALGORITHM, e);
×
239
    }
240
  }
241

242
  private boolean isJunction(Path path) {
243

244
    if (!SystemInfoImpl.INSTANCE.isWindows()) {
3!
245
      return false;
2✔
246
    }
247

248
    try {
249
      BasicFileAttributes attr = Files.readAttributes(path, BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
×
250
      return attr.isOther() && attr.isDirectory();
×
251
    } catch (NoSuchFileException e) {
×
252
      return false; // file doesn't exist
×
253
    } catch (IOException e) {
×
254
      // errors in reading the attributes of the file
255
      throw new IllegalStateException("An unexpected error occurred whilst checking if the file: " + path + " is a junction", e);
×
256
    }
257
  }
258

259
  @Override
260
  public void backup(Path fileOrFolder) {
261

262
    if (Files.isSymbolicLink(fileOrFolder) || isJunction(fileOrFolder)) {
7!
263
      delete(fileOrFolder);
×
264
    } else {
265
      // fileOrFolder is a directory
266
      Path backupPath = this.context.getIdeHome().resolve(IdeContext.FOLDER_UPDATES).resolve(IdeContext.FOLDER_BACKUPS);
8✔
267
      LocalDateTime now = LocalDateTime.now();
2✔
268
      String date = DateTimeUtil.formatDate(now);
3✔
269
      String time = DateTimeUtil.formatTime(now);
3✔
270
      Path backupDatePath = backupPath.resolve(date);
4✔
271
      mkdirs(backupDatePath);
3✔
272
      Path target = backupDatePath.resolve(fileOrFolder.getFileName().toString() + "_" + time);
8✔
273
      this.context.info("Creating backup by moving {} to {}", fileOrFolder, target);
14✔
274
      move(fileOrFolder, target);
4✔
275
    }
276
  }
1✔
277

278
  @Override
279
  public void move(Path source, Path targetDir) {
280

281
    this.context.trace("Moving {} to {}", source, targetDir);
14✔
282
    try {
283
      Files.move(source, targetDir);
6✔
284
    } catch (IOException e) {
×
285
      String fileType = Files.isSymbolicLink(source) ? "symlink" : isJunction(source) ? "junction" : Files.isDirectory(source) ? "directory" : "file";
×
286
      String message = "Failed to move " + fileType + ": " + source + " to " + targetDir + ".";
×
287
      if (this.context.getSystemInfo().isWindows()) {
×
288
        message = message + "\n" + WINDOWS_FILE_LOCK_WARNING;
×
289
      }
290
      throw new IllegalStateException(message, e);
×
291
    }
1✔
292
  }
1✔
293

294
  @Override
295
  public void copy(Path source, Path target, FileCopyMode mode) {
296

297
    if (mode != FileCopyMode.COPY_TREE_CONTENT) {
3✔
298
      // if we want to copy the file or folder "source" to the existing folder "target" in a shell this will copy
299
      // source into that folder so that we as a result have a copy in "target/source".
300
      // With Java NIO the raw copy method will fail as we cannot copy "source" to the path of the "target" folder.
301
      // For folders we want the same behavior as the linux "cp -r" command so that the "source" folder is copied
302
      // and not only its content what also makes it consistent with the move method that also behaves this way.
303
      // Therefore we need to add the filename (foldername) of "source" to the "target" path before.
304
      // For the rare cases, where we want to copy the content of a folder (cp -r source/* target) we support
305
      // it via the COPY_TREE_CONTENT mode.
306
      target = target.resolve(source.getFileName());
5✔
307
    }
308
    boolean fileOnly = mode.isFileOnly();
3✔
309
    if (fileOnly) {
2✔
310
      this.context.debug("Copying file {} to {}", source, target);
15✔
311
    } else {
312
      this.context.debug("Copying {} recursively to {}", source, target);
14✔
313
    }
314
    if (fileOnly && Files.isDirectory(source)) {
7!
315
      throw new IllegalStateException("Expected file but found a directory to copy at " + source);
×
316
    }
317
    if (mode.isFailIfExists()) {
3✔
318
      if (Files.exists(target)) {
5!
319
        throw new IllegalStateException("Failed to copy " + source + " to already existing target " + target);
×
320
      }
321
    } else if (mode == FileCopyMode.COPY_TREE_OVERRIDE_TREE) {
3✔
322
      delete(target);
3✔
323
    }
324
    try {
325
      copyRecursive(source, target, mode);
5✔
326
    } catch (IOException e) {
×
327
      throw new IllegalStateException("Failed to copy " + source + " to " + target, e);
×
328
    }
1✔
329
  }
1✔
330

331
  private void copyRecursive(Path source, Path target, FileCopyMode mode) throws IOException {
332

333
    if (Files.isDirectory(source)) {
5✔
334
      mkdirs(target);
3✔
335
      try (Stream<Path> childStream = Files.list(source)) {
4✔
336
        Iterator<Path> iterator = childStream.iterator();
3✔
337
        while (iterator.hasNext()) {
3✔
338
          Path child = iterator.next();
4✔
339
          copyRecursive(child, target.resolve(child.getFileName()), mode);
8✔
340
        }
1✔
341
      }
342
    } else if (Files.exists(source)) {
5!
343
      if (mode.isOverrideFile()) {
3✔
344
        delete(target);
3✔
345
      }
346
      this.context.trace("Copying {} to {}", source, target);
14✔
347
      Files.copy(source, target);
7✔
348
    } else {
349
      throw new IOException("Path " + source + " does not exist.");
×
350
    }
351
  }
1✔
352

353
  /**
354
   * Deletes the given {@link Path} if it is a symbolic link or a Windows junction. And throws an {@link IllegalStateException} if there is a file at the given
355
   * {@link Path} that is neither a symbolic link nor a Windows junction.
356
   *
357
   * @param path the {@link Path} to delete.
358
   * @throws IOException if the actual {@link Files#delete(Path) deletion} fails.
359
   */
360
  private void deleteLinkIfExists(Path path) throws IOException {
361

362
    boolean isJunction = isJunction(path); // since broken junctions are not detected by Files.exists()
4✔
363
    boolean isSymlink = Files.exists(path) && Files.isSymbolicLink(path);
12!
364

365
    assert !(isSymlink && isJunction);
5!
366

367
    if (isJunction || isSymlink) {
4!
368
      this.context.info("Deleting previous " + (isJunction ? "junction" : "symlink") + " at " + path);
8!
369
      Files.delete(path);
2✔
370
    }
371
  }
1✔
372

373
  /**
374
   * Adapts the given {@link Path} to be relative or absolute depending on the given {@code relative} flag. Additionally, {@link Path#toRealPath(LinkOption...)}
375
   * is applied to {@code source}.
376
   *
377
   * @param source the {@link Path} to adapt.
378
   * @param targetLink the {@link Path} used to calculate the relative path to the {@code source} if {@code relative} is set to {@code true}.
379
   * @param relative the {@code relative} flag.
380
   * @return the adapted {@link Path}.
381
   * @see FileAccessImpl#symlink(Path, Path, boolean)
382
   */
383
  private Path adaptPath(Path source, Path targetLink, boolean relative) throws IOException {
384

385
    if (source.isAbsolute()) {
3✔
386
      try {
387
        source = source.toRealPath(LinkOption.NOFOLLOW_LINKS); // to transform ../d1/../d2 to ../d2
9✔
388
      } catch (IOException e) {
×
389
        throw new IOException("Calling toRealPath() on the source (" + source + ") in method FileAccessImpl.adaptPath() failed.", e);
×
390
      }
1✔
391
      if (relative) {
2✔
392
        source = targetLink.getParent().relativize(source);
5✔
393
        // to make relative links like this work: dir/link -> dir
394
        source = (source.toString().isEmpty()) ? Path.of(".") : source;
12✔
395
      }
396
    } else { // source is relative
397
      if (relative) {
2✔
398
        // even though the source is already relative, toRealPath should be called to transform paths like
399
        // this ../d1/../d2 to ../d2
400
        source = targetLink.getParent().relativize(targetLink.resolveSibling(source).toRealPath(LinkOption.NOFOLLOW_LINKS));
14✔
401
        source = (source.toString().isEmpty()) ? Path.of(".") : source;
12✔
402
      } else { // !relative
403
        try {
404
          source = targetLink.resolveSibling(source).toRealPath(LinkOption.NOFOLLOW_LINKS);
11✔
405
        } catch (IOException e) {
×
406
          throw new IOException("Calling toRealPath() on " + targetLink + ".resolveSibling(" + source + ") in method FileAccessImpl.adaptPath() failed.", e);
×
407
        }
1✔
408
      }
409
    }
410
    return source;
2✔
411
  }
412

413
  /**
414
   * Creates a Windows junction at {@code targetLink} pointing to {@code source}.
415
   *
416
   * @param source must be another Windows junction or a directory.
417
   * @param targetLink the location of the Windows junction.
418
   */
419
  private void createWindowsJunction(Path source, Path targetLink) {
420

421
    this.context.trace("Creating a Windows junction at " + targetLink + " with " + source + " as source.");
×
422
    Path fallbackPath;
423
    if (!source.isAbsolute()) {
×
424
      this.context.warning("You are on Windows and you do not have permissions to create symbolic links. Junctions are used as an "
×
425
          + "alternative, however, these can not point to relative paths. So the source (" + source + ") is interpreted as an absolute path.");
426
      try {
427
        fallbackPath = targetLink.resolveSibling(source).toRealPath(LinkOption.NOFOLLOW_LINKS);
×
428
      } catch (IOException e) {
×
429
        throw new IllegalStateException(
×
430
            "Since Windows junctions are used, the source must be an absolute path. The transformation of the passed " + "source (" + source
431
                + ") to an absolute path failed.", e);
432
      }
×
433

434
    } else {
435
      fallbackPath = source;
×
436
    }
437
    if (!Files.isDirectory(fallbackPath)) { // if source is a junction. This returns true as well.
×
438
      throw new IllegalStateException(
×
439
          "These junctions can only point to directories or other junctions. Please make sure that the source (" + fallbackPath + ") is one of these.");
440
    }
441
    this.context.newProcess().executable("cmd").addArgs("/c", "mklink", "/d", "/j", targetLink.toString(), fallbackPath.toString()).run();
×
442
  }
×
443

444
  @Override
445
  public void symlink(Path source, Path targetLink, boolean relative) {
446

447
    Path adaptedSource = null;
2✔
448
    try {
449
      adaptedSource = adaptPath(source, targetLink, relative);
6✔
450
    } catch (IOException e) {
×
451
      throw new IllegalStateException("Failed to adapt source for source (" + source + ") target (" + targetLink + ") and relative (" + relative + ")", e);
×
452
    }
1✔
453
    this.context.trace("Creating {} symbolic link {} pointing to {}", adaptedSource.isAbsolute() ? "" : "relative", targetLink, adaptedSource);
23✔
454

455
    try {
456
      deleteLinkIfExists(targetLink);
3✔
457
    } catch (IOException e) {
×
458
      throw new IllegalStateException("Failed to delete previous symlink or Windows junction at " + targetLink, e);
×
459
    }
1✔
460

461
    try {
462
      Files.createSymbolicLink(targetLink, adaptedSource);
6✔
463
    } catch (FileSystemException e) {
×
464
      if (SystemInfoImpl.INSTANCE.isWindows()) {
×
465
        this.context.info("Due to lack of permissions, Microsoft's mklink with junction had to be used to create "
×
466
            + "a Symlink. See https://github.com/devonfw/IDEasy/blob/main/documentation/symlinks.asciidoc for " + "further details. Error was: "
467
            + e.getMessage());
×
468
        createWindowsJunction(adaptedSource, targetLink);
×
469
      } else {
470
        throw new RuntimeException(e);
×
471
      }
472
    } catch (IOException e) {
×
473
      throw new IllegalStateException(
×
474
          "Failed to create a " + (adaptedSource.isAbsolute() ? "" : "relative") + "symbolic link " + targetLink + " pointing to " + source, e);
×
475
    }
1✔
476
  }
1✔
477

478
  @Override
479
  public Path toRealPath(Path path) {
480

481
    try {
482
      Path realPath = path.toRealPath();
×
483
      if (!realPath.equals(path)) {
×
484
        this.context.trace("Resolved path {} to {}", path, realPath);
×
485
      }
486
      return realPath;
×
487
    } catch (IOException e) {
×
488
      throw new IllegalStateException("Failed to get real path for " + path, e);
×
489
    }
490
  }
491

492
  @Override
493
  public Path createTempDir(String name) {
494

495
    try {
496
      Path tmp = this.context.getTempPath();
4✔
497
      Path tempDir = tmp.resolve(name);
4✔
498
      int tries = 1;
2✔
499
      while (Files.exists(tempDir)) {
5!
500
        long id = System.nanoTime() & 0xFFFF;
×
501
        tempDir = tmp.resolve(name + "-" + id);
×
502
        tries++;
×
503
        if (tries > 200) {
×
504
          throw new IOException("Unable to create unique name!");
×
505
        }
506
      }
×
507
      return Files.createDirectory(tempDir);
5✔
508
    } catch (IOException e) {
×
509
      throw new IllegalStateException("Failed to create temporary directory with prefix '" + name + "'!", e);
×
510
    }
511
  }
512

513
  @Override
514
  public void extract(Path archiveFile, Path targetDir, Consumer<Path> postExtractHook, boolean extract) {
515

516
    if (Files.isDirectory(archiveFile)) {
5✔
517
      Path properInstallDir = archiveFile; // getProperInstallationSubDirOf(archiveFile, archiveFile);
2✔
518
      this.context.warning("Found directory for download at {} hence copying without extraction!", archiveFile);
10✔
519
      copy(properInstallDir, targetDir, FileCopyMode.COPY_TREE_CONTENT);
5✔
520
      postExtractHook(postExtractHook, properInstallDir);
4✔
521
      return;
1✔
522
    } else if (!extract) {
2!
523
      mkdirs(targetDir);
×
524
      move(archiveFile, targetDir.resolve(archiveFile.getFileName()));
×
525
      return;
×
526
    }
527
    Path tmpDir = createTempDir("extract-" + archiveFile.getFileName());
6✔
528
    this.context.trace("Trying to extract the downloaded file {} to {} and move it to {}.", archiveFile, tmpDir, targetDir);
18✔
529
    String filename = archiveFile.getFileName().toString();
4✔
530
    TarCompression tarCompression = TarCompression.of(filename);
3✔
531
    if (tarCompression != null) {
2!
532
      extractTar(archiveFile, tmpDir, tarCompression);
6✔
533
    } else {
534
      String extension = FilenameUtil.getExtension(filename);
×
535
      if (extension == null) {
×
536
        throw new IllegalStateException("Unknown archive format without extension - can not extract " + archiveFile);
×
537
      } else {
538
        this.context.trace("Determined file extension {}", extension);
×
539
      }
540
      switch (extension) {
×
541
        case "zip", "jar" -> {
542
          extractZip(archiveFile, tmpDir);
×
543
        }
×
544
        case "dmg" -> {
545
          extractDmg(archiveFile, tmpDir);
×
546
        }
×
547
        case "msi" -> {
548
          extractMsi(archiveFile, tmpDir);
×
549
        }
×
550
        case "pkg" -> {
551
          extractPkg(archiveFile, tmpDir);
×
552
        }
×
553
        default -> {
554
          throw new IllegalStateException("Unknown archive format " + extension + ". Can not extract " + archiveFile);
×
555
        }
556
      }
557
    }
558
    Path properInstallDir = getProperInstallationSubDirOf(tmpDir, archiveFile);
5✔
559
    postExtractHook(postExtractHook, properInstallDir);
4✔
560
    move(properInstallDir, targetDir);
4✔
561
    delete(tmpDir);
3✔
562
  }
1✔
563

564
  private void postExtractHook(Consumer<Path> postExtractHook, Path properInstallDir) {
565

566
    if (postExtractHook != null) {
2!
567
      postExtractHook.accept(properInstallDir);
3✔
568
    }
569
  }
1✔
570

571
  /**
572
   * @param path the {@link Path} to start the recursive search from.
573
   * @return the deepest subdir {@code s} of the passed path such that all directories between {@code s} and the passed path (including {@code s}) are the sole
574
   * item in their respective directory and {@code s} is not named "bin".
575
   */
576
  private Path getProperInstallationSubDirOf(Path path, Path archiveFile) {
577

578
    try (Stream<Path> stream = Files.list(path)) {
3✔
579
      Path[] subFiles = stream.toArray(Path[]::new);
8✔
580
      if (subFiles.length == 0) {
3!
581
        throw new CliException("The downloaded package " + archiveFile + " seems to be empty as you can check in the extracted folder " + path);
×
582
      } else if (subFiles.length == 1) {
4✔
583
        String filename = subFiles[0].getFileName().toString();
6✔
584
        if (!filename.equals(IdeContext.FOLDER_BIN) && !filename.equals(IdeContext.FOLDER_CONTENTS) && !filename.endsWith(".app") && Files.isDirectory(
19!
585
            subFiles[0])) {
586
          return getProperInstallationSubDirOf(subFiles[0], archiveFile);
9✔
587
        }
588
      }
589
      return path;
4✔
590
    } catch (IOException e) {
4!
591
      throw new IllegalStateException("Failed to get sub-files of " + path);
×
592
    }
593
  }
594

595
  @Override
596
  public void extractZip(Path file, Path targetDir) {
597

598
    extractArchive(file, targetDir, in -> new ZipArchiveInputStream(in));
×
599
  }
×
600

601
  @Override
602
  public void extractTar(Path file, Path targetDir, TarCompression compression) {
603

604
    extractArchive(file, targetDir, in -> new TarArchiveInputStream(compression.unpack(in)));
13✔
605
  }
1✔
606

607
  /**
608
   * @param permissions The integer as returned by {@link TarArchiveEntry#getMode()} that represents the file permissions of a file on a Unix file system.
609
   * @return A String representing the file permissions. E.g. "rwxrwxr-x" or "rw-rw-r--"
610
   */
611
  public static String generatePermissionString(int permissions) {
612

613
    // Ensure that only the last 9 bits are considered
614
    permissions &= 0b111111111;
4✔
615

616
    StringBuilder permissionStringBuilder = new StringBuilder("rwxrwxrwx");
5✔
617

618
    for (int i = 0; i < 9; i++) {
7✔
619
      int mask = 1 << i;
4✔
620
      char currentChar = ((permissions & mask) != 0) ? permissionStringBuilder.charAt(8 - i) : '-';
12✔
621
      permissionStringBuilder.setCharAt(8 - i, currentChar);
6✔
622
    }
623

624
    return permissionStringBuilder.toString();
3✔
625
  }
626

627
  private void extractArchive(Path file, Path targetDir, Function<InputStream, ArchiveInputStream> unpacker) {
628

629
    this.context.trace("Unpacking archive {} to {}", file, targetDir);
14✔
630
    try (InputStream is = Files.newInputStream(file); ArchiveInputStream ais = unpacker.apply(is)) {
10✔
631
      ArchiveEntry entry = ais.getNextEntry();
3✔
632
      boolean isTar = ais instanceof TarArchiveInputStream;
3✔
633
      while (entry != null) {
2✔
634
        String permissionStr = null;
2✔
635
        if (isTar) {
2!
636
          int tarMode = ((TarArchiveEntry) entry).getMode();
4✔
637
          permissionStr = generatePermissionString(tarMode);
3✔
638
        }
639

640
        Path entryName = Path.of(entry.getName());
6✔
641
        Path entryPath = targetDir.resolve(entryName).toAbsolutePath();
5✔
642
        if (!entryPath.startsWith(targetDir)) {
4!
643
          throw new IOException("Preventing path traversal attack from " + entryName + " to " + entryPath);
×
644
        }
645
        if (entry.isDirectory()) {
3✔
646
          mkdirs(entryPath);
4✔
647
        } else {
648
          // ensure the file can also be created if directory entry was missing or out of order...
649
          mkdirs(entryPath.getParent());
4✔
650
          Files.copy(ais, entryPath);
6✔
651
        }
652
        if (isTar && !this.context.getSystemInfo().isWindows()) {
7!
653
          Set<PosixFilePermission> permissions = PosixFilePermissions.fromString(permissionStr);
3✔
654
          Files.setPosixFilePermissions(entryPath, permissions);
4✔
655
        }
656
        entry = ais.getNextEntry();
3✔
657
      }
1✔
658
    } catch (IOException e) {
×
659
      throw new IllegalStateException("Failed to extract " + file + " to " + targetDir, e);
×
660
    }
1✔
661
  }
1✔
662

663
  @Override
664
  public void extractDmg(Path file, Path targetDir) {
665

666
    assert this.context.getSystemInfo().isMac();
×
667

668
    Path mountPath = this.context.getIdeHome().resolve(IdeContext.FOLDER_UPDATES).resolve(IdeContext.FOLDER_VOLUME);
×
669
    mkdirs(mountPath);
×
670
    ProcessContext pc = this.context.newProcess();
×
671
    pc.executable("hdiutil");
×
672
    pc.addArgs("attach", "-quiet", "-nobrowse", "-mountpoint", mountPath, file);
×
673
    pc.run();
×
674
    Path appPath = findFirst(mountPath, p -> p.getFileName().toString().endsWith(".app"), false);
×
675
    if (appPath == null) {
×
676
      throw new IllegalStateException("Failed to unpack DMG as no MacOS *.app was found in file " + file);
×
677
    }
678

679
    copy(appPath, targetDir, FileCopyMode.COPY_TREE_OVERRIDE_TREE);
×
680
    pc.addArgs("detach", "-force", mountPath);
×
681
    pc.run();
×
682
  }
×
683

684
  @Override
685
  public void extractMsi(Path file, Path targetDir) {
686

687
    this.context.newProcess().executable("msiexec").addArgs("/a", file, "/qn", "TARGETDIR=" + targetDir).run();
×
688
    // msiexec also creates a copy of the MSI
689
    Path msiCopy = targetDir.resolve(file.getFileName());
×
690
    delete(msiCopy);
×
691
  }
×
692

693
  @Override
694
  public void extractPkg(Path file, Path targetDir) {
695

696
    Path tmpDirPkg = createTempDir("ide-pkg-");
×
697
    ProcessContext pc = this.context.newProcess();
×
698
    // we might also be able to use cpio from commons-compression instead of external xar...
699
    pc.executable("xar").addArgs("-C", tmpDirPkg, "-xf", file).run();
×
700
    Path contentPath = findFirst(tmpDirPkg, p -> p.getFileName().toString().equals("Payload"), true);
×
701
    extractTar(contentPath, targetDir, TarCompression.GZ);
×
702
    delete(tmpDirPkg);
×
703
  }
×
704

705
  @Override
706
  public void delete(Path path) {
707

708
    if (!Files.exists(path)) {
5✔
709
      this.context.trace("Deleting {} skipped as the path does not exist.", path);
10✔
710
      return;
1✔
711
    }
712
    this.context.debug("Deleting {} ...", path);
10✔
713
    try {
714
      if (Files.isSymbolicLink(path) || isJunction(path)) {
7!
715
        Files.delete(path);
×
716
      } else {
717
        deleteRecursive(path);
3✔
718
      }
719
    } catch (IOException e) {
×
720
      throw new IllegalStateException("Failed to delete " + path, e);
×
721
    }
1✔
722
  }
1✔
723

724
  private void deleteRecursive(Path path) throws IOException {
725

726
    if (Files.isDirectory(path)) {
5✔
727
      try (Stream<Path> childStream = Files.list(path)) {
3✔
728
        Iterator<Path> iterator = childStream.iterator();
3✔
729
        while (iterator.hasNext()) {
3✔
730
          Path child = iterator.next();
4✔
731
          deleteRecursive(child);
3✔
732
        }
1✔
733
      }
734
    }
735
    this.context.trace("Deleting {} ...", path);
10✔
736
    Files.delete(path);
2✔
737
  }
1✔
738

739
  @Override
740
  public Path findFirst(Path dir, Predicate<Path> filter, boolean recursive) {
741

742
    try {
743
      if (!Files.isDirectory(dir)) {
5✔
744
        return null;
2✔
745
      }
746
      return findFirstRecursive(dir, filter, recursive);
6✔
747
    } catch (IOException e) {
×
748
      throw new IllegalStateException("Failed to search for file in " + dir, e);
×
749
    }
750
  }
751

752
  private Path findFirstRecursive(Path dir, Predicate<Path> filter, boolean recursive) throws IOException {
753

754
    List<Path> folders = null;
2✔
755
    try (Stream<Path> childStream = Files.list(dir)) {
3✔
756
      Iterator<Path> iterator = childStream.iterator();
3✔
757
      while (iterator.hasNext()) {
3✔
758
        Path child = iterator.next();
4✔
759
        if (filter.test(child)) {
4✔
760
          return child;
4✔
761
        } else if (recursive && Files.isDirectory(child)) {
2!
762
          if (folders == null) {
×
763
            folders = new ArrayList<>();
×
764
          }
765
          folders.add(child);
×
766
        }
767
      }
1✔
768
    }
4!
769
    if (folders != null) {
2!
770
      for (Path child : folders) {
×
771
        Path match = findFirstRecursive(child, filter, recursive);
×
772
        if (match != null) {
×
773
          return match;
×
774
        }
775
      }
×
776
    }
777
    return null;
2✔
778
  }
779

780
  @Override
781
  public List<Path> listChildren(Path dir, Predicate<Path> filter) {
782

783
    if (!Files.isDirectory(dir)) {
5✔
784
      return List.of();
2✔
785
    }
786
    List<Path> children = new ArrayList<>();
4✔
787
    try (Stream<Path> childStream = Files.list(dir)) {
3✔
788
      Iterator<Path> iterator = childStream.iterator();
3✔
789
      while (iterator.hasNext()) {
3✔
790
        Path child = iterator.next();
4✔
791
        if (filter.test(child)) {
4!
792
          this.context.trace("Accepted file {}", child);
10✔
793
          children.add(child);
5✔
794
        } else {
795
          this.context.trace("Ignoring file {} according to filter", child);
×
796
        }
797
      }
1✔
798
    } catch (IOException e) {
×
799
      throw new IllegalStateException("Failed to find children of directory " + dir, e);
×
800
    }
1✔
801
    return children;
2✔
802
  }
803

804
  @Override
805
  public boolean isEmptyDir(Path dir) {
806

807
    return listChildren(dir, f -> true).isEmpty();
8✔
808
  }
809

810
  @Override
811
  public Path findExistingFile(String fileName, List<Path> searchDirs) {
812

813
    for (Path dir : searchDirs) {
10!
814
      Path filePath = dir.resolve(fileName);
4✔
815
      try {
816
        if (Files.exists(filePath)) {
5!
817
          return filePath;
2✔
818
        }
819
      } catch (Exception e) {
×
820
        throw new IllegalStateException("Unexpected error while checking existence of file " + filePath + " .", e);
×
821
      }
×
822
    }
×
823
    return null;
×
824
  }
825
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc