Java Posix setPermissionsToFile(File f, Collection perms)

Here you can find the source of setPermissionsToFile(File f, Collection perms)

Description

set Permissions To File

License

Apache License

Parameter

Parameter Description
f The File
perms A Collection of PosixFilePermission s to set on it. <B>Note:</B> the file is set to readable/writable/executable not only by the owner if <U>any</U> of relevant the owner/group/others permission is set

Declaration

public static void setPermissionsToFile(File f, Collection<PosixFilePermission> perms) 

Method Source Code

//package com.java2s;
/*/*from  www  .j a  va2  s .  com*/
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you 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.io.File;

import java.nio.file.attribute.PosixFilePermission;

import java.util.Collection;

public class Main {
    /**
     * @param f     The {@link File}
     * @param perms A {@link Collection} of {@link PosixFilePermission}s to set on it.
     *              <B>Note:</B> the file is set to readable/writable/executable not only by the
     *              owner if <U>any</U> of relevant the owner/group/others permission is set
     */
    public static void setPermissionsToFile(File f, Collection<PosixFilePermission> perms) {
        boolean readable = perms != null
                && (perms.contains(PosixFilePermission.OWNER_READ) || perms.contains(PosixFilePermission.GROUP_READ)
                        || perms.contains(PosixFilePermission.OTHERS_READ));
        f.setReadable(readable, false);

        boolean writable = perms != null && (perms.contains(PosixFilePermission.OWNER_WRITE)
                || perms.contains(PosixFilePermission.GROUP_WRITE)
                || perms.contains(PosixFilePermission.OTHERS_WRITE));
        f.setWritable(writable, false);

        boolean executable = perms != null && (perms.contains(PosixFilePermission.OWNER_EXECUTE)
                || perms.contains(PosixFilePermission.GROUP_EXECUTE)
                || perms.contains(PosixFilePermission.OTHERS_EXECUTE));
        f.setExecutable(executable, false);
    }
}

Related

  1. givePerm(File f, Set perms)
  2. givePerms(File f, Set perms, boolean recursive)
  3. intFilePermissions(Set permissions)
  4. mapValue(PosixFilePermission p)
  5. posixFilePermissions(int mode)
  6. toOctalFileMode(Set permissions)
  7. toUnixFileMode(Set perms)
  8. unixModeToPosixSet(int unixMode)