aboutsummaryrefslogtreecommitdiff
path: root/cmd/cat.go
blob: f3ea2bf0b68abce14f03aa8b8d9fbbf14e5c73bf (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
package cmd

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

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

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

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

	reference := params[0]

	snapish := "@"
	if(n_params > 1) {
		snapish = params[1]
	}

	snapRef := snap.ToRelative(snapish)

	oracle := snap.GetOracle(reference)

	path := oracle.PathTo(snapRef)

	file, err := os.Open(path)

	if err != nil {
		die.Fatalf("Could not open snapshot %s\nError: %v", path, err)
	}

	defer func() {
        if err = file.Close(); err != nil {
            die.Fatalf("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.Fatalf("Encountered error while reading file: %v", err)
        }
    }
}