Reuse validated filesystem facts through acquisition
This commit is contained in:
@@ -178,9 +178,11 @@ func acquireInput(request inputAcquisitionRequest, policy acquisitionPolicy, sta
|
||||
input.document = versionProcessing.payload
|
||||
}
|
||||
if request.input.filesystem != nil && (selectedSource.kind == sourceNone || !activeOverlay(request.ordinaryOverlay)) {
|
||||
if diagnostic := validateFilesystemContext(selectedSource, request.input.filesystem, parsed); diagnostic != nil {
|
||||
resolved, diagnostic := validateFilesystemContext(selectedSource, request.input.filesystem, parsed, tier.hostToken)
|
||||
if diagnostic != nil {
|
||||
return input, []Diagnostic{*diagnostic}
|
||||
}
|
||||
input.resolvedFilesystem = &resolved
|
||||
}
|
||||
}
|
||||
if !request.input.imports && input.document != nil {
|
||||
|
||||
@@ -7,73 +7,33 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
func validateFilesystemContext(source Source, context *FilesystemContext, main parsedSource) *Diagnostic {
|
||||
if source.kind == sourceNone && context.FS == nil {
|
||||
return validateAbsentHostFilesystemContext(context)
|
||||
}
|
||||
if nativeContext(source, context) {
|
||||
resolved, diagnostic := resolveNativeFilesystemContext(source, context)
|
||||
if diagnostic != nil {
|
||||
return diagnostic
|
||||
}
|
||||
return validateResolvedFilesystemDirectories(resolved)
|
||||
}
|
||||
resolved, diagnostic := resolveHostFilesystemContext(source, context, main, nil)
|
||||
func validateFilesystemContext(source Source, context *FilesystemContext, main parsedSource, hostToken *namespaceToken) (resolvedFilesystemContext, *Diagnostic) {
|
||||
resolved, diagnostic := resolveFilesystemContext(source, context, main, hostToken)
|
||||
if diagnostic != nil {
|
||||
return diagnostic
|
||||
// Keep absent-input validation's existing diagnostic while sharing
|
||||
// root/base derivation with overlay and resource-context handling.
|
||||
if source.kind == sourceNone && context.FS == nil && diagnostic.Code == CodeImportOutsideRoot {
|
||||
diagnostic.Message = "import base must stay inside the configuration root"
|
||||
}
|
||||
return resolvedFilesystemContext{}, diagnostic
|
||||
}
|
||||
if diagnostic := validateResolvedFilesystemDirectories(resolved); diagnostic != nil {
|
||||
return diagnostic
|
||||
return resolvedFilesystemContext{}, diagnostic
|
||||
}
|
||||
if resolved.native != nil {
|
||||
return resolved, nil
|
||||
}
|
||||
root, err := os.OpenRoot(resolved.rootPath)
|
||||
if err != nil {
|
||||
value := globFilesystemDiagnostic(err, "configuration root could not be opened", Location{})
|
||||
value.Primary = nil
|
||||
return &value
|
||||
return resolvedFilesystemContext{}, &value
|
||||
}
|
||||
if err := root.Close(); err != nil {
|
||||
value := importDiagnostic(CodeImportRead, "configuration root could not be closed", nil)
|
||||
return &value
|
||||
return resolvedFilesystemContext{}, &value
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateAbsentHostFilesystemContext(context *FilesystemContext) *Diagnostic {
|
||||
rootPath, err := canonicalExistingPath(context.Root)
|
||||
if err != nil {
|
||||
value := importDiagnostic(CodeImportInvalid, "configuration root could not be canonicalized", nil)
|
||||
return &value
|
||||
}
|
||||
basePath := rootPath
|
||||
if context.BaseDir != "" {
|
||||
basePath, err = canonicalExistingPath(context.BaseDir)
|
||||
if err != nil {
|
||||
value := importDiagnostic(CodeImportInvalid, "import base directory could not be canonicalized", nil)
|
||||
return &value
|
||||
}
|
||||
}
|
||||
if !containedPath(rootPath, basePath) {
|
||||
value := importDiagnostic(CodeImportOutsideRoot, "import base must stay inside the configuration root", nil)
|
||||
return &value
|
||||
}
|
||||
for _, candidate := range []string{rootPath, basePath} {
|
||||
info, err := os.Stat(candidate)
|
||||
if err != nil || !info.IsDir() {
|
||||
value := importDiagnostic(CodeImportInvalid, "configuration root and import base must be directories", nil)
|
||||
return &value
|
||||
}
|
||||
}
|
||||
root, err := os.OpenRoot(rootPath)
|
||||
if err != nil {
|
||||
value := globFilesystemDiagnostic(err, "configuration root could not be opened", Location{})
|
||||
value.Primary = nil
|
||||
return &value
|
||||
}
|
||||
if err := root.Close(); err != nil {
|
||||
value := importDiagnostic(CodeImportRead, "configuration root could not be closed", nil)
|
||||
return &value
|
||||
}
|
||||
return nil
|
||||
return resolved, nil
|
||||
}
|
||||
|
||||
func filesystemOptionDiagnostics(source Source, context *FilesystemContext) []Diagnostic {
|
||||
|
||||
@@ -43,7 +43,6 @@ func resolveOrdinaryOverlayFilesystem(source Source, context *FilesystemContext,
|
||||
value := optionDiagnostic(CodeOptionsInvalid, "NoSource overlays require explicit filesystem context")
|
||||
return resolvedFilesystemContext{}, &value
|
||||
}
|
||||
return resolveAbsentFilesystemContext(context)
|
||||
}
|
||||
if existing != nil {
|
||||
return *existing, nil
|
||||
|
||||
Reference in New Issue
Block a user