forked from wagoodman/bashful
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharchive.go
144 lines (119 loc) · 2.75 KB
/
archive.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package main
import (
"archive/tar"
"compress/gzip"
"errors"
"io"
"os"
"path/filepath"
"strings"
)
type Archiver interface {
Archive(srcPath string, preservePath bool) error
Close()
}
func NewArchive(dest string) Archiver {
fw, err := os.Create(dest)
checkError(err, "Could not create archive file")
gw := gzip.NewWriter(fw)
tw := tar.NewWriter(gw)
return &archive{
outputFile: fw,
gzipWriter: gw,
tarWriter: tw,
}
}
type archive struct {
outputFile *os.File
gzipWriter *gzip.Writer
tarWriter *tar.Writer
}
func (archiver *archive) Close() {
archiver.tarWriter.Close()
archiver.gzipWriter.Close()
archiver.outputFile.Close()
}
func isDir(pth string) (bool, error) {
fi, err := os.Stat(pth)
if err != nil {
return false, err
}
return fi.Mode().IsDir(), nil
}
func (archiver *archive) Archive(srcPath string, preservePath bool) error {
absPath, err := filepath.Abs(srcPath)
if err != nil {
return err
}
isDirectory, err := isDir(srcPath)
checkError(err, "Could not determine if this is a directory.")
if isDirectory || !preservePath {
err = filepath.Walk(absPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
var relative string
if os.IsPathSeparator(srcPath[len(srcPath)-1]) {
relative, err = filepath.Rel(absPath, path)
} else {
relative, err = filepath.Rel(filepath.Dir(absPath), path)
}
relative = filepath.ToSlash(relative)
if err != nil {
return err
}
return archiver.addTarFile(path, relative)
})
} else {
fields := strings.Split(srcPath, string(os.PathSeparator))
for idx := range fields {
path := strings.Join(fields[:idx+1], string(os.PathSeparator))
err := archiver.addTarFile(path, path)
checkError(err, "Unable to archive file")
}
}
return err
}
func (archiver *archive) addTarFile(path, name string) error {
if strings.Contains(path, "..") {
return errors.New("Path cannot contain a relative marker of '..': " + path)
}
fi, err := os.Lstat(path)
if err != nil {
return err
}
link := ""
if fi.Mode()&os.ModeSymlink != 0 {
if link, err = os.Readlink(path); err != nil {
return err
}
}
hdr, err := tar.FileInfoHeader(fi, link)
if err != nil {
return err
}
if fi.IsDir() && !os.IsPathSeparator(name[len(name)-1]) {
name = name + "/"
}
if hdr.Typeflag == tar.TypeReg && name == "." {
// archiving a single file
hdr.Name = filepath.ToSlash(filepath.Base(path))
} else {
hdr.Name = filepath.ToSlash(name)
}
if err := archiver.tarWriter.WriteHeader(hdr); err != nil {
return err
}
if hdr.Typeflag == tar.TypeReg {
file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()
_, err = io.Copy(archiver.tarWriter, file)
if err != nil {
return err
}
}
return nil
}