Tuxera Inc | 0bf909b | 2021-07-27 21:17:09 +0300 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014-2021 Tuxera Inc. |
| 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 "Filesystems.h" |
| 18 | |
| 19 | #include "Apfs.h" |
| 20 | #include "HfsPlus.h" |
| 21 | #include "Ntfs.h" |
| 22 | #include "Exfat.h" |
| 23 | #include "Vfat.h" |
| 24 | |
| 25 | #include <errno.h> |
| 26 | |
| 27 | namespace android { |
| 28 | namespace vold { |
| 29 | namespace filesystems { |
| 30 | |
| 31 | status_t Detect(const std::string& source, FSType *outFsType) { |
| 32 | int err; |
| 33 | bool result = false; |
| 34 | |
| 35 | if ((err = hfsplus::Detect(source, &result)) == 0 && result) { |
| 36 | *outFsType = FSTYPE_HFSPLUS; |
| 37 | } |
| 38 | else if ((err = ntfs::Detect(source, &result)) == 0 && result) { |
| 39 | *outFsType = FSTYPE_NTFS; |
| 40 | } |
| 41 | else if ((err = exfat::Detect(source, &result)) == 0 && result) { |
| 42 | *outFsType = FSTYPE_EXFAT; |
| 43 | } |
| 44 | else if ((err = apfs::Detect(source, &result)) == 0 && result) { |
| 45 | *outFsType = FSTYPE_APFS; |
| 46 | } |
| 47 | else { |
| 48 | /* Until we implement reliable FAT detection code. */ |
| 49 | *outFsType = FSTYPE_FAT; |
| 50 | } |
| 51 | |
| 52 | return 0; /* Always succeed since FAT is the default fallback. */ |
| 53 | } |
| 54 | |
| 55 | bool IsSupported(FSType fsType) { |
| 56 | switch (fsType) { |
| 57 | case FSTYPE_APFS: |
| 58 | return false; |
| 59 | case FSTYPE_HFSPLUS: |
| 60 | return false; |
| 61 | case FSTYPE_NTFS: |
| 62 | return false; |
| 63 | case FSTYPE_EXFAT: |
| 64 | return exfat::IsSupported(); |
| 65 | case FSTYPE_FAT: |
| 66 | return vfat::IsSupported(); |
| 67 | default: |
| 68 | return false; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | status_t Check(FSType fsType, const std::string& source) { |
| 73 | switch (fsType) { |
| 74 | case FSTYPE_APFS: |
| 75 | errno = ENODATA; |
| 76 | return -1; |
| 77 | case FSTYPE_HFSPLUS: |
| 78 | errno = ENODATA; |
| 79 | return -1; |
| 80 | case FSTYPE_NTFS: |
| 81 | errno = ENODATA; |
| 82 | return -1; |
| 83 | case FSTYPE_EXFAT: |
| 84 | return exfat::Check(source); |
| 85 | case FSTYPE_FAT: |
| 86 | return vfat::Check(source); |
| 87 | default: |
| 88 | errno = ENODATA; |
| 89 | return -1; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | status_t Mount(FSType fsType, const std::string& source, |
| 94 | const std::string& target, bool ro, bool remount, bool executable, |
| 95 | int ownerUid, int ownerGid, int permMask, bool createLost) { |
| 96 | switch (fsType) { |
| 97 | case FSTYPE_APFS: |
| 98 | errno = ENOTSUP; |
| 99 | return -1; |
| 100 | case FSTYPE_HFSPLUS: |
| 101 | errno = ENOTSUP; |
| 102 | return -1; |
| 103 | case FSTYPE_NTFS: |
| 104 | errno = ENOTSUP; |
| 105 | return -1; |
| 106 | case FSTYPE_EXFAT: |
| 107 | return exfat::Mount(source, target, ro, remount, executable, ownerUid, |
| 108 | ownerGid, permMask); |
| 109 | case FSTYPE_FAT: |
| 110 | return vfat::Mount(source, target, ro, remount, executable, ownerUid, |
| 111 | ownerGid, permMask, createLost); |
| 112 | default: |
| 113 | errno = ENOTSUP; |
| 114 | return -1; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | const char* FsName(FSType fsType) { |
| 119 | switch (fsType) { |
| 120 | case FSTYPE_FAT: |
| 121 | return "VFAT"; |
| 122 | case FSTYPE_EXFAT: |
| 123 | return "exFAT"; |
| 124 | case FSTYPE_NTFS: |
| 125 | return "NTFS"; |
| 126 | case FSTYPE_HFSPLUS: |
| 127 | return "HFS+"; |
| 128 | case FSTYPE_APFS: |
| 129 | return "APFS"; |
| 130 | default: |
| 131 | return "<unknown filesystem>"; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | } // namespace filesystems |
| 136 | } // namespace vold |
| 137 | } // namespace android |