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

devonfw / IDEasy / 26876438778

03 Jun 2026 09:37AM UTC coverage: 71.113% (+0.02%) from 71.092%
26876438778

push

github

web-flow
#1985: install commandlet test fails (#2001)

4522 of 7040 branches covered (64.23%)

Branch coverage included in aggregate %.

11711 of 15787 relevant lines covered (74.18%)

3.14 hits per line

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

68.01
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.FileOutputStream;
5
import java.io.IOException;
6
import java.io.InputStream;
7
import java.io.OutputStream;
8
import java.io.Reader;
9
import java.io.Writer;
10
import java.net.http.HttpClient.Version;
11
import java.net.http.HttpResponse;
12
import java.nio.charset.StandardCharsets;
13
import java.nio.file.FileSystemException;
14
import java.nio.file.Files;
15
import java.nio.file.LinkOption;
16
import java.nio.file.NoSuchFileException;
17
import java.nio.file.Path;
18
import java.nio.file.StandardCopyOption;
19
import java.nio.file.attribute.BasicFileAttributes;
20
import java.nio.file.attribute.DosFileAttributeView;
21
import java.nio.file.attribute.FileTime;
22
import java.nio.file.attribute.PosixFileAttributeView;
23
import java.nio.file.attribute.PosixFilePermission;
24
import java.security.DigestInputStream;
25
import java.security.MessageDigest;
26
import java.security.NoSuchAlgorithmException;
27
import java.time.Duration;
28
import java.time.LocalDateTime;
29
import java.util.ArrayList;
30
import java.util.Enumeration;
31
import java.util.HashSet;
32
import java.util.Iterator;
33
import java.util.List;
34
import java.util.Objects;
35
import java.util.Properties;
36
import java.util.Set;
37
import java.util.function.Consumer;
38
import java.util.function.Function;
39
import java.util.function.Predicate;
40
import java.util.stream.Stream;
41

42
import org.apache.commons.compress.archivers.ArchiveEntry;
43
import org.apache.commons.compress.archivers.ArchiveInputStream;
44
import org.apache.commons.compress.archivers.ArchiveOutputStream;
45
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
46
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
47
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
48
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
49
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
50
import org.apache.commons.compress.archivers.zip.ZipFile;
51
import org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream;
52
import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream;
53
import org.apache.commons.compress.compressors.gzip.GzipParameters;
54
import org.apache.commons.io.IOUtils;
55
import org.slf4j.Logger;
56
import org.slf4j.LoggerFactory;
57

58
import com.devonfw.tools.ide.cli.CliException;
59
import com.devonfw.tools.ide.context.IdeContext;
60
import com.devonfw.tools.ide.io.ini.IniComment;
61
import com.devonfw.tools.ide.io.ini.IniFile;
62
import com.devonfw.tools.ide.io.ini.IniSection;
63
import com.devonfw.tools.ide.os.SystemInfoImpl;
64
import com.devonfw.tools.ide.process.ProcessContext;
65
import com.devonfw.tools.ide.process.ProcessMode;
66
import com.devonfw.tools.ide.process.ProcessResult;
67
import com.devonfw.tools.ide.util.DateTimeUtil;
68
import com.devonfw.tools.ide.util.FilenameUtil;
69
import com.devonfw.tools.ide.util.HexUtil;
70
import com.devonfw.tools.ide.variable.IdeVariables;
71

72
/**
73
 * Implementation of {@link FileAccess}.
74
 */
75
public class FileAccessImpl extends HttpDownloader implements FileAccess {
76

77
  private static final Logger LOG = LoggerFactory.getLogger(FileAccessImpl.class);
4✔
78

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

81
  private static final String WINDOWS_FILE_LOCK_WARNING =
82
      "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"
83
          + WINDOWS_FILE_LOCK_DOCUMENTATION_PAGE;
84

85
  private final IdeContext context;
86

87
  /**
88
   * The constructor.
89
   *
90
   * @param context the {@link IdeContext} to use.
91
   */
92
  public FileAccessImpl(IdeContext context) {
93

94
    super();
2✔
95
    this.context = context;
3✔
96
  }
1✔
97

98
  @Override
99
  public void download(String url, Path target) {
100

101
    if (url.startsWith("http")) {
4✔
102
      downloadViaHttp(url, target);
5✔
103
    } else if (url.startsWith("ftp") || url.startsWith("sftp")) {
8!
104
      throw new IllegalArgumentException("Unsupported download URL: " + url);
×
105
    } else {
106
      Path source = Path.of(url);
5✔
107
      if (isFile(source)) {
4!
108
        // network drive
109
        copyFileWithProgressBar(source, target);
5✔
110
      } else {
111
        throw new IllegalArgumentException("Download path does not point to a downloadable file: " + url);
×
112
      }
113
    }
114
  }
1✔
115

116
  private void downloadViaHttp(String url, Path target) {
117

118
    List<Version> httpProtocols = IdeVariables.HTTP_VERSIONS.get(this.context);
6✔
119
    Exception lastException = null;
2✔
120
    if (httpProtocols.isEmpty()) {
3!
121
      try {
122
        downloadWithHttpVersion(url, target, null);
5✔
123
        return;
1✔
124
      } catch (Exception e) {
×
125
        lastException = e;
×
126
      }
×
127
    } else {
128
      for (Version version : httpProtocols) {
×
129
        try {
130
          downloadWithHttpVersion(url, target, version);
×
131
          return;
×
132
        } catch (Exception ex) {
×
133
          lastException = ex;
×
134
        }
135
      }
×
136
    }
137
    throw new IllegalStateException("Failed to download file from URL " + url + " to " + target, lastException);
×
138
  }
139

140
  private void downloadWithHttpVersion(String url, Path target, Version httpVersion) throws Exception {
141

142
    if (httpVersion == null) {
2!
143
      LOG.info("Trying to download {} from {}", target.getFileName(), url);
7✔
144
    } else {
145
      LOG.info("Trying to download {} from {} with HTTP protocol version {}", target.getFileName(), url, httpVersion);
×
146
    }
147
    mkdirs(target.getParent());
4✔
148
    this.context.getNetworkStatus().invokeNetworkTask(() ->
11✔
149
    {
150
      httpGet(url, httpVersion, (response) -> downloadFileWithProgressBar(url, target, response));
13✔
151
      return null;
2✔
152
    }, url);
153
  }
1✔
154

155
  /**
156
   * Downloads a file while showing a {@link IdeProgressBar}.
157
   *
158
   * @param url the url to download.
159
   * @param target Path of the target directory.
160
   * @param response the {@link HttpResponse} to use.
161
   */
162
  private void downloadFileWithProgressBar(String url, Path target, HttpResponse<InputStream> response) {
163

164
    long contentLength = response.headers().firstValueAsLong("content-length").orElse(-1);
7✔
165
    if (contentLength < 0) {
4✔
166
      LOG.warn("Content-Length was not provided by download from {}", url);
4✔
167
    }
168

169
    byte[] data = new byte[1024];
3✔
170
    boolean fileComplete = false;
2✔
171
    int count;
172

173
    try (InputStream body = response.body();
4✔
174
        FileOutputStream fileOutput = new FileOutputStream(target.toFile());
6✔
175
        BufferedOutputStream bufferedOut = new BufferedOutputStream(fileOutput, data.length);
7✔
176
        IdeProgressBar pb = this.context.newProgressBarForDownload(contentLength)) {
5✔
177
      while (!fileComplete) {
2✔
178
        count = body.read(data);
4✔
179
        if (count <= 0) {
2✔
180
          fileComplete = true;
3✔
181
        } else {
182
          bufferedOut.write(data, 0, count);
5✔
183
          pb.stepBy(count);
5✔
184
        }
185
      }
186
    } catch (Exception e) {
×
187
      throw new RuntimeException(e);
×
188
    }
1✔
189
  }
1✔
190

191
  private void copyFileWithProgressBar(Path source, Path target) {
192

193
    long size = getFileSize(source);
4✔
194
    if (size < 100_000) {
4✔
195
      copy(source, target, FileCopyMode.COPY_FILE_TO_TARGET_OVERRIDE);
5✔
196
      return;
1✔
197
    }
198
    try (InputStream in = Files.newInputStream(source); OutputStream out = Files.newOutputStream(target)) {
10✔
199
      byte[] buf = new byte[1024];
3✔
200
      try (IdeProgressBar pb = this.context.newProgressbarForCopying(size)) {
5✔
201
        int readBytes;
202
        while ((readBytes = in.read(buf)) > 0) {
6✔
203
          out.write(buf, 0, readBytes);
5✔
204
          pb.stepBy(readBytes);
5✔
205
        }
206
      } catch (Exception e) {
×
207
        throw new RuntimeException(e);
×
208
      }
1✔
209
    } catch (IOException e) {
×
210
      throw new RuntimeException("Failed to copy from " + source + " to " + target, e);
×
211
    }
1✔
212
  }
1✔
213

214
  @Override
215
  public String download(String url) {
216

217
    LOG.debug("Downloading text body from {}", url);
4✔
218
    return httpGetAsString(url);
3✔
219
  }
220

221
  @Override
222
  public void mkdirs(Path directory) {
223

224
    if (Files.isDirectory(directory)) {
5✔
225
      return;
1✔
226
    }
227
    LOG.trace("Creating directory {}", directory);
4✔
228
    try {
229
      Files.createDirectories(directory);
5✔
230
    } catch (IOException e) {
×
231
      throw new IllegalStateException("Failed to create directory " + directory, e);
×
232
    }
1✔
233
  }
1✔
234

235
  @Override
236
  public boolean isFile(Path file) {
237

238
    if (!Files.exists(file)) {
5!
239
      LOG.trace("File {} does not exist", file);
×
240
      return false;
×
241
    }
242
    if (Files.isDirectory(file)) {
5!
243
      LOG.trace("Path {} is a directory but a regular file was expected", file);
×
244
      return false;
×
245
    }
246
    return true;
2✔
247
  }
248

249
  @Override
250
  public boolean isExpectedFolder(Path folder) {
251

252
    if (Files.isDirectory(folder)) {
5✔
253
      return true;
2✔
254
    }
255
    LOG.warn("Expected folder was not found at {}", folder);
4✔
256
    return false;
2✔
257
  }
258

259
  @Override
260
  public String checksum(Path file, String hashAlgorithm) {
261

262
    MessageDigest md;
263
    try {
264
      md = MessageDigest.getInstance(hashAlgorithm);
3✔
265
    } catch (NoSuchAlgorithmException e) {
×
266
      throw new IllegalStateException("No such hash algorithm " + hashAlgorithm, e);
×
267
    }
1✔
268
    byte[] buffer = new byte[1024];
3✔
269
    try (InputStream is = Files.newInputStream(file); DigestInputStream dis = new DigestInputStream(is, md)) {
11✔
270
      int read = 0;
2✔
271
      while (read >= 0) {
2✔
272
        read = dis.read(buffer);
5✔
273
      }
274
    } catch (Exception e) {
×
275
      throw new IllegalStateException("Failed to read and hash file " + file, e);
×
276
    }
1✔
277
    byte[] digestBytes = md.digest();
3✔
278
    return HexUtil.toHexString(digestBytes);
3✔
279
  }
280

281
  @Override
282
  public boolean isJunction(Path path) {
283

284
    if (!SystemInfoImpl.INSTANCE.isWindows()) {
3!
285
      return false;
2✔
286
    }
287
    try {
288
      BasicFileAttributes attr = Files.readAttributes(path, BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
×
289
      return attr.isOther() && attr.isDirectory();
×
290
    } catch (NoSuchFileException e) {
×
291
      return false; // file doesn't exist
×
292
    } catch (IOException e) {
×
293
      // For broken junctions, reading attributes might fail with various IOExceptions.
294
      // In such cases, we should check if the path exists without following links.
295
      // If it exists but we can't read its attributes, it's likely a broken junction.
296
      if (Files.exists(path, LinkOption.NOFOLLOW_LINKS)) {
×
297
        LOG.debug("Path {} exists but attributes cannot be read, likely a broken junction: {}", path, e.getMessage());
×
298
        return true; // Assume it's a broken junction
×
299
      }
300
      // If it doesn't exist at all, it's not a junction
301
      return false;
×
302
    }
303
  }
304

305
  @Override
306
  public Path backup(Path fileOrFolder) {
307

308
    if ((fileOrFolder != null) && (Files.isSymbolicLink(fileOrFolder) || isJunction(fileOrFolder))) {
9!
309
      delete(fileOrFolder);
4✔
310
    } else if ((fileOrFolder != null) && Files.exists(fileOrFolder)) {
7!
311
      LOG.trace("Going to backup {}", fileOrFolder);
4✔
312
      LocalDateTime now = LocalDateTime.now();
2✔
313
      String date = DateTimeUtil.formatDate(now, true);
4✔
314
      String time = DateTimeUtil.formatTime(now);
3✔
315
      String filename = fileOrFolder.getFileName().toString();
4✔
316
      Path backupBaseDir = this.context.getIdeHome();
4✔
317
      if (backupBaseDir == null) {
2!
318
        backupBaseDir = this.context.getIdePath();
×
319
      }
320
      Path backupPath = backupBaseDir.resolve(IdeContext.FOLDER_BACKUPS).resolve(date).resolve(time + "_" + filename);
10✔
321
      backupPath = appendParentPath(backupPath, fileOrFolder.getParent(), 2);
6✔
322
      mkdirs(backupPath);
3✔
323
      Path target = backupPath.resolve(filename);
4✔
324
      LOG.info("Creating backup by moving {} to {}", fileOrFolder, target);
5✔
325
      move(fileOrFolder, target);
6✔
326
      return target;
2✔
327
    } else {
328
      LOG.trace("Backup of {} skipped as the path does not exist.", fileOrFolder);
4✔
329
    }
330
    return fileOrFolder;
2✔
331
  }
332

333
  private static Path appendParentPath(Path path, Path parent, int max) {
334

335
    if ((parent == null) || (max <= 0)) {
4!
336
      return path;
2✔
337
    }
338
    return appendParentPath(path, parent.getParent(), max - 1).resolve(parent.getFileName());
11✔
339
  }
340

341
  @Override
342
  public void move(Path source, Path targetDir, StandardCopyOption... copyOptions) {
343

344
    LOG.trace("Moving {} to {}", source, targetDir);
5✔
345
    try {
346
      Files.move(source, targetDir, copyOptions);
5✔
347
    } catch (IOException e) {
×
348
      String fileType = Files.isSymbolicLink(source) ? "symlink" : isJunction(source) ? "junction" : Files.isDirectory(source) ? "directory" : "file";
×
349
      String message = "Failed to move " + fileType + ": " + source + " to " + targetDir + ".";
×
350
      if (this.context.getSystemInfo().isWindows()) {
×
351
        message = message + "\n" + WINDOWS_FILE_LOCK_WARNING;
×
352
      }
353
      throw new IllegalStateException(message, e);
×
354
    }
1✔
355
  }
1✔
356

357
  @Override
358
  public void copy(Path source, Path target, FileCopyMode mode, PathCopyListener listener) {
359

360
    if (mode.isUseSourceFilename()) {
3✔
361
      // if we want to copy the file or folder "source" to the existing folder "target" in a shell this will copy
362
      // source into that folder so that we as a result have a copy in "target/source".
363
      // With Java NIO the raw copy method will fail as we cannot copy "source" to the path of the "target" folder.
364
      // For folders we want the same behavior as the linux "cp -r" command so that the "source" folder is copied
365
      // and not only its content what also makes it consistent with the move method that also behaves this way.
366
      // Therefore we need to add the filename (foldername) of "source" to the "target" path before.
367
      // For the rare cases, where we want to copy the content of a folder (cp -r source/* target) we support
368
      // it via the COPY_TREE_CONTENT mode.
369
      Path fileName = source.getFileName();
3✔
370
      if (fileName != null) { // if filename is null, we are copying the root of a (virtual filesystem)
2!
371
        target = target.resolve(fileName.toString());
5✔
372
      }
373
    }
374
    boolean fileOnly = mode.isFileOnly();
3✔
375
    String operation = mode.getOperation();
3✔
376
    if (mode.isExtract()) {
3!
377
      LOG.debug("Starting to {} to {}", operation, target);
×
378
    } else {
379
      if (fileOnly) {
2✔
380
        LOG.debug("Starting to {} file {} to {}", operation, source, target);
18✔
381
      } else {
382
        LOG.debug("Starting to {} {} recursively to {}", operation, source, target);
17✔
383
      }
384
    }
385
    if (fileOnly && Files.isDirectory(source)) {
7!
386
      throw new IllegalStateException("Expected file but found a directory to copy at " + source);
×
387
    }
388
    if (mode.isFailIfExists()) {
3✔
389
      if (Files.exists(target)) {
5!
390
        throw new IllegalStateException("Failed to " + operation + " " + source + " to already existing target " + target);
×
391
      }
392
    } else if (mode == FileCopyMode.COPY_TREE_OVERRIDE_TREE) {
3✔
393
      delete(target);
3✔
394
    }
395
    try {
396
      copyRecursive(source, target, mode, listener);
6✔
397
    } catch (IOException e) {
×
398
      throw new IllegalStateException("Failed to " + operation + " " + source + " to " + target, e);
×
399
    }
1✔
400
  }
1✔
401

402
  private void copyRecursive(Path source, Path target, FileCopyMode mode, PathCopyListener listener) throws IOException {
403

404
    if (Files.isDirectory(source)) {
5✔
405
      mkdirs(target);
3✔
406
      try (Stream<Path> childStream = Files.list(source)) {
3✔
407
        Iterator<Path> iterator = childStream.iterator();
3✔
408
        while (iterator.hasNext()) {
3✔
409
          Path child = iterator.next();
4✔
410
          copyRecursive(child, target.resolve(child.getFileName().toString()), mode, listener);
10✔
411
        }
1✔
412
      }
413
      listener.onCopy(source, target, true);
6✔
414
    } else if (Files.exists(source)) {
5!
415
      if (mode.isOverride()) {
3✔
416
        delete(target);
3✔
417
      }
418
      LOG.trace("Starting to {} {} to {}", mode.getOperation(), source, target);
18✔
419
      Files.copy(source, target);
6✔
420
      listener.onCopy(source, target, false);
6✔
421
    } else {
422
      throw new IOException("Path " + source + " does not exist.");
×
423
    }
424
  }
1✔
425

426
  /**
427
   * Deletes the given {@link Path} if it is a symbolic link or a Windows junction or hard link. And throws an {@link IllegalStateException} if it fails to
428
   * delete the file at the given {@link Path}.
429
   *
430
   * @param path the {@link Path} to delete.
431
   */
432
  private void deleteLinkIfExists(Path path) {
433

434
    boolean isJunction = isJunction(path); // since broken junctions are not detected by Files.exists()
4✔
435
    boolean exists = Files.exists(path, LinkOption.NOFOLLOW_LINKS);
9✔
436

437
    if (isJunction || exists) {
4!
438
      LOG.info("Deleting previous link or file at " + path);
5✔
439
      try {
440
        Files.delete(path);
2✔
441
      } catch (IOException e) {
×
442
        throw new IllegalStateException("Failed to delete link or file at " + path, e);
×
443
      }
1✔
444
    }
445
  }
1✔
446

447
  /**
448
   * Adapts the given {@link Path} to be relative or absolute depending on the given {@code relative} flag. Additionally, {@link Path#toRealPath(LinkOption...)}
449
   * is applied to {@code target}.
450
   *
451
   * @param link the {@link Path} the link should point to and that is to be adapted.
452
   * @param source the {@link Path} to the link. It is used to calculate the relative path to the {@code target} if {@code relative} is set to
453
   *     {@code true}.
454
   * @param relative the {@code relative} flag.
455
   * @return the adapted {@link Path}.
456
   * @see #symlink(Path, Path, boolean)
457
   */
458
  private Path adaptPath(Path source, Path link, boolean relative) {
459

460
    if (!source.isAbsolute()) {
3✔
461
      source = link.resolveSibling(source);
4✔
462
    }
463
    try {
464
      source = source.toRealPath(LinkOption.NOFOLLOW_LINKS); // to transform ../d1/../d2 to ../d2
9✔
465
    } catch (IOException e) {
×
466
      throw new RuntimeException("Failed to get real path of " + source, e);
×
467
    }
1✔
468
    if (relative) {
2✔
469
      source = link.getParent().relativize(source);
5✔
470
      // to make relative links like this work: dir/link -> dir
471
      source = (source.toString().isEmpty()) ? Path.of(".") : source;
11✔
472
    }
473
    return source;
2✔
474
  }
475

476
  /**
477
   * Creates a Windows link using mklink at {@code link} pointing to {@code target}.
478
   *
479
   * @param source the {@link Path} the link will point to.
480
   * @param link the {@link Path} where to create the link.
481
   * @param type the {@link PathLinkType}.
482
   */
483
  private void mklinkOnWindows(Path source, Path absoluteSource, Path link, PathLinkType type, boolean relative) {
484

485
    Path finalSource = source;
×
486
    Path finalLink = link;
×
487
    Path cwd = null;
×
488
    if (relative) {
×
489
      int count = getCommonNameCount(absoluteSource, link);
×
490
      if (count < 1) {
×
491
        LOG.error("Cannot create relative link at {} pointing to {} - falling back to absolute link", link, absoluteSource);
×
492
      } else {
493
        cwd = getPathStart(absoluteSource, count - 1);
×
494
        finalSource = getPathEnd(absoluteSource, count);
×
495
        finalLink = getPathEnd(link, count);
×
496
      }
497
    }
498

499
    String option = type.getMklinkOption();
×
500
    if (type == PathLinkType.SYMBOLIC_LINK) {
×
501
      boolean directoryTarget = Files.isDirectory(absoluteSource);
×
502
      option = directoryTarget ? "/j" : "/d";
×
503
    }
504

505
    if (!runMklink(finalSource, finalLink, cwd, option)) {
×
506
      throw new IllegalStateException("Failed to create Windows link at " + link + " pointing to " + source);
×
507
    }
508
  }
×
509

510
  private boolean runMklink(Path source, Path link, Path cwd, String option) {
511

512
    LOG.trace("Creating a Windows link with mklink {} at {} pointing to {}", option, link, source);
×
513
    ProcessContext pc = this.context.newProcess().executable("cmd").addArgs("/c", "mklink", option);
×
514
    if (cwd != null) {
×
515
      pc.directory(cwd);
×
516
    }
517
    ProcessResult result = pc.addArgs(link.toString(), source.toString()).run(ProcessMode.DEFAULT);
×
518
    try {
519
      result.failOnError();
×
520
      return true;
×
521
    } catch (RuntimeException e) {
×
522
      LOG.debug("mklink {} failed for {} -> {}: {}", option, link, source, e.getMessage());
×
523
      return false;
×
524
    }
525
  }
526

527
  @Override
528
  public void link(Path source, Path link, boolean relative, PathLinkType type) {
529

530
    Path finalLink = link.toAbsolutePath().normalize();
4✔
531
    Path finalSource;
532
    try {
533
      finalSource = adaptPath(source, finalLink, relative);
6✔
534
    } catch (Exception e) {
×
535
      throw new IllegalStateException("Failed to adapt target (" + source + ") for link (" + finalLink + ") and relative (" + relative + ")", e);
×
536
    }
1✔
537
    Path absoluteSource = finalSource.isAbsolute() ? finalSource : finalLink.getParent().resolve(finalSource).normalize();
11✔
538
    String relativeOrAbsolute = relative ? "relative" : "absolute";
6✔
539
    LOG.debug("Creating {} {} at {} pointing to {}", relativeOrAbsolute, type, finalLink, finalSource);
21✔
540
    deleteLinkIfExists(finalLink);
3✔
541
    try {
542
      // Attention: JavaDoc and position of path arguments can be very confusing - see comment in #1736
543
      if (type == PathLinkType.SYMBOLIC_LINK) {
3!
544
        Files.createSymbolicLink(finalLink, finalSource);
7✔
545
      } else if (type == PathLinkType.HARD_LINK) {
×
546
        createHardLink(finalSource, finalLink);
×
547
      } else {
548
        throw new IllegalStateException("" + type);
×
549
      }
550
    } catch (FileSystemException e) {
×
551
      if (SystemInfoImpl.INSTANCE.isWindows()) {
×
552
        LOG.info(
×
553
            "Due to lack of permissions, Microsoft's mklink with junction had to be used to create a Symlink. See\n"
554
                + "https://github.com/devonfw/IDEasy/blob/main/documentation/symlink.adoc for further details. Error was: "
555
                + e.getMessage());
×
556

557
        try {
558
          mklinkOnWindows(finalSource, absoluteSource, finalLink, type, relative);
×
559
        } catch (IllegalStateException mkEx) {
×
560
          LOG.info("Creating a hard link as a fallback for the failed mklink attempt.");
×
561
          createHardLink(absoluteSource, finalLink);
×
562
        }
×
563
      } else {
564
        throw new RuntimeException(e);
×
565
      }
566
    } catch (IOException e) {
×
567
      throw new IllegalStateException("Failed to create a " + relativeOrAbsolute + " " + type + " at " + finalLink + " pointing to " + source, e);
×
568
    }
1✔
569
  }
1✔
570

571

572
  /**
573
   * Creates a hard link at {@code link} pointing to {@code source}.
574
   *
575
   * @param source the {@link Path} the hard link will point to.
576
   * @param link the {@link Path} where to create the hard link.
577
   */
578
  void createHardLink(Path source, Path link) {
579
    try {
580
      Files.createLink(link, source);
×
581
      LOG.trace("Created hard link at {} pointing to {}", link, source);
×
582
    } catch (IOException e) {
×
583
      throw new RuntimeException("Failed to create a hardlink for " + source + " at " + link, e);
×
584
    }
×
585
  }
×
586

587
  @Override
588
  public Path getPathStart(Path path, int nameEnd) {
589

590
    int count = path.getNameCount();
3✔
591
    int delta = count - nameEnd - 1;
6✔
592
    if (delta < 0) {
2!
593
      throw new IllegalArgumentException("Cannot get start before segment " + nameEnd + " from path " + path + " that has only " + count + " segments.");
×
594
    }
595
    Path result = path;
2✔
596
    while (delta > 0) {
2✔
597
      result = result.getParent();
3✔
598
      delta--;
2✔
599
    }
600
    return result;
2✔
601
  }
602

603
  @Override
604
  public Path getPathEnd(Path path, int nameStart) {
605

606
    int count = path.getNameCount();
3✔
607
    if (nameStart > count) {
3!
608
      throw new IllegalArgumentException("Cannot get end after segment " + nameStart + " from path " + path + " that has only " + count + " segments.");
×
609
    } else if (nameStart == count) {
3!
610
      return Path.of(".");
×
611
    }
612
    Path result = path.getName(nameStart++);
5✔
613
    while (nameStart < count) {
3✔
614
      result = result.resolve(path.getName(nameStart++));
8✔
615
    }
616
    return result;
2✔
617
  }
618

619
  @Override
620
  public int getCommonNameCount(Path path1, Path path2) {
621

622
    if ((path1 == null) || (path2 == null) || !Objects.equals(path1.getRoot(), path2.getRoot())) {
10✔
623
      return -1;
2✔
624
    }
625
    int minNameCount = Math.min(path1.getNameCount(), path2.getNameCount());
6✔
626
    int i = 0;
2✔
627
    while (i < minNameCount) {
3!
628
      Path segment1 = path1.getName(i);
4✔
629
      Path segment2 = path2.getName(i);
4✔
630
      if (!segment1.equals(segment2)) {
4✔
631
        break;
1✔
632
      }
633
      i++;
1✔
634
    }
1✔
635
    return i;
2✔
636
  }
637

638
  @Override
639
  public Path toRealPath(Path path) {
640

641
    return toRealPath(path, true);
5✔
642
  }
643

644
  @Override
645
  public Path toCanonicalPath(Path path) {
646

647
    return toRealPath(path, false);
5✔
648
  }
649

650
  private Path toRealPath(Path path, boolean resolveLinks) {
651

652
    try {
653
      Path realPath;
654
      if (resolveLinks) {
2✔
655
        realPath = path.toRealPath();
6✔
656
      } else {
657
        realPath = path.toRealPath(LinkOption.NOFOLLOW_LINKS);
9✔
658
      }
659
      if (!realPath.equals(path)) {
4✔
660
        LOG.trace("Resolved path {} to {}", path, realPath);
5✔
661
      }
662
      return realPath;
2✔
663
    } catch (IOException e) {
×
664
      throw new IllegalStateException("Failed to get real path for " + path, e);
×
665
    }
666
  }
667

668
  @Override
669
  public Path createTempDir(String name) {
670

671
    try {
672
      Path tmp = this.context.getTempPath();
4✔
673
      Path tempDir = tmp.resolve(name);
4✔
674
      int tries = 1;
2✔
675
      while (Files.exists(tempDir)) {
5!
676
        long id = System.nanoTime() & 0xFFFF;
×
677
        tempDir = tmp.resolve(name + "-" + id);
×
678
        tries++;
×
679
        if (tries > 200) {
×
680
          throw new IOException("Unable to create unique name!");
×
681
        }
682
      }
×
683
      return Files.createDirectory(tempDir);
5✔
684
    } catch (IOException e) {
×
685
      throw new IllegalStateException("Failed to create temporary directory with prefix '" + name + "'!", e);
×
686
    }
687
  }
688

689
  @Override
690
  public void extract(Path archiveFile, Path targetDir, Consumer<Path> postExtractHook, boolean extract) {
691

692
    if (Files.isDirectory(archiveFile)) {
5✔
693
      LOG.warn("Found directory for download at {} hence copying without extraction!", archiveFile);
4✔
694
      copy(archiveFile, targetDir, FileCopyMode.COPY_TREE_CONTENT);
5✔
695
      postExtractHook(postExtractHook, targetDir);
4✔
696
      return;
1✔
697
    } else if (!extract) {
2✔
698
      mkdirs(targetDir);
3✔
699
      move(archiveFile, targetDir.resolve(archiveFile.getFileName()));
9✔
700
      return;
1✔
701
    }
702
    Path tmpDir = createTempDir("extract-" + archiveFile.getFileName());
7✔
703
    LOG.trace("Trying to extract the file {} to {} and move it to {}.", archiveFile, tmpDir, targetDir);
17✔
704
    String filename = archiveFile.getFileName().toString();
4✔
705
    TarCompression tarCompression = TarCompression.of(filename);
3✔
706
    if (tarCompression != null) {
2✔
707
      extractTar(archiveFile, tmpDir, tarCompression);
6✔
708
    } else {
709
      String extension = FilenameUtil.getExtension(filename);
3✔
710
      if (extension == null) {
2!
711
        throw new IllegalStateException("Unknown archive format without extension - can not extract " + archiveFile);
×
712
      } else {
713
        LOG.trace("Determined file extension {}", extension);
4✔
714
      }
715
      switch (extension) {
8!
716
        case "zip" -> extractZip(archiveFile, tmpDir);
×
717
        case "jar" -> extractJar(archiveFile, tmpDir);
5✔
718
        case "dmg" -> extractDmg(archiveFile, tmpDir);
×
719
        case "msi" -> extractMsi(archiveFile, tmpDir);
×
720
        case "pkg" -> extractPkg(archiveFile, tmpDir);
×
721
        default -> throw new IllegalStateException("Unknown archive format " + extension + ". Can not extract " + archiveFile);
×
722
      }
723
    }
724
    Path properInstallDir = getProperInstallationSubDirOf(tmpDir, archiveFile);
5✔
725
    postExtractHook(postExtractHook, properInstallDir);
4✔
726
    move(properInstallDir, targetDir);
6✔
727
    delete(tmpDir);
3✔
728
  }
1✔
729

730
  private void postExtractHook(Consumer<Path> postExtractHook, Path properInstallDir) {
731

732
    if (postExtractHook != null) {
2✔
733
      postExtractHook.accept(properInstallDir);
3✔
734
    }
735
  }
1✔
736

737
  /**
738
   * @param path the {@link Path} to start the recursive search from.
739
   * @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
740
   *     item in their respective directory and {@code s} is not named "bin".
741
   */
742
  private Path getProperInstallationSubDirOf(Path path, Path archiveFile) {
743

744
    try (Stream<Path> stream = Files.list(path)) {
3✔
745
      Path[] subFiles = stream.toArray(Path[]::new);
8✔
746
      if (subFiles.length == 0) {
3!
747
        throw new CliException("The downloaded package " + archiveFile + " seems to be empty as you can check in the extracted folder " + path);
×
748
      } else if (subFiles.length == 1) {
4✔
749
        String filename = subFiles[0].getFileName().toString();
6✔
750
        if (!filename.equals(IdeContext.FOLDER_BIN) && !filename.equals(IdeContext.FOLDER_CONTENTS) && !filename.endsWith(".app") && Files.isDirectory(
19!
751
            subFiles[0])) {
752
          return getProperInstallationSubDirOf(subFiles[0], archiveFile);
9✔
753
        }
754
      }
755
      return path;
4✔
756
    } catch (IOException e) {
4!
757
      throw new IllegalStateException("Failed to get sub-files of " + path);
×
758
    }
759
  }
760

761
  @Override
762
  public void extractZip(Path file, Path targetDir) {
763

764
    LOG.info("Extracting ZIP file {} to {}", file, targetDir);
5✔
765
    extractZipWithJava(file, targetDir);
4✔
766
  }
1✔
767

768
  /**
769
   * Extracts a ZIP archive to the given target directory using Java (commons-compress {@link ZipFile}).
770
   * <p>
771
   * Symlinks are handled in a two-phase approach: all regular files and directories are written first, and symlinks are collected and created afterwards. This
772
   * ensures every symlink target already exists on disk before the link is made.
773
   * <p>
774
   * {@link ZipFile} is used instead of {@link org.apache.commons.compress.archivers.zip.ZipArchiveInputStream} because Unix file attributes (needed for symlink
775
   * detection and permission restoration) are stored in the ZIP central directory at the end of the file. A sequential stream only sees the local file headers,
776
   * where external attributes are always zero. {@link ZipFile} reads the central directory via random access, so attributes are always correct.
777
   *
778
   * @param file the ZIP archive to extract.
779
   * @param targetDir the directory to extract into.
780
   */
781
  private void extractZipWithJava(Path file, Path targetDir) {
782

783
    final List<PathLink> links = new ArrayList<>();
4✔
784
    try (ZipFile zipFile = ZipFile.builder().setPath(file).get();
6✔
785
        IdeProgressBar pb = this.context.newProgressbarForExtracting(getFileSize(file))) {
7✔
786

787
      final Path root = targetDir.toAbsolutePath().normalize();
4✔
788
      Enumeration<ZipArchiveEntry> entries = zipFile.getEntries();
3✔
789

790
      while (entries.hasMoreElements()) {
3✔
791
        ZipArchiveEntry entry = entries.nextElement();
4✔
792
        String entryName = entry.getName();
3✔
793
        Path entryPath = resolveRelativePathSecure(entryName, root);
5✔
794

795
        if (isZipSymlink(entry)) {
3✔
796
          try (InputStream entryStream = zipFile.getInputStream(entry)) {
5✔
797
            // For symlink entries the file content IS the link target path (Unix zip convention).
798
            String linkTarget = IOUtils.toString(entryStream, StandardCharsets.UTF_8);
4✔
799
            Path parent = entryPath.getParent();
3✔
800
            Path source = parent.resolve(linkTarget).normalize();
5✔
801
            resolveRelativePathSecure(source, root, linkTarget);
6✔
802
            links.add(new PathLink(source, entryPath, PathLinkType.SYMBOLIC_LINK));
9✔
803
            mkdirs(parent);
3✔
804
          }
805
        } else if (entry.isDirectory()) {
3✔
806
          mkdirs(entryPath);
4✔
807
        } else {
808
          mkdirs(entryPath.getParent());
4✔
809
          try (InputStream entryStream = zipFile.getInputStream(entry)) {
4✔
810
            Files.copy(entryStream, entryPath, StandardCopyOption.REPLACE_EXISTING);
10✔
811
          }
812
          onFileCopiedFromZip(entry, entryPath);
4✔
813
        }
814
        pb.stepBy(Math.max(0L, entry.getSize()));
6✔
815
      }
1✔
816

817
      // Phase 2: create all symlinks now that their targets are guaranteed to exist.
818
      for (PathLink link : links) {
10✔
819
        link(link);
3✔
820
      }
1✔
821
    } catch (IOException e) {
×
822
      throw new IllegalStateException("Failed to extract " + file + " to " + targetDir, e);
×
823
    }
1✔
824
  }
1✔
825

826
  /**
827
   * Returns the Unix file mode stored in the external file attributes of the given ZIP entry.
828
   * <p>
829
   * The ZIP specification stores platform-specific metadata in a 32-bit external-attributes field. When the archive was created on a Unix system the layout
830
   * is:
831
   * <ul>
832
   *   <li>bits 31–16 (upper 16 bits): Unix file mode (same bit layout as {@code stat.st_mode})</li>
833
   *   <li>bits 15–0 (lower 16 bits): MS-DOS attributes (read-only, hidden, …)</li>
834
   * </ul>
835
   * Shifting right by 16 and masking with {@code 0xFFFF} isolates the Unix mode.
836
   *
837
   * @param entry the ZIP entry to read.
838
   * @return the Unix file mode, or {@code 0} if no Unix attributes are present.
839
   */
840
  private static int getZipUnixMode(ZipArchiveEntry entry) {
841

842
    // Right-shift by 16 to move the upper 16 bits (Unix mode) into the lower 16 bits, then
843
    // mask with 0xFFFF to discard any sign-extended bits introduced by the cast.
844
    return (int) ((entry.getExternalAttributes() >> 16) & 0xFFFF);
8✔
845
  }
846

847
  /**
848
   * Returns {@code true} if the given ZIP entry represents a symbolic link.
849
   * <p>
850
   * Unix file modes encode the file type in the top 4 bits (bits 15–12) of the mode word. The possible file-type values are defined in {@code <sys/stat.h>}:
851
   * <ul>
852
   *   <li>{@code 0x8000} – regular file ({@code S_IFREG})</li>
853
   *   <li>{@code 0x4000} – directory ({@code S_IFDIR})</li>
854
   *   <li>{@code 0xA000} – symbolic link ({@code S_IFLNK})</li>
855
   * </ul>
856
   * Masking with {@code 0xF000} isolates those top 4 bits and comparing against {@code 0xA000} identifies symlinks.
857
   *
858
   * @param entry the ZIP entry to test.
859
   * @return {@code true} if the entry is a symbolic link, {@code false} otherwise.
860
   */
861
  private static boolean isZipSymlink(ZipArchiveEntry entry) {
862

863
    // 0xF000 masks the file-type nibble; 0xA000 is the Unix S_IFLNK constant.
864
    return (getZipUnixMode(entry) & 0xF000) == 0xA000;
10✔
865
  }
866

867
  /**
868
   * Applies Unix file permissions stored in a ZIP entry to the extracted file.
869
   * <p>
870
   * Permissions are only applied on non-Windows systems because POSIX permission bits have no equivalent on Windows. If the entry carries no Unix attributes
871
   * (mode is {@code 0}), the call is a no-op.
872
   *
873
   * @param entry the source ZIP entry carrying the Unix mode.
874
   * @param target the extracted file whose permissions should be updated.
875
   */
876
  private void onFileCopiedFromZip(ZipArchiveEntry entry, Path target) {
877

878
    if (!this.context.getSystemInfo().isWindows()) {
5✔
879
      try {
880
        int unixMode = getZipUnixMode(entry);
3✔
881
        if (unixMode != 0) {
2✔
882
          Files.setPosixFilePermissions(target, PathPermissions.of(unixMode).toPosix());
6✔
883
        }
884
      } catch (Exception e) {
×
885
        LOG.error("Failed to transfer zip permissions for {}", target, e);
×
886
      }
1✔
887
    }
888
  }
1✔
889

890
  @Override
891
  public void extractTar(Path file, Path targetDir, TarCompression compression) {
892

893
    extractArchive(file, targetDir, in -> new TarArchiveInputStream(compression.unpack(in)));
13✔
894
  }
1✔
895

896
  @Override
897
  public void extractJar(Path file, Path targetDir) {
898

899
    extractZip(file, targetDir);
4✔
900
  }
1✔
901

902
  /**
903
   * @param permissions The integer as returned by {@link TarArchiveEntry#getMode()} that represents the file permissions of a file on a Unix file system.
904
   * @return A String representing the file permissions. E.g. "rwxrwxr-x" or "rw-rw-r--"
905
   */
906
  public static String generatePermissionString(int permissions) {
907

908
    // Ensure that only the last 9 bits are considered
909
    permissions &= 0b111111111;
4✔
910

911
    StringBuilder permissionStringBuilder = new StringBuilder("rwxrwxrwx");
5✔
912
    for (int i = 0; i < 9; i++) {
7✔
913
      int mask = 1 << i;
4✔
914
      char currentChar = ((permissions & mask) != 0) ? permissionStringBuilder.charAt(8 - i) : '-';
12✔
915
      permissionStringBuilder.setCharAt(8 - i, currentChar);
6✔
916
    }
917

918
    return permissionStringBuilder.toString();
3✔
919
  }
920

921
  private void extractArchive(Path file, Path targetDir, Function<InputStream, ArchiveInputStream<?>> unpacker) {
922

923
    LOG.info("Extracting TAR file {} to {}", file, targetDir);
5✔
924

925
    final List<PathLink> links = new ArrayList<>();
4✔
926
    try (InputStream is = Files.newInputStream(file);
5✔
927
        ArchiveInputStream<?> ais = unpacker.apply(is);
5✔
928
        IdeProgressBar pb = this.context.newProgressbarForExtracting(getFileSize(file))) {
7✔
929

930
      final Path root = targetDir.toAbsolutePath().normalize();
4✔
931

932
      ArchiveEntry entry = ais.getNextEntry();
3✔
933
      while (entry != null) {
2✔
934
        String entryName = entry.getName();
3✔
935
        Path entryPath = resolveRelativePathSecure(entryName, root);
5✔
936
        PathPermissions permissions = null;
2✔
937
        PathLinkType linkType = null;
2✔
938
        if (entry instanceof TarArchiveEntry tae) {
6!
939
          if (tae.isSymbolicLink()) {
3✔
940
            linkType = PathLinkType.SYMBOLIC_LINK;
3✔
941
          } else if (tae.isLink()) {
3!
942
            linkType = PathLinkType.HARD_LINK;
×
943
          }
944
          if (linkType == null) {
2✔
945
            permissions = PathPermissions.of(tae.getMode());
5✔
946
          } else {
947
            Path parent = entryPath.getParent();
3✔
948
            String sourcePathString = tae.getLinkName();
3✔
949
            Path source = parent.resolve(sourcePathString).normalize();
5✔
950
            source = resolveRelativePathSecure(source, root, sourcePathString);
6✔
951
            links.add(new PathLink(source, entryPath, linkType));
9✔
952
            mkdirs(parent);
3✔
953
          }
954
        }
955
        if (entry.isDirectory()) {
3✔
956
          mkdirs(entryPath);
4✔
957
        } else if (linkType == null) { // regular file
2✔
958
          mkdirs(entryPath.getParent());
4✔
959
          Files.copy(ais, entryPath, StandardCopyOption.REPLACE_EXISTING);
10✔
960
          // POSIX perms on non-Windows
961
          if (permissions != null) {
2!
962
            setFilePermissions(entryPath, permissions, false);
5✔
963
          }
964
        }
965
        pb.stepBy(Math.max(0L, entry.getSize()));
6✔
966
        entry = ais.getNextEntry();
3✔
967
      }
1✔
968
      // post process links
969
      for (PathLink link : links) {
10✔
970
        link(link);
3✔
971
      }
1✔
972
    } catch (Exception e) {
×
973
      throw new IllegalStateException("Failed to extract " + file + " to " + targetDir, e);
×
974
    }
1✔
975
  }
1✔
976

977
  private Path resolveRelativePathSecure(String entryName, Path root) {
978
    // Handle ZIP entries with absolute paths (e.g. "/bin/java") by converting them to relative paths
979
    if (entryName != null && entryName.startsWith("/")) {
6!
980
      entryName = entryName.substring(1);
×
981
    }
982

983
    Path entryPath = root.resolve(entryName).normalize();
5✔
984
    return resolveRelativePathSecure(entryPath, root, entryName);
6✔
985
  }
986

987
  private Path resolveRelativePathSecure(Path entryPath, Path root, String entryName) {
988

989
    if (!entryPath.startsWith(root)) {
4!
990
      throw new IllegalStateException("Preventing path traversal attack from " + entryName + " to " + entryPath + " leaving " + root);
×
991
    }
992
    return entryPath;
2✔
993
  }
994

995
  @Override
996
  public void extractDmg(Path file, Path targetDir) {
997

998
    LOG.info("Extracting DMG file {} to {}", file, targetDir);
×
999
    assert this.context.getSystemInfo().isMac();
×
1000

1001
    Path mountPath = this.context.getIdeHome().resolve(IdeContext.FOLDER_UPDATES).resolve(IdeContext.FOLDER_VOLUME);
×
1002
    mkdirs(mountPath);
×
1003
    ProcessContext pc = this.context.newProcess();
×
1004
    pc.executable("hdiutil");
×
1005
    pc.addArgs("attach", "-quiet", "-nobrowse", "-mountpoint", mountPath, file);
×
1006
    pc.run();
×
1007
    Path appPath = findFirst(mountPath, p -> p.getFileName().toString().endsWith(".app"), false);
×
1008
    if (appPath == null) {
×
1009
      throw new IllegalStateException("Failed to unpack DMG as no MacOS *.app was found in file " + file);
×
1010
    }
1011

1012
    copy(appPath, targetDir, FileCopyMode.COPY_TREE_OVERRIDE_TREE);
×
1013
    pc.addArgs("detach", "-force", mountPath);
×
1014
    pc.run();
×
1015
  }
×
1016

1017
  @Override
1018
  public void extractMsi(Path file, Path targetDir) {
1019

1020
    LOG.info("Extracting MSI file {} to {}", file, targetDir);
×
1021
    this.context.newProcess().executable("msiexec").addArgs("/a", file, "/qn", "TARGETDIR=" + targetDir).run();
×
1022
    // msiexec also creates a copy of the MSI
1023
    Path msiCopy = targetDir.resolve(file.getFileName());
×
1024
    delete(msiCopy);
×
1025
  }
×
1026

1027
  @Override
1028
  public void extractPkg(Path file, Path targetDir) {
1029

1030
    LOG.info("Extracting PKG file {} to {}", file, targetDir);
×
1031
    assert this.context.getSystemInfo().isMac();
×
1032
    Path tmpDirPkg = createTempDir("ide-pkg-");
×
1033
    ProcessContext pc = this.context.newProcess();
×
1034
    pc.executable("xar").addArgs("-C", tmpDirPkg, "-xf", file).run();
×
1035
    Path contentPath = findFirst(tmpDirPkg, p -> p.getFileName().toString().equals("Payload"), true);
×
1036
    extractPkgPayloadWithSystemTar(contentPath, targetDir);
×
1037
    delete(tmpDirPkg);
×
1038
  }
×
1039

1040
  private void extractPkgPayloadWithSystemTar(Path payload, Path targetDir) {
1041

1042
    mkdirs(targetDir);
×
1043
    ProcessContext pc = this.context.newProcess();
×
1044
    pc.executable("/usr/bin/tar");
×
1045
    pc.addArgs("-xf", payload, "-C", targetDir);
×
1046
    pc.run();
×
1047
  }
×
1048

1049
  @Override
1050
  public void compress(Path dir, OutputStream out, String path) {
1051

1052
    String extension = FilenameUtil.getExtension(path);
3✔
1053
    TarCompression tarCompression = TarCompression.of(extension);
3✔
1054
    if (tarCompression != null) {
2!
1055
      compressTar(dir, out, tarCompression);
6✔
1056
    } else if (extension.equals("zip")) {
×
1057
      compressZip(dir, out);
×
1058
    } else {
1059
      throw new IllegalArgumentException("Unsupported extension: " + extension);
×
1060
    }
1061
  }
1✔
1062

1063
  @Override
1064
  public void compressTar(Path dir, OutputStream out, TarCompression tarCompression) {
1065

1066
    switch (tarCompression) {
8!
1067
      case null -> compressTar(dir, out);
×
1068
      case NONE -> compressTar(dir, out);
×
1069
      case GZ -> compressTarGz(dir, out);
5✔
1070
      case BZIP2 -> compressTarBzip2(dir, out);
×
1071
      default -> throw new IllegalArgumentException("Unsupported tar compression: " + tarCompression);
×
1072
    }
1073
  }
1✔
1074

1075
  @Override
1076
  public void compressTarGz(Path dir, OutputStream out) {
1077

1078
    GzipParameters parameters = new GzipParameters();
4✔
1079
    parameters.setModificationTime(0);
3✔
1080
    try (GzipCompressorOutputStream gzOut = new GzipCompressorOutputStream(out, parameters)) {
6✔
1081
      compressTarOrThrow(dir, gzOut);
4✔
1082
    } catch (IOException e) {
×
1083
      throw new IllegalStateException("Failed to compress directory " + dir + " to tar.gz file.", e);
×
1084
    }
1✔
1085
  }
1✔
1086

1087
  @Override
1088
  public void compressTarBzip2(Path dir, OutputStream out) {
1089

1090
    try (BZip2CompressorOutputStream bzip2Out = new BZip2CompressorOutputStream(out)) {
×
1091
      compressTarOrThrow(dir, bzip2Out);
×
1092
    } catch (IOException e) {
×
1093
      throw new IllegalStateException("Failed to compress directory " + dir + " to tar.bz2 file.", e);
×
1094
    }
×
1095
  }
×
1096

1097
  @Override
1098
  public void compressTar(Path dir, OutputStream out) {
1099

1100
    try {
1101
      compressTarOrThrow(dir, out);
×
1102
    } catch (IOException e) {
×
1103
      throw new IllegalStateException("Failed to compress directory " + dir + " to tar file.", e);
×
1104
    }
×
1105
  }
×
1106

1107
  private void compressTarOrThrow(Path dir, OutputStream out) throws IOException {
1108

1109
    try (TarArchiveOutputStream tarOut = new TarArchiveOutputStream(out)) {
5✔
1110
      compressRecursive(dir, tarOut, "");
5✔
1111
      tarOut.finish();
2✔
1112
    }
1113
  }
1✔
1114

1115
  @Override
1116
  public void compressZip(Path dir, OutputStream out) {
1117

1118
    try (ZipArchiveOutputStream zipOut = new ZipArchiveOutputStream(out)) {
5✔
1119
      compressRecursive(dir, zipOut, "");
5✔
1120
      zipOut.finish();
2✔
1121
    } catch (IOException e) {
×
1122
      throw new IllegalStateException("Failed to compress directory " + dir + " to zip file.", e);
×
1123
    }
1✔
1124
  }
1✔
1125

1126
  private <E extends ArchiveEntry> void compressRecursive(Path path, ArchiveOutputStream<E> out, String relativePath) {
1127

1128
    try (Stream<Path> childStream = Files.list(path).sorted()) {
4✔
1129
      Iterator<Path> iterator = childStream.iterator();
3✔
1130
      while (iterator.hasNext()) {
3✔
1131
        Path child = iterator.next();
4✔
1132
        String relativeChildPath = relativePath + "/" + child.getFileName().toString();
6✔
1133
        boolean isDirectory = Files.isDirectory(child);
5✔
1134
        E archiveEntry = out.createArchiveEntry(child, relativeChildPath);
7✔
1135
        FileTime none = FileTime.fromMillis(0);
3✔
1136
        if (archiveEntry instanceof TarArchiveEntry tarEntry) {
6✔
1137
          tarEntry.setCreationTime(none);
3✔
1138
          tarEntry.setModTime(none);
3✔
1139
          tarEntry.setLastAccessTime(none);
3✔
1140
          tarEntry.setLastModifiedTime(none);
3✔
1141
          tarEntry.setUserId(0);
3✔
1142
          tarEntry.setUserName("user");
3✔
1143
          tarEntry.setGroupId(0);
3✔
1144
          tarEntry.setGroupName("group");
3✔
1145
          PathPermissions filePermissions = getFilePermissions(child);
4✔
1146
          tarEntry.setMode(filePermissions.toMode());
4✔
1147
        } else if (archiveEntry instanceof ZipArchiveEntry zipEntry) {
7!
1148
          zipEntry.setCreationTime(none);
4✔
1149
          zipEntry.setLastAccessTime(none);
4✔
1150
          zipEntry.setLastModifiedTime(none);
4✔
1151
          zipEntry.setTime(none);
3✔
1152
        }
1153
        out.putArchiveEntry(archiveEntry);
3✔
1154
        if (!isDirectory) {
2✔
1155
          try (InputStream in = Files.newInputStream(child)) {
5✔
1156
            IOUtils.copy(in, out);
4✔
1157
          }
1158
        }
1159
        out.closeArchiveEntry();
2✔
1160
        if (isDirectory) {
2✔
1161
          compressRecursive(child, out, relativeChildPath);
5✔
1162
        }
1163
      }
1✔
1164
    } catch (IOException e) {
×
1165
      throw new IllegalStateException("Failed to compress " + path, e);
×
1166
    }
1✔
1167
  }
1✔
1168

1169
  @Override
1170
  public void delete(Path path) {
1171

1172
    if (!Files.exists(path, LinkOption.NOFOLLOW_LINKS)) {
9✔
1173
      LOG.trace("Deleting {} skipped as the path does not exist.", path);
4✔
1174
      return;
1✔
1175
    }
1176
    LOG.debug("Deleting {} ...", path);
4✔
1177
    try {
1178
      if (isLink(path)) {
4✔
1179
        deletePath(path);
4✔
1180
      } else {
1181
        deleteRecursive(path);
3✔
1182
      }
1183
    } catch (IOException e) {
×
1184
      throw new IllegalStateException("Failed to delete " + path, e);
×
1185
    }
1✔
1186
  }
1✔
1187

1188
  private void deleteRecursive(Path path) throws IOException {
1189

1190
    if (Files.isSymbolicLink(path) || isJunction(path)) {
7!
1191
      LOG.trace("Deleting link {} ...", path);
4✔
1192
      Files.delete(path);
2✔
1193
      return;
1✔
1194
    }
1195
    if (Files.isDirectory(path, LinkOption.NOFOLLOW_LINKS)) {
9✔
1196
      try (Stream<Path> childStream = Files.list(path)) {
3✔
1197
        Iterator<Path> iterator = childStream.iterator();
3✔
1198
        while (iterator.hasNext()) {
3✔
1199
          Path child = iterator.next();
4✔
1200
          deleteRecursive(child);
3✔
1201
        }
1✔
1202
      }
1203
    }
1204
    deletePath(path);
3✔
1205
  }
1✔
1206

1207
  private boolean isLink(Path path) {
1208

1209
    return Files.isSymbolicLink(path) || isJunction(path);
11!
1210
  }
1211

1212
  private void deletePath(Path path) throws IOException {
1213

1214
    LOG.trace("Deleting {} ...", path);
4✔
1215
    boolean isSetWritable = setWritable(path, true);
5✔
1216
    if (!isSetWritable) {
2✔
1217
      LOG.debug("Couldn't give write access to file: {}", path);
4✔
1218
    }
1219
    Files.delete(path);
2✔
1220
  }
1✔
1221

1222
  @Override
1223
  public Path findFirst(Path dir, Predicate<Path> filter, boolean recursive) {
1224

1225
    try {
1226
      if (!Files.isDirectory(dir)) {
5!
1227
        return null;
×
1228
      }
1229
      return findFirstRecursive(dir, filter, recursive);
6✔
1230
    } catch (IOException e) {
×
1231
      throw new IllegalStateException("Failed to search for file in " + dir, e);
×
1232
    }
1233
  }
1234

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

1237
    List<Path> folders = null;
2✔
1238
    try (Stream<Path> childStream = Files.list(dir)) {
3✔
1239
      Iterator<Path> iterator = childStream.iterator();
3✔
1240
      while (iterator.hasNext()) {
3✔
1241
        Path child = iterator.next();
4✔
1242
        if (filter.test(child)) {
4✔
1243
          return child;
4✔
1244
        } else if (recursive && Files.isDirectory(child)) {
2!
1245
          if (folders == null) {
×
1246
            folders = new ArrayList<>();
×
1247
          }
1248
          folders.add(child);
×
1249
        }
1250
      }
1✔
1251
    }
4!
1252
    if (folders != null) {
2!
1253
      for (Path child : folders) {
×
1254
        Path match = findFirstRecursive(child, filter, recursive);
×
1255
        if (match != null) {
×
1256
          return match;
×
1257
        }
1258
      }
×
1259
    }
1260
    return null;
2✔
1261
  }
1262

1263
  @Override
1264
  public Path findAncestor(Path path, Path baseDir, int subfolderCount) {
1265

1266
    if ((path == null) || (baseDir == null)) {
4!
1267
      LOG.debug("Path should not be null for findAncestor.");
3✔
1268
      return null;
2✔
1269
    }
1270
    if (subfolderCount <= 0) {
2!
1271
      throw new IllegalArgumentException("Subfolder count: " + subfolderCount);
×
1272
    }
1273
    // 1. option relativize
1274
    // 2. recursive getParent
1275
    // 3. loop getParent???
1276
    // 4. getName + getNameCount
1277
    path = path.toAbsolutePath().normalize();
4✔
1278
    baseDir = baseDir.toAbsolutePath().normalize();
4✔
1279
    int directoryNameCount = path.getNameCount();
3✔
1280
    int baseDirNameCount = baseDir.getNameCount();
3✔
1281
    int delta = directoryNameCount - baseDirNameCount - subfolderCount;
6✔
1282
    if (delta < 0) {
2!
1283
      return null;
×
1284
    }
1285
    // ensure directory is a sub-folder of baseDir
1286
    for (int i = 0; i < baseDirNameCount; i++) {
7✔
1287
      if (!path.getName(i).toString().equals(baseDir.getName(i).toString())) {
10✔
1288
        return null;
2✔
1289
      }
1290
    }
1291
    Path result = path;
2✔
1292
    while (delta > 0) {
2✔
1293
      result = result.getParent();
3✔
1294
      delta--;
2✔
1295
    }
1296
    return result;
2✔
1297
  }
1298

1299
  @Override
1300
  public List<Path> listChildrenMapped(Path dir, Function<Path, Path> filter) {
1301

1302
    if (!Files.isDirectory(dir)) {
5✔
1303
      return List.of();
2✔
1304
    }
1305
    List<Path> children = new ArrayList<>();
4✔
1306
    try (Stream<Path> childStream = Files.list(dir)) {
3✔
1307
      Iterator<Path> iterator = childStream.iterator();
3✔
1308
      while (iterator.hasNext()) {
3✔
1309
        Path child = iterator.next();
4✔
1310
        Path filteredChild = filter.apply(child);
5✔
1311
        if (filteredChild != null) {
2✔
1312
          if (filteredChild == child) {
3!
1313
            LOG.trace("Accepted file {}", child);
5✔
1314
          } else {
1315
            LOG.trace("Accepted file {} and mapped to {}", child, filteredChild);
×
1316
          }
1317
          children.add(filteredChild);
5✔
1318
        } else {
1319
          LOG.trace("Ignoring file {} according to filter", child);
4✔
1320
        }
1321
      }
1✔
1322
    } catch (IOException e) {
×
1323
      throw new IllegalStateException("Failed to find children of directory " + dir, e);
×
1324
    }
1✔
1325
    return children;
2✔
1326
  }
1327

1328
  @Override
1329
  public boolean isEmptyDir(Path dir) {
1330

1331
    return listChildren(dir, f -> true).isEmpty();
6✔
1332
  }
1333

1334
  @Override
1335
  public boolean isNonEmptyFile(Path file) {
1336

1337
    if (Files.isRegularFile(file)) {
5✔
1338
      return (getFileSize(file) > 0);
10✔
1339
    }
1340
    return false;
2✔
1341
  }
1342

1343
  private long getFileSize(Path file) {
1344

1345
    try {
1346
      return Files.size(file);
3✔
1347
    } catch (IOException e) {
×
1348
      LOG.warn("Failed to determine size of file {}: {}", file, e.toString(), e);
×
1349
      return 0;
×
1350
    }
1351
  }
1352

1353

1354
  @Override
1355
  public Path findExistingFile(String fileName, List<Path> searchDirs) {
1356

1357
    for (Path dir : searchDirs) {
10!
1358
      Path filePath = dir.resolve(fileName);
4✔
1359
      try {
1360
        if (Files.exists(filePath)) {
5✔
1361
          return filePath;
2✔
1362
        }
1363
      } catch (Exception e) {
×
1364
        throw new IllegalStateException("Unexpected error while checking existence of file " + filePath + " .", e);
×
1365
      }
1✔
1366
    }
1✔
1367
    return null;
×
1368
  }
1369

1370
  @Override
1371
  public boolean setWritable(Path file, boolean writable) {
1372

1373
    if (!Files.exists(file)) {
5✔
1374
      return false;
2✔
1375
    }
1376
    try {
1377
      // POSIX
1378
      PosixFileAttributeView posix = Files.getFileAttributeView(file, PosixFileAttributeView.class);
7✔
1379
      if (posix != null) {
2!
1380
        Set<PosixFilePermission> permissions = new HashSet<>(posix.readAttributes().permissions());
7✔
1381
        boolean changed;
1382
        if (writable) {
2!
1383
          changed = permissions.add(PosixFilePermission.OWNER_WRITE);
5✔
1384
        } else {
1385
          changed = permissions.remove(PosixFilePermission.OWNER_WRITE);
×
1386
        }
1387
        if (changed) {
2!
1388
          posix.setPermissions(permissions);
×
1389
        }
1390
        return true;
2✔
1391
      }
1392

1393
      // Windows
1394
      DosFileAttributeView dos = Files.getFileAttributeView(file, DosFileAttributeView.class);
×
1395
      if (dos != null) {
×
1396
        dos.setReadOnly(!writable);
×
1397
        return true;
×
1398
      }
1399

1400
      LOG.debug("Failed to set writing permission for file {}", file);
×
1401
      return false;
×
1402

1403
    } catch (IOException e) {
×
1404
      LOG.debug("Error occurred when trying to set writing permission for file {}: {}", file, e.toString(), e);
×
1405
      return false;
×
1406
    }
1407
  }
1408

1409
  @Override
1410
  public void makeExecutable(Path path, boolean confirm) {
1411

1412
    if (Files.exists(path)) {
5✔
1413
      if (skipPermissionsIfWindows(path)) {
4!
1414
        return;
×
1415
      }
1416
      PathPermissions existingPermissions = getFilePermissions(path);
4✔
1417
      PathPermissions executablePermissions = existingPermissions.makeExecutable();
3✔
1418
      boolean update = (executablePermissions != existingPermissions);
7✔
1419
      if (update) {
2✔
1420
        if (confirm) {
2!
1421
          boolean yesContinue = this.context.question(
×
1422
              "We want to execute {} but this command seems to lack executable permissions!\n"
1423
                  + "Most probably the tool vendor did forget to add x-flags in the binary release package.\n"
1424
                  + "Before running the command, we suggest to set executable permissions to the file:\n"
1425
                  + "{}\n"
1426
                  + "For security reasons we ask for your confirmation so please check this request.\n"
1427
                  + "Changing permissions from {} to {}.\n"
1428
                  + "Do you confirm to make the command executable before running it?", path.getFileName(), path, existingPermissions, executablePermissions);
×
1429
          if (!yesContinue) {
×
1430
            return;
×
1431
          }
1432
        }
1433
        setFilePermissions(path, executablePermissions, false);
6✔
1434
      } else {
1435
        LOG.trace("Executable flags already present so no need to set them for file {}", path);
4✔
1436
      }
1437
    } else {
1✔
1438
      LOG.warn("Cannot set executable flag on file that does not exist: {}", path);
4✔
1439
    }
1440
  }
1✔
1441

1442
  @Override
1443
  public PathPermissions getFilePermissions(Path path) {
1444

1445
    PathPermissions pathPermissions;
1446
    String info = "";
2✔
1447
    if (SystemInfoImpl.INSTANCE.isWindows()) {
3!
1448
      info = "mocked-";
×
1449
      if (Files.isDirectory(path)) {
×
1450
        pathPermissions = PathPermissions.MODE_RWX_RX_RX;
×
1451
      } else {
1452
        Path parent = path.getParent();
×
1453
        if ((parent != null) && (parent.getFileName().toString().equals("bin"))) {
×
1454
          pathPermissions = PathPermissions.MODE_RWX_RX_RX;
×
1455
        } else {
1456
          pathPermissions = PathPermissions.MODE_RW_R_R;
×
1457
        }
1458
      }
×
1459
    } else {
1460
      Set<PosixFilePermission> permissions;
1461
      try {
1462
        // Read the current file permissions
1463
        permissions = Files.getPosixFilePermissions(path);
5✔
1464
      } catch (IOException e) {
×
1465
        throw new RuntimeException("Failed to get permissions for " + path, e);
×
1466
      }
1✔
1467
      pathPermissions = PathPermissions.of(permissions);
3✔
1468
    }
1469
    LOG.trace("Read {}permissions of {} as {}.", info, path, pathPermissions);
17✔
1470
    return pathPermissions;
2✔
1471
  }
1472

1473
  @Override
1474
  public void setFilePermissions(Path path, PathPermissions permissions, boolean logErrorAndContinue) {
1475

1476
    if (skipPermissionsIfWindows(path)) {
4!
1477
      return;
×
1478
    }
1479
    try {
1480
      LOG.debug("Setting permissions for {} to {}", path, permissions);
5✔
1481
      // Set the new permissions
1482
      Files.setPosixFilePermissions(path, permissions.toPosix());
5✔
1483
    } catch (IOException e) {
×
1484
      String message = "Failed to set permissions to " + permissions + " for path " + path;
×
1485
      if (logErrorAndContinue) {
×
1486
        LOG.warn(message, e);
×
1487
      } else {
1488
        throw new RuntimeException(message, e);
×
1489
      }
1490
    }
1✔
1491
  }
1✔
1492

1493
  private boolean skipPermissionsIfWindows(Path path) {
1494

1495
    if (SystemInfoImpl.INSTANCE.isWindows()) {
3!
1496
      LOG.trace("Windows does not have file permissions hence omitting for {}", path);
×
1497
      return true;
×
1498
    }
1499
    return false;
2✔
1500
  }
1501

1502
  @Override
1503
  public void touch(Path file) {
1504

1505
    if (Files.exists(file)) {
5✔
1506
      try {
1507
        Files.setLastModifiedTime(file, FileTime.fromMillis(System.currentTimeMillis()));
5✔
1508
      } catch (IOException e) {
×
1509
        throw new IllegalStateException("Could not update modification-time of " + file, e);
×
1510
      }
1✔
1511
    } else {
1512
      try {
1513
        Files.createFile(file);
5✔
1514
      } catch (IOException e) {
1✔
1515
        throw new IllegalStateException("Could not create empty file " + file, e);
8✔
1516
      }
1✔
1517
    }
1518
  }
1✔
1519

1520
  @Override
1521
  public String readFileContent(Path file) {
1522

1523
    LOG.trace("Reading content of file from {}", file);
4✔
1524
    if (!Files.exists((file))) {
5✔
1525
      LOG.debug("File {} does not exist", file);
4✔
1526
      return null;
2✔
1527
    }
1528
    try {
1529
      String content = Files.readString(file);
3✔
1530
      LOG.trace("Completed reading {} character(s) from file {}", content.length(), file);
7✔
1531
      return content;
2✔
1532
    } catch (IOException e) {
×
1533
      throw new IllegalStateException("Failed to read file " + file, e);
×
1534
    }
1535
  }
1536

1537
  @Override
1538
  public void writeFileContent(String content, Path file, boolean createParentDir) {
1539

1540
    if (createParentDir) {
2✔
1541
      mkdirs(file.getParent());
4✔
1542
    }
1543
    if (content == null) {
2!
1544
      content = "";
×
1545
    }
1546
    LOG.trace("Writing content with {} character(s) to file {}", content.length(), file);
7✔
1547
    if (Files.exists(file)) {
5✔
1548
      LOG.info("Overriding content of file {}", file);
4✔
1549
    }
1550
    try {
1551
      Files.writeString(file, content);
6✔
1552
      LOG.trace("Wrote content to file {}", file);
4✔
1553
    } catch (IOException e) {
×
1554
      throw new RuntimeException("Failed to write file " + file, e);
×
1555
    }
1✔
1556
  }
1✔
1557

1558
  @Override
1559
  public List<String> readFileLines(Path file) {
1560

1561
    LOG.trace("Reading lines of file from {}", file);
4✔
1562
    if (!Files.exists(file)) {
5✔
1563
      LOG.warn("File {} does not exist", file);
4✔
1564
      return null;
2✔
1565
    }
1566
    try {
1567
      List<String> content = Files.readAllLines(file);
3✔
1568
      LOG.trace("Completed reading {} lines from file {}", content.size(), file);
7✔
1569
      return content;
2✔
1570
    } catch (IOException e) {
×
1571
      throw new IllegalStateException("Failed to read file " + file, e);
×
1572
    }
1573
  }
1574

1575
  @Override
1576
  public void writeFileLines(List<String> content, Path file, boolean createParentDir) {
1577

1578
    if (createParentDir) {
2!
1579
      mkdirs(file.getParent());
×
1580
    }
1581
    if (content == null) {
2!
1582
      content = List.of();
×
1583
    }
1584
    LOG.trace("Writing content with {} lines to file {}", content.size(), file);
7✔
1585
    if (Files.exists(file)) {
5✔
1586
      LOG.debug("Overriding content of file {}", file);
4✔
1587
    }
1588
    try {
1589
      Files.write(file, content);
6✔
1590
      LOG.trace("Wrote lines to file {}", file);
4✔
1591
    } catch (IOException e) {
×
1592
      throw new RuntimeException("Failed to write file " + file, e);
×
1593
    }
1✔
1594
  }
1✔
1595

1596
  @Override
1597
  public void readProperties(Path file, Properties properties) {
1598

1599
    try (Reader reader = Files.newBufferedReader(file)) {
3✔
1600
      properties.load(reader);
3✔
1601
      LOG.debug("Successfully loaded {} properties from {}", properties.size(), file);
7✔
1602
    } catch (IOException e) {
×
1603
      throw new IllegalStateException("Failed to read properties file: " + file, e);
×
1604
    }
1✔
1605
  }
1✔
1606

1607
  @Override
1608
  public void writeProperties(Properties properties, Path file, boolean createParentDir) {
1609

1610
    if (createParentDir) {
2✔
1611
      mkdirs(file.getParent());
4✔
1612
    }
1613
    try (Writer writer = Files.newBufferedWriter(file)) {
5✔
1614
      properties.store(writer, null); // do not get confused - Java still writes a date/time header that cannot be omitted
4✔
1615
      LOG.debug("Successfully saved {} properties to {}", properties.size(), file);
7✔
1616
    } catch (IOException e) {
×
1617
      throw new IllegalStateException("Failed to save properties file during tests.", e);
×
1618
    }
1✔
1619
  }
1✔
1620

1621
  @Override
1622
  public void readIniFile(Path file, IniFile iniFile) {
1623

1624
    if (!Files.exists(file)) {
5✔
1625
      LOG.debug("INI file {} does not exist.", iniFile);
4✔
1626
      return;
1✔
1627
    }
1628
    List<String> iniLines = readFileLines(file);
4✔
1629
    IniSection currentIniSection = iniFile.getInitialSection();
3✔
1630
    for (String line : iniLines) {
10✔
1631
      if (line.trim().startsWith("[")) {
5✔
1632
        currentIniSection = iniFile.getOrCreateSection(line);
5✔
1633
      } else if (line.isBlank() || IniComment.COMMENT_SYMBOLS.contains(line.trim().charAt(0))) {
11✔
1634
        currentIniSection.addComment(line);
4✔
1635
      } else {
1636
        int index = line.indexOf('=');
4✔
1637
        if (index > 0) {
2!
1638
          currentIniSection.setPropertyLine(line);
3✔
1639
        }
1640
      }
1641
    }
1✔
1642
  }
1✔
1643

1644
  @Override
1645
  public void writeIniFile(IniFile iniFile, Path file, boolean createParentDir) {
1646

1647
    String iniString = iniFile.toString();
3✔
1648
    writeFileContent(iniString, file, createParentDir);
5✔
1649
  }
1✔
1650

1651
  @Override
1652
  public Duration getFileAge(Path path) {
1653

1654
    if (Files.exists(path)) {
5✔
1655
      try {
1656
        long currentTime = System.currentTimeMillis();
2✔
1657
        long fileModifiedTime = Files.getLastModifiedTime(path).toMillis();
6✔
1658
        return Duration.ofMillis(currentTime - fileModifiedTime);
5✔
1659
      } catch (IOException e) {
×
1660
        LOG.warn("Could not get modification-time of {}.", path, e);
×
1661
      }
×
1662
    } else {
1663
      LOG.debug("Path {} is missing - skipping modification-time and file age check.", path);
4✔
1664
    }
1665
    return null;
2✔
1666
  }
1667

1668
  @Override
1669
  public boolean isFileAgeRecent(Path path, Duration cacheDuration) {
1670

1671
    Duration age = getFileAge(path);
4✔
1672
    if (age == null) {
2✔
1673
      return false;
2✔
1674
    }
1675
    LOG.debug("The path {} was last updated {} ago and caching duration is {}.", path, age, cacheDuration);
17✔
1676
    return (age.toMillis() <= cacheDuration.toMillis());
10✔
1677
  }
1678
}
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