1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
|
/*
* Copyright (C) 2012-2013 Team XBMC
* http://xbmc.org
*
* This Program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This Program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with XBMC; see the file COPYING. If not, see
* <http://www.gnu.org/licenses/>.
*
*/
#include "AndroidStorageProvider.h"
#include "android/activity/XBMCApp.h"
#include "guilib/LocalizeStrings.h"
#include "filesystem/File.h"
#include "utils/log.h"
#include "utils/RegExp.h"
#include "utils/StringUtils.h"
#include "utils/URIUtils.h"
CAndroidStorageProvider::CAndroidStorageProvider()
{
m_removableLength = 0;
PumpDriveChangeEvents(NULL);
}
std::string CAndroidStorageProvider::unescape(const std::string& str)
{
std::string retString;
for (uint32_t i=0; i < str.length(); ++i)
{
if (str[i] != '\\')
retString += str[i];
else
{
i += 1;
if (str[i] == 'u') // unicode
{
// TODO
}
else if (str[i] >= '0' && str[i] <= '7') // octal
{
std::string octString;
while (str[i] >= '0' && str[i] <= '7')
{
octString += str[i];
i += 1;
}
if (octString.length() != 0)
{
uint8_t val = 0;
for (int j=octString.length()-1; j>=0; --j)
{
val += ((uint8_t)(octString[j] - '0')) * (1 << ((octString.length() - (j+1)) * 3));
}
retString += (char)val;
i -= 1;
}
}
}
}
return retString;
}
void CAndroidStorageProvider::GetLocalDrives(VECSOURCES &localDrives)
{
CMediaSource share;
// external directory
std::string path;
if (CXBMCApp::GetExternalStorage(path) && !path.empty() && XFILE::CFile::Exists(path))
{
share.strPath = path;
share.strName = g_localizeStrings.Get(21456);
share.m_ignore = true;
localDrives.push_back(share);
}
// root directory
share.strPath = "/";
share.strName = g_localizeStrings.Get(21453);
localDrives.push_back(share);
}
void CAndroidStorageProvider::GetRemovableDrives(VECSOURCES &removableDrives)
{
// mounted usb disks
std::vector<CStdString> result;
CRegExp reMount;
reMount.RegComp("^(.+?)\\s+(.+?)\\s+(.+?)\\s");
char line[1024];
FILE* pipe = fopen("/proc/mounts", "r");
if (pipe)
{
while (fgets(line, sizeof(line) - 1, pipe))
{
if (reMount.RegFind(line) != -1)
{
bool accepted = false;
std::string mountStr = reMount.GetReplaceString("\\2");
std::string fsStr = reMount.GetReplaceString("\\3");
const char* mount = mountStr.c_str();
const char* fs = fsStr.c_str();
// Here we choose which filesystems are approved
if (strcmp(fs, "fuseblk") == 0 || strcmp(fs, "vfat") == 0
|| strcmp(fs, "ext2") == 0 || strcmp(fs, "ext3") == 0 || strcmp(fs, "ext4") == 0
|| strcmp(fs, "reiserfs") == 0 || strcmp(fs, "xfs") == 0
|| strcmp(fs, "ntfs-3g") == 0 || strcmp(fs, "iso9660") == 0
|| strcmp(fs, "exfat") == 0
|| strcmp(fs, "fusefs") == 0 || strcmp(fs, "hfs") == 0)
accepted = true;
// Ignore everything but usb
if (!StringUtils::StartsWith(mountStr, "/mnt/usb"))
accepted = false;
if(accepted)
result.push_back(mount);
}
}
fclose(pipe);
} else
CLog::Log(LOGERROR, "Cannot read mount points");
for (unsigned int i = 0; i < result.size(); i++)
{
CMediaSource share;
share.strPath = unescape(result[i]);
share.strName = URIUtils::GetFileName(share.strPath);
share.m_ignore = true;
removableDrives.push_back(share);
}
}
std::vector<CStdString> CAndroidStorageProvider::GetDiskUsage()
{
std::vector<CStdString> result;
std::string usage;
// add header
CXBMCApp::GetStorageUsage("", usage);
result.push_back(usage);
usage.clear();
// add rootfs
if (CXBMCApp::GetStorageUsage("/", usage) && !usage.empty())
result.push_back(usage);
usage.clear();
// add external storage if available
std::string path;
if (CXBMCApp::GetExternalStorage(path) && !path.empty() &&
CXBMCApp::GetStorageUsage(path, usage) && !usage.empty())
result.push_back(usage);
// add removable storage
VECSOURCES drives;
GetRemovableDrives(drives);
for (unsigned int i = 0; i < drives.size(); i++)
{
usage.clear();
if (CXBMCApp::GetStorageUsage(drives[i].strPath, usage) && !usage.empty())
result.push_back(usage);
}
return result;
}
bool CAndroidStorageProvider::Eject(CStdString mountpath)
{
return false;
}
bool CAndroidStorageProvider::PumpDriveChangeEvents(IStorageEventsCallback *callback)
{
VECSOURCES drives;
GetRemovableDrives(drives);
bool changed = drives.size() != m_removableLength;
m_removableLength = drives.size();
return changed;
}
|