diff --git a/common/src/main/java/me/lucko/luckperms/common/storage/backing/FlatfileBacking.java b/common/src/main/java/me/lucko/luckperms/common/storage/backing/FlatfileBacking.java index cd63c191..94a4e791 100644 --- a/common/src/main/java/me/lucko/luckperms/common/storage/backing/FlatfileBacking.java +++ b/common/src/main/java/me/lucko/luckperms/common/storage/backing/FlatfileBacking.java @@ -63,15 +63,6 @@ import java.util.stream.Collectors; public abstract class FlatfileBacking extends AbstractBacking { private static final String LOG_FORMAT = "%s(%s): [%s] %s(%s) --> %s"; - protected static T call(Callable c, T def) { - try { - return c.call(); - } catch (Exception e) { - e.printStackTrace(); - return def; - } - } - private final Logger actionLogger = Logger.getLogger("lp_actions"); private Map uuidCache = new ConcurrentHashMap<>(); @@ -216,6 +207,16 @@ public abstract class FlatfileBacking extends AbstractBacking { plugin.applyToFileWatcher(fileWatcher -> fileWatcher.registerChange(type, file.getName())); } + protected T call(String file, Callable c, T def) { + try { + return c.call(); + } catch (Exception e) { + plugin.getLog().warn("Exception thrown whilst performing i/o: " + file); + e.printStackTrace(); + return def; + } + } + @Override public boolean logAction(LogEntry entry) { actionLogger.info(String.format(LOG_FORMAT, @@ -266,7 +267,7 @@ public abstract class FlatfileBacking extends AbstractBacking { public boolean deleteGroup(Group group) { group.getIoLock().lock(); try { - return call(() -> { + return call(group.getName(), () -> { File groupFile = new File(groupsDir, group.getName() + fileExtension); registerFileAction("groups", groupFile); @@ -301,7 +302,7 @@ public abstract class FlatfileBacking extends AbstractBacking { public boolean deleteTrack(Track track) { track.getIoLock().lock(); try { - return call(() -> { + return call(track.getName(), () -> { File trackFile = new File(tracksDir, track.getName() + fileExtension); registerFileAction("tracks", trackFile); diff --git a/common/src/main/java/me/lucko/luckperms/common/storage/backing/JSONBacking.java b/common/src/main/java/me/lucko/luckperms/common/storage/backing/JSONBacking.java index 6172f828..934968e8 100644 --- a/common/src/main/java/me/lucko/luckperms/common/storage/backing/JSONBacking.java +++ b/common/src/main/java/me/lucko/luckperms/common/storage/backing/JSONBacking.java @@ -94,28 +94,31 @@ public class JSONBacking extends FlatfileBacking { @Override public boolean applyBulkUpdate(BulkUpdate bulkUpdate) { - return call(() -> { + return call("null", () -> { if (bulkUpdate.getDataType().isIncludingUsers()) { File[] files = usersDir.listFiles((dir, name1) -> name1.endsWith(".json")); if (files == null) return false; for (File file : files) { - registerFileAction("users", file); + call(file.getName(), () -> { + registerFileAction("users", file); - JsonObject object = readObjectFromFile(file); + JsonObject object = readObjectFromFile(file); - Set nodes = new HashSet<>(); - nodes.addAll(deserializePermissions(object.get("permissions").getAsJsonArray())); + Set nodes = new HashSet<>(); + nodes.addAll(deserializePermissions(object.get("permissions").getAsJsonArray())); - Set results = nodes.stream() - .map(n -> Optional.ofNullable(bulkUpdate.apply(n))) - .filter(Optional::isPresent) - .map(Optional::get) - .collect(Collectors.toSet()); + Set results = nodes.stream() + .map(n -> Optional.ofNullable(bulkUpdate.apply(n))) + .filter(Optional::isPresent) + .map(Optional::get) + .collect(Collectors.toSet()); - object.add("permissions", serializePermissions(results)); + object.add("permissions", serializePermissions(results)); - writeElementToFile(file, object); + writeElementToFile(file, object); + return true; + }, true); } } @@ -124,22 +127,25 @@ public class JSONBacking extends FlatfileBacking { if (files == null) return false; for (File file : files) { - registerFileAction("groups", file); + call(file.getName(), () -> { + registerFileAction("groups", file); - JsonObject object = readObjectFromFile(file); + JsonObject object = readObjectFromFile(file); - Set nodes = new HashSet<>(); - nodes.addAll(deserializePermissions(object.get("permissions").getAsJsonArray())); + Set nodes = new HashSet<>(); + nodes.addAll(deserializePermissions(object.get("permissions").getAsJsonArray())); - Set results = nodes.stream() - .map(n -> Optional.ofNullable(bulkUpdate.apply(n))) - .filter(Optional::isPresent) - .map(Optional::get) - .collect(Collectors.toSet()); + Set results = nodes.stream() + .map(n -> Optional.ofNullable(bulkUpdate.apply(n))) + .filter(Optional::isPresent) + .map(Optional::get) + .collect(Collectors.toSet()); - object.add("permissions", serializePermissions(results)); + object.add("permissions", serializePermissions(results)); - writeElementToFile(file, object); + writeElementToFile(file, object); + return true; + }, true); } } @@ -152,7 +158,7 @@ public class JSONBacking extends FlatfileBacking { User user = plugin.getUserManager().getOrMake(UserIdentifier.of(uuid, username)); user.getIoLock().lock(); try { - return call(() -> { + return call(uuid.toString(), () -> { File userFile = new File(usersDir, uuid.toString() + ".json"); registerFileAction("users", userFile); @@ -199,7 +205,7 @@ public class JSONBacking extends FlatfileBacking { public boolean saveUser(User user) { user.getIoLock().lock(); try { - return call(() -> { + return call(user.getUuid().toString(), () -> { File userFile = new File(usersDir, user.getUuid().toString() + ".json"); registerFileAction("users", userFile); @@ -236,29 +242,32 @@ public class JSONBacking extends FlatfileBacking { @Override public boolean cleanupUsers() { - return call(() -> { + return call("null", () -> { File[] files = usersDir.listFiles((dir, name1) -> name1.endsWith(".json")); if (files == null) return false; for (File file : files) { - registerFileAction("users", file); + call(file.getName(), () -> { + registerFileAction("users", file); - JsonObject object = readObjectFromFile(file); + JsonObject object = readObjectFromFile(file); - Set nodes = new HashSet<>(); - nodes.addAll(deserializePermissions(object.get("permissions").getAsJsonArray())); + Set nodes = new HashSet<>(); + nodes.addAll(deserializePermissions(object.get("permissions").getAsJsonArray())); - boolean shouldDelete = false; - if (nodes.size() == 1) { - for (NodeModel e : nodes) { - // There's only one - shouldDelete = e.getPermission().equalsIgnoreCase("group.default") && e.isValue(); + boolean shouldDelete = false; + if (nodes.size() == 1) { + for (NodeModel e : nodes) { + // There's only one + shouldDelete = e.getPermission().equalsIgnoreCase("group.default") && e.isValue(); + } } - } - if (shouldDelete) { - file.delete(); - } + if (shouldDelete) { + file.delete(); + } + return true; + }, true); } return true; }, false); @@ -267,27 +276,30 @@ public class JSONBacking extends FlatfileBacking { @Override public List> getUsersWithPermission(String permission) { ImmutableList.Builder> held = ImmutableList.builder(); - boolean success = call(() -> { + boolean success = call("null", () -> { File[] files = usersDir.listFiles((dir, name1) -> name1.endsWith(".json")); if (files == null) return false; for (File file : files) { - registerFileAction("users", file); + call(file.getName(), () -> { + registerFileAction("users", file); - UUID holder = UUID.fromString(file.getName().substring(0, file.getName().length() - 5)); + UUID holder = UUID.fromString(file.getName().substring(0, file.getName().length() - 5)); - JsonObject object = readObjectFromFile(file); + JsonObject object = readObjectFromFile(file); - Set nodes = new HashSet<>(); - nodes.addAll(deserializePermissions(object.get("permissions").getAsJsonArray())); + Set nodes = new HashSet<>(); + nodes.addAll(deserializePermissions(object.get("permissions").getAsJsonArray())); - for (NodeModel e : nodes) { - if (!e.getPermission().equalsIgnoreCase(permission)) { - continue; + for (NodeModel e : nodes) { + if (!e.getPermission().equalsIgnoreCase(permission)) { + continue; + } + + held.add(NodeHeldPermission.of(holder, e)); } - - held.add(NodeHeldPermission.of(holder, e)); - } + return true; + }, true); } return true; }, false); @@ -299,7 +311,7 @@ public class JSONBacking extends FlatfileBacking { Group group = plugin.getGroupManager().getOrMake(name); group.getIoLock().lock(); try { - return call(() -> { + return call(name, () -> { File groupFile = new File(groupsDir, name + ".json"); registerFileAction("groups", groupFile); @@ -336,7 +348,7 @@ public class JSONBacking extends FlatfileBacking { Group group = plugin.getGroupManager().getOrMake(name); group.getIoLock().lock(); try { - return call(() -> { + return call(name, () -> { File groupFile = new File(groupsDir, name + ".json"); registerFileAction("groups", groupFile); @@ -359,7 +371,7 @@ public class JSONBacking extends FlatfileBacking { public boolean saveGroup(Group group) { group.getIoLock().lock(); try { - return call(() -> { + return call(group.getName(), () -> { File groupFile = new File(groupsDir, group.getName() + ".json"); registerFileAction("groups", groupFile); @@ -386,27 +398,30 @@ public class JSONBacking extends FlatfileBacking { @Override public List> getGroupsWithPermission(String permission) { ImmutableList.Builder> held = ImmutableList.builder(); - boolean success = call(() -> { + boolean success = call("null", () -> { File[] files = groupsDir.listFiles((dir, name1) -> name1.endsWith(".json")); if (files == null) return false; for (File file : files) { - registerFileAction("groups", file); + call(file.getName(), () -> { + registerFileAction("groups", file); - String holder = file.getName().substring(0, file.getName().length() - 5); + String holder = file.getName().substring(0, file.getName().length() - 5); - JsonObject object = readObjectFromFile(file); + JsonObject object = readObjectFromFile(file); - Set nodes = new HashSet<>(); - nodes.addAll(deserializePermissions(object.get("permissions").getAsJsonArray())); + Set nodes = new HashSet<>(); + nodes.addAll(deserializePermissions(object.get("permissions").getAsJsonArray())); - for (NodeModel e : nodes) { - if (!e.getPermission().equalsIgnoreCase(permission)) { - continue; + for (NodeModel e : nodes) { + if (!e.getPermission().equalsIgnoreCase(permission)) { + continue; + } + + held.add(NodeHeldPermission.of(holder, e)); } - - held.add(NodeHeldPermission.of(holder, e)); - } + return true; + }, true); } return true; }, false); @@ -418,7 +433,7 @@ public class JSONBacking extends FlatfileBacking { Track track = plugin.getTrackManager().getOrMake(name); track.getIoLock().lock(); try { - return call(() -> { + return call(name, () -> { File trackFile = new File(tracksDir, name + ".json"); registerFileAction("tracks", trackFile); @@ -459,7 +474,7 @@ public class JSONBacking extends FlatfileBacking { Track track = plugin.getTrackManager().getOrMake(name); track.getIoLock().lock(); try { - return call(() -> { + return call(name, () -> { File trackFile = new File(tracksDir, name + ".json"); registerFileAction("tracks", trackFile); @@ -485,7 +500,7 @@ public class JSONBacking extends FlatfileBacking { public boolean saveTrack(Track track) { track.getIoLock().lock(); try { - return call(() -> { + return call(track.getName(), () -> { File trackFile = new File(tracksDir, track.getName() + ".json"); registerFileAction("tracks", trackFile); diff --git a/common/src/main/java/me/lucko/luckperms/common/storage/backing/YAMLBacking.java b/common/src/main/java/me/lucko/luckperms/common/storage/backing/YAMLBacking.java index a10ac4ee..3e1639ba 100644 --- a/common/src/main/java/me/lucko/luckperms/common/storage/backing/YAMLBacking.java +++ b/common/src/main/java/me/lucko/luckperms/common/storage/backing/YAMLBacking.java @@ -98,27 +98,30 @@ public class YAMLBacking extends FlatfileBacking { @Override public boolean applyBulkUpdate(BulkUpdate bulkUpdate) { - return call(() -> { + return call("null", () -> { if (bulkUpdate.getDataType().isIncludingUsers()) { File[] files = usersDir.listFiles((dir, name1) -> name1.endsWith(".yml")); if (files == null) return false; for (File file : files) { - registerFileAction("users", file); + call(file.getName(), () -> { + registerFileAction("users", file); - Map values = readMapFromFile(file); + Map values = readMapFromFile(file); - Set nodes = new HashSet<>(); - nodes.addAll(deserializePermissions((List) values.get("permissions"))); + Set nodes = new HashSet<>(); + nodes.addAll(deserializePermissions((List) values.get("permissions"))); - Set results = nodes.stream() - .map(n -> Optional.ofNullable(bulkUpdate.apply(n))) - .filter(Optional::isPresent) - .map(Optional::get) - .collect(Collectors.toSet()); + Set results = nodes.stream() + .map(n -> Optional.ofNullable(bulkUpdate.apply(n))) + .filter(Optional::isPresent) + .map(Optional::get) + .collect(Collectors.toSet()); - values.put("permissions", serializePermissions(results)); - writeMapToFile(file, values); + values.put("permissions", serializePermissions(results)); + writeMapToFile(file, values); + return true; + }, true); } } @@ -127,21 +130,24 @@ public class YAMLBacking extends FlatfileBacking { if (files == null) return false; for (File file : files) { - registerFileAction("groups", file); + call(file.getName(), () -> { + registerFileAction("groups", file); - Map values = readMapFromFile(file); + Map values = readMapFromFile(file); - Set nodes = new HashSet<>(); - nodes.addAll(deserializePermissions((List) values.get("permissions"))); + Set nodes = new HashSet<>(); + nodes.addAll(deserializePermissions((List) values.get("permissions"))); - Set results = nodes.stream() - .map(n -> Optional.ofNullable(bulkUpdate.apply(n))) - .filter(Optional::isPresent) - .map(Optional::get) - .collect(Collectors.toSet()); + Set results = nodes.stream() + .map(n -> Optional.ofNullable(bulkUpdate.apply(n))) + .filter(Optional::isPresent) + .map(Optional::get) + .collect(Collectors.toSet()); - values.put("permissions", serializePermissions(results)); - writeMapToFile(file, values); + values.put("permissions", serializePermissions(results)); + writeMapToFile(file, values); + return true; + }, true); } } @@ -154,7 +160,7 @@ public class YAMLBacking extends FlatfileBacking { User user = plugin.getUserManager().getOrMake(UserIdentifier.of(uuid, username)); user.getIoLock().lock(); try { - return call(() -> { + return call(uuid.toString(), () -> { File userFile = new File(usersDir, uuid.toString() + ".yml"); registerFileAction("users", userFile); if (userFile.exists()) { @@ -201,7 +207,7 @@ public class YAMLBacking extends FlatfileBacking { public boolean saveUser(User user) { user.getIoLock().lock(); try { - return call(() -> { + return call(user.getUuid().toString(), () -> { File userFile = new File(usersDir, user.getUuid().toString() + ".yml"); registerFileAction("users", userFile); if (!GenericUserManager.shouldSave(user)) { @@ -237,29 +243,32 @@ public class YAMLBacking extends FlatfileBacking { @Override public boolean cleanupUsers() { - return call(() -> { + return call("null", () -> { File[] files = usersDir.listFiles((dir, name1) -> name1.endsWith(".yml")); if (files == null) return false; for (File file : files) { - registerFileAction("users", file); + call(file.getName(), () -> { + registerFileAction("users", file); - Map values = readMapFromFile(file); + Map values = readMapFromFile(file); - Set nodes = new HashSet<>(); - nodes.addAll(deserializePermissions((List) values.get("permissions"))); + Set nodes = new HashSet<>(); + nodes.addAll(deserializePermissions((List) values.get("permissions"))); - boolean shouldDelete = false; - if (nodes.size() == 1) { - for (NodeModel e : nodes) { - // There's only one - shouldDelete = e.getPermission().equalsIgnoreCase("group.default") && e.isValue(); + boolean shouldDelete = false; + if (nodes.size() == 1) { + for (NodeModel e : nodes) { + // There's only one + shouldDelete = e.getPermission().equalsIgnoreCase("group.default") && e.isValue(); + } } - } - if (shouldDelete) { - file.delete(); - } + if (shouldDelete) { + file.delete(); + } + return true; + }, true); } return true; }, false); @@ -268,27 +277,30 @@ public class YAMLBacking extends FlatfileBacking { @Override public List> getUsersWithPermission(String permission) { ImmutableList.Builder> held = ImmutableList.builder(); - boolean success = call(() -> { + boolean success = call("null", () -> { File[] files = usersDir.listFiles((dir, name1) -> name1.endsWith(".yml")); if (files == null) return false; for (File file : files) { - registerFileAction("users", file); + call(file.getName(), () -> { + registerFileAction("users", file); - UUID holder = UUID.fromString(file.getName().substring(0, file.getName().length() - 4)); + UUID holder = UUID.fromString(file.getName().substring(0, file.getName().length() - 4)); - Map values = readMapFromFile(file); + Map values = readMapFromFile(file); - Set nodes = new HashSet<>(); - nodes.addAll(deserializePermissions((List) values.get("permissions"))); + Set nodes = new HashSet<>(); + nodes.addAll(deserializePermissions((List) values.get("permissions"))); - for (NodeModel e : nodes) { - if (!e.getPermission().equalsIgnoreCase(permission)) { - continue; + for (NodeModel e : nodes) { + if (!e.getPermission().equalsIgnoreCase(permission)) { + continue; + } + + held.add(NodeHeldPermission.of(holder, e)); } - - held.add(NodeHeldPermission.of(holder, e)); - } + return true; + }, true); } return true; }, false); @@ -300,7 +312,7 @@ public class YAMLBacking extends FlatfileBacking { Group group = plugin.getGroupManager().getOrMake(name); group.getIoLock().lock(); try { - return call(() -> { + return call(name, () -> { File groupFile = new File(groupsDir, name + ".yml"); registerFileAction("groups", groupFile); @@ -335,7 +347,7 @@ public class YAMLBacking extends FlatfileBacking { Group group = plugin.getGroupManager().getOrMake(name); group.getIoLock().lock(); try { - return call(() -> { + return call(name, () -> { File groupFile = new File(groupsDir, name + ".yml"); registerFileAction("groups", groupFile); @@ -358,7 +370,7 @@ public class YAMLBacking extends FlatfileBacking { public boolean saveGroup(Group group) { group.getIoLock().lock(); try { - return call(() -> { + return call(group.getName(), () -> { File groupFile = new File(groupsDir, group.getName() + ".yml"); registerFileAction("groups", groupFile); @@ -385,27 +397,31 @@ public class YAMLBacking extends FlatfileBacking { @Override public List> getGroupsWithPermission(String permission) { ImmutableList.Builder> held = ImmutableList.builder(); - boolean success = call(() -> { + boolean success = call("null", () -> { File[] files = groupsDir.listFiles((dir, name1) -> name1.endsWith(".yml")); if (files == null) return false; for (File file : files) { - registerFileAction("groups", file); + call(file.getName(), () -> { + registerFileAction("groups", file); - String holder = file.getName().substring(0, file.getName().length() - 4); + String holder = file.getName().substring(0, file.getName().length() - 4); - Map values = readMapFromFile(file); + Map values = readMapFromFile(file); - Set nodes = new HashSet<>(); - nodes.addAll(deserializePermissions((List) values.get("permissions"))); + Set nodes = new HashSet<>(); + nodes.addAll(deserializePermissions((List) values.get("permissions"))); - for (NodeModel e : nodes) { - if (!e.getPermission().equalsIgnoreCase(permission)) { - continue; + for (NodeModel e : nodes) { + if (!e.getPermission().equalsIgnoreCase(permission)) { + continue; + } + + held.add(NodeHeldPermission.of(holder, e)); } - held.add(NodeHeldPermission.of(holder, e)); - } + return true; + }, true); } return true; }, false); @@ -417,7 +433,7 @@ public class YAMLBacking extends FlatfileBacking { Track track = plugin.getTrackManager().getOrMake(name); track.getIoLock().lock(); try { - return call(() -> { + return call(name, () -> { File trackFile = new File(tracksDir, name + ".yml"); registerFileAction("tracks", trackFile); @@ -450,7 +466,7 @@ public class YAMLBacking extends FlatfileBacking { Track track = plugin.getTrackManager().getOrMake(name); track.getIoLock().lock(); try { - return call(() -> { + return call(name, () -> { File trackFile = new File(tracksDir, name + ".yml"); registerFileAction("tracks", trackFile); @@ -471,7 +487,7 @@ public class YAMLBacking extends FlatfileBacking { public boolean saveTrack(Track track) { track.getIoLock().lock(); try { - return call(() -> { + return call(track.getName(), () -> { File trackFile = new File(tracksDir, track.getName() + ".yml"); registerFileAction("tracks", trackFile);