2626
2727import edu .umd .cs .findbugs .annotations .SuppressFBWarnings ;
2828import hudson .EnvVars ;
29+ import hudson .Extension ;
2930import hudson .FilePath ;
3031import hudson .Launcher ;
3132import hudson .Util ;
33+ import hudson .model .ParametersAction ;
34+ import hudson .model .Queue ;
3235import hudson .model .Run ;
3336import hudson .model .TaskListener ;
37+ import hudson .model .queue .QueueListener ;
3438import java .io .File ;
3539import java .io .IOException ;
3640import java .io .InputStream ;
37- import java .nio .file .Files ;
3841
42+ import java .util .List ;
43+ import java .util .logging .Level ;
44+ import java .util .logging .Logger ;
3945import org .apache .commons .fileupload .FileItem ;
4046import org .apache .commons .io .FileUtils ;
4147import org .jenkinsci .plugins .workflow .flow .FlowExecutionOwner ;
4450
4551public final class StashedFileParameterValue extends AbstractFileParameterValue {
4652
53+ private static final Logger LOGGER = Logger .getLogger (StashedFileParameterValue .class .getName ());
54+
4755 private static final long serialVersionUID = 1L ;
4856
57+ private final String tmpFile ;
58+
4959 @ SuppressFBWarnings (value = "SE_TRANSIENT_FIELD_NOT_RESTORED" , justification = "Doesn't make sense to persist it" )
50- private transient File tmp ;
51-
60+
5261 @ DataBoundConstructor public StashedFileParameterValue (String name , FileItem file ) throws IOException {
5362 this (name , file .getInputStream ());
5463 setFilename (file .getName ());
@@ -57,14 +66,15 @@ public final class StashedFileParameterValue extends AbstractFileParameterValue
5766
5867 StashedFileParameterValue (String name , InputStream src ) throws IOException {
5968 super (name );
60- tmp = new File (Util .createTempDir (), name );
61- tmp .deleteOnExit ();
69+ File tmp = new File (Util .createTempDir (), name );
6270 FileUtils .copyInputStreamToFile (src , tmp );
71+ tmpFile = tmp .getAbsolutePath ();
6372 }
6473
6574 @ Override public void buildEnvironment (Run <?, ?> build , EnvVars env ) {
6675 super .buildEnvironment (build , env );
67- if (tmp != null ) {
76+ File tmp = tmpFile != null ? new File (tmpFile ) : null ;
77+ if (tmp != null && tmp .isFile ()) {
6878 try {
6979 FlowExecutionOwner feo = build instanceof FlowExecutionOwner .Executable ? ((FlowExecutionOwner .Executable ) build ).asFlowExecutionOwner () : null ;
7080 TaskListener listener = feo != null ? feo .getListener () : TaskListener .NULL ;
@@ -75,8 +85,7 @@ public final class StashedFileParameterValue extends AbstractFileParameterValue
7585 throw new RuntimeException ( x );
7686 }
7787 try {
78- Files .deleteIfExists (tmp .toPath ());
79- tmp = null ;
88+ FileUtils .deleteDirectory (tmp .getParentFile ());
8089 } catch (IOException e ) {
8190 throw new RuntimeException (e );
8291 }
@@ -88,4 +97,30 @@ public final class StashedFileParameterValue extends AbstractFileParameterValue
8897 return tempDir .child (name );
8998 }
9099
100+ @ Extension
101+ public static class CancelledQueueListener extends QueueListener {
102+
103+ @ Override
104+ public void onLeft (Queue .LeftItem li ) {
105+ if (li .isCancelled ()) {
106+ List <ParametersAction > actions = li .getActions (ParametersAction .class );
107+ actions .forEach (a -> {
108+ a .getAllParameters ().stream ()
109+ .filter (p -> p instanceof StashedFileParameterValue )
110+ .map (p -> (StashedFileParameterValue ) p )
111+ .forEach (p -> {
112+ if (p .tmpFile != null ) {
113+ File tmp = new File (p .tmpFile );
114+ try {
115+ FileUtils .deleteDirectory (tmp .getParentFile ());
116+ } catch (IOException | IllegalArgumentException e ) {
117+ LOGGER .log (Level .WARNING , "Unable to delete temporary file {0} for parameter {1} of task {2}" ,
118+ new Object []{tmp .getAbsolutePath (), p .getName (), li .task .getName ()});
119+ }
120+ }
121+ });
122+ });
123+ }
124+ }
125+ }
91126}
0 commit comments