blob: 5e8b494f772873823051ddb5e28d56b143b5cf20 [file] [log] [blame]
Tuxera Inc0bf909b2021-07-27 21:17:09 +03001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 * Copyright (C) 2010-2021 Tuxera Inc.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#include <dirent.h>
19#include <errno.h>
20#include <fcntl.h>
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <unistd.h>
25
26#include <linux/fs.h>
27#include <sys/ioctl.h>
28#include <sys/mman.h>
29#include <sys/mount.h>
30#include <sys/stat.h>
31#include <sys/types.h>
32#include <sys/wait.h>
33
34#include <linux/kdev_t.h>
35
36#define LOG_TAG "Vold::Apfs"
37
38#include <android-base/logging.h>
39#include <android-base/stringprintf.h>
40#include <selinux/selinux.h>
41
42#include <logwrap/logwrap.h>
43
44#include "Utils.h"
45#include "Apfs.h"
46#include "VoldUtil.h"
47
48using android::base::StringPrintf;
49
50namespace android {
51namespace vold {
52namespace apfs {
53
54status_t Detect(const std::string& source, bool *outResult) {
55 const char *const fsPath = source.c_str();
56
57 int retval = -1;
58
59 int fd = open(fsPath, O_RDONLY | O_CLOEXEC);
60 if (fd != -1) {
61 loff_t seek_res = lseek64(fd, 0, SEEK_SET);
62 if (seek_res == 0) {
63 char sb_region[4096];
64 ssize_t read_res = read(fd, sb_region, 4096);
65 if (read_res >= 512) {
66 if (!memcmp(&sb_region[32], "NXSB", 4)) {
67 LOG(INFO) << "APFS filesystem detected.";
68 *outResult = true;
69 }
70 else {
71 LOG(VERBOSE) << "Filesystem detection failed (not an APFS "
72 << "filesystem).";
73 *outResult = false;
74 }
75
76 retval = 0;
77 }
78 else if (read_res != -1) {
79 int err = errno ? errno : EIO;
80 LOG(ERROR) << "Error while reading 4096 bytes from device "
81 << "offset 0: " << strerror(errno) << " (" << errno
82 << ")";
83 errno = err;
84 }
85 }
86 else if (seek_res != -1) {
87 int err = errno ? errno : EIO;
88 LOG(ERROR) << "Error while seeking to device offset 0: "
89 << strerror(errno) << " (" << errno << ")";
90 errno = err;
91 }
92
93 close(fd);
94 }
95
96 return retval;
97}
98
99} // namespace apfs
100} // namespace vold
101} // namespace android