aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/unix/Directories.java
blob: aa2e958040228dbab5bc0fc71b6ddd42115e8f43 (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
// Copyright 2014 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.lib.unix;

import com.google.devtools.build.lib.shell.AbnormalTerminationException;
import com.google.devtools.build.lib.shell.Command;
import com.google.devtools.build.lib.shell.CommandException;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * Provides utility methods for working with directories in a Unix environment.
 */
public final class Directories {

  /**
   * Deletes a file or directory and all contents recursively, like {@code rm -rf file}.
   *
   * <p>If the file argument is a symbolic link, the link will be deleted but not the target of the
   * link. If the argument is a directory, symbolic links within the directory will not be followed.
   * If the argument does not exist, throws a FileNotFoundException.
   *
   * @param file the file or directory to delete
   * @throws FileNotFoundException if the file or directory does not exist
   * @throws IOException if an I/O error occurs
   */
  public static void deleteRecursively(File file) throws IOException {
    deleteRecursivelyImpl(file, true);
  }

  /**
   * Deletes a file or directory and all contents recursively, like {@code rm -rf file}, if it
   * exists.
   *
   * <p>If the file argument is a symbolic link, the link will be deleted but not the target of the
   * link. If the argument is a directory, symbolic links within the directory will not be followed.
   *
   * @param file the file or directory to delete
   * @return {@code true} if the file or directory was deleted by this method; {@code false} if the
   * file or directory could not be deleted because it did not exist
   * @throws IOException if an I/O error occurs
   */
  public static boolean deleteRecursivelyIfExists(File file) throws IOException {
    return deleteRecursivelyImpl(file, false);
  }

  private static boolean deleteRecursivelyImpl(File file, boolean failIfFileDoesNotExist)
      throws IOException {
    if (!file.exists()) {
      if (failIfFileDoesNotExist) {
        throw new FileNotFoundException(file.getPath());
      } else {
        return false;
      }
    }
    String filePath = file.getPath();
    if (!filePath.isEmpty() && filePath.charAt(0) == '-') {
      filePath = "./" + filePath;
    }
    try {
      new Command(new String[] {"/bin/rm", "-rf", filePath}).execute();
    } catch (AbnormalTerminationException e) {
      String message =
          e.getResult().getTerminationStatus() + ": " + new String(e.getResult().getStderr());
      throw new IOException(message, e);
    } catch (CommandException e) {
      throw new IOException(e);
    }
    return true;
  }

  private Directories() {}
}