DVDCodecs: Amlogic: Handle conditions in which amcodec should be opened during Open()
[vuplus_xbmc] / xbmc / linux / PosixMountProvider.cpp
1 /*
2  *      Copyright (C) 2005-2013 Team XBMC
3  *      http://xbmc.org
4  *
5  *  This Program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2, or (at your option)
8  *  any later version.
9  *
10  *  This Program 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
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with XBMC; see the file COPYING.  If not, see
17  *  <http://www.gnu.org/licenses/>.
18  *
19  */
20 #include "PosixMountProvider.h"
21 #include "utils/RegExp.h"
22 #include "utils/StdString.h"
23 #include "utils/URIUtils.h"
24 #include "utils/log.h"
25
26 CPosixMountProvider::CPosixMountProvider()
27 {
28   m_removableLength = 0;
29   PumpDriveChangeEvents(NULL);
30 }
31
32 void CPosixMountProvider::Initialize()
33 {
34   CLog::Log(LOGDEBUG, "Selected Posix mount as storage provider");
35 }
36
37 void CPosixMountProvider::GetDrives(VECSOURCES &drives)
38 {
39   std::vector<CStdString> result;
40
41   CRegExp reMount;
42 #if defined(TARGET_DARWIN)
43   reMount.RegComp("on (.+) \\(([^,]+)");
44 #else
45   reMount.RegComp("on (.+) type ([^ ]+)");
46 #endif
47   char line[1024];
48
49   FILE* pipe = popen("mount", "r");
50
51   if (pipe)
52   {
53     while (fgets(line, sizeof(line) - 1, pipe))
54     {
55       if (reMount.RegFind(line) != -1)
56       {
57         bool accepted = false;
58         std::string mountStr = reMount.GetReplaceString("\\1");
59         std::string fsStr    = reMount.GetReplaceString("\\2");
60         const char* mount = mountStr.c_str();
61         const char* fs    = fsStr.c_str();
62
63         // Here we choose which filesystems are approved
64         if (strcmp(fs, "fuseblk") == 0 || strcmp(fs, "vfat") == 0
65             || strcmp(fs, "ext2") == 0 || strcmp(fs, "ext3") == 0
66             || strcmp(fs, "reiserfs") == 0 || strcmp(fs, "xfs") == 0
67             || strcmp(fs, "ntfs-3g") == 0 || strcmp(fs, "iso9660") == 0
68             || strcmp(fs, "exfat") == 0
69             || strcmp(fs, "fusefs") == 0 || strcmp(fs, "hfs") == 0)
70           accepted = true;
71
72         // Ignore root
73         if (strcmp(mount, "/") == 0)
74           accepted = false;
75
76         if(accepted)
77           result.push_back(mount);
78       }
79     }
80     pclose(pipe);
81   }
82
83   for (unsigned int i = 0; i < result.size(); i++)
84   {
85     CMediaSource share;
86     share.strPath = result[i];
87     share.strName = URIUtils::GetFileName(result[i]);
88     share.m_ignore = true;
89     drives.push_back(share);
90   }
91 }
92
93 std::vector<CStdString> CPosixMountProvider::GetDiskUsage()
94 {
95   std::vector<CStdString> result;
96   char line[1024];
97
98 #if defined(TARGET_DARWIN)
99   FILE* pipe = popen("df -hT ufs,cd9660,hfs,udf", "r");
100 #elif defined(TARGET_FREEBSD)
101   FILE* pipe = popen("df -h -t ufs,cd9660,hfs,udf,zfs", "r");
102 #else
103   FILE* pipe = popen("df -h", "r");
104 #endif
105   
106   static const char* excludes[] = {"rootfs","devtmpfs","tmpfs","none","/dev/loop", "udev", NULL};
107
108   if (pipe)
109   {
110     while (fgets(line, sizeof(line) - 1, pipe))
111     {
112       bool ok=true;
113       for (int i=0;excludes[i];++i)
114       {
115         if (strstr(line,excludes[i]))
116         {
117           ok=false;
118           break;
119         }
120       }
121       if (ok)
122         result.push_back(line);
123     }
124     pclose(pipe);
125   }
126
127   return result;
128 }
129
130 bool CPosixMountProvider::PumpDriveChangeEvents(IStorageEventsCallback *callback)
131 {
132   VECSOURCES drives;
133   GetRemovableDrives(drives);
134   bool changed = drives.size() != m_removableLength;
135   m_removableLength = drives.size();
136   return changed;
137 }