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

devonfw / IDEasy / 23490767965

24 Mar 2026 01:01PM UTC coverage: 70.405% (-0.02%) from 70.429%
23490767965

push

github

web-flow
#1738: Fixed SymLink loop (#1756)

Co-authored-by: Jörg Hohwiller <hohwille@users.noreply.github.com>

4160 of 6512 branches covered (63.88%)

Branch coverage included in aggregate %.

10782 of 14711 relevant lines covered (73.29%)

3.09 hits per line

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

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

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

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

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

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

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

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

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

86
  private final IdeContext context;
87

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

427
  /**
428
   * 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
429
   * {@link Path} that is neither a symbolic link nor a Windows junction.
430
   *
431
   * @param path the {@link Path} to delete.
432
   */
433
  private void deleteLinkIfExists(Path path) {
434

435
    boolean isJunction = isJunction(path); // since broken junctions are not detected by Files.exists()
4✔
436
    boolean isSymlink = Files.exists(path, LinkOption.NOFOLLOW_LINKS) && Files.isSymbolicLink(path);
16!
437

438
    assert !(isSymlink && isJunction);
5!
439

440
    if (isJunction || isSymlink) {
4!
441
      LOG.info("Deleting previous " + (isJunction ? "junction" : "symlink") + " at " + path);
8!
442
      try {
443
        Files.delete(path);
2✔
444
      } catch (IOException e) {
×
445
        throw new IllegalStateException("Failed to delete link at " + path, e);
×
446
      }
1✔
447
    }
448
  }
1✔
449

450
  /**
451
   * Adapts the given {@link Path} to be relative or absolute depending on the given {@code relative} flag. Additionally, {@link Path#toRealPath(LinkOption...)}
452
   * is applied to {@code target}.
453
   *
454
   * @param link the {@link Path} the link should point to and that is to be adapted.
455
   * @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
456
   *     {@code true}.
457
   * @param relative the {@code relative} flag.
458
   * @return the adapted {@link Path}.
459
   * @see #symlink(Path, Path, boolean)
460
   */
461
  private Path adaptPath(Path source, Path link, boolean relative) {
462

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

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

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

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

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

513
  private boolean runMklink(Path source, Path link, Path cwd, String option) {
514

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

530
  @Override
531
  public void link(Path source, Path link, boolean relative, PathLinkType type) {
532

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

568
  @Override
569
  public Path getPathStart(Path path, int nameEnd) {
570

571
    int count = path.getNameCount();
3✔
572
    int delta = count - nameEnd - 1;
6✔
573
    if (delta < 0) {
2!
574
      throw new IllegalArgumentException("Cannot get start before segment " + nameEnd + " from path " + path + " that has only " + count + " segments.");
×
575
    }
576
    Path result = path;
2✔
577
    while (delta > 0) {
2✔
578
      result = result.getParent();
3✔
579
      delta--;
2✔
580
    }
581
    return result;
2✔
582
  }
583

584
  @Override
585
  public Path getPathEnd(Path path, int nameStart) {
586

587
    int count = path.getNameCount();
3✔
588
    if (nameStart > count) {
3!
589
      throw new IllegalArgumentException("Cannot get end after segment " + nameStart + " from path " + path + " that has only " + count + " segments.");
×
590
    } else if (nameStart == count) {
3!
591
      return Path.of(".");
×
592
    }
593
    Path result = path.getName(nameStart++);
5✔
594
    while (nameStart < count) {
3✔
595
      result = result.resolve(path.getName(nameStart++));
8✔
596
    }
597
    return result;
2✔
598
  }
599

600
  @Override
601
  public int getCommonNameCount(Path path1, Path path2) {
602

603
    if ((path1 == null) || (path2 == null) || !Objects.equals(path1.getRoot(), path2.getRoot())) {
10✔
604
      return -1;
2✔
605
    }
606
    int minNameCount = Math.min(path1.getNameCount(), path2.getNameCount());
6✔
607
    int i = 0;
2✔
608
    while (i < minNameCount) {
3!
609
      Path segment1 = path1.getName(i);
4✔
610
      Path segment2 = path2.getName(i);
4✔
611
      if (!segment1.equals(segment2)) {
4✔
612
        break;
1✔
613
      }
614
      i++;
1✔
615
    }
1✔
616
    return i;
2✔
617
  }
618

619
  @Override
620
  public Path toRealPath(Path path) {
621

622
    return toRealPath(path, true);
5✔
623
  }
624

625
  @Override
626
  public Path toCanonicalPath(Path path) {
627

628
    return toRealPath(path, false);
5✔
629
  }
630

631
  private Path toRealPath(Path path, boolean resolveLinks) {
632

633
    try {
634
      Path realPath;
635
      if (resolveLinks) {
2✔
636
        realPath = path.toRealPath();
6✔
637
      } else {
638
        realPath = path.toRealPath(LinkOption.NOFOLLOW_LINKS);
9✔
639
      }
640
      if (!realPath.equals(path)) {
4✔
641
        LOG.trace("Resolved path {} to {}", path, realPath);
5✔
642
      }
643
      return realPath;
2✔
644
    } catch (IOException e) {
×
645
      throw new IllegalStateException("Failed to get real path for " + path, e);
×
646
    }
647
  }
648

649
  @Override
650
  public Path createTempDir(String name) {
651

652
    try {
653
      Path tmp = this.context.getTempPath();
4✔
654
      Path tempDir = tmp.resolve(name);
4✔
655
      int tries = 1;
2✔
656
      while (Files.exists(tempDir)) {
5!
657
        long id = System.nanoTime() & 0xFFFF;
×
658
        tempDir = tmp.resolve(name + "-" + id);
×
659
        tries++;
×
660
        if (tries > 200) {
×
661
          throw new IOException("Unable to create unique name!");
×
662
        }
663
      }
×
664
      return Files.createDirectory(tempDir);
5✔
665
    } catch (IOException e) {
×
666
      throw new IllegalStateException("Failed to create temporary directory with prefix '" + name + "'!", e);
×
667
    }
668
  }
669

670
  @Override
671
  public void extract(Path archiveFile, Path targetDir, Consumer<Path> postExtractHook, boolean extract) {
672

673
    if (Files.isDirectory(archiveFile)) {
5✔
674
      LOG.warn("Found directory for download at {} hence copying without extraction!", archiveFile);
4✔
675
      copy(archiveFile, targetDir, FileCopyMode.COPY_TREE_CONTENT);
5✔
676
      postExtractHook(postExtractHook, targetDir);
4✔
677
      return;
1✔
678
    } else if (!extract) {
2✔
679
      mkdirs(targetDir);
3✔
680
      move(archiveFile, targetDir.resolve(archiveFile.getFileName()));
9✔
681
      return;
1✔
682
    }
683
    Path tmpDir = createTempDir("extract-" + archiveFile.getFileName());
7✔
684
    LOG.trace("Trying to extract the file {} to {} and move it to {}.", archiveFile, tmpDir, targetDir);
17✔
685
    String filename = archiveFile.getFileName().toString();
4✔
686
    TarCompression tarCompression = TarCompression.of(filename);
3✔
687
    if (tarCompression != null) {
2✔
688
      extractTar(archiveFile, tmpDir, tarCompression);
6✔
689
    } else {
690
      String extension = FilenameUtil.getExtension(filename);
3✔
691
      if (extension == null) {
2!
692
        throw new IllegalStateException("Unknown archive format without extension - can not extract " + archiveFile);
×
693
      } else {
694
        LOG.trace("Determined file extension {}", extension);
4✔
695
      }
696
      switch (extension) {
8!
697
        case "zip" -> extractZip(archiveFile, tmpDir);
×
698
        case "jar" -> extractJar(archiveFile, tmpDir);
5✔
699
        case "dmg" -> extractDmg(archiveFile, tmpDir);
×
700
        case "msi" -> extractMsi(archiveFile, tmpDir);
×
701
        case "pkg" -> extractPkg(archiveFile, tmpDir);
×
702
        default -> throw new IllegalStateException("Unknown archive format " + extension + ". Can not extract " + archiveFile);
×
703
      }
704
    }
705
    Path properInstallDir = getProperInstallationSubDirOf(tmpDir, archiveFile);
5✔
706
    postExtractHook(postExtractHook, properInstallDir);
4✔
707
    move(properInstallDir, targetDir);
6✔
708
    delete(tmpDir);
3✔
709
  }
1✔
710

711
  private void postExtractHook(Consumer<Path> postExtractHook, Path properInstallDir) {
712

713
    if (postExtractHook != null) {
2✔
714
      postExtractHook.accept(properInstallDir);
3✔
715
    }
716
  }
1✔
717

718
  /**
719
   * @param path the {@link Path} to start the recursive search from.
720
   * @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
721
   *     item in their respective directory and {@code s} is not named "bin".
722
   */
723
  private Path getProperInstallationSubDirOf(Path path, Path archiveFile) {
724

725
    try (Stream<Path> stream = Files.list(path)) {
3✔
726
      Path[] subFiles = stream.toArray(Path[]::new);
8✔
727
      if (subFiles.length == 0) {
3!
728
        throw new CliException("The downloaded package " + archiveFile + " seems to be empty as you can check in the extracted folder " + path);
×
729
      } else if (subFiles.length == 1) {
4✔
730
        String filename = subFiles[0].getFileName().toString();
6✔
731
        if (!filename.equals(IdeContext.FOLDER_BIN) && !filename.equals(IdeContext.FOLDER_CONTENTS) && !filename.endsWith(".app") && Files.isDirectory(
19!
732
            subFiles[0])) {
733
          return getProperInstallationSubDirOf(subFiles[0], archiveFile);
9✔
734
        }
735
      }
736
      return path;
4✔
737
    } catch (IOException e) {
4!
738
      throw new IllegalStateException("Failed to get sub-files of " + path);
×
739
    }
740
  }
741

742
  @Override
743
  public void extractZip(Path file, Path targetDir) {
744

745
    LOG.info("Extracting ZIP file {} to {}", file, targetDir);
5✔
746
    URI uri = URI.create("jar:" + file.toUri());
6✔
747
    try (FileSystem fs = FileSystems.newFileSystem(uri, FS_ENV)) {
4✔
748
      long size = 0;
2✔
749
      for (Path root : fs.getRootDirectories()) {
11✔
750
        size += getFileSizeRecursive(root);
6✔
751
      }
1✔
752
      try (final IdeProgressBar progressBar = this.context.newProgressbarForExtracting(size)) {
5✔
753
        for (Path root : fs.getRootDirectories()) {
11✔
754
          copy(root, targetDir, FileCopyMode.EXTRACT, (s, t, d) -> onFileCopiedFromZip(s, t, d, progressBar));
15✔
755
        }
1✔
756
      }
757
    } catch (IOException e) {
×
758
      throw new IllegalStateException("Failed to extract " + file + " to " + targetDir, e);
×
759
    }
1✔
760
  }
1✔
761

762
  @SuppressWarnings("unchecked")
763
  private void onFileCopiedFromZip(Path source, Path target, boolean directory, IdeProgressBar progressBar) {
764

765
    if (directory) {
2✔
766
      return;
1✔
767
    }
768
    if (!this.context.getSystemInfo().isWindows()) {
5✔
769
      try {
770
        Object attribute = Files.getAttribute(source, "zip:permissions");
6✔
771
        if (attribute instanceof Set<?> permissionSet) {
6✔
772
          Files.setPosixFilePermissions(target, (Set<PosixFilePermission>) permissionSet);
4✔
773
        }
774
      } catch (Exception e) {
×
775
        LOG.error("Failed to transfer zip permissions for {}", target, e);
×
776
      }
1✔
777
    }
778
    progressBar.stepBy(getFileSize(target));
5✔
779
  }
1✔
780

781
  @Override
782
  public void extractTar(Path file, Path targetDir, TarCompression compression) {
783

784
    extractArchive(file, targetDir, in -> new TarArchiveInputStream(compression.unpack(in)));
13✔
785
  }
1✔
786

787
  @Override
788
  public void extractJar(Path file, Path targetDir) {
789

790
    extractZip(file, targetDir);
4✔
791
  }
1✔
792

793
  /**
794
   * @param permissions The integer as returned by {@link TarArchiveEntry#getMode()} that represents the file permissions of a file on a Unix file system.
795
   * @return A String representing the file permissions. E.g. "rwxrwxr-x" or "rw-rw-r--"
796
   */
797
  public static String generatePermissionString(int permissions) {
798

799
    // Ensure that only the last 9 bits are considered
800
    permissions &= 0b111111111;
4✔
801

802
    StringBuilder permissionStringBuilder = new StringBuilder("rwxrwxrwx");
5✔
803
    for (int i = 0; i < 9; i++) {
7✔
804
      int mask = 1 << i;
4✔
805
      char currentChar = ((permissions & mask) != 0) ? permissionStringBuilder.charAt(8 - i) : '-';
12✔
806
      permissionStringBuilder.setCharAt(8 - i, currentChar);
6✔
807
    }
808

809
    return permissionStringBuilder.toString();
3✔
810
  }
811

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

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

816
    final List<PathLink> links = new ArrayList<>();
4✔
817
    try (InputStream is = Files.newInputStream(file);
5✔
818
        ArchiveInputStream<?> ais = unpacker.apply(is);
5✔
819
        IdeProgressBar pb = this.context.newProgressbarForExtracting(getFileSize(file))) {
7✔
820

821
      final Path root = targetDir.toAbsolutePath().normalize();
4✔
822

823
      ArchiveEntry entry = ais.getNextEntry();
3✔
824
      while (entry != null) {
2✔
825
        String entryName = entry.getName();
3✔
826
        Path entryPath = resolveRelativePathSecure(entryName, root);
5✔
827
        PathPermissions permissions = null;
2✔
828
        PathLinkType linkType = null;
2✔
829
        if (entry instanceof TarArchiveEntry tae) {
6!
830
          if (tae.isSymbolicLink()) {
3✔
831
            linkType = PathLinkType.SYMBOLIC_LINK;
3✔
832
          } else if (tae.isLink()) {
3!
833
            linkType = PathLinkType.HARD_LINK;
×
834
          }
835
          if (linkType == null) {
2✔
836
            permissions = PathPermissions.of(tae.getMode());
5✔
837
          } else {
838
            Path parent = entryPath.getParent();
3✔
839
            String sourcePathString = tae.getLinkName();
3✔
840
            Path source = parent.resolve(sourcePathString).normalize();
5✔
841
            source = resolveRelativePathSecure(source, root, sourcePathString);
6✔
842
            links.add(new PathLink(source, entryPath, linkType));
9✔
843
            mkdirs(parent);
3✔
844
          }
845
        }
846
        if (entry.isDirectory()) {
3✔
847
          mkdirs(entryPath);
4✔
848
        } else if (linkType == null) { // regular file
2✔
849
          mkdirs(entryPath.getParent());
4✔
850
          Files.copy(ais, entryPath, StandardCopyOption.REPLACE_EXISTING);
10✔
851
          // POSIX perms on non-Windows
852
          if (permissions != null) {
2!
853
            setFilePermissions(entryPath, permissions, false);
5✔
854
          }
855
        }
856
        pb.stepBy(Math.max(0L, entry.getSize()));
6✔
857
        entry = ais.getNextEntry();
3✔
858
      }
1✔
859
      // post process links
860
      for (PathLink link : links) {
10✔
861
        link(link);
3✔
862
      }
1✔
863
    } catch (Exception e) {
×
864
      throw new IllegalStateException("Failed to extract " + file + " to " + targetDir, e);
×
865
    }
1✔
866
  }
1✔
867

868
  private Path resolveRelativePathSecure(String entryName, Path root) {
869

870
    Path entryPath = root.resolve(entryName).normalize();
5✔
871
    return resolveRelativePathSecure(entryPath, root, entryName);
6✔
872
  }
873

874
  private Path resolveRelativePathSecure(Path entryPath, Path root, String entryName) {
875

876
    if (!entryPath.startsWith(root)) {
4!
877
      throw new IllegalStateException("Preventing path traversal attack from " + entryName + " to " + entryPath + " leaving " + root);
×
878
    }
879
    return entryPath;
2✔
880
  }
881

882
  @Override
883
  public void extractDmg(Path file, Path targetDir) {
884

885
    LOG.info("Extracting DMG file {} to {}", file, targetDir);
×
886
    assert this.context.getSystemInfo().isMac();
×
887

888
    Path mountPath = this.context.getIdeHome().resolve(IdeContext.FOLDER_UPDATES).resolve(IdeContext.FOLDER_VOLUME);
×
889
    mkdirs(mountPath);
×
890
    ProcessContext pc = this.context.newProcess();
×
891
    pc.executable("hdiutil");
×
892
    pc.addArgs("attach", "-quiet", "-nobrowse", "-mountpoint", mountPath, file);
×
893
    pc.run();
×
894
    Path appPath = findFirst(mountPath, p -> p.getFileName().toString().endsWith(".app"), false);
×
895
    if (appPath == null) {
×
896
      throw new IllegalStateException("Failed to unpack DMG as no MacOS *.app was found in file " + file);
×
897
    }
898

899
    copy(appPath, targetDir, FileCopyMode.COPY_TREE_OVERRIDE_TREE);
×
900
    pc.addArgs("detach", "-force", mountPath);
×
901
    pc.run();
×
902
  }
×
903

904
  @Override
905
  public void extractMsi(Path file, Path targetDir) {
906

907
    LOG.info("Extracting MSI file {} to {}", file, targetDir);
×
908
    this.context.newProcess().executable("msiexec").addArgs("/a", file, "/qn", "TARGETDIR=" + targetDir).run();
×
909
    // msiexec also creates a copy of the MSI
910
    Path msiCopy = targetDir.resolve(file.getFileName());
×
911
    delete(msiCopy);
×
912
  }
×
913

914
  @Override
915
  public void extractPkg(Path file, Path targetDir) {
916

917
    LOG.info("Extracting PKG file {} to {}", file, targetDir);
×
918
    Path tmpDirPkg = createTempDir("ide-pkg-");
×
919
    ProcessContext pc = this.context.newProcess();
×
920
    // we might also be able to use cpio from commons-compression instead of external xar...
921
    pc.executable("xar").addArgs("-C", tmpDirPkg, "-xf", file).run();
×
922
    Path contentPath = findFirst(tmpDirPkg, p -> p.getFileName().toString().equals("Payload"), true);
×
923
    extractTar(contentPath, targetDir, TarCompression.GZ);
×
924
    delete(tmpDirPkg);
×
925
  }
×
926

927
  @Override
928
  public void compress(Path dir, OutputStream out, String path) {
929

930
    String extension = FilenameUtil.getExtension(path);
3✔
931
    TarCompression tarCompression = TarCompression.of(extension);
3✔
932
    if (tarCompression != null) {
2!
933
      compressTar(dir, out, tarCompression);
6✔
934
    } else if (extension.equals("zip")) {
×
935
      compressZip(dir, out);
×
936
    } else {
937
      throw new IllegalArgumentException("Unsupported extension: " + extension);
×
938
    }
939
  }
1✔
940

941
  @Override
942
  public void compressTar(Path dir, OutputStream out, TarCompression tarCompression) {
943

944
    switch (tarCompression) {
8!
945
      case null -> compressTar(dir, out);
×
946
      case NONE -> compressTar(dir, out);
×
947
      case GZ -> compressTarGz(dir, out);
5✔
948
      case BZIP2 -> compressTarBzip2(dir, out);
×
949
      default -> throw new IllegalArgumentException("Unsupported tar compression: " + tarCompression);
×
950
    }
951
  }
1✔
952

953
  @Override
954
  public void compressTarGz(Path dir, OutputStream out) {
955

956
    try (GzipCompressorOutputStream gzOut = new GzipCompressorOutputStream(out)) {
5✔
957
      compressTarOrThrow(dir, gzOut);
4✔
958
    } catch (IOException e) {
×
959
      throw new IllegalStateException("Failed to compress directory " + dir + " to tar.gz file.", e);
×
960
    }
1✔
961
  }
1✔
962

963
  @Override
964
  public void compressTarBzip2(Path dir, OutputStream out) {
965

966
    try (BZip2CompressorOutputStream bzip2Out = new BZip2CompressorOutputStream(out)) {
×
967
      compressTarOrThrow(dir, bzip2Out);
×
968
    } catch (IOException e) {
×
969
      throw new IllegalStateException("Failed to compress directory " + dir + " to tar.bz2 file.", e);
×
970
    }
×
971
  }
×
972

973
  @Override
974
  public void compressTar(Path dir, OutputStream out) {
975

976
    try {
977
      compressTarOrThrow(dir, out);
×
978
    } catch (IOException e) {
×
979
      throw new IllegalStateException("Failed to compress directory " + dir + " to tar file.", e);
×
980
    }
×
981
  }
×
982

983
  private void compressTarOrThrow(Path dir, OutputStream out) throws IOException {
984

985
    try (TarArchiveOutputStream tarOut = new TarArchiveOutputStream(out)) {
5✔
986
      compressRecursive(dir, tarOut, "");
5✔
987
      tarOut.finish();
2✔
988
    }
989
  }
1✔
990

991
  @Override
992
  public void compressZip(Path dir, OutputStream out) {
993

994
    try (ZipArchiveOutputStream zipOut = new ZipArchiveOutputStream(out)) {
×
995
      compressRecursive(dir, zipOut, "");
×
996
      zipOut.finish();
×
997
    } catch (IOException e) {
×
998
      throw new IllegalStateException("Failed to compress directory " + dir + " to zip file.", e);
×
999
    }
×
1000
  }
×
1001

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

1004
    try (Stream<Path> childStream = Files.list(path)) {
3✔
1005
      Iterator<Path> iterator = childStream.iterator();
3✔
1006
      while (iterator.hasNext()) {
3✔
1007
        Path child = iterator.next();
4✔
1008
        String relativeChildPath = relativePath + "/" + child.getFileName().toString();
6✔
1009
        boolean isDirectory = Files.isDirectory(child);
5✔
1010
        E archiveEntry = out.createArchiveEntry(child, relativeChildPath);
7✔
1011
        if (archiveEntry instanceof TarArchiveEntry tarEntry) {
6!
1012
          FileTime none = FileTime.fromMillis(0);
3✔
1013
          tarEntry.setCreationTime(none);
3✔
1014
          tarEntry.setModTime(none);
3✔
1015
          tarEntry.setLastAccessTime(none);
3✔
1016
          tarEntry.setLastModifiedTime(none);
3✔
1017
          tarEntry.setUserId(0);
3✔
1018
          tarEntry.setUserName("user");
3✔
1019
          tarEntry.setGroupId(0);
3✔
1020
          tarEntry.setGroupName("group");
3✔
1021
          PathPermissions filePermissions = getFilePermissions(child);
4✔
1022
          tarEntry.setMode(filePermissions.toMode());
4✔
1023
        }
1024
        out.putArchiveEntry(archiveEntry);
3✔
1025
        if (!isDirectory) {
2✔
1026
          try (InputStream in = Files.newInputStream(child)) {
5✔
1027
            IOUtils.copy(in, out);
4✔
1028
          }
1029
        }
1030
        out.closeArchiveEntry();
2✔
1031
        if (isDirectory) {
2✔
1032
          compressRecursive(child, out, relativeChildPath);
5✔
1033
        }
1034
      }
1✔
1035
    } catch (IOException e) {
×
1036
      throw new IllegalStateException("Failed to compress " + path, e);
×
1037
    }
1✔
1038
  }
1✔
1039

1040
  @Override
1041
  public void delete(Path path) {
1042

1043
    if (!Files.exists(path, LinkOption.NOFOLLOW_LINKS)) {
9✔
1044
      LOG.trace("Deleting {} skipped as the path does not exist.", path);
4✔
1045
      return;
1✔
1046
    }
1047
    LOG.debug("Deleting {} ...", path);
4✔
1048
    try {
1049
      if (isLink(path)) {
4✔
1050
        deletePath(path);
4✔
1051
      } else {
1052
        deleteRecursive(path);
3✔
1053
      }
1054
    } catch (IOException e) {
×
1055
      throw new IllegalStateException("Failed to delete " + path, e);
×
1056
    }
1✔
1057
  }
1✔
1058

1059
  private void deleteRecursive(Path path) throws IOException {
1060

1061
    if (Files.isSymbolicLink(path) || isJunction(path)) {
7!
1062
      LOG.trace("Deleting link {} ...", path);
4✔
1063
      Files.delete(path);
2✔
1064
      return;
1✔
1065
    }
1066
    if (Files.isDirectory(path, LinkOption.NOFOLLOW_LINKS)) {
9✔
1067
      try (Stream<Path> childStream = Files.list(path)) {
3✔
1068
        Iterator<Path> iterator = childStream.iterator();
3✔
1069
        while (iterator.hasNext()) {
3✔
1070
          Path child = iterator.next();
4✔
1071
          deleteRecursive(child);
3✔
1072
        }
1✔
1073
      }
1074
    }
1075
    deletePath(path);
3✔
1076
  }
1✔
1077

1078
  private boolean isLink(Path path) {
1079

1080
    return Files.isSymbolicLink(path) || isJunction(path);
11!
1081
  }
1082

1083
  private void deletePath(Path path) throws IOException {
1084

1085
    LOG.trace("Deleting {} ...", path);
4✔
1086
    boolean isSetWritable = setWritable(path, true);
5✔
1087
    if (!isSetWritable) {
2✔
1088
      LOG.debug("Couldn't give write access to file: {}", path);
4✔
1089
    }
1090
    Files.delete(path);
2✔
1091
  }
1✔
1092

1093
  @Override
1094
  public Path findFirst(Path dir, Predicate<Path> filter, boolean recursive) {
1095

1096
    try {
1097
      if (!Files.isDirectory(dir)) {
5!
1098
        return null;
×
1099
      }
1100
      return findFirstRecursive(dir, filter, recursive);
6✔
1101
    } catch (IOException e) {
×
1102
      throw new IllegalStateException("Failed to search for file in " + dir, e);
×
1103
    }
1104
  }
1105

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

1108
    List<Path> folders = null;
2✔
1109
    try (Stream<Path> childStream = Files.list(dir)) {
3✔
1110
      Iterator<Path> iterator = childStream.iterator();
3✔
1111
      while (iterator.hasNext()) {
3✔
1112
        Path child = iterator.next();
4✔
1113
        if (filter.test(child)) {
4✔
1114
          return child;
4✔
1115
        } else if (recursive && Files.isDirectory(child)) {
2!
1116
          if (folders == null) {
×
1117
            folders = new ArrayList<>();
×
1118
          }
1119
          folders.add(child);
×
1120
        }
1121
      }
1✔
1122
    }
4!
1123
    if (folders != null) {
2!
1124
      for (Path child : folders) {
×
1125
        Path match = findFirstRecursive(child, filter, recursive);
×
1126
        if (match != null) {
×
1127
          return match;
×
1128
        }
1129
      }
×
1130
    }
1131
    return null;
2✔
1132
  }
1133

1134
  @Override
1135
  public Path findAncestor(Path path, Path baseDir, int subfolderCount) {
1136

1137
    if ((path == null) || (baseDir == null)) {
4!
1138
      LOG.debug("Path should not be null for findAncestor.");
3✔
1139
      return null;
2✔
1140
    }
1141
    if (subfolderCount <= 0) {
2!
1142
      throw new IllegalArgumentException("Subfolder count: " + subfolderCount);
×
1143
    }
1144
    // 1. option relativize
1145
    // 2. recursive getParent
1146
    // 3. loop getParent???
1147
    // 4. getName + getNameCount
1148
    path = path.toAbsolutePath().normalize();
4✔
1149
    baseDir = baseDir.toAbsolutePath().normalize();
4✔
1150
    int directoryNameCount = path.getNameCount();
3✔
1151
    int baseDirNameCount = baseDir.getNameCount();
3✔
1152
    int delta = directoryNameCount - baseDirNameCount - subfolderCount;
6✔
1153
    if (delta < 0) {
2!
1154
      return null;
×
1155
    }
1156
    // ensure directory is a sub-folder of baseDir
1157
    for (int i = 0; i < baseDirNameCount; i++) {
7✔
1158
      if (!path.getName(i).toString().equals(baseDir.getName(i).toString())) {
10✔
1159
        return null;
2✔
1160
      }
1161
    }
1162
    Path result = path;
2✔
1163
    while (delta > 0) {
2✔
1164
      result = result.getParent();
3✔
1165
      delta--;
2✔
1166
    }
1167
    return result;
2✔
1168
  }
1169

1170
  @Override
1171
  public List<Path> listChildrenMapped(Path dir, Function<Path, Path> filter) {
1172

1173
    if (!Files.isDirectory(dir)) {
5✔
1174
      return List.of();
2✔
1175
    }
1176
    List<Path> children = new ArrayList<>();
4✔
1177
    try (Stream<Path> childStream = Files.list(dir)) {
3✔
1178
      Iterator<Path> iterator = childStream.iterator();
3✔
1179
      while (iterator.hasNext()) {
3✔
1180
        Path child = iterator.next();
4✔
1181
        Path filteredChild = filter.apply(child);
5✔
1182
        if (filteredChild != null) {
2✔
1183
          if (filteredChild == child) {
3!
1184
            LOG.trace("Accepted file {}", child);
5✔
1185
          } else {
1186
            LOG.trace("Accepted file {} and mapped to {}", child, filteredChild);
×
1187
          }
1188
          children.add(filteredChild);
5✔
1189
        } else {
1190
          LOG.trace("Ignoring file {} according to filter", child);
4✔
1191
        }
1192
      }
1✔
1193
    } catch (IOException e) {
×
1194
      throw new IllegalStateException("Failed to find children of directory " + dir, e);
×
1195
    }
1✔
1196
    return children;
2✔
1197
  }
1198

1199
  @Override
1200
  public boolean isEmptyDir(Path dir) {
1201

1202
    return listChildren(dir, f -> true).isEmpty();
8✔
1203
  }
1204

1205
  @Override
1206
  public boolean isNonEmptyFile(Path file) {
1207

1208
    if (Files.isRegularFile(file)) {
5✔
1209
      return (getFileSize(file) > 0);
10✔
1210
    }
1211
    return false;
2✔
1212
  }
1213

1214
  private long getFileSize(Path file) {
1215

1216
    try {
1217
      return Files.size(file);
3✔
1218
    } catch (IOException e) {
×
1219
      LOG.warn("Failed to determine size of file {}: {}", file, e.toString(), e);
×
1220
      return 0;
×
1221
    }
1222
  }
1223

1224
  private long getFileSizeRecursive(Path path) {
1225

1226
    long size = 0;
2✔
1227
    if (Files.isDirectory(path)) {
5✔
1228
      try (Stream<Path> childStream = Files.list(path)) {
3✔
1229
        Iterator<Path> iterator = childStream.iterator();
3✔
1230
        while (iterator.hasNext()) {
3✔
1231
          Path child = iterator.next();
4✔
1232
          size += getFileSizeRecursive(child);
6✔
1233
        }
1✔
1234
      } catch (IOException e) {
×
1235
        throw new RuntimeException("Failed to iterate children of folder " + path, e);
×
1236
      }
1✔
1237
    } else {
1238
      size += getFileSize(path);
6✔
1239
    }
1240
    return size;
2✔
1241
  }
1242

1243
  @Override
1244
  public Path findExistingFile(String fileName, List<Path> searchDirs) {
1245

1246
    for (Path dir : searchDirs) {
10!
1247
      Path filePath = dir.resolve(fileName);
4✔
1248
      try {
1249
        if (Files.exists(filePath)) {
5✔
1250
          return filePath;
2✔
1251
        }
1252
      } catch (Exception e) {
×
1253
        throw new IllegalStateException("Unexpected error while checking existence of file " + filePath + " .", e);
×
1254
      }
1✔
1255
    }
1✔
1256
    return null;
×
1257
  }
1258

1259
  @Override
1260
  public boolean setWritable(Path file, boolean writable) {
1261

1262
    if (!Files.exists(file)) {
5✔
1263
      return false;
2✔
1264
    }
1265
    try {
1266
      // POSIX
1267
      PosixFileAttributeView posix = Files.getFileAttributeView(file, PosixFileAttributeView.class);
7✔
1268
      if (posix != null) {
2!
1269
        Set<PosixFilePermission> permissions = new HashSet<>(posix.readAttributes().permissions());
7✔
1270
        boolean changed;
1271
        if (writable) {
2!
1272
          changed = permissions.add(PosixFilePermission.OWNER_WRITE);
5✔
1273
        } else {
1274
          changed = permissions.remove(PosixFilePermission.OWNER_WRITE);
×
1275
        }
1276
        if (changed) {
2!
1277
          posix.setPermissions(permissions);
×
1278
        }
1279
        return true;
2✔
1280
      }
1281

1282
      // Windows
1283
      DosFileAttributeView dos = Files.getFileAttributeView(file, DosFileAttributeView.class);
×
1284
      if (dos != null) {
×
1285
        dos.setReadOnly(!writable);
×
1286
        return true;
×
1287
      }
1288

1289
      LOG.debug("Failed to set writing permission for file {}", file);
×
1290
      return false;
×
1291

1292
    } catch (IOException e) {
×
1293
      LOG.debug("Error occurred when trying to set writing permission for file {}: {}", file, e.toString(), e);
×
1294
      return false;
×
1295
    }
1296
  }
1297

1298
  @Override
1299
  public void makeExecutable(Path path, boolean confirm) {
1300

1301
    if (Files.exists(path)) {
5✔
1302
      if (skipPermissionsIfWindows(path)) {
4!
1303
        return;
×
1304
      }
1305
      PathPermissions existingPermissions = getFilePermissions(path);
4✔
1306
      PathPermissions executablePermissions = existingPermissions.makeExecutable();
3✔
1307
      boolean update = (executablePermissions != existingPermissions);
7✔
1308
      if (update) {
2✔
1309
        if (confirm) {
2!
1310
          boolean yesContinue = this.context.question(
×
1311
              "We want to execute {} but this command seems to lack executable permissions!\n"
1312
                  + "Most probably the tool vendor did forget to add x-flags in the binary release package.\n"
1313
                  + "Before running the command, we suggest to set executable permissions to the file:\n"
1314
                  + "{}\n"
1315
                  + "For security reasons we ask for your confirmation so please check this request.\n"
1316
                  + "Changing permissions from {} to {}.\n"
1317
                  + "Do you confirm to make the command executable before running it?", path.getFileName(), path, existingPermissions, executablePermissions);
×
1318
          if (!yesContinue) {
×
1319
            return;
×
1320
          }
1321
        }
1322
        setFilePermissions(path, executablePermissions, false);
6✔
1323
      } else {
1324
        LOG.trace("Executable flags already present so no need to set them for file {}", path);
4✔
1325
      }
1326
    } else {
1✔
1327
      LOG.warn("Cannot set executable flag on file that does not exist: {}", path);
4✔
1328
    }
1329
  }
1✔
1330

1331
  @Override
1332
  public PathPermissions getFilePermissions(Path path) {
1333

1334
    PathPermissions pathPermissions;
1335
    String info = "";
2✔
1336
    if (SystemInfoImpl.INSTANCE.isWindows()) {
3!
1337
      info = "mocked-";
×
1338
      if (Files.isDirectory(path)) {
×
1339
        pathPermissions = PathPermissions.MODE_RWX_RX_RX;
×
1340
      } else {
1341
        Path parent = path.getParent();
×
1342
        if ((parent != null) && (parent.getFileName().toString().equals("bin"))) {
×
1343
          pathPermissions = PathPermissions.MODE_RWX_RX_RX;
×
1344
        } else {
1345
          pathPermissions = PathPermissions.MODE_RW_R_R;
×
1346
        }
1347
      }
×
1348
    } else {
1349
      Set<PosixFilePermission> permissions;
1350
      try {
1351
        // Read the current file permissions
1352
        permissions = Files.getPosixFilePermissions(path);
5✔
1353
      } catch (IOException e) {
×
1354
        throw new RuntimeException("Failed to get permissions for " + path, e);
×
1355
      }
1✔
1356
      pathPermissions = PathPermissions.of(permissions);
3✔
1357
    }
1358
    LOG.trace("Read {}permissions of {} as {}.", info, path, pathPermissions);
17✔
1359
    return pathPermissions;
2✔
1360
  }
1361

1362
  @Override
1363
  public void setFilePermissions(Path path, PathPermissions permissions, boolean logErrorAndContinue) {
1364

1365
    if (skipPermissionsIfWindows(path)) {
4!
1366
      return;
×
1367
    }
1368
    try {
1369
      LOG.debug("Setting permissions for {} to {}", path, permissions);
5✔
1370
      // Set the new permissions
1371
      Files.setPosixFilePermissions(path, permissions.toPosix());
5✔
1372
    } catch (IOException e) {
×
1373
      String message = "Failed to set permissions to " + permissions + " for path " + path;
×
1374
      if (logErrorAndContinue) {
×
1375
        LOG.warn(message, e);
×
1376
      } else {
1377
        throw new RuntimeException(message, e);
×
1378
      }
1379
    }
1✔
1380
  }
1✔
1381

1382
  private boolean skipPermissionsIfWindows(Path path) {
1383

1384
    if (SystemInfoImpl.INSTANCE.isWindows()) {
3!
1385
      LOG.trace("Windows does not have file permissions hence omitting for {}", path);
×
1386
      return true;
×
1387
    }
1388
    return false;
2✔
1389
  }
1390

1391
  @Override
1392
  public void touch(Path file) {
1393

1394
    if (Files.exists(file)) {
5✔
1395
      try {
1396
        Files.setLastModifiedTime(file, FileTime.fromMillis(System.currentTimeMillis()));
5✔
1397
      } catch (IOException e) {
×
1398
        throw new IllegalStateException("Could not update modification-time of " + file, e);
×
1399
      }
1✔
1400
    } else {
1401
      try {
1402
        Files.createFile(file);
5✔
1403
      } catch (IOException e) {
1✔
1404
        throw new IllegalStateException("Could not create empty file " + file, e);
8✔
1405
      }
1✔
1406
    }
1407
  }
1✔
1408

1409
  @Override
1410
  public String readFileContent(Path file) {
1411

1412
    LOG.trace("Reading content of file from {}", file);
4✔
1413
    if (!Files.exists((file))) {
5!
1414
      LOG.debug("File {} does not exist", file);
×
1415
      return null;
×
1416
    }
1417
    try {
1418
      String content = Files.readString(file);
3✔
1419
      LOG.trace("Completed reading {} character(s) from file {}", content.length(), file);
7✔
1420
      return content;
2✔
1421
    } catch (IOException e) {
×
1422
      throw new IllegalStateException("Failed to read file " + file, e);
×
1423
    }
1424
  }
1425

1426
  @Override
1427
  public void writeFileContent(String content, Path file, boolean createParentDir) {
1428

1429
    if (createParentDir) {
2✔
1430
      mkdirs(file.getParent());
4✔
1431
    }
1432
    if (content == null) {
2!
1433
      content = "";
×
1434
    }
1435
    LOG.trace("Writing content with {} character(s) to file {}", content.length(), file);
7✔
1436
    if (Files.exists(file)) {
5✔
1437
      LOG.info("Overriding content of file {}", file);
4✔
1438
    }
1439
    try {
1440
      Files.writeString(file, content);
6✔
1441
      LOG.trace("Wrote content to file {}", file);
4✔
1442
    } catch (IOException e) {
×
1443
      throw new RuntimeException("Failed to write file " + file, e);
×
1444
    }
1✔
1445
  }
1✔
1446

1447
  @Override
1448
  public List<String> readFileLines(Path file) {
1449

1450
    LOG.trace("Reading lines of file from {}", file);
4✔
1451
    if (!Files.exists(file)) {
5✔
1452
      LOG.warn("File {} does not exist", file);
4✔
1453
      return null;
2✔
1454
    }
1455
    try {
1456
      List<String> content = Files.readAllLines(file);
3✔
1457
      LOG.trace("Completed reading {} lines from file {}", content.size(), file);
7✔
1458
      return content;
2✔
1459
    } catch (IOException e) {
×
1460
      throw new IllegalStateException("Failed to read file " + file, e);
×
1461
    }
1462
  }
1463

1464
  @Override
1465
  public void writeFileLines(List<String> content, Path file, boolean createParentDir) {
1466

1467
    if (createParentDir) {
2!
1468
      mkdirs(file.getParent());
×
1469
    }
1470
    if (content == null) {
2!
1471
      content = List.of();
×
1472
    }
1473
    LOG.trace("Writing content with {} lines to file {}", content.size(), file);
7✔
1474
    if (Files.exists(file)) {
5✔
1475
      LOG.debug("Overriding content of file {}", file);
4✔
1476
    }
1477
    try {
1478
      Files.write(file, content);
6✔
1479
      LOG.trace("Wrote lines to file {}", file);
4✔
1480
    } catch (IOException e) {
×
1481
      throw new RuntimeException("Failed to write file " + file, e);
×
1482
    }
1✔
1483
  }
1✔
1484

1485
  @Override
1486
  public void readProperties(Path file, Properties properties) {
1487

1488
    try (Reader reader = Files.newBufferedReader(file)) {
3✔
1489
      properties.load(reader);
3✔
1490
      LOG.debug("Successfully loaded {} properties from {}", properties.size(), file);
7✔
1491
    } catch (IOException e) {
×
1492
      throw new IllegalStateException("Failed to read properties file: " + file, e);
×
1493
    }
1✔
1494
  }
1✔
1495

1496
  @Override
1497
  public void writeProperties(Properties properties, Path file, boolean createParentDir) {
1498

1499
    if (createParentDir) {
2✔
1500
      mkdirs(file.getParent());
4✔
1501
    }
1502
    try (Writer writer = Files.newBufferedWriter(file)) {
5✔
1503
      properties.store(writer, null); // do not get confused - Java still writes a date/time header that cannot be omitted
4✔
1504
      LOG.debug("Successfully saved {} properties to {}", properties.size(), file);
7✔
1505
    } catch (IOException e) {
×
1506
      throw new IllegalStateException("Failed to save properties file during tests.", e);
×
1507
    }
1✔
1508
  }
1✔
1509

1510
  @Override
1511
  public void readIniFile(Path file, IniFile iniFile) {
1512

1513
    if (!Files.exists(file)) {
5✔
1514
      LOG.debug("INI file {} does not exist.", iniFile);
4✔
1515
      return;
1✔
1516
    }
1517
    List<String> iniLines = readFileLines(file);
4✔
1518
    IniSection currentIniSection = iniFile.getInitialSection();
3✔
1519
    for (String line : iniLines) {
10✔
1520
      if (line.trim().startsWith("[")) {
5✔
1521
        currentIniSection = iniFile.getOrCreateSection(line);
5✔
1522
      } else if (line.isBlank() || IniComment.COMMENT_SYMBOLS.contains(line.trim().charAt(0))) {
11✔
1523
        currentIniSection.addComment(line);
4✔
1524
      } else {
1525
        int index = line.indexOf('=');
4✔
1526
        if (index > 0) {
2!
1527
          currentIniSection.setProperty(line);
3✔
1528
        }
1529
      }
1530
    }
1✔
1531
  }
1✔
1532

1533
  @Override
1534
  public void writeIniFile(IniFile iniFile, Path file, boolean createParentDir) {
1535

1536
    String iniString = iniFile.toString();
3✔
1537
    writeFileContent(iniString, file, createParentDir);
5✔
1538
  }
1✔
1539

1540
  @Override
1541
  public Duration getFileAge(Path path) {
1542

1543
    if (Files.exists(path)) {
5✔
1544
      try {
1545
        long currentTime = System.currentTimeMillis();
2✔
1546
        long fileModifiedTime = Files.getLastModifiedTime(path).toMillis();
6✔
1547
        return Duration.ofMillis(currentTime - fileModifiedTime);
5✔
1548
      } catch (IOException e) {
×
1549
        LOG.warn("Could not get modification-time of {}.", path, e);
×
1550
      }
×
1551
    } else {
1552
      LOG.debug("Path {} is missing - skipping modification-time and file age check.", path);
4✔
1553
    }
1554
    return null;
2✔
1555
  }
1556

1557
  @Override
1558
  public boolean isFileAgeRecent(Path path, Duration cacheDuration) {
1559

1560
    Duration age = getFileAge(path);
4✔
1561
    if (age == null) {
2✔
1562
      return false;
2✔
1563
    }
1564
    LOG.debug("The path {} was last updated {} ago and caching duration is {}.", path, age, cacheDuration);
17✔
1565
    return (age.toMillis() <= cacheDuration.toMillis());
10✔
1566
  }
1567
}
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