| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.store.fs; |
| 7 |
|
| 8 |
import java.io.IOException; |
| 9 |
import java.io.InputStream; |
| 10 |
import java.io.OutputStream; |
| 11 |
import java.nio.channels.Channels; |
| 12 |
import java.nio.channels.FileChannel; |
| 13 |
import java.util.List; |
| 14 |
import java.util.concurrent.ConcurrentHashMap; |
| 15 |
import org.h2.store.fs.disk.FilePathDisk; |
| 16 |
import org.h2.util.MathUtils; |
| 17 |
|
| 18 |
/** |
| 19 |
* A path to a file. It similar to the Java 7 <code>java.nio.file.Path</code>, |
| 20 |
* but simpler, and works with older versions of Java. It also implements the |
| 21 |
* relevant methods found in <code>java.nio.file.FileSystem</code> and |
| 22 |
* <code>FileSystems</code> |
| 23 |
*/ |
| 24 |
public abstract class FilePath { |
| 25 |
|
| 26 |
private static final FilePath defaultProvider; |
| 27 |
|
| 28 |
private static final ConcurrentHashMap<String, FilePath> providers; |
| 29 |
|
| 30 |
/** |
| 31 |
* The prefix for temporary files. |
| 32 |
*/ |
| 33 |
private static String tempRandom; |
| 34 |
private static long tempSequence; |
| 35 |
|
| 36 |
/** |
| 37 |
* The complete path (which may be absolute or relative, depending on the |
| 38 |
* file system). |
| 39 |
*/ |
| 40 |
public String name; |
| 41 |
|
| 42 |
static { |
| 43 |
ConcurrentHashMap<String, FilePath> map = new ConcurrentHashMap<>(); |
| 44 |
FilePath p = new FilePathDisk(); |
| 45 |
map.put(p.getScheme(), p); |
| 46 |
map.put("nio", p); |
| 47 |
defaultProvider = p; |
| 48 |
for (String c : new String[] { |
| 49 |
"org.h2.store.fs.mem.FilePathMem", |
| 50 |
"org.h2.store.fs.mem.FilePathMemLZF", |
| 51 |
"org.h2.store.fs.niomem.FilePathNioMem", |
| 52 |
"org.h2.store.fs.niomem.FilePathNioMemLZF", |
| 53 |
"org.h2.store.fs.split.FilePathSplit", |
| 54 |
"org.h2.store.fs.niomapped.FilePathNioMapped", |
| 55 |
"org.h2.store.fs.async.FilePathAsync", |
| 56 |
"org.h2.store.fs.zip.FilePathZip", |
| 57 |
"org.h2.store.fs.retry.FilePathRetryOnInterrupt" |
| 58 |
}) { |
| 59 |
try { |
| 60 |
p = (FilePath) Class.forName(c).getDeclaredConstructor().newInstance(); |
| 61 |
map.put(p.getScheme(), p); |
| 62 |
} catch (Exception e) { |
| 63 |
// ignore - the files may be excluded in purpose |
| 64 |
} |
| 65 |
} |
| 66 |
providers = map; |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Get the file path object for the given path. |
| 71 |
* Windows-style '\' is replaced with '/'. |
| 72 |
* |
| 73 |
* @param path the path |
| 74 |
* @return the file path object |
| 75 |
*/ |
| 76 |
public static FilePath get(String path) { |
| 77 |
path = path.replace('\\', '/'); |
| 78 |
int index = path.indexOf(':'); |
| 79 |
if (index < 2) { |
| 80 |
// use the default provider if no prefix or |
| 81 |
// only a single character (drive name) |
| 82 |
return defaultProvider.getPath(path); |
| 83 |
} |
| 84 |
String scheme = path.substring(0, index); |
| 85 |
FilePath p = providers.get(scheme); |
| 86 |
if (p == null) { |
| 87 |
// provider not found - use the default |
| 88 |
p = defaultProvider; |
| 89 |
} |
| 90 |
return p.getPath(path); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Register a file provider. |
| 95 |
* |
| 96 |
* @param provider the file provider |
| 97 |
*/ |
| 98 |
public static void register(FilePath provider) { |
| 99 |
providers.put(provider.getScheme(), provider); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Unregister a file provider. |
| 104 |
* |
| 105 |
* @param provider the file provider |
| 106 |
*/ |
| 107 |
public static void unregister(FilePath provider) { |
| 108 |
providers.remove(provider.getScheme()); |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Get the size of a file in bytes |
| 113 |
* |
| 114 |
* @return the size in bytes |
| 115 |
*/ |
| 116 |
public abstract long size(); |
| 117 |
|
| 118 |
/** |
| 119 |
* Rename a file if this is allowed. |
| 120 |
* |
| 121 |
* @param newName the new fully qualified file name |
| 122 |
* @param atomicReplace whether the move should be atomic, and the target |
| 123 |
* file should be replaced if it exists and replacing is possible |
| 124 |
*/ |
| 125 |
public abstract void moveTo(FilePath newName, boolean atomicReplace); |
| 126 |
|
| 127 |
/** |
| 128 |
* Create a new file. |
| 129 |
* |
| 130 |
* @return true if creating was successful |
| 131 |
*/ |
| 132 |
public abstract boolean createFile(); |
| 133 |
|
| 134 |
/** |
| 135 |
* Checks if a file exists. |
| 136 |
* |
| 137 |
* @return true if it exists |
| 138 |
*/ |
| 139 |
public abstract boolean exists(); |
| 140 |
|
| 141 |
/** |
| 142 |
* Delete a file or directory if it exists. |
| 143 |
* Directories may only be deleted if they are empty. |
| 144 |
*/ |
| 145 |
public abstract void delete(); |
| 146 |
|
| 147 |
/** |
| 148 |
* List the files and directories in the given directory. |
| 149 |
* |
| 150 |
* @return the list of fully qualified file names |
| 151 |
*/ |
| 152 |
public abstract List<FilePath> newDirectoryStream(); |
| 153 |
|
| 154 |
/** |
| 155 |
* Normalize a file name. |
| 156 |
* |
| 157 |
* @return the normalized file name |
| 158 |
*/ |
| 159 |
public abstract FilePath toRealPath(); |
| 160 |
|
| 161 |
/** |
| 162 |
* Get the parent directory of a file or directory. |
| 163 |
* |
| 164 |
* @return the parent directory name |
| 165 |
*/ |
| 166 |
public abstract FilePath getParent(); |
| 167 |
|
| 168 |
/** |
| 169 |
* Check if it is a file or a directory. |
| 170 |
* |
| 171 |
* @return true if it is a directory |
| 172 |
*/ |
| 173 |
public abstract boolean isDirectory(); |
| 174 |
|
| 175 |
/** |
| 176 |
* Check if it is a regular file. |
| 177 |
* |
| 178 |
* @return true if it is a regular file |
| 179 |
*/ |
| 180 |
public abstract boolean isRegularFile(); |
| 181 |
|
| 182 |
/** |
| 183 |
* Check if the file name includes a path. |
| 184 |
* |
| 185 |
* @return if the file name is absolute |
| 186 |
*/ |
| 187 |
public abstract boolean isAbsolute(); |
| 188 |
|
| 189 |
/** |
| 190 |
* Get the last modified date of a file |
| 191 |
* |
| 192 |
* @return the last modified date |
| 193 |
*/ |
| 194 |
public abstract long lastModified(); |
| 195 |
|
| 196 |
/** |
| 197 |
* Check if the file is writable. |
| 198 |
* |
| 199 |
* @return if the file is writable |
| 200 |
*/ |
| 201 |
public abstract boolean canWrite(); |
| 202 |
|
| 203 |
/** |
| 204 |
* Create a directory (all required parent directories already exist). |
| 205 |
*/ |
| 206 |
public abstract void createDirectory(); |
| 207 |
|
| 208 |
/** |
| 209 |
* Get the file or directory name (the last element of the path). |
| 210 |
* |
| 211 |
* @return the last element of the path |
| 212 |
*/ |
| 213 |
public String getName() { |
| 214 |
int idx = Math.max(name.indexOf(':'), name.lastIndexOf('/')); |
| 215 |
return idx < 0 ? name : name.substring(idx + 1); |
| 216 |
} |
| 217 |
|
| 218 |
/** |
| 219 |
* Create an output stream to write into the file. |
| 220 |
* |
| 221 |
* @param append if true, the file will grow, if false, the file will be |
| 222 |
* truncated first |
| 223 |
* @return the output stream |
| 224 |
* @throws IOException If an I/O error occurs |
| 225 |
*/ |
| 226 |
public OutputStream newOutputStream(boolean append) throws IOException { |
| 227 |
return newFileChannelOutputStream(open("rw"), append); |
| 228 |
} |
| 229 |
|
| 230 |
/** |
| 231 |
* Create a new output stream from the channel. |
| 232 |
* |
| 233 |
* @param channel the file channel |
| 234 |
* @param append true for append mode, false for truncate and overwrite |
| 235 |
* @return the output stream |
| 236 |
* @throws IOException on I/O exception |
| 237 |
*/ |
| 238 |
public static OutputStream newFileChannelOutputStream(FileChannel channel, boolean append) |
| 239 |
throws IOException { |
| 240 |
if (append) { |
| 241 |
channel.position(channel.size()); |
| 242 |
} else { |
| 243 |
channel.position(0); |
| 244 |
channel.truncate(0); |
| 245 |
} |
| 246 |
return Channels.newOutputStream(channel); |
| 247 |
} |
| 248 |
|
| 249 |
/** |
| 250 |
* Open a random access file object. |
| 251 |
* |
| 252 |
* @param mode the access mode. Supported are r, rw, rws, rwd |
| 253 |
* @return the file object |
| 254 |
* @throws IOException If an I/O error occurs |
| 255 |
*/ |
| 256 |
public abstract FileChannel open(String mode) throws IOException; |
| 257 |
|
| 258 |
/** |
| 259 |
* Create an input stream to read from the file. |
| 260 |
* |
| 261 |
* @return the input stream |
| 262 |
* @throws IOException If an I/O error occurs |
| 263 |
*/ |
| 264 |
public InputStream newInputStream() throws IOException { |
| 265 |
return Channels.newInputStream(open("r")); |
| 266 |
} |
| 267 |
|
| 268 |
/** |
| 269 |
* Disable the ability to write. |
| 270 |
* |
| 271 |
* @return true if the call was successful |
| 272 |
*/ |
| 273 |
public abstract boolean setReadOnly(); |
| 274 |
|
| 275 |
/** |
| 276 |
* Create a new temporary file. |
| 277 |
* |
| 278 |
* @param suffix the suffix |
| 279 |
* @param inTempDir if the file should be stored in the temporary directory |
| 280 |
* @return the name of the created file |
| 281 |
* @throws IOException on failure |
| 282 |
*/ |
| 283 |
@SuppressWarnings("unused") |
| 284 |
public FilePath createTempFile(String suffix, boolean inTempDir) throws IOException { |
| 285 |
while (true) { |
| 286 |
FilePath p = getPath(name + getNextTempFileNamePart(false) + suffix); |
| 287 |
if (p.exists() || !p.createFile()) { |
| 288 |
// in theory, the random number could collide |
| 289 |
getNextTempFileNamePart(true); |
| 290 |
continue; |
| 291 |
} |
| 292 |
p.open("rw").close(); |
| 293 |
return p; |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
/** |
| 298 |
* Get the next temporary file name part (the part in the middle). |
| 299 |
* |
| 300 |
* @param newRandom if the random part of the filename should change |
| 301 |
* @return the file name part |
| 302 |
*/ |
| 303 |
private static synchronized String getNextTempFileNamePart( |
| 304 |
boolean newRandom) { |
| 305 |
if (newRandom || tempRandom == null) { |
| 306 |
tempRandom = MathUtils.randomInt(Integer.MAX_VALUE) + "."; |
| 307 |
} |
| 308 |
return tempRandom + tempSequence++; |
| 309 |
} |
| 310 |
|
| 311 |
/** |
| 312 |
* Get the string representation. The returned string can be used to |
| 313 |
* construct a new object. |
| 314 |
* |
| 315 |
* @return the path as a string |
| 316 |
*/ |
| 317 |
@Override |
| 318 |
public String toString() { |
| 319 |
return name; |
| 320 |
} |
| 321 |
|
| 322 |
/** |
| 323 |
* Get the scheme (prefix) for this file provider. |
| 324 |
* This is similar to |
| 325 |
* <code>java.nio.file.spi.FileSystemProvider.getScheme</code>. |
| 326 |
* |
| 327 |
* @return the scheme |
| 328 |
*/ |
| 329 |
public abstract String getScheme(); |
| 330 |
|
| 331 |
/** |
| 332 |
* Convert a file to a path. This is similar to |
| 333 |
* <code>java.nio.file.spi.FileSystemProvider.getPath</code>, but may |
| 334 |
* return an object even if the scheme doesn't match in case of the |
| 335 |
* default file provider. |
| 336 |
* |
| 337 |
* @param path the path |
| 338 |
* @return the file path object |
| 339 |
*/ |
| 340 |
public abstract FilePath getPath(String path); |
| 341 |
|
| 342 |
/** |
| 343 |
* Get the unwrapped file name (without wrapper prefixes if wrapping / |
| 344 |
* delegating file systems are used). |
| 345 |
* |
| 346 |
* @return the unwrapped path |
| 347 |
*/ |
| 348 |
public FilePath unwrap() { |
| 349 |
return this; |
| 350 |
} |
| 351 |
|
| 352 |
} |