2 * Copyright (C) 2005-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
9 #include "PosixMountProvider.h"
11 #include "utils/RegExp.h"
12 #include "utils/URIUtils.h"
13 #include "utils/log.h"
17 CPosixMountProvider::CPosixMountProvider()
19 m_removableLength
= 0;
20 PumpDriveChangeEvents(NULL
);
23 void CPosixMountProvider::Initialize()
25 CLog::Log(LOGDEBUG
, "Selected Posix mount as storage provider");
28 void CPosixMountProvider::GetDrives(std::vector
<CMediaSource
>& drives
)
30 std::vector
<std::string
> result
;
33 #if defined(TARGET_DARWIN) || defined(TARGET_FREEBSD)
34 reMount
.RegComp("on (.+) \\(([^,]+)");
36 reMount
.RegComp("on (.+) type ([^ ]+)");
40 FILE* pipe
= popen("mount", "r");
44 while (fgets(line
, sizeof(line
) - 1, pipe
))
46 if (reMount
.RegFind(line
) != -1)
48 bool accepted
= false;
49 std::string mountStr
= reMount
.GetReplaceString("\\1");
50 std::string fsStr
= reMount
.GetReplaceString("\\2");
51 const char* mount
= mountStr
.c_str();
52 const char* fs
= fsStr
.c_str();
54 // Here we choose which filesystems are approved
55 if (strcmp(fs
, "fuseblk") == 0 || strcmp(fs
, "vfat") == 0
56 || strcmp(fs
, "ext2") == 0 || strcmp(fs
, "ext3") == 0
57 || strcmp(fs
, "reiserfs") == 0 || strcmp(fs
, "xfs") == 0
58 || strcmp(fs
, "ntfs-3g") == 0 || strcmp(fs
, "iso9660") == 0
59 || strcmp(fs
, "exfat") == 0
60 || strcmp(fs
, "fusefs") == 0 || strcmp(fs
, "hfs") == 0)
64 if (strcmp(mount
, "/") == 0)
68 result
.emplace_back(mount
);
74 for (unsigned int i
= 0; i
< result
.size(); i
++)
77 share
.strPath
= result
[i
];
78 share
.strName
= URIUtils::GetFileName(result
[i
]);
79 share
.m_ignore
= true;
80 drives
.push_back(share
);
84 std::vector
<std::string
> CPosixMountProvider::GetDiskUsage()
86 std::vector
<std::string
> result
;
89 #if defined(TARGET_DARWIN)
90 FILE* pipe
= popen("df -hT ufs,cd9660,hfs,udf", "r");
91 #elif defined(TARGET_FREEBSD)
92 FILE* pipe
= popen("df -h -t ufs,cd9660,hfs,udf,zfs", "r");
94 FILE* pipe
= popen("df -h", "r");
97 static const char* excludes
[] = {"rootfs","devtmpfs","tmpfs","none","efivarfs","systemd-1","/dev/loop", "udev", NULL
};
101 while (fgets(line
, sizeof(line
) - 1, pipe
))
104 for (int i
=0;excludes
[i
];++i
)
106 if (strstr(line
,excludes
[i
]))
113 result
.emplace_back(line
);
121 bool CPosixMountProvider::Eject(const std::string
& mountpath
)
124 #if !defined(TARGET_DARWIN_EMBEDDED)
125 // just go ahead and try to umount the disk
126 // if it does umount, life is good, if not, no loss.
127 std::string cmd
= "umount \"" + mountpath
+ "\"";
128 int status
= system(cmd
.c_str());
137 bool CPosixMountProvider::PumpDriveChangeEvents(IStorageEventsCallback
*callback
)
139 std::vector
<CMediaSource
> drives
;
140 GetRemovableDrives(drives
);
141 bool changed
= drives
.size() != m_removableLength
;
142 m_removableLength
= drives
.size();