| 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.dev.fs; |
| 7 |
|
| 8 |
import java.io.FileNotFoundException; |
| 9 |
import java.io.IOException; |
| 10 |
import java.io.InputStream; |
| 11 |
import java.io.OutputStream; |
| 12 |
import java.nio.ByteBuffer; |
| 13 |
import java.nio.channels.Channels; |
| 14 |
import java.nio.channels.FileChannel; |
| 15 |
import java.nio.channels.FileLock; |
| 16 |
import java.util.ArrayList; |
| 17 |
import java.util.zip.ZipEntry; |
| 18 |
import java.util.zip.ZipInputStream; |
| 19 |
import org.h2.engine.Constants; |
| 20 |
import org.h2.message.DbException; |
| 21 |
import org.h2.store.fs.FakeFileChannel; |
| 22 |
import org.h2.store.fs.FileBase; |
| 23 |
import org.h2.store.fs.FilePath; |
| 24 |
import org.h2.store.fs.FileUtils; |
| 25 |
import org.h2.store.fs.disk.FilePathDisk; |
| 26 |
import org.h2.util.IOUtils; |
| 27 |
|
| 28 |
/** |
| 29 |
* This is a read-only file system that allows to access databases stored in a |
| 30 |
* .zip or .jar file. The problem of this file system is that data is always |
| 31 |
* accessed as a stream. This implementation allows to stack file systems. |
| 32 |
*/ |
| 33 |
public class FilePathZip2 extends FilePath { |
| 34 |
|
| 35 |
/** |
| 36 |
* Register the file system. |
| 37 |
* |
| 38 |
* @return the instance |
| 39 |
*/ |
| 40 |
public static FilePathZip2 register() { |
| 41 |
FilePathZip2 instance = new FilePathZip2(); |
| 42 |
FilePath.register(instance); |
| 43 |
return instance; |
| 44 |
} |
| 45 |
|
| 46 |
@Override |
| 47 |
public FilePathZip2 getPath(String path) { |
| 48 |
FilePathZip2 p = new FilePathZip2(); |
| 49 |
p.name = path; |
| 50 |
return p; |
| 51 |
} |
| 52 |
|
| 53 |
@Override |
| 54 |
public void createDirectory() { |
| 55 |
// ignore |
| 56 |
} |
| 57 |
|
| 58 |
@Override |
| 59 |
public boolean createFile() { |
| 60 |
throw DbException.getUnsupportedException("write"); |
| 61 |
} |
| 62 |
|
| 63 |
@Override |
| 64 |
public FilePath createTempFile(String suffix, boolean inTempDir) throws IOException { |
| 65 |
if (!inTempDir) { |
| 66 |
throw new IOException("File system is read-only"); |
| 67 |
} |
| 68 |
return new FilePathDisk().getPath(name).createTempFile(suffix, true); |
| 69 |
} |
| 70 |
|
| 71 |
@Override |
| 72 |
public void delete() { |
| 73 |
throw DbException.getUnsupportedException("write"); |
| 74 |
} |
| 75 |
|
| 76 |
@Override |
| 77 |
public boolean exists() { |
| 78 |
try { |
| 79 |
String entryName = getEntryName(); |
| 80 |
if (entryName.length() == 0) { |
| 81 |
return true; |
| 82 |
} |
| 83 |
ZipInputStream file = openZip(); |
| 84 |
boolean result = false; |
| 85 |
while (true) { |
| 86 |
ZipEntry entry = file.getNextEntry(); |
| 87 |
if (entry == null) { |
| 88 |
break; |
| 89 |
} |
| 90 |
if (entry.getName().equals(entryName)) { |
| 91 |
result = true; |
| 92 |
break; |
| 93 |
} |
| 94 |
file.closeEntry(); |
| 95 |
} |
| 96 |
file.close(); |
| 97 |
return result; |
| 98 |
} catch (IOException e) { |
| 99 |
return false; |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
@Override |
| 104 |
public long lastModified() { |
| 105 |
return 0; |
| 106 |
} |
| 107 |
|
| 108 |
@Override |
| 109 |
public FilePath getParent() { |
| 110 |
int idx = name.lastIndexOf('/'); |
| 111 |
return idx < 0 ? null : getPath(name.substring(0, idx)); |
| 112 |
} |
| 113 |
|
| 114 |
@Override |
| 115 |
public boolean isAbsolute() { |
| 116 |
String fileName = translateFileName(name); |
| 117 |
return FilePath.get(fileName).isAbsolute(); |
| 118 |
} |
| 119 |
|
| 120 |
@Override |
| 121 |
public FilePath unwrap() { |
| 122 |
return FilePath.get(name.substring(getScheme().length() + 1)); |
| 123 |
} |
| 124 |
|
| 125 |
@Override |
| 126 |
public boolean isDirectory() { |
| 127 |
return isRegularOrDirectory(true); |
| 128 |
} |
| 129 |
|
| 130 |
@Override |
| 131 |
public boolean isRegularFile() { |
| 132 |
return isRegularOrDirectory(false); |
| 133 |
} |
| 134 |
|
| 135 |
private boolean isRegularOrDirectory(boolean directory) { |
| 136 |
try { |
| 137 |
String entryName = getEntryName(); |
| 138 |
if (entryName.length() == 0) { |
| 139 |
return directory; |
| 140 |
} |
| 141 |
ZipInputStream file = openZip(); |
| 142 |
boolean result = false; |
| 143 |
while (true) { |
| 144 |
ZipEntry entry = file.getNextEntry(); |
| 145 |
if (entry == null) { |
| 146 |
break; |
| 147 |
} |
| 148 |
String n = entry.getName(); |
| 149 |
if (n.equals(entryName)) { |
| 150 |
result = entry.isDirectory() == directory; |
| 151 |
break; |
| 152 |
} else if (n.startsWith(entryName)) { |
| 153 |
if (n.length() == entryName.length() + 1) { |
| 154 |
if (n.equals(entryName + "/")) { |
| 155 |
result = directory; |
| 156 |
break; |
| 157 |
} |
| 158 |
} |
| 159 |
} |
| 160 |
file.closeEntry(); |
| 161 |
} |
| 162 |
file.close(); |
| 163 |
return result; |
| 164 |
} catch (IOException e) { |
| 165 |
return false; |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
@Override |
| 170 |
public boolean canWrite() { |
| 171 |
return false; |
| 172 |
} |
| 173 |
|
| 174 |
@Override |
| 175 |
public boolean setReadOnly() { |
| 176 |
return true; |
| 177 |
} |
| 178 |
|
| 179 |
@Override |
| 180 |
public long size() { |
| 181 |
try { |
| 182 |
String entryName = getEntryName(); |
| 183 |
ZipInputStream file = openZip(); |
| 184 |
long result = 0; |
| 185 |
while (true) { |
| 186 |
ZipEntry entry = file.getNextEntry(); |
| 187 |
if (entry == null) { |
| 188 |
break; |
| 189 |
} |
| 190 |
if (entry.getName().equals(entryName)) { |
| 191 |
result = entry.getSize(); |
| 192 |
if (result == -1) { |
| 193 |
result = 0; |
| 194 |
while (true) { |
| 195 |
long x = file.skip(16 * Constants.IO_BUFFER_SIZE); |
| 196 |
if (x == 0) { |
| 197 |
break; |
| 198 |
} |
| 199 |
result += x; |
| 200 |
} |
| 201 |
} |
| 202 |
break; |
| 203 |
} |
| 204 |
file.closeEntry(); |
| 205 |
} |
| 206 |
file.close(); |
| 207 |
return result; |
| 208 |
} catch (IOException e) { |
| 209 |
return 0; |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
@Override |
| 214 |
public ArrayList<FilePath> newDirectoryStream() { |
| 215 |
String path = name; |
| 216 |
try { |
| 217 |
if (path.indexOf('!') < 0) { |
| 218 |
path += "!"; |
| 219 |
} |
| 220 |
if (!path.endsWith("/")) { |
| 221 |
path += "/"; |
| 222 |
} |
| 223 |
ZipInputStream file = openZip(); |
| 224 |
String dirName = getEntryName(); |
| 225 |
String prefix = path.substring(0, path.length() - dirName.length()); |
| 226 |
ArrayList<FilePath> list = new ArrayList<>(); |
| 227 |
while (true) { |
| 228 |
ZipEntry entry = file.getNextEntry(); |
| 229 |
if (entry == null) { |
| 230 |
break; |
| 231 |
} |
| 232 |
String entryName = entry.getName(); |
| 233 |
if (entryName.startsWith(dirName) && entryName.length() > dirName.length()) { |
| 234 |
int idx = entryName.indexOf('/', dirName.length()); |
| 235 |
if (idx < 0 || idx >= entryName.length() - 1) { |
| 236 |
list.add(getPath(prefix + entryName)); |
| 237 |
} |
| 238 |
} |
| 239 |
file.closeEntry(); |
| 240 |
} |
| 241 |
file.close(); |
| 242 |
return list; |
| 243 |
} catch (IOException e) { |
| 244 |
throw DbException.convertIOException(e, "listFiles " + path); |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
@Override |
| 249 |
public FilePath toRealPath() { |
| 250 |
return this; |
| 251 |
} |
| 252 |
|
| 253 |
@Override |
| 254 |
public InputStream newInputStream() throws IOException { |
| 255 |
return Channels.newInputStream(open("r")); |
| 256 |
} |
| 257 |
|
| 258 |
@Override |
| 259 |
public FileChannel open(String mode) throws IOException { |
| 260 |
String entryName = getEntryName(); |
| 261 |
if (entryName.length() == 0) { |
| 262 |
throw new FileNotFoundException(); |
| 263 |
} |
| 264 |
ZipInputStream in = openZip(); |
| 265 |
while (true) { |
| 266 |
ZipEntry entry = in.getNextEntry(); |
| 267 |
if (entry == null) { |
| 268 |
break; |
| 269 |
} |
| 270 |
if (entry.getName().equals(entryName)) { |
| 271 |
return new FileZip2(name, entryName, in, size()); |
| 272 |
} |
| 273 |
in.closeEntry(); |
| 274 |
} |
| 275 |
in.close(); |
| 276 |
throw new FileNotFoundException(name); |
| 277 |
} |
| 278 |
|
| 279 |
@Override |
| 280 |
public OutputStream newOutputStream(boolean append) { |
| 281 |
throw DbException.getUnsupportedException("write"); |
| 282 |
} |
| 283 |
|
| 284 |
@Override |
| 285 |
public void moveTo(FilePath newName, boolean atomicReplace) { |
| 286 |
throw DbException.getUnsupportedException("write"); |
| 287 |
} |
| 288 |
|
| 289 |
private String getEntryName() { |
| 290 |
int idx = name.indexOf('!'); |
| 291 |
String fileName; |
| 292 |
if (idx <= 0) { |
| 293 |
fileName = ""; |
| 294 |
} else { |
| 295 |
fileName = name.substring(idx + 1); |
| 296 |
} |
| 297 |
fileName = fileName.replace('\\', '/'); |
| 298 |
if (fileName.startsWith("/")) { |
| 299 |
fileName = fileName.substring(1); |
| 300 |
} |
| 301 |
return fileName; |
| 302 |
} |
| 303 |
|
| 304 |
private ZipInputStream openZip() throws IOException { |
| 305 |
String fileName = translateFileName(name); |
| 306 |
return new ZipInputStream(FileUtils.newInputStream(fileName)); |
| 307 |
} |
| 308 |
|
| 309 |
private static String translateFileName(String fileName) { |
| 310 |
if (fileName.startsWith("zip2:")) { |
| 311 |
fileName = fileName.substring("zip2:".length()); |
| 312 |
} |
| 313 |
int idx = fileName.indexOf('!'); |
| 314 |
if (idx >= 0) { |
| 315 |
fileName = fileName.substring(0, idx); |
| 316 |
} |
| 317 |
return FilePathDisk.expandUserHomeDirectory(fileName); |
| 318 |
} |
| 319 |
|
| 320 |
@Override |
| 321 |
public String getScheme() { |
| 322 |
return "zip2"; |
| 323 |
} |
| 324 |
|
| 325 |
} |
| 326 |
|
| 327 |
/** |
| 328 |
* The file is read from a stream. When reading from start to end, the same |
| 329 |
* input stream is re-used, however when reading from end to start, a new input |
| 330 |
* stream is opened for each request. |
| 331 |
*/ |
| 332 |
class FileZip2 extends FileBase { |
| 333 |
|
| 334 |
private static final byte[] SKIP_BUFFER = new byte[1024]; |
| 335 |
|
| 336 |
private final String fullName; |
| 337 |
private final String name; |
| 338 |
private final long length; |
| 339 |
private long pos; |
| 340 |
private InputStream in; |
| 341 |
private long inPos; |
| 342 |
private boolean skipUsingRead; |
| 343 |
|
| 344 |
FileZip2(String fullName, String name, ZipInputStream in, long length) { |
| 345 |
this.fullName = fullName; |
| 346 |
this.name = name; |
| 347 |
this.length = length; |
| 348 |
this.in = in; |
| 349 |
} |
| 350 |
|
| 351 |
@Override |
| 352 |
public void implCloseChannel() throws IOException { |
| 353 |
in.close(); |
| 354 |
} |
| 355 |
|
| 356 |
@Override |
| 357 |
public long position() { |
| 358 |
return pos; |
| 359 |
} |
| 360 |
|
| 361 |
@Override |
| 362 |
public long size() { |
| 363 |
return length; |
| 364 |
} |
| 365 |
|
| 366 |
@Override |
| 367 |
public int read(ByteBuffer dst) throws IOException { |
| 368 |
seek(); |
| 369 |
int len = in.read(dst.array(), dst.arrayOffset() + dst.position(), |
| 370 |
dst.remaining()); |
| 371 |
if (len > 0) { |
| 372 |
dst.position(dst.position() + len); |
| 373 |
pos += len; |
| 374 |
inPos += len; |
| 375 |
} |
| 376 |
return len; |
| 377 |
} |
| 378 |
|
| 379 |
private void seek() throws IOException { |
| 380 |
if (inPos > pos) { |
| 381 |
if (in != null) { |
| 382 |
in.close(); |
| 383 |
} |
| 384 |
in = null; |
| 385 |
} |
| 386 |
if (in == null) { |
| 387 |
in = FileUtils.newInputStream(fullName); |
| 388 |
inPos = 0; |
| 389 |
} |
| 390 |
if (inPos < pos) { |
| 391 |
long skip = pos - inPos; |
| 392 |
if (!skipUsingRead) { |
| 393 |
try { |
| 394 |
IOUtils.skipFully(in, skip); |
| 395 |
} catch (NullPointerException e) { |
| 396 |
// workaround for Android |
| 397 |
skipUsingRead = true; |
| 398 |
} |
| 399 |
} |
| 400 |
if (skipUsingRead) { |
| 401 |
while (skip > 0) { |
| 402 |
int s = (int) Math.min(SKIP_BUFFER.length, skip); |
| 403 |
s = in.read(SKIP_BUFFER, 0, s); |
| 404 |
skip -= s; |
| 405 |
} |
| 406 |
} |
| 407 |
inPos = pos; |
| 408 |
} |
| 409 |
} |
| 410 |
|
| 411 |
@Override |
| 412 |
public FileChannel position(long newPos) { |
| 413 |
this.pos = newPos; |
| 414 |
return this; |
| 415 |
} |
| 416 |
|
| 417 |
@Override |
| 418 |
public FileChannel truncate(long newLength) throws IOException { |
| 419 |
throw new IOException("File is read-only"); |
| 420 |
} |
| 421 |
|
| 422 |
@Override |
| 423 |
public void force(boolean metaData) throws IOException { |
| 424 |
// nothing to do |
| 425 |
} |
| 426 |
|
| 427 |
@Override |
| 428 |
public int write(ByteBuffer src) throws IOException { |
| 429 |
throw new IOException("File is read-only"); |
| 430 |
} |
| 431 |
|
| 432 |
@Override |
| 433 |
public synchronized FileLock tryLock(long position, long size, |
| 434 |
boolean shared) throws IOException { |
| 435 |
if (shared) { |
| 436 |
return new FileLock(FakeFileChannel.INSTANCE, position, size, shared) { |
| 437 |
|
| 438 |
@Override |
| 439 |
public boolean isValid() { |
| 440 |
return true; |
| 441 |
} |
| 442 |
|
| 443 |
@Override |
| 444 |
public void release() throws IOException { |
| 445 |
// ignore |
| 446 |
}}; |
| 447 |
} |
| 448 |
return null; |
| 449 |
} |
| 450 |
|
| 451 |
@Override |
| 452 |
public String toString() { |
| 453 |
return "zip2:" + name; |
| 454 |
} |
| 455 |
|
| 456 |
} |