• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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.ondevicepersonalization.services.data.vendor;
18 
19 import com.android.ondevicepersonalization.internal.util.LoggerFactory;
20 
21 import java.io.File;
22 import java.util.ArrayList;
23 import java.util.HashMap;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Set;
27 
28 public class FileUtils {
29     private static final LoggerFactory.Logger sLogger = LoggerFactory.getLogger();
30     private static final String TAG = "FileUtils";
FileUtils()31     private FileUtils() {}
32 
33     /**
34      * Deletes all files from the directory that no longer
35      * exist in the given keySet or are not the most recent version.
36      */
cleanUpFilesDir(Set<String> keySet, File dir)37     public static void cleanUpFilesDir(Set<String> keySet, File dir) {
38         // Delete any non-recent files with same key or non-existent key.
39         List<File> filesToDelete = new ArrayList<>();
40         Map<String, File> filesSeen = new HashMap<>();
41         if (dir.isDirectory()) {
42             for (File f : dir.listFiles()) {
43                 try {
44                     String[] fileNameList = f.getName().split("_");
45                     long timestamp = Long.parseLong(fileNameList[1]);
46                     String fKey = fileNameList[0];
47 
48                     // Key no longer exists in DB. Mark for deletion
49                     if (!keySet.contains(fKey)) {
50                         filesToDelete.add(f);
51                     }
52 
53                     // If duplicate key, mark oldest key for deletion
54                     if (filesSeen.containsKey(fKey)) {
55                         File existingFile = filesSeen.get(fKey);
56                         if (timestamp < Long.parseLong(existingFile.getName().split("_")[1])) {
57                             filesToDelete.add(f);
58                         } else {
59                             filesToDelete.add(existingFile);
60                             filesSeen.put(fKey, f);
61                         }
62                     } else {
63                         filesSeen.put(fKey, f);
64                     }
65                 } catch (Exception e) {
66                     // Delete any files that do not match expected format.
67                     sLogger.w(TAG + " :Failed to parse file: " + f.getName(), e);
68                     filesToDelete.add(f);
69                 }
70             }
71         }
72         for (File f : filesToDelete) {
73             f.delete();
74         }
75     }
76 
77     /**
78      * Deletes a directory and all files recursively
79      */
deleteDirectory(File fileOrDirectory)80     public static void deleteDirectory(File fileOrDirectory) {
81         if (fileOrDirectory.isDirectory()) {
82             for (File child : fileOrDirectory.listFiles()) {
83                 deleteDirectory(child);
84             }
85         }
86         fileOrDirectory.delete();
87     }
88 
89 }
90