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 爬虫爬取网站的一些信息
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题
  • ¥15 Utunbu中vscode下cern root工作台中写的程序root的头文件无法包含
  • ¥15 麒麟V10桌面版SP1如何配置bonding
  • ¥15 Marscode IDE 如何预览新建的 HTML 文件
  • ¥15 K8S部署二进制集群过程中calico一直报错