diff options
Diffstat (limited to 'cmd/cat.go')
-rw-r--r-- | cmd/cat.go | 54 |
1 files changed, 49 insertions, 5 deletions
@@ -3,19 +3,63 @@ package cmd import ( "fmt" "golang.flu0r1ne.net/zfdiff/snap" + "os" + "io" ) func Cat(params []string) { - if(len(params) == 0) { + n_params := len(params) + + if(n_params == 0) { die.Fatal("Reference file is required") } - if(len(params) > 3) { + if(n_params > 3) { die.Fatal("Too many arguments provided") } - snapRef := snap.ToRelative(params[0]) - reference := params[1] + var reference string + snapish := "" + + if(n_params == 1) { + reference = params[0] + } else { + snapish, reference = params[0], params[1] + } + + snapRef := snap.ToRelative(snapish) + + oracle := snap.GetOracle(reference) + + path := oracle.ResolveRelative(snapRef) + + file, err := os.Open(path) + + if err != nil { + die.Fatal("Could not open snapshot %s\nError: %v", path, err) + } + + defer func() { + if err = file.Close(); err != nil { + die.Fatal("Could not close file %s\nError: %v", path, err) + } + }() + + buf := make([]byte, 10*1024*1024) //10Ki + + for { + n, err := file.Read(buf) + + if n > 0 { + fmt.Printf("%s", buf[:n]) + } + + if err == io.EOF { + break + } - fmt.Println(snapRef, reference); + if err != nil { + die.Fatal("Encountered error while reading file: %v", err) + } + } }
\ No newline at end of file |