-
Notifications
You must be signed in to change notification settings - Fork 0
/
tar.go
83 lines (74 loc) · 1.92 KB
/
tar.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
package tar
// The following example shows how to create a tarball as a table in the SQLFS.
/*
func Tar(db *sql.DB, dir string, inc include, compress bool) (e error) {
// Create a file in the SQLFS.
sqlfn := fmt.Sprintf("sqlflow_models.%s", dir)
sqlf, e := sqlfs.Create(db, sqlfn)
if e != nil {
return fmt.Errorf("Cannot create sqlfs file %s: %v", sqlfn, e)
}
defer func() { e = sqlf.Close() }()
return tar.Tar(sqlf, dir, inc, compress)
}
*/
import (
"archive/tar"
"compress/gzip"
"fmt"
"io"
"io/ioutil"
"os"
"path"
)
type include func(dir, relative string, fi os.FileInfo) bool
// Tar a directory into a writer.
func Tar(w io.Writer, dir string, inc include, compress bool) (e error) {
var tw *tar.Writer
if compress {
gw := gzip.NewWriter(w)
defer gw.Close()
tw = tar.NewWriter(gw)
} else {
tw = tar.NewWriter(w)
}
defer tw.Close()
return recursiveTar(tw, dir, "", inc, compress)
}
func recursiveTar(tw *tar.Writer, dir, relative string, inc include, compress bool) (e error) {
abs := path.Join(dir, relative)
fis, e := ioutil.ReadDir(abs)
if e != nil {
return fmt.Errorf("Tar: ReadDir(%s) failed: %v", abs, e)
}
for _, fs := range fis {
if inc == nil || inc(dir, relative, fs) { // Include only certain files.
fn := path.Join(relative, fs.Name())
if fs.IsDir() {
if e = recursiveTar(tw, dir, fn, inc, compress); e != nil {
return e
}
} else {
if e = tw.WriteHeader(&tar.Header{
Name: fn,
Mode: 0600,
Size: fs.Size()}); e != nil {
return fmt.Errorf("Tar: WriteHeader(%s): %v", fn, e)
}
f, e := os.Open(path.Join(dir, fn))
if e != nil {
return fmt.Errorf("Tar: cannot open %s: %v", fn, e)
}
n, e := io.Copy(tw, f)
if n != fs.Size() {
return fmt.Errorf("Tar: copied %d of %d bytes of %s", n, fs.Size(), fn)
}
if e != nil {
return fmt.Errorf("Tar: failed to copy %s: %v", fn, e)
}
f.Close()
}
}
}
return nil
}