forked from knqyf263/go-rpmdb
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add username, groupname, and fileflag support
Signed-off-by: Alex Goodman <alex.goodman@anchore.com>
- Loading branch information
Showing
5 changed files
with
252 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package rpmdb | ||
|
||
// source: https://github.com/rpm-software-management/rpm/blob/da55723907418bfb3939cd6ddd941b3fdb4f6887/lib/rpmfiles.h#L46-L63 | ||
const ( | ||
RPMFILE_CONFIG int32 = 1 << iota /*!< from %%config */ | ||
RPMFILE_DOC /*!< from %%doc */ | ||
RPMFILE_ICON /*!< from %%donotuse. */ | ||
RPMFILE_MISSINGOK /*!< from %%config(missingok) */ | ||
RPMFILE_NOREPLACE /*!< from %%config(noreplace) */ | ||
RPMFILE_SPECFILE /*!< @todo (unnecessary) marks 1st file in srpm. */ | ||
RPMFILE_GHOST /*!< from %%ghost */ | ||
RPMFILE_LICENSE /*!< from %%license */ | ||
RPMFILE_README /*!< from %%readme */ | ||
_ /* bit 9 unused */ | ||
_ /* bit 10 unused */ | ||
RPMFILE_PUBKEY /*!< from %%pubkey */ | ||
RPMFILE_ARTIFACT /*!< from %%artifact */ | ||
) | ||
|
||
var flagChar = map[int32]string{ | ||
RPMFILE_CONFIG: "c", | ||
RPMFILE_DOC: "d", | ||
RPMFILE_SPECFILE: "s", | ||
RPMFILE_MISSINGOK: "m", | ||
RPMFILE_NOREPLACE: "n", | ||
RPMFILE_GHOST: "g", | ||
RPMFILE_LICENSE: "l", | ||
RPMFILE_README: "r", | ||
RPMFILE_ARTIFACT: "a", | ||
} | ||
|
||
var flagFormatOrder = []int32{ | ||
RPMFILE_DOC, | ||
RPMFILE_CONFIG, | ||
RPMFILE_SPECFILE, | ||
RPMFILE_MISSINGOK, | ||
RPMFILE_NOREPLACE, | ||
RPMFILE_GHOST, | ||
RPMFILE_LICENSE, | ||
RPMFILE_README, | ||
RPMFILE_ARTIFACT, | ||
} | ||
|
||
type FileFlags int32 | ||
|
||
// source: https://github.com/rpm-software-management/rpm/blob/551e66fc94668e62910008d047428eb5ec62f896/lib/verify.c#L298-L312 | ||
func (flags FileFlags) String() (result string) { | ||
for _, flag := range flagFormatOrder { | ||
if int32(flags)&flag == 0 { | ||
// flag is NOT set | ||
continue | ||
} | ||
if char, exists := flagChar[flag]; exists { | ||
result += char | ||
} | ||
} | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package rpmdb | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestFormatFileFlags(t *testing.T) { | ||
tests := []struct { | ||
flags FileFlags | ||
expected string | ||
}{ | ||
// empty | ||
{ | ||
flags: 0, | ||
expected: "", | ||
}, | ||
// check that the formatting works relative to the configured bits | ||
{ | ||
flags: FileFlags(RPMFILE_CONFIG), | ||
expected: "c", | ||
}, | ||
{ | ||
flags: FileFlags(RPMFILE_DOC), | ||
expected: "d", | ||
}, | ||
{ | ||
flags: FileFlags(RPMFILE_MISSINGOK), | ||
expected: "m", | ||
}, | ||
{ | ||
flags: FileFlags(RPMFILE_NOREPLACE), | ||
expected: "n", | ||
}, | ||
{ | ||
flags: FileFlags(RPMFILE_SPECFILE), | ||
expected: "s", | ||
}, | ||
{ | ||
flags: FileFlags(RPMFILE_GHOST), | ||
expected: "g", | ||
}, | ||
{ | ||
flags: FileFlags(RPMFILE_LICENSE), | ||
expected: "l", | ||
}, | ||
{ | ||
flags: FileFlags(RPMFILE_README), | ||
expected: "r", | ||
}, | ||
{ | ||
flags: FileFlags(RPMFILE_ARTIFACT), | ||
expected: "a", | ||
}, | ||
{ | ||
flags: FileFlags(RPMFILE_CONFIG | RPMFILE_DOC | RPMFILE_SPECFILE | RPMFILE_MISSINGOK | RPMFILE_NOREPLACE | RPMFILE_GHOST | RPMFILE_LICENSE | RPMFILE_README | RPMFILE_ARTIFACT), | ||
expected: "dcsmnglra", | ||
}, | ||
{ | ||
flags: FileFlags(RPMFILE_DOC | RPMFILE_ARTIFACT), | ||
expected: "da", | ||
}, | ||
// check that the formatting matches relative to verified correct values | ||
// see helpful examples from: rpm --dbpath=/var/lib/rpm -qa --queryformat '%{FILEFLAGS:fflags}|%{FILEFLAGS}\n' | ||
{ | ||
flags: FileFlags(89), | ||
expected: "cmng", | ||
}, | ||
{ | ||
flags: FileFlags(16), | ||
expected: "n", | ||
}, | ||
{ | ||
flags: FileFlags(64), | ||
expected: "g", | ||
}, | ||
{ | ||
flags: FileFlags(17), | ||
expected: "cn", | ||
}, | ||
{ | ||
flags: FileFlags(4096), | ||
expected: "a", | ||
}, | ||
} | ||
for _, test := range tests { | ||
t.Run(test.expected, func(t *testing.T) { | ||
assert.Equal(t, test.expected, test.flags.String()) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters