mirror of
https://github.com/anchore/syft.git
synced 2025-11-17 08:23:15 +01:00
convert posix path back to windows (#4285)
Signed-off-by: Joseph Shapiro <joeyashapiro@gmail.com>
This commit is contained in:
parent
fc74b07369
commit
538b4a2194
@ -322,7 +322,7 @@ func (r directoryIndexer) addDirectoryToIndex(p string, info os.FileInfo) error
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
metadata := file.NewMetadataFromPath(p, info)
|
metadata := NewMetadataFromPath(p, info)
|
||||||
r.index.Add(*ref, metadata)
|
r.index.Add(*ref, metadata)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
@ -334,7 +334,7 @@ func (r directoryIndexer) addFileToIndex(p string, info os.FileInfo) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
metadata := file.NewMetadataFromPath(p, info)
|
metadata := NewMetadataFromPath(p, info)
|
||||||
r.index.Add(*ref, metadata)
|
r.index.Add(*ref, metadata)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
@ -416,7 +416,7 @@ func (r directoryIndexer) addSymlinkToIndex(p string, info os.FileInfo) (string,
|
|||||||
targetAbsPath = filepath.Clean(filepath.Join(path.Dir(p), linkTarget))
|
targetAbsPath = filepath.Clean(filepath.Join(path.Dir(p), linkTarget))
|
||||||
}
|
}
|
||||||
|
|
||||||
metadata := file.NewMetadataFromPath(p, info)
|
metadata := NewMetadataFromPath(p, info)
|
||||||
metadata.LinkDestination = linkTarget
|
metadata.LinkDestination = linkTarget
|
||||||
r.index.Add(*ref, metadata)
|
r.index.Add(*ref, metadata)
|
||||||
|
|
||||||
|
|||||||
@ -173,7 +173,7 @@ func (r *fileIndexer) addDirectoryToIndex(path string, info os.FileInfo) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
metadata := file.NewMetadataFromPath(path, info)
|
metadata := NewMetadataFromPath(path, info)
|
||||||
r.index.Add(*ref, metadata)
|
r.index.Add(*ref, metadata)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
@ -185,7 +185,7 @@ func (r *fileIndexer) addFileToIndex(path string, info os.FileInfo) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
metadata := file.NewMetadataFromPath(path, info)
|
metadata := NewMetadataFromPath(path, info)
|
||||||
r.index.Add(*ref, metadata)
|
r.index.Add(*ref, metadata)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
20
syft/internal/fileresolver/get_xid.go
Normal file
20
syft/internal/fileresolver/get_xid.go
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
//go:build !windows
|
||||||
|
|
||||||
|
package fileresolver
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"syscall"
|
||||||
|
)
|
||||||
|
|
||||||
|
// getXid is the UID GID system info for unix
|
||||||
|
func getXid(info os.FileInfo) (uid, gid int) {
|
||||||
|
uid = -1
|
||||||
|
gid = -1
|
||||||
|
if stat, ok := info.Sys().(*syscall.Stat_t); ok {
|
||||||
|
uid = int(stat.Uid)
|
||||||
|
gid = int(stat.Gid)
|
||||||
|
}
|
||||||
|
|
||||||
|
return uid, gid
|
||||||
|
}
|
||||||
12
syft/internal/fileresolver/get_xid_win.go
Normal file
12
syft/internal/fileresolver/get_xid_win.go
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
//go:build windows
|
||||||
|
|
||||||
|
package fileresolver
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
// getXid is a placeholder for windows file information
|
||||||
|
func getXid(info os.FileInfo) (uid, gid int) {
|
||||||
|
return -1, -1
|
||||||
|
}
|
||||||
44
syft/internal/fileresolver/metadata.go
Normal file
44
syft/internal/fileresolver/metadata.go
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
package fileresolver
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/anchore/stereoscope/pkg/file"
|
||||||
|
"github.com/anchore/syft/internal"
|
||||||
|
"github.com/anchore/syft/syft/internal/windows"
|
||||||
|
)
|
||||||
|
|
||||||
|
func NewMetadataFromPath(path string, info os.FileInfo) file.Metadata {
|
||||||
|
var mimeType string
|
||||||
|
uid, gid := getXid(info)
|
||||||
|
|
||||||
|
ty := file.TypeFromMode(info.Mode())
|
||||||
|
|
||||||
|
if ty == file.TypeRegular {
|
||||||
|
usablePath := path
|
||||||
|
// denormalize the path back to windows so we can open the file
|
||||||
|
if windows.HostRunningOnWindows() {
|
||||||
|
usablePath = windows.FromPosix(usablePath)
|
||||||
|
}
|
||||||
|
|
||||||
|
f, err := os.Open(usablePath)
|
||||||
|
if err != nil {
|
||||||
|
// TODO: it may be that the file is inaccessible, however, this is not an error or a warning. In the future we need to track these as known-unknowns
|
||||||
|
f = nil
|
||||||
|
} else {
|
||||||
|
defer internal.CloseAndLogError(f, usablePath)
|
||||||
|
}
|
||||||
|
|
||||||
|
mimeType = file.MIMEType(f)
|
||||||
|
}
|
||||||
|
|
||||||
|
return file.Metadata{
|
||||||
|
FileInfo: info,
|
||||||
|
Path: path,
|
||||||
|
Type: ty,
|
||||||
|
// unsupported across platforms
|
||||||
|
UserID: uid,
|
||||||
|
GroupID: gid,
|
||||||
|
MIMEType: mimeType,
|
||||||
|
}
|
||||||
|
}
|
||||||
50
syft/internal/fileresolver/metadata_test.go
Normal file
50
syft/internal/fileresolver/metadata_test.go
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
package fileresolver
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/anchore/stereoscope/pkg/file"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestFileMetadataFromPath(t *testing.T) {
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
path string
|
||||||
|
expectedType file.Type
|
||||||
|
expectedMIMEType string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
path: "test-fixtures/symlinks-simple/readme",
|
||||||
|
expectedType: file.TypeRegular,
|
||||||
|
expectedMIMEType: "text/plain",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
path: "test-fixtures/symlinks-simple/link_to_new_readme",
|
||||||
|
expectedType: file.TypeSymLink,
|
||||||
|
expectedMIMEType: "",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
path: "test-fixtures/symlinks-simple/link_to_link_to_new_readme",
|
||||||
|
expectedType: file.TypeSymLink,
|
||||||
|
expectedMIMEType: "",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
path: "test-fixtures/symlinks-simple",
|
||||||
|
expectedType: file.TypeDirectory,
|
||||||
|
expectedMIMEType: "",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, test := range tests {
|
||||||
|
t.Run(test.path, func(t *testing.T) {
|
||||||
|
info, err := os.Lstat(test.path)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
actual := NewMetadataFromPath(test.path, info)
|
||||||
|
assert.Equal(t, test.expectedMIMEType, actual.MIMEType, "unexpected MIME type for %s", test.path)
|
||||||
|
assert.Equal(t, test.expectedType, actual.Type, "unexpected type for %s", test.path)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user