summaryrefslogtreecommitdiff
path: root/java/com/google/android/libraries/mobiledatadownload/file/openers/RecursiveDeleteOpener.java
blob: 237def1a013542fc6e9aa33490f6a8409193b5db (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
103
104
105
106
107
108
109
110
111
112
113
114
/*
 * Copyright 2022 Google LLC
 *
 * 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.android.libraries.mobiledatadownload.file.openers;

import android.net.Uri;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.system.Os;
import android.system.OsConstants;
import android.system.StructStat;
import com.google.android.libraries.mobiledatadownload.file.OpenContext;
import com.google.android.libraries.mobiledatadownload.file.Opener;
import com.google.android.libraries.mobiledatadownload.file.SynchronousFileStorage;
import com.google.android.libraries.mobiledatadownload.file.common.internal.Exceptions;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Deletes the file or directory at the given URI recursively. This behaves similarly to {@link
 * SynchronousFileStorage#deleteRecursively} except as described in the following paragraph.
 *
 * <p>If an IO exception occurs attempting to read, open, or delete any file under the given
 * directory, this method skips that file and continues. All such exceptions are collected and,
 * after attempting to delete all files, an {@code IOException} is thrown containing those
 * exceptions as {@linkplain Throwable#getSuppressed() suppressed exceptions}.
 *
 * <p>WARNING: this opener suffers from the following caveats and should be used with caution:
 *
 * <ul>
 *   <li>Directory tree traversal is not an atomic operation
 * </ul>
 *
 * <p>Usage: <code>
 * storage.open(uri, RecursiveDeleteOpener.create());
 * </code>
 */
public final class RecursiveDeleteOpener implements Opener<Void> {
  private boolean noFollowLinks;

  public static RecursiveDeleteOpener create() {
    return new RecursiveDeleteOpener();
  }

  @CanIgnoreReturnValue
  public RecursiveDeleteOpener withNoFollowLinks() {
    this.noFollowLinks = true;
    return this;
  }

  @Override
  public Void open(OpenContext openContext) throws IOException {
    List<IOException> exceptions = new ArrayList<>();
    deleteRecursively(openContext.storage(), openContext.encodedUri(), exceptions);
    if (!exceptions.isEmpty()) {
      throw Exceptions.combinedIOException("Failed to delete one or more files", exceptions);
    }

    return null; // for Void return type
  }

  private void deleteRecursively(
      SynchronousFileStorage storage, Uri uri, List<IOException> exceptions) {
    ReadFileOpener readFileOpener = ReadFileOpener.create().withShortCircuit();
    try {
      if (storage.isDirectory(uri)) {
        if (!noFollowLinks || !isSymlink(uri, storage, readFileOpener)) {
          for (Uri child : storage.children(uri)) {
            deleteRecursively(storage, child, exceptions);
          }
        }
        storage.deleteDirectory(uri);
      } else {
        storage.deleteFile(uri);
      }
    } catch (IOException e) {
      exceptions.add(e);
    }
  }

  private static boolean isSymlink(
      Uri uri, SynchronousFileStorage storage, ReadFileOpener readFileOpener) {
    if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
      try {
        File file = storage.open(uri, readFileOpener);
        if (file == null || !file.exists()) {
          return false;
        }
        StructStat stat = Os.lstat(file.getAbsolutePath());
        return (stat.st_mode & OsConstants.S_IFLNK) != 0;
      } catch (Exception e) {
        // NOTE: this should be ErrnoException, but we're forced to catch Exception to avoid
        // breaking lower sdk levels (exceptions aren't stripped from dead code blocks).
        return false;
      }
    }
    return false;
  }
}