Commit graph

37 commits

Author SHA1 Message Date
Colin Cross
2f95ec7031 Fix reading absolute paths through OsFs
OsFs may be asked to read absolute paths if buildDir is absolute.
Check if the path is absolute before prepending srcDir to it.

Bug: 146437378
Test: fs_test.go
Change-Id: I2a67593e9d836ca3e11dc10b81f49a4fb49d2cdf
2020-01-10 13:52:22 -08:00
Colin Cross
c5fa50e057 Allow primary builder to change working directory
Bug: 146437378
Test: pathtools/fs_test.go
Change-Id: I513ceb9b8b0b4f18223bc34ecad9846fe220709b
2020-01-08 15:54:58 -08:00
Colin Cross
a26ae89903 Simplify pathtools.Match
filepath.Match has always supported hierarchical patterns (a/*),
so pathtools.Match can be significantly simplified by reusing
filepath.Match directly instead of recursively stepping through
the path elements using filepath.Match on each one.

Test: glob_test.go
Change-Id: I8af59ee880f0402609b994922bafb1961fcabcf3
2019-05-20 14:57:36 -07:00
Colin Cross
a4cc6837db Add more pathtools.Match test cases
Add some more test cases for pathtools.Match before making
signficant changes to it.

Test: glob_test.go
Change-Id: I2eeb5ebf03fb645a2053852a1a9f4e368d22084a
2019-05-20 14:57:31 -07:00
Colin Cross
3316a5ee54 Add pathtools.Filesystem.Stat for soong_zip
Stat is used by soong_zip.  Add it to the FileSystem interface
and add tests for it.

Test: fs_test.go
Change-Id: I1baa2b27398846a4e55bcf4fa291c62f507a4e9d
2018-09-27 15:54:24 -07:00
Colin Cross
15fbefb87e Add tests for pathtools.FileSystem.Lstat
Test: fs_test.go
Change-Id: I202d639d8db3f0cf11d927cbf9ca5aac073ccc43
2018-09-27 15:54:24 -07:00
Colin Cross
e81b432f09 Add pathtools.FileSystem.Readlink
Readlink is used by soong_zip.  Add it to the FileSystem interface
and add tests for it.

Test: fs_test.go
Change-Id: Ie8ca5cd7cae98a47980a50d2891501fe79fd47a5
2018-09-26 16:54:15 -07:00
Colin Cross
d85b3c78c7 Replace exact errors in tests with errno checking
Only check that functions that return an *os.SyscallError have
the correct errno, not that they have identical test text.

Test: fs_test.go
Change-Id: Iba050cb0474eaf2643858bcca4e52ba770702c2f
2018-09-26 16:54:15 -07:00
Colin Cross
192dbc59c1 Make pathtools.FileSystem.Open return a ReaderAtSeekerCloser
Both *os.File and *bytes.Buffer support all of io.Reader,
io.ReaderAt, io.Seeker and io.Closer.  Return a combo interface
so that soong_zip can use the result in an io.SectionReader.

Test: m checkbuild
Change-Id: I31c3ce35e28c52bae20b536b5905de2f8a3d1478
2018-09-26 16:54:15 -07:00
Colin Cross
daf9de1999 Fix typo in TestMockFs_followSymlinks
c/f was tested twice and d/f was not tested.

Test: fs_test.go
Change-Id: I0fd6ba8ef37a3407f9be98608efed387cca5d7ff
2018-09-26 16:54:15 -07:00
Colin Cross
e98d0828c8 Add ShouldFollowSymlinks argument to pathtools.Glob
Allow the caller to specify whether symlinks should be followed
(the old behavior) or not.

Test: glob_test.go
Test: fs_test.go
Change-Id: I550dc91b8e6370fb32a9a1cbdcb2edade48bda46
2018-09-24 15:09:32 -07:00
Colin Cross
9e1ff7423b Fix recursive globs through symlinks
filepath.Walk does not walk symlinks to directories, which leads to
inconsitent behavior with following symlinks.  Replace the use of
filepath.Walk in ListDirsRecursive with a helper function that lists
each directory and walks anything for which IsDir reports true, which
includes following symlinks, and then reconstructs the path through
the symlink.

Test: fs_test.go
Test: glob_test.go
Change-Id: Ie4dd0820e9c7c0a124caa65210ce20439a44da16
2018-09-24 15:09:32 -07:00
Colin Cross
c64f26418e Add symlink support to mockFs
Add support for specifying symlinks in mock filesystems to prepare
for glob symlink tests.

This patch leaves incorrect behavior by not walking symlinks in
mockFs.ListDirsRecursive, but it matches what osFs does.

Test: fs_test.go
Change-Id: If87a83c00f21e14696faf890b7b09e88b18e95b9
2018-09-24 15:09:32 -07:00
Colin Cross
d9bea8f909 Add OsFs tests for escaped globs
Test: glob_test.go
Change-Id: I18d5f0c1139c68eafe4993792e8640d2144d49e1
2018-09-21 15:55:06 -07:00
Colin Cross
e3b7ec32c9 Improve error message for globbed dangling symlink
IsDir on a dangling symlink produces ErrNotExist.  Manually
check if the file exists but is a symlink to report a better
error.

Test: m checkbuild
Change-Id: I3181e74002436d74ec35a0923635835e561030dd
2018-09-21 15:55:03 -07:00
Colin Cross
a470612f97 Fix globs matching files with wildcard characters in the name
Globs that match a file that looks like a glob were causing duplicate
results because the prefix match would then re-match the
filename as a wildcard.  Add escaping to prevent re-matching.

Also add tests for globs on files with wildcards, tests for
? and [a-z] glob matches supported by filepath.Match, and tests
for escaped wildcard characters.

Test: glob_test.go
Change-Id: Id11a754863979bb36cca0dbd18ea2e76dd1470e3
2018-09-19 15:44:40 -07:00
Colin Cross
c0c3b0f946 Support trailing slash in pathtools.Match patterns
pathtools.Match was trimming the trailing slash from patterns.  Make
it handle a trailing slash by first confirming that both the pattern
and name either both have or both do not have a trailing slash, and
then trimming it from both.

Bug: 111389216
Test: TestMatch in glob_test.go
Change-Id: I743e00c14d885de5b5a060aa9e2b22c81dc7e09d
2018-07-13 21:20:03 -07:00
Dan Willemsen
b6c90239d6 Append / to directories in Glob results
This makes it easy for users of Glob to detect whether the match is a
file or directory. Doing the check at this level means that the filelist
file used as a dependency will be updated if a directory is replaced
with a file of the same name, or vice versa.

Change-Id: I79ebba39327218bcdcf50b393498306119de9d6c
2018-02-23 17:21:37 -08:00
Colin Cross
f9c2e8cc43 Export pathtools.Match
Export pathtools.Match so that tools that operate on file lists
(like zip listings) can use the same glob format.

Test: m checkbuild
Change-Id: Ic4b90139c381c57551836590da3ea54826b0227a
2017-11-22 12:51:59 -08:00
Colin Cross
a4fae90d1e Fix recursive glob on MockFs
Recursive globs on MockFs were ending up in an unmocked os.Lstat
call in walkAllDirs.  Move walkAllDirs into the FileSystem interface
as ListDirsRecursive.

Also duplicate the glob tests on both the real filesystem and on
MockFs.

Test: glob_test.go
Change-Id: Ia6b6b5eecdd17955a49d684a0fd5e55df05cfe62
2017-10-05 16:06:15 -07:00
Jeff Gaston
aca4220583 Clearer error in case of Android.bp being unreadable
Bug: 64600838
Test: mkdir errtest \
      && ln -s /tmp/dontexist errtest/Android.bp \
      # and add errtest to ./Android.bp \
      && m nothing \
      # and check that the error message mentions a symlink

Change-Id: I841ec12d613f61ccc3396538062bee48c8c1ca27
2017-09-01 17:29:30 -07:00
Colin Cross
616c278b70 Add . and / to MockFs
Add the final directory to MockFs so that Exists() on . and /
returns true.

Test: java_test.go
Change-Id: I1320f4f267ee6b6a7dbf1132630df306602e84d8
2017-07-14 08:16:00 -07:00
Dan Willemsen
75fec88fc6 Fix glob deps for non-wild patterns
Patterns that were not wild would return an empty "dirs" list if the
file was found. But then if they were removed, we wouldn't know to
update the glob file and re-run the primary builder.

In this case, instead of adding the final directory into the dirs list,
add the matched files themselves. Due to editors performing atomic
writes (the directory timestamp often gets updated at the same time as
file timestamp) this is probably more efficient. In either case, we're
only re-running the individual glob, which is rather cheap.

Rename startGlob/Glob return name from "dirs" to "deps" since it may
contain files now too.
2017-06-22 17:53:39 -07:00
Colin Cross
b519a7e1b6 Add globbing to filesystem mocking
Add globbing to filesystem mocking so that more code can be tested
against the mock.  Also moves the filesystem mock to pathtools,
and renames pathtools.GlobWithExcludes to pathtools.Glob, replacing
the existing pathtools.Glob.

Test: blueprint tests
Change-Id: I722df8121bc870c4a861d7c249245c57dcb607be
2017-02-02 16:48:06 -08:00
Dan Willemsen
53f4950eea Prevent glob from accessing hidden files
Hidden files are often source control related (".git", ".repo", etc) or
editor related (vim's .*.swp), so are not guaranteed to exist, and may
be temporary. The build system shouldn't be using these files by
default.

If the glob pattern explicitly uses "." at the beginning of a path
component, allow returning hidden files for that component. Because of
this behavior, non-wildcard globs remain unchanged.

The one behavior that cannot be handled anymore is including hidden
files in recursive globs.

Change-Id: I583c506e9a18ed2ff7ca011a791165d9582de90f
2016-12-19 15:17:26 -08:00
Colin Cross
08e4954a8c Fix optional_subdirs globbing
Bypassing c.glob() and using filepath.Glob() directly for non-glob
paths does not add dependencies on directories that contain missing
files.  For optional_subdirs, this means no dependency is added to
rerun the primary builder when an Android.bp file is added to an
optional_subdirs directory.  Always use c.glob(), for the non-optional
case it will not insert any dependencies if the file exists (as tested
by glob_test.go's no-wild tests), and if the file doesn't exist the
len(matches) == 0 will error out.

Change-Id: I370479c6e89f5ff590897702e256256a4dca6952
2016-11-14 15:41:41 -08:00
Colin Cross
ced59eeaf1 Add better error message when stat returns nil
Sometimes os.Stat on a path seems to return nil after filepath.Glob
returned the path as valid.  Return the error message to see why.

Bug: 32676828
Test: builds
Change-Id: Ieedddb673b4d641e5de08778febeb3d8ea025c0d
2016-11-05 14:52:57 -07:00
Colin Cross
127d2eae8b Import globbing from Soong
Add globbing with dependency checking to blueprint.  Calling
ModuleContext.GlobWithDeps or SingletonContext.GlobWithDeps will return
a list of files that match the globs, while also adding efficient
dependencies to rerun the primary builder if a file that matches the
glob is added or removed.

Also use the globbing support for optional_subdirs=, subdirs= and build=
lines in blueprints files.  The globbing slightly changes the behavior
of subname= lines, it no longer falls back to looking for a file called
"Blueprints".  Blueprint files that need to include a subdirectory with
a different name can use build= instead of subdir= to directly include
them.  The Blueprints file is updated to reset subname="Blueprints" in
case we want to include subdirectories inside blueprint and the primary
builder has changed the subname.

Also adds a new test directory that contains a simple primary builder
tree to test regeneration for globbing, and runs the tests in travis.

Change-Id: I83ce525fd11e11579cc58ba5308d01ca8eea7bc6
2016-11-03 13:54:03 -07:00
Colin Cross
7d2ee0df95 Fix glob dependencies when initial non-wild path does not exist
If the initial non-wild part of a glob path does not exist, return
the last existing part of the path as a dependency to detect if the
path is created later.

Change-Id: Ib5a39e6830cb386deed26e017279d0aac1bc9a20
2015-06-18 10:49:36 -07:00
Colin Cross
5d6d4c7efb Add GlobWithExcludes to pathtools
Add GlobWithExcludes, which takes a pattern and a list of exclude
patterns, and returns all files that match the pattern but do not
match any exclude patterns.

Change-Id: I8b94b3ba5a37409071b475b9a4035f52f47863f1
2015-04-29 22:31:44 -07:00
Colin Cross
7bac3c6cf2 Add recursive glob support to pathtools.Glob
Recursive globs are supported by passing ** in any single non-final
path element.  For example, path/**/*.java will find all files named
*.java under "path".

Change-Id: Ifebd76f8959289f7d0d378504053c1c6b88cdeed
2015-04-29 22:31:40 -07:00
Colin Cross
4b793e5798 Rewrite pathtools.Glob to track partially-matched directories
The directory structure:
  a/
    a
  b/
    b
With the glob pattern */a would previously return []string{"a"} for
dirs, but it needs to return []string{"a", "b"} in order to re-run
the generator if a file called "a" is created inside b/.

Rewrite Glob to manually recurse through path elements, only calling
filepath.Glob for a pattern with wilds in the last element of the
path, and add the globbed directory to the dirs list each time.

Also add tests and test data for pathtools.Glob.

Change-Id: Ibbdb2f99809ea0826d4fa82066cf84103005ef57
2015-04-24 11:11:15 -07:00
Colin Cross
63d5d4d9e4 Fix formatting
gofmt -w .

Change-Id: If9cf0b7bd810f899edffcd2edf361fa83245bd2a
2015-04-20 16:41:55 -07:00
Colin Cross
7bf6f62130 Replace ReplaceExtension regexp with manual string operations
regexp is overkill for a simple extension replacement, just find
the last '.' and add the new extension after it.  Saves 5% cpu time
on one workload.

Change-Id: Ic299c9ec5132d15bbea2c9c7778c000d6fdf509a
2015-04-15 11:03:06 -07:00
Jean-Francois Dupuis
418d5c5278 Add function to return list of globbed file
Change-Id: If501246dc4d7d5b877cc2f871f8786f47c200d00
2015-04-14 23:34:30 -04:00
Colin Cross
8e0c51192a Add license headers and LICENSE file
Change-Id: I6f7c7374093c0745ee4aa677480376a06648b358
2015-01-23 14:23:27 -08:00
Colin Cross
3e8e74f276 Move blueprint/* up a directory
Make integrating with go tools easier by putting the blueprint package
files in the top level directory of the git project instead of in a
subdirectory called blueprint.

Change-Id: I35c144c5fe7ddf34e478d0c47c50b2f6c92c2a03
2015-01-23 14:23:27 -08:00