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

devonfw / IDEasy / 9805587641

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

Pull #297

github

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

1974 of 3525 branches covered (56.0%)

Branch coverage included in aggregate %.

5240 of 8177 relevant lines covered (64.08%)

2.8 hits per line

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

63.64
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);
×
121
  }
×
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);
×
131
  }
×
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
   * 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
158
   * 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
159
   * then copies the application to the applications folder via drag and drop in order to perform the installation.
160
   *
161
   * @param file the DMG file to extract.
162
   * @param targetDir the target directory where to extract the contents to.
163
   */
164
  void extractDmg(Path file, Path targetDir);
165

166
  /**
167
   * Extracts an MSI (Microsoft Installer) file. MSI files are commonly used for software releases on Windows that allow an installation wizard and easy later
168
   * uninstallation.
169
   *
170
   * @param file the MSI file to extract.
171
   * @param targetDir the target directory where to extract the contents to.
172
   */
173
  void extractMsi(Path file, Path targetDir);
174

175
  /**
176
   * 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
177
   * 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
178
   * specific structure.
179
   *
180
   * @param file the PKG file to extract.
181
   * @param targetDir the target directory where to extract the contents to.
182
   */
183
  void extractPkg(Path file, Path targetDir);
184

185
  /**
186
   * @param path the {@link Path} to convert.
187
   * @return the absolute and physical {@link Path} (without symbolic links).
188
   */
189
  Path toRealPath(Path path);
190

191
  /**
192
   * Deletes the given {@link Path} idempotent and recursive.
193
   *
194
   * @param path the {@link Path} to delete.
195
   */
196
  void delete(Path path);
197

198
  /**
199
   * 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
200
   * is done.
201
   *
202
   * @param name the default name of the temporary directory to create. A prefix or suffix may be added to ensure uniqueness.
203
   * @return the {@link Path} to the newly created and unique temporary directory.
204
   */
205
  Path createTempDir(String name);
206

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

215
  /**
216
   * @param dir the {@link Path} to the directory where to list the children.
217
   * @param filter the {@link Predicate} used to {@link Predicate#test(Object) decide} which children to include (if {@code true} is returned).
218
   * @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
219
   * directory.
220
   */
221
  List<Path> listChildren(Path dir, Predicate<Path> filter);
222

223
  /**
224
   * Finds the existing file with the specified name in the given list of directories.
225
   *
226
   * @param fileName The name of the file to find.
227
   * @param searchDirs The list of directories to search for the file.
228
   * @return The {@code Path} of the existing file, or {@code null} if the file is not found.
229
   */
230
  Path findExistingFile(String fileName, List<Path> searchDirs);
231

232
  /**
233
   * Checks if the given directory is empty.
234
   *
235
   * @param dir The {@link Path} object representing the directory to check.
236
   * @return {@code true} if the directory is empty, {@code false} otherwise.
237
   */
238
  boolean isEmptyDir(Path dir);
239

240
  /**
241
   * Makes the file executable.
242
   *
243
   * @param path Path to the file.
244
   * @throws IOException if an I/O error occurs.
245
   */
246
  default void makeExecutable(Path path) throws IOException {
247

248
    Files.setPosixFilePermissions(path, RWX_RX_RX);
4✔
249
  }
1✔
250
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc