Tuxera Inc | 0bf909b | 2021-07-27 21:17:09 +0300 | [diff] [blame] | 1 | /* |
| 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::HfsPlus" |
| 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 "HfsPlus.h" |
| 45 | #include "Utils.h" |
| 46 | #include "VoldUtil.h" |
| 47 | |
| 48 | using android::base::StringPrintf; |
| 49 | |
| 50 | namespace android { |
| 51 | namespace vold { |
| 52 | namespace hfsplus { |
| 53 | |
| 54 | status_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 >= (1024 + 512)) { |
| 66 | if (!memcmp(&sb_region[1024], "H+\x00\x04", 4)) { |
| 67 | LOG(INFO) << "HFS+ filesystem detected."; |
| 68 | *outResult = true; |
| 69 | } |
| 70 | else if (!memcmp(&sb_region[1024], "HX\x00\x05", 4)) { |
| 71 | LOG(INFO) << "HFSX filesystem detected."; |
| 72 | *outResult = true; |
| 73 | } |
| 74 | else { |
| 75 | LOG(VERBOSE) << "Filesystem detection failed (not an " |
| 76 | << "HFS+/HFSX filesystem)."; |
| 77 | *outResult = false; |
| 78 | } |
| 79 | |
| 80 | retval = 0; |
| 81 | } |
| 82 | else if (read_res != -1) { |
| 83 | int err = errno ? errno : EIO; |
| 84 | LOG(ERROR) << "Error while reading 4096 bytes from device " |
| 85 | << "offset 0: " << strerror(errno) << " (" << errno |
| 86 | << ")"; |
| 87 | errno = err; |
| 88 | } |
| 89 | } |
| 90 | else if (seek_res != -1) { |
| 91 | int err = errno ? errno : EIO; |
| 92 | LOG(ERROR) << "Error while seeking to device offset 0: " |
| 93 | << strerror(errno) << " (" << errno << ")"; |
| 94 | errno = err; |
| 95 | } |
| 96 | |
| 97 | close(fd); |
| 98 | } |
| 99 | |
| 100 | return retval; |
| 101 | } |
| 102 | |
| 103 | } // namespace hfsplus |
| 104 | } // namespace vold |
| 105 | } // namespace android |