aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/tools/android/java/com/google/devtools/build/android/ScopedTemporaryDirectory.java
blob: 9c3f5ceb1193e02613c5e39281dd916b10febbd7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Copyright 2016 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.devtools.build.android;

import static java.nio.file.attribute.PosixFilePermission.OWNER_EXECUTE;
import static java.nio.file.attribute.PosixFilePermission.OWNER_READ;
import static java.nio.file.attribute.PosixFilePermission.OWNER_WRITE;

import java.io.Closeable;
import java.io.IOException;
import java.nio.file.FileStore;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.DosFileAttributeView;
import java.nio.file.attribute.PosixFileAttributeView;
import java.util.EnumSet;

/**
 * Creates a temporary directory that will be deleted once a scope closes. NOTE: If an error occurs
 * during deletion, it will just stop rather than try and continue.
 */
final class ScopedTemporaryDirectory extends SimpleFileVisitor<Path> implements Closeable {

  private static final boolean IS_WINDOWS = System.getProperty("os.name").startsWith("Windows");

  private final Path path;

  public ScopedTemporaryDirectory(String prefix) throws IOException {
    this.path = Files.createTempDirectory(prefix);
  }

  public Path getPath() {
    return this.path;
  }

  public Path subDirectoryOf(String... directories) throws IOException {
    Path sub = this.path;
    for (String directory : directories) {
      sub = sub.resolve(directory);
    }
    return Files.createDirectories(sub);
  }

  private void makeWritable(Path file) throws IOException {
    FileStore fileStore = Files.getFileStore(file);
    if (IS_WINDOWS && fileStore.supportsFileAttributeView(DosFileAttributeView.class)) {
      DosFileAttributeView dosAttribs =
          Files.getFileAttributeView(file, DosFileAttributeView.class);
      if (dosAttribs != null) {
        dosAttribs.setReadOnly(false);
      }
    } else if (fileStore.supportsFileAttributeView(PosixFileAttributeView.class)) {
      PosixFileAttributeView posixAttribs =
          Files.getFileAttributeView(file, PosixFileAttributeView.class);
      if (posixAttribs != null) {
        posixAttribs.setPermissions(EnumSet.of(OWNER_READ, OWNER_WRITE, OWNER_EXECUTE));
      }
    }
  }

  @Override
  public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
    if (IS_WINDOWS) {
      makeWritable(dir);
    }
    return FileVisitResult.CONTINUE;
  }

  @Override
  public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
    if (IS_WINDOWS) {
      makeWritable(file);
    }
    Files.delete(file);
    return FileVisitResult.CONTINUE;
  }

  @Override
  public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
    Files.delete(dir);
    return FileVisitResult.CONTINUE;
  }

  @Override
  public void close() throws IOException {
    Files.walkFileTree(path, this);
  }
}