From 6f470766cbff04da020c7496392a4e13ea6e4206 Mon Sep 17 00:00:00 2001 From: Nicolas Simonds Date: Wed, 19 Jun 2024 14:37:00 -0700 Subject: [PATCH] fix: make CopyFile create target directories with sensible permissions Traversal bits (aka, x-bits) on directories are occasionally useful. Make sure they are set when creating target dirs. Fixes: Issue #214 --- internal/repository/copy.go | 6 +++++- internal/repository/copy_public_test.go | 4 ++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/internal/repository/copy.go b/internal/repository/copy.go index 42128de..b165a44 100644 --- a/internal/repository/copy.go +++ b/internal/repository/copy.go @@ -68,9 +68,13 @@ func (r *Copy) CopyFile( return err } + // Ensure target directory exists; it will be created with the same mode as + // the target file, plus x-bits. + dirMode := si.Mode() & 0o777 + dirMode |= ((dirMode & 0o444) >> 2) | ((dirMode & 0o222) >> 1) + _ = r.appFs.MkdirAll(r.appFs.Dir(dst), dirMode) // Open dest file for writing; make it owner-only perms before putting // anything in it - _ = r.appFs.MkdirAll(r.appFs.Dir(dst), si.Mode()) out, err := r.appFs.Create(dst) if err != nil { return err diff --git a/internal/repository/copy_public_test.go b/internal/repository/copy_public_test.go index 7ec3fad..0449a6a 100644 --- a/internal/repository/copy_public_test.go +++ b/internal/repository/copy_public_test.go @@ -76,6 +76,10 @@ func (suite *CopyPublicTestSuite) TestCopyFileOk() { got, _ := avfs.Exists(suite.appFs, assertFile) assert.True(suite.T(), got) + // ensure target dir is created with sensible permissions — expressed + // in "canonical" form here so the test result is legible + parent, _ := suite.appFs.Stat(suite.dstDir) + assert.Equal(suite.T(), fs.FileMode(0o20000000755), parent.Mode()) } func (suite *CopyPublicTestSuite) TestCopyFileReturnsError() {