Skip to content

Instantly share code, notes, and snippets.

@sahib
Created April 15, 2016 11:10
Show Gist options
  • Save sahib/4363c36dc2352d1fba7634fd46eb7978 to your computer and use it in GitHub Desktop.
Save sahib/4363c36dc2352d1fba7634fd46eb7978 to your computer and use it in GitHub Desktop.
package main
import (
"flag"
"fmt"
"golang.org/x/net/context"
"os"
"os/user"
"path/filepath"
core "github.com/ipfs/go-ipfs/core"
coreunix "github.com/ipfs/go-ipfs/core/coreunix"
fsrepo "github.com/ipfs/go-ipfs/repo/fsrepo"
)
func main() {
repo := flag.String("repo", "", "Path to the ipfs repo")
hash := flag.String("hash", "", "B58 hash of the file")
flag.Parse()
if *hash == "" {
fmt.Println("Need a hash.")
return
}
if *repo == "" {
user, err := user.Current()
if err != nil {
fmt.Println("Failed to guess user dir")
return
}
*repo = filepath.Join(user.HomeDir, ".ipfs")
}
rp, err := fsrepo.Open(*repo)
if err != nil {
fmt.Println("Unable to open repo `%s`: %v", *repo, err)
return
}
cfg := &core.BuildCfg{
Repo: rp,
Online: false,
}
ctx := context.Background()
nd, err := core.NewNode(ctx, cfg)
if err != nil {
fmt.Println("Creating new node failed", err)
return
}
reader, err := coreunix.Cat(ctx, nd, *hash)
if err != nil {
fmt.Println("ipfs cat: %v", err)
return
}
////////////////////////////////////////
// END OF SETUP - TROUBLE STARTS HERE //
////////////////////////////////////////
// This is not necessary to trigger the bug, just
// to show that the first call returns the correct offset
fmt.Println(reader.Seek(0, os.SEEK_END))
n := int64(1) // Even 0 seems to trigger the bug.
// This should be negative, but ipfs uses positive offsets for SEEK_END?
// https://github.com/ipfs/go-ipfs/blob/master/unixfs/io/dagreader.go#L277
// fmt.Println(reader.Seek(-n, os.SEEK_END))
fmt.Println(reader.Seek(n, os.SEEK_END))
fmt.Println(reader.Read(make([]byte, n)))
// This prints (0 EOF) for larger files (probably around 256K is the "bug border"),
// however returns the actual size of the file for smaller files.
fmt.Println(reader.Seek(0, os.SEEK_END))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment