rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | /* uint64_t ...*/ |
| 2 | #include <inttypes.h> |
| 3 | #include <stdbool.h> |
| 4 | |
| 5 | /* struct stat, open */ |
| 6 | #include <sys/types.h> |
| 7 | #include <sys/stat.h> |
| 8 | #include <fcntl.h> |
| 9 | |
| 10 | /* fsync(), close() */ |
| 11 | #include <unistd.h> |
| 12 | |
| 13 | /* strerror */ |
| 14 | #include <string.h> |
| 15 | /* errno */ |
| 16 | #include <errno.h> |
| 17 | |
| 18 | /* statfs() and statvfs() */ |
| 19 | #include <sys/statfs.h> |
| 20 | #include <sys/statvfs.h> |
| 21 | #include <sys/vfs.h> |
| 22 | |
| 23 | /* ioctl, BLKGETSIZE64 */ |
| 24 | #include <linux/fs.h> |
| 25 | #include <sys/ioctl.h> |
| 26 | |
| 27 | #ifdef __ANDROID__ |
| 28 | /* Property */ |
| 29 | #include <cutils/properties.h> |
| 30 | #include <sys/system_properties.h> |
| 31 | #elif defined(__YOCTO_OS__) |
| 32 | #include <stdio.h> |
| 33 | #endif |
| 34 | |
| 35 | /* mrdump related */ |
| 36 | #include "mrdump_log.h" |
| 37 | #include "mrdump_common.h" |
| 38 | #include "mrdump_support_ext4.h" |
| 39 | #include "mrdump_support_f2fs.h" |
| 40 | #include "mrdump_status_private.h" |
| 41 | |
| 42 | void mrdump_close(int fd) |
| 43 | { |
| 44 | fsync(fd); |
| 45 | close(fd); |
| 46 | } |
| 47 | |
| 48 | int mrdump_file_is_exist(const char *path) |
| 49 | { |
| 50 | struct stat s; |
| 51 | if (!path) |
| 52 | return 0; |
| 53 | if (stat(path, &s) != 0) |
| 54 | return 0; |
| 55 | return 1; |
| 56 | } |
| 57 | |
| 58 | int mrdump_get_data_os(void) |
| 59 | { |
| 60 | if (mount_as_ext4(MRDUMP_EXT4_MOUNT_POINT)) |
| 61 | return MRDUMP_DATA_FS_EXT4; |
| 62 | |
| 63 | if (mount_as_f2fs(MRDUMP_EXT4_MOUNT_POINT)) |
| 64 | return MRDUMP_DATA_FS_F2FS; |
| 65 | |
| 66 | MD_LOGI("%s: unknown os\n", __func__); |
| 67 | return MRDUMP_DATA_FS_NONE; |
| 68 | } |
| 69 | |
| 70 | uint64_t mrdump_get_partition_free_size(const char *mountp) |
| 71 | { |
| 72 | struct statvfs vfs; |
| 73 | uint64_t psize; |
| 74 | |
| 75 | if(statvfs(mountp, &vfs) == 0) { |
| 76 | psize = (uint64_t) vfs.f_frsize * (uint64_t) vfs.f_bfree; |
| 77 | MD_LOGD("%s: size of %s: %" PRId64 " bytes.\n", __func__, mountp, psize); |
| 78 | return psize; |
| 79 | } |
| 80 | |
| 81 | MD_LOGE("%s: statvfs of %s got failed(%d), %s\n", __func__, mountp, errno, strerror(errno)); |
| 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | uint64_t mrdump_get_partition_size(char *fullpath) |
| 86 | { |
| 87 | int fd = open(fullpath, O_RDONLY); |
| 88 | if (0 > fd) { |
| 89 | MD_LOGE("%s: open fullpath failed. (%s)\n", __func__, fullpath); |
| 90 | return 0; |
| 91 | } |
| 92 | |
| 93 | uint64_t psize; |
| 94 | int ret = ioctl(fd, BLKGETSIZE64, &psize); |
| 95 | if (0 > ret) { |
| 96 | MD_LOGE("%s: ioctl BLKGETSIZE64 failed. (%s)\n", __func__, fullpath); |
| 97 | mrdump_close(fd); |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | mrdump_close(fd); |
| 102 | |
| 103 | return psize; |
| 104 | } |
| 105 | |
| 106 | static char *mrdump_get_device_node_from_fstab(const char *fstab, const char *mountp) |
| 107 | { |
| 108 | #ifdef __ANDROID_ |
| 109 | int ret; |
| 110 | FILE *fp; |
| 111 | char c, myline[1024]; |
| 112 | char *delim="\x09\x20"; |
| 113 | char *DeviceNode, *MountPoint; |
| 114 | |
| 115 | DeviceNode = NULL; |
| 116 | fp = fopen(fstab, "r"); |
| 117 | if(fp == NULL) |
| 118 | return NULL; |
| 119 | |
| 120 | while(!feof(fp)) { |
| 121 | |
| 122 | // getline |
| 123 | ret = fscanf(fp, "%[^\n]", myline); |
| 124 | |
| 125 | // strtok strings |
| 126 | if(ret > 0) { |
| 127 | if(myline[0] == '/') { |
| 128 | DeviceNode = strtok(myline, delim); |
| 129 | MountPoint = strtok(NULL, delim); |
| 130 | if(MountPoint != NULL) { |
| 131 | if(!strcmp(MountPoint, mountp)) { |
| 132 | fclose(fp); |
| 133 | return strdup(DeviceNode); |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | /* clear newline character */ |
| 140 | ret = fscanf(fp, "%c", &c); |
| 141 | if (ret != 1) { |
| 142 | MD_LOGE("%s: not EOL.", __func__); |
| 143 | fclose(fp); |
| 144 | return NULL; |
| 145 | } |
| 146 | } |
| 147 | fclose(fp); |
| 148 | return NULL; |
| 149 | #elif defined(__YOCTO_OS__) |
| 150 | #endif |
| 151 | } |
| 152 | |
| 153 | #ifdef __ANDROID_ |
| 154 | static const char *fstab_path_prefix[] = { |
| 155 | "/vendor/etc/fstab", |
| 156 | "/fstab", |
| 157 | NULL |
| 158 | }; |
| 159 | #elif defined(__YOCTO_OS__) |
| 160 | static const char expdb_dev[] = "/dev/disk/by-partlabel/expdb"; |
| 161 | static const char preallocate_dev[] = "/dev/disk/by-partlabel/log"; |
| 162 | #endif |
| 163 | |
| 164 | char *mrdump_get_device_node(const char *mountp) |
| 165 | { |
| 166 | #ifdef __ANDROID_ |
| 167 | int i; |
| 168 | char *DeviceNode = NULL; |
| 169 | char fstab_filename[PROPERTY_VALUE_MAX]; |
| 170 | |
| 171 | /* get hardware parts */ |
| 172 | char propbuf[PROPERTY_VALUE_MAX]; |
| 173 | if(property_get("ro.hardware", propbuf, NULL) == 0) |
| 174 | property_get("ro.board.platform", propbuf, ""); |
| 175 | |
| 176 | for(i = 0; fstab_path_prefix[i] != NULL; i++) { |
| 177 | snprintf(fstab_filename, sizeof(fstab_filename), "%s.%s", fstab_path_prefix[i], propbuf); |
| 178 | if(mrdump_file_is_exist(fstab_filename)) |
| 179 | DeviceNode = mrdump_get_device_node_from_fstab(fstab_filename, mountp); |
| 180 | if(DeviceNode) |
| 181 | return DeviceNode; |
| 182 | } |
| 183 | |
| 184 | /* search for path: /fstab */ |
| 185 | DeviceNode = mrdump_get_device_node_from_fstab("/fstab", mountp); |
| 186 | char fstab_filename[PATH_MAX]; |
| 187 | |
| 188 | for (i = 0; fstab_path_prefix[i] != NULL; i++) { |
| 189 | snprintf(fstab_filename, sizeof(fstab_filename), "%s", fstab_path_prefix[i]); |
| 190 | if(mrdump_file_is_exist(fstab_filename)) |
| 191 | DeviceNode = mrdump_get_device_node_from_fstab(fstab_filename, mountp); |
| 192 | if(DeviceNode) |
| 193 | return DeviceNode; |
| 194 | } |
| 195 | return DeviceNode; |
| 196 | #elif defined(__YOCTO_OS__) |
| 197 | if (!strncmp(mountp, MRDUMP_EXPDB_NAME, sizeof(MRDUMP_EXPDB_NAME))) |
| 198 | return strdup(expdb_dev); |
| 199 | |
| 200 | if (!strncmp(mountp, MRDUMP_EXT4_MOUNT_POINT, sizeof(MRDUMP_EXT4_MOUNT_POINT))) |
| 201 | return strdup(preallocate_dev); |
| 202 | |
| 203 | return NULL; |
| 204 | #endif |
| 205 | } |