Source file src/cmd/go/internal/work/shell_windows.go

     1  // Copyright 2025 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package work
     6  
     7  import (
     8  	"internal/syscall/windows"
     9  	"io/fs"
    10  	"os"
    11  	"unsafe"
    12  )
    13  
    14  // move moves a file from src to dst, setting the security information
    15  // on the destination file to inherit the permissions from the
    16  // destination parent directory.
    17  func (sh *Shell) move(src, dst string, perm fs.FileMode) (err error) {
    18  	if err := os.Rename(src, dst); err != nil {
    19  		return err
    20  	}
    21  	defer func() {
    22  		if err != nil {
    23  			os.Remove(dst) // clean up if we failed to set the mode or security info
    24  		}
    25  	}()
    26  	if err := os.Chmod(dst, perm); err != nil {
    27  		return err
    28  	}
    29  	// We need to respect the ACL permissions of the destination parent folder.
    30  	// https://go.dev/issue/22343.
    31  	var acl windows.ACL
    32  	if err := windows.InitializeAcl(&acl, uint32(unsafe.Sizeof(acl)), windows.ACL_REVISION); err != nil {
    33  		return err
    34  	}
    35  	secInfo := windows.DACL_SECURITY_INFORMATION | windows.UNPROTECTED_DACL_SECURITY_INFORMATION
    36  	return windows.SetNamedSecurityInfo(dst, windows.SE_FILE_OBJECT, secInfo, nil, nil, &acl, nil)
    37  }
    38  

View as plain text