/
zip_extra_unix.go
49 lines (42 loc) · 998 Bytes
/
zip_extra_unix.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
// +build linux darwin freebsd openbsd
package archives
import (
"archive/zip"
"bytes"
"encoding/binary"
"errors"
"io"
"os"
"syscall"
)
func createZipUIDGidField(w io.Writer, fi os.FileInfo) (err error) {
stat, ok := fi.Sys().(*syscall.Stat_t)
if !ok {
return
}
ugField := ZipUIDGidField{
1,
4, stat.Uid,
4, stat.Gid,
}
ugFieldType := ZipExtraField{
Type: ZipUIDGidFieldType,
Size: uint16(binary.Size(&ugField)),
}
err = binary.Write(w, binary.LittleEndian, &ugFieldType)
if err == nil {
err = binary.Write(w, binary.LittleEndian, &ugField)
}
return nil
}
func processZipUIDGidField(data []byte, file *zip.FileHeader) error {
var ugField ZipUIDGidField
err := binary.Read(bytes.NewReader(data), binary.LittleEndian, &ugField)
if err != nil {
return err
}
if !(ugField.Version == 1 && ugField.UIDSize == 4 && ugField.GIDSize == 4) {
return errors.New("uid/gid data not supported")
}
return os.Lchown(file.Name, int(ugField.UID), int(ugField.Gid))
}