mirror of
https://gitee.com/rainbond/Rainbond.git
synced 2024-12-02 03:37:46 +08:00
[REV] zip support uid and gid
This commit is contained in:
parent
ebe217b36e
commit
8149e5151b
@ -28,6 +28,7 @@ import (
|
||||
"os/exec"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
@ -403,22 +404,23 @@ func Zip(source, target string) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
header, err := zip.FileInfoHeader(info)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if baseDir != "" {
|
||||
header.Name = filepath.Join(baseDir, strings.TrimPrefix(path, source))
|
||||
}
|
||||
|
||||
if info.IsDir() {
|
||||
header.Name += "/"
|
||||
} else {
|
||||
header.Method = zip.Deflate
|
||||
}
|
||||
|
||||
//set file uid and
|
||||
elem := reflect.ValueOf(info.Sys()).Elem()
|
||||
uid := elem.FieldByName("Uid").Uint()
|
||||
gid := elem.FieldByName("Gid").Uint()
|
||||
header.Comment = fmt.Sprintf("%d/%d", uid, gid)
|
||||
writer, err := archive.CreateHeader(header)
|
||||
if err != nil {
|
||||
return err
|
||||
@ -474,6 +476,16 @@ func Unzip(archive, target string) error {
|
||||
if _, err := io.Copy(targetFile, fileReader); err != nil {
|
||||
return err
|
||||
}
|
||||
if file.Comment != "" && strings.Contains(file.Comment, "/") {
|
||||
guid := strings.Split(file.Comment, "/")
|
||||
if len(guid) == 2 {
|
||||
uid, _ := strconv.Atoi(guid[0])
|
||||
gid, _ := strconv.Atoi(guid[1])
|
||||
if err := os.Chown(path, uid, gid); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if err := run(); err != nil {
|
||||
|
@ -51,13 +51,13 @@ func TestGetDirSizeByCmd(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestZip(t *testing.T) {
|
||||
if err := Zip("/tmp/test/", "/tmp/test.zip"); err != nil {
|
||||
if err := Zip("/tmp/cache", "/tmp/cache.zip"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnzip(t *testing.T) {
|
||||
if err := Unzip("/tmp/test.aaa.zip", "/tmp/rainbond"); err != nil {
|
||||
if err := Unzip("/tmp/cache.zip", "/tmp/cache0"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user