|
44 | 44 | import java.nio.channels.FileChannel;
|
45 | 45 | import java.nio.charset.StandardCharsets;
|
46 | 46 | import java.nio.file.Files;
|
| 47 | +import java.nio.file.LinkOption; |
47 | 48 | import java.nio.file.NoSuchFileException;
|
48 | 49 | import java.nio.file.Path;
|
49 | 50 | import java.nio.file.Paths;
|
50 | 51 | import java.nio.file.StandardOpenOption;
|
| 52 | +import java.nio.file.attribute.FileAttribute; |
51 | 53 | import java.util.ArrayList;
|
52 | 54 | import java.util.Arrays;
|
| 55 | +import java.util.LinkedList; |
53 | 56 | import java.util.List;
|
54 | 57 | import java.util.concurrent.Executor;
|
55 | 58 |
|
@@ -251,7 +254,42 @@ public void beginPart(final HeaderMap headers) {
|
251 | 254 | if (fileName != null && fileSizeThreshold == 0) {
|
252 | 255 | try {
|
253 | 256 | if (tempFileLocation != null) {
|
254 |
| - file = Files.createTempFile(tempFileLocation, "undertow", "upload"); |
| 257 | + //Files impl is buggy, hence zero len |
| 258 | + final FileAttribute[] emptyFA = new FileAttribute[] {}; |
| 259 | + final LinkOption[] emptyLO = new LinkOption[] {}; |
| 260 | + final Path normalized = tempFileLocation.normalize(); |
| 261 | + if (!Files.exists(normalized)) { |
| 262 | + final int pathElementsCount = normalized.getNameCount(); |
| 263 | + Path tmp = normalized; |
| 264 | + LinkedList<Path> dirsToGuard = new LinkedList<>(); |
| 265 | + for(int i=0;i<pathElementsCount;i++) { |
| 266 | + if(Files.exists(tmp, emptyLO)) { |
| 267 | + if(!Files.isDirectory(tmp, emptyLO)) { |
| 268 | + //First existing element in path is a file, |
| 269 | + //this will cause java.nio.file.FileSystemException |
| 270 | + throw UndertowMessages.MESSAGES.pathElementIsRegularFile(tmp); |
| 271 | + } |
| 272 | + break; |
| 273 | + } else { |
| 274 | + dirsToGuard.addFirst(tmp); |
| 275 | + tmp = tmp.getParent(); |
| 276 | + } |
| 277 | + } |
| 278 | + try { |
| 279 | + Files.createDirectories(normalized, emptyFA); |
| 280 | + } finally { |
| 281 | + for (Path p : dirsToGuard) { |
| 282 | + try { |
| 283 | + p.toFile().deleteOnExit(); |
| 284 | + } catch (Exception e) { |
| 285 | + break; |
| 286 | + } |
| 287 | + } |
| 288 | + } |
| 289 | + } else if (!Files.isDirectory(normalized, emptyLO)) { |
| 290 | + throw new IOException(UndertowMessages.MESSAGES.pathNotADirectory(normalized)); |
| 291 | + } |
| 292 | + file = Files.createTempFile(normalized, "undertow", "upload"); |
255 | 293 | } else {
|
256 | 294 | file = Files.createTempFile("undertow", "upload");
|
257 | 295 | }
|
|
0 commit comments