Skip to content

Commit

Permalink
all: rely on umask when creating files and directories
Browse files Browse the repository at this point in the history
That is, create directories with permission bits 0o777
and files with permission bits 0o666, and let the user's umask setting
decide whether groups and other users should also have access.

This is pretty common in Go, and works as best as can be expected
on all platforms including Windows.

We fix all code except the copied golangorgx gopls code.

Signed-off-by: Daniel Martí <mvdan@mvdan.cc>
Change-Id: I306613b35caab7d8ac8db2ec05a856802b1eb08b
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1200569
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
Reviewed-by: Matthew Sackman <matthew@cue.works>
  • Loading branch information
mvdan committed Sep 3, 2024
1 parent d20e479 commit 2da59f2
Show file tree
Hide file tree
Showing 17 changed files with 24 additions and 24 deletions.
4 changes: 2 additions & 2 deletions cmd/cue/cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ func (fo *originalFile) restore() error {
if len(fo.contents) == 0 {
return os.Remove(fo.filename)
}
return os.WriteFile(fo.filename, fo.contents, 0644)
return os.WriteFile(fo.filename, fo.contents, 0666)
}

type fileInfo struct {
Expand Down Expand Up @@ -287,7 +287,7 @@ func (fi *fileInfo) appendAndCheck() (fo originalFile, err error) {
return originalFile{}, err
}

if err = os.WriteFile(fi.filename, b, 0644); err != nil {
if err = os.WriteFile(fi.filename, b, 0666); err != nil {
// Just in case, attempt to restore original file.
_ = fo.restore()
return originalFile{}, err
Expand Down
2 changes: 1 addition & 1 deletion cmd/cue/cmd/fix.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func runFixAll(cmd *Command, args []string) error {
return err
}
} else {
if err := os.WriteFile(f.Filename, b, 0644); err != nil {
if err := os.WriteFile(f.Filename, b, 0666); err != nil {
errs = errors.Append(errs, errors.Promote(err, "write"))
}
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/cue/cmd/fmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ func formatFile(file *build.File, opts []format.Option, doDiff, check bool, cmd
case file.Filename == "-":
// already wrote the formatted source to stdout above
default:
if err := os.WriteFile(file.Filename, formatted, 0644); err != nil {
if err := os.WriteFile(file.Filename, formatted, 0666); err != nil {
return false, err
}
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/cue/cmd/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -496,8 +496,8 @@ func writeFile(p *buildPlan, f *ast.File, cueFile string) error {
_, err := p.cmd.OutOrStdout().Write(b)
return err
}
_ = os.MkdirAll(filepath.Dir(cueFile), 0755)
return os.WriteFile(cueFile, b, 0644)
_ = os.MkdirAll(filepath.Dir(cueFile), 0777)
return os.WriteFile(cueFile, b, 0666)
}

type hoister struct {
Expand Down
6 changes: 3 additions & 3 deletions cmd/cue/cmd/modinit.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func runModInit(cmd *Command, args []string) (err error) {
return err
}

err = os.Mkdir(mod, 0755)
err = os.Mkdir(mod, 0777)
if err != nil && !os.IsExist(err) {
return err
}
Expand All @@ -97,10 +97,10 @@ func runModInit(cmd *Command, args []string) (err error) {
return err
}

if err = os.Mkdir(filepath.Join(mod, "usr"), 0755); err != nil {
if err = os.Mkdir(filepath.Join(mod, "usr"), 0777); err != nil {
return err
}
if err = os.Mkdir(filepath.Join(mod, "pkg"), 0755); err != nil {
if err = os.Mkdir(filepath.Join(mod, "pkg"), 0777); err != nil {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/cue/cmd/trim.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func runTrim(cmd *Command, args []string) error {
filename = dst
}

err = os.WriteFile(filename, b, 0644)
err = os.WriteFile(filename, b, 0666)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cue/interpreter/wasm/wasm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func check(t *testing.T, dir string, got string) {
golden := filepath.Join("testdata", dir) + ".golden"

if cuetest.UpdateGoldenFiles {
os.WriteFile(golden, []byte(got), 0644)
os.WriteFile(golden, []byte(got), 0666)
}

want := string(must(os.ReadFile(golden))(t))
Expand Down
2 changes: 1 addition & 1 deletion cue/load/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -731,7 +731,7 @@ package %s
x: 1
`, string(c))
err := os.WriteFile(filepath.Join(testDir, string(c)+".cue"), []byte(contents), 0o644)
err := os.WriteFile(filepath.Join(testDir, string(c)+".cue"), []byte(contents), 0o666)
qt.Assert(t, qt.IsNil(err))
}

Expand Down
4 changes: 2 additions & 2 deletions cue/testdata/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ func (e *extractor) extractTest(x *ast.CompositeLit) {

e.a.Comment = e.header.Bytes()

_ = os.Mkdir(e.dir, 0755)
_ = os.Mkdir(e.dir, 0777)

name := fmt.Sprintf("%03d", e.count)
if e.name != "" {
Expand All @@ -268,7 +268,7 @@ func (e *extractor) extractTest(x *ast.CompositeLit) {
name = strings.ReplaceAll(name, " ", "_")
name = strings.ReplaceAll(name, ":", "_")
filename := filepath.Join(e.dir, name+".txtar")
err := os.WriteFile(filename, txtar.Format(e.a), 0644)
err := os.WriteFile(filename, txtar.Format(e.a), 0666)
if err != nil {
e.fatalf("Could not write file: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion encoding/gocode/testdata/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func main() {
}

goFile := filepath.Join("testdata", d.Name(), "cue_gen.go")
if err := os.WriteFile(goFile, b, 0644); err != nil {
if err := os.WriteFile(goFile, b, 0666); err != nil {
log.Fatal(err)
}
}
Expand Down
2 changes: 1 addition & 1 deletion encoding/openapi/decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func TestDecode(t *testing.T) {
if cuetest.UpdateGoldenFiles {
a.Files[outIndex].Data = b
b = txtar.Format(a)
err = os.WriteFile(fullpath, b, 0644)
err = os.WriteFile(fullpath, b, 0666)
if err != nil {
t.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion encoding/openapi/openapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ func TestParseDefinitions(t *testing.T) {

wantFile := filepath.Join("testdata", tc.out)
if cuetest.UpdateGoldenFiles {
_ = os.WriteFile(wantFile, out.Bytes(), 0644)
_ = os.WriteFile(wantFile, out.Bytes(), 0666)
return
}

Expand Down
6 changes: 3 additions & 3 deletions encoding/protobuf/protobuf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func TestExtractDefinitions(t *testing.T) {

wantFile := filepath.Join("testdata", filepath.Base(file)+".out.cue")
if cuetest.UpdateGoldenFiles {
_ = os.WriteFile(wantFile, out.Bytes(), 0644)
_ = os.WriteFile(wantFile, out.Bytes(), 0666)
return
}

Expand Down Expand Up @@ -102,8 +102,8 @@ func TestBuild(t *testing.T) {
if err != nil {
t.Fatal(err)
}
_ = os.MkdirAll(filepath.Dir(f.Filename), 0755)
err = os.WriteFile(f.Filename, b, 0644)
_ = os.MkdirAll(filepath.Dir(f.Filename), 0777)
err = os.WriteFile(f.Filename, b, 0666)
if err != nil {
t.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/core/adt/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func OpenNodeGraph(title, path, code, out, graph string) {
}
numberOpened++

err := os.MkdirAll(path, 0755)
err := os.MkdirAll(path, 0777)
if err != nil {
log.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/encoding/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ func writer(f *build.File, cfg *Config) (_ io.Writer, close func() error, err er
// Swap O_EXCL for O_TRUNC to allow replacing an entire existing file.
mode = os.O_WRONLY | os.O_CREATE | os.O_TRUNC
}
f, err := os.OpenFile(path, mode, 0o644)
f, err := os.OpenFile(path, mode, 0o666)
if err != nil {
if errors.Is(err, fs.ErrExist) {
return errors.Wrapf(fs.ErrExist, token.NoPos, "error writing %q", path)
Expand Down
2 changes: 1 addition & 1 deletion internal/encoding/yaml/decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -971,7 +971,7 @@ func TestFiles(t *testing.T) {
}
got := cueStr(expr)
if cuetest.UpdateGoldenFiles {
os.WriteFile(filename, []byte(got), 0644)
os.WriteFile(filename, []byte(got), 0666)
return
}
b, err := os.ReadFile(filename)
Expand Down
2 changes: 1 addition & 1 deletion pkg/qgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func extract(args []string) {
if err != nil {
log.Fatal(err)
}
err = os.WriteFile(lastPkg+".go", b, 0644)
err = os.WriteFile(lastPkg+".go", b, 0666)
if err != nil {
log.Fatal(err)
}
Expand Down

0 comments on commit 2da59f2

Please sign in to comment.