File: zip_extra_unix.go

package info (click to toggle)
gitlab-ci-multi-runner 14.10.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 31,248 kB
  • sloc: sh: 1,694; makefile: 384; asm: 79; ruby: 68
file content (50 lines) | stat: -rw-r--r-- 1,168 bytes parent folder | download
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
//go:build aix || android || darwin || dragonfly || freebsd || hurd || illumos || linux || netbsd || openbsd || solaris
// +build aix android darwin dragonfly freebsd hurd illumos linux netbsd openbsd solaris

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 err
}

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))
}