Java Posix getPosixPermissions(int fileMode)

Here you can find the source of getPosixPermissions(int fileMode)

Description

Given an integer representing file permissions, returns a set of PosixFilePermissions representing the corresponding permissions.

License

Apache License

Parameter

Parameter Description
fileMode Integer representing a file mode

Return

Set of permissions in the input mode

Declaration

public static Set<PosixFilePermission> getPosixPermissions(int fileMode) 

Method Source Code


//package com.java2s;
/*/*w  ww  . j a  v a 2  s  . c  om*/
 * Copyright 2018 Google, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License")
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.nio.file.attribute.PosixFilePermission;
import java.util.HashSet;
import java.util.Set;

public class Main {
    private static PosixFilePermission[] permissionBits = new PosixFilePermission[] {
            PosixFilePermission.OTHERS_EXECUTE, PosixFilePermission.OTHERS_WRITE, PosixFilePermission.OTHERS_READ,
            PosixFilePermission.GROUP_EXECUTE, PosixFilePermission.GROUP_WRITE, PosixFilePermission.GROUP_READ,
            PosixFilePermission.OWNER_EXECUTE, PosixFilePermission.OWNER_WRITE, PosixFilePermission.OWNER_READ };

    /**
     * Given an integer representing file permissions, returns a set of PosixFilePermissions
     * representing the corresponding permissions.
     * @param fileMode Integer representing a file mode
     * @return Set of permissions in the input mode
     */
    public static Set<PosixFilePermission> getPosixPermissions(int fileMode) {
        Set<PosixFilePermission> posixPermissions = new HashSet<>();

        for (int i = 0; i < permissionBits.length; i++) {
            int bit = fileMode & (1 << i);
            if (bit != 0) {
                posixPermissions.add(permissionBits[i]);
            }
        }

        return posixPermissions;
    }
}

Related

  1. fromPosixPermission(PosixFilePermission permission)
  2. getFileMode(Set posixPermissions)
  3. getPosixFileAttributes(@Nonnull File file)
  4. getPosixFilePermissions(int mode)
  5. getPosixPermissions(File forFile)
  6. getPosixPermissionsAsInt( Set permissionSet)
  7. givePerm(File f, Set perms)
  8. givePerms(File f, Set perms, boolean recursive)
  9. intFilePermissions(Set permissions)