Fix calculating path for copying ownership (#1859)

* Fix calculating path for copying ownership

* fix CI
This commit is contained in:
Andrei Kvapil 2021-12-24 13:09:53 +01:00 committed by GitHub
parent 7065921aa4
commit e38b0c8d90
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 10 deletions

View File

@ -1,3 +1,17 @@
# Copyright 2021 Google, Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
FROM alpine:3.11 as builder FROM alpine:3.11 as builder
RUN mkdir -p /myapp/somedir \ RUN mkdir -p /myapp/somedir \

View File

@ -905,7 +905,7 @@ func CopyFileOrSymlink(src string, destDir string, root string) error {
if err != nil { if err != nil {
return errors.Wrap(err, "copying file") return errors.Wrap(err, "copying file")
} }
err = CopyOwnership(src, destDir) err = CopyOwnership(src, destDir, root)
if err != nil { if err != nil {
return errors.Wrap(err, "copying ownership") return errors.Wrap(err, "copying ownership")
} }
@ -913,7 +913,7 @@ func CopyFileOrSymlink(src string, destDir string, root string) error {
} }
// CopyOwnership copies the file or directory ownership recursively at src to dest // CopyOwnership copies the file or directory ownership recursively at src to dest
func CopyOwnership(src string, destDir string) error { func CopyOwnership(src string, destDir string, root string) error {
return filepath.Walk(src, func(path string, info os.FileInfo, err error) error { return filepath.Walk(src, func(path string, info os.FileInfo, err error) error {
if err != nil { if err != nil {
return err return err
@ -921,7 +921,7 @@ func CopyOwnership(src string, destDir string) error {
if IsSymlink(info) { if IsSymlink(info) {
return nil return nil
} }
relPath, err := filepath.Rel(filepath.Dir(src), path) relPath, err := filepath.Rel(root, path)
if err != nil { if err != nil {
return err return err
} }
@ -955,9 +955,7 @@ func CopyOwnership(src string, destDir string) error {
return errors.Wrap(err, "reading ownership") return errors.Wrap(err, "reading ownership")
} }
stat := info.Sys().(*syscall.Stat_t) stat := info.Sys().(*syscall.Stat_t)
err = os.Chown(destPath, int(stat.Uid), int(stat.Gid)) return os.Chown(destPath, int(stat.Uid), int(stat.Gid))
return nil
}) })
} }

View File

@ -62,10 +62,10 @@ func Test_IsLocalTarArchive(t *testing.T) {
func Test_AddFileToTar(t *testing.T) { func Test_AddFileToTar(t *testing.T) {
testDir, err := ioutil.TempDir("", "") testDir, err := ioutil.TempDir("", "")
if err != nil { if err != nil {
t.Fatalf("err setting up temp dir: %v", err) t.Fatalf("err setting up temp dir: %v", err)
} }
defer os.RemoveAll(testDir) defer os.RemoveAll(testDir)
path := filepath.Join(testDir, regularFiles[0]) path := filepath.Join(testDir, regularFiles[0])
if err := ioutil.WriteFile(path, []byte("hello"), os.ModePerm); err != nil { if err := ioutil.WriteFile(path, []byte("hello"), os.ModePerm); err != nil {