• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2022 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 package org.chromium.net;
6 
7 import org.chromium.base.Log;
8 
9 import java.io.File;
10 
11 /**
12  * Simpler fork of the org.chromium.base class that doesn't rely on java.util.function.Function
13  * (unavailable on Android <= M).
14  */
15 public class FileUtils {
16     private static final String TAG = "FileUtils";
17 
recursivelyDeleteFile(File currentFile)18     public static boolean recursivelyDeleteFile(File currentFile) {
19         if (!currentFile.exists()) {
20             // This file could be a broken symlink, so try to delete. If we don't delete a broken
21             // symlink, the directory containing it cannot be deleted.
22             currentFile.delete();
23             return true;
24         }
25 
26         if (currentFile.isDirectory()) {
27             File[] files = currentFile.listFiles();
28             if (files != null) {
29                 for (var file : files) {
30                     recursivelyDeleteFile(file);
31                 }
32             }
33         }
34 
35         boolean ret = currentFile.delete();
36         if (!ret) {
37             Log.e(TAG, "Failed to delete: %s", currentFile);
38         }
39         return ret;
40     }
41 }
42