Skip to content

Commit

Permalink
Add single file omnibor ADG and ignore symlinks
Browse files Browse the repository at this point in the history
Signed-off-by: Frederick Kautz <fkautz@alumni.cmu.edu>
  • Loading branch information
fkautz committed Aug 8, 2023
1 parent 1f53af7 commit 8f1021a
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 71 deletions.
14 changes: 12 additions & 2 deletions directory_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@ func (plug *DirectoryPlugin) Sha256ADG(m map[string]string) {
}

func (plug *DirectoryPlugin) Add(path string) error {
// if this is a broken symlink, ignore
fileInfo, err := os.Lstat(path)
if err != nil {
return err
}
if fileInfo.Mode()&os.ModeSymlink != 0 {
// path is a symlink
if _, err := os.Stat(path); err != nil {
return nil
}
}

stat, err := os.Stat(path)
if err != nil {
return err
Expand Down Expand Up @@ -61,8 +73,6 @@ func (plug *DirectoryPlugin) Store(envelope *Envelope) error {
continue
}
}
//sha1tree := make(map[string]omnibor.ArtifactTree)
//sha256tree := make(map[string]omnibor.ArtifactTree)

for _, key := range keys {
if sha1tree != nil {
Expand Down
69 changes: 47 additions & 22 deletions file_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,56 +49,81 @@ func NewFilePlugin() Plugin {
}
}

func (plug *FilePlugin) Add(path string) error {
stat, err := os.Stat(path)
func (plug *FilePlugin) Add(filePath string) error {
// ignore broken symlink
localFileInfo, err := os.Lstat(filePath)
if err != nil {
return err
}
if stat.IsDir() {
if localFileInfo.Mode()&os.ModeSymlink != 0 {
if _, err = os.Stat(filePath); err != nil {
if err != nil {
return nil
}
}
}
fileInfo, err := os.Stat(filePath)
// if file is a symlink and the symlink points to a broken path, return nil
if err != nil {
return err
}

if fileInfo.IsDir() {
return nil
}
f, err := os.Open(path)

file, err := os.Open(filePath)
if err != nil {
return err
}

// explicitly ignore error from closing file
defer func(f *os.File) {
_ = f.Close()
}(f)
defer func(file *os.File) {
_ = file.Close()
}(file)

for _, hashAlgo := range plug.algorithms {

for _, algorithm := range plug.algorithms {
_, err := f.Seek(0, 0)
_, err := file.Seek(0, 0)
if err != nil {
return err
}

if strings.HasPrefix(algorithm, "gitoid:") {
var res *gitoid.GitOID
switch algorithm {
if strings.HasPrefix(hashAlgo, "gitoid:") {

var hashResult *gitoid.GitOID

switch hashAlgo {
case "gitoid:sha1":
res, err = gitoid.New(f, gitoid.WithContentLength(stat.Size()))
hashResult, err = gitoid.New(file, gitoid.WithContentLength(fileInfo.Size()))
case "gitoid:sha256":
res, err = gitoid.New(f, gitoid.WithContentLength(stat.Size()), gitoid.WithSha256())
hashResult, err = gitoid.New(file, gitoid.WithContentLength(fileInfo.Size()), gitoid.WithSha256())
}

if err != nil {
return err
}
plug.files[algorithm][path] = res.String()

plug.files[hashAlgo][filePath] = hashResult.String()

} else {
switch algorithm {

switch hashAlgo {
case "sha1":
hasher := sha1.New()
_, err = io.Copy(hasher, f)
res := hasher.Sum([]byte{})
plug.files[algorithm][path] = fmt.Sprintf("%x", res)
_, err = io.Copy(hasher, file)
hashBytes := hasher.Sum([]byte{})
plug.files[hashAlgo][filePath] = fmt.Sprintf("%x", hashBytes)

case "sha256":
hasher := sha256.New()
_, err = io.Copy(hasher, f)
res := hasher.Sum([]byte{})
plug.files[algorithm][path] = fmt.Sprintf("%x", res)
_, err = io.Copy(hasher, file)
hashBytes := hasher.Sum([]byte{})
plug.files[hashAlgo][filePath] = fmt.Sprintf("%x", hashBytes)
}

}

}

return nil
Expand Down
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ go 1.20
require (
github.com/edwarnicke/gitoid v0.0.0-20220710194850-1be5bfda1f9d
github.com/omnibor/omnibor-go v0.0.0-20230521145532-a77de61a16cd
github.com/stretchr/testify v1.8.2
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/kr/pretty v0.3.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.8.2 // indirect
github.com/rogpeppe/go-internal v1.8.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/edwarnicke/gitoid v0.0.0-20220710194850-1be5bfda1f9d h1:4l+Uq5zFWSagXgGFaKRRVWJrnlzeathyagWgYUltCgY=
github.com/edwarnicke/gitoid v0.0.0-20220710194850-1be5bfda1f9d/go.mod h1:WxWwA3EYuCQjlR5EBUX3uaTS8bh9BOa7BcqVREHQ0uQ=
github.com/omnibor/omnibor-go v0.0.0-20230226193847-c1d2b6d19159 h1:lM5TaZOWo76IG5zElw3ozfnWuwIqOt0/YW410IJGKaE=
github.com/omnibor/omnibor-go v0.0.0-20230226193847-c1d2b6d19159/go.mod h1:ArlQivzDQvZnFe8itjlA3ndPTXd9iWOgqzF31OyIEFQ=
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/omnibor/omnibor-go v0.0.0-20230521145532-a77de61a16cd h1:25EpGVgctk6V3DUa1gqFHvjVbmdWqM+jBZAed7p/krQ=
github.com/omnibor/omnibor-go v0.0.0-20230521145532-a77de61a16cd/go.mod h1:ArlQivzDQvZnFe8itjlA3ndPTXd9iWOgqzF31OyIEFQ=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
Expand All @@ -17,6 +18,7 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
45 changes: 45 additions & 0 deletions omnitrail.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package omnitrail

import (
"fmt"
"sort"
)

func NewTrail(option ...Option) Factory {
o := &Options{}
for _, opt := range option {
Expand All @@ -23,3 +28,43 @@ func NewTrail(option ...Option) Factory {
},
}
}

func FormatADGString(mapping Factory) string {
res := ""
sha1adgs := mapping.Sha1ADGs()
// create a list of all keys sorted in lexical order
keys := make([]string, 0, len(sha1adgs))
for k := range sha1adgs {
keys = append(keys, k)
}
// sort the keys
sort.Strings(keys)

for _, k := range keys {
v := sha1adgs[k]
if v != "" {
res += fmt.Sprintln(k)
res += fmt.Sprintln(v)
res += fmt.Sprintln("--")
}
}
res += fmt.Sprintln("----")

keys = make([]string, 0, len(sha1adgs))
sha2adgs := mapping.Sha256ADGs()
keys = make([]string, 0, len(sha2adgs))
for k := range sha2adgs {
keys = append(keys, k)
}
sort.Strings(keys)

for _, k := range keys {
v := sha2adgs[k]
if v != "" {
res += fmt.Sprintln(k)
res += fmt.Sprintln(v)
res += fmt.Sprintln("--")
}
}
return res
}
43 changes: 1 addition & 42 deletions omnitrail_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package omnitrail

import (
"encoding/json"
"fmt"
"github.com/stretchr/testify/assert"
"os"
"os/user"
Expand Down Expand Up @@ -71,7 +70,7 @@ func testAdd(t *testing.T, name string) {

assert.Equal(t, &expectedEnvelope, mapping.Envelope())

res := formatADGString(mapping)
res := FormatADGString(mapping)

expectedBytes, err = os.ReadFile("./test/" + name + ".adg")
assert.NoError(t, err)
Expand All @@ -93,43 +92,3 @@ func getShortestKey(expectedEnvelope *Envelope) string {
shortestKey := keys[0]
return shortestKey
}

func formatADGString(mapping Factory) string {
res := ""
sha1adgs := mapping.Sha1ADGs()
// create a list of all keys sorted in lexical order
keys := make([]string, 0, len(sha1adgs))
for k := range sha1adgs {
keys = append(keys, k)
}
// sort the keys
sort.Strings(keys)

for _, k := range keys {
v := sha1adgs[k]
if v != "" {
res += fmt.Sprintln(k)
res += fmt.Sprintln(v)
res += fmt.Sprintln("--")
}
}
res += fmt.Sprintln("----")

keys = make([]string, 0, len(sha1adgs))
sha2adgs := mapping.Sha256ADGs()
keys = make([]string, 0, len(sha2adgs))
for k := range sha2adgs {
keys = append(keys, k)
}
sort.Strings(keys)

for _, k := range keys {
v := sha2adgs[k]
if v != "" {
res += fmt.Sprintln(k)
res += fmt.Sprintln(v)
res += fmt.Sprintln("--")
}
}
return res
}
15 changes: 13 additions & 2 deletions posix_plugin_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@ type posixInfo struct {
}

func (p *PosixPlugin) Add(path string) error {
// check if symlink is broken
localFileInfo, err := os.Lstat(path)
if err != nil {
return err
}
if localFileInfo.Mode()&os.ModeSymlink != 0 {
if _, err = os.Stat(path); err != nil {
if err != nil {
return nil
}
}
}
stat, err := os.Stat(path)
if err != nil {
return err
Expand Down Expand Up @@ -48,8 +60,7 @@ func (p *PosixPlugin) Store(envelope *Envelope) error {
element.Posix.OwnerUID = strconv.Itoa(int(p.params[path].uid))
element.Posix.OwnerGID = strconv.Itoa(int(p.params[path].gid))
if p.params[path].size != 0 {
siezString := strconv.Itoa(int(p.params[path].size))
element.Posix.Size = siezString
element.Posix.Size = strconv.Itoa(int(p.params[path].size))
}
}
return nil
Expand Down

0 comments on commit 8f1021a

Please sign in to comment.