CopyOnWriteFSProvider .java

net.minecraft.util.filefix.virtualfilesystem.CopyOnWriteFSProvider (class): members: DUMMY_DIRECTORY_VIEW, ChannelFactory, newFileChannel, getRealPath.

net.minecraft.util.filefix.virtualfilesystemclass336 linescommonextends FileSystemProvider11.5 KB

API Surface (27)

String name()
BasicFileAttributes readAttributes()
void setTimes(final FileTime lastModifiedTime, final FileTime lastAccessTime, final FileTime createTime)
String getScheme()
FileSystem newFileSystem(final URI uri, final Map<String, ?> env)
FileSystem getFileSystem(final URI uri)
Path getPath(final URI uri)
SeekableByteChannel newByteChannel(final Path path, final Set<? extends OpenOption> options, final FileAttribute<?>... attrs)
FileChannel newFileChannel(final Path path, final Set<? extends OpenOption> options, final FileAttribute<?>... attrs)
DirectoryStream<Path> newDirectoryStream(final Path dir, final Filter<? super Path> filter)
void close()
Iterator<Path> iterator()
void createDirectory(final Path dir, final FileAttribute<?>... attrs)
void delete(final Path path)
void copy(final Path source, final Path target, final CopyOption... options)
void move(final Path source, final Path target, final CopyOption... options)
boolean isSameFile(final Path path, final Path path2)
boolean isHidden(final Path path)
FileStore getFileStore(final Path path)
void checkAccess(final Path path, final AccessMode... modes)
String name()
BasicFileAttributes readAttributes()
void setTimes(final FileTime lastModifiedTime, final FileTime lastAccessTime, final FileTime createTime)
<A extends BasicFileAttributes> A readAttributes(final Path path, final Class<A> type, final LinkOption... options)
Map<String, Object> readAttributes(final Path path, final String attributes, final LinkOption... options)
void setAttribute(final Path path, final String attribute, final Object value, final LinkOption... options)
CopyOnWriteFSPath getRealPath(final CopyOnWriteFSPath path)

Fields & Constants (4)

SCHEME
DUMMY_DIRECTORY_VIEW
fs
attrs

String literals (7)

"x-mc-copy-on-write"
"basic"
"DELETE_ON_CLOSE is not supported by CowFS"
": not a regular file"
"Can't remove root"
": can't move root directory"
"CowFs does not support atomic move"

Full Source

CopyOnWriteFSProvider.java336 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.util.filefix.virtualfilesystem;
2
3
import java.io.IOException;
4
import java.net.URI;
5
import java.nio.channels.FileChannel;
6
import java.nio.channels.SeekableByteChannel;
7
import java.nio.file.AccessMode;
8
import java.nio.file.AtomicMoveNotSupportedException;
9
import java.nio.file.CopyOption;
10
import java.nio.file.DirectoryStream;
11
import java.nio.file.FileStore;
12
import java.nio.file.FileSystem;
13
import java.nio.file.Files;
14
import java.nio.file.LinkOption;
15
import java.nio.file.OpenOption;
16
import java.nio.file.Path;
17
import java.nio.file.StandardCopyOption;
18
import java.nio.file.StandardOpenOption;
19
import java.nio.file.DirectoryStream.Filter;
20
import java.nio.file.attribute.BasicFileAttributeView;
21
import java.nio.file.attribute.BasicFileAttributes;
22
import java.nio.file.attribute.FileAttribute;
23
import java.nio.file.attribute.FileAttributeView;
24
import java.nio.file.attribute.FileTime;
25
import java.nio.file.spi.FileSystemProvider;
26
import java.util.ArrayList;
27
import java.util.Iterator;
28
import java.util.List;
29
import java.util.Map;
30
import java.util.Objects;
31
import java.util.Set;
32
import net.minecraft.util.DummyFileAttributes;
33
import net.minecraft.util.filefix.virtualfilesystem.exception.CowFSDirectoryNotEmptyException;
34
import net.minecraft.util.filefix.virtualfilesystem.exception.CowFSFileAlreadyExistsException;
35
import net.minecraft.util.filefix.virtualfilesystem.exception.CowFSFileSystemException;
36
import net.minecraft.util.filefix.virtualfilesystem.exception.CowFSNoSuchFileException;
37
import org.jspecify.annotations.Nullable;
38
39
public class CopyOnWriteFSProvider extends FileSystemProvider {
40
	public static final String SCHEME = "x-mc-copy-on-write";
41
	private static final BasicFileAttributeView DUMMY_DIRECTORY_VIEW = new BasicFileAttributeView() {
42
		@Override
43
		public String name() {
44
			return "basic";
45
		}
46
47
		@Override
48
		public BasicFileAttributes readAttributes() {
49
			return DummyFileAttributes.DIRECTORY;
50
		}
51
52
		@Override
53
		public void setTimes(final FileTime lastModifiedTime, final FileTime lastAccessTime, final FileTime createTime) throws IOException {
54
		}
55
	};
56
	private final CopyOnWriteFileSystem fs;
57
58
	public CopyOnWriteFSProvider(final CopyOnWriteFileSystem fileSystem) {
59
		this.fs = fileSystem;
60
	}
61
62
	@Override
63
	public String getScheme() {
64
		return "x-mc-copy-on-write";
65
	}
66
67
	@Override
68
	public FileSystem newFileSystem(final URI uri, final Map<String, ?> env) throws IOException {
69
		throw new UnsupportedOperationException();
70
	}
71
72
	@Override
73
	public FileSystem getFileSystem(final URI uri) {
74
		throw new UnsupportedOperationException();
75
	}
76
77
	@Override
78
	public Path getPath(final URI uri) {
79
		throw new UnsupportedOperationException();
80
	}
81
82
	@Override
83
	public SeekableByteChannel newByteChannel(final Path path, final Set<? extends OpenOption> options, final FileAttribute<?>... attrs) throws IOException {
84
		return this.newChannel(path, options, attrs, Files::newByteChannel);
85
	}
86
87
	@Override
88
	public FileChannel newFileChannel(final Path path, final Set<? extends OpenOption> options, final FileAttribute<?>... attrs) throws IOException {
89
		return this.newChannel(path, options, attrs, FileChannel::open);
90
	}
91
92
	private synchronized <C> C newChannel(
93
		final Path path, final Set<? extends OpenOption> options, final FileAttribute<?>[] attrs, final CopyOnWriteFSProvider.ChannelFactory<C> channelFactory
94
	) throws IOException {
95
		CopyOnWriteFSPath cowPath = CopyOnWriteFSPath.asCow(path);
96
		if (options.contains(StandardOpenOption.DELETE_ON_CLOSE)) {
97
			throw new UnsupportedOperationException("DELETE_ON_CLOSE is not supported by CowFS");
98
		}
99
100
		return (C)(switch (this.fs.fileTree().byPathOrNull(cowPath)) {
101
			case null -> {
102
				if (!options.contains(StandardOpenOption.CREATE) && !options.contains(StandardOpenOption.CREATE_NEW)) {
103
					throw new CowFSNoSuchFileException(cowPath.toString());
104
				}
105
106
				DirectoryNode directoryNode = this.fs.fileTree().directoryByPath(Objects.requireNonNull(cowPath.getParent()));
107
				Path tempFile = this.fs.createTemporaryFilePath();
108
				C result = channelFactory.newChannel(tempFile, options, attrs);
109
				FileNode child = new FileNode(cowPath, tempFile, true);
110
				directoryNode.addChild(child);
111
				yield result;
112
			}
113
			case FileNode fileNode -> {
114
				if (wantsWrite(options)) {
115
					fileNode.ensureCopy();
116
				}
117
118
				yield channelFactory.newChannel(fileNode.storagePath(), options, attrs);
119
			}
120
			case DirectoryNode var14 -> throw new CowFSFileSystemException(cowPath + ": not a regular file");
121
			default -> throw new MatchException(null, null);
122
		});
123
	}
124
125
	private static boolean wantsWrite(final Set<? extends OpenOption> options) {
126
		return options.contains(StandardOpenOption.WRITE) || !options.contains(StandardOpenOption.READ) && options.contains(StandardOpenOption.APPEND);
127
	}
128
129
	@Override
130
	public synchronized DirectoryStream<Path> newDirectoryStream(final Path dir, final Filter<? super Path> filter) throws IOException {
131
		CopyOnWriteFSPath cowPath = CopyOnWriteFSPath.asCow(dir);
132
		DirectoryNode directoryNode = this.fs.fileTree().directoryByPath(cowPath);
133
		final List<Path> result = new ArrayList<>();
134
135
		for (Node childNode : directoryNode.children()) {
136
			Path path = childNode.path();
137
			if (filter.accept(path)) {
138
				result.add(path);
139
			}
140
		}
141
142
		return new DirectoryStream<Path>() {
143
			@Override
144
			public void close() {
145
			}
146
147
			@Override
148
			public Iterator<Path> iterator() {
149
				return result.iterator();
150
			}
151
		};
152
	}
153
154
	@Override
155
	public synchronized void createDirectory(final Path dir, final FileAttribute<?>... attrs) throws IOException {
156
		CopyOnWriteFSPath cowPath = CopyOnWriteFSPath.asCow(dir);
157
		CopyOnWriteFSPath parent = cowPath.getParent();
158
		if (parent == null) {
159
			throw new CowFSFileAlreadyExistsException(cowPath.toString());
160
		}
161
162
		DirectoryNode parentFolder = this.fs.fileTree().directoryByPath(parent);
163
		String folderName = Objects.requireNonNull(cowPath.getFileName()).toString();
164
		if (parentFolder.getChild(folderName) != null) {
165
			throw new CowFSFileAlreadyExistsException(cowPath.toString());
166
		}
167
168
		parentFolder.addChild(new DirectoryNode(cowPath));
169
	}
170
171
	@Override
172
	public synchronized void delete(final Path path) throws IOException {
173
		CopyOnWriteFSPath cowPath = CopyOnWriteFSPath.asCow(path);
174
		Node node = this.fs.fileTree().byPath(cowPath);
175
		if (node.parent == null) {
176
			throw new CowFSFileSystemException("Can't remove root");
177
		}
178
179
		String name = Objects.requireNonNull(node.name());
180
		if (node instanceof DirectoryNode directoryNode) {
181
			if (!directoryNode.children().isEmpty()) {
182
				throw new CowFSDirectoryNotEmptyException(cowPath.toString());
183
			}
184
		} else if (node instanceof FileNode fileNode) {
185
			fileNode.deleteCopy();
186
		}
187
188
		node.parent.removeChild(name);
189
	}
190
191
	@Override
192
	public void copy(final Path source, final Path target, final CopyOption... options) throws IOException {
193
		throw new UnsupportedOperationException();
194
	}
195
196
	@Override
197
	public synchronized void move(final Path source, final Path target, final CopyOption... options) throws IOException {
198
		CopyOnWriteFSPath sourceCow = CopyOnWriteFSPath.asCow(source);
199
		CopyOnWriteFSPath targetCow = CopyOnWriteFSPath.asCow(target);
200
		if (sourceCow.isRoot()) {
201
			throw new CowFSFileSystemException(sourceCow + ": can't move root directory");
202
		}
203
204
		boolean replaceExisting = false;
205
206
		for (CopyOption option : options) {
207
			if (option.equals(StandardCopyOption.ATOMIC_MOVE)) {
208
				throw new AtomicMoveNotSupportedException(sourceCow.toString(), targetCow.toString(), "CowFs does not support atomic move");
209
			}
210
211
			if (option.equals(StandardCopyOption.REPLACE_EXISTING)) {
212
				replaceExisting = true;
213
			}
214
		}
215
216
		Node sourceNode = this.fs.fileTree().byPathOrNull(sourceCow);
217
		if (sourceNode == null) {
218
			throw new CowFSNoSuchFileException(sourceCow.toString());
219
		}
220
221
		CopyOnWriteFSPath parent = targetCow.toAbsolutePath().getParent();
222
		if (parent == null) {
223
			throw new CowFSFileAlreadyExistsException(targetCow.toString());
224
		}
225
226
		if (this.fs.fileTree().byPathOrNull(parent) instanceof DirectoryNode folderTarget) {
227
			String newName = Objects.requireNonNull(targetCow.getFileName()).toString();
228
			Node oldChild = folderTarget.getChild(newName);
229
			if (oldChild != null) {
230
				if (oldChild.equals(sourceNode)) {
231
					return;
232
				}
233
234
				if (!replaceExisting) {
235
					throw new CowFSFileAlreadyExistsException(targetCow.toString());
236
				}
237
238
				folderTarget.removeChild(newName);
239
			}
240
241
			Objects.requireNonNull(sourceNode.parent).removeChild(Objects.requireNonNull(sourceNode.name()));
242
			sourceNode.setPath(targetCow);
243
			folderTarget.addChild(sourceNode);
244
		} else {
245
			throw new CowFSNoSuchFileException(targetCow.toString());
246
		}
247
	}
248
249
	@Override
250
	public boolean isSameFile(final Path path, final Path path2) {
251
		throw new UnsupportedOperationException();
252
	}
253
254
	@Override
255
	public boolean isHidden(final Path path) {
256
		throw new UnsupportedOperationException();
257
	}
258
259
	@Override
260
	public FileStore getFileStore(final Path path) {
261
		throw new UnsupportedOperationException();
262
	}
263
264
	@Override
265
	public synchronized void checkAccess(final Path path, final AccessMode... modes) throws IOException {
266
		CopyOnWriteFSPath cowPath = CopyOnWriteFSPath.asCow(path);
267
		Node node = this.fs.fileTree().byPath(cowPath);
268
269
		Path checkPath = switch (node) {
270
			case DirectoryNode var9 -> this.fs.tmpDirectory();
271
			case FileNode file -> file.storagePath();
272
			default -> throw new MatchException(null, null);
273
		};
274
		checkPath.getFileSystem().provider().checkAccess(checkPath, modes);
275
	}
276
277
	@Override
278
	public synchronized <V extends FileAttributeView> @Nullable V getFileAttributeView(final Path path, final Class<V> type, final LinkOption... options) {
279
		final CopyOnWriteFSPath cowPath = CopyOnWriteFSPath.asCow(path);
280
		Node node = this.fs.fileTree().byPathOrNull(cowPath);
281
282
		return (V)(switch (node) {
283
			case null -> type == BasicFileAttributeView.class ? new BasicFileAttributeView() {
284
				@Override
285
				public String name() {
286
					return "basic";
287
				}
288
289
				@Override
290
				public BasicFileAttributes readAttributes() throws IOException {
291
					throw new CowFSNoSuchFileException(cowPath.toString());
292
				}
293
294
				@Override
295
				public void setTimes(final FileTime lastModifiedTime, final FileTime lastAccessTime, final FileTime createTime) throws IOException {
296
					throw new CowFSNoSuchFileException(cowPath.toString());
297
				}
298
			} : null;
299
			case DirectoryNode var9 -> type == BasicFileAttributeView.class ? DUMMY_DIRECTORY_VIEW : null;
300
			case FileNode file -> Files.getFileAttributeView(file.storagePath(), type, options);
301
			default -> throw new MatchException(null, null);
302
		});
303
	}
304
305
	@Override
306
	public synchronized <A extends BasicFileAttributes> A readAttributes(final Path path, final Class<A> type, final LinkOption... options) throws IOException {
307
		CopyOnWriteFSPath cowPath = CopyOnWriteFSPath.asCow(path);
308
		Node node = this.fs.fileTree().byPath(cowPath);
309
310
		return (A)(switch (node) {
311
			case DirectoryNode var9 -> DummyFileAttributes.DIRECTORY;
312
			case FileNode file -> Files.readAttributes(file.storagePath(), type, options);
313
			default -> throw new MatchException(null, null);
314
		});
315
	}
316
317
	@Override
318
	public Map<String, Object> readAttributes(final Path path, final String attributes, final LinkOption... options) {
319
		throw new UnsupportedOperationException();
320
	}
321
322
	@Override
323
	public void setAttribute(final Path path, final String attribute, final Object value, final LinkOption... options) {
324
		throw new UnsupportedOperationException();
325
	}
326
327
	public synchronized CopyOnWriteFSPath getRealPath(final CopyOnWriteFSPath path) throws CowFSNoSuchFileException {
328
		return this.fs.fileTree().byPath(path.toAbsolutePath()).path;
329
	}
330
331
	@FunctionalInterface
332
	private interface ChannelFactory<C> {
333
		C newChannel(Path path, Set<? extends OpenOption> options, FileAttribute<?>... attrs) throws IOException;
334
	}
335
}
336

Decompiled Minecraft 26.3 source of CopyOnWriteFSProvider (net/minecraft/util/filefix/virtualfilesystem/CopyOnWriteFSProvider.java) with Mojang official mappings, stored verbatim on the site.