forked from apache/paimon-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileLock.java
More file actions
165 lines (150 loc) · 5.3 KB
/
FileLock.java
File metadata and controls
165 lines (150 loc) · 5.3 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.paimon.python;
import org.apache.paimon.utils.Preconditions;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
/** A file lock used for avoiding race condition among multiple threads/processes. */
public class FileLock {
private static final String TEMP_DIR = System.getProperty("java.io.tmpdir");
private final File file;
private FileOutputStream outputStream;
private java.nio.channels.FileLock lock;
/**
* Initialize a FileLock using a file located at fullPath.
*
* @param fullPath The path of the locking file
*/
public FileLock(String fullPath) {
Preconditions.checkNotNull(fullPath, "fullPath should not be null");
Path path = Paths.get(fullPath);
String normalizedFileName = normalizeFileName(path.getFileName().toString());
if (normalizedFileName.isEmpty()) {
throw new IllegalArgumentException("There are no legal characters in the file name");
}
this.file =
path.getParent() == null
? new File(TEMP_DIR, normalizedFileName)
: new File(path.getParent().toString(), normalizedFileName);
}
/**
* Initialize a FileLock using a file located at parentDir/fileName.
*
* @param parentDir The parent dir of the locking file
* @param fileName The name of the locking file
*/
public FileLock(String parentDir, String fileName) {
Preconditions.checkNotNull(parentDir, "parentDir should not be null");
Preconditions.checkNotNull(fileName, "fileName should not be null");
this.file = new File(parentDir, normalizeFileName(fileName));
}
/**
* Initialize a FileLock using a file located inside temp folder.
*
* @param fileName The name of the locking file
* @return The initialized FileLock
*/
public static FileLock inTempFolder(String fileName) {
return new FileLock(TEMP_DIR, fileName);
}
/**
* Check whether the locking file exists in the file system. Create it if it does not exist.
* Then create a FileOutputStream for it.
*
* @throws IOException If the file path is invalid or the parent dir does not exist
*/
private void init() throws IOException {
if (!this.file.exists()) {
this.file.createNewFile();
}
outputStream = new FileOutputStream(this.file);
}
/**
* Try to acquire a lock on the locking file. This method immediately returns whenever the lock
* is acquired or not.
*
* @return True if successfully acquired the lock
* @throws IOException If the file path is invalid
*/
public boolean tryLock() throws IOException {
if (outputStream == null) {
init();
}
try {
lock = outputStream.getChannel().tryLock();
} catch (Exception e) {
return false;
}
return lock != null;
}
/**
* Release the file lock.
*
* @throws IOException If the FileChannel is closed
*/
public void unlock() throws IOException {
if (lock != null && lock.channel().isOpen()) {
lock.release();
}
}
/**
* Release the file lock, close the fileChannel and FileOutputStream then try deleting the
* locking file if other file lock does not need it, which means the lock will not be used
* anymore.
*
* @throws IOException If an I/O error occurs
*/
public void unlockAndDestroy() throws IOException {
try {
unlock();
if (lock != null) {
lock.channel().close();
lock = null;
}
if (outputStream != null) {
outputStream.close();
outputStream = null;
}
} finally {
this.file.delete();
}
}
/**
* Check whether a FileLock is actually holding the lock.
*
* @return True if it is actually holding the lock
*/
public boolean isValid() {
if (lock != null) {
return lock.isValid();
}
return false;
}
/**
* Normalize the file name, which only allows slash, backslash, digits and letters.
*
* @param fileName Original file name
* @return File name with illegal characters stripped
*/
private static String normalizeFileName(String fileName) {
return fileName.replaceAll("[^\\w/\\\\]", "");
}
}