Skip to content

Commit 2c264e3

Browse files
authored
fix: better handle files owned by the fs (#834)
* 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>
1 parent 41b3349 commit 2c264e3

File tree

9 files changed

+81
-2
lines changed

9 files changed

+81
-2
lines changed

files/files.go

+3
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,9 @@ func addTree(
490490
c.Destination = NormalizeAbsoluteDirPath(destination)
491491
c.FileInfo.Mode = info.Mode() &^ umask
492492
c.FileInfo.MTime = info.ModTime()
493+
if ownedByFilesystem(c.Destination) {
494+
c.Type = TypeImplicitDir
495+
}
493496
case d.Type()&os.ModeSymlink != 0:
494497
linkDestination, err := os.Readlink(path)
495498
if err != nil {

files/files_test.go

+65
Original file line numberDiff line numberDiff line change
@@ -974,3 +974,68 @@ func TestAsExplicitRelativePath(t *testing.T) {
974974
assert.Equal(t, expected, files.AsExplicitRelativePath(input))
975975
}
976976
}
977+
978+
func TestIssue829(t *testing.T) {
979+
var config testStruct
980+
dec := yaml.NewDecoder(strings.NewReader(`---
981+
contents:
982+
# Implementation.
983+
- src: ./testdata/issue829/lib/
984+
dst: /usr/lib/
985+
type: tree
986+
- src: ./testdata/issue829/usr/
987+
dst: /usr/
988+
type: tree
989+
- src: ./testdata/issue829/var/
990+
dst: /var/
991+
type: tree
992+
993+
# Configuration.
994+
- src: ./testdata/issue829/etc/logrotate.d/acme
995+
dst: /etc/logrotate.d/acme
996+
type: config|noreplace
997+
- src: ./testdata/issue829/etc/sysconfig/acme
998+
dst: /etc/sysconfig/acme
999+
type: config|noreplace
1000+
`))
1001+
dec.KnownFields(true)
1002+
require.NoError(t, dec.Decode(&config))
1003+
files, err := files.PrepareForPackager(
1004+
config.Contents,
1005+
0,
1006+
"",
1007+
false,
1008+
mtime,
1009+
)
1010+
require.NoError(t, err)
1011+
1012+
expect := map[string]string{
1013+
"/etc/": "implicit dir",
1014+
"/etc/logrotate.d/": "implicit dir",
1015+
"/etc/logrotate.d/acme": "config|noreplace",
1016+
"/etc/sysconfig/": "implicit dir",
1017+
"/etc/sysconfig/acme": "config|noreplace",
1018+
"/usr/lib/": "implicit dir",
1019+
"/usr/lib/systemd/": "implicit dir",
1020+
"/usr/lib/systemd/system/": "implicit dir",
1021+
"/usr/lib/systemd/system/acme.service": "file",
1022+
"/usr/": "implicit dir",
1023+
"/usr/bin/": "implicit dir",
1024+
"/usr/bin/acme": "file",
1025+
"/usr/share/": "implicit dir",
1026+
"/usr/share/doc/": "implicit dir",
1027+
"/usr/share/doc/acme/": "dir",
1028+
"/usr/share/doc/acme/readme.md": "file",
1029+
"/usr/share/man/": "implicit dir",
1030+
"/usr/share/man/man1/": "implicit dir",
1031+
"/usr/share/man/man1/acme.1.gz": "file",
1032+
"/var/": "implicit dir",
1033+
"/var/log/": "implicit dir",
1034+
"/var/log/acme/": "dir",
1035+
"/var/log/acme/.gitkeep": "file",
1036+
}
1037+
1038+
for _, file := range files {
1039+
require.Equal(t, expect[file.Destination], file.Type, "invalid type for %s", file.Destination)
1040+
}
1041+
}

files/fs.go

+13-2
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,17 @@ import "path/filepath"
44

55
func ownedByFilesystem(path string) bool {
66
p := filepath.Clean(path)
7-
for _, pp := range fsPaths {
7+
for _, pp := range append(fsPaths, logrotatePaths...) {
88
if p == pp {
99
return true
1010
}
1111
}
1212
return false
1313
}
1414

15-
// from: repoquery --installed -l filesystem | while read -r f; do test -d $f && echo $f; done
15+
// yum install yum-utils
16+
17+
// repoquery --installed -l filesystem | while read -r f; do test -d "\"$f\"," && echo $f; done
1618
var fsPaths = []string{
1719
"/afs",
1820
"/bin",
@@ -213,3 +215,12 @@ var fsPaths = []string{
213215
"/var/tmp",
214216
"/var/yp",
215217
}
218+
219+
// repoquery --installed -l logrotate | while read -r f; do test -d "\"$f\"," && echo $f; done
220+
var logrotatePaths = []string{
221+
"/etc/logrotate.d",
222+
"/usr/lib/.build-id",
223+
"/usr/lib/.build-id/ae",
224+
"/usr/share/licenses/logrotate",
225+
"/var/lib/logrotate",
226+
}

files/testdata/issue829/etc/logrotate.d/acme

Whitespace-only changes.

files/testdata/issue829/etc/sysconfig/acme

Whitespace-only changes.

files/testdata/issue829/lib/systemd/system/acme.service

Whitespace-only changes.

files/testdata/issue829/usr/share/doc/acme/readme.md

Whitespace-only changes.

files/testdata/issue829/usr/share/man/man1/acme.1.gz

Whitespace-only changes.

files/testdata/issue829/var/log/acme/.gitkeep

Whitespace-only changes.

0 commit comments

Comments
 (0)