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

MemMapFS: uses shared file position #54

Closed
vetinari opened this issue Jan 3, 2016 · 0 comments
Closed

MemMapFS: uses shared file position #54

vetinari opened this issue Jan 3, 2016 · 0 comments
Assignees

Comments

@vetinari
Copy link
Collaborator

vetinari commented Jan 3, 2016

EDITed: fixed typo
The following code demonstrates this behavior, it should print

    OS=<datadata>
    FS=<datadata>

but the FS line reads FS=<testdatadata>

package main

import (
    "os"
    "fmt"
    "io/ioutil"
    "github.com/spf13/afero"
)

func main() {
    fileName := "./afero-demo2.txt"
    fh1, err := os.Create(fileName)
    if err != nil {
        panic("os.Create failed: "+err.Error())
    }
    _, err = fh1.Write([]byte("test"))
    if err != nil {
        panic("fh.Write failed: "+err.Error())
    }
    fh1.Seek(0, os.SEEK_SET)

    fh2, err := os.OpenFile(fileName, os.O_RDWR, 0777)
    if err != nil {
        panic("os.OpenFile failed: "+err.Error())
    }
    fh2.Seek(0, os.SEEK_END)
    fh2.Write([]byte("data"))
    fh2.Close()

    fh1.Write([]byte("data"))
    fh1.Close()
    // the file now should contain "datadata"
    data, _ := ioutil.ReadFile(fileName)
    fmt.Printf("OS=<%s>\n", data)

    fs := &afero.MemMapFs{}
    afh1, err := fs.Create(fileName)
    if err != nil {
        panic("fs.Create failed: "+err.Error())
    }
    _, err = afh1.Write([]byte("test"))
    if err != nil {
        panic("fh.Write failed: "+err.Error())
    }
    afh1.Seek(0, os.SEEK_SET)

    afh2, err := fs.OpenFile(fileName, os.O_RDWR, 0777)
    if err != nil {
        panic("fs.OpenFile failed: "+err.Error())
    }
    afh2.Seek(0, os.SEEK_END)
    afh2.Write([]byte("data"))
    afh2.Close()

    afh1.Write([]byte("data"))
    afh1.Close()
    // the file now should contain "datadata"
    data, _ = afero.ReadFile(fs, fileName)
    fmt.Printf("FS=<%s>\n", data)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants