Unify host and native filesystem acquisition
This commit is contained in:
@@ -22,6 +22,7 @@ type Source struct {
|
||||
kind sourceKind
|
||||
path string
|
||||
label string
|
||||
namespace *nativeNamespace
|
||||
data []byte
|
||||
reader *readerSource
|
||||
createErr sourceCreationError
|
||||
@@ -32,6 +33,7 @@ type sourceKind uint8
|
||||
const (
|
||||
sourceInvalid sourceKind = iota
|
||||
sourceFile
|
||||
sourceFilesystem
|
||||
sourceBytes
|
||||
sourceReader
|
||||
sourceNone
|
||||
@@ -60,6 +62,13 @@ func File(path string) Source {
|
||||
return Source{kind: sourceFile, path: path, label: path}
|
||||
}
|
||||
|
||||
func filesystemFile(namespace *nativeNamespace, name string) Source {
|
||||
if namespace == nil || namespace.filesystem == nil || name == "." || !fs.ValidPath(name) {
|
||||
return Source{kind: sourceFilesystem, createErr: creationInvalidPath}
|
||||
}
|
||||
return Source{kind: sourceFilesystem, path: name, label: namespace.label + "/" + name, namespace: namespace}
|
||||
}
|
||||
|
||||
// Bytes snapshots data immediately. Label must be nonempty and safe to display;
|
||||
// constructor problems are reported when the source is consumed.
|
||||
func Bytes(label string, data []byte) Source {
|
||||
@@ -143,6 +152,18 @@ func readSource(source Source, sourceID string, limits sourceLimits) (sourceRead
|
||||
return sourceRead{info: info}, []Diagnostic{sourceReadDiagnostic(failure, sourceID, CodeSourceLimitExceeded, "source exceeds the configured byte limit")}
|
||||
}
|
||||
return sourceRead{data: bytes.Clone(data), info: info}, nil
|
||||
case sourceFilesystem:
|
||||
access := newNativeAccess(source.namespace.filesystem, ".", source.namespace.token)
|
||||
info.Path = source.path
|
||||
canonical, err := access.canonical(source.path)
|
||||
if err != nil {
|
||||
return sourceRead{info: info}, []Diagnostic{classifySourceError(err, sourceID)}
|
||||
}
|
||||
data, failure := readRegularFile(canonical, access.stat, access.open, limit)
|
||||
if failure != nil {
|
||||
return sourceRead{info: info}, []Diagnostic{sourceReadDiagnostic(failure, sourceID, CodeSourceLimitExceeded, "source exceeds the configured byte limit")}
|
||||
}
|
||||
return sourceRead{data: bytes.Clone(data), info: info}, nil
|
||||
case sourceBytes:
|
||||
input = bytes.NewReader(source.data)
|
||||
case sourceReader:
|
||||
|
||||
Reference in New Issue
Block a user