Skip to content

Commit

Permalink
Add permission-db cleanup on startup
Browse files Browse the repository at this point in the history
  • Loading branch information
Toilal committed Jan 9, 2019
1 parent ee9039f commit 9d85286
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
8 changes: 6 additions & 2 deletions src/main/java/world/gfi/nfs4j/api/Api.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package world.gfi.nfs4j.api;

import com.google.gson.JsonParseException;
import com.google.gson.JsonSyntaxException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import world.gfi.nfs4j.Daemon;
import world.gfi.nfs4j.config.ApiConfig;
import world.gfi.nfs4j.config.ShareConfig;
Expand All @@ -23,6 +24,8 @@ public class Api {
private final Daemon daemon;
private final ApiConfig config;

private static final Logger logger = LoggerFactory.getLogger(Api.class);

public Api(ApiConfig config, Daemon daemon) {
this.config = config;
this.daemon = daemon;
Expand All @@ -48,10 +51,11 @@ public void start() {
}
});
}

exception(Exception.class, (exception, req, res) -> {
res.status(500);
res.type("application/json");
logger.error(exception.getMessage(), exception);
Error error = new Error(exception.getMessage());
res.body(new JsonTransformer().render(error));
});
Expand Down
15 changes: 13 additions & 2 deletions src/main/java/world/gfi/nfs4j/fs/permission/PermissionsMapDb.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class PermissionsMapDb<A extends BasicFileAttributes> implements Permissi
private final ConcurrentMap<String, Integer> gidMap;
private final PermissionsReader<A> defaultPermissions;
private final FileIdReader<A> idReader;
private final ShareConfig share;

public static String getFilename(String alias) {
if (alias == null) {
Expand All @@ -45,12 +46,13 @@ public static String getFilename(String alias) {
}

public PermissionsMapDb(ShareConfig share, String alias, PermissionsReader<A> defaultPermissions, FileIdReader<A> idReader) throws IOException {
this.share = share;
this.defaultPermissions = defaultPermissions;
this.idReader = idReader;

File dbFile;
if (share.isLocalMetadata()) {
String basePath = share.getPath().toString();
if (this.share.isLocalMetadata()) {
String basePath = this.share.getPath().toString();
Path directory = Paths.get(basePath, ".nfs4j").normalize();
Files.createDirectories(directory);
dbFile = Paths.get(directory.toString(), getFilename(alias)).normalize().toFile();
Expand All @@ -70,6 +72,15 @@ public PermissionsMapDb(ShareConfig share, String alias, PermissionsReader<A> de
this.maskMap = db.hashMap("mask", Serializer.STRING, Serializer.INTEGER).createOrOpen();
this.uidMap = db.hashMap("uid", Serializer.STRING, Serializer.INTEGER).createOrOpen();
this.gidMap = db.hashMap("gid", Serializer.STRING, Serializer.INTEGER).createOrOpen();

this.cleanup();
}

public void cleanup() {
this.id.keySet().removeIf(p -> !new File(p).exists());
this.maskMap.keySet().removeIf(p -> !new File(p).exists());
this.uidMap.keySet().removeIf(p -> !new File(p).exists());
this.gidMap.keySet().removeIf(p -> !new File(p).exists());
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ public long getFileId(Path path, A attrs) {
try {
return JnaWindowsUtils.getFileId(path);
} catch (LastErrorException e) {
LOG.warn("Can't read File id with JNA. Falling back to creation time.", e);
if (e.getErrorCode() == 5 || e.getErrorCode() == 32) {
LOG.debug("Can't read File id with JNA on " + path.normalize().toString() + ". Falling back to creation time. (Error Code: " + e.getErrorCode() + ")");
} else {
LOG.warn("Can't read File id with JNA on " + path.normalize().toString() + ". Falling back to creation time. (Error Code: " + e.getErrorCode() + ")");
}
return super.getFileId(path, attrs);
}
}
Expand Down

0 comments on commit 9d85286

Please sign in to comment.