1 /*
2  * Copyright (C) 2017 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 android.util.apk;
18 
19 import android.annotation.NonNull;
20 import android.os.incremental.IncrementalManager;
21 
22 import java.io.FileDescriptor;
23 import java.io.IOException;
24 import java.security.DigestException;
25 
26 /** Source of data to be digested. */
27 interface DataSource {
28 
29     /**
30      * Returns the size (in bytes) of the data offered by this source.
31      */
size()32     long size();
33 
34     /**
35      * Feeds the specified region of this source's data into the provided digester.
36      *
37      * @param offset offset of the region inside this data source.
38      * @param size size (in bytes) of the region.
39      */
feedIntoDataDigester(DataDigester md, long offset, int size)40     void feedIntoDataDigester(DataDigester md, long offset, int size)
41             throws IOException, DigestException;
42 
43     /**
44      * Creates a DataSource that can handle the passed fd in the most efficient and safe manner.
45      * @param fd file descriptor to read from
46      * @param pos starting offset
47      * @param size size of the region
48      * @return created DataSource object
49      */
create(@onNull FileDescriptor fd, long pos, long size)50     static @NonNull DataSource create(@NonNull FileDescriptor fd, long pos, long size) {
51         if (IncrementalManager.isIncrementalFileFd(fd)) {
52             // IncFS-based files may have missing pages, and reading those via mmap() results
53             // in a SIGBUS signal. Java doesn't have a good way of catching it, ending up killing
54             // the process by default. Going back to read() is the safest option for these files.
55             return new ReadFileDataSource(fd, pos, size);
56         } else {
57             return new MemoryMappedFileDataSource(fd, pos, size);
58         }
59     }
60 }
61