Source file src/cmd/go/internal/modload/stat_unix.go

     1  // Copyright 2019 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  //go:build unix
     6  
     7  package modload
     8  
     9  import (
    10  	"io/fs"
    11  	"os"
    12  	"syscall"
    13  )
    14  
    15  // hasWritePerm reports whether the current user has permission to write to the
    16  // file with the given info.
    17  //
    18  // Although the root user on most Unix systems can write to files even without
    19  // permission, hasWritePerm reports false if no appropriate permission bit is
    20  // set even if the current user is root.
    21  func hasWritePerm(path string, fi fs.FileInfo) bool {
    22  	if os.Getuid() == 0 {
    23  		// The root user can access any file, but we still want to default to
    24  		// read-only mode if the go.mod file is marked as globally non-writable.
    25  		// (If the user really intends not to be in readonly mode, they can
    26  		// pass -mod=mod explicitly.)
    27  		return fi.Mode()&0222 != 0
    28  	}
    29  
    30  	const W_OK = 0x2
    31  	return syscall.Access(path, W_OK) == nil
    32  }
    33  

View as plain text