1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-06-17 10:50:01 +00:00
CC-Tweaked/projects/core/src/testFixtures/java/dan200/computercraft/test/core/filesystem/ReadOnlyWritableMount.java
Jonathan Coates 367773e173
Some refactoring of mounts
- Separate FileMount into separate FileMount and WritableFileMount
   classes. This separates the (relatively simple) read-only code from
   the (soon to be even more complex) read/write code.

   It also allows you to create read-only mounts which don't bother with
   filesystem accounting, which is nice.

 - Make openForWrite/openForAppend always return a SeekableFileHandle.
   Appendable files still cannot be seeked within, but that check is now
   done on the FS side.

 - Refactor the various mount tests to live in test contract interfaces,
   allowing us to reuse them between mounts.

 - Clean up our error handling a little better. (Most) file-specific code
   has been moved to FileMount, and ArchiveMount-derived classes now
   throw correct path-localised exceptions.
2022-12-09 22:02:31 +00:00

93 lines
2.6 KiB
Java

/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2022. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/
package dan200.computercraft.test.core.filesystem;
import dan200.computercraft.api.filesystem.FileOperationException;
import dan200.computercraft.api.filesystem.Mount;
import dan200.computercraft.api.filesystem.WritableMount;
import java.io.IOException;
import java.nio.channels.SeekableByteChannel;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.List;
/**
* Wraps a {@link Mount} into a read-only {@link WritableMount}.
*
* @param mount The original read-only mount we're wrapping.
*/
public record ReadOnlyWritableMount(Mount mount) implements WritableMount {
@Override
public boolean exists(String path) throws IOException {
return mount.exists(path);
}
@Override
public boolean isDirectory(String path) throws IOException {
return mount.isDirectory(path);
}
@Override
public void list(String path, List<String> contents) throws IOException {
mount.list(path, contents);
}
@Override
public long getSize(String path) throws IOException {
return mount.getSize(path);
}
@Override
public SeekableByteChannel openForRead(String path) throws IOException {
return mount.openForRead(path);
}
@Override
public BasicFileAttributes getAttributes(String path) throws IOException {
return mount.getAttributes(path);
}
@Override
public void makeDirectory(String path) throws IOException {
throw new FileOperationException(path, "Access denied");
}
@Override
public void delete(String path) throws IOException {
throw new FileOperationException(path, "Access denied");
}
@Override
public void rename(String source, String dest) throws IOException {
throw new FileOperationException(source, "Access denied");
}
@Override
public SeekableByteChannel openForWrite(String path) throws IOException {
throw new FileOperationException(path, "Access denied");
}
@Override
public SeekableByteChannel openForAppend(String path) throws IOException {
throw new FileOperationException(path, "Access denied");
}
@Override
public long getRemainingSpace() {
return Integer.MAX_VALUE;
}
@Override
public long getCapacity() {
return Integer.MAX_VALUE;
}
@Override
public boolean isReadOnly(String path) {
return true;
}
}