Skip to content

Commit

Permalink
fix: allow using directories as contents in archlinux packager (#568)
Browse files Browse the repository at this point in the history
* fix: Recursively add files if content is directory

* fix: Skip contents with wrong packager value

* test: Add acceptance tests for directories
  • Loading branch information
Elara6331 committed Oct 27, 2022
1 parent f649caf commit b4bd781
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 0 deletions.
50 changes: 50 additions & 0 deletions arch/arch.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"errors"
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"strconv"
Expand Down Expand Up @@ -159,11 +160,60 @@ func createFilesInTar(info *nfpm.Info, tw *tar.Writer) ([]MtreeEntry, int64, err
var entries []MtreeEntry
var totalSize int64

var contents []*files.Content

for _, content := range info.Contents {
if content.Packager != "" && content.Packager != packagerName {
continue
}

switch content.Type {
case "dir", "symlink":
contents = append(contents, content)
continue
}

fi, err := os.Stat(content.Source)
if err != nil {
return nil, 0, err
}

if fi.IsDir() {
err = filepath.WalkDir(content.Source, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}

relPath := strings.TrimPrefix(path, content.Source)

if d.IsDir() {
return nil
}

c := &files.Content{
Source: path,
Destination: filepath.Join(content.Destination, relPath),
FileInfo: &files.ContentFileInfo{
Mode: d.Type(),
},
}

if d.Type()&os.ModeSymlink != 0 {
c.Type = "symlink"
}

contents = append(contents, c)
return nil
})
if err != nil {
return nil, 0, err
}
} else {
contents = append(contents, content)
}
}

for _, content := range contents {
path := normalizePath(content.Destination)

switch content.Type {
Expand Down
4 changes: 4 additions & 0 deletions arch/arch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ func exampleInfo() *nfpm.Info {
Destination: "/etc/fake/fake-link.conf",
Type: "symlink",
},
{
Source: "../testdata/something",
Destination: "/etc/something",
},
},
Scripts: nfpm.Scripts{
PreInstall: "../testdata/scripts/preinstall.sh",
Expand Down
5 changes: 5 additions & 0 deletions testdata/acceptance/apk.dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ RUN test -f /usr/share/whatever/folder/folder2/file2
RUN test -d /var/log/whatever
RUN test -d /usr/share/foo
RUN test -d /usr/foo/bar/something
RUN test -d /etc/something
RUN test -f /etc/something/a
RUN test -f /etc/something/b
RUN test -d /etc/something/c
RUN test -f /etc/something/c/d
RUN test $(stat -c %a /usr/sbin/fake) -eq 4755
RUN test -f /tmp/preinstall-proof
RUN test -f /tmp/postinstall-proof
Expand Down
5 changes: 5 additions & 0 deletions testdata/acceptance/archlinux.dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ RUN test -f /usr/share/whatever/folder/folder2/file2
RUN test -d /var/log/whatever
RUN test -d /usr/share/foo
RUN test -d /usr/foo/bar/something
RUN test -d /etc/something
RUN test -f /etc/something/a
RUN test -f /etc/something/b
RUN test -d /etc/something/c
RUN test -f /etc/something/c/d
RUN test $(stat -c %a /usr/bin/fake) -eq 4755
RUN test -f /tmp/preinstall-proof
RUN test -f /tmp/postinstall-proof
Expand Down
2 changes: 2 additions & 0 deletions testdata/acceptance/core.complex.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ contents:
- src: ./testdata/whatever.conf
dst: /etc/foo/whatever.conf
type: config
- src: ./testdata/something
dst: /etc/something
- src: ./testdata/fake
dst: /usr/sbin/fake
packager: deb
Expand Down
5 changes: 5 additions & 0 deletions testdata/acceptance/deb.dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ RUN test -f /usr/share/whatever/folder/folder2/file2
RUN test -d /var/log/whatever
RUN test -d /usr/share/foo
RUN test -d /usr/foo/bar/something
RUN test -d /etc/something
RUN test -f /etc/something/a
RUN test -f /etc/something/b
RUN test -d /etc/something/c
RUN test -f /etc/something/c/d
RUN test $(stat -c %a /usr/sbin/fake) -eq 4755
RUN test -f /tmp/preinstall-proof
RUN test -f /tmp/postinstall-proof
Expand Down
5 changes: 5 additions & 0 deletions testdata/acceptance/rpm.dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ RUN test -f /usr/share/whatever/folder/folder2/file2
RUN test -d /var/log/whatever
RUN test -d /usr/share/foo
RUN test -d /usr/foo/bar/something
RUN test -d /etc/something
RUN test -f /etc/something/a
RUN test -f /etc/something/b
RUN test -d /etc/something/c
RUN test -f /etc/something/c/d
RUN test $(stat -c %a /usr/sbin/fake) -eq 4755
RUN test -f /tmp/preinstall-proof
RUN test -f /tmp/postinstall-proof
Expand Down
1 change: 1 addition & 0 deletions testdata/something/a
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a
1 change: 1 addition & 0 deletions testdata/something/b
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
b
1 change: 1 addition & 0 deletions testdata/something/c/d
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
d

0 comments on commit b4bd781

Please sign in to comment.