true
if this file permission includes the
* owner read permission, or false
if not.
*/
public boolean isOwnerReadable()
{
return is(encodedPermission, OWNER_READABLE);
}
/**
* Indicates whether this file permission includes the owner write
* permission.
*
* @return true
if this file permission includes the
* owner write permission, or false
if not.
*/
public boolean isOwnerWritable()
{
return is(encodedPermission, OWNER_WRITABLE);
}
/**
* Indicates whether this file permission includes the owner execute
* permission.
*
* @return true
if this file permission includes the
* owner execute permission, or false
if not.
*/
public boolean isOwnerExecutable()
{
return is(encodedPermission, OWNER_EXECUTABLE);
}
/**
* Indicates whether this file permission includes the group read
* permission.
*
* @return true
if this file permission includes the
* group read permission, or false
if not.
*/
public boolean isGroupReadable()
{
return is(encodedPermission, GROUP_READABLE);
}
/**
* Indicates whether this file permission includes the group write
* permission.
*
* @return true
if this file permission includes the
* group write permission, or false
if not.
*/
public boolean isGroupWritable()
{
return is(encodedPermission, GROUP_WRITABLE);
}
/**
* Indicates whether this file permission includes the group execute
* permission.
*
* @return true
if this file permission includes the
* group execute permission, or false
if not.
*/
public boolean isGroupExecutable()
{
return is(encodedPermission, GROUP_EXECUTABLE);
}
/**
* Indicates whether this file permission includes the other read
* permission.
*
* @return true
if this file permission includes the
* other read permission, or false
if not.
*/
public boolean isOtherReadable()
{
return is(encodedPermission, OTHER_READABLE);
}
/**
* Indicates whether this file permission includes the other write
* permission.
*
* @return true
if this file permission includes the
* other write permission, or false
if not.
*/
public boolean isOtherWritable()
{
return is(encodedPermission, OTHER_WRITABLE);
}
/**
* Indicates whether this file permission includes the other execute
* permission.
*
* @return true
if this file permission includes the
* other execute permission, or false
if not.
*/
public boolean isOtherExecutable()
{
return is(encodedPermission, OTHER_EXECUTABLE);
}
private boolean is(int encodedPermissions, int permission)
{
return (encodedPermissions & permission) == permission;
}
/**
* Attempts to set the given permissions on the specified file. If
* the underlying platform does not allow the full level of
* granularity specified in the permissions, then an attempt will be
* made to set them as closely as possible to the provided
* permissions, erring on the side of security.
*
* @param f The file to which the permissions should be applied.
* @param p The permissions to apply to the file.
*
* @return true
if the permissions (or the nearest
* equivalent) were successfully applied to the specified
* file, or false
if was not possible to set
* the permissions on the current platform.
*
* @throws FileNotFoundException If the specified file does not
* exist.
*
* @throws DirectoryException If a problem occurs while trying to
* set the file permissions.
*/
public static boolean setPermissions(File f, FilePermission p)
throws FileNotFoundException, DirectoryException
{
if (!f.exists())
{
throw new FileNotFoundException(ERR_FILEPERM_SET_NO_SUCH_FILE.get(f.getAbsolutePath()).toString());
}
Path filePath = f.toPath();
PosixFileAttributeView posix = Files.getFileAttributeView(filePath, PosixFileAttributeView.class);
if (posix != null)
{
StringBuilder posixMode = new StringBuilder();
toPOSIXString(p, posixMode, "", "", "");
Settrue
if the permissions (or the nearest
* equivalent) were successfully applied to the specified
* file, or false
if was not possible to set
* the permissions on the current platform.
*
* The file is known to exist therefore there is no need for
* exists() checks.
*/
public static boolean setSafePermissions(File f, Integer p)
{
Path filePath = f.toPath();
PosixFileAttributeView posix = Files.getFileAttributeView(filePath, PosixFileAttributeView.class);
if (posix != null)
{
StringBuilder posixMode = new StringBuilder();
toPOSIXString(new FilePermission(p), posixMode, "", "", "");
Set* For example: "rwxr-xrw-" and "Owner=rwx, Group=r-x", Other=rw-". * * @param p The file permissions to use. * @param buffer The buffer being appended to. * @param owner The owner prefix, must not be null. * @param group The group prefix, must not be null. * @param other The other prefix, must not be null. */ private static void toPOSIXString(FilePermission p, StringBuilder buffer, String owner, String group, String other) { buffer.append(owner); buffer.append(p.isOwnerReadable() ? "r" : "-"); buffer.append(p.isOwnerWritable() ? "w" : "-"); buffer.append(p.isOwnerExecutable() ? "x" : "-"); buffer.append(group); buffer.append(p.isGroupReadable() ? "r" : "-"); buffer.append(p.isGroupWritable() ? "w" : "-"); buffer.append(p.isGroupExecutable() ? "x" : "-"); buffer.append(other); buffer.append(p.isOtherReadable() ? "r" : "-"); buffer.append(p.isOtherWritable() ? "w" : "-"); buffer.append(p.isOtherExecutable() ? "x" : "-"); } /** * Retrieves a string representation of this file permission. * * @return A string representation of this file permission. */ @Override public String toString() { StringBuilder buffer = new StringBuilder(); toString(buffer); return buffer.toString(); } /** * Appends a string representation of this file permission to the * given buffer. * * @param buffer The buffer to which the data should be appended. */ public void toString(StringBuilder buffer) { toPOSIXString(this, buffer, "Owner=", ", Group=", ", Other="); } }