Skip to content

Commit

Permalink
Fix up exception declarations; use EnvironmentalExecException.
Browse files Browse the repository at this point in the history
--
MOS_MIGRATED_REVID=109404922
  • Loading branch information
ulfjack authored and kchodorow committed Dec 4, 2015
1 parent 85e6078 commit c3e5e2a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.google.devtools.build.lib.actions.ActionInput;
import com.google.devtools.build.lib.actions.ActionInputHelper;
import com.google.devtools.build.lib.actions.Artifact;
import com.google.devtools.build.lib.actions.EnvironmentalExecException;
import com.google.devtools.build.lib.actions.ExecException;
import com.google.devtools.build.lib.actions.ExecutionStrategy;
import com.google.devtools.build.lib.actions.Executor;
Expand Down Expand Up @@ -122,16 +123,10 @@ public void exec(Spawn spawn, ActionExecutionContext actionExecutionContext)
// Gather all necessary mounts for the sandbox.
mounts = getMounts(spawn, actionExecutionContext);
} catch (IllegalArgumentException | IOException e) {
throw new UserExecException("Could not prepare mounts for sandbox execution", e);
throw new EnvironmentalExecException("Could not prepare mounts for sandbox execution", e);
}

ImmutableSet<Path> createDirs;
try {
createDirs = createImportantDirs(spawn.getEnvironment());
} catch (IOException e) {
throw new UserExecException(
"Could not prepare the set of important directories to create in the sandbox", e);
}
ImmutableSet<Path> createDirs = createImportantDirs(spawn.getEnvironment());

int timeout = getTimeout(spawn);

Expand Down Expand Up @@ -178,7 +173,7 @@ public void run() {
}
}

private int getTimeout(Spawn spawn) throws UserExecException {
private int getTimeout(Spawn spawn) throws ExecException {
String timeoutStr = spawn.getExecutionInfo().get("timeout");
if (timeoutStr != null) {
try {
Expand All @@ -196,7 +191,7 @@ private int getTimeout(Spawn spawn) throws UserExecException {
* <p>Note that $HOME is handled by namespace-sandbox.c, because it changes user to nobody and the
* home directory of that user is not known by us.
*/
private ImmutableSet<Path> createImportantDirs(Map<String, String> env) throws IOException {
private ImmutableSet<Path> createImportantDirs(Map<String, String> env) {
ImmutableSet.Builder<Path> dirs = ImmutableSet.builder();
FileSystem fs = blazeDirs.getFileSystem();
if (env.containsKey("TEST_TMPDIR")) {
Expand All @@ -207,7 +202,7 @@ private ImmutableSet<Path> createImportantDirs(Map<String, String> env) throws I
}

private ImmutableMap<Path, Path> getMounts(Spawn spawn, ActionExecutionContext executionContext)
throws IOException, UserExecException {
throws IOException, ExecException {
MountMap mounts = new MountMap();
mounts.putAll(mountUsualUnixDirs());
mounts.putAll(withRecursedDirs(setupBlazeUtils()));
Expand Down Expand Up @@ -314,7 +309,7 @@ private MountMap mountUsualUnixDirs() throws IOException {
/**
* Mount the embedded tools.
*/
private MountMap setupBlazeUtils() throws IOException {
private MountMap setupBlazeUtils() {
MountMap mounts = new MountMap();
Path mount = blazeDirs.getEmbeddedBinariesRoot().getRelative("build-runfiles");
mounts.put(mount, mount);
Expand All @@ -324,7 +319,7 @@ private MountMap setupBlazeUtils() throws IOException {
/**
* Mount all runfiles that the spawn needs as specified in its runfiles manifests.
*/
private MountMap mountRunfilesFromManifests(Spawn spawn) throws IOException, UserExecException {
private MountMap mountRunfilesFromManifests(Spawn spawn) throws IOException, ExecException {
MountMap mounts = new MountMap();
for (Entry<PathFragment, Artifact> manifest : spawn.getRunfilesManifests().entrySet()) {
String manifestFilePath = manifest.getValue().getPath().getPathString();
Expand All @@ -340,7 +335,7 @@ private MountMap mountRunfilesFromManifests(Spawn spawn) throws IOException, Use
* Mount all files that the spawn needs as specified in its fileset manifests.
*/
private MountMap mountFilesFromFilesetManifests(
Spawn spawn, ActionExecutionContext executionContext) throws IOException, UserExecException {
Spawn spawn, ActionExecutionContext executionContext) throws IOException, ExecException {
final FilesetActionContext filesetContext =
executionContext.getExecutor().getContext(FilesetActionContext.class);
MountMap mounts = new MountMap();
Expand All @@ -358,7 +353,7 @@ private MountMap mountFilesFromFilesetManifests(

static MountMap parseManifestFile(
Path targetDirectory, File manifestFile, boolean isFilesetManifest, String workspaceName)
throws IOException, UserExecException {
throws IOException, ExecException {
MountMap mounts = new MountMap();
int lineNum = 0;
for (String line : Files.readLines(manifestFile, StandardCharsets.UTF_8)) {
Expand All @@ -376,7 +371,8 @@ static MountMap parseManifestFile(
PathFragment targetPathFragment = new PathFragment(fields[0]);
if (!workspaceName.isEmpty()) {
if (!targetPathFragment.getSegment(0).equals(workspaceName)) {
throw new UserExecException("Fileset manifest line must start with workspace name");
throw new EnvironmentalExecException(
"Fileset manifest line must start with workspace name");
}
targetPathFragment = targetPathFragment.subFragment(1, targetPathFragment.segmentCount());
}
Expand Down Expand Up @@ -428,8 +424,7 @@ private MountMap mountRunfilesFromSuppliers(Spawn spawn) throws IOException {
/**
* Mount all inputs of the spawn.
*/
private MountMap mountInputs(Spawn spawn, ActionExecutionContext actionExecutionContext)
throws IOException {
private MountMap mountInputs(Spawn spawn, ActionExecutionContext actionExecutionContext) {
MountMap mounts = new MountMap();

List<ActionInput> inputs =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.google.common.io.ByteStreams;
import com.google.common.io.Files;
import com.google.devtools.build.lib.actions.ActionInput;
import com.google.devtools.build.lib.actions.ExecException;
import com.google.devtools.build.lib.actions.UserExecException;
import com.google.devtools.build.lib.analysis.config.BinTools;
import com.google.devtools.build.lib.runtime.BlazeRuntime;
Expand Down Expand Up @@ -104,7 +105,7 @@ static boolean isSupported(BlazeRuntime runtime) {
* @param env - environment to run sandbox in
* @param cwd - current working directory
* @param outErr - error output to capture sandbox's and command's stderr
* @throws CommandException
* @throws ExecException
*/
public void run(
List<String> spawnArguments,
Expand All @@ -114,7 +115,7 @@ public void run(
Collection<? extends ActionInput> outputs,
int timeout,
boolean blockNetwork)
throws IOException, UserExecException {
throws IOException, ExecException {
createFileSystem(outputs);

List<String> fileArgs = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import com.google.devtools.build.lib.actions.UserExecException;
import com.google.devtools.build.lib.vfs.FileSystemUtils;
import com.google.devtools.build.lib.vfs.Path;
import com.google.devtools.build.lib.vfs.PathFragment;
Expand Down Expand Up @@ -68,12 +67,12 @@ private ImmutableMap<String, String> userFriendlyMap(Map<Path, Path> input) {
* output of {@code LinuxSandboxedStrategy#fixMounts} for it.
*/
private ImmutableMap<String, String> userFriendlyMounts(
Map<String, String> linksAndFiles, List<String> customMounts) throws IOException {
Map<String, String> linksAndFiles, List<String> customMounts) throws Exception {
return userFriendlyMap(mounts(linksAndFiles, customMounts));
}

private ImmutableMap<Path, Path> mounts(
Map<String, String> linksAndFiles, List<String> customMounts) throws IOException {
Map<String, String> linksAndFiles, List<String> customMounts) throws Exception {
createTreeStructure(linksAndFiles);

ImmutableMap.Builder<Path, Path> mounts = ImmutableMap.builder();
Expand All @@ -92,11 +91,11 @@ private ImmutableMap<Path, Path> mounts(
* the output of {@code LinuxSandboxedStrategy#fixMounts} for it.
*/
private Map<String, String> userFriendlyMounts(Map<String, String> linksAndFiles)
throws IOException {
throws Exception {
return userFriendlyMap(mounts(linksAndFiles));
}

private Map<Path, Path> mounts(Map<String, String> linksAndFiles) throws IOException {
private Map<Path, Path> mounts(Map<String, String> linksAndFiles) throws Exception {
return mounts(
linksAndFiles, ImmutableList.of(Iterables.getFirst(linksAndFiles.keySet(), null)));
}
Expand All @@ -118,7 +117,7 @@ private ImmutableMap<Path, Path> asserts(List<String> asserts) {
return pathifiedAsserts.build();
}

private void createTreeStructure(Map<String, String> linksAndFiles) throws IOException {
private void createTreeStructure(Map<String, String> linksAndFiles) throws Exception {
for (Entry<String, String> entry : linksAndFiles.entrySet()) {
Path filePath = workspaceDir.getRelative(entry.getKey());
String linkTarget = entry.getValue();
Expand All @@ -136,7 +135,7 @@ private void createTreeStructure(Map<String, String> linksAndFiles) throws IOExc
}

@Test
public void testResolvesRelativeFileToFileSymlinkInSameDir() throws IOException {
public void testResolvesRelativeFileToFileSymlinkInSameDir() throws Exception {
Map<String, String> testFiles = new LinkedHashMap<>();
testFiles.put("symlink.txt", "goal.txt");
testFiles.put("goal.txt", "");
Expand All @@ -149,7 +148,7 @@ public void testResolvesRelativeFileToFileSymlinkInSameDir() throws IOException
}

@Test
public void testResolvesRelativeFileToFileSymlinkInSubDir() throws IOException {
public void testResolvesRelativeFileToFileSymlinkInSubDir() throws Exception {
Map<String, String> testFiles =
ImmutableMap.of(
"symlink.txt", "x/goal.txt",
Expand All @@ -160,7 +159,7 @@ public void testResolvesRelativeFileToFileSymlinkInSubDir() throws IOException {
}

@Test
public void testResolvesRelativeFileToFileSymlinkInParentDir() throws IOException {
public void testResolvesRelativeFileToFileSymlinkInParentDir() throws Exception {
Map<String, String> testFiles =
ImmutableMap.of(
"x/symlink.txt", "../goal.txt",
Expand All @@ -172,7 +171,7 @@ public void testResolvesRelativeFileToFileSymlinkInParentDir() throws IOExceptio
}

@Test
public void testRecursesSubDirs() throws IOException {
public void testRecursesSubDirs() throws Exception {
ImmutableList<String> inputFile = ImmutableList.of("a/b");

Map<String, String> testFiles =
Expand All @@ -191,7 +190,7 @@ public void testRecursesSubDirs() throws IOException {
* Test that the algorithm correctly identifies and refuses symlink loops.
*/
@Test
public void testCatchesSymlinkLoop() throws IOException {
public void testCatchesSymlinkLoop() throws Exception {
try {
mounts(
ImmutableMap.of(
Expand All @@ -212,7 +211,7 @@ public void testCatchesSymlinkLoop() throws IOException {
* directories (e.g. "a -> dir/file/file").
*/
@Test
public void testCatchesIllegalSymlink() throws IOException {
public void testCatchesIllegalSymlink() throws Exception {
try {
mounts(
ImmutableMap.of(
Expand All @@ -228,7 +227,7 @@ public void testCatchesIllegalSymlink() throws IOException {
}

@Test
public void testParseManifestFile() throws IOException, UserExecException {
public void testParseManifestFile() throws Exception {
Path targetDir = workspaceDir.getRelative("runfiles");
targetDir.createDirectory();

Expand All @@ -255,7 +254,7 @@ public void testParseManifestFile() throws IOException, UserExecException {
}

@Test
public void testParseFilesetManifestFile() throws IOException, UserExecException {
public void testParseFilesetManifestFile() throws Exception {
Path targetDir = workspaceDir.getRelative("fileset");
targetDir.createDirectory();

Expand Down

0 comments on commit c3e5e2a

Please sign in to comment.