| 205 | } |
| 206 | |
| 207 | static |
| 208 | bool queryDevice(char const* deviceName, queryType query, char *answer, size_t answer_maxsize) |
| 209 | { |
| 210 | struct stat buf; |
| 211 | DADiskRef disk; |
| 212 | CFDictionaryRef descDict; |
| 213 | CFBooleanRef b_ref; |
| 214 | bool result = false; |
| 215 | int isMediaWhole = 0; |
| 216 | |
| 217 | assert(deviceName); |
| 218 | |
| 219 | char devFilePath[ MAXPATHLEN ]; |
| 220 | char *deviceFilePath = &devFilePath[0]; |
| 221 | |
| 222 | strlcpy( deviceFilePath, _PATH_DEV, sizeof( devFilePath ) ); |
| 223 | strlcat( deviceFilePath, deviceName, sizeof( devFilePath ) ); |
| 224 | |
| 225 | if (stat(deviceFilePath, &buf) < 0) { |
| 226 | fprintf(stderr, "Error: stat failed on %s: %s\n", deviceFilePath, strerror(errno)); |
| 227 | return false; |
| 228 | } |
| 229 | if (!(buf.st_mode & (S_IFCHR | S_IFBLK))) { |
| 230 | fprintf(stderr, "Error: %s is not a block or character special device\n", deviceFilePath); |
| 231 | return false; |
| 232 | } |
| 233 | |
| 234 | if (getDASessionAndDisk(deviceName, NULL, &disk) < 0) |
| 235 | return false; |
| 236 | |
| 237 | descDict = DADiskCopyDescription(disk); |
| 238 | CFRelease(disk); |
| 239 | |
| 240 | if (!descDict) { |
| 241 | return false; |
| 242 | } |
| 243 | |
| 244 | if (CFDictionaryGetValueIfPresent(descDict, kDADiskDescriptionMediaWholeKey, (void const**) &b_ref) && |
| 245 | CFBooleanGetValue(b_ref)) |
| 246 | isMediaWhole = 1; |
| 247 | |
| 248 | // If query partition scheme, the device must be a whole disk |
| 249 | if (query == query_partitionscheme && !isMediaWhole) { |
| 250 | fprintf(stderr, "Error: device must be a whole disk to query about partition scheme !\n"); |
| 251 | CFRelease(descDict); |
| 252 | return false; |
| 253 | } |
| 254 | |
| 255 | CFStringRef key = NULL; |
| 256 | switch (query) { |
| 257 | case query_fstype: |
| 258 | key = kDADiskDescriptionVolumeKindKey; |
| 259 | break; |
| 260 | case query_bsdname: |
| 261 | key = kDADiskDescriptionMediaBSDNameKey; |
| 262 | break; |
| 263 | case query_mountpoint: |
| 264 | key = kDADiskDescriptionVolumePathKey; |
no test coverage detected