...

Source file src/github.com/containerd/continuity/fs/copy_windows.go

Documentation: github.com/containerd/continuity/fs

     1  /*
     2     Copyright The containerd Authors.
     3  
     4     Licensed under the Apache License, Version 2.0 (the "License");
     5     you may not use this file except in compliance with the License.
     6     You may obtain a copy of the License at
     7  
     8         http://www.apache.org/licenses/LICENSE-2.0
     9  
    10     Unless required by applicable law or agreed to in writing, software
    11     distributed under the License is distributed on an "AS IS" BASIS,
    12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13     See the License for the specific language governing permissions and
    14     limitations under the License.
    15  */
    16  
    17  package fs
    18  
    19  import (
    20  	"errors"
    21  	"fmt"
    22  	"os"
    23  
    24  	winio "github.com/Microsoft/go-winio"
    25  	"golang.org/x/sys/windows"
    26  )
    27  
    28  const (
    29  	seTakeOwnershipPrivilege = "SeTakeOwnershipPrivilege"
    30  )
    31  
    32  func copyFileInfo(fi os.FileInfo, src, name string) error {
    33  	if err := os.Chmod(name, fi.Mode()); err != nil {
    34  		return fmt.Errorf("failed to chmod %s: %w", name, err)
    35  	}
    36  
    37  	// Copy file ownership and ACL
    38  	// We need SeRestorePrivilege and SeTakeOwnershipPrivilege in order
    39  	// to restore security info on a file, especially if we're trying to
    40  	// apply security info which includes SIDs not necessarily present on
    41  	// the host.
    42  	privileges := []string{winio.SeRestorePrivilege, seTakeOwnershipPrivilege}
    43  	if err := winio.EnableProcessPrivileges(privileges); err != nil {
    44  		return err
    45  	}
    46  	defer winio.DisableProcessPrivileges(privileges)
    47  
    48  	secInfo, err := windows.GetNamedSecurityInfo(
    49  		src, windows.SE_FILE_OBJECT,
    50  		windows.OWNER_SECURITY_INFORMATION|windows.DACL_SECURITY_INFORMATION)
    51  	if err != nil {
    52  		return err
    53  	}
    54  
    55  	dacl, _, err := secInfo.DACL()
    56  	if err != nil {
    57  		return err
    58  	}
    59  
    60  	sid, _, err := secInfo.Owner()
    61  	if err != nil {
    62  		return err
    63  	}
    64  
    65  	if err := windows.SetNamedSecurityInfo(
    66  		name, windows.SE_FILE_OBJECT,
    67  		windows.OWNER_SECURITY_INFORMATION|windows.DACL_SECURITY_INFORMATION,
    68  		sid, nil, dacl, nil); err != nil {
    69  		return err
    70  	}
    71  	return nil
    72  }
    73  
    74  func copyXAttrs(dst, src string, excludes map[string]struct{}, errorHandler XAttrErrorHandler) error {
    75  	return nil
    76  }
    77  
    78  func copyIrregular(dst string, fi os.FileInfo) error {
    79  	return errors.New("irregular copy not supported")
    80  }
    81  

View as plain text