diff options
author | Jeff Gaston <jeffrygaston@google.com> | 2017-05-16 00:31:56 +0000 |
---|---|---|
committer | android-build-merger <android-build-merger@google.com> | 2017-05-16 00:31:56 +0000 |
commit | c4492e364e3c081615e4a7178e8a9f8764ef0c45 (patch) | |
tree | 0b1af5a7d3e89557d45d795c64dbf48d67db4584 /android | |
parent | 224d524b042488708f68bf5ae55214c9e72216f1 (diff) | |
parent | e626e0dbded76b962e5ded7d92a6f79657b3c322 (diff) | |
download | build_soong-c4492e364e3c081615e4a7178e8a9f8764ef0c45.tar.gz build_soong-c4492e364e3c081615e4a7178e8a9f8764ef0c45.tar.bz2 build_soong-c4492e364e3c081615e4a7178e8a9f8764ef0c45.zip |
Merge "Clarify paths.go somewhat" am: bdb02bd334 am: 0669b93b74
am: e626e0dbde
Change-Id: I2cbc0fbd797c50ff69df29d4e7b7482b2f8acd32
Diffstat (limited to 'android')
-rw-r--r-- | android/package_ctx.go | 6 | ||||
-rw-r--r-- | android/paths.go | 68 |
2 files changed, 37 insertions, 37 deletions
diff --git a/android/package_ctx.go b/android/package_ctx.go index d9bb1090..53044039 100644 --- a/android/package_ctx.go +++ b/android/package_ctx.go @@ -120,16 +120,16 @@ func (p AndroidPackageContext) IntermediatesPathVariable(name, path string) blue }) } -// PrefixedPathsForOptionalSourceVariable returns a Variable whose value is the +// PrefixedExistentPathsForSourcesVariable returns a Variable whose value is the // list of present source paths prefixed with the supplied prefix. It may only // be called during a Go package's initialization - either from the init() // function or as part of a package-scoped variable's initialization. -func (p AndroidPackageContext) PrefixedPathsForOptionalSourceVariable( +func (p AndroidPackageContext) PrefixedExistentPathsForSourcesVariable( name, prefix string, paths []string) blueprint.Variable { return p.VariableFunc(name, func(config interface{}) (string, error) { ctx := &configErrorWrapper{p, config.(Config), []error{}} - paths := PathsForOptionalSource(ctx, "", paths) + paths := ExistentPathsForSources(ctx, "", paths) if len(ctx.errors) > 0 { return "", ctx.errors[0] } diff --git a/android/paths.go b/android/paths.go index 0d26dc02..a23dd74e 100644 --- a/android/paths.go +++ b/android/paths.go @@ -97,6 +97,8 @@ type Path interface { type WritablePath interface { Path + // the writablePath method doesn't directly do anything, + // but it allows a struct to distinguish between whether or not it implements the WritablePath interface writablePath() } @@ -137,7 +139,7 @@ func ResPathWithName(ctx ModuleContext, p Path, name string) ModuleResPath { if path, ok := p.(resPathProvider); ok { return path.resPathWithName(ctx, name) } - reportPathError(ctx, "Tried to create object file from unsupported path: %s (%s)", reflect.TypeOf(p).Name(), p) + reportPathError(ctx, "Tried to create res file from unsupported path: %s (%s)", reflect.TypeOf(p).Name(), p) return PathForModuleRes(ctx) } @@ -188,7 +190,7 @@ func PathsForSource(ctx PathContext, paths []string) Paths { ret := make(Paths, 0, len(paths)) intermediates := filepath.Join(modCtx.ModuleDir(), modCtx.ModuleName(), modCtx.ModuleSubDir(), "missing") for _, path := range paths { - p := OptionalPathForSource(ctx, intermediates, path) + p := ExistentPathForSource(ctx, intermediates, path) if p.Valid() { ret = append(ret, p.Path()) } else { @@ -205,13 +207,13 @@ func PathsForSource(ctx PathContext, paths []string) Paths { return ret } -// PathsForOptionalSource returns a list of Paths rooted from SrcDir that are +// ExistentPathsForSources returns a list of Paths rooted from SrcDir that are // found in the tree. If any are not found, they are omitted from the list, // and dependencies are added so that we're re-run when they are added. -func PathsForOptionalSource(ctx PathContext, intermediates string, paths []string) Paths { +func ExistentPathsForSources(ctx PathContext, intermediates string, paths []string) Paths { ret := make(Paths, 0, len(paths)) for _, path := range paths { - p := OptionalPathForSource(ctx, intermediates, path) + p := ExistentPathForSource(ctx, intermediates, path) if p.Valid() { ret = append(ret, p.Path()) } @@ -337,12 +339,11 @@ func safePathForSource(ctx PathContext, path string) SourcePath { return ret } -// PathForSource returns a SourcePath for the provided paths... (which are -// joined together with filepath.Join). This also validates that the path -// doesn't escape the source dir, or is contained in the build dir. On error, it -// will return a usable, but invalid SourcePath, and report a ModuleError. -func PathForSource(ctx PathContext, paths ...string) SourcePath { - p := validatePath(ctx, paths...) +// PathForSource joins the provided path components and validates that the result +// neither escapes the source dir nor is in the out dir. +// On error, it will return a usable, but invalid SourcePath, and report a ModuleError. +func PathForSource(ctx PathContext, pathComponents ...string) SourcePath { + p := validatePath(ctx, pathComponents...) ret := SourcePath{basePath{p, pathConfig(ctx), ""}} abs, err := filepath.Abs(ret.String()) @@ -368,16 +369,16 @@ func PathForSource(ctx PathContext, paths ...string) SourcePath { return ret } -// OptionalPathForSource returns an OptionalPath with the SourcePath if the +// ExistentPathForSource returns an OptionalPath with the SourcePath if the // path exists, or an empty OptionalPath if it doesn't exist. Dependencies are added // so that the ninja file will be regenerated if the state of the path changes. -func OptionalPathForSource(ctx PathContext, intermediates string, paths ...string) OptionalPath { - if len(paths) == 0 { +func ExistentPathForSource(ctx PathContext, intermediates string, pathComponents ...string) OptionalPath { + if len(pathComponents) == 0 { // For when someone forgets the 'intermediates' argument panic("Missing path(s)") } - p := validatePath(ctx, paths...) + p := validatePath(ctx, pathComponents...) path := SourcePath{basePath{p, pathConfig(ctx), ""}} abs, err := filepath.Abs(path.String()) @@ -483,12 +484,11 @@ type OutputPath struct { var _ Path = OutputPath{} -// PathForOutput returns an OutputPath for the provided paths... (which are -// joined together with filepath.Join). This also validates that the path -// does not escape the build dir. On error, it will return a usable, but invalid -// OutputPath, and report a ModuleError. -func PathForOutput(ctx PathContext, paths ...string) OutputPath { - path := validatePath(ctx, paths...) +// PathForOutput joins the provided paths and returns an OutputPath that is +// validated to not escape the build dir. +// On error, it will return a usable, but invalid OutputPath, and report a ModuleError. +func PathForOutput(ctx PathContext, pathComponents ...string) OutputPath { + path := validatePath(ctx, pathComponents...) return OutputPath{basePath{path, pathConfig(ctx), ""}} } @@ -597,7 +597,7 @@ func PathForVndkRefAbiDump(ctx ModuleContext, version, fileName string, vndkOrNd } refDumpFileStr := "prebuilts/abi-dumps/" + vndkOrNdkDir + "/" + version + "/" + archName + "/" + sourceOrBinaryDir + "/" + fileName + ext - return OptionalPathForSource(ctx, "", refDumpFileStr) + return ExistentPathForSource(ctx, "", refDumpFileStr) } // PathForModuleOut returns a Path representing the paths... under the module's @@ -647,8 +647,8 @@ var _ Path = ModuleObjPath{} // PathForModuleObj returns a Path representing the paths... under the module's // 'obj' directory. -func PathForModuleObj(ctx ModuleContext, paths ...string) ModuleObjPath { - p := validatePath(ctx, paths...) +func PathForModuleObj(ctx ModuleContext, pathComponents ...string) ModuleObjPath { + p := validatePath(ctx, pathComponents...) return ModuleObjPath{PathForModuleOut(ctx, "obj", p)} } @@ -662,14 +662,14 @@ var _ Path = ModuleResPath{} // PathForModuleRes returns a Path representing the paths... under the module's // 'res' directory. -func PathForModuleRes(ctx ModuleContext, paths ...string) ModuleResPath { - p := validatePath(ctx, paths...) +func PathForModuleRes(ctx ModuleContext, pathComponents ...string) ModuleResPath { + p := validatePath(ctx, pathComponents...) return ModuleResPath{PathForModuleOut(ctx, "res", p)} } // PathForModuleInstall returns a Path representing the install path for the // module appended with paths... -func PathForModuleInstall(ctx ModuleContext, paths ...string) OutputPath { +func PathForModuleInstall(ctx ModuleContext, pathComponents ...string) OutputPath { var outPaths []string if ctx.Device() { partition := "system" @@ -689,14 +689,14 @@ func PathForModuleInstall(ctx ModuleContext, paths ...string) OutputPath { if ctx.Debug() { outPaths = append([]string{"debug"}, outPaths...) } - outPaths = append(outPaths, paths...) + outPaths = append(outPaths, pathComponents...) return PathForOutput(ctx, outPaths...) } // validateSafePath validates a path that we trust (may contain ninja variables). // Ensures that each path component does not attempt to leave its component. -func validateSafePath(ctx PathContext, paths ...string) string { - for _, path := range paths { +func validateSafePath(ctx PathContext, pathComponents ...string) string { + for _, path := range pathComponents { path := filepath.Clean(path) if path == ".." || strings.HasPrefix(path, "../") || strings.HasPrefix(path, "/") { reportPathError(ctx, "Path is outside directory: %s", path) @@ -706,20 +706,20 @@ func validateSafePath(ctx PathContext, paths ...string) string { // TODO: filepath.Join isn't necessarily correct with embedded ninja // variables. '..' may remove the entire ninja variable, even if it // will be expanded to multiple nested directories. - return filepath.Join(paths...) + return filepath.Join(pathComponents...) } // validatePath validates that a path does not include ninja variables, and that // each path component does not attempt to leave its component. Returns a joined // version of each path component. -func validatePath(ctx PathContext, paths ...string) string { - for _, path := range paths { +func validatePath(ctx PathContext, pathComponents ...string) string { + for _, path := range pathComponents { if strings.Contains(path, "$") { reportPathError(ctx, "Path contains invalid character($): %s", path) return "" } } - return validateSafePath(ctx, paths...) + return validateSafePath(ctx, pathComponents...) } type testPath struct { |