diff options
author | Jürg Billeter <j@bitron.ch> | 2020-03-03 17:18:11 +0100 |
---|---|---|
committer | Jürg Billeter <j@bitron.ch> | 2020-03-10 15:46:04 +0000 |
commit | 7fbfccce44247122c5b9257e73328de16afa0445 (patch) | |
tree | 44e14a6c3e68e286905e1df825d5e856b58eac46 /src/buildstream/storage | |
parent | f78e6ee55e98c91e2cef055e232df5b955ac7323 (diff) | |
download | buildstream-7fbfccce44247122c5b9257e73328de16afa0445.tar.gz |
storage: Add public Directory.exists() method
Diffstat (limited to 'src/buildstream/storage')
-rw-r--r-- | src/buildstream/storage/_casbaseddirectory.py | 6 | ||||
-rw-r--r-- | src/buildstream/storage/_filebaseddirectory.py | 32 | ||||
-rw-r--r-- | src/buildstream/storage/directory.py | 12 |
3 files changed, 31 insertions, 19 deletions
diff --git a/src/buildstream/storage/_casbaseddirectory.py b/src/buildstream/storage/_casbaseddirectory.py index e4b267a63..ffcf085bb 100644 --- a/src/buildstream/storage/_casbaseddirectory.py +++ b/src/buildstream/storage/_casbaseddirectory.py @@ -795,7 +795,7 @@ class CasBasedDirectory(Directory): return self.__digest - def _exists(self, *path, follow_symlinks=False): + def exists(self, *path, follow_symlinks=False): try: subdir = self.descend(*path[:-1], follow_symlinks=follow_symlinks) target = subdir.index.get(path[-1]) @@ -804,8 +804,8 @@ class CasBasedDirectory(Directory): linklocation = target.target newpath = linklocation.split(os.path.sep) if os.path.isabs(linklocation): - return subdir._find_root()._exists(*newpath, follow_symlinks=True) - return subdir._exists(*newpath, follow_symlinks=True) + return subdir._find_root().exists(*newpath, follow_symlinks=True) + return subdir.exists(*newpath, follow_symlinks=True) else: return True return False diff --git a/src/buildstream/storage/_filebaseddirectory.py b/src/buildstream/storage/_filebaseddirectory.py index aafe33279..62fcc39c0 100644 --- a/src/buildstream/storage/_filebaseddirectory.py +++ b/src/buildstream/storage/_filebaseddirectory.py @@ -232,6 +232,22 @@ class FileBasedDirectory(Directory): def get_size(self): return utils._get_dir_size(self.external_directory) + def exists(self, *path, follow_symlinks=False): + try: + subdir = self.descend(*path[:-1], follow_symlinks=follow_symlinks) + newpath = os.path.join(subdir.external_directory, path[-1]) + st = os.lstat(newpath) + if follow_symlinks and stat.S_ISLNK(st.st_mode): + linklocation = os.readlink(newpath) + newpath = linklocation.split(os.path.sep) + if os.path.isabs(linklocation): + return subdir._find_root().exists(*newpath, follow_symlinks=True) + return subdir.exists(*newpath, follow_symlinks=True) + else: + return True + except (VirtualDirectoryError, FileNotFoundError): + return False + def __str__(self): # This returns the whole path (since we don't know where the directory started) # which exposes the sandbox directory; we will have to assume for the time being @@ -358,22 +374,6 @@ class FileBasedDirectory(Directory): os.symlink(entry.target, dest_path) result.files_written.append(relative_pathname) - def _exists(self, *path, follow_symlinks=False): - try: - subdir = self.descend(*path[:-1], follow_symlinks=follow_symlinks) - newpath = os.path.join(subdir.external_directory, path[-1]) - st = os.lstat(newpath) - if follow_symlinks and stat.S_ISLNK(st.st_mode): - linklocation = os.readlink(newpath) - newpath = linklocation.split(os.path.sep) - if os.path.isabs(linklocation): - return subdir._find_root()._exists(*newpath, follow_symlinks=True) - return subdir._exists(*newpath, follow_symlinks=True) - else: - return True - except (VirtualDirectoryError, FileNotFoundError): - return False - def _create_empty_file(self, name): with open(os.path.join(self.external_directory, name), "w"): pass diff --git a/src/buildstream/storage/directory.py b/src/buildstream/storage/directory.py index 55cc717f2..a9249baee 100644 --- a/src/buildstream/storage/directory.py +++ b/src/buildstream/storage/directory.py @@ -196,6 +196,18 @@ class Directory: and effective space used may be lower than this number due to deduplication. """ raise NotImplementedError() + def exists(self, *paths: str, follow_symlinks: bool = False) -> bool: + """ Check whether the specified path exists. + + Args: + *paths: A list of strings which are all path components. + follow_symlinks: True to follow symlinks. + + Returns: + True if the path exists, False otherwise. + """ + raise NotImplementedError() + # FileType: # |