Enrico Granata | 9c1f272 | 2017-09-21 17:19:04 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.car.test.utils; |
| 18 | |
| 19 | import android.annotation.Nullable; |
| 20 | import android.os.SystemClock; |
| 21 | import java.io.File; |
| 22 | import java.io.IOException; |
| 23 | import java.nio.file.FileVisitResult; |
| 24 | import java.nio.file.Files; |
| 25 | import java.nio.file.Path; |
| 26 | import java.nio.file.SimpleFileVisitor; |
| 27 | import java.nio.file.attribute.BasicFileAttributes; |
| 28 | |
| 29 | /** |
| 30 | * A utility class that creates a temporary directory. |
| 31 | * The directory, and any files contained within, are automatically deleted when calling close(). |
| 32 | * |
| 33 | * Example usage: |
| 34 | * |
| 35 | * try (TemporaryDirectory tf = new TemporaryDirectory("myTest")) { |
| 36 | * ... |
| 37 | * } // directory gets deleted here |
| 38 | */ |
| 39 | public class TemporaryDirectory implements AutoCloseable { |
| 40 | private Path mDirectory; |
| 41 | |
| 42 | private static final class DeletingVisitor extends SimpleFileVisitor<Path> { |
| 43 | FileVisitResult consume(Path path) throws IOException { |
| 44 | Files.delete(path); |
| 45 | return FileVisitResult.CONTINUE; |
| 46 | } |
| 47 | |
| 48 | @Override |
| 49 | public FileVisitResult visitFile(Path path, BasicFileAttributes basicFileAttributes) |
| 50 | throws IOException { |
| 51 | return consume(path); |
| 52 | } |
| 53 | |
| 54 | @Override |
| 55 | public FileVisitResult postVisitDirectory(Path path, IOException e) |
| 56 | throws IOException { |
| 57 | return consume(path); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | private static final SimpleFileVisitor<Path> DELETE = new DeletingVisitor(); |
| 62 | |
Enrico Granata | 1690a62 | 2018-01-22 17:34:46 -0800 | [diff] [blame] | 63 | TemporaryDirectory(Path directory) throws IOException { |
| 64 | mDirectory = Files.createDirectory(directory); |
| 65 | } |
| 66 | |
Enrico Granata | 9c1f272 | 2017-09-21 17:19:04 -0700 | [diff] [blame] | 67 | public TemporaryDirectory(@Nullable String prefix) throws IOException { |
| 68 | if (prefix == null) { |
| 69 | prefix = TemporaryDirectory.class.getSimpleName(); |
| 70 | } |
| 71 | prefix += String.valueOf(SystemClock.elapsedRealtimeNanos()); |
| 72 | |
| 73 | mDirectory = Files.createTempDirectory(prefix); |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public void close() throws Exception { |
| 78 | Files.walkFileTree(mDirectory, DELETE); |
| 79 | } |
| 80 | |
| 81 | public File getDirectory() { |
| 82 | return mDirectory.toFile(); |
| 83 | } |
Enrico Granata | 1690a62 | 2018-01-22 17:34:46 -0800 | [diff] [blame] | 84 | |
| 85 | public Path getPath() { return mDirectory; } |
| 86 | |
| 87 | public TemporaryDirectory getSubdirectory(String name) throws IOException { |
| 88 | return new TemporaryDirectory(mDirectory.resolve(name)); |
| 89 | } |
Enrico Granata | 9c1f272 | 2017-09-21 17:19:04 -0700 | [diff] [blame] | 90 | } |