aboutsummaryrefslogtreecommitdiff
path: root/cmd/cat.go
blob: 186f7da9b27f85ade35c5575dbf7c8fcd511cad1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package cmd

import (
	"fmt"
	"golang.flu0r1ne.net/zfdiff/snap"
	"os"
	"io"
)

func Cat(params []string) {
	n_params := len(params)

	if(n_params == 0) {
		die.Fatal("Reference file is required")
	}

	if(n_params > 3) {
		die.Fatal("Too many arguments provided")
	}

	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
        }

        if err != nil {
            die.Fatal("Encountered error while reading file: %v", err)
        }
    }
}