Skip to content
This repository has been archived by the owner on Mar 24, 2023. It is now read-only.

Commit

Permalink
set filemodes after writing file to sidestep umask
Browse files Browse the repository at this point in the history
debug.log override filemodes to clarify octal vs bin issues
  • Loading branch information
laverya committed Aug 26, 2019
1 parent 930e0c3 commit 451e277
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
1 change: 1 addition & 0 deletions pkg/lifecycle/render/github/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ func (r *LocalRenderer) resolveProxyGithubAssets(asset api.GitHubAsset, builder

mode := os.FileMode(0644) // TODO: how to get mode info from github?
if asset.AssetShared.Mode != os.FileMode(0000) {
debug.Log("event", "applying override permissions", "override.filemode", asset.AssetShared.Mode, "override.filemode.int", int(asset.AssetShared.Mode))
mode = asset.AssetShared.Mode
}
if err := rootFs.WriteFile(filePath, []byte(built), mode); err != nil {
Expand Down
6 changes: 5 additions & 1 deletion pkg/lifecycle/render/inline/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,18 @@ func (r *LocalRenderer) Execute(

mode := os.FileMode(0644)
if builtAsset.Mode != os.FileMode(0) {
debug.Log("event", "applying override permissions")
debug.Log("event", "applying override permissions", "override.filemode", builtAsset.Mode, "override.filemode.int", int(builtAsset.Mode))
mode = builtAsset.Mode
}

if err := rootFs.WriteFile(builtAsset.Dest, []byte(builtAsset.Contents), mode); err != nil {
debug.Log("event", "execute.fail", "err", err)
return errors.Wrapf(err, "Write inline asset to %s", builtAsset.Dest)
}
if err := rootFs.Chmod(builtAsset.Dest, mode); err != nil {
debug.Log("event", "chmod.fail", "err", err)
return errors.Wrapf(err, "Set inline asset %s filemode to %s", builtAsset.Dest, mode)
}
return nil

}
Expand Down
21 changes: 21 additions & 0 deletions pkg/lifecycle/render/inline/render_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"

"github.com/replicatedhq/libyaml"
Expand Down Expand Up @@ -227,6 +228,8 @@ func TestAfero(t *testing.T) {

err := aferoFS.WriteFile("test.txt", []byte("Hello, World!"), mode)
req.NoError(err)
err = aferoFS.Chmod("test.txt", mode)
req.NoError(err)

stat, err := aferoFS.Stat("test.txt")
req.NoError(err)
Expand All @@ -243,11 +246,29 @@ func TestAfero(t *testing.T) {

err = realFS.WriteFile("test.txt", []byte("Hello, World!"), mode)
req.NoError(err)
err = realFS.Chmod("test.txt", mode)
req.NoError(err)

stat, err := realFS.Stat("test.txt")
req.NoError(err)

req.Equal(fmt.Sprint(mode), fmt.Sprint(stat.Mode()))
})

t.Run(fmt.Sprint(mode)+" manual FS", func(t *testing.T) {
req := require.New(t)
tempdir, err := ioutil.TempDir("", "afero-test")
req.NoError(err)
defer os.RemoveAll(tempdir)

err = ioutil.WriteFile(filepath.Join(tempdir, "test.txt"), []byte("Hello, World!"), mode)
req.NoError(err)
err = os.Chmod(filepath.Join(tempdir, "test.txt"), mode)
req.NoError(err)
stat, err := os.Stat(filepath.Join(tempdir, "test.txt"))
req.NoError(err)

req.Equal(fmt.Sprint(mode), fmt.Sprint(stat.Mode()))
})
}
}

0 comments on commit 451e277

Please sign in to comment.