Jeff Gaston | efc1b41 | 2017-03-29 17:29:06 -0700 | [diff] [blame^] | 1 | // Copyright 2017 Google Inc. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package main |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | "io/ioutil" |
| 20 | "os" |
| 21 | "os/exec" |
| 22 | "path" |
| 23 | "path/filepath" |
| 24 | "strings" |
| 25 | ) |
| 26 | |
| 27 | func main() { |
| 28 | error := run() |
| 29 | if error != nil { |
| 30 | fmt.Fprintln(os.Stderr, error) |
| 31 | os.Exit(1) |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | var usage = "Usage: sbox -c <commandToRun> --sandbox-path <sandboxPath> <outputFiles>" |
| 36 | |
| 37 | func usageError(violation string) error { |
| 38 | return fmt.Errorf("Usage error: %s.\n %s", violation, usage) |
| 39 | } |
| 40 | |
| 41 | func run() error { |
| 42 | var outFiles []string |
| 43 | args := os.Args[1:] |
| 44 | |
| 45 | var rawCommand string |
| 46 | var sandboxesRoot string |
| 47 | |
| 48 | for i := 0; i < len(args); i++ { |
| 49 | arg := args[i] |
| 50 | if arg == "--sandbox-path" { |
| 51 | sandboxesRoot = args[i+1] |
| 52 | i++ |
| 53 | } else if arg == "-c" { |
| 54 | rawCommand = args[i+1] |
| 55 | i++ |
| 56 | } else { |
| 57 | outFiles = append(outFiles, arg) |
| 58 | } |
| 59 | } |
| 60 | if len(rawCommand) == 0 { |
| 61 | return usageError("-c <commandToRun> is required and must be non-empty") |
| 62 | } |
| 63 | if outFiles == nil { |
| 64 | return usageError("at least one output file must be given") |
| 65 | } |
| 66 | if len(sandboxesRoot) == 0 { |
| 67 | // In practice, the value of sandboxesRoot will mostly likely be at a fixed location relative to OUT_DIR, |
| 68 | // and the sbox executable will most likely be at a fixed location relative to OUT_DIR too, so |
| 69 | // the value of sandboxesRoot will most likely be at a fixed location relative to the sbox executable |
| 70 | // However, Soong also needs to be able to separately remove the sandbox directory on startup (if it has anything left in it) |
| 71 | // and by passing it as a parameter we don't need to duplicate its value |
| 72 | return usageError("--sandbox-path <sandboxPath> is required and must be non-empty") |
| 73 | } |
| 74 | |
| 75 | os.MkdirAll(sandboxesRoot, 0777) |
| 76 | |
| 77 | tempDir, err := ioutil.TempDir(sandboxesRoot, "sbox") |
| 78 | if err != nil { |
| 79 | return fmt.Errorf("Failed to create temp dir: %s", err) |
| 80 | } |
| 81 | |
| 82 | // In the common case, the following line of code is what removes the sandbox |
| 83 | // If a fatal error occurs (such as if our Go process is killed unexpectedly), |
| 84 | // then at the beginning of the next build, Soong will retry the cleanup |
| 85 | defer os.RemoveAll(tempDir) |
| 86 | |
| 87 | if strings.Contains(rawCommand, "__SBOX_OUT_DIR__") { |
| 88 | rawCommand = strings.Replace(rawCommand, "__SBOX_OUT_DIR__", tempDir, -1) |
| 89 | } |
| 90 | |
| 91 | if strings.Contains(rawCommand, "__SBOX_OUT_FILES__") { |
| 92 | // expands into a space-separated list of output files to be generated into the sandbox directory |
| 93 | tempOutPaths := []string{} |
| 94 | for _, outputPath := range outFiles { |
| 95 | tempOutPath := path.Join(tempDir, outputPath) |
| 96 | tempOutPaths = append(tempOutPaths, tempOutPath) |
| 97 | } |
| 98 | pathsText := strings.Join(tempOutPaths, " ") |
| 99 | rawCommand = strings.Replace(rawCommand, "__SBOX_OUT_FILES__", pathsText, -1) |
| 100 | } |
| 101 | |
| 102 | for _, filePath := range outFiles { |
| 103 | os.MkdirAll(path.Join(tempDir, filepath.Dir(filePath)), 0777) |
| 104 | } |
| 105 | |
| 106 | cmd := exec.Command("bash", "-c", rawCommand) |
| 107 | cmd.Stdin = os.Stdin |
| 108 | cmd.Stdout = os.Stdout |
| 109 | cmd.Stderr = os.Stderr |
| 110 | err = cmd.Run() |
| 111 | if exit, ok := err.(*exec.ExitError); ok && !exit.Success() { |
| 112 | return fmt.Errorf("sbox command %#v failed with err %#v\n", cmd, err) |
| 113 | } else if err != nil { |
| 114 | return err |
| 115 | } |
| 116 | |
| 117 | for _, filePath := range outFiles { |
| 118 | tempPath := filepath.Join(tempDir, filePath) |
| 119 | fileInfo, err := os.Stat(tempPath) |
| 120 | if err != nil { |
| 121 | return fmt.Errorf("command run under sbox did not create expected output file %s", filePath) |
| 122 | } |
| 123 | if fileInfo.IsDir() { |
| 124 | return fmt.Errorf("Output path %s refers to a directory, not a file. This is not permitted because it prevents robust up-to-date checks", filePath) |
| 125 | } |
| 126 | err = os.Rename(tempPath, filePath) |
| 127 | if err != nil { |
| 128 | return err |
| 129 | } |
| 130 | } |
| 131 | // TODO(jeffrygaston) if a process creates more output files than it declares, should there be a warning? |
| 132 | return nil |
| 133 | } |