Java FileStore canExecuteExecutable(File file)

Here you can find the source of canExecuteExecutable(File file)

Description

can Execute Executable

License

Apache License

Declaration

static boolean canExecuteExecutable(File file) throws IOException 

Method Source Code


//package com.java2s;
/*/*from w  ww. jav a2 s.c  o m*/
 * Copyright 2017 The Android Open Source Project
 *
 * 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 static java.nio.file.attribute.PosixFilePermission.GROUP_EXECUTE;
import static java.nio.file.attribute.PosixFilePermission.OTHERS_EXECUTE;
import static java.nio.file.attribute.PosixFilePermission.OWNER_EXECUTE;
import java.io.File;
import java.io.IOException;

import java.nio.file.attribute.PosixFilePermission;

import java.util.EnumSet;
import java.util.Set;

public class Main {
    static boolean canExecuteExecutable(File file) throws IOException {
        // If we can already execute, there is nothing to do.
        if (file.canExecute()) {
            return true;
        }

        // On volumes, with noexec set, even files with the executable POSIX permissions will
        // fail to execute. The File#canExecute() method honors this behavior, probaby via
        // parsing the noexec flag when initializing the UnixFileStore, though the flag is not
        // exposed via a public API.  To find out if library is being loaded off a volume with
        // noexec, confirm or add executalbe permissions, then check File#canExecute().

        // Note: We use FQCN to not break when used in java6
        Set<PosixFilePermission> existingFilePermissions = java.nio.file.Files
                .getPosixFilePermissions(file.toPath());
        Set<java.nio.file.attribute.PosixFilePermission> executePermissions = EnumSet.of(OWNER_EXECUTE,
                GROUP_EXECUTE, OTHERS_EXECUTE);
        if (existingFilePermissions.containsAll(executePermissions)) {
            return false;
        }

        Set<java.nio.file.attribute.PosixFilePermission> newPermissions = EnumSet.copyOf(existingFilePermissions);
        newPermissions.addAll(executePermissions);
        java.nio.file.Files.setPosixFilePermissions(file.toPath(), newPermissions);
        return file.canExecute();
    }
}

Related

  1. createTempFileWithAttributes(Path tempFilePath, File fileName)
  2. diskFree()
  3. ensureExists(Path folder)
  4. findTopResourceInVolume(FileStore fstore, Path path)