From 7d8a23fd0a0dd6cdecef9e50a777c14644bc19fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Billeter?= Date: Mon, 10 Feb 2020 07:24:09 +0100 Subject: sandbox: Remove unused _mounter.py --- src/buildstream/sandbox/_mounter.py | 148 ------------------------------- tests/sandboxes/mounting/mount_simple.py | 52 ----------- 2 files changed, 200 deletions(-) delete mode 100644 src/buildstream/sandbox/_mounter.py delete mode 100644 tests/sandboxes/mounting/mount_simple.py diff --git a/src/buildstream/sandbox/_mounter.py b/src/buildstream/sandbox/_mounter.py deleted file mode 100644 index 3adf8ff5b..000000000 --- a/src/buildstream/sandbox/_mounter.py +++ /dev/null @@ -1,148 +0,0 @@ -# -# Copyright (C) 2017 Codethink Limited -# -# This program is free software; you can redistribute it and/or -# modify it under the terms of the GNU Lesser General Public -# License as published by the Free Software Foundation; either -# version 2 of the License, or (at your option) any later version. -# -# This library is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -# Lesser General Public License for more details. -# -# You should have received a copy of the GNU Lesser General Public -# License along with this library. If not, see . -# -# Authors: -# Tristan Maat - -import sys -from contextlib import contextmanager - -from .._exceptions import SandboxError -from .. import utils, _signals - - -# A class to wrap the `mount` and `umount` system commands -class Mounter: - @classmethod - def _mount(cls, dest, src=None, mount_type=None, stdout=None, stderr=None, options=None, flags=None): - - if stdout is None: - stdout = sys.stdout - if stderr is None: - stderr = sys.stderr - - argv = [utils.get_host_tool("mount")] - if mount_type: - argv.extend(["-t", mount_type]) - if options: - argv.extend(["-o", options]) - if flags: - argv.extend(flags) - - if src is not None: - argv += [src] - argv += [dest] - - status, _ = utils._call(argv, terminate=True, stdout=stdout, stderr=stderr) - - if status != 0: - raise SandboxError("`{}` failed with exit code {}".format(" ".join(argv), status)) - - return dest - - @classmethod - def _umount(cls, path, stdout=None, stderr=None): - if stdout is None: - stdout = sys.stdout - if stderr is None: - stderr = sys.stderr - - cmd = [utils.get_host_tool("umount"), "-R", path] - status, _ = utils._call(cmd, terminate=True, stdout=stdout, stderr=stderr) - - if status != 0: - raise SandboxError("`{}` failed with exit code {}".format(" ".join(cmd), status)) - - # mount() - # - # A wrapper for the `mount` command. The device is unmounted when - # the context is left. - # - # Args: - # dest (str) - The directory to mount to - # src (str) - The directory to mount - # stdout (file) - stdout - # stderr (file) - stderr - # mount_type (str|None) - The mount type (can be omitted or None) - # kwargs - Arguments to pass to the mount command, such as `ro=True` - # - # Yields: - # (str) The path to the destination - # - @classmethod - @contextmanager - def mount(cls, dest, src=None, stdout=None, stderr=None, mount_type=None, **kwargs): - if stdout is None: - stdout = sys.stdout - if stderr is None: - stderr = sys.stderr - - def kill_proc(): - cls._umount(dest, stdout, stderr) - - options = ",".join([key for key, val in kwargs.items() if val]) - - path = cls._mount(dest, src, mount_type, stdout=stdout, stderr=stderr, options=options) - try: - with _signals.terminator(kill_proc): - yield path - finally: - cls._umount(dest, stdout, stderr) - - # bind_mount() - # - # Mount a directory to a different location (a hardlink for all - # intents and purposes). The directory is unmounted when the - # context is left. - # - # Args: - # dest (str) - The directory to mount to - # src (str) - The directory to mount - # stdout (file) - stdout - # stderr (file) - stderr - # kwargs - Arguments to pass to the mount command, such as `ro=True` - # - # Yields: - # (str) The path to the destination - # - # While this is equivalent to `mount --rbind`, this option may not - # exist and can be dangerous, requiring careful cleanupIt is - # recommended to use this function over a manual mount invocation. - # - @classmethod - @contextmanager - def bind_mount(cls, dest, src=None, stdout=None, stderr=None, **kwargs): - if stdout is None: - stdout = sys.stdout - if stderr is None: - stderr = sys.stderr - - def kill_proc(): - cls._umount(dest, stdout, stderr) - - kwargs["rbind"] = True - options = ",".join([key for key, val in kwargs.items() if val]) - - path = cls._mount(dest, src, None, stdout, stderr, options) - - try: - with _signals.terminator(kill_proc): - # Make the rbind a slave to avoid unmounting vital devices in - # /proc - cls._mount(dest, flags=["--make-rslave"]) - yield path - finally: - cls._umount(dest, stdout, stderr) diff --git a/tests/sandboxes/mounting/mount_simple.py b/tests/sandboxes/mounting/mount_simple.py deleted file mode 100644 index 0e78a5603..000000000 --- a/tests/sandboxes/mounting/mount_simple.py +++ /dev/null @@ -1,52 +0,0 @@ -import os -import tempfile -from contextlib import ExitStack - -import pytest - -from buildstream.sandbox._mounter import Mounter - - -@pytest.mark.skipif(not os.geteuid() == 0, reason="requires root permissions") -def test_bind_mount(): - with ExitStack() as stack: - src = stack.enter_context(tempfile.TemporaryDirectory()) - target = stack.enter_context(tempfile.TemporaryDirectory()) - - with open(os.path.join(src, "test"), "a") as test: - test.write("Test") - - with Mounter.bind_mount(target, src) as dest: - # Ensure we get the correct path back - assert dest == target - - # Ensure we can access files from src from target - with open(os.path.join(target, "test"), "r") as test: - assert test.read() == "Test" - - # Ensure the files from src are gone from target - with pytest.raises(FileNotFoundError): - with open(os.path.join(target, "test"), "r"): - # Actual contents don't matter - pass - - # Ensure the files in src are still in src - with open(os.path.join(src, "test"), "r") as test: - assert test.read() == "Test" - - -@pytest.mark.skipif(not os.geteuid() == 0, reason="requires root permissions") -def test_mount_proc(): - with ExitStack() as stack: - src = "/proc" - target = stack.enter_context(tempfile.TemporaryDirectory()) - - with Mounter.mount(target, src, mount_type="proc", ro=True) as dest: - # Ensure we get the correct path back - assert dest == target - - # Ensure /proc is actually mounted - assert os.listdir(src) == os.listdir(target) - - # Ensure /proc is unmounted correctly - assert os.listdir(target) == [] -- cgit v1.2.1 From 9319c6fcc172b47f6305c1f110c83cd1b1706638 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Billeter?= Date: Mon, 10 Feb 2020 07:37:35 +0100 Subject: utils.py: Drop device node support from _process_list() This should not be needed. For non-privileged users `mknod()` will anyway fail. Let's consistently mark it as unsupported. --- src/buildstream/utils.py | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/src/buildstream/utils.py b/src/buildstream/utils.py index 9593f3e75..2b6c2761b 100644 --- a/src/buildstream/utils.py +++ b/src/buildstream/utils.py @@ -1059,17 +1059,6 @@ def _process_list( actionfunc(srcpath, destpath, result=result) - elif stat.S_ISCHR(mode) or stat.S_ISBLK(mode): - # Block or character device. Put contents of st_dev in a mknod. - if not safe_remove(destpath): - result.ignored.append(path) - continue - - if os.path.lexists(destpath): - os.remove(destpath) - os.mknod(destpath, file_stat.st_mode, file_stat.st_rdev) - os.chmod(destpath, file_stat.st_mode) - elif stat.S_ISFIFO(mode): os.mkfifo(destpath, mode) -- cgit v1.2.1 From b5477b27c821f1e2e35ff84e850786af0522f492 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Billeter?= Date: Mon, 10 Feb 2020 07:47:00 +0100 Subject: cascache.py: Remove unused _get_subdir() method --- src/buildstream/_cas/cascache.py | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/src/buildstream/_cas/cascache.py b/src/buildstream/_cas/cascache.py index bb2abc6c8..083c8e8dc 100644 --- a/src/buildstream/_cas/cascache.py +++ b/src/buildstream/_cas/cascache.py @@ -547,22 +547,6 @@ class CASCache: # Local Private Methods # ################################################ - def _get_subdir(self, tree, subdir): - head, name = os.path.split(subdir) - if head: - tree = self._get_subdir(tree, head) - - directory = remote_execution_pb2.Directory() - - with open(self.objpath(tree), "rb") as f: - directory.ParseFromString(f.read()) - - for dirnode in directory.directories: - if dirnode.name == name: - return dirnode.digest - - raise CASCacheError("Subdirectory {} not found".format(name)) - def _reachable_refs_dir(self, reachable, tree, update_mtime=False, check_exists=False): if tree.hash in reachable: return -- cgit v1.2.1