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

Errors when modifying files from a plugin. Go1.8 #18713

Closed
adanto opened this issue Jan 19, 2017 · 2 comments
Closed

Errors when modifying files from a plugin. Go1.8 #18713

adanto opened this issue Jan 19, 2017 · 2 comments

Comments

@adanto
Copy link

adanto commented Jan 19, 2017

When executing a plugin importing the "C" library, I'm finding some bugs that doesn't let me read and write files with some libraries.

I'm running the application on a Debian amd64 and the go version is go1.8rc1. I've prepared an example that shows how the error appears:

This is the main file that loads the plugin and executes the F function:

package main

import (
	"plugin"
)

func main() {

	p, err := plugin.Open("pluginReader.so")
	if err != nil {
		panic(err)
	}

	f, err := p.Lookup("F")
	if err != nil {
		panic(err)
	}
	
	f.(func())()
}

And this is the plugin, that can be compiled using
go build -buildmode=plugin pluginReader.go

package main

import (
	"C"
	"fmt"
	"bufio"
	"os"
	"io"

)

func F() {

	fmt.Println("Openning file")
	f, err := os.Open("testFile")

	if err != nil {
		fmt.Print(err)
		return
	}

	fmt.Println("Creaating reader with bufio")
	r4 := bufio.NewReader(f)
	fmt.Printf("Created! Lets copy... %d bytes\n", r4.Buffered())


	// This works!!!
	f2, _ := os.Create("copyOfTestFileA")
	_, err = io.Copy(f2, r4)
	if err != nil {
		fmt.Println(err)
	}


	// This does not work...
	f2, _ = os.Create("copyOfTestFileB")
	w := bufio.NewWriter(f2)
	_, err = io.Copy(w, r4)
	if err != nil {
		fmt.Println(err)
	}

}

When the execution finished, you will see it creates a copy of the "testFile" (named "copyOfTestFileA"), that has the same content as the first one, and another ("copyOfTestFileB") that is empty. This is the problem I'm getting. What causes that second file to be empty?

@tzneal
Copy link
Member

tzneal commented Jan 19, 2017

This is not a bug in Go, there are errors in your code:

  • The file position is at the end of the file before your second copy so there is nothing to read, use f.Seek to seek back to the beginning
  • In the general case, the buffer may have some data buffered so call Reset after seeking the underlying reader.
  • You're not flushing the writer (see bufio.Writer example)

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

@olekukonko
Copy link

I think you sould also consider io.MultiWriter

@golang golang locked and limited conversation to collaborators Jan 19, 2018
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

5 participants