-
Notifications
You must be signed in to change notification settings - Fork 531
/
FileUtil.java
119 lines (110 loc) · 4.45 KB
/
FileUtil.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
* Copyright 2016-2022 chronicle.software
*
* https://chronicle.software
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.openhft.chronicle.queue.util;
import net.openhft.chronicle.queue.internal.util.InternalFileUtil;
import org.jetbrains.annotations.NotNull;
import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.stream.Stream;
/**
* Utility methods for handling Files in connection with ChronicleQueue.
*
* @author Per Minborg
* @since 5.17.34
*/
public final class FileUtil {
private FileUtil() {}
/**
* Returns a Stream of roll Queue files that are likely removable
* from the given {@code baseDir} without affecting any Queue
* process that is currently active in the given {@code baseDir} reading
* data sequentially.
* <p>
* Files are returned in order of creation and can successively be removed
* in that order. If the removal of a particular file fails, then subsequent
* files must be untouched.
* <p>
* WARNING: This method is inherently un-deterministic as new Queue processes may
* join or leave at any time asynchronously. Thus, it is not recommended to store
* results produced by this method for longer periods.
* <p>
* Only sequential reading is supported because random access Tailers can read at
* any location at any time.
* <p>
* Here is an example of how unused files can be removed:
*
* <pre>{@code
* for (File file : removableFileCandidates(baseDir).collect(Collectors.toList())) {
* if (!file.delete()) {
* break;
* }
* }
* }</pre>
*
* @param baseDir containing queue file removal candidates
* @return a Stream of roll Queue files that are likely removable
* from the given {@code baseDir} without affecting any Queue
* process that is currently active in the given {@code baseDir}
* reading data sequentially
* @throws UnsupportedOperationException if this operation is not
* supported for the current platform (e.g. Windows).
*/
@NotNull
public static Stream<File> removableRollFileCandidates(@NotNull File baseDir) {
return InternalFileUtil.removableRollFileCandidates(baseDir);
}
/**
* Returns all files currently opened by any process, including the PID of the process holding the file open.
* <p>
* Method is only supported currently on Linux operating systems.
*
* @return a {@link Map} of the absolute paths to all the open files on the system, mapped to the PID holding the file open
* @throws UnsupportedOperationException if getAllOpenFiles is not supported by the operating system
* @throws IOException if an error occurs while traversing filesystem metadata for open files
*/
public static Map<String, String> getAllOpenFiles() throws IOException {
return InternalFileUtil.getAllOpenFiles();
}
/**
* Returns if the provided {@code file} has the Chronicle Queue file
* suffix. The current file suffix is ".cq4".
*
* @param file to check
* @return if the provided {@code file} has the ChronicleQueue file
* suffix
*/
public static boolean hasQueueSuffix(@NotNull File file) {
return InternalFileUtil.hasQueueSuffix(file);
}
/**
* Returns if the given {@code file } is used by any process (i.e.
* has the file open for reading or writing).
* <p>
* If the open state of the given {@code file} can not be determined, {@code true }
* is returned.
*
* @param file to check
* @return if the given {@code file } is used by any process
* @throws UnsupportedOperationException if this operation is not
* supported for the current platform (e.g. Windows).
*/
public static FileState state(@NotNull File file) {
return InternalFileUtil.state(file);
}
}