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 ( |
Dan Willemsen | c89b6f1 | 2019-08-29 14:47:40 -0700 | [diff] [blame] | 18 | "bytes" |
Ulf Adams | b73daa5 | 2020-11-25 23:09:09 +0100 | [diff] [blame] | 19 | "crypto/sha1" |
| 20 | "encoding/hex" |
Jeff Gaston | 90cfb09 | 2017-09-26 16:46:10 -0700 | [diff] [blame] | 21 | "errors" |
Jeff Gaston | 93f0f37 | 2017-11-01 13:33:02 -0700 | [diff] [blame] | 22 | "flag" |
Jeff Gaston | efc1b41 | 2017-03-29 17:29:06 -0700 | [diff] [blame] | 23 | "fmt" |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 24 | "io" |
Jeff Gaston | efc1b41 | 2017-03-29 17:29:06 -0700 | [diff] [blame] | 25 | "io/ioutil" |
| 26 | "os" |
| 27 | "os/exec" |
Jeff Gaston | efc1b41 | 2017-03-29 17:29:06 -0700 | [diff] [blame] | 28 | "path/filepath" |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 29 | "strconv" |
Jeff Gaston | efc1b41 | 2017-03-29 17:29:06 -0700 | [diff] [blame] | 30 | "strings" |
Colin Cross | d1c1e6f | 2019-03-29 13:54:39 -0700 | [diff] [blame] | 31 | "time" |
Dan Willemsen | c89b6f1 | 2019-08-29 14:47:40 -0700 | [diff] [blame] | 32 | |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 33 | "android/soong/cmd/sbox/sbox_proto" |
Dan Willemsen | c89b6f1 | 2019-08-29 14:47:40 -0700 | [diff] [blame] | 34 | "android/soong/makedeps" |
Colin Cross | e55bd42 | 2021-03-23 13:44:30 -0700 | [diff] [blame] | 35 | "android/soong/response" |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 36 | |
| 37 | "github.com/golang/protobuf/proto" |
Jeff Gaston | efc1b41 | 2017-03-29 17:29:06 -0700 | [diff] [blame] | 38 | ) |
| 39 | |
Jeff Gaston | 93f0f37 | 2017-11-01 13:33:02 -0700 | [diff] [blame] | 40 | var ( |
| 41 | sandboxesRoot string |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 42 | manifestFile string |
Jeff Gaston | 93f0f37 | 2017-11-01 13:33:02 -0700 | [diff] [blame] | 43 | keepOutDir bool |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 44 | ) |
| 45 | |
| 46 | const ( |
| 47 | depFilePlaceholder = "__SBOX_DEPFILE__" |
| 48 | sandboxDirPlaceholder = "__SBOX_SANDBOX_DIR__" |
Jeff Gaston | 93f0f37 | 2017-11-01 13:33:02 -0700 | [diff] [blame] | 49 | ) |
| 50 | |
| 51 | func init() { |
| 52 | flag.StringVar(&sandboxesRoot, "sandbox-path", "", |
| 53 | "root of temp directory to put the sandbox into") |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 54 | flag.StringVar(&manifestFile, "manifest", "", |
| 55 | "textproto manifest describing the sandboxed command(s)") |
Jeff Gaston | 93f0f37 | 2017-11-01 13:33:02 -0700 | [diff] [blame] | 56 | flag.BoolVar(&keepOutDir, "keep-out-dir", false, |
| 57 | "whether to keep the sandbox directory when done") |
Jeff Gaston | 93f0f37 | 2017-11-01 13:33:02 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | func usageViolation(violation string) { |
| 61 | if violation != "" { |
| 62 | fmt.Fprintf(os.Stderr, "Usage error: %s.\n\n", violation) |
| 63 | } |
| 64 | |
| 65 | fmt.Fprintf(os.Stderr, |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 66 | "Usage: sbox --manifest <manifest> --sandbox-path <sandboxPath>\n") |
Jeff Gaston | 93f0f37 | 2017-11-01 13:33:02 -0700 | [diff] [blame] | 67 | |
| 68 | flag.PrintDefaults() |
| 69 | |
| 70 | os.Exit(1) |
| 71 | } |
| 72 | |
Jeff Gaston | efc1b41 | 2017-03-29 17:29:06 -0700 | [diff] [blame] | 73 | func main() { |
Jeff Gaston | 93f0f37 | 2017-11-01 13:33:02 -0700 | [diff] [blame] | 74 | flag.Usage = func() { |
| 75 | usageViolation("") |
| 76 | } |
| 77 | flag.Parse() |
| 78 | |
Jeff Gaston | efc1b41 | 2017-03-29 17:29:06 -0700 | [diff] [blame] | 79 | error := run() |
| 80 | if error != nil { |
| 81 | fmt.Fprintln(os.Stderr, error) |
| 82 | os.Exit(1) |
| 83 | } |
| 84 | } |
| 85 | |
Jeff Gaston | 90cfb09 | 2017-09-26 16:46:10 -0700 | [diff] [blame] | 86 | func findAllFilesUnder(root string) (paths []string) { |
| 87 | paths = []string{} |
| 88 | filepath.Walk(root, func(path string, info os.FileInfo, err error) error { |
| 89 | if !info.IsDir() { |
| 90 | relPath, err := filepath.Rel(root, path) |
| 91 | if err != nil { |
| 92 | // couldn't find relative path from ancestor? |
| 93 | panic(err) |
| 94 | } |
| 95 | paths = append(paths, relPath) |
| 96 | } |
| 97 | return nil |
| 98 | }) |
| 99 | return paths |
| 100 | } |
| 101 | |
Jeff Gaston | efc1b41 | 2017-03-29 17:29:06 -0700 | [diff] [blame] | 102 | func run() error { |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 103 | if manifestFile == "" { |
| 104 | usageViolation("--manifest <manifest> is required and must be non-empty") |
Jeff Gaston | efc1b41 | 2017-03-29 17:29:06 -0700 | [diff] [blame] | 105 | } |
Jeff Gaston | 02a684b | 2017-10-27 14:59:27 -0700 | [diff] [blame] | 106 | if sandboxesRoot == "" { |
Jeff Gaston | efc1b41 | 2017-03-29 17:29:06 -0700 | [diff] [blame] | 107 | // In practice, the value of sandboxesRoot will mostly likely be at a fixed location relative to OUT_DIR, |
| 108 | // and the sbox executable will most likely be at a fixed location relative to OUT_DIR too, so |
| 109 | // the value of sandboxesRoot will most likely be at a fixed location relative to the sbox executable |
| 110 | // However, Soong also needs to be able to separately remove the sandbox directory on startup (if it has anything left in it) |
| 111 | // and by passing it as a parameter we don't need to duplicate its value |
Jeff Gaston | 93f0f37 | 2017-11-01 13:33:02 -0700 | [diff] [blame] | 112 | usageViolation("--sandbox-path <sandboxPath> is required and must be non-empty") |
Jeff Gaston | efc1b41 | 2017-03-29 17:29:06 -0700 | [diff] [blame] | 113 | } |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 114 | |
| 115 | manifest, err := readManifest(manifestFile) |
| 116 | |
| 117 | if len(manifest.Commands) == 0 { |
| 118 | return fmt.Errorf("at least one commands entry is required in %q", manifestFile) |
Jeff Gaston | 193f2fb | 2017-06-12 15:00:12 -0700 | [diff] [blame] | 119 | } |
| 120 | |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 121 | // setup sandbox directory |
| 122 | err = os.MkdirAll(sandboxesRoot, 0777) |
Jeff Gaston | 8a88db5 | 2017-11-06 13:33:14 -0800 | [diff] [blame] | 123 | if err != nil { |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 124 | return fmt.Errorf("failed to create %q: %w", sandboxesRoot, err) |
Jeff Gaston | 8a88db5 | 2017-11-06 13:33:14 -0800 | [diff] [blame] | 125 | } |
Jeff Gaston | efc1b41 | 2017-03-29 17:29:06 -0700 | [diff] [blame] | 126 | |
Ulf Adams | b73daa5 | 2020-11-25 23:09:09 +0100 | [diff] [blame] | 127 | // This tool assumes that there are no two concurrent runs with the same |
| 128 | // manifestFile. It should therefore be safe to use the hash of the |
| 129 | // manifestFile as the temporary directory name. We do this because it |
| 130 | // makes the temporary directory name deterministic. There are some |
| 131 | // tools that embed the name of the temporary output in the output, and |
| 132 | // they otherwise cause non-determinism, which then poisons actions |
| 133 | // depending on this one. |
| 134 | hash := sha1.New() |
| 135 | hash.Write([]byte(manifestFile)) |
| 136 | tempDir := filepath.Join(sandboxesRoot, "sbox", hex.EncodeToString(hash.Sum(nil))) |
| 137 | |
| 138 | err = os.RemoveAll(tempDir) |
| 139 | if err != nil { |
| 140 | return err |
| 141 | } |
| 142 | err = os.MkdirAll(tempDir, 0777) |
Jeff Gaston | efc1b41 | 2017-03-29 17:29:06 -0700 | [diff] [blame] | 143 | if err != nil { |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 144 | return fmt.Errorf("failed to create temporary dir in %q: %w", sandboxesRoot, err) |
Jeff Gaston | efc1b41 | 2017-03-29 17:29:06 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | // In the common case, the following line of code is what removes the sandbox |
| 148 | // If a fatal error occurs (such as if our Go process is killed unexpectedly), |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 149 | // then at the beginning of the next build, Soong will wipe the temporary |
| 150 | // directory. |
Jeff Gaston | f49082a | 2017-06-07 13:22:22 -0700 | [diff] [blame] | 151 | defer func() { |
| 152 | // in some cases we decline to remove the temp dir, to facilitate debugging |
Jeff Gaston | 93f0f37 | 2017-11-01 13:33:02 -0700 | [diff] [blame] | 153 | if !keepOutDir { |
Jeff Gaston | f49082a | 2017-06-07 13:22:22 -0700 | [diff] [blame] | 154 | os.RemoveAll(tempDir) |
| 155 | } |
| 156 | }() |
Jeff Gaston | efc1b41 | 2017-03-29 17:29:06 -0700 | [diff] [blame] | 157 | |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 158 | // If there is more than one command in the manifest use a separate directory for each one. |
| 159 | useSubDir := len(manifest.Commands) > 1 |
| 160 | var commandDepFiles []string |
Jeff Gaston | efc1b41 | 2017-03-29 17:29:06 -0700 | [diff] [blame] | 161 | |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 162 | for i, command := range manifest.Commands { |
| 163 | localTempDir := tempDir |
| 164 | if useSubDir { |
| 165 | localTempDir = filepath.Join(localTempDir, strconv.Itoa(i)) |
Jeff Gaston | efc1b41 | 2017-03-29 17:29:06 -0700 | [diff] [blame] | 166 | } |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 167 | depFile, err := runCommand(command, localTempDir) |
Jeff Gaston | 02a684b | 2017-10-27 14:59:27 -0700 | [diff] [blame] | 168 | if err != nil { |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 169 | // Running the command failed, keep the temporary output directory around in |
| 170 | // case a user wants to inspect it for debugging purposes. Soong will delete |
| 171 | // it at the beginning of the next build anyway. |
| 172 | keepOutDir = true |
Jeff Gaston | 02a684b | 2017-10-27 14:59:27 -0700 | [diff] [blame] | 173 | return err |
| 174 | } |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 175 | if depFile != "" { |
| 176 | commandDepFiles = append(commandDepFiles, depFile) |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | outputDepFile := manifest.GetOutputDepfile() |
| 181 | if len(commandDepFiles) > 0 && outputDepFile == "" { |
| 182 | return fmt.Errorf("Sandboxed commands used %s but output depfile is not set in manifest file", |
| 183 | depFilePlaceholder) |
| 184 | } |
| 185 | |
| 186 | if outputDepFile != "" { |
| 187 | // Merge the depfiles from each command in the manifest to a single output depfile. |
| 188 | err = rewriteDepFiles(commandDepFiles, outputDepFile) |
| 189 | if err != nil { |
| 190 | return fmt.Errorf("failed merging depfiles: %w", err) |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | return nil |
| 195 | } |
| 196 | |
| 197 | // readManifest reads an sbox manifest from a textproto file. |
| 198 | func readManifest(file string) (*sbox_proto.Manifest, error) { |
| 199 | manifestData, err := ioutil.ReadFile(file) |
| 200 | if err != nil { |
| 201 | return nil, fmt.Errorf("error reading manifest %q: %w", file, err) |
| 202 | } |
| 203 | |
| 204 | manifest := sbox_proto.Manifest{} |
| 205 | |
| 206 | err = proto.UnmarshalText(string(manifestData), &manifest) |
| 207 | if err != nil { |
| 208 | return nil, fmt.Errorf("error parsing manifest %q: %w", file, err) |
| 209 | } |
| 210 | |
| 211 | return &manifest, nil |
| 212 | } |
| 213 | |
| 214 | // runCommand runs a single command from a manifest. If the command references the |
| 215 | // __SBOX_DEPFILE__ placeholder it returns the name of the depfile that was used. |
| 216 | func runCommand(command *sbox_proto.Command, tempDir string) (depFile string, err error) { |
| 217 | rawCommand := command.GetCommand() |
| 218 | if rawCommand == "" { |
| 219 | return "", fmt.Errorf("command is required") |
| 220 | } |
| 221 | |
Colin Cross | e55bd42 | 2021-03-23 13:44:30 -0700 | [diff] [blame] | 222 | pathToTempDirInSbox := tempDir |
| 223 | if command.GetChdir() { |
| 224 | pathToTempDirInSbox = "." |
| 225 | } |
| 226 | |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 227 | err = os.MkdirAll(tempDir, 0777) |
| 228 | if err != nil { |
| 229 | return "", fmt.Errorf("failed to create %q: %w", tempDir, err) |
| 230 | } |
| 231 | |
| 232 | // Copy in any files specified by the manifest. |
Colin Cross | fc2d842 | 2021-04-15 18:36:42 -0700 | [diff] [blame^] | 233 | err = copyFiles(command.CopyBefore, "", tempDir, false) |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 234 | if err != nil { |
| 235 | return "", err |
| 236 | } |
Colin Cross | e55bd42 | 2021-03-23 13:44:30 -0700 | [diff] [blame] | 237 | err = copyRspFiles(command.RspFiles, tempDir, pathToTempDirInSbox) |
| 238 | if err != nil { |
| 239 | return "", err |
Colin Cross | c590ec4 | 2021-03-11 17:20:02 -0800 | [diff] [blame] | 240 | } |
| 241 | |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 242 | if strings.Contains(rawCommand, depFilePlaceholder) { |
Colin Cross | c590ec4 | 2021-03-11 17:20:02 -0800 | [diff] [blame] | 243 | depFile = filepath.Join(pathToTempDirInSbox, "deps.d") |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 244 | rawCommand = strings.Replace(rawCommand, depFilePlaceholder, depFile, -1) |
| 245 | } |
| 246 | |
| 247 | if strings.Contains(rawCommand, sandboxDirPlaceholder) { |
Colin Cross | c590ec4 | 2021-03-11 17:20:02 -0800 | [diff] [blame] | 248 | rawCommand = strings.Replace(rawCommand, sandboxDirPlaceholder, pathToTempDirInSbox, -1) |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | // Emulate ninja's behavior of creating the directories for any output files before |
| 252 | // running the command. |
| 253 | err = makeOutputDirs(command.CopyAfter, tempDir) |
| 254 | if err != nil { |
| 255 | return "", err |
Jeff Gaston | efc1b41 | 2017-03-29 17:29:06 -0700 | [diff] [blame] | 256 | } |
| 257 | |
Jeff Gaston | 193f2fb | 2017-06-12 15:00:12 -0700 | [diff] [blame] | 258 | commandDescription := rawCommand |
| 259 | |
Jeff Gaston | efc1b41 | 2017-03-29 17:29:06 -0700 | [diff] [blame] | 260 | cmd := exec.Command("bash", "-c", rawCommand) |
| 261 | cmd.Stdin = os.Stdin |
| 262 | cmd.Stdout = os.Stdout |
| 263 | cmd.Stderr = os.Stderr |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 264 | |
| 265 | if command.GetChdir() { |
| 266 | cmd.Dir = tempDir |
Colin Cross | c590ec4 | 2021-03-11 17:20:02 -0800 | [diff] [blame] | 267 | path := os.Getenv("PATH") |
| 268 | absPath, err := makeAbsPathEnv(path) |
| 269 | if err != nil { |
| 270 | return "", err |
| 271 | } |
| 272 | err = os.Setenv("PATH", absPath) |
| 273 | if err != nil { |
| 274 | return "", fmt.Errorf("Failed to update PATH: %w", err) |
| 275 | } |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 276 | } |
Jeff Gaston | efc1b41 | 2017-03-29 17:29:06 -0700 | [diff] [blame] | 277 | err = cmd.Run() |
Jeff Gaston | 193f2fb | 2017-06-12 15:00:12 -0700 | [diff] [blame] | 278 | |
Colin Cross | fc2d842 | 2021-04-15 18:36:42 -0700 | [diff] [blame^] | 279 | if err != nil { |
| 280 | // The command failed, do a best effort copy of output files out of the sandbox. This is |
| 281 | // especially useful for linters with baselines that print an error message on failure |
| 282 | // with a command to copy the output lint errors to the new baseline. Use a copy instead of |
| 283 | // a move to leave the sandbox intact for manual inspection |
| 284 | copyFiles(command.CopyAfter, tempDir, "", true) |
| 285 | } |
| 286 | |
Jeff Gaston | efc1b41 | 2017-03-29 17:29:06 -0700 | [diff] [blame] | 287 | if exit, ok := err.(*exec.ExitError); ok && !exit.Success() { |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 288 | return "", fmt.Errorf("sbox command failed with err:\n%s\n%w\n", commandDescription, err) |
Jeff Gaston | efc1b41 | 2017-03-29 17:29:06 -0700 | [diff] [blame] | 289 | } else if err != nil { |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 290 | return "", err |
Jeff Gaston | efc1b41 | 2017-03-29 17:29:06 -0700 | [diff] [blame] | 291 | } |
| 292 | |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 293 | missingOutputErrors := validateOutputFiles(command.CopyAfter, tempDir) |
| 294 | |
Jeff Gaston | 90cfb09 | 2017-09-26 16:46:10 -0700 | [diff] [blame] | 295 | if len(missingOutputErrors) > 0 { |
| 296 | // find all created files for making a more informative error message |
| 297 | createdFiles := findAllFilesUnder(tempDir) |
| 298 | |
| 299 | // build error message |
| 300 | errorMessage := "mismatch between declared and actual outputs\n" |
| 301 | errorMessage += "in sbox command(" + commandDescription + ")\n\n" |
| 302 | errorMessage += "in sandbox " + tempDir + ",\n" |
| 303 | errorMessage += fmt.Sprintf("failed to create %v files:\n", len(missingOutputErrors)) |
| 304 | for _, missingOutputError := range missingOutputErrors { |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 305 | errorMessage += " " + missingOutputError.Error() + "\n" |
Jeff Gaston | 90cfb09 | 2017-09-26 16:46:10 -0700 | [diff] [blame] | 306 | } |
| 307 | if len(createdFiles) < 1 { |
| 308 | errorMessage += "created 0 files." |
| 309 | } else { |
| 310 | errorMessage += fmt.Sprintf("did create %v files:\n", len(createdFiles)) |
| 311 | creationMessages := createdFiles |
| 312 | maxNumCreationLines := 10 |
| 313 | if len(creationMessages) > maxNumCreationLines { |
| 314 | creationMessages = creationMessages[:maxNumCreationLines] |
| 315 | creationMessages = append(creationMessages, fmt.Sprintf("...%v more", len(createdFiles)-maxNumCreationLines)) |
| 316 | } |
| 317 | for _, creationMessage := range creationMessages { |
| 318 | errorMessage += " " + creationMessage + "\n" |
| 319 | } |
| 320 | } |
| 321 | |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 322 | return "", errors.New(errorMessage) |
Jeff Gaston | f49082a | 2017-06-07 13:22:22 -0700 | [diff] [blame] | 323 | } |
| 324 | // the created files match the declared files; now move them |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 325 | err = moveFiles(command.CopyAfter, tempDir, "") |
| 326 | |
| 327 | return depFile, nil |
| 328 | } |
| 329 | |
| 330 | // makeOutputDirs creates directories in the sandbox dir for every file that has a rule to be copied |
| 331 | // out of the sandbox. This emulate's Ninja's behavior of creating directories for output files |
| 332 | // so that the tools don't have to. |
| 333 | func makeOutputDirs(copies []*sbox_proto.Copy, sandboxDir string) error { |
| 334 | for _, copyPair := range copies { |
| 335 | dir := joinPath(sandboxDir, filepath.Dir(copyPair.GetFrom())) |
| 336 | err := os.MkdirAll(dir, 0777) |
| 337 | if err != nil { |
| 338 | return err |
Jeff Gaston | 193f2fb | 2017-06-12 15:00:12 -0700 | [diff] [blame] | 339 | } |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 340 | } |
| 341 | return nil |
| 342 | } |
| 343 | |
| 344 | // validateOutputFiles verifies that all files that have a rule to be copied out of the sandbox |
| 345 | // were created by the command. |
| 346 | func validateOutputFiles(copies []*sbox_proto.Copy, sandboxDir string) []error { |
| 347 | var missingOutputErrors []error |
| 348 | for _, copyPair := range copies { |
| 349 | fromPath := joinPath(sandboxDir, copyPair.GetFrom()) |
| 350 | fileInfo, err := os.Stat(fromPath) |
| 351 | if err != nil { |
| 352 | missingOutputErrors = append(missingOutputErrors, fmt.Errorf("%s: does not exist", fromPath)) |
| 353 | continue |
| 354 | } |
| 355 | if fileInfo.IsDir() { |
| 356 | missingOutputErrors = append(missingOutputErrors, fmt.Errorf("%s: not a file", fromPath)) |
| 357 | } |
| 358 | } |
| 359 | return missingOutputErrors |
| 360 | } |
| 361 | |
Colin Cross | fc2d842 | 2021-04-15 18:36:42 -0700 | [diff] [blame^] | 362 | // copyFiles copies files in or out of the sandbox. If allowFromNotExists is true then errors |
| 363 | // caused by a from path not existing are ignored. |
| 364 | func copyFiles(copies []*sbox_proto.Copy, fromDir, toDir string, allowFromNotExists bool) error { |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 365 | for _, copyPair := range copies { |
| 366 | fromPath := joinPath(fromDir, copyPair.GetFrom()) |
| 367 | toPath := joinPath(toDir, copyPair.GetTo()) |
Colin Cross | fc2d842 | 2021-04-15 18:36:42 -0700 | [diff] [blame^] | 368 | err := copyOneFile(fromPath, toPath, copyPair.GetExecutable(), allowFromNotExists) |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 369 | if err != nil { |
| 370 | return fmt.Errorf("error copying %q to %q: %w", fromPath, toPath, err) |
| 371 | } |
| 372 | } |
| 373 | return nil |
| 374 | } |
| 375 | |
Colin Cross | fc2d842 | 2021-04-15 18:36:42 -0700 | [diff] [blame^] | 376 | // copyOneFile copies a file and its permissions. If forceExecutable is true it adds u+x to the |
| 377 | // permissions. If allowFromNotExists is true it returns nil if the from path doesn't exist. |
| 378 | func copyOneFile(from string, to string, forceExecutable, allowFromNotExists bool) error { |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 379 | err := os.MkdirAll(filepath.Dir(to), 0777) |
| 380 | if err != nil { |
| 381 | return err |
| 382 | } |
| 383 | |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 384 | stat, err := os.Stat(from) |
| 385 | if err != nil { |
Colin Cross | fc2d842 | 2021-04-15 18:36:42 -0700 | [diff] [blame^] | 386 | if os.IsNotExist(err) && allowFromNotExists { |
| 387 | return nil |
| 388 | } |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 389 | return err |
| 390 | } |
| 391 | |
| 392 | perm := stat.Mode() |
Colin Cross | fc2d842 | 2021-04-15 18:36:42 -0700 | [diff] [blame^] | 393 | if forceExecutable { |
Colin Cross | 859dfd9 | 2020-11-30 20:12:47 -0800 | [diff] [blame] | 394 | perm = perm | 0100 // u+x |
| 395 | } |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 396 | |
| 397 | in, err := os.Open(from) |
| 398 | if err != nil { |
| 399 | return err |
| 400 | } |
| 401 | defer in.Close() |
| 402 | |
Colin Cross | 607c0b7 | 2021-03-31 12:54:06 -0700 | [diff] [blame] | 403 | // Remove the target before copying. In most cases the file won't exist, but if there are |
| 404 | // duplicate copy rules for a file and the source file was read-only the second copy could |
| 405 | // fail. |
| 406 | err = os.Remove(to) |
| 407 | if err != nil && !os.IsNotExist(err) { |
| 408 | return err |
| 409 | } |
| 410 | |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 411 | out, err := os.Create(to) |
| 412 | if err != nil { |
| 413 | return err |
| 414 | } |
| 415 | defer func() { |
| 416 | out.Close() |
| 417 | if err != nil { |
| 418 | os.Remove(to) |
| 419 | } |
| 420 | }() |
| 421 | |
| 422 | _, err = io.Copy(out, in) |
| 423 | if err != nil { |
| 424 | return err |
| 425 | } |
| 426 | |
| 427 | if err = out.Close(); err != nil { |
| 428 | return err |
| 429 | } |
| 430 | |
| 431 | if err = os.Chmod(to, perm); err != nil { |
| 432 | return err |
| 433 | } |
| 434 | |
| 435 | return nil |
| 436 | } |
| 437 | |
Colin Cross | e55bd42 | 2021-03-23 13:44:30 -0700 | [diff] [blame] | 438 | // copyRspFiles copies rsp files into the sandbox with path mappings, and also copies the files |
| 439 | // listed into the sandbox. |
| 440 | func copyRspFiles(rspFiles []*sbox_proto.RspFile, toDir, toDirInSandbox string) error { |
| 441 | for _, rspFile := range rspFiles { |
| 442 | err := copyOneRspFile(rspFile, toDir, toDirInSandbox) |
| 443 | if err != nil { |
| 444 | return err |
| 445 | } |
| 446 | } |
| 447 | return nil |
| 448 | } |
| 449 | |
| 450 | // copyOneRspFiles copies an rsp file into the sandbox with path mappings, and also copies the files |
| 451 | // listed into the sandbox. |
| 452 | func copyOneRspFile(rspFile *sbox_proto.RspFile, toDir, toDirInSandbox string) error { |
| 453 | in, err := os.Open(rspFile.GetFile()) |
| 454 | if err != nil { |
| 455 | return err |
| 456 | } |
| 457 | defer in.Close() |
| 458 | |
| 459 | files, err := response.ReadRspFile(in) |
| 460 | if err != nil { |
| 461 | return err |
| 462 | } |
| 463 | |
| 464 | for i, from := range files { |
| 465 | // Convert the real path of the input file into the path inside the sandbox using the |
| 466 | // path mappings. |
| 467 | to := applyPathMappings(rspFile.PathMappings, from) |
| 468 | |
| 469 | // Copy the file into the sandbox. |
Colin Cross | fc2d842 | 2021-04-15 18:36:42 -0700 | [diff] [blame^] | 470 | err := copyOneFile(from, joinPath(toDir, to), false, false) |
Colin Cross | e55bd42 | 2021-03-23 13:44:30 -0700 | [diff] [blame] | 471 | if err != nil { |
| 472 | return err |
| 473 | } |
| 474 | |
| 475 | // Rewrite the name in the list of files to be relative to the sandbox directory. |
| 476 | files[i] = joinPath(toDirInSandbox, to) |
| 477 | } |
| 478 | |
| 479 | // Convert the real path of the rsp file into the path inside the sandbox using the path |
| 480 | // mappings. |
| 481 | outRspFile := joinPath(toDir, applyPathMappings(rspFile.PathMappings, rspFile.GetFile())) |
| 482 | |
| 483 | err = os.MkdirAll(filepath.Dir(outRspFile), 0777) |
| 484 | if err != nil { |
| 485 | return err |
| 486 | } |
| 487 | |
| 488 | out, err := os.Create(outRspFile) |
| 489 | if err != nil { |
| 490 | return err |
| 491 | } |
| 492 | defer out.Close() |
| 493 | |
| 494 | // Write the rsp file with converted paths into the sandbox. |
| 495 | err = response.WriteRspFile(out, files) |
| 496 | if err != nil { |
| 497 | return err |
| 498 | } |
| 499 | |
| 500 | return nil |
| 501 | } |
| 502 | |
| 503 | // applyPathMappings takes a list of path mappings and a path, and returns the path with the first |
| 504 | // matching path mapping applied. If the path does not match any of the path mappings then it is |
| 505 | // returned unmodified. |
| 506 | func applyPathMappings(pathMappings []*sbox_proto.PathMapping, path string) string { |
| 507 | for _, mapping := range pathMappings { |
| 508 | if strings.HasPrefix(path, mapping.GetFrom()+"/") { |
| 509 | return joinPath(mapping.GetTo()+"/", strings.TrimPrefix(path, mapping.GetFrom()+"/")) |
| 510 | } |
| 511 | } |
| 512 | return path |
| 513 | } |
| 514 | |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 515 | // moveFiles moves files specified by a set of copy rules. It uses os.Rename, so it is restricted |
| 516 | // to moving files where the source and destination are in the same filesystem. This is OK for |
| 517 | // sbox because the temporary directory is inside the out directory. It updates the timestamp |
| 518 | // of the new file. |
| 519 | func moveFiles(copies []*sbox_proto.Copy, fromDir, toDir string) error { |
| 520 | for _, copyPair := range copies { |
| 521 | fromPath := joinPath(fromDir, copyPair.GetFrom()) |
| 522 | toPath := joinPath(toDir, copyPair.GetTo()) |
| 523 | err := os.MkdirAll(filepath.Dir(toPath), 0777) |
| 524 | if err != nil { |
| 525 | return err |
| 526 | } |
| 527 | |
| 528 | err = os.Rename(fromPath, toPath) |
Jeff Gaston | 8a88db5 | 2017-11-06 13:33:14 -0800 | [diff] [blame] | 529 | if err != nil { |
| 530 | return err |
| 531 | } |
Colin Cross | d1c1e6f | 2019-03-29 13:54:39 -0700 | [diff] [blame] | 532 | |
| 533 | // Update the timestamp of the output file in case the tool wrote an old timestamp (for example, tar can extract |
| 534 | // files with old timestamps). |
| 535 | now := time.Now() |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 536 | err = os.Chtimes(toPath, now, now) |
Jeff Gaston | efc1b41 | 2017-03-29 17:29:06 -0700 | [diff] [blame] | 537 | if err != nil { |
| 538 | return err |
| 539 | } |
| 540 | } |
Jeff Gaston | efc1b41 | 2017-03-29 17:29:06 -0700 | [diff] [blame] | 541 | return nil |
| 542 | } |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 543 | |
| 544 | // Rewrite one or more depfiles so that it doesn't include the (randomized) sandbox directory |
| 545 | // to an output file. |
| 546 | func rewriteDepFiles(ins []string, out string) error { |
| 547 | var mergedDeps []string |
| 548 | for _, in := range ins { |
| 549 | data, err := ioutil.ReadFile(in) |
| 550 | if err != nil { |
| 551 | return err |
| 552 | } |
| 553 | |
| 554 | deps, err := makedeps.Parse(in, bytes.NewBuffer(data)) |
| 555 | if err != nil { |
| 556 | return err |
| 557 | } |
| 558 | mergedDeps = append(mergedDeps, deps.Inputs...) |
| 559 | } |
| 560 | |
| 561 | deps := makedeps.Deps{ |
| 562 | // Ninja doesn't care what the output file is, so we can use any string here. |
| 563 | Output: "outputfile", |
| 564 | Inputs: mergedDeps, |
| 565 | } |
| 566 | |
| 567 | // Make the directory for the output depfile in case it is in a different directory |
| 568 | // than any of the output files. |
| 569 | outDir := filepath.Dir(out) |
| 570 | err := os.MkdirAll(outDir, 0777) |
| 571 | if err != nil { |
| 572 | return fmt.Errorf("failed to create %q: %w", outDir, err) |
| 573 | } |
| 574 | |
| 575 | return ioutil.WriteFile(out, deps.Print(), 0666) |
| 576 | } |
| 577 | |
| 578 | // joinPath wraps filepath.Join but returns file without appending to dir if file is |
| 579 | // absolute. |
| 580 | func joinPath(dir, file string) string { |
| 581 | if filepath.IsAbs(file) { |
| 582 | return file |
| 583 | } |
| 584 | return filepath.Join(dir, file) |
| 585 | } |
Colin Cross | c590ec4 | 2021-03-11 17:20:02 -0800 | [diff] [blame] | 586 | |
| 587 | func makeAbsPathEnv(pathEnv string) (string, error) { |
| 588 | pathEnvElements := filepath.SplitList(pathEnv) |
| 589 | for i, p := range pathEnvElements { |
| 590 | if !filepath.IsAbs(p) { |
| 591 | absPath, err := filepath.Abs(p) |
| 592 | if err != nil { |
| 593 | return "", fmt.Errorf("failed to make PATH entry %q absolute: %w", p, err) |
| 594 | } |
| 595 | pathEnvElements[i] = absPath |
| 596 | } |
| 597 | } |
| 598 | return strings.Join(pathEnvElements, string(filepath.ListSeparator)), nil |
| 599 | } |