Skip to content

Commit

Permalink
Merge pull request #1672 from ryanmoran/main
Browse files Browse the repository at this point in the history
Use OS-default permissions when creating archive root
  • Loading branch information
jkutner committed Mar 24, 2023
2 parents 759d672 + 1186f6a commit 11e71c7
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 deletions.
2 changes: 1 addition & 1 deletion internal/build/container_ops_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ drwxrwxrwx 2 123 456 (.*) some-vol
} else {
// Expected results
h.AssertContainsMatch(t, outBuf.String(), `
drwsrwsrwt 2 123 456 (.*) some-vol
drwxr-xr-x 2 123 456 (.*) some-vol
`)
}
}
Expand Down
5 changes: 5 additions & 0 deletions pkg/archive/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"archive/tar"
"archive/zip"
"io"
"io/fs"
"os"
"path/filepath"
"time"
Expand All @@ -16,6 +17,7 @@ import (
)

var NormalizedDateTime time.Time
var Umask fs.FileMode

func init() {
NormalizedDateTime = time.Date(1980, time.January, 1, 0, 0, 1, 0, time.UTC)
Expand Down Expand Up @@ -172,6 +174,9 @@ func WriteDirToTar(tw TarWriter, srcDir, basePath string, uid, gid int, mode int
Name: basePath,
Mode: mode,
}
if rootHeader.Mode == -1 {
rootHeader.Mode = int64(fs.ModePerm &^ Umask)
}
finalizeHeader(rootHeader, uid, gid, mode, normalizeModTime)
if err := tw.WriteHeader(rootHeader); err != nil {
return err
Expand Down
4 changes: 3 additions & 1 deletion pkg/archive/archive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package archive_test

import (
"archive/tar"
"io/fs"
"math/rand"
"net"
"os"
Expand Down Expand Up @@ -246,7 +247,7 @@ func testArchive(t *testing.T, when spec.G, it spec.S) {

tw := tar.NewWriter(fh)

err = archive.WriteDirToTar(tw, src, "/nested/dir/dir-in-archive", 1234, 2345, -1, true, false, nil)
err = archive.WriteDirToTar(tw, src, "/nested/dir/dir-in-archive", 1234, 2345, -1, true, true, nil)
h.AssertNil(t, err)
h.AssertNil(t, tw.Close())
h.AssertNil(t, fh.Close())
Expand All @@ -258,6 +259,7 @@ func testArchive(t *testing.T, when spec.G, it spec.S) {
tr := tar.NewReader(file)

verify := h.NewTarVerifier(t, tr, 1234, 2345)
verify.NextDirectory("/nested/dir/dir-in-archive", int64(fs.ModePerm&^archive.Umask))
verify.NextFile("/nested/dir/dir-in-archive/some-file.txt", "some-content", fileMode(t, filepath.Join(src, "some-file.txt")))
verify.NextDirectory("/nested/dir/dir-in-archive/sub-dir", fileMode(t, filepath.Join(src, "sub-dir")))
if runtime.GOOS != "windows" {
Expand Down
13 changes: 13 additions & 0 deletions pkg/archive/umask_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//go:build unix

package archive

import (
"io/fs"
"syscall"
)

func init() {
Umask = fs.FileMode(syscall.Umask(0))
syscall.Umask(int(Umask))
}

0 comments on commit 11e71c7

Please sign in to comment.