• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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 #include "utility.h"
18 
19 #include <stdint.h>
20 #include <sys/vfs.h>
21 #include <unistd.h>
22 
23 #include <android-base/logging.h>
24 #include <libfiemap_writer/fiemap_writer.h>
25 
26 namespace android {
27 namespace fiemap_writer {
28 
DetermineMaximumFileSize(const std::string & file_path)29 uint64_t DetermineMaximumFileSize(const std::string& file_path) {
30     // Create the smallest file possible (one block).
31     auto writer = FiemapWriter::Open(file_path, 1);
32     if (!writer) {
33         return 0;
34     }
35 
36     uint64_t result = 0;
37     switch (writer->fs_type()) {
38         case EXT4_SUPER_MAGIC:
39             // The minimum is 16GiB, so just report that. If we wanted we could parse the
40             // superblock and figure out if 64-bit support is enabled.
41             result = 17179869184ULL;
42             break;
43         case F2FS_SUPER_MAGIC:
44             // Formula is from https://www.kernel.org/doc/Documentation/filesystems/f2fs.txt
45             // 4KB * (923 + 2 * 1018 + 2 * 1018 * 1018 + 1018 * 1018 * 1018) := 3.94TB.
46             result = 4329690886144ULL;
47             break;
48         case MSDOS_SUPER_MAGIC:
49             // 4GB-1, which we want aligned to the block size.
50             result = 4294967295;
51             result -= (result % writer->block_size());
52             break;
53         default:
54             LOG(ERROR) << "Unknown file system type: " << writer->fs_type();
55             break;
56     }
57 
58     // Close and delete the temporary file.
59     writer = nullptr;
60     unlink(file_path.c_str());
61 
62     return result;
63 }
64 
65 }  // namespace fiemap_writer
66 }  // namespace android
67