forked from rolinh/targo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtargo.go
More file actions
194 lines (166 loc) · 4.3 KB
/
targo.go
File metadata and controls
194 lines (166 loc) · 4.3 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// Copyright 2015 Robin Hahling. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package targo provides functions to create or extract tar archives.
package targo
import (
"archive/tar"
"errors"
"io"
"os"
"path/filepath"
"strings"
)
// Create creates a tar archive from a directory.
// The resulting tar archive format is in POSIX.1 format.
func Create(destPath, dirPath string) error {
fi, err := os.Stat(dirPath)
if err != nil {
return err
}
if !fi.IsDir() {
return errors.New("given path is not a directory: " + dirPath)
}
file, err := os.Create(destPath)
if err != nil {
return err
}
defer file.Close()
tw := tar.NewWriter(file)
defer tw.Close()
err = filepath.Walk(dirPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return nil
}
var link string
mode := info.Mode()
switch {
// symlinks need special treatment
case mode&os.ModeSymlink != 0:
linkDest, _ := os.Readlink(path)
if link, err = filepath.EvalSymlinks(path); err == nil {
if rel, err := filepath.Rel(filepath.Dir(path), link); err == nil {
link = rel
} else {
link = linkDest
}
} else {
// it may be a broken symlink, simply attempt to read it
link = linkDest
}
// we don't want to tar these sort of files
case mode&(os.ModeNamedPipe|os.ModeSocket|os.ModeDevice) != 0:
return nil
}
hdr, err := tar.FileInfoHeader(info, link)
if err != nil {
return err
}
// Name is usually only the basename when created with FileInfoHeader()
hdr.Name, err = filepath.Rel(filepath.Dir(dirPath), path)
if err != nil {
return err
}
// Some pre-POSIX.1-1988 tar implementations indicated a directory by
// having a trailing slash in the name. Honor that here.
if info.IsDir() {
hdr.Name += "/"
}
if err := tw.WriteHeader(hdr); err != nil {
return err
}
// no content to write if it is a directory or symlink
if !info.Mode().IsRegular() {
return nil
}
return func() error {
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
if _, err = io.Copy(tw, f); err != nil {
return err
}
return nil
}()
})
return err
}
// CreateInPlace creates a tar archive from a directory in place which means
// that the original directory is removed after the tar archive is created.
// The .tar suffix will be added to dirPath once the archive is created.
func CreateInPlace(dirPath string) error {
if err := Create(dirPath+".tar", dirPath); err != nil {
return err
}
return os.RemoveAll(dirPath)
}
// Extract extracts a tar archive given its path.
func Extract(destPath, archivePath string) error {
fi, err := os.Stat(archivePath)
if err != nil {
return err
}
if fi.IsDir() {
return errors.New("given path is a directory: " + archivePath)
}
if err := os.MkdirAll(destPath, os.ModePerm); err != nil {
return err
}
archiveFile, err := os.Open(archivePath)
if err != nil {
return err
}
defer archiveFile.Close()
tr := tar.NewReader(archiveFile)
for {
hdr, err := tr.Next()
if err == io.EOF {
break
}
if err != nil {
return err
}
mode := hdr.FileInfo().Mode()
switch {
case mode&os.ModeDir != 0:
if err := os.Mkdir(filepath.Join(destPath, hdr.Name), mode); err != nil {
return err
}
case mode&os.ModeSymlink != 0:
os.Symlink(hdr.Linkname, filepath.Join(destPath, hdr.Name))
default: // consider it a regular file
createFile := func() error {
f, err := os.Create(filepath.Join(destPath, hdr.Name))
if err != nil {
return err
}
defer f.Close()
if _, err := io.Copy(f, tr); err != nil {
return err
}
return nil
}
if err = createFile(); err != nil {
return err
}
}
}
return nil
}
// ExtractInPlace extracts a tar archive, in place, given its path. The
// original tar archive is removed after extraction and only its content
// remains.
// Note that archivePath is expected to have a file extension.
func ExtractInPlace(archivePath string) error {
ext := filepath.Ext(archivePath)
if ext == "" {
return errors.New("expected a file extension (" + archivePath + ")")
}
destPath := filepath.Dir(strings.TrimSuffix(archivePath, ext))
if err := Extract(destPath, archivePath); err != nil {
return err
}
return os.Remove(archivePath)
}