Java Path File Name nio addRestorePermissions(String username, Path file)

Here you can find the source of addRestorePermissions(String username, Path file)

Description

Add the proper File-System permissions to a file so that SQL Server can run a RESTORE query.

License

Apache License

Parameter

Parameter Description
username The username that SQL Server runs as, e.g. "NETWORK SERVICE"
file The file whose permissions will be modified.

Exception

Parameter Description
IOException an exception

Declaration

public static void addRestorePermissions(String username, Path file)
        throws IOException 

Method Source Code

//package com.java2s;
/**/*ww w  .j a va2s  . c  o  m*/
 * 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.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.AclEntry;
import java.nio.file.attribute.AclEntryPermission;
import java.nio.file.attribute.AclEntryType;
import java.nio.file.attribute.AclFileAttributeView;
import java.nio.file.attribute.UserPrincipal;
import java.nio.file.attribute.UserPrincipalLookupService;

import java.util.Collections;
import java.util.EnumSet;

public class Main {
    /**
     * Add the proper File-System permissions to a file so that SQL Server can run a RESTORE query.
     * 
     * @param username The username that SQL Server runs as, e.g. "NETWORK SERVICE"
     * @param file The file whose permissions will be modified.
     * @throws IOException 
     */
    public static void addRestorePermissions(String username, Path file)
            throws IOException {
        AclFileAttributeView aclAttr = Files.getFileAttributeView(file,
                AclFileAttributeView.class);

        UserPrincipalLookupService currULS = file.getFileSystem()
                .getUserPrincipalLookupService();
        UserPrincipal principal = currULS.lookupPrincipalByName(username);

        AclEntry.Builder builder = AclEntry.newBuilder();
        builder.setPermissions(EnumSet.of(AclEntryPermission.READ_DATA,
                AclEntryPermission.READ_ACL,
                AclEntryPermission.READ_ATTRIBUTES,
                AclEntryPermission.READ_NAMED_ATTRS,
                AclEntryPermission.EXECUTE, AclEntryPermission.SYNCHRONIZE));

        builder.setPrincipal(principal);
        builder.setType(AclEntryType.ALLOW);
        aclAttr.setAcl(Collections.singletonList(builder.build()));
    }
}

Related

  1. addPath(JarOutputStream outputStream, Path path, String entryName)
  2. appendTargetToBuildFile(String targetName, Path dir)
  3. checkFile(final String friendlyname, final Path file, final boolean isdirectory, final boolean canwrite)
  4. compileAndLoad(Path basePath, String className)
  5. concatFileName(String fileName, Path pathLocation)