1515package com .google .devtools .build .lib .sandbox ;
1616
1717import static com .google .common .base .Preconditions .checkNotNull ;
18- import static com .google .common .base .Preconditions .checkState ;
1918import static com .google .devtools .build .lib .vfs .Dirent .Type .DIRECTORY ;
2019import static com .google .devtools .build .lib .vfs .Dirent .Type .SYMLINK ;
2120
@@ -62,21 +61,6 @@ public final class SandboxHelpers {
6261 private static final GoogleLogger logger = GoogleLogger .forEnclosingClass ();
6362
6463 private static final AtomicBoolean warnedAboutMovesBeingCopies = new AtomicBoolean (false );
65- /**
66- * If true, materialize virtual inputs only inside the sandbox, not the output tree. This flag
67- * exists purely to support rolling this out as the defaut in a controlled manner.
68- */
69- private final boolean delayVirtualInputMaterialization ;
70-
71- /**
72- * Constructs a new collection of helpers.
73- *
74- * @param delayVirtualInputMaterialization whether to materialize virtual inputs only inside the
75- * sandbox
76- */
77- public SandboxHelpers (boolean delayVirtualInputMaterialization ) {
78- this .delayVirtualInputMaterialization = delayVirtualInputMaterialization ;
79- }
8064
8165 /**
8266 * Writes a virtual input file so that the final file is always consistent to all readers.
@@ -365,38 +349,21 @@ public static final class SandboxInputs {
365349 new AtomicInteger ();
366350
367351 private final Map <PathFragment , Path > files ;
368- // Virtual inputs that are not materialized during {@link #processInputFiles}
369- private final Set <VirtualActionInput > virtualInputsWithDelayedMaterialization ;
370- // Virtual inputs that are materialized during {@link #processInputFiles}.
371- private final Map <VirtualActionInput , byte []> materializedVirtualInputs ;
352+ private final Map <VirtualActionInput , byte []> virtualInputs ;
372353 private final Map <PathFragment , PathFragment > symlinks ;
373354
374355 private static final SandboxInputs EMPTY_INPUTS =
375- new SandboxInputs (
376- ImmutableMap .of (), ImmutableSet .of (), ImmutableMap .of (), ImmutableMap .of ());
356+ new SandboxInputs (ImmutableMap .of (), ImmutableMap .of (), ImmutableMap .of ());
377357
378358 public SandboxInputs (
379359 Map <PathFragment , Path > files ,
380- Set <VirtualActionInput > virtualInputsWithDelayedMaterialization ,
381- Map <VirtualActionInput , byte []> materializedVirtualInputs ,
360+ Map <VirtualActionInput , byte []> virtualInputs ,
382361 Map <PathFragment , PathFragment > symlinks ) {
383- checkState (
384- virtualInputsWithDelayedMaterialization .isEmpty () || materializedVirtualInputs .isEmpty (),
385- "Either virtualInputsWithDelayedMaterialization or materializedVirtualInputs should be"
386- + " empty." );
387362 this .files = files ;
388- this .virtualInputsWithDelayedMaterialization = virtualInputsWithDelayedMaterialization ;
389- this .materializedVirtualInputs = materializedVirtualInputs ;
363+ this .virtualInputs = virtualInputs ;
390364 this .symlinks = symlinks ;
391365 }
392366
393- public SandboxInputs (
394- Map <PathFragment , Path > files ,
395- Set <VirtualActionInput > virtualInputsWithDelayedMaterialization ,
396- Map <PathFragment , PathFragment > symlinks ) {
397- this (files , virtualInputsWithDelayedMaterialization , ImmutableMap .of (), symlinks );
398- }
399-
400367 public static SandboxInputs getEmptyInputs () {
401368 return EMPTY_INPUTS ;
402369 }
@@ -425,8 +392,6 @@ public Map<PathFragment, PathFragment> getSymlinks() {
425392 private static byte [] materializeVirtualInput (
426393 VirtualActionInput input , Path execroot , boolean isExecRootSandboxed ) throws IOException {
427394 if (input instanceof EmptyActionInput ) {
428- // TODO(b/150963503): We can turn this into an unreachable code path when the old
429- // !delayVirtualInputMaterialization code path is deleted.
430395 return new byte [0 ];
431396 }
432397
@@ -447,31 +412,8 @@ private static byte[] materializeVirtualInput(
447412 return writeVirtualInputTo (input , outputPath );
448413 }
449414
450- /**
451- * Materializes virtual files inside the sandboxed execroot once it is known.
452- *
453- * <p>These are files that do not have to exist in the execroot: we can materialize them only
454- * inside the sandbox, which means we can create them <i>before</i> we grab the output tree lock
455- * (but assuming we do so inside the sandbox only).
456- *
457- * @param sandboxExecRoot the path to the <i>sandboxed</i> execroot
458- * @return digests of written virtual inputs
459- * @throws IOException if any virtual input cannot be materialized
460- */
461- public ImmutableMap <VirtualActionInput , byte []> materializeVirtualInputs (Path sandboxExecRoot )
462- throws IOException {
463- if (!materializedVirtualInputs .isEmpty ()) {
464- return ImmutableMap .copyOf (materializedVirtualInputs );
465- }
466-
467- ImmutableMap .Builder <VirtualActionInput , byte []> digests =
468- ImmutableMap .builderWithExpectedSize (virtualInputsWithDelayedMaterialization .size ());
469- for (VirtualActionInput input : virtualInputsWithDelayedMaterialization ) {
470- byte [] digest =
471- materializeVirtualInput (input , sandboxExecRoot , /*isExecRootSandboxed=*/ false );
472- digests .put (input , digest );
473- }
474- return digests .buildOrThrow ();
415+ public ImmutableMap <VirtualActionInput , byte []> getVirtualInputDigests () {
416+ return ImmutableMap .copyOf (virtualInputs );
475417 }
476418
477419 /**
@@ -481,19 +423,13 @@ public ImmutableMap<VirtualActionInput, byte[]> materializeVirtualInputs(Path sa
481423 public SandboxInputs limitedCopy (Set <PathFragment > allowed ) {
482424 return new SandboxInputs (
483425 Maps .filterKeys (files , allowed ::contains ),
484- ImmutableSet .of (),
485426 ImmutableMap .of (),
486427 Maps .filterKeys (symlinks , allowed ::contains ));
487428 }
488429
489430 @ Override
490431 public String toString () {
491- return "Files: "
492- + files
493- + "\n VirtualInputs: "
494- + virtualInputsWithDelayedMaterialization
495- + "\n Symlinks: "
496- + symlinks ;
432+ return "Files: " + files + "\n VirtualInputs: " + virtualInputs + "\n Symlinks: " + symlinks ;
497433 }
498434 }
499435
@@ -519,66 +455,37 @@ private static byte[] writeVirtualInputTo(VirtualActionInput input, Path target)
519455 * Returns the inputs of a Spawn as a map of PathFragments relative to an execRoot to paths in the
520456 * host filesystem where the input files can be found.
521457 *
522- * <p>This does not (and must not) write any {@link VirtualActionInput}s found because we do not
523- * yet know where they should be written to. We have a path to an {@code execRoot}, but this path
524- * should be treated as read-only because we may not be holding its lock. The caller should use
525- * {@link SandboxInputs#materializeVirtualInputs(Path)} to later write these inputs when it knows
526- * where they should be written to.
527- *
528458 * @throws IOException if processing symlinks fails
529459 */
530460 public SandboxInputs processInputFiles (Map <PathFragment , ActionInput > inputMap , Path execRoot )
531461 throws IOException {
532462 Map <PathFragment , Path > inputFiles = new TreeMap <>();
533- Set <VirtualActionInput > virtualInputsWithDelayedMaterialization = new HashSet <>();
534463 Map <PathFragment , PathFragment > inputSymlinks = new TreeMap <>();
535- Map <VirtualActionInput , byte []> materializedVirtualInputs = new HashMap <>();
464+ Map <VirtualActionInput , byte []> virtualInputs = new HashMap <>();
536465
537466 for (Map .Entry <PathFragment , ActionInput > e : inputMap .entrySet ()) {
538467 PathFragment pathFragment = e .getKey ();
539468 ActionInput actionInput = e .getValue ();
540469
541- // TODO(b/150963503): Make delayVirtualInputMaterialization the default and remove the
542- // alternate code path.
543- if (delayVirtualInputMaterialization ) {
544- if (actionInput instanceof VirtualActionInput ) {
545- if (actionInput instanceof EmptyActionInput ) {
546- inputFiles .put (pathFragment , null );
547- } else {
548- virtualInputsWithDelayedMaterialization .add ((VirtualActionInput ) actionInput );
549- }
550- } else if (actionInput .isSymlink ()) {
551- Path inputPath = execRoot .getRelative (actionInput .getExecPath ());
552- inputSymlinks .put (pathFragment , inputPath .readSymbolicLink ());
553- } else {
554- Path inputPath = execRoot .getRelative (actionInput .getExecPath ());
555- inputFiles .put (pathFragment , inputPath );
556- }
557- } else {
558- if (actionInput instanceof VirtualActionInput ) {
559- byte [] digest =
560- SandboxInputs .materializeVirtualInput (
561- (VirtualActionInput ) actionInput , execRoot , /* isExecRootSandboxed=*/ true );
562- materializedVirtualInputs .put ((VirtualActionInput ) actionInput , digest );
563- }
470+ if (actionInput instanceof VirtualActionInput ) {
471+ byte [] digest =
472+ SandboxInputs .materializeVirtualInput (
473+ (VirtualActionInput ) actionInput , execRoot , /* isExecRootSandboxed=*/ true );
474+ virtualInputs .put ((VirtualActionInput ) actionInput , digest );
475+ }
564476
565- if (actionInput .isSymlink ()) {
566- Path inputPath = execRoot .getRelative (actionInput .getExecPath ());
567- inputSymlinks .put (pathFragment , inputPath .readSymbolicLink ());
568- } else {
569- Path inputPath =
570- actionInput instanceof EmptyActionInput
571- ? null
572- : execRoot .getRelative (actionInput .getExecPath ());
573- inputFiles .put (pathFragment , inputPath );
574- }
477+ if (actionInput .isSymlink ()) {
478+ Path inputPath = execRoot .getRelative (actionInput .getExecPath ());
479+ inputSymlinks .put (pathFragment , inputPath .readSymbolicLink ());
480+ } else {
481+ Path inputPath =
482+ actionInput instanceof EmptyActionInput
483+ ? null
484+ : execRoot .getRelative (actionInput .getExecPath ());
485+ inputFiles .put (pathFragment , inputPath );
575486 }
576487 }
577- return new SandboxInputs (
578- inputFiles ,
579- virtualInputsWithDelayedMaterialization ,
580- materializedVirtualInputs ,
581- inputSymlinks );
488+ return new SandboxInputs (inputFiles , virtualInputs , inputSymlinks );
582489 }
583490
584491 /** The file and directory outputs of a sandboxed spawn. */
0 commit comments