/
cat.go
116 lines (97 loc) · 2.98 KB
/
cat.go
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package main
import (
"context"
"errors"
"flag"
"fmt"
"os"
"io"
"github.com/folbricht/desync"
)
const catUsage = `desync cat [options] <index> [<outputfile>]
Stream a blob to stdout or a file-like object, optionally seeking and limiting
the read length.
Unlike extract, this supports output to FIFOs, named pipes, and other
non-seekable destinations.
This is inherently slower than extract as while multiple chunks can be
retrieved concurrently, writing to stdout cannot be parallelized.
Use '-' to read the index from STDIN.`
func cat(ctx context.Context, args []string) error {
var (
cacheLocation string
n int
err error
storeLocations = new(multiArg)
offset int
length int
readIndex bool
clientCert string
clientKey string
)
flags := flag.NewFlagSet("cat", flag.ExitOnError)
flags.Usage = func() {
fmt.Fprintln(os.Stderr, catUsage)
flags.PrintDefaults()
}
flags.BoolVar(&readIndex, "i", false, "Read index file (caidx), not catar, in 2-argument mode")
flags.Var(storeLocations, "s", "casync store location, can be multiples")
flags.StringVar(&cacheLocation, "c", "", "use local store as cache")
flags.IntVar(&n, "n", 10, "number of goroutines")
flags.IntVar(&offset, "o", 0, "offset in bytes to seek to before reading")
flags.IntVar(&length, "l", 0, "number of bytes to read")
flags.BoolVar(&desync.TrustInsecure, "t", false, "trust invalid certificates")
flags.StringVar(&clientCert, "clientCert", "", "Path to Client Certificate for TLS authentication")
flags.StringVar(&clientKey, "clientKey", "", "Path to Client Key for TLS authentication")
flags.Parse(args)
if flags.NArg() < 1 {
return errors.New("Not enough arguments. See -h for help.")
}
if flags.NArg() > 2 {
return errors.New("Too many arguments. See -h for help.")
}
if clientKey != "" && clientCert == "" || clientCert != "" && clientKey == "" {
return errors.New("-clientKey and -clientCert options need to be provided together.")
}
var outFile io.Writer
if flags.NArg() == 2 {
outFileName := flags.Arg(1)
outFile, err = os.Create(outFileName)
if err != nil {
return err
}
} else {
outFile = os.Stdout
}
inFile := flags.Arg(0)
// Checkout the store
if len(storeLocations.list) == 0 {
return errors.New("No casync store provided. See -h for help.")
}
// Parse the store locations, open the stores and add a cache is requested
opts := storeOptions{
n: n,
clientCert: clientCert,
clientKey: clientKey,
}
s, err := MultiStoreWithCache(opts, cacheLocation, storeLocations.list...)
if err != nil {
return err
}
defer s.Close()
// Read the input
c, err := readCaibxFile(inFile, opts)
if err != nil {
return err
}
// Write the output
readSeeker := desync.NewIndexReadSeeker(c, s)
if _, err = readSeeker.Seek(int64(offset), io.SeekStart); err != nil {
return err
}
if length > 0 {
_, err = io.CopyN(outFile, readSeeker, int64(length))
} else {
_, err = io.Copy(outFile, readSeeker)
}
return err
}