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