1 /*
2  * Copyright (C) 2022 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.providers.contacts.util;
18 
19 import android.util.Log;
20 
21 import java.io.File;
22 import java.io.IOException;
23 
24 public final class FileUtilities {
25 
26     public static final String TAG = FileUtilities.class.getSimpleName();
27     public static final String INVALID_CALL_LOG_PATH_EXCEPTION_MESSAGE =
28             "Invalid [Call Log] path. Cannot operate on file:";
29 
30     /**
31      * Checks, whether the child directory is the same as, or a sub-directory of the base
32      * directory.
33      */
isSameOrSubDirectory(File base, File child)34     public static boolean isSameOrSubDirectory(File base, File child) {
35         try {
36             File basePath = base.getCanonicalFile();
37             File currPath = child.getCanonicalFile();
38             while (currPath != null) {
39                 if (basePath.equals(currPath)) {
40                     return true;
41                 }
42                 currPath = currPath.getParentFile(); // pops sub-dir
43             }
44             return false;
45         } catch (IOException ex) {
46             Log.e(TAG, "Error while accessing file", ex);
47             return false;
48         }
49     }
50 }
51