2424
2525package io .jenkins .plugins .file_parameters ;
2626
27- import edu .umd .cs .findbugs .annotations .SuppressFBWarnings ;
2827import hudson .EnvVars ;
28+ import hudson .Extension ;
2929import hudson .FilePath ;
3030import hudson .Launcher ;
31- import hudson .Util ;
31+ import hudson .model .ParametersAction ;
32+ import hudson .model .Queue ;
3233import hudson .model .Run ;
3334import hudson .model .TaskListener ;
35+ import hudson .model .queue .QueueListener ;
3436import java .io .File ;
3537import java .io .IOException ;
3638import java .io .InputStream ;
3739import java .nio .file .Files ;
38-
40+ import java .util .List ;
41+ import java .util .logging .Level ;
42+ import java .util .logging .Logger ;
43+ import jenkins .model .Jenkins ;
3944import org .apache .commons .fileupload2 .core .FileItem ;
4045import org .apache .commons .io .FileUtils ;
4146import org .jenkinsci .plugins .workflow .flow .FlowExecutionOwner ;
4449
4550public final class StashedFileParameterValue extends AbstractFileParameterValue {
4651
52+ private static final Logger LOGGER = Logger .getLogger (StashedFileParameterValue .class .getName ());
53+
4754 private static final long serialVersionUID = 1L ;
4855
49- @ SuppressFBWarnings (value = "SE_TRANSIENT_FIELD_NOT_RESTORED" , justification = "Doesn't make sense to persist it" )
50- private transient File tmp ;
51-
56+ private String tmpFile ;
57+
5258 @ DataBoundConstructor public StashedFileParameterValue (String name , FileItem file ) throws IOException {
5359 this (name , file .getInputStream ());
5460 setFilename (file .getName ());
@@ -57,14 +63,18 @@ public final class StashedFileParameterValue extends AbstractFileParameterValue
5763
5864 StashedFileParameterValue (String name , InputStream src ) throws IOException {
5965 super (name );
60- tmp = new File (Util .createTempDir (), name );
61- tmp .deleteOnExit ();
66+ File dir = new File (Jenkins .get ().getRootDir (), "stashedFileParameterValueFiles" );
67+ Files .createDirectories (dir .toPath ());
68+ File tmpDir = Files .createTempDirectory (dir .toPath (), null ).toFile ();
69+ File tmp = new File (tmpDir , 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,8 @@ 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 ());
89+ tmpFile = null ;
8090 } catch (IOException e ) {
8191 throw new RuntimeException (e );
8292 }
@@ -88,4 +98,30 @@ public final class StashedFileParameterValue extends AbstractFileParameterValue
8898 return tempDir .child (name );
8999 }
90100
101+ @ Extension
102+ public static class CancelledQueueListener extends QueueListener {
103+
104+ @ Override
105+ public void onLeft (Queue .LeftItem li ) {
106+ if (li .isCancelled ()) {
107+ List <ParametersAction > actions = li .getActions (ParametersAction .class );
108+ actions .forEach (a -> {
109+ a .getAllParameters ().stream ()
110+ .filter (p -> p instanceof StashedFileParameterValue )
111+ .map (p -> (StashedFileParameterValue ) p )
112+ .forEach (p -> {
113+ if (p .tmpFile != null ) {
114+ File tmp = new File (p .tmpFile );
115+ try {
116+ FileUtils .deleteDirectory (tmp .getParentFile ());
117+ } catch (IOException | IllegalArgumentException e ) {
118+ LOGGER .log (Level .WARNING , "Unable to delete temporary file {0} for parameter {1} of task {2}" ,
119+ new Object []{tmp .getAbsolutePath (), p .getName (), li .task .getName ()});
120+ }
121+ }
122+ });
123+ });
124+ }
125+ }
126+ }
91127}
0 commit comments