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

devonfw / IDEasy / 9805587641

05 Jul 2024 08:42AM UTC coverage: 61.648% (+0.6%) from 61.034%
9805587641

Pull #297

github

web-flow
Merge 21be926e1 into bd17cb99c
Pull Request #297: #25: implement tool commandlet for intellij

1974 of 3525 branches covered (56.0%)

Branch coverage included in aggregate %.

5240 of 8177 relevant lines covered (64.08%)

2.8 hits per line

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

55.08
cli/src/main/java/com/devonfw/tools/ide/io/FileAccessImpl.java
1
package com.devonfw.tools.ide.io;
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
import org.apache.commons.compress.archivers.ArchiveEntry;
12
import org.apache.commons.compress.archivers.ArchiveInputStream;
13
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
14
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
15
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
16

17
import java.io.BufferedOutputStream;
18
import java.io.FileInputStream;
19
import java.io.FileOutputStream;
20
import java.io.IOException;
21
import java.io.InputStream;
22
import java.io.OutputStream;
23
import java.net.InetSocketAddress;
24
import java.net.Proxy;
25
import java.net.ProxySelector;
26
import java.net.URI;
27
import java.net.http.HttpClient;
28
import java.net.http.HttpClient.Redirect;
29
import java.net.http.HttpRequest;
30
import java.net.http.HttpResponse;
31
import java.nio.file.FileSystemException;
32
import java.nio.file.Files;
33
import java.nio.file.LinkOption;
34
import java.nio.file.NoSuchFileException;
35
import java.nio.file.Path;
36
import java.nio.file.attribute.BasicFileAttributes;
37
import java.nio.file.attribute.PosixFilePermission;
38
import java.nio.file.attribute.PosixFilePermissions;
39
import java.security.DigestInputStream;
40
import java.security.MessageDigest;
41
import java.security.NoSuchAlgorithmException;
42
import java.time.LocalDateTime;
43
import java.util.ArrayList;
44
import java.util.Iterator;
45
import java.util.List;
46
import java.util.Set;
47
import java.util.function.Consumer;
48
import java.util.function.Function;
49
import java.util.function.Predicate;
50
import java.util.stream.Stream;
51

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

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

59
  private static final String WINDOWS_FILE_LOCK_WARNING =
60
      "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"
61
          + WINDOWS_FILE_LOCK_DOCUMENTATION_PAGE;
62

63
  private final IdeContext context;
64

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

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

76
  private HttpClient createHttpClient(String url) {
77

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

241
  private boolean isJunction(Path path) {
242

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

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

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

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

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

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

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

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

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

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

352
  /**
353
   * 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
354
   * {@link Path} that is neither a symbolic link nor a Windows junction.
355
   *
356
   * @param path the {@link Path} to delete.
357
   * @throws IOException if the actual {@link Files#delete(Path) deletion} fails.
358
   */
359
  private void deleteLinkIfExists(Path path) throws IOException {
360

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

570
  /**
571
   * @param path the {@link Path} to start the recursive search from.
572
   * @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
573
   * item in their respective directory and {@code s} is not named "bin".
574
   */
575
  private Path getProperInstallationSubDirOf(Path path, Path archiveFile) {
576

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

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

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

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

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

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

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

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

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

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

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

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

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

662
  public void extractDmg(Path file, Path targetDir) {
663

664
    assert this.context.getSystemInfo().isMac();
×
665

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

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

682
  public void extractMsi(Path file, Path targetDir) {
683

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

690
  public void extractPkg(Path file, Path targetDir) {
691

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

701
  @Override
702
  public void delete(Path path) {
703

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

720
  private void deleteRecursive(Path path) throws IOException {
721

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

735
  @Override
736
  public Path findFirst(Path dir, Predicate<Path> filter, boolean recursive) {
737

738
    try {
739
      return findFirstRecursive(dir, filter, recursive);
6✔
740
    } catch (IOException e) {
×
741
      throw new IllegalStateException("Failed to search for file in " + dir, e);
×
742
    }
743
  }
744

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

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

773
  @Override
774
  public List<Path> listChildren(Path dir, Predicate<Path> filter) {
775

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

797
  @Override
798
  public boolean isEmptyDir(Path dir) {
799

800
    return listChildren(dir, f -> true).isEmpty();
8✔
801
  }
802

803
  @Override
804
  public Path findExistingFile(String fileName, List<Path> searchDirs) {
805

806
    for (Path dir : searchDirs) {
10!
807
      Path filePath = dir.resolve(fileName);
4✔
808
      try {
809
        if (Files.exists(filePath)) {
5!
810
          return filePath;
2✔
811
        }
812
      } catch (Exception e) {
×
813
        throw new IllegalStateException("Unexpected error while checking existence of file " + filePath + " .", e);
×
814
      }
×
815
    }
×
816
    return null;
×
817
  }
818
}
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

© 2025 Coveralls, Inc