Skip to content

Commit

Permalink
fix: better handle files owned by the fs (#834)
Browse files Browse the repository at this point in the history
* fix: better handle files owned by the fs

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>

* fix: only on dirs

* chore: comments

---------

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
  • Loading branch information
caarlos0 authored Jul 4, 2024
1 parent 41b3349 commit 2c264e3
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 2 deletions.
3 changes: 3 additions & 0 deletions files/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,9 @@ func addTree(
c.Destination = NormalizeAbsoluteDirPath(destination)
c.FileInfo.Mode = info.Mode() &^ umask
c.FileInfo.MTime = info.ModTime()
if ownedByFilesystem(c.Destination) {
c.Type = TypeImplicitDir
}
case d.Type()&os.ModeSymlink != 0:
linkDestination, err := os.Readlink(path)
if err != nil {
Expand Down
65 changes: 65 additions & 0 deletions files/files_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -974,3 +974,68 @@ func TestAsExplicitRelativePath(t *testing.T) {
assert.Equal(t, expected, files.AsExplicitRelativePath(input))
}
}

func TestIssue829(t *testing.T) {
var config testStruct
dec := yaml.NewDecoder(strings.NewReader(`---
contents:
# Implementation.
- src: ./testdata/issue829/lib/
dst: /usr/lib/
type: tree
- src: ./testdata/issue829/usr/
dst: /usr/
type: tree
- src: ./testdata/issue829/var/
dst: /var/
type: tree
# Configuration.
- src: ./testdata/issue829/etc/logrotate.d/acme
dst: /etc/logrotate.d/acme
type: config|noreplace
- src: ./testdata/issue829/etc/sysconfig/acme
dst: /etc/sysconfig/acme
type: config|noreplace
`))
dec.KnownFields(true)
require.NoError(t, dec.Decode(&config))
files, err := files.PrepareForPackager(
config.Contents,
0,
"",
false,
mtime,
)
require.NoError(t, err)

expect := map[string]string{
"/etc/": "implicit dir",
"/etc/logrotate.d/": "implicit dir",
"/etc/logrotate.d/acme": "config|noreplace",
"/etc/sysconfig/": "implicit dir",
"/etc/sysconfig/acme": "config|noreplace",
"/usr/lib/": "implicit dir",
"/usr/lib/systemd/": "implicit dir",
"/usr/lib/systemd/system/": "implicit dir",
"/usr/lib/systemd/system/acme.service": "file",
"/usr/": "implicit dir",
"/usr/bin/": "implicit dir",
"/usr/bin/acme": "file",
"/usr/share/": "implicit dir",
"/usr/share/doc/": "implicit dir",
"/usr/share/doc/acme/": "dir",
"/usr/share/doc/acme/readme.md": "file",
"/usr/share/man/": "implicit dir",
"/usr/share/man/man1/": "implicit dir",
"/usr/share/man/man1/acme.1.gz": "file",
"/var/": "implicit dir",
"/var/log/": "implicit dir",
"/var/log/acme/": "dir",
"/var/log/acme/.gitkeep": "file",
}

for _, file := range files {
require.Equal(t, expect[file.Destination], file.Type, "invalid type for %s", file.Destination)
}
}
15 changes: 13 additions & 2 deletions files/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@ import "path/filepath"

func ownedByFilesystem(path string) bool {
p := filepath.Clean(path)
for _, pp := range fsPaths {
for _, pp := range append(fsPaths, logrotatePaths...) {
if p == pp {
return true
}
}
return false
}

// from: repoquery --installed -l filesystem | while read -r f; do test -d $f && echo $f; done
// yum install yum-utils

// repoquery --installed -l filesystem | while read -r f; do test -d "\"$f\"," && echo $f; done
var fsPaths = []string{
"/afs",
"/bin",
Expand Down Expand Up @@ -213,3 +215,12 @@ var fsPaths = []string{
"/var/tmp",
"/var/yp",
}

// repoquery --installed -l logrotate | while read -r f; do test -d "\"$f\"," && echo $f; done
var logrotatePaths = []string{
"/etc/logrotate.d",
"/usr/lib/.build-id",
"/usr/lib/.build-id/ae",
"/usr/share/licenses/logrotate",
"/var/lib/logrotate",
}
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.

0 comments on commit 2c264e3

Please sign in to comment.