1 /*
2  * This file is part of libbluray
3  * Copyright (C) 2014  VideoLAN
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library. If not, see
17  * <http://www.gnu.org/licenses/>.
18  */
19 
20 #if HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23 
24 #include "mount.h"
25 
26 #include "util/strutl.h"
27 
28 #include <string.h>
29 
30 #define _DARWIN_C_SOURCE
31 #include <sys/stat.h>
32 
33 #include <DiskArbitration/DADisk.h>
34 
bsdname_get_mountpoint(const char * device_path)35 static char *bsdname_get_mountpoint(const char *device_path)
36 {
37     char *result = NULL;
38 
39     DASessionRef session = DASessionCreate(kCFAllocatorDefault);
40     if (session) {
41         DADiskRef disk = DADiskCreateFromBSDName(kCFAllocatorDefault, session, device_path);
42         if (disk) {
43             CFDictionaryRef desc = DADiskCopyDescription(disk);
44             if (desc) {
45                 // Get Volume path as CFURL
46                 CFURLRef url = CFDictionaryGetValue(desc, kDADiskDescriptionVolumePathKey);
47                 if (url) {
48                     // Copy Volume path as C char array
49                     char tmp_path[PATH_MAX];
50                     if (CFURLGetFileSystemRepresentation(url, true, (UInt8*)tmp_path, sizeof(tmp_path))) {
51                         result = str_dup(tmp_path);
52                     }
53                 }
54                 CFRelease(desc);
55             }
56             CFRelease(disk);
57         }
58         CFRelease(session);
59     }
60 
61     return result;
62 }
63 
64 
mount_get_mountpoint(const char * device_path)65 char *mount_get_mountpoint(const char *device_path)
66 {
67     struct stat st;
68     if (stat(device_path, &st) == 0) {
69         // If it's a directory, all is good
70         if (S_ISDIR(st.st_mode)) {
71             return str_dup(device_path);
72         }
73     }
74 
75     char *mountpoint = bsdname_get_mountpoint(device_path);
76     if (mountpoint) {
77         return mountpoint;
78     }
79 
80     return str_dup(device_path);
81 }
82