DirectoryLock .java

net.minecraft.util.DirectoryLock (class, 98 lines): members: LockException, lockFile, LOCK_FILE, DUMMY, lock, isLocked, isValid, close, create.

net.minecraft.utilclass98 linescommon2.5 KB

Implements

API Surface (5)

DirectoryLock create(final Path dir)
void close()
boolean isValid()
boolean isLocked(final Path dir)
DirectoryLock.LockException alreadyLocked(final Path path)

Fields & Constants (4)

LOCK_FILE
DUMMY
lockFile
lock

String literals (1)

"session.lock"

Code Graph: Dependencies (2)

Edges (calls / imports / inherits / references) resolved by graphify from the AST. Every chip opens the class source.

Full Source

DirectoryLock.java98 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.util;
2
3
import java.io.IOException;
4
import java.nio.ByteBuffer;
5
import java.nio.channels.FileChannel;
6
import java.nio.channels.FileLock;
7
import java.nio.charset.StandardCharsets;
8
import java.nio.file.AccessDeniedException;
9
import java.nio.file.NoSuchFileException;
10
import java.nio.file.Path;
11
import java.nio.file.StandardOpenOption;
12
13
public class DirectoryLock implements AutoCloseable {
14
	public static final String LOCK_FILE = "session.lock";
15
	private final FileChannel lockFile;
16
	private final FileLock lock;
17
	private static final ByteBuffer DUMMY;
18
19
	public static DirectoryLock create(final Path dir) throws IOException {
20
		Path lockPath = dir.resolve("session.lock");
21
		FileUtil.createDirectoriesSafe(dir);
22
		FileChannel lockFile = FileChannel.open(lockPath, StandardOpenOption.CREATE, StandardOpenOption.WRITE);
23
24
		try {
25
			lockFile.write(DUMMY.duplicate());
26
			lockFile.force(true);
27
			FileLock lock = lockFile.tryLock();
28
			if (lock == null) {
29
				throw DirectoryLock.LockException.alreadyLocked(lockPath);
30
			} else {
31
				return new DirectoryLock(lockFile, lock);
32
			}
33
		} catch (IOException e) {
34
			try {
35
				lockFile.close();
36
			} catch (IOException nested) {
37
				e.addSuppressed(nested);
38
			}
39
40
			throw e;
41
		}
42
	}
43
44
	private DirectoryLock(final FileChannel lockFile, final FileLock lock) {
45
		this.lockFile = lockFile;
46
		this.lock = lock;
47
	}
48
49
	@Override
50
	public void close() throws IOException {
51
		try {
52
			if (this.lock.isValid()) {
53
				this.lock.release();
54
			}
55
		} finally {
56
			if (this.lockFile.isOpen()) {
57
				this.lockFile.close();
58
			}
59
		}
60
	}
61
62
	public boolean isValid() {
63
		return this.lock.isValid();
64
	}
65
66
	public static boolean isLocked(final Path dir) throws IOException {
67
		Path lockPath = dir.resolve("session.lock");
68
69
		try (
70
			FileChannel lockFile = FileChannel.open(lockPath, StandardOpenOption.WRITE);
71
			FileLock maybeLock = lockFile.tryLock();
72
		) {
73
			return maybeLock == null;
74
		} catch (AccessDeniedException e) {
75
			return true;
76
		} catch (NoSuchFileException e) {
77
			return false;
78
		}
79
	}
80
81
	static {
82
		byte[] chars = "☃".getBytes(StandardCharsets.UTF_8);
83
		DUMMY = ByteBuffer.allocateDirect(chars.length);
84
		DUMMY.put(chars);
85
		DUMMY.flip();
86
	}
87
88
	public static class LockException extends IOException {
89
		private LockException(final Path path, final String message) {
90
			super(path.toAbsolutePath() + ": " + message);
91
		}
92
93
		public static DirectoryLock.LockException alreadyLocked(final Path path) {
94
			return new DirectoryLock.LockException(path, "already locked (possibly by other Minecraft instance?)");
95
		}
96
	}
97
}
98

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