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

devonfw / IDEasy / 17731367199

15 Sep 2025 11:23AM UTC coverage: 68.544% (-0.08%) from 68.627%
17731367199

Pull #1463

github

web-flow
Merge 36474e12e into 2efad3d2e
Pull Request #1463: #1454: Add ng commandlet

3413 of 5447 branches covered (62.66%)

Branch coverage included in aggregate %.

8901 of 12518 relevant lines covered (71.11%)

3.12 hits per line

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

66.73
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.URI;
11
import java.net.http.HttpClient;
12
import java.net.http.HttpClient.Redirect;
13
import java.net.http.HttpClient.Version;
14
import java.net.http.HttpRequest;
15
import java.net.http.HttpRequest.Builder;
16
import java.net.http.HttpResponse;
17
import java.nio.file.FileSystem;
18
import java.nio.file.FileSystemException;
19
import java.nio.file.FileSystems;
20
import java.nio.file.Files;
21
import java.nio.file.LinkOption;
22
import java.nio.file.NoSuchFileException;
23
import java.nio.file.Path;
24
import java.nio.file.Paths;
25
import java.nio.file.StandardCopyOption;
26
import java.nio.file.attribute.BasicFileAttributes;
27
import java.nio.file.attribute.DosFileAttributeView;
28
import java.nio.file.attribute.FileTime;
29
import java.nio.file.attribute.PosixFileAttributeView;
30
import java.nio.file.attribute.PosixFilePermission;
31
import java.security.DigestInputStream;
32
import java.security.MessageDigest;
33
import java.security.NoSuchAlgorithmException;
34
import java.time.Duration;
35
import java.time.LocalDateTime;
36
import java.util.ArrayList;
37
import java.util.HashSet;
38
import java.util.Iterator;
39
import java.util.List;
40
import java.util.Map;
41
import java.util.Properties;
42
import java.util.Set;
43
import java.util.function.Consumer;
44
import java.util.function.Function;
45
import java.util.function.Predicate;
46
import java.util.stream.Stream;
47

48
import org.apache.commons.compress.archivers.ArchiveEntry;
49
import org.apache.commons.compress.archivers.ArchiveInputStream;
50
import org.apache.commons.compress.archivers.ArchiveOutputStream;
51
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
52
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
53
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
54
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
55
import org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream;
56
import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream;
57
import org.apache.commons.io.IOUtils;
58

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

74
/**
75
 * Implementation of {@link FileAccess}.
76
 */
77
public class FileAccessImpl implements FileAccess {
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 static final int MODE_RWX_RX_RX = 0755;
86
  private static final int MODE_RW_R_R = 0644;
87

88
  private static final Map<String, String> FS_ENV = Map.of("encoding", "UTF-8");
5✔
89

90
  private final IdeContext context;
91

92
  /**
93
   * The constructor.
94
   *
95
   * @param context the {@link IdeContext} to use.
96
   */
97
  public FileAccessImpl(IdeContext context) {
98

99
    super();
2✔
100
    this.context = context;
3✔
101
  }
1✔
102

103
  private HttpClient createHttpClient(String url) {
104

105
    return HttpClient.newBuilder().followRedirects(Redirect.ALWAYS).build();
5✔
106
  }
107

108
  @Override
109
  public void download(String url, Path target) {
110

111
    if (this.context.isOffline()) {
4!
112
      throw CliOfflineException.ofDownloadViaUrl(url);
×
113
    }
114
    if (url.startsWith("http")) {
4✔
115
      downloadViaHttp(url, target);
5✔
116
    } else if (url.startsWith("ftp") || url.startsWith("sftp")) {
8!
117
      throw new IllegalArgumentException("Unsupported download URL: " + url);
×
118
    } else {
119
      Path source = Path.of(url);
5✔
120
      if (isFile(source)) {
4!
121
        // network drive
122
        copyFileWithProgressBar(source, target);
5✔
123
      } else {
124
        throw new IllegalArgumentException("Download path does not point to a downloadable file: " + url);
×
125
      }
126
    }
127
  }
1✔
128

129
  private void downloadViaHttp(String url, Path target) {
130
    List<Version> httpProtocols = IdeVariables.HTTP_VERSIONS.get(context);
6✔
131
    Exception lastException = null;
2✔
132
    if (httpProtocols.isEmpty()) {
3!
133
      try {
134
        this.downloadWithHttpVersion(url, target, null);
5✔
135
        return;
1✔
136
      } catch (Exception e) {
×
137
        lastException = e;
×
138
      }
×
139
    } else {
140
      for (Version version : httpProtocols) {
×
141
        try {
142
          this.downloadWithHttpVersion(url, target, version);
×
143
          return;
×
144
        } catch (Exception ex) {
×
145
          lastException = ex;
×
146
        }
147
      }
×
148
    }
149
    throw new IllegalStateException("Failed to download file from URL " + url + " to " + target, lastException);
×
150
  }
151

152
  private void downloadWithHttpVersion(String url, Path target, Version httpVersion) throws Exception {
153

154
    if (httpVersion == null) {
2!
155
      this.context.info("Trying to download {} from {}", target.getFileName(), url);
16✔
156
    } else {
157
      this.context.info("Trying to download: {} with HTTP protocol version: {}", url, httpVersion);
×
158
    }
159
    mkdirs(target.getParent());
4✔
160

161
    Builder builder = HttpRequest.newBuilder()
2✔
162
        .uri(URI.create(url))
2✔
163
        .GET();
2✔
164
    if (httpVersion != null) {
2!
165
      builder.version(httpVersion);
×
166
    }
167
    try (HttpClient client = createHttpClient(url)) {
4✔
168
      HttpRequest request = builder.build();
3✔
169
      HttpResponse<InputStream> response = client.send(request, HttpResponse.BodyHandlers.ofInputStream());
5✔
170
      int statusCode = response.statusCode();
3✔
171
      if (statusCode == 200) {
3!
172
        downloadFileWithProgressBar(url, target, response);
6✔
173
      } else {
174
        throw new IllegalStateException("Download failed with status code " + statusCode);
×
175
      }
176
    }
177
  }
1✔
178

179
  /**
180
   * Downloads a file while showing a {@link IdeProgressBar}.
181
   *
182
   * @param url the url to download.
183
   * @param target Path of the target directory.
184
   * @param response the {@link HttpResponse} to use.
185
   */
186
  private void downloadFileWithProgressBar(String url, Path target, HttpResponse<InputStream> response) {
187

188
    long contentLength = response.headers().firstValueAsLong("content-length").orElse(-1);
7✔
189
    informAboutMissingContentLength(contentLength, url);
4✔
190

191
    byte[] data = new byte[1024];
3✔
192
    boolean fileComplete = false;
2✔
193
    int count;
194

195
    try (InputStream body = response.body();
4✔
196
        FileOutputStream fileOutput = new FileOutputStream(target.toFile());
6✔
197
        BufferedOutputStream bufferedOut = new BufferedOutputStream(fileOutput, data.length);
7✔
198
        IdeProgressBar pb = this.context.newProgressBarForDownload(contentLength)) {
5✔
199
      while (!fileComplete) {
2✔
200
        count = body.read(data);
4✔
201
        if (count <= 0) {
2✔
202
          fileComplete = true;
3✔
203
        } else {
204
          bufferedOut.write(data, 0, count);
5✔
205
          pb.stepBy(count);
5✔
206
        }
207
      }
208

209
    } catch (Exception e) {
×
210
      throw new RuntimeException(e);
×
211
    }
1✔
212
  }
1✔
213

214
  private void copyFileWithProgressBar(Path source, Path target) {
215

216
    long size = getFileSize(source);
4✔
217
    if (size < 100_000) {
4✔
218
      copy(source, target, FileCopyMode.COPY_FILE_TO_TARGET_OVERRIDE);
5✔
219
      return;
1✔
220
    }
221
    try (InputStream in = Files.newInputStream(source);
5✔
222
        OutputStream out = Files.newOutputStream(target)) {
5✔
223
      byte[] buf = new byte[1024];
3✔
224
      try (IdeProgressBar pb = this.context.newProgressbarForCopying(size)) {
5✔
225
        int readBytes;
226
        while ((readBytes = in.read(buf)) > 0) {
6✔
227
          out.write(buf, 0, readBytes);
5✔
228
          pb.stepBy(readBytes);
5✔
229
        }
230
      } catch (Exception e) {
×
231
        throw new RuntimeException(e);
×
232
      }
1✔
233
    } catch (IOException e) {
×
234
      throw new RuntimeException("Failed to copy from " + source + " to " + target, e);
×
235
    }
1✔
236
  }
1✔
237

238
  private void informAboutMissingContentLength(long contentLength, String url) {
239

240
    if (contentLength < 0) {
4✔
241
      this.context.warning("Content-Length was not provided by download from {}", url);
10✔
242
    }
243
  }
1✔
244

245
  @Override
246
  public void mkdirs(Path directory) {
247

248
    if (Files.isDirectory(directory)) {
5✔
249
      return;
1✔
250
    }
251
    this.context.trace("Creating directory {}", directory);
10✔
252
    try {
253
      Files.createDirectories(directory);
5✔
254
    } catch (IOException e) {
×
255
      throw new IllegalStateException("Failed to create directory " + directory, e);
×
256
    }
1✔
257
  }
1✔
258

259
  @Override
260
  public boolean isFile(Path file) {
261

262
    if (!Files.exists(file)) {
5!
263
      this.context.trace("File {} does not exist", file);
×
264
      return false;
×
265
    }
266
    if (Files.isDirectory(file)) {
5!
267
      this.context.trace("Path {} is a directory but a regular file was expected", file);
×
268
      return false;
×
269
    }
270
    return true;
2✔
271
  }
272

273
  @Override
274
  public boolean isExpectedFolder(Path folder) {
275

276
    if (Files.isDirectory(folder)) {
5✔
277
      return true;
2✔
278
    }
279
    this.context.warning("Expected folder was not found at {}", folder);
10✔
280
    return false;
2✔
281
  }
282

283
  @Override
284
  public String checksum(Path file, String hashAlgorithm) {
285

286
    MessageDigest md;
287
    try {
288
      md = MessageDigest.getInstance(hashAlgorithm);
×
289
    } catch (NoSuchAlgorithmException e) {
×
290
      throw new IllegalStateException("No such hash algorithm " + hashAlgorithm, e);
×
291
    }
×
292
    byte[] buffer = new byte[1024];
×
293
    try (InputStream is = Files.newInputStream(file); DigestInputStream dis = new DigestInputStream(is, md)) {
×
294
      int read = 0;
×
295
      while (read >= 0) {
×
296
        read = dis.read(buffer);
×
297
      }
298
    } catch (Exception e) {
×
299
      throw new IllegalStateException("Failed to read and hash file " + file, e);
×
300
    }
×
301
    byte[] digestBytes = md.digest();
×
302
    return HexUtil.toHexString(digestBytes);
×
303
  }
304

305
  @Override
306
  public boolean isJunction(Path path) {
307

308
    if (!SystemInfoImpl.INSTANCE.isWindows()) {
3!
309
      return false;
2✔
310
    }
311
    try {
312
      BasicFileAttributes attr = Files.readAttributes(path, BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
×
313
      return attr.isOther() && attr.isDirectory();
×
314
    } catch (NoSuchFileException e) {
×
315
      return false; // file doesn't exist
×
316
    } catch (IOException e) {
×
317
      // errors in reading the attributes of the file
318
      throw new IllegalStateException("An unexpected error occurred whilst checking if the file: " + path + " is a junction", e);
×
319
    }
320
  }
321

322
  @Override
323
  public Path backup(Path fileOrFolder) {
324

325
    if ((fileOrFolder != null) && (Files.isSymbolicLink(fileOrFolder) || isJunction(fileOrFolder))) {
9!
326
      delete(fileOrFolder);
4✔
327
    } else if ((fileOrFolder != null) && Files.exists(fileOrFolder)) {
7!
328
      LocalDateTime now = LocalDateTime.now();
2✔
329
      String date = DateTimeUtil.formatDate(now, true);
4✔
330
      String time = DateTimeUtil.formatTime(now);
3✔
331
      String filename = fileOrFolder.getFileName().toString();
4✔
332
      Path backupPath = this.context.getIdeHome().resolve(IdeContext.FOLDER_BACKUPS).resolve(date).resolve(time + "_" + filename);
12✔
333
      backupPath = appendParentPath(backupPath, fileOrFolder.getParent(), 2);
6✔
334
      mkdirs(backupPath);
3✔
335
      Path target = backupPath.resolve(filename);
4✔
336
      this.context.info("Creating backup by moving {} to {}", fileOrFolder, target);
14✔
337
      move(fileOrFolder, target);
6✔
338
      return target;
2✔
339
    } else {
340
      this.context.trace("Backup of {} skipped as the path does not exist.", fileOrFolder);
10✔
341
    }
342
    return fileOrFolder;
2✔
343
  }
344

345
  private static Path appendParentPath(Path path, Path parent, int max) {
346

347
    if ((parent == null) || (max <= 0)) {
4!
348
      return path;
2✔
349
    }
350
    return appendParentPath(path, parent.getParent(), max - 1).resolve(parent.getFileName());
11✔
351
  }
352

353
  @Override
354
  public void move(Path source, Path targetDir, StandardCopyOption... copyOptions) {
355

356
    this.context.trace("Moving {} to {}", source, targetDir);
14✔
357
    try {
358
      Files.move(source, targetDir, copyOptions);
5✔
359
    } catch (IOException e) {
×
360
      String fileType = Files.isSymbolicLink(source) ? "symlink" : isJunction(source) ? "junction" : Files.isDirectory(source) ? "directory" : "file";
×
361
      String message = "Failed to move " + fileType + ": " + source + " to " + targetDir + ".";
×
362
      if (this.context.getSystemInfo().isWindows()) {
×
363
        message = message + "\n" + WINDOWS_FILE_LOCK_WARNING;
×
364
      }
365
      throw new IllegalStateException(message, e);
×
366
    }
1✔
367
  }
1✔
368

369
  @Override
370
  public void copy(Path source, Path target, FileCopyMode mode, PathCopyListener listener) {
371

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

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

416
    if (Files.isDirectory(source)) {
5✔
417
      mkdirs(target);
3✔
418
      try (Stream<Path> childStream = Files.list(source)) {
3✔
419
        Iterator<Path> iterator = childStream.iterator();
3✔
420
        while (iterator.hasNext()) {
3✔
421
          Path child = iterator.next();
4✔
422
          copyRecursive(child, target.resolve(child.getFileName().toString()), mode, listener);
10✔
423
        }
1✔
424
      }
425
      listener.onCopy(source, target, true);
6✔
426
    } else if (Files.exists(source)) {
5!
427
      if (mode.isOverride()) {
3✔
428
        delete(target);
3✔
429
      }
430
      this.context.trace("Starting to {} {} to {}", mode.getOperation(), source, target);
19✔
431
      Files.copy(source, target);
6✔
432
      listener.onCopy(source, target, false);
6✔
433
    } else {
434
      throw new IOException("Path " + source + " does not exist.");
×
435
    }
436
  }
1✔
437

438
  /**
439
   * 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
440
   * {@link Path} that is neither a symbolic link nor a Windows junction.
441
   *
442
   * @param path the {@link Path} to delete.
443
   */
444
  private void deleteLinkIfExists(Path path) {
445

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

449
    assert !(isSymlink && isJunction);
5!
450

451
    if (isJunction || isSymlink) {
4!
452
      this.context.info("Deleting previous " + (isJunction ? "junction" : "symlink") + " at " + path);
9!
453
      try {
454
        Files.delete(path);
2✔
455
      } catch (IOException e) {
×
456
        throw new IllegalStateException("Failed to delete link at " + path, e);
×
457
      }
1✔
458
    }
459
  }
1✔
460

461
  /**
462
   * Adapts the given {@link Path} to be relative or absolute depending on the given {@code relative} flag. Additionally, {@link Path#toRealPath(LinkOption...)}
463
   * is applied to {@code target}.
464
   *
465
   * @param target the {@link Path} the link should point to and that is to be adapted.
466
   * @param link the {@link Path} to the link. It is used to calculate the relative path to the {@code target} if {@code relative} is set to {@code true}.
467
   * @param relative the {@code relative} flag.
468
   * @return the adapted {@link Path}.
469
   * @see #symlink(Path, Path, boolean)
470
   */
471
  private Path adaptPath(Path target, Path link, boolean relative) {
472

473
    if (!target.isAbsolute()) {
3✔
474
      target = link.resolveSibling(target);
4✔
475
    }
476
    try {
477
      target = target.toRealPath(LinkOption.NOFOLLOW_LINKS); // to transform ../d1/../d2 to ../d2
9✔
478
    } catch (IOException e) {
×
479
      throw new RuntimeException("Failed to get real path of " + target, e);
×
480
    }
1✔
481
    if (relative) {
2✔
482
      target = link.getParent().relativize(target);
5✔
483
      // to make relative links like this work: dir/link -> dir
484
      target = (target.toString().isEmpty()) ? Path.of(".") : target;
11✔
485
    }
486
    return target;
2✔
487
  }
488

489
  /**
490
   * Creates a Windows link using mklink at {@code link} pointing to {@code target}.
491
   *
492
   * @param target the {@link Path} the link will point to.
493
   * @param link the {@link Path} where to create the link.
494
   * @param type the {@link PathLinkType}.
495
   */
496
  private void mklinkOnWindows(Path target, Path link, PathLinkType type) {
497

498
    this.context.trace("Creating a Windows link at {} pointing to {}", link, target);
×
499
    ProcessResult result = this.context.newProcess().executable("cmd").addArgs("/c", "mklink", type.getMklinkOption(), link.toString(), target.toString())
×
500
        .run(ProcessMode.DEFAULT);
×
501
    result.failOnError();
×
502
  }
×
503

504
  @Override
505
  public void link(Path target, Path link, boolean relative, PathLinkType type) {
506

507
    final Path finalTarget;
508
    try {
509
      finalTarget = adaptPath(target, link, relative);
6✔
510
    } catch (Exception e) {
×
511
      throw new IllegalStateException("Failed to adapt target (" + target + ") for link (" + link + ") and relative (" + relative + ")", e);
×
512
    }
1✔
513
    String relativeOrAbsolute = finalTarget.isAbsolute() ? "absolute" : "relative";
7✔
514
    this.context.debug("Creating {} {} at {} pointing to {}", relativeOrAbsolute, type, link, finalTarget);
22✔
515
    deleteLinkIfExists(link);
3✔
516
    try {
517
      if (type == PathLinkType.SYMBOLIC_LINK) {
3!
518
        Files.createSymbolicLink(link, finalTarget);
7✔
519
      } else if (type == PathLinkType.HARD_LINK) {
×
520
        Files.createLink(link, finalTarget);
×
521
      } else {
522
        throw new IllegalStateException("" + type);
×
523
      }
524
    } catch (FileSystemException e) {
×
525
      if (SystemInfoImpl.INSTANCE.isWindows()) {
×
526
        this.context.info(
×
527
            "Due to lack of permissions, Microsoft's mklink with junction had to be used to create a Symlink. See\n"
528
                + "https://github.com/devonfw/IDEasy/blob/main/documentation/symlink.adoc for further details. Error was: "
529
                + e.getMessage());
×
530
        mklinkOnWindows(finalTarget, link, type);
×
531
      } else {
532
        throw new RuntimeException(e);
×
533
      }
534
    } catch (IOException e) {
×
535
      throw new IllegalStateException(
×
536
          "Failed to create a " + relativeOrAbsolute + " " + type + " at " + link + " pointing to " + target, e);
537
    }
1✔
538
  }
1✔
539

540
  @Override
541
  public Path toRealPath(Path path) {
542

543
    return toRealPath(path, true);
5✔
544
  }
545

546
  @Override
547
  public Path toCanonicalPath(Path path) {
548

549
    return toRealPath(path, false);
5✔
550
  }
551

552
  private Path toRealPath(Path path, boolean resolveLinks) {
553

554
    try {
555
      Path realPath;
556
      if (resolveLinks) {
2✔
557
        realPath = path.toRealPath();
6✔
558
      } else {
559
        realPath = path.toRealPath(LinkOption.NOFOLLOW_LINKS);
9✔
560
      }
561
      if (!realPath.equals(path)) {
4✔
562
        this.context.trace("Resolved path {} to {}", path, realPath);
14✔
563
      }
564
      return realPath;
2✔
565
    } catch (IOException e) {
×
566
      throw new IllegalStateException("Failed to get real path for " + path, e);
×
567
    }
568
  }
569

570
  @Override
571
  public Path createTempDir(String name) {
572

573
    try {
574
      Path tmp = this.context.getTempPath();
4✔
575
      Path tempDir = tmp.resolve(name);
4✔
576
      int tries = 1;
2✔
577
      while (Files.exists(tempDir)) {
5!
578
        long id = System.nanoTime() & 0xFFFF;
×
579
        tempDir = tmp.resolve(name + "-" + id);
×
580
        tries++;
×
581
        if (tries > 200) {
×
582
          throw new IOException("Unable to create unique name!");
×
583
        }
584
      }
×
585
      return Files.createDirectory(tempDir);
5✔
586
    } catch (IOException e) {
×
587
      throw new IllegalStateException("Failed to create temporary directory with prefix '" + name + "'!", e);
×
588
    }
589
  }
590

591
  @Override
592
  public void extract(Path archiveFile, Path targetDir, Consumer<Path> postExtractHook, boolean extract) {
593

594
    if (Files.isDirectory(archiveFile)) {
5✔
595
      this.context.warning("Found directory for download at {} hence copying without extraction!", archiveFile);
10✔
596
      copy(archiveFile, targetDir, FileCopyMode.COPY_TREE_CONTENT);
5✔
597
      postExtractHook(postExtractHook, targetDir);
4✔
598
      return;
1✔
599
    } else if (!extract) {
2✔
600
      mkdirs(targetDir);
3✔
601
      move(archiveFile, targetDir.resolve(archiveFile.getFileName()));
9✔
602
      return;
1✔
603
    }
604
    Path tmpDir = createTempDir("extract-" + archiveFile.getFileName());
7✔
605
    this.context.trace("Trying to extract the downloaded file {} to {} and move it to {}.", archiveFile, tmpDir, targetDir);
18✔
606
    String filename = archiveFile.getFileName().toString();
4✔
607
    TarCompression tarCompression = TarCompression.of(filename);
3✔
608
    if (tarCompression != null) {
2✔
609
      extractTar(archiveFile, tmpDir, tarCompression);
6✔
610
    } else {
611
      String extension = FilenameUtil.getExtension(filename);
3✔
612
      if (extension == null) {
2!
613
        throw new IllegalStateException("Unknown archive format without extension - can not extract " + archiveFile);
×
614
      } else {
615
        this.context.trace("Determined file extension {}", extension);
10✔
616
      }
617
      switch (extension) {
8!
618
        case "zip" -> extractZip(archiveFile, tmpDir);
×
619
        case "jar" -> extractJar(archiveFile, tmpDir);
5✔
620
        case "dmg" -> extractDmg(archiveFile, tmpDir);
×
621
        case "msi" -> extractMsi(archiveFile, tmpDir);
×
622
        case "pkg" -> extractPkg(archiveFile, tmpDir);
×
623
        default -> throw new IllegalStateException("Unknown archive format " + extension + ". Can not extract " + archiveFile);
×
624
      }
625
    }
626
    Path properInstallDir = getProperInstallationSubDirOf(tmpDir, archiveFile);
5✔
627
    postExtractHook(postExtractHook, properInstallDir);
4✔
628
    move(properInstallDir, targetDir);
6✔
629
    delete(tmpDir);
3✔
630
  }
1✔
631

632
  private void postExtractHook(Consumer<Path> postExtractHook, Path properInstallDir) {
633

634
    if (postExtractHook != null) {
2✔
635
      postExtractHook.accept(properInstallDir);
3✔
636
    }
637
  }
1✔
638

639
  /**
640
   * @param path the {@link Path} to start the recursive search from.
641
   * @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
642
   *     item in their respective directory and {@code s} is not named "bin".
643
   */
644
  private Path getProperInstallationSubDirOf(Path path, Path archiveFile) {
645

646
    try (Stream<Path> stream = Files.list(path)) {
3✔
647
      Path[] subFiles = stream.toArray(Path[]::new);
8✔
648
      if (subFiles.length == 0) {
3!
649
        throw new CliException("The downloaded package " + archiveFile + " seems to be empty as you can check in the extracted folder " + path);
×
650
      } else if (subFiles.length == 1) {
4✔
651
        String filename = subFiles[0].getFileName().toString();
6✔
652
        if (!filename.equals(IdeContext.FOLDER_BIN) && !filename.equals(IdeContext.FOLDER_CONTENTS) && !filename.endsWith(".app") && Files.isDirectory(
4!
653
            subFiles[0])) {
654
          return getProperInstallationSubDirOf(subFiles[0], archiveFile);
×
655
        }
656
      }
657
      return path;
4✔
658
    } catch (IOException e) {
×
659
      throw new IllegalStateException("Failed to get sub-files of " + path);
×
660
    }
661
  }
662

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

666
    this.context.info("Extracting ZIP file {} to {}", file, targetDir);
14✔
667
    URI uri = URI.create("jar:" + file.toUri());
6✔
668
    try (FileSystem fs = FileSystems.newFileSystem(uri, FS_ENV)) {
4✔
669
      long size = 0;
2✔
670
      for (Path root : fs.getRootDirectories()) {
11✔
671
        size += getFileSizeRecursive(root);
6✔
672
      }
1✔
673
      try (final IdeProgressBar progressBar = this.context.newProgressbarForExtracting(size)) {
5✔
674
        for (Path root : fs.getRootDirectories()) {
11✔
675
          copy(root, targetDir, FileCopyMode.EXTRACT, (s, t, d) -> onFileCopiedFromZip(s, t, d, progressBar));
15✔
676
        }
1✔
677
      }
678
    } catch (IOException e) {
×
679
      throw new IllegalStateException("Failed to extract " + file + " to " + targetDir, e);
×
680
    }
1✔
681
  }
1✔
682

683
  @SuppressWarnings("unchecked")
684
  private void onFileCopiedFromZip(Path source, Path target, boolean directory, IdeProgressBar progressBar) {
685

686
    if (directory) {
2✔
687
      return;
1✔
688
    }
689
    if (!context.getSystemInfo().isWindows()) {
5✔
690
      try {
691
        Object attribute = Files.getAttribute(source, "zip:permissions");
6✔
692
        if (attribute instanceof Set<?> permissionSet) {
6✔
693
          Files.setPosixFilePermissions(target, (Set<PosixFilePermission>) permissionSet);
4✔
694
        }
695
      } catch (Exception e) {
×
696
        context.error(e, "Failed to transfer zip permissions for {}", target);
×
697
      }
1✔
698
    }
699
    progressBar.stepBy(getFileSize(target));
5✔
700
  }
1✔
701

702
  @Override
703
  public void extractTar(Path file, Path targetDir, TarCompression compression) {
704

705
    extractArchive(file, targetDir, in -> new TarArchiveInputStream(compression.unpack(in)));
13✔
706
  }
1✔
707

708
  @Override
709
  public void extractJar(Path file, Path targetDir) {
710

711
    extractZip(file, targetDir);
4✔
712
  }
1✔
713

714
  /**
715
   * @param permissions The integer as returned by {@link TarArchiveEntry#getMode()} that represents the file permissions of a file on a Unix file system.
716
   * @return A String representing the file permissions. E.g. "rwxrwxr-x" or "rw-rw-r--"
717
   */
718
  public static String generatePermissionString(int permissions) {
719

720
    // Ensure that only the last 9 bits are considered
721
    permissions &= 0b111111111;
4✔
722

723
    StringBuilder permissionStringBuilder = new StringBuilder("rwxrwxrwx");
5✔
724
    for (int i = 0; i < 9; i++) {
7✔
725
      int mask = 1 << i;
4✔
726
      char currentChar = ((permissions & mask) != 0) ? permissionStringBuilder.charAt(8 - i) : '-';
12✔
727
      permissionStringBuilder.setCharAt(8 - i, currentChar);
6✔
728
    }
729

730
    return permissionStringBuilder.toString();
3✔
731
  }
732

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

735
    this.context.info("Extracting TAR file {} to {}", file, targetDir);
14✔
736

737
    final List<PathLink> links = new ArrayList<>();
4✔
738
    try (InputStream is = Files.newInputStream(file);
5✔
739
        ArchiveInputStream<?> ais = unpacker.apply(is);
5✔
740
        IdeProgressBar pb = this.context.newProgressbarForExtracting(getFileSize(file))) {
7✔
741

742
      final boolean isTar = (ais instanceof TarArchiveInputStream);
3✔
743
      final boolean isWindows = this.context.getSystemInfo().isWindows();
5✔
744
      final Path root = targetDir.toAbsolutePath().normalize();
4✔
745

746
      ArchiveEntry entry = ais.getNextEntry();
3✔
747
      while (entry != null) {
2✔
748
        String entryName = entry.getName();
3✔
749
        Path entryPath = resolveRelativePathSecure(entryName, root);
5✔
750
        PathPermissions permissions = null;
2✔
751
        PathLinkType linkType = null;
2✔
752
        if (entry instanceof TarArchiveEntry tae) {
6!
753
          if (tae.isSymbolicLink()) {
3!
754
            linkType = PathLinkType.SYMBOLIC_LINK;
×
755
          } else if (tae.isLink()) {
3!
756
            linkType = PathLinkType.HARD_LINK;
×
757
          }
758
          if (linkType == null) {
2!
759
            permissions = PathPermissions.of(tae.getMode());
5✔
760
          } else {
761
            Path parent = entryPath.getParent();
×
762
            String linkName = tae.getLinkName();
×
763
            Path linkTarget = parent.resolve(linkName).normalize();
×
764
            Path target = resolveRelativePathSecure(linkTarget, root, linkName);
×
765
            links.add(new PathLink(entryPath, target, linkType));
×
766
            mkdirs(parent);
×
767
          }
768
        }
769

770
        if (entry.isDirectory()) {
3✔
771
          mkdirs(entryPath);
4✔
772
        } else if (linkType == null) { // regular file
2!
773
          mkdirs(entryPath.getParent());
4✔
774
          Files.copy(ais, entryPath, StandardCopyOption.REPLACE_EXISTING);
10✔
775
          // POSIX perms on non-Windows
776
          if (!isWindows && (permissions != null)) {
4!
777
            setFilePermissions(entryPath, permissions, false);
5✔
778
          }
779
        }
780
        pb.stepBy(Math.max(0L, entry.getSize()));
6✔
781
        entry = ais.getNextEntry();
3✔
782
      }
1✔
783
      // post process links
784
      for (PathLink link : links) {
6!
785
        link(link);
×
786
      }
×
787
    } catch (Exception e) {
×
788
      throw new IllegalStateException("Failed to extract " + file + " to " + targetDir, e);
×
789
    }
1✔
790
  }
1✔
791

792
  private Path resolveRelativePathSecure(String entryName, Path root) {
793

794
    Path entryPath = root.resolve(entryName).normalize();
5✔
795
    return resolveRelativePathSecure(entryPath, root, entryName);
6✔
796
  }
797

798
  private Path resolveRelativePathSecure(Path entryPath, Path root, String entryName) {
799

800
    if (!entryPath.startsWith(root)) {
4!
801
      throw new IllegalStateException("Preventing path traversal attack from " + entryName + " to " + entryPath + " leaving " + root);
×
802
    }
803
    return entryPath;
2✔
804
  }
805

806

807
  @Override
808
  public void extractDmg(Path file, Path targetDir) {
809

810
    this.context.info("Extracting DMG file {} to {}", file, targetDir);
×
811
    assert this.context.getSystemInfo().isMac();
×
812

813
    Path mountPath = this.context.getIdeHome().resolve(IdeContext.FOLDER_UPDATES).resolve(IdeContext.FOLDER_VOLUME);
×
814
    mkdirs(mountPath);
×
815
    ProcessContext pc = this.context.newProcess();
×
816
    pc.executable("hdiutil");
×
817
    pc.addArgs("attach", "-quiet", "-nobrowse", "-mountpoint", mountPath, file);
×
818
    pc.run();
×
819
    Path appPath = findFirst(mountPath, p -> p.getFileName().toString().endsWith(".app"), false);
×
820
    if (appPath == null) {
×
821
      throw new IllegalStateException("Failed to unpack DMG as no MacOS *.app was found in file " + file);
×
822
    }
823

824
    copy(appPath, targetDir, FileCopyMode.COPY_TREE_OVERRIDE_TREE);
×
825
    pc.addArgs("detach", "-force", mountPath);
×
826
    pc.run();
×
827
  }
×
828

829
  @Override
830
  public void extractMsi(Path file, Path targetDir) {
831

832
    this.context.info("Extracting MSI file {} to {}", file, targetDir);
×
833
    this.context.newProcess().executable("msiexec").addArgs("/a", file, "/qn", "TARGETDIR=" + targetDir).run();
×
834
    // msiexec also creates a copy of the MSI
835
    Path msiCopy = targetDir.resolve(file.getFileName());
×
836
    delete(msiCopy);
×
837
  }
×
838

839
  @Override
840
  public void extractPkg(Path file, Path targetDir) {
841

842
    this.context.info("Extracting PKG file {} to {}", file, targetDir);
×
843
    Path tmpDirPkg = createTempDir("ide-pkg-");
×
844
    ProcessContext pc = this.context.newProcess();
×
845
    // we might also be able to use cpio from commons-compression instead of external xar...
846
    pc.executable("xar").addArgs("-C", tmpDirPkg, "-xf", file).run();
×
847
    Path contentPath = findFirst(tmpDirPkg, p -> p.getFileName().toString().equals("Payload"), true);
×
848
    extractTar(contentPath, targetDir, TarCompression.GZ);
×
849
    delete(tmpDirPkg);
×
850
  }
×
851

852
  @Override
853
  public void compress(Path dir, OutputStream out, String format) {
854

855
    String extension = FilenameUtil.getExtension(format);
3✔
856
    TarCompression tarCompression = TarCompression.of(extension);
3✔
857
    if (tarCompression != null) {
2!
858
      compressTar(dir, out, tarCompression);
6✔
859
    } else if (extension.equals("zip")) {
×
860
      compressZip(dir, out);
×
861
    } else {
862
      throw new IllegalArgumentException("Unsupported extension: " + extension);
×
863
    }
864
  }
1✔
865

866
  @Override
867
  public void compressTar(Path dir, OutputStream out, TarCompression tarCompression) {
868
    switch (tarCompression) {
8!
869
      case null -> compressTar(dir, out);
×
870
      case NONE -> compressTar(dir, out);
×
871
      case GZ -> compressTarGz(dir, out);
5✔
872
      case BZIP2 -> compressTarBzip2(dir, out);
×
873
      default -> throw new IllegalArgumentException("Unsupported tar compression: " + tarCompression);
×
874
    }
875
  }
1✔
876

877
  @Override
878
  public void compressTarGz(Path dir, OutputStream out) {
879
    try (GzipCompressorOutputStream gzOut = new GzipCompressorOutputStream(out)) {
5✔
880
      compressTarOrThrow(dir, gzOut);
4✔
881
    } catch (IOException e) {
×
882
      throw new IllegalStateException("Failed to compress directory " + dir + " to tar.gz file.", e);
×
883
    }
1✔
884
  }
1✔
885

886
  @Override
887
  public void compressTarBzip2(Path dir, OutputStream out) {
888
    try (BZip2CompressorOutputStream bzip2Out = new BZip2CompressorOutputStream(out)) {
×
889
      compressTarOrThrow(dir, bzip2Out);
×
890
    } catch (IOException e) {
×
891
      throw new IllegalStateException("Failed to compress directory " + dir + " to tar.bz2 file.", e);
×
892
    }
×
893
  }
×
894

895
  @Override
896
  public void compressTar(Path dir, OutputStream out) {
897
    try {
898
      compressTarOrThrow(dir, out);
×
899
    } catch (IOException e) {
×
900
      throw new IllegalStateException("Failed to compress directory " + dir + " to tar file.", e);
×
901
    }
×
902
  }
×
903

904
  private void compressTarOrThrow(Path dir, OutputStream out) throws IOException {
905
    try (TarArchiveOutputStream tarOut = new TarArchiveOutputStream(out)) {
5✔
906
      compressRecursive(dir, tarOut, "");
5✔
907
      tarOut.finish();
2✔
908
    }
909
  }
1✔
910

911
  @Override
912
  public void compressZip(Path dir, OutputStream out) {
913
    try (ZipArchiveOutputStream zipOut = new ZipArchiveOutputStream(out)) {
×
914
      compressRecursive(dir, zipOut, "");
×
915
      zipOut.finish();
×
916
    } catch (IOException e) {
×
917
      throw new IllegalStateException("Failed to compress directory " + dir + " to zip file.", e);
×
918
    }
×
919
  }
×
920

921
  private <E extends ArchiveEntry> void compressRecursive(Path path, ArchiveOutputStream<E> out, String relativePath) {
922
    try (Stream<Path> childStream = Files.list(path)) {
3✔
923
      Iterator<Path> iterator = childStream.iterator();
3✔
924
      while (iterator.hasNext()) {
3✔
925
        Path child = iterator.next();
4✔
926
        String relativeChildPath = relativePath + "/" + child.getFileName().toString();
6✔
927
        boolean isDirectory = Files.isDirectory(child);
5✔
928
        E archiveEntry = out.createArchiveEntry(child, relativeChildPath);
7✔
929
        if (archiveEntry instanceof TarArchiveEntry tarEntry) {
6!
930
          FileTime none = FileTime.fromMillis(0);
3✔
931
          tarEntry.setCreationTime(none);
3✔
932
          tarEntry.setModTime(none);
3✔
933
          tarEntry.setLastAccessTime(none);
3✔
934
          tarEntry.setLastModifiedTime(none);
3✔
935
          tarEntry.setUserId(0);
3✔
936
          tarEntry.setUserName("user");
3✔
937
          tarEntry.setGroupId(0);
3✔
938
          tarEntry.setGroupName("group");
3✔
939
          if (isDirectory) {
2✔
940
            tarEntry.setMode(MODE_RWX_RX_RX);
4✔
941
          } else {
942
            if (relativePath.endsWith("bin")) {
4✔
943
              tarEntry.setMode(MODE_RWX_RX_RX);
4✔
944
            } else {
945
              tarEntry.setMode(MODE_RW_R_R);
3✔
946
            }
947
          }
948
        }
949
        out.putArchiveEntry(archiveEntry);
3✔
950
        if (!isDirectory) {
2✔
951
          try (InputStream in = Files.newInputStream(child)) {
5✔
952
            IOUtils.copy(in, out);
4✔
953
          }
954
        }
955
        out.closeArchiveEntry();
2✔
956
        if (isDirectory) {
2✔
957
          compressRecursive(child, out, relativeChildPath);
5✔
958
        }
959
      }
1✔
960
    } catch (IOException e) {
×
961
      throw new IllegalStateException("Failed to compress " + path, e);
×
962
    }
1✔
963
  }
1✔
964

965
  @Override
966
  public void delete(Path path) {
967

968
    if (!Files.exists(path, LinkOption.NOFOLLOW_LINKS)) {
9✔
969
      this.context.trace("Deleting {} skipped as the path does not exist.", path);
10✔
970
      return;
1✔
971
    }
972
    this.context.debug("Deleting {} ...", path);
10✔
973
    try {
974
      if (Files.isSymbolicLink(path) || isJunction(path)) {
7!
975
        Files.delete(path);
3✔
976
      } else {
977
        deleteRecursive(path);
3✔
978
      }
979
    } catch (IOException e) {
×
980
      throw new IllegalStateException("Failed to delete " + path, e);
×
981
    }
1✔
982
  }
1✔
983

984
  private void deleteRecursive(Path path) throws IOException {
985

986
    if (Files.isDirectory(path)) {
5✔
987
      try (Stream<Path> childStream = Files.list(path)) {
3✔
988
        Iterator<Path> iterator = childStream.iterator();
3✔
989
        while (iterator.hasNext()) {
3✔
990
          Path child = iterator.next();
4✔
991
          deleteRecursive(child);
3✔
992
        }
1✔
993
      }
994
    }
995
    this.context.trace("Deleting {} ...", path);
10✔
996
    boolean isSetWritable = setWritable(path, true);
5✔
997
    if (!isSetWritable) {
2✔
998
      this.context.debug("Couldn't give write access to file: " + path);
6✔
999
    }
1000
    Files.delete(path);
2✔
1001
  }
1✔
1002

1003
  @Override
1004
  public Path findFirst(Path dir, Predicate<Path> filter, boolean recursive) {
1005

1006
    try {
1007
      if (!Files.isDirectory(dir)) {
5✔
1008
        return null;
2✔
1009
      }
1010
      return findFirstRecursive(dir, filter, recursive);
6✔
1011
    } catch (IOException e) {
×
1012
      throw new IllegalStateException("Failed to search for file in " + dir, e);
×
1013
    }
1014
  }
1015

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

1018
    List<Path> folders = null;
2✔
1019
    try (Stream<Path> childStream = Files.list(dir)) {
3✔
1020
      Iterator<Path> iterator = childStream.iterator();
3✔
1021
      while (iterator.hasNext()) {
3✔
1022
        Path child = iterator.next();
4✔
1023
        if (filter.test(child)) {
4✔
1024
          return child;
4✔
1025
        } else if (recursive && Files.isDirectory(child)) {
2!
1026
          if (folders == null) {
×
1027
            folders = new ArrayList<>();
×
1028
          }
1029
          folders.add(child);
×
1030
        }
1031
      }
1✔
1032
    }
4!
1033
    if (folders != null) {
2!
1034
      for (Path child : folders) {
×
1035
        Path match = findFirstRecursive(child, filter, recursive);
×
1036
        if (match != null) {
×
1037
          return match;
×
1038
        }
1039
      }
×
1040
    }
1041
    return null;
2✔
1042
  }
1043

1044
  @Override
1045
  public List<Path> listChildrenMapped(Path dir, Function<Path, Path> filter) {
1046

1047
    if (!Files.isDirectory(dir)) {
5✔
1048
      return List.of();
2✔
1049
    }
1050
    List<Path> children = new ArrayList<>();
4✔
1051
    try (Stream<Path> childStream = Files.list(dir)) {
3✔
1052
      Iterator<Path> iterator = childStream.iterator();
3✔
1053
      while (iterator.hasNext()) {
3✔
1054
        Path child = iterator.next();
4✔
1055
        Path filteredChild = filter.apply(child);
5✔
1056
        if (filteredChild != null) {
2✔
1057
          if (filteredChild == child) {
3!
1058
            this.context.trace("Accepted file {}", child);
11✔
1059
          } else {
1060
            this.context.trace("Accepted file {} and mapped to {}", child, filteredChild);
×
1061
          }
1062
          children.add(filteredChild);
5✔
1063
        } else {
1064
          this.context.trace("Ignoring file {} according to filter", child);
10✔
1065
        }
1066
      }
1✔
1067
    } catch (IOException e) {
×
1068
      throw new IllegalStateException("Failed to find children of directory " + dir, e);
×
1069
    }
1✔
1070
    return children;
2✔
1071
  }
1072

1073
  @Override
1074
  public boolean isEmptyDir(Path dir) {
1075

1076
    return listChildren(dir, f -> true).isEmpty();
8✔
1077
  }
1078

1079
  private long getFileSize(Path file) {
1080

1081
    try {
1082
      return Files.size(file);
3✔
1083
    } catch (IOException e) {
×
1084
      this.context.warning(e.getMessage(), e);
×
1085
      return 0;
×
1086
    }
1087
  }
1088

1089
  private long getFileSizeRecursive(Path path) {
1090

1091
    long size = 0;
2✔
1092
    if (Files.isDirectory(path)) {
5✔
1093
      try (Stream<Path> childStream = Files.list(path)) {
3✔
1094
        Iterator<Path> iterator = childStream.iterator();
3✔
1095
        while (iterator.hasNext()) {
3✔
1096
          Path child = iterator.next();
4✔
1097
          size += getFileSizeRecursive(child);
6✔
1098
        }
1✔
1099
      } catch (IOException e) {
×
1100
        throw new RuntimeException("Failed to iterate children of folder " + path, e);
×
1101
      }
1✔
1102
    } else {
1103
      size += getFileSize(path);
6✔
1104
    }
1105
    return size;
2✔
1106
  }
1107

1108
  @Override
1109
  public Path findExistingFile(String fileName, List<Path> searchDirs) {
1110

1111
    for (Path dir : searchDirs) {
10!
1112
      Path filePath = dir.resolve(fileName);
4✔
1113
      try {
1114
        if (Files.exists(filePath)) {
5✔
1115
          return filePath;
2✔
1116
        }
1117
      } catch (Exception e) {
×
1118
        throw new IllegalStateException("Unexpected error while checking existence of file " + filePath + " .", e);
×
1119
      }
1✔
1120
    }
1✔
1121
    return null;
×
1122
  }
1123

1124
  @Override
1125
  public boolean setWritable(Path file, boolean writable) {
1126
    try {
1127
      // POSIX
1128
      PosixFileAttributeView posix = Files.getFileAttributeView(file, PosixFileAttributeView.class);
7✔
1129
      if (posix != null) {
2!
1130
        Set<PosixFilePermission> permissions = new HashSet<>(posix.readAttributes().permissions());
7✔
1131
        boolean changed;
1132
        if (writable) {
2!
1133
          changed = permissions.add(PosixFilePermission.OWNER_WRITE);
5✔
1134
        } else {
1135
          changed = permissions.remove(PosixFilePermission.OWNER_WRITE);
×
1136
        }
1137
        if (changed) {
2!
1138
          posix.setPermissions(permissions);
×
1139
        }
1140
        return true;
2✔
1141
      }
1142

1143
      // Windows
1144
      DosFileAttributeView dos = Files.getFileAttributeView(file, DosFileAttributeView.class);
×
1145
      if (dos != null) {
×
1146
        dos.setReadOnly(!writable);
×
1147
        return true;
×
1148
      }
1149

1150
      this.context.debug("Failed to set writing permission for file {}", file);
×
1151
      return false;
×
1152

1153
    } catch (IOException e) {
1✔
1154
      this.context.debug("Error occurred when trying to set writing permission for file " + file + ": " + e);
8✔
1155
      return false;
2✔
1156
    }
1157
  }
1158

1159
  @Override
1160
  public void makeExecutable(Path path, boolean confirm) {
1161

1162
    if (Files.exists(path)) {
5✔
1163
      if (skipPermissionsIfWindows(path)) {
4!
1164
        return;
×
1165
      }
1166
      Set<PosixFilePermission> existingPosixPermissions;
1167
      try {
1168
        // Read the current file permissions
1169
        existingPosixPermissions = Files.getPosixFilePermissions(path);
5✔
1170
      } catch (IOException e) {
×
1171
        throw new RuntimeException("Failed to get permissions for " + path, e);
×
1172
      }
1✔
1173

1174
      PathPermissions existingPermissions = PathPermissions.of(existingPosixPermissions);
3✔
1175
      PathPermissions executablePermissions = existingPermissions.makeExecutable();
3✔
1176
      boolean update = (executablePermissions != existingPermissions);
7✔
1177
      if (update) {
2✔
1178
        if (confirm) {
2!
1179
          boolean yesContinue = this.context.question(
×
1180
              "We want to execute {} but this command seems to lack executable permissions!\n"
1181
                  + "Most probably the tool vendor did forgot to add x-flags in the binary release package.\n"
1182
                  + "Before running the command, we suggest to set executable permissions to the file:\n"
1183
                  + "{}\n"
1184
                  + "For security reasons we ask for your confirmation so please check this request.\n"
1185
                  + "Changing permissions from {} to {}.\n"
1186
                  + "Do you confirm to make the command executable before running it?", path.getFileName(), path, existingPermissions, executablePermissions);
×
1187
          if (!yesContinue) {
×
1188
            return;
×
1189
          }
1190
        }
1191
        setFilePermissions(path, executablePermissions, false);
6✔
1192
      } else {
1193
        this.context.trace("Executable flags already present so no need to set them for file {}", path);
10✔
1194
      }
1195
    } else {
1✔
1196
      this.context.warning("Cannot set executable flag on file that does not exist: {}", path);
10✔
1197
    }
1198
  }
1✔
1199

1200
  @Override
1201
  public void setFilePermissions(Path path, PathPermissions permissions, boolean logErrorAndContinue) {
1202

1203
    if (skipPermissionsIfWindows(path)) {
4!
1204
      return;
×
1205
    }
1206
    try {
1207
      this.context.debug("Setting permissions for {} to {}", path, permissions);
14✔
1208
      // Set the new permissions
1209
      Files.setPosixFilePermissions(path, permissions.toPosix());
5✔
1210
    } catch (IOException e) {
×
1211
      if (logErrorAndContinue) {
×
1212
        this.context.warning().log(e, "Failed to set permissions to {} for path {}", permissions, path);
×
1213
      } else {
1214
        throw new RuntimeException("Failed to set permissions to " + permissions + " for path " + path, e);
×
1215
      }
1216
    }
1✔
1217
  }
1✔
1218

1219
  private boolean skipPermissionsIfWindows(Path path) {
1220
    if (SystemInfoImpl.INSTANCE.isWindows()) {
3!
1221
      this.context.trace("Windows does not have file permissions hence omitting for {}", path);
×
1222
      return true;
×
1223
    }
1224
    return false;
2✔
1225
  }
1226

1227
  @Override
1228
  public void touch(Path file) {
1229

1230
    if (Files.exists(file)) {
5✔
1231
      try {
1232
        Files.setLastModifiedTime(file, FileTime.fromMillis(System.currentTimeMillis()));
5✔
1233
      } catch (IOException e) {
×
1234
        throw new IllegalStateException("Could not update modification-time of " + file, e);
×
1235
      }
1✔
1236
    } else {
1237
      try {
1238
        Files.createFile(file);
5✔
1239
      } catch (IOException e) {
1✔
1240
        throw new IllegalStateException("Could not create empty file " + file, e);
8✔
1241
      }
1✔
1242
    }
1243
  }
1✔
1244

1245
  @Override
1246
  public String readFileContent(Path file) {
1247

1248
    this.context.trace("Reading content of file from {}", file);
10✔
1249
    if (!Files.exists((file))) {
5!
1250
      this.context.debug("File {} does not exist", file);
×
1251
      return null;
×
1252
    }
1253
    try {
1254
      String content = Files.readString(file);
3✔
1255
      this.context.trace("Completed reading {} character(s) from file {}", content.length(), file);
16✔
1256
      return content;
2✔
1257
    } catch (IOException e) {
×
1258
      throw new IllegalStateException("Failed to read file " + file, e);
×
1259
    }
1260
  }
1261

1262
  @Override
1263
  public void writeFileContent(String content, Path file, boolean createParentDir) {
1264

1265
    if (createParentDir) {
2✔
1266
      mkdirs(file.getParent());
4✔
1267
    }
1268
    if (content == null) {
2!
1269
      content = "";
×
1270
    }
1271
    this.context.trace("Writing content with {} character(s) to file {}", content.length(), file);
16✔
1272
    if (Files.exists(file)) {
5✔
1273
      this.context.info("Overriding content of file {}", file);
10✔
1274
    }
1275
    try {
1276
      Files.writeString(file, content);
6✔
1277
      this.context.trace("Wrote content to file {}", file);
10✔
1278
    } catch (IOException e) {
×
1279
      throw new RuntimeException("Failed to write file " + file, e);
×
1280
    }
1✔
1281
  }
1✔
1282

1283
  @Override
1284
  public List<String> readFileLines(Path file) {
1285

1286
    this.context.trace("Reading content of file from {}", file);
10✔
1287
    if (!Files.exists(file)) {
5✔
1288
      this.context.warning("File {} does not exist", file);
10✔
1289
      return null;
2✔
1290
    }
1291
    try {
1292
      List<String> content = Files.readAllLines(file);
3✔
1293
      this.context.trace("Completed reading {} lines from file {}", content.size(), file);
16✔
1294
      return content;
2✔
1295
    } catch (IOException e) {
×
1296
      throw new IllegalStateException("Failed to read file " + file, e);
×
1297
    }
1298
  }
1299

1300
  @Override
1301
  public void writeFileLines(List<String> content, Path file, boolean createParentDir) {
1302

1303
    if (createParentDir) {
2!
1304
      mkdirs(file.getParent());
×
1305
    }
1306
    if (content == null) {
2!
1307
      content = List.of();
×
1308
    }
1309
    this.context.trace("Writing content with {} lines to file {}", content.size(), file);
16✔
1310
    if (Files.exists(file)) {
5✔
1311
      this.context.debug("Overriding content of file {}", file);
10✔
1312
    }
1313
    try {
1314
      Files.write(file, content);
6✔
1315
      this.context.trace("Wrote content to file {}", file);
10✔
1316
    } catch (IOException e) {
×
1317
      throw new RuntimeException("Failed to write file " + file, e);
×
1318
    }
1✔
1319
  }
1✔
1320

1321
  @Override
1322
  public void readProperties(Path file, Properties properties) {
1323

1324
    try (Reader reader = Files.newBufferedReader(file)) {
3✔
1325
      properties.load(reader);
3✔
1326
      this.context.debug("Successfully loaded {} properties from {}", properties.size(), file);
16✔
1327
    } catch (IOException e) {
×
1328
      throw new IllegalStateException("Failed to read properties file: " + file, e);
×
1329
    }
1✔
1330
  }
1✔
1331

1332
  @Override
1333
  public void writeProperties(Properties properties, Path file, boolean createParentDir) {
1334

1335
    if (createParentDir) {
2✔
1336
      mkdirs(file.getParent());
4✔
1337
    }
1338
    try (Writer writer = Files.newBufferedWriter(file)) {
5✔
1339
      properties.store(writer, null); // do not get confused - Java still writes a date/time header that cannot be omitted
4✔
1340
      this.context.debug("Successfully saved {} properties to {}", properties.size(), file);
16✔
1341
    } catch (IOException e) {
×
1342
      throw new IllegalStateException("Failed to save properties file during tests.", e);
×
1343
    }
1✔
1344
  }
1✔
1345

1346
  @Override
1347
  public void readIniFile(Path file, IniFile iniFile) {
1348
    if (!Files.exists(file)) {
5!
1349
      this.context.debug("INI file {} does not exist.", iniFile);
×
1350
      return;
×
1351
    }
1352
    List<String> iniLines = readFileLines(file);
4✔
1353
    IniSection currentIniSection = iniFile.getInitialSection();
3✔
1354
    for (String line : iniLines) {
10✔
1355
      if (line.trim().startsWith("[")) {
5✔
1356
        currentIniSection = iniFile.getOrCreateSection(line);
5✔
1357
      } else if (line.isBlank() || IniComment.COMMENT_SYMBOLS.contains(line.trim().charAt(0))) {
11✔
1358
        currentIniSection.addComment(line);
4✔
1359
      } else {
1360
        int index = line.indexOf('=');
4✔
1361
        if (index > 0) {
2!
1362
          currentIniSection.setProperty(line);
3✔
1363
        }
1364
      }
1365
    }
1✔
1366
  }
1✔
1367

1368
  @Override
1369
  public void writeIniFile(IniFile iniFile, Path file, boolean createParentDir) {
1370
    String iniString = iniFile.toString();
3✔
1371
    writeFileContent(iniString, file, createParentDir);
5✔
1372
  }
1✔
1373

1374
  @Override
1375
  public Duration getFileAge(Path path) {
1376
    if (Files.exists(path)) {
5✔
1377
      try {
1378
        long currentTime = System.currentTimeMillis();
2✔
1379
        long fileModifiedTime = Files.getLastModifiedTime(path).toMillis();
6✔
1380
        return Duration.ofMillis(currentTime - fileModifiedTime);
5✔
1381
      } catch (IOException e) {
×
1382
        this.context.warning().log(e, "Could not get modification-time of {}.", path);
×
1383
      }
×
1384
    } else {
1385
      this.context.debug("Path {} is missing - skipping modification-time and file age check.", path);
10✔
1386
    }
1387
    return null;
2✔
1388
  }
1389

1390
  @Override
1391
  public boolean isFileAgeRecent(Path path, Duration cacheDuration) {
1392

1393
    Duration age = getFileAge(path);
4✔
1394
    if (age == null) {
2✔
1395
      return false;
2✔
1396
    }
1397
    context.debug("The path {} was last updated {} ago and caching duration is {}.", path, age, cacheDuration);
18✔
1398
    return (age.toMillis() <= cacheDuration.toMillis());
10✔
1399
  }
1400
}
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