github.com/secure-build/gitlab-runner@v12.5.0+incompatible/helpers/archives/zip_extra_unix.go (about)

     1  // +build linux darwin freebsd openbsd
     2  
     3  package archives
     4  
     5  import (
     6  	"archive/zip"
     7  	"bytes"
     8  	"encoding/binary"
     9  	"errors"
    10  	"io"
    11  	"os"
    12  	"syscall"
    13  )
    14  
    15  func createZipUIDGidField(w io.Writer, fi os.FileInfo) (err error) {
    16  	stat, ok := fi.Sys().(*syscall.Stat_t)
    17  	if !ok {
    18  		return
    19  	}
    20  
    21  	ugField := ZipUIDGidField{
    22  		1,
    23  		4, stat.Uid,
    24  		4, stat.Gid,
    25  	}
    26  	ugFieldType := ZipExtraField{
    27  		Type: ZipUIDGidFieldType,
    28  		Size: uint16(binary.Size(&ugField)),
    29  	}
    30  	err = binary.Write(w, binary.LittleEndian, &ugFieldType)
    31  	if err == nil {
    32  		err = binary.Write(w, binary.LittleEndian, &ugField)
    33  	}
    34  	return err
    35  }
    36  
    37  func processZipUIDGidField(data []byte, file *zip.FileHeader) error {
    38  	var ugField ZipUIDGidField
    39  	err := binary.Read(bytes.NewReader(data), binary.LittleEndian, &ugField)
    40  	if err != nil {
    41  		return err
    42  	}
    43  
    44  	if !(ugField.Version == 1 && ugField.UIDSize == 4 && ugField.GIDSize == 4) {
    45  		return errors.New("uid/gid data not supported")
    46  	}
    47  
    48  	return os.Lchown(file.Name, int(ugField.UID), int(ugField.Gid))
    49  }