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

archive/tar: when uncompress the tar file, found io.Reader/io.Writer some problem but don't know reason #36176

Closed
conero opened this issue Dec 17, 2019 · 5 comments
Labels
FrozenDueToAge WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.

Comments

@conero
Copy link

conero commented Dec 17, 2019

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

$ go version
go version go1.13.3 windows/amd64

Does this issue reproduce with the latest release?

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

go env Output
$ go env
set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\~\go-build
set GOENV=C:\Users\~\env
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GONOPROXY=*.corp.example.com
set GONOSUMDB=*.corp.example.com
set GOOS=windows
set GOPATH=C:\~\go
set GOPRIVATE=*.corp.example.com
set GOPROXY=https://goproxy.io,direct
set GOROOT=c:\go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=c:\go\~\windows_amd64
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=C:\conero\~\go.mod
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=C:\Users\~\go-build654453722=/tmp/go-build -gno-record-gcc-switches

What did you do?

func TestCase(){
	// ……
	//un-compress
	tr := tar.NewReader(fh)
	for {
		hdr, err := tr.Next()
		if err == io.EOF {
			break // End of archive
		}
		if err != nil {
			log.Fatal(err)
		}

		vpath := targetDir + hdr.Name
		vpath = fs.StdPathName(vpath)

		if hdr.Name == jenkins.RunDelLogFilename {
			cttBys, _ := ioutil.ReadAll(tr)
			cttStr := string(cttBys)

			fmt.Println(cttStr)
			//		/packgo-test-x2
			ioutil.WriteFile("./jc-test.log", cttBys, 0777)
			//	./jc-test.log:
			//		/packgo-test-cp
			//		/packgo-test
			//		/packgo-test-x2

			// problem:
			//       `fmt.Println(cttStr)` and `./jc-test.log ` had diffrent content. But Why?
			return
		}
		// ……
	}
	// ……
}

What did you expect to see?

Content of fmt.Println(cttStr) and ./jc-test.log is same.

What did you see instead?

Content of fmt.Println(cttStr) and ./jc-test.log is deffrent. So I can handler the real content.

@dmitshur
Copy link
Contributor

In your snippet, there is a line that reads all bytes from the tar reader:

cttBys, _ := ioutil.ReadAll(tr)

The error value isn't check. Can you try adding something that checks if the error is non-nil and reports it?

@dmitshur dmitshur added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Dec 18, 2019
@dmitshur dmitshur changed the title When uncompress the tar file, found io.Reader/io.Writer some problem but donot know reason. archive/tar: when uncompress the tar file, found io.Reader/io.Writer some problem but don't know reason Dec 18, 2019
@networkimprov
Copy link

ioutil.WriteFile() also returns an error which should be checked.

@conero
Copy link
Author

conero commented Dec 19, 2019

I tried to add the Error-check, there was not error, and the result did not change.

if hdr.Name == jenkins.RunDelLogFilename {
    cttBys, er := ioutil.ReadAll(tr)
    if er != nil{
        fmt.Printf(" tar content read is fail, Error: %v\r\n", er.Error())
        return
    }
    //~~
    er = ioutil.WriteFile("./jc-test.log", cttBys, 0777)
    if er != nil{
        fmt.Printf(" ./jc-test.log, Error: %v\r\n", er.Error())
        return
    }
}

@dmitshur
Copy link
Contributor

// problem:
//       `fmt.Println(cttStr)` and `./jc-test.log ` had diffrent content. But Why?

What is the difference in the content exactly?

Are you able to make a smaller self-contained sample that we can reproduce this problem with? That will help with understanding whether the problem is a bug in archive/tar or elsewhere.

@agnivade agnivade added WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. and removed WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. labels Dec 19, 2019
@conero
Copy link
Author

conero commented Dec 23, 2019

@dmitshur

What is the difference in the content exactly?

fmt.Println(cttStr): The content is /packgo-test-x2. And ./jc-test.log that is what I want like:

/packgo-test-cp
/packgo-test
/packgo-test-x2

To simplify the diffrent that got the result by ioutil.ReadAll, the same content use fmt.Println and ioutil.WriteFile are different. It's unpredictable and strange.

@conero conero closed this as completed May 18, 2021
@golang golang locked and limited conversation to collaborators May 18, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Projects
None yet
Development

No branches or pull requests

5 participants