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 <sys/stat.h>
20 #include <sys/types.h>
21 #include <unistd.h>
22 
23 #include "super_vbmeta_format.h"
24 
25 using android::base::ErrnoError;
26 using android::base::Error;
27 using android::base::Result;
28 
29 namespace android {
30 namespace fs_mgr {
31 
GetFileSize(int fd)32 Result<uint64_t> GetFileSize(int fd) {
33     struct stat sb;
34     if (fstat(fd, &sb) == -1) {
35         return ErrnoError() << "Couldn't get the file size";
36     }
37     return sb.st_size;
38 }
39 
IndexOffset(const uint8_t vbmeta_index)40 uint64_t IndexOffset(const uint8_t vbmeta_index) {
41     /* There are primary and backup vbmeta table in super_vbmeta,
42        so SUPER_VBMETA_TABLE_MAX_SIZE is counted twice. */
43     return 2 * SUPER_VBMETA_TABLE_MAX_SIZE + vbmeta_index * VBMETA_IMAGE_MAX_SIZE;
44 }
45 
46 }  // namespace fs_mgr
47 }  // namespace android
48