Last active
December 24, 2019 08:15
-
-
Save DenisVerkhoturov/3119cef0d1a26006923503c1daf79957 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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