• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2022 Google LLC
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 package com.google.android.libraries.mobiledatadownload.file.openers;
17 
18 import android.net.Uri;
19 import com.google.android.libraries.mobiledatadownload.file.OpenContext;
20 import com.google.android.libraries.mobiledatadownload.file.Opener;
21 import com.google.android.libraries.mobiledatadownload.file.SynchronousFileStorage;
22 import com.google.common.collect.Iterables;
23 import java.io.FileNotFoundException;
24 import java.io.IOException;
25 import java.util.ArrayDeque;
26 import java.util.Deque;
27 
28 /**
29  * Calculates the size of a directory by recursively summing the size of all of its children. If the
30  * Uri refers to an empty directory, returns 0. If the Uri refers to a file or does not exist,
31  * throws {@link FileNotFoundException}.
32  *
33  * <p>WARNING: this opener suffers from the following caveats and should be used with caution:
34  *
35  * <ul>
36  *   <li>Directory tree traversal is not an atomic operation
37  *   <li>There are no special considerations for symlinks, meaning the opener could get caught in a
38  *       recursive directory loop (i.e. a directory that contains a symlink to itself)
39  *   <li>Fails fast if there is an I/O error while processing a given child Uri
40  * </ul>
41  *
42  * <p>Usage: long size = storage.open(uri, RecursiveSizeOpener.create());
43  */
44 public final class RecursiveSizeOpener implements Opener<Long> {
RecursiveSizeOpener()45   private RecursiveSizeOpener() {}
46 
create()47   public static RecursiveSizeOpener create() {
48     return new RecursiveSizeOpener();
49   }
50 
51   @Override
open(OpenContext context)52   public Long open(OpenContext context) throws IOException {
53     long totalSize = 0;
54     Deque<Uri> toProcess = new ArrayDeque<>();
55     SynchronousFileStorage storage = context.storage();
56 
57     // Stripping the Uri fragment means children filenames don't get encoded by transforms. This is
58     // intentional: we're simply calculating the total file size regardless of the "correct" names.
59     // Children API call throws FNF if the Uri is a file or does not exist.
60     Uri uriWithoutFragment = context.originalUri().buildUpon().fragment(null).build();
61     Iterables.addAll(toProcess, storage.children(uriWithoutFragment));
62 
63     // NOTE: breadth-first traversal is an arbitrary implementation choice
64     while (!toProcess.isEmpty()) {
65       Uri uri = toProcess.remove();
66       if (storage.isDirectory(uri)) {
67         Iterables.addAll(toProcess, storage.children(uri));
68       } else if (storage.exists(uri)) {
69         totalSize += storage.fileSize(uri);
70       } else {
71         throw new FileNotFoundException(String.format("Child %s could not be opened", uri));
72       }
73     }
74 
75     return totalSize;
76   }
77 }
78