Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

os: OpenFile always truncates (ignores flags) #53591

Closed
aytekinar opened this issue Jun 28, 2022 · 2 comments
Closed

os: OpenFile always truncates (ignores flags) #53591

aytekinar opened this issue Jun 28, 2022 · 2 comments

Comments

@aytekinar
Copy link

What version of Go are you using (go version)?

$ go version
go version go1.18.3 linux/amd64

Does this issue reproduce with the latest release?

Yes.

What operating system and processor architecture are you using (go env)?

My host OS is Windows 11 Enterprise.

I am using Go inside a (Docker-based) development container (based on the image golang:1.18-bullseye) under Visual Studio Code.

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOENV="/root/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.18.3"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/workspace/go.mod"
GOWORK=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build2210480654=/tmp/go-build -gno-record-gcc-switches"

What did you do?

I have used the following Go code:

// main.go
package main

import (
	"log"
	"os"
)

func overwriteFileWithGarbage(garbage string) (n int, err error) {
	file, err := os.OpenFile("/tmp/some-file.txt", os.O_CREATE|os.O_WRONLY, 0666)
	if err != nil {
		return
	}
	defer func() {
		if fileErr := file.Close(); err == nil {
			err = fileErr
		}
	}()
	n, err = file.WriteString(garbage)
	return
}

func main() {
	if n, err := overwriteFileWithGarbage("garbage"); err != nil {
		log.Fatalf("error occurred: %v\n", err)
	} else {
		log.Printf("%d bytes written\n", n)
	}
}

and run the following to reproduce the issue:

$ echo "line-1" >> /tmp/some-file.txt
$ echo "line-2" >> /tmp/some-file.txt
$ echo "line-3" >> /tmp/some-file.txt
$ cat /tmp/some-file.txt
line-1
line-2
line-3
$ go run main.go
2022/06/28 21:56:02 7 bytes written

What did you expect to see?

$ cat /tmp/some-file.txt 
garbage

What did you see instead?

$ cat /tmp/some-file.txt 
garbageline-2
line-3
@seankhliao
Copy link
Member

looks normal, did your want O_TRUNC?

Unlike many projects, the Go project does not use GitHub Issues for general discussion or asking questions. GitHub Issues are used for tracking bugs and proposals only.

For questions please refer to https://github.com/golang/go/wiki/Questions

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Jun 28, 2022
@aytekinar
Copy link
Author

Apologies for the noise! I think I should take a break, now.

Somehow, I have been reading/inferring O_TRUNC as being O_APPEND. Hence, leaving O_TRUNC outside, I was thinking/hoping to get the file created from scratch every time 🤦🏻 Totally my bad!

@golang golang locked and limited conversation to collaborators Jun 28, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants