Skip to content

Commit

Permalink
Make stat files readable for other users on linux
Browse files Browse the repository at this point in the history
  • Loading branch information
LostLuma committed Aug 19, 2023
1 parent d493b7e commit d974835
Showing 1 changed file with 14 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.nio.file.attribute.FileAttribute;
import java.nio.file.attribute.PosixFilePermissions;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
Expand All @@ -24,7 +27,9 @@ public class ServerPlayerStats {

// Use Minecraft logger as it is already properly set up
private static final Logger LOGGER = Logger.getLogger("Minecraft");

private static final Path STATS = Path.of("world").resolve("stats");
private static final FileAttribute<?>[] DEFAULT_ATTRIBUTES = getDefaultFileAttributes();

public ServerPlayerStats(PlayerEntity player) {
this.name = player.getSourceName();
Expand Down Expand Up @@ -67,7 +72,7 @@ public void save() {
try {
Files.createDirectories(STATS);

Path temp = Files.createTempFile(STATS, this.name, ".json");
Path temp = Files.createTempFile(STATS, this.name, ".json", DEFAULT_ATTRIBUTES);
Files.writeString(temp, this.serialize(), StandardCharsets.UTF_8);

Files.move(temp, path, StandardCopyOption.ATOMIC_MOVE); // Prevent issues on server crash
Expand Down Expand Up @@ -108,4 +113,12 @@ public String serialize() {

return result.toString();
}

private static FileAttribute<?>[] getDefaultFileAttributes() {
if (!System.getProperty("os.name").toLowerCase(Locale.ROOT).contains("linux")) {
return new FileAttribute[0];
}

return new FileAttribute[]{PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString("rw-r--r--"))};
}
}

0 comments on commit d974835

Please sign in to comment.