28
28
import java .io .File ;
29
29
import java .io .IOException ;
30
30
import java .io .InputStreamReader ;
31
- import java .nio .file .FileSystems ;
32
31
import java .nio .file .Files ;
33
32
import java .nio .file .Path ;
34
- import java .nio .file .attribute .FileTime ;
35
- import java .nio .file .attribute .UserPrincipal ;
36
33
import java .time .Duration ;
37
- import java .time .Instant ;
38
- import java .time .temporal .ChronoUnit ;
39
34
import java .util .ArrayList ;
40
35
import java .util .Arrays ;
41
36
import java .util .Formatter ;
@@ -478,9 +473,6 @@ private static Subprocess javaHelper(List<String> vmArgs, Map<String, String> en
478
473
479
474
private static final Set <String > EXECUTABLES_USING_ARGFILES = Set .of ("java" , "java.exe" , "javac" , "javac.exe" );
480
475
481
- // Keep in sync with the {@code catch_files} array in {@code ci/common.jsonnet}.
482
- private static final TemporaryFiles SUBPROCESS_ARGFILES = new TemporaryFiles (SubprocessUtil .class .getName () + "." , ".argfile" );
483
-
484
476
/**
485
477
* Creates an argfile for {@code command} if {@code command.get(0)} denotes an
486
478
* {@linkplain #EXECUTABLES_USING_ARGFILES executable supporting argfiles}.
@@ -491,15 +483,16 @@ private static Subprocess javaHelper(List<String> vmArgs, Map<String, String> en
491
483
public static Path makeArgfile (List <String > command ) {
492
484
File exe = new File (command .get (0 ));
493
485
if (EXECUTABLES_USING_ARGFILES .contains (exe .getName ())) {
494
- Path argfile = SUBPROCESS_ARGFILES .newFile ();
495
486
expandArgFileArgs (command );
496
487
try {
488
+ // Keep in sync with the {@code catch_files} array in {@code ci/common.jsonnet}.
489
+ Path argfile = Files .createTempFile (SubprocessUtil .class .getName () + "." , ".argfile" );
497
490
String content = expandArgFileArgs (command ).stream ().map (SubprocessUtil ::quoteArgfileArgument ).collect (Collectors .joining ("\n " ));
498
491
Files .writeString (argfile , "# " + content );
492
+ return argfile ;
499
493
} catch (IOException e ) {
500
494
throw new AssertionError (e );
501
495
}
502
- return argfile ;
503
496
}
504
497
return null ;
505
498
}
@@ -598,76 +591,4 @@ private static int findMainClassIndex(List<String> commandLine) {
598
591
}
599
592
throw new InternalError ();
600
593
}
601
-
602
- /**
603
- * Manages temporary files with a given prefix and suffix. When new files are created, temporary
604
- * files older than 2 hours are deleted.
605
- */
606
- public static class TemporaryFiles {
607
- private final Path dir ;
608
- private final FileTime maxAge ;
609
- private final String prefix ;
610
- private final String suffix ;
611
- private final UserPrincipal user ;
612
-
613
- public TemporaryFiles (String prefix , String suffix ) {
614
- this .dir = Path .of (System .getProperty ("java.io.tmpdir" ));
615
- this .maxAge = DEBUG ? //
616
- FileTime .from (Instant .now ().minus (5 , ChronoUnit .SECONDS )) : //
617
- FileTime .from (Instant .now ().minus (2 , ChronoUnit .HOURS ));
618
- this .prefix = prefix ;
619
- this .suffix = suffix ;
620
- try {
621
- this .user = FileSystems .getDefault ().getUserPrincipalLookupService ().lookupPrincipalByName (System .getProperty ("user.name" ));
622
- } catch (IOException e ) {
623
- throw new AssertionError (e );
624
- }
625
- }
626
-
627
- /**
628
- * Creates a new temporary file after {@linkplain #removeOldFiles() deleting} old temporary
629
- * files.
630
- */
631
- Path newFile () {
632
- removeOldFiles ();
633
- try {
634
- Path path = Files .createTempFile (dir , prefix , suffix ).toAbsolutePath ();
635
- if (DEBUG ) {
636
- System .out .println ("created " + path );
637
- }
638
- return path ;
639
- } catch (IOException e ) {
640
- throw new AssertionError (e );
641
- }
642
- }
643
-
644
- /**
645
- * Deletes argfiles in {@link #dir} that start with {@link #prefix} and end with
646
- * {@link #suffix} whose last modification time is before {@link #maxAge}.
647
- */
648
- private void removeOldFiles () {
649
- try {
650
- Files .list (dir ).filter (this ::match ).forEach (p -> {
651
- try {
652
- if (Files .getOwner (p ).equals (user ) && Files .getLastModifiedTime (p ).compareTo (maxAge ) < 0 ) {
653
- Files .delete (p );
654
- if (DEBUG ) {
655
- System .out .println ("deleted " + p );
656
- }
657
- return ;
658
- }
659
- } catch (IOException e ) {
660
- System .err .printf ("Error deleting %s: %e%n" , p , e );
661
- }
662
- });
663
- } catch (IOException e ) {
664
- System .err .printf ("Error listing files in %s: %e%n" , dir , e );
665
- }
666
- }
667
-
668
- private boolean match (Path p ) {
669
- String fileName = p .getFileName ().toString ();
670
- return fileName .startsWith (prefix ) && fileName .endsWith (suffix );
671
- }
672
- }
673
594
}
0 commit comments