File.Write() creates empty files
trixden opened this issue · comments
Code:
var Location vfs.Location = initLocation()
func initLocation() vfs.Location {
if location, err := vfssimple.NewLocation("file:///path/to/public"); err != nil {
log.Fatal(err.Error())
} else if ok, err := location.Exists(); err != nil {
log.Fatal(err.Error())
} else if !ok {
log.Fatal("Unexpected error")
} else {
return location
}
return nil
}
func MakeTestFile() {
f, err := Location.NewFile("test.txt")
if err != nil {
log.Fatal(err.Error())
}
if num, err := f.Write([]byte("This is a test text")); err != nil {
log.Fatal(err.Error())
} else {
log.Println("String was writed in file")
}
f.Close()
}
Problem:
The function MakeTestFile
creates a file without errors, but it is empty.
System information:
- Go version: 1.14.1
- Os/Arch: linux/amd64
- Package version: 1.6.2
But! The temp folder contains a text file named test.txt.1605546281517101373860380273. I can't understand why this is happening.
As I understand, the file should be moved from the temp folder to the expected location with f.Close ()
. But I am getting error:
rename /tmp/test.txt.1605550051063334634276804707 /path/to/public/test.txt: invalid cross-device link
Sorry, i hadn't noticed this. I'll try to take a look in the next couple of days.
It seems the issue is that fact that /tmp and /path/to/public/ are mounted on different filesystems. It makes some since that renaming across filesystem boundaries doesn't work. Renaming (os.Rename()) is less expensive than a copy/delete (move) but copye/delete would solve for this.