forked from blakesmith/ar
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwriter.go
181 lines (161 loc) · 5.16 KB
/
writer.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
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
/*
Copyright (c) 2013 Blake Smith <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package ar
import (
"bytes"
"errors"
"io"
"strconv"
)
var (
ErrWriteTooLong = errors.New("ar: write too long")
)
// Writer provides sequential writing of an ar archive.
// An ar archive is sequence of header file pairs
// Call WriteHeader to begin writing a new file, then call Write to supply the file's data
//
// Example:
// archive := ar.NewWriter(writer)
// archive.WriteGlobalHeader()
// header := new(ar.Header)
// header.Size = 15 // bytes
// if err := archive.WriteHeader(header); err != nil {
// return err
// }
// io.Copy(archive, data)
type Writer struct {
w io.Writer
nb int64 // number of unwritten bytes for the current file entry
longFilenames map[string]int // content for the GNU long filenames entry (if needed)
}
// Create a new ar writer that writes to w
func NewWriter(w io.Writer) *Writer { return &Writer{w: w, longFilenames: map[string]int{}} }
func (aw *Writer) numeric(b []byte, x int64) {
s := strconv.FormatInt(x, 10)
for len(s) < len(b) {
s = s + " "
}
copy(b, []byte(s))
}
func (aw *Writer) octal(b []byte, x int64) {
s := "100" + strconv.FormatInt(x, 8)
for len(s) < len(b) {
s = s + " "
}
copy(b, []byte(s))
}
func (aw *Writer) string(b []byte, str string) {
s := str
for len(s) < len(b) {
s = s + " "
}
copy(b, []byte(s))
}
// Writes to the current entry in the ar archive
// Returns ErrWriteTooLong if more than header.Size
// bytes are written after a call to WriteHeader
func (aw *Writer) Write(b []byte) (n int, err error) {
if int64(len(b)) > aw.nb {
b = b[0:aw.nb]
err = ErrWriteTooLong
}
n, werr := aw.w.Write(b)
aw.nb -= int64(n)
if werr != nil {
return n, werr
}
if len(b)%2 == 1 { // data size must be aligned to an even byte
if _, err := aw.w.Write([]byte{'\n'}); err != nil {
// Return n although we actually wrote n+1 bytes.
// This is to make io.Copy() to work correctly.
return n, err
}
}
return
}
func (aw *Writer) WriteGlobalHeader() error {
_, err := aw.w.Write([]byte(GLOBAL_HEADER))
return err
}
// WriteGlobalHeaderForLongFiles writes the global header, and any GNU-style entries to handle
// "long" filenames (i.e. ones over 16 chars).
// If you do not call this (and just call WriteGlobalHeader) then long filenames will be written
// in BSD style later on.
func (aw *Writer) WriteGlobalHeaderForLongFiles(filenames []string) error {
if err := aw.WriteGlobalHeader(); err != nil {
return err
}
var data []byte
for _, filename := range filenames {
if len(filename) > 16 {
aw.longFilenames[filename] = len(data)
data = append(data, []byte(filename)...)
data = append(data, '/')
data = append(data, '\n')
}
}
if len(data) == 0 {
return nil
}
// need at least one long filename
if err := aw.WriteHeader(&Header{Name: "//", Mode: 0420, Size: int64(len(data))}); err != nil {
return err
}
_, err := io.Copy(aw, bytes.NewReader(data))
return err
}
// Writes the header to the underlying writer and prepares
// to receive the file payload
func (aw *Writer) WriteHeader(hdr *Header) error {
aw.nb = int64(hdr.Size)
header := make([]byte, HEADER_BYTE_SIZE)
s := slicer(header)
var bsdName []byte
if len(hdr.Name) > 16 {
idx, present := aw.longFilenames[hdr.Name]
if present {
// already known, write GNU-style name
aw.string(s.next(16), "/"+strconv.Itoa(idx))
} else {
// not known, assume they want BSD-style names.
bsdName = append([]byte(hdr.Name), 0, 0) // seems to pad with at least two nulls
if len(bsdName)%2 != 0 {
bsdName = append(bsdName, 0) // pad out to an even number
}
aw.string(s.next(16), "#1/"+strconv.Itoa(len(bsdName)))
// These seem to pad with two nulls?
aw.nb += int64(len(bsdName))
hdr.Size += int64(len(bsdName))
}
} else {
aw.string(s.next(16), hdr.Name)
}
aw.numeric(s.next(12), hdr.ModTime.Unix())
aw.numeric(s.next(6), int64(hdr.Uid))
aw.numeric(s.next(6), int64(hdr.Gid))
aw.octal(s.next(8), hdr.Mode)
aw.numeric(s.next(10), hdr.Size)
aw.string(s.next(2), "`\n")
_, err := aw.w.Write(header)
if err == nil && bsdName != nil {
// BSD-style writes the name before the data section
_, err = aw.Write(bsdName)
}
return err
}