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

devonfw / IDEasy / 10162545933

30 Jul 2024 12:41PM UTC coverage: 61.685% (+0.5%) from 61.217%
10162545933

push

github

web-flow
#433: Implement Intellij plugins installation (#490)

2040 of 3641 branches covered (56.03%)

Branch coverage included in aggregate %.

5419 of 8451 relevant lines covered (64.12%)

2.82 hits per line

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

81.82
cli/src/main/java/com/devonfw/tools/ide/io/FileAccess.java
1
package com.devonfw.tools.ide.io;
2

3
import java.io.IOException;
4
import java.nio.file.Files;
5
import java.nio.file.Path;
6
import java.nio.file.attribute.PosixFilePermission;
7
import java.util.List;
8
import java.util.Set;
9
import java.util.function.Consumer;
10
import java.util.function.Predicate;
11

12
/**
13
 * Interface that gives access to various operations on files.
14
 */
15
public interface FileAccess {
16

17
  /** {@link PosixFilePermission}s for "rwxr-xr-x" or 0755. */
18
  Set<PosixFilePermission> RWX_RX_RX = Set.of(PosixFilePermission.OWNER_READ, PosixFilePermission.OWNER_WRITE, PosixFilePermission.OWNER_EXECUTE,
10✔
19
      PosixFilePermission.GROUP_READ, PosixFilePermission.GROUP_EXECUTE, PosixFilePermission.OTHERS_READ, PosixFilePermission.OTHERS_EXECUTE);
20

21
  /**
22
   * Downloads a file from an arbitrary location.
23
   *
24
   * @param url the location of the binary file to download. May also be a local or remote path to copy from.
25
   * @param targetFile the {@link Path} to the target file to download to. Should not already exists. Missing parent directories will be created automatically.
26
   */
27
  void download(String url, Path targetFile);
28

29
  /**
30
   * Creates the entire {@link Path} as directories if not already existing.
31
   *
32
   * @param directory the {@link Path} to {@link java.nio.file.Files#createDirectories(Path, java.nio.file.attribute.FileAttribute...) create}.
33
   */
34
  void mkdirs(Path directory);
35

36
  /**
37
   * @param file the {@link Path} to check.
38
   * @return {@code true} if the given {@code file} points to an existing file, {@code false} otherwise (the given {@link Path} does not exist or is a
39
   * directory).
40
   */
41
  boolean isFile(Path file);
42

43
  /**
44
   * @param folder the {@link Path} to check.
45
   * @return {@code true} if the given {@code folder} points to an existing directory, {@code false} otherwise (a warning is logged in this case).
46
   */
47
  boolean isExpectedFolder(Path folder);
48

49
  /**
50
   * @param file the {@link Path} to compute the checksum of.
51
   * @return the computed checksum (SHA-266).
52
   */
53
  String checksum(Path file);
54

55
  /**
56
   * Moves the given {@link Path} to the backup.
57
   *
58
   * @param fileOrFolder the {@link Path} to move to the backup (soft-deletion).
59
   */
60
  void backup(Path fileOrFolder);
61

62
  /**
63
   * @param source the source {@link Path file or folder} to move.
64
   * @param targetDir the {@link Path} with the directory to move {@code source} into.
65
   */
66
  void move(Path source, Path targetDir);
67

68
  /**
69
   * Creates a symbolic link. If the given {@code targetLink} already exists and is a symbolic link or a Windows junction, it will be replaced. In case of
70
   * missing privileges, Windows Junctions may be used as fallback, which must point to absolute paths. Therefore, the created link will be absolute instead of
71
   * relative.
72
   *
73
   * @param source the source {@link Path} to link to, may be relative or absolute.
74
   * @param targetLink the {@link Path} where the symbolic link shall be created pointing to {@code source}.
75
   * @param relative - {@code true} if the symbolic link shall be relative, {@code false} if it shall be absolute.
76
   */
77
  void symlink(Path source, Path targetLink, boolean relative);
78

79
  /**
80
   * Creates a relative symbolic link. If the given {@code targetLink} already exists and is a symbolic link or a Windows junction, it will be replaced. In case
81
   * of missing privileges, Windows Junctions may be used as fallback, which must point to absolute paths. Therefore, the created link will be absolute instead
82
   * of relative.
83
   *
84
   * @param source the source {@link Path} to link to, may be relative or absolute.
85
   * @param targetLink the {@link Path} where the symbolic link shall be created pointing to {@code source}.
86
   */
87
  default void symlink(Path source, Path targetLink) {
88

89
    symlink(source, targetLink, true);
5✔
90
  }
1✔
91

92
  /**
93
   * @param source the source {@link Path file or folder} to copy.
94
   * @param target the {@link Path} to copy {@code source} to. See {@link #copy(Path, Path, FileCopyMode)} for details. will always ensure that in the end you
95
   * will find the same content of {@code source} in {@code target}.
96
   */
97
  default void copy(Path source, Path target) {
98

99
    copy(source, target, FileCopyMode.COPY_TREE_FAIL_IF_EXISTS);
5✔
100
  }
1✔
101

102
  /**
103
   * @param source the source {@link Path file or folder} to copy.
104
   * @param target the {@link Path} to copy {@code source} to. Unlike the Linux {@code cp} command this method will not take the filename of {@code source} and
105
   * copy that to {@code target} in case that is an existing folder. Instead it will always be simple and stupid and just copy from {@code source} to
106
   * {@code target}. Therefore the result is always clear and easy to predict and understand. Also you can easily rename a file to copy. While
107
   * {@code cp my-file target} may lead to a different result than {@code cp my-file target/} this method will always ensure that in the end you will find the
108
   * same content of {@code source} in {@code target}.
109
   * @param fileOnly - {@code true} if {@code fileOrFolder} is expected to be a file and an exception shall be thrown if it is a directory, {@code false}
110
   * otherwise (copy recursively).
111
   */
112
  void copy(Path source, Path target, FileCopyMode fileOnly);
113

114
  /**
115
   * @param archiveFile the {@link Path} to the file to extract.
116
   * @param targetDir the {@link Path} to the directory where to extract the {@code archiveFile} to.
117
   */
118
  default void extract(Path archiveFile, Path targetDir) {
119

120
    extract(archiveFile, targetDir, null);
5✔
121
  }
1✔
122

123
  /**
124
   * @param archiveFile the {@link Path} to the archive file to extract.
125
   * @param targetDir the {@link Path} to the directory where to extract the {@code archiveFile}.
126
   * @param postExtractHook the {@link Consumer} to be called after the extraction on the final folder before it is moved to {@code targetDir}.
127
   */
128
  default void extract(Path archiveFile, Path targetDir, Consumer<Path> postExtractHook) {
129

130
    extract(archiveFile, targetDir, postExtractHook, true);
6✔
131
  }
1✔
132

133
  /**
134
   * @param archiveFile the {@link Path} to the archive file to extract.
135
   * @param targetDir the {@link Path} to the directory where to extract the {@code archiveFile}.
136
   * @param postExtractHook the {@link Consumer} to be called after the extraction on the final folder before it is moved to {@code targetDir}.
137
   * @param extract {@code true} if the {@code archiveFile} should be extracted (default), {@code false} otherwise.
138
   */
139
  void extract(Path archiveFile, Path targetDir, Consumer<Path> postExtractHook, boolean extract);
140

141
  /**
142
   * Extracts a ZIP file what is the common archive format on Windows. Initially invented by PKZIP for MS-DOS and also famous from WinZIP software for Windows.
143
   *
144
   * @param file the ZIP file to extract.
145
   * @param targetDir the {@link Path} with the directory to unzip to.
146
   */
147
  void extractZip(Path file, Path targetDir);
148

149
  /**
150
   * @param file the ZIP file to extract.
151
   * @param targetDir the {@link Path} with the directory to unzip to.
152
   * @param compression the {@link TarCompression} to use.
153
   */
154
  void extractTar(Path file, Path targetDir, TarCompression compression);
155

156
  /**
157
   * @param file the JAR file to extract.
158
   * @param targetDir the {@link Path} with the directory to extract to.
159
   */
160
  void extractJar(Path file, Path targetDir);
161

162
  /**
163
   * Extracts an Apple DMG (Disk Image) file that is similar to an ISO image. DMG files are commonly used for software releases on MacOS. Double-clicking such
164
   * files on MacOS mounts them and show the application together with a symbolic link to the central applications folder and some help instructions. The user
165
   * then copies the application to the applications folder via drag and drop in order to perform the installation.
166
   *
167
   * @param file the DMG file to extract.
168
   * @param targetDir the target directory where to extract the contents to.
169
   */
170
  void extractDmg(Path file, Path targetDir);
171

172
  /**
173
   * Extracts an MSI (Microsoft Installer) file. MSI files are commonly used for software releases on Windows that allow an installation wizard and easy later
174
   * uninstallation.
175
   *
176
   * @param file the MSI file to extract.
177
   * @param targetDir the target directory where to extract the contents to.
178
   */
179
  void extractMsi(Path file, Path targetDir);
180

181
  /**
182
   * Extracts an Apple PKG (Package) file. PKG files are used instead of {@link #extractDmg(Path, Path) DMG files} if additional changes have to be performed
183
   * like drivers to be installed. Similar to what {@link #extractMsi(Path, Path) MSI} is on Windows. PKG files are internally a xar based archive with a
184
   * specific structure.
185
   *
186
   * @param file the PKG file to extract.
187
   * @param targetDir the target directory where to extract the contents to.
188
   */
189
  void extractPkg(Path file, Path targetDir);
190

191
  /**
192
   * @param path the {@link Path} to convert.
193
   * @return the absolute and physical {@link Path} (without symbolic links).
194
   */
195
  Path toRealPath(Path path);
196

197
  /**
198
   * Deletes the given {@link Path} idempotent and recursive.
199
   *
200
   * @param path the {@link Path} to delete.
201
   */
202
  void delete(Path path);
203

204
  /**
205
   * Creates a new temporary directory. ATTENTION: The user of this method is responsible to do house-keeping and {@link #delete(Path) delete} it after the work
206
   * is done.
207
   *
208
   * @param name the default name of the temporary directory to create. A prefix or suffix may be added to ensure uniqueness.
209
   * @return the {@link Path} to the newly created and unique temporary directory.
210
   */
211
  Path createTempDir(String name);
212

213
  /**
214
   * @param dir the folder to search.
215
   * @param filter the {@link Predicate} used to find the {@link Predicate#test(Object) match}.
216
   * @param recursive - {@code true} to search recursive in all sub-folders, {@code false} otherwise.
217
   * @return the first child {@link Path} matching the given {@link Predicate} or {@code null} if no match was found.
218
   */
219
  Path findFirst(Path dir, Predicate<Path> filter, boolean recursive);
220

221
  /**
222
   * @param dir the {@link Path} to the directory where to list the children.
223
   * @param filter the {@link Predicate} used to {@link Predicate#test(Object) decide} which children to include (if {@code true} is returned).
224
   * @return all children of the given {@link Path} that match the given {@link Predicate}. Will be the empty list of the given {@link Path} is not an existing
225
   * directory.
226
   */
227
  List<Path> listChildren(Path dir, Predicate<Path> filter);
228

229
  /**
230
   * Finds the existing file with the specified name in the given list of directories.
231
   *
232
   * @param fileName The name of the file to find.
233
   * @param searchDirs The list of directories to search for the file.
234
   * @return The {@code Path} of the existing file, or {@code null} if the file is not found.
235
   */
236
  Path findExistingFile(String fileName, List<Path> searchDirs);
237

238
  /**
239
   * Checks if the given directory is empty.
240
   *
241
   * @param dir The {@link Path} object representing the directory to check.
242
   * @return {@code true} if the directory is empty, {@code false} otherwise.
243
   */
244
  boolean isEmptyDir(Path dir);
245

246
  /**
247
   * Makes the file executable.
248
   *
249
   * @param path Path to the file.
250
   * @throws IOException if an I/O error occurs.
251
   */
252
  default void makeExecutable(Path path) throws IOException {
253

254
    Files.setPosixFilePermissions(path, RWX_RX_RX);
×
255
  }
×
256
}
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