Skip to content

Instantly share code, notes, and snippets.

@DenisVerkhoturov
Last active December 24, 2019 08:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DenisVerkhoturov/3119cef0d1a26006923503c1daf79957 to your computer and use it in GitHub Desktop.
Save DenisVerkhoturov/3119cef0d1a26006923503c1daf79957 to your computer and use it in GitHub Desktop.
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.AclFileAttributeView;
import java.nio.file.attribute.AclEntry;
import java.nio.file.attribute.AclEntryType;
import java.nio.file.attribute.AclEntryPermission;
import java.nio.file.attribute.UserPrincipal;
public class PathUtils {
public static Path inaccessible(final Path path) throws IOException {
final String os = System.getProperty("os.name").toLowerCase();
if (os.contains("win")) {
final UserPrincipal currentUser =
path
.getFileSystem()
.getUserPrincipalLookupService()
.lookupPrincipalByName(System.getProperty("user.name"));
final AclFileAttributeView view = Files.getFileAttributeView(path, AclFileAttributeView.class);
final AclEntry denyReadAndWrite =
AclEntry
.newBuilder()
.setType(AclEntryType.DENY)
.setPrincipal(currentUser)
.setPermissions(AclEntryPermission.READ_DATA, AclEntryPermission.ADD_FILE)
.build();
final java.util.List<AclEntry> acl = view.getAcl();
acl.add(0, denyReadAndWrite);
view.setAcl(acl);
} else {
path.toFile().setReadable(false);
path.toFile().setWritable(false);
}
return path;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment