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: files not correctly closed in MacOS X #20817

Closed
FrankStorbeck opened this issue Jun 27, 2017 · 1 comment
Closed

os: files not correctly closed in MacOS X #20817

FrankStorbeck opened this issue Jun 27, 2017 · 1 comment

Comments

@FrankStorbeck
Copy link

@FrankStorbeck FrankStorbeck commented Jun 27, 2017

used go version 1.8.3
go env shows:
GOARCH="amd64"
GOBIN="/Users/franks/Documents/Development/sandbox/franks/go/bin"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/franks/Documents/Development/sandbox/franks/go"
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/y1/6h37hs7h8xj9vn001s7c4_h00000gn/T/go-build852796593=/tmp/go-build -gno-record-gcc-switches -fno-common"
CXX="clang++"
CGO_ENABLED="1"
PKG_CONFIG="pkg-config"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"

the following code shows that after closing a file and then rewriting to that file results in a file still
holding the tail of the first write.

Code:

package main

import (
	"bufio"
	"io"
	"log"
	"os"
	"os/exec"
)

func doWrite(aContent string) error {
	f, err := os.OpenFile("content.txt", os.O_WRONLY|os.O_CREATE, 0600)
	if err != nil {
		return err
	}
	defer f.Close()

	_, err = f.WriteString(aContent + "\n")
	return err
}

func doRead() (string, error) {
	f, err := os.Open("content.txt")
	if err != nil {
		return "", err
	}
	defer f.Close()

	buf := bufio.NewReader(f)
	content := ""
	doRead := true
	for doRead {
		line, err := buf.ReadString('\n')
		if err != nil {
			if err != io.EOF {
				return "", err
			}
			doRead = false
		}
		content = content + line
	}

	return content, nil
}

func main() {
	// show go version first:
	cmd := exec.Command("go", "version")
	version, err := cmd.CombinedOutput()
	if err != nil {
		log.Fatalf("%s", err)
	}
	log.Printf("%s", string(version))

	// first write a long string to "content.txt"
	if err = doWrite("abcdefghijklmnopqrstuvwxyz"); err != nil {
		log.Fatalf("doWrite error (1): %s", err)
	}

	// read and show the contents:
	content, err := doRead()
	if err != nil {
		log.Fatalf("doRead error (1): %s", err)
	}
	log.Printf("doRead content (1): %q", content)

	// now write a short string again to "content.txt"
	if err = doWrite("abc"); err != nil {
		log.Fatalf("doWrite error (2): %s", err)
	}

	// read and show the contents:
	content, err = doRead()
	if err != nil {
		log.Fatalf("doRead error (2): %s", err)
	}
	log.Printf("doRead content (2): %q", content)
}

the program generates:

2017/06/27 17:50:26 go version go1.8.3 darwin/amd64
2017/06/27 17:50:26 doRead content (1): "abcdefghijklmnopqrstuvwxyz\n"
2017/06/27 17:50:26 doRead content (2): "abc\nefghijklmnopqrstuvwxyz\n"

I Expected:

2017/06/27 17:50:26 go version go1.8.3 darwin/amd64
2017/06/27 17:50:26 doRead content (1): "abcdefghijklmnopqrstuvwxyz\n"
2017/06/27 17:50:26 doRead content (2): "abc\n"
"abc\n"

but the program shows

"abc\nefghijklmnopqrstuvwxyz\n"
@bradfitz
Copy link
Contributor

@bradfitz bradfitz commented Jun 27, 2017

Closing a file descriptor doesn't delete the file.

And os.O_WRONLY|os.O_CREATE doesn't mean to replace the file. See https://golang.org/pkg/os/#pkg-constants or http://pubs.opengroup.org/onlinepubs/009695399/functions/open.html for more details. (Go's os.OpenFile is a thin wrapper around the operating system's functionality)

To delete the file, use os.Remove.

For questions about Go, see https://golang.org/wiki/Questions.

@bradfitz bradfitz closed this Jun 27, 2017
@mikioh mikioh changed the title files not correctly closed in MacOS X os: files not correctly closed in MacOS X Aug 2, 2017
@golang golang locked and limited conversation to collaborators Aug 3, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.