Black Lives Matter. Support the Equal Justice Initiative.

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

Documentation: cmd/go/internal/modload

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

View as plain text