mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2024-12-25 09:50:31 +00:00
61 lines
915 B
Go
61 lines
915 B
Go
|
package update
|
||
|
|
||
|
import (
|
||
|
"archive/tar"
|
||
|
"compress/gzip"
|
||
|
"io"
|
||
|
"io/fs"
|
||
|
"os"
|
||
|
"path/filepath"
|
||
|
)
|
||
|
|
||
|
const tarDirectoryMode fs.FileMode = 0x755
|
||
|
|
||
|
func Untar(dst string, r io.Reader) error {
|
||
|
gzr, err := gzip.NewReader(r)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
defer gzr.Close()
|
||
|
|
||
|
tr := tar.NewReader(gzr)
|
||
|
|
||
|
for {
|
||
|
header, err := tr.Next()
|
||
|
|
||
|
switch {
|
||
|
case err == io.EOF:
|
||
|
return nil
|
||
|
|
||
|
case err != nil:
|
||
|
return err
|
||
|
|
||
|
case header == nil:
|
||
|
continue
|
||
|
}
|
||
|
|
||
|
target := filepath.Join(dst, header.Name)
|
||
|
|
||
|
switch header.Typeflag {
|
||
|
case tar.TypeDir:
|
||
|
if _, err := os.Stat(target); err != nil {
|
||
|
if err := os.MkdirAll(target, tarDirectoryMode); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
}
|
||
|
|
||
|
case tar.TypeReg:
|
||
|
f, err := os.OpenFile(target, os.O_CREATE|os.O_RDWR, os.FileMode(header.Mode))
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
if _, err := io.Copy(f, tr); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
f.Close()
|
||
|
}
|
||
|
}
|
||
|
}
|