]> Cypherpunks repositories - gostls13.git/commitdiff
gofix: don't rewrite O_APPEND opens
authorRuss Cox <rsc@golang.org>
Tue, 5 Apr 2011 15:12:02 +0000 (11:12 -0400)
committerRuss Cox <rsc@golang.org>
Tue, 5 Apr 2011 15:12:02 +0000 (11:12 -0400)
R=r, rog
CC=golang-dev
https://golang.org/cl/4364041

misc/dashboard/builder/exec.go
src/cmd/gofix/osopen.go
src/cmd/gofix/osopen_test.go
src/cmd/goinstall/main.go
src/pkg/crypto/tls/generate_cert.go

index c122d4a07cc4bed9f2666c19963bae332a860028..3c6fbdced47f718300256e4daa80c324ce73caf9 100644 (file)
@@ -49,7 +49,7 @@ func runLog(envv []string, logfile, dir string, argv ...string) (output string,
        b := new(bytes.Buffer)
        var w io.Writer = b
        if logfile != "" {
-               f, err := os.Create(logfile)
+               f, err := os.OpenFile(logfile, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666)
                if err != nil {
                        return
                }
index 4b9812477e5cf5d1fdf44ccf2e2516920c881ca6..5d7f5cbde54f20e26c4b5333599408d91494536a 100644 (file)
@@ -70,6 +70,7 @@ func osopen(f *ast.File) bool {
 func isCreateFlag(flag ast.Expr) bool {
        foundCreate := false
        foundTrunc := false
+       foundAppend := false
        // OR'ing of flags: is O_CREATE on?  + or | would be fine; we just look for os.O_CREATE
        // and don't worry about the actual opeator.
        p := flag.Pos()
@@ -85,12 +86,21 @@ func isCreateFlag(flag ast.Expr) bool {
                if isPkgDot(lhs, "os", "O_TRUNC") {
                        foundTrunc = true
                }
+               if isPkgDot(lhs, "os", "O_APPEND") {
+                       foundAppend = true
+               }
                if !isBinary {
                        break
                }
                flag = expr.X
        }
-       if foundCreate && !foundTrunc {
+       if !foundCreate {
+               return false
+       }
+       if foundAppend {
+               return false
+       }
+       if !foundTrunc {
                warn(p, "rewrote os.Open with O_CREATE but not O_TRUNC to os.Create")
        }
        return foundCreate
index b662b62db50042e5e99f05736083d7a8debd23cf..b0a4f63c6939f584decbede81418eeaac06898ce 100644 (file)
@@ -27,6 +27,7 @@ func f() {
        os.Open(a, os.O_CREATE, 0666)
        os.Open(a, os.O_CREATE|os.O_TRUNC, 0664)
        os.Open(a, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
+       os.Open(a, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666)
        _ = os.O_CREAT
 }
 `,
@@ -46,6 +47,7 @@ func f() {
        os.Create(a)
        os.Create(a)
        os.Create(a)
+       os.OpenFile(a, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666)
        _ = os.O_CREATE
 }
 `,
index 6d5d34884ce730d02f71b44be9c3df1ceb419a57..8fec8e312a9770bf2cf3a0b9bd3b55a787c6dff4 100644 (file)
@@ -120,7 +120,7 @@ func logPackage(pkg string) {
        if installedPkgs[pkg] {
                return
        }
-       fout, err := os.Create(logfile)
+       fout, err := os.OpenFile(logfile, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666)
        if err != nil {
                fmt.Fprintf(os.Stderr, "%s: %s\n", argv0, err)
                return
index 5c783fd296991f5d64758fca3e5c687562d17614..5b8c700e5f909bf949280d1f2f2187ede8ae1dbb 100644 (file)
@@ -59,7 +59,7 @@ func main() {
        certOut.Close()
        log.Print("written cert.pem\n")
 
-       keyOut, err := os.OpenFile("key.pem", os.O_WRONLY|os.O_CREAT, 0600)
+       keyOut, err := os.OpenFile("key.pem", os.O_WRONLY|os.O_CREAT|os.O_TRUNC, 0600)
        if err != nil {
                log.Print("failed to open key.pem for writing:", err)
                return