Skip to content

Commit

Permalink
Merge pull request #1437 from Luap99/subscription-save
Browse files Browse the repository at this point in the history
pkg/subscription: fix regression in saveTo()
  • Loading branch information
openshift-merge-robot authored Apr 26, 2023
2 parents a670892 + e6ca254 commit e45d573
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
14 changes: 9 additions & 5 deletions pkg/subscriptions/subscriptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,24 @@ var (
UserOverrideMountsFile = filepath.Join(os.Getenv("HOME"), ".config/containers/mounts.conf")
)

// subscriptionData stores the name of the file and the content read from it
// subscriptionData stores the relative name of the file and the content read from it
type subscriptionData struct {
name string
// relPath is the relative path to the file
relPath string
data []byte
mode os.FileMode
dirMode os.FileMode
}

// saveTo saves subscription data to given directory
func (s subscriptionData) saveTo(dir string) error {
if err := umask.MkdirAllIgnoreUmask(dir, s.dirMode); err != nil {
// We need to join the path here and create all parent directories, only
// creating dir is not good enough as relPath could also contain directories.
path := filepath.Join(dir, s.relPath)
if err := umask.MkdirAllIgnoreUmask(filepath.Dir(path), s.dirMode); err != nil {
return fmt.Errorf("create subscription directory: %w", err)
}
if err := umask.WriteFileIgnoreUmask(filepath.Join(dir, s.name), s.data, s.mode); err != nil {
if err := umask.WriteFileIgnoreUmask(path, s.data, s.mode); err != nil {
return fmt.Errorf("write subscription data: %w", err)
}
return nil
Expand Down Expand Up @@ -96,7 +100,7 @@ func readFileOrDir(root, name string, parentMode os.FileMode) ([]subscriptionDat
return nil, err
}
return []subscriptionData{{
name: name,
relPath: name,
data: bytes,
mode: s.Mode(),
dirMode: parentMode,
Expand Down
32 changes: 32 additions & 0 deletions pkg/subscriptions/subscriptions_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package subscriptions

import (
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
)

func TestReadAllAndSaveTo(t *testing.T) {
const testMode = os.FileMode(0o700)

rootDir := t.TempDir()
childDir := filepath.Join(rootDir, "child")
err := os.Mkdir(childDir, testMode)
assert.NoError(t, err, "mkdir child")

filePath := "child/file"
err = os.WriteFile(filepath.Join(rootDir, filePath), []byte("test"), testMode)
assert.NoError(t, err, "write file")

data, err := readAll(rootDir, "", testMode)
assert.NoError(t, err, "readAll")
assert.Len(t, data, 1, "readAll should return one result")

tmpDir := t.TempDir()
err = data[0].saveTo(tmpDir)
assert.NoError(t, err, "saveTo()")

assert.FileExists(t, filepath.Join(tmpDir, filePath), "file exists at correct location")
}

0 comments on commit e45d573

Please sign in to comment.