doudong8713 2016-10-19 08:16
浏览 153
已采纳

Golang虚拟文件

I have a closed soruce application that takes a file as an input, calculates its hash and does some other stuff I have no control. Modifiying source or reverse engineering is not feasible.

The program is designed to work with regular files, however I need to supply a very large file from HDFS. Copying the file will take too much time and space on disk. So I was thinking of using FUSE but I did not find a good solution. I tried using a named pipe as follows:

func readFile(namenode, path string, pipe *os.File) {
    client, err := hdfs.New(namenode)
    log.Println(err, client)

    hdfsFile, err := client.Open(path)
    if err != nil {
        log.Fatal(err)
    }
    log.Println(hdfsFile)

    // written, err := io.Copy(pipe, hdfsFile)
    bytes := make([]byte, 4096)
    for {
        read, err := hdfsFile.Read(bytes)
        log.Println(read, err)
        if err != nil {
            break
        }
        written, err := pipe.Write(bytes)
        log.Println(written, err)
    }
    err = pipe.Close()
    log.Println(err)
}

I know the above code is not complete, the test file is 10MB, however after reading 8 times 4096 bytes named pipe buffer gets full and the other program takes it all and closes the pipe.

But after a while the other program that is reading the pipe closes the pipe and I get broken pipe error. Is there any possibility of creating a virtual file other than fuse and pipe?

  • 写回答

2条回答 默认 最新

  • dshyu6866 2016-10-19 09:23
    关注

    I think you actually had the right idea with FUSE. Without sources for your upstream application, it's hard to say what file semantics it's trying to use (though some time with strace might help to illuminate what's going on. Maybe...).

    In any case, I'd have a look at the Go-FUSE project, specifically the hello.go example, which shows exactly how to handle the single file case very well.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog