Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 261 lines (220 sloc) 7.327 kB
65e877a @wagnerrp Transition FileSystemInfo into a proper class rather than a glorified
wagnerrp authored
1 #include <unistd.h>
2 #include <cstdlib>
3 #include "compat.h"
4
5 #ifdef linux
6 #include <sys/vfs.h>
7 #include <sys/sysinfo.h>
8 #endif
9
10 #if CONFIG_DARWIN
11 #include <mach/mach.h>
12 #endif
13
14 #ifdef BSD
15 #include <sys/param.h>
16 #include <sys/mount.h> // for struct statfs
17 #endif
18
19 using namespace std;
20
21 #include <QList>
22 #include <QString>
23 #include <QStringList>
24
25 #include "filesysteminfo.h"
26 #include "mythcoreutil.h"
27
28 // for serialization
29 #define INT_TO_LIST(x) do { list << QString::number(x); } while (0)
30 #define DATETIME_TO_LIST(x) INT_TO_LIST((x).toTime_t())
31 #define STR_TO_LIST(x) do { list << (x); } while (0)
32
33 // for deserialization
34 #define NEXT_STR() do { if (it == listend) \
35 { \
7a1fce0 @Beirdo Renamed LogPrint() to LOG()
Beirdo authored
36 LOG(VB_GENERAL, LOG_ALERT, listerror); \
65e877a @wagnerrp Transition FileSystemInfo into a proper class rather than a glorified
wagnerrp authored
37 clear(); \
38 return false; \
39 } \
40 ts = *it++; } while (0)
41 #define INT_FROM_LIST(x) do { NEXT_STR(); (x) = ts.toLongLong(); } while (0)
42 #define ENUM_FROM_LIST(x, y) do { NEXT_STR(); (x) = ((y)ts.toInt()); } while (0)
43 #define DATETIME_FROM_LIST(x) \
df47df3 @daniel-kristjansson This changes the internal representation of time to UTC.
daniel-kristjansson authored
44 do { NEXT_STR(); x = fromTime_t(ts.toUInt()); } while (0)
65e877a @wagnerrp Transition FileSystemInfo into a proper class rather than a glorified
wagnerrp authored
45 #define STR_FROM_LIST(x) do { NEXT_STR(); (x) = ts; } while (0)
46
47 #define LOC QString("FileSystemInfo: ")
48
49 FileSystemInfo::FileSystemInfo(void) :
50 m_hostname(""), m_path(""), m_local(false), m_fsid(-1),
65a2592 @wagnerrp Initialize FileSystemInfo::m_weight to 0 as per cppcheck.
wagnerrp authored
51 m_grpid(-1), m_blksize(4096), m_total(0), m_used(0), m_weight(0)
65e877a @wagnerrp Transition FileSystemInfo into a proper class rather than a glorified
wagnerrp authored
52 {
53 }
54
55 FileSystemInfo::FileSystemInfo(const FileSystemInfo &other)
56 {
57 clone(other);
58 }
59
60 FileSystemInfo::FileSystemInfo(QString hostname, QString path, bool local,
83a03aa @wagnerrp size_t type corrections within the scope of the autoexpirer
wagnerrp authored
61 int fsid, int groupid, int blksize, int64_t total, int64_t used) :
65e877a @wagnerrp Transition FileSystemInfo into a proper class rather than a glorified
wagnerrp authored
62 m_hostname(hostname), m_path(path), m_local(local), m_fsid(fsid),
65a2592 @wagnerrp Initialize FileSystemInfo::m_weight to 0 as per cppcheck.
wagnerrp authored
63 m_grpid(groupid), m_blksize(blksize), m_total(total), m_used(used),
64 m_weight(0)
65e877a @wagnerrp Transition FileSystemInfo into a proper class rather than a glorified
wagnerrp authored
65 {
66 }
67
68 FileSystemInfo::FileSystemInfo(QStringList::const_iterator &it,
69 QStringList::const_iterator end)
70 {
71 FromStringList(it, end);
72 }
73
74 FileSystemInfo::FileSystemInfo(const QStringList &slist)
75 {
76 FromStringList(slist);
77 }
78
79 void FileSystemInfo::clone(const FileSystemInfo &other)
80 {
81 m_hostname = other.m_hostname;
82 m_path = other.m_path;
83 m_local = other.m_local;
84 m_fsid = other.m_fsid;
85 m_grpid = other.m_grpid;
86 m_blksize = other.m_blksize;
87 m_total = other.m_total;
88 m_used = other.m_used;
934041b @wagnerrp Missed semicolon in 65a25923793
wagnerrp authored
89 m_weight = other.m_weight;
65e877a @wagnerrp Transition FileSystemInfo into a proper class rather than a glorified
wagnerrp authored
90 }
91
92 FileSystemInfo &FileSystemInfo::operator=(const FileSystemInfo &other)
93 {
94 clone(other);
95 return *this;
96 }
97
98 void FileSystemInfo::clear(void)
99 {
100 m_hostname = "";
101 m_path = "";
102 m_local = false;
103 m_fsid = -1;
104 m_grpid = -1;
105 m_blksize = 4096;
106 m_total = 0;
107 m_used = 0;
65a2592 @wagnerrp Initialize FileSystemInfo::m_weight to 0 as per cppcheck.
wagnerrp authored
108 m_weight = 0;
65e877a @wagnerrp Transition FileSystemInfo into a proper class rather than a glorified
wagnerrp authored
109 }
110
111 bool FileSystemInfo::ToStringList(QStringList &list) const
112 {
113 STR_TO_LIST(m_hostname);
114 STR_TO_LIST(m_path);
115 INT_TO_LIST(m_local);
116 INT_TO_LIST(m_fsid);
117 INT_TO_LIST(m_grpid);
118 INT_TO_LIST(m_blksize);
119 INT_TO_LIST(m_total);
120 INT_TO_LIST(m_used);
121
122 return true;
123 }
124
125 bool FileSystemInfo::FromStringList(const QStringList &slist)
126 {
127 QStringList::const_iterator it = slist.constBegin();
128 return FromStringList(it, slist.constEnd());
129 }
130
131 bool FileSystemInfo::FromStringList(QStringList::const_iterator &it,
132 QStringList::const_iterator listend)
133 {
134 QString listerror = LOC + "FromStringList, not enough items in list.";
135 QString ts;
136
137 STR_FROM_LIST(m_hostname);
138 STR_FROM_LIST(m_path);
139 INT_FROM_LIST(m_local);
140 INT_FROM_LIST(m_fsid);
141 INT_FROM_LIST(m_grpid);
142 INT_FROM_LIST(m_blksize);
143 INT_FROM_LIST(m_total);
144 INT_FROM_LIST(m_used);
145
65a2592 @wagnerrp Initialize FileSystemInfo::m_weight to 0 as per cppcheck.
wagnerrp authored
146 m_weight = 0;
147
65e877a @wagnerrp Transition FileSystemInfo into a proper class rather than a glorified
wagnerrp authored
148 return true;
149 }
150
151 const QList<FileSystemInfo> FileSystemInfo::RemoteGetInfo(MythSocket *sock)
152 {
153 FileSystemInfo fsInfo;
154 QList<FileSystemInfo> fsInfos;
155 QStringList strlist(QString("QUERY_FREE_SPACE_LIST"));
156
157 bool sent;
158
159 if (sock)
160 sent = sock->SendReceiveStringList(strlist);
161 else
162 sent = gCoreContext->SendReceiveStringList(strlist);
163
164 if (sent)
165 {
166 int numdisks = strlist.size()/NUMDISKINFOLINES;
167
168 QStringList::const_iterator it = strlist.begin();
169 for (int i = 0; i < numdisks; i++)
170 {
171 fsInfo.FromStringList(it, strlist.end());
172 fsInfos.append(fsInfo);
173 }
174 }
175
176 return fsInfos;
177 }
178
179 void FileSystemInfo::Consolidate(QList<FileSystemInfo> &disks,
83a03aa @wagnerrp size_t type corrections within the scope of the autoexpirer
wagnerrp authored
180 bool merge, int64_t fuzz)
65e877a @wagnerrp Transition FileSystemInfo into a proper class rather than a glorified
wagnerrp authored
181 {
182 int newid = 0;
183
184 QList<FileSystemInfo>::iterator it1, it2;
185 for (it1 = disks.begin(); it1 != disks.end(); ++it1)
186 {
187 if (it1->getFSysID() == -1)
188 {
189 it1->setFSysID(newid++);
190 if (merge)
191 it1->setPath(it1->getHostname().section(".", 0, 0)
192 + ":" + it1->getPath());
193 }
194
195 for (it2 = it1+1; it2 != disks.end(); ++it2)
196 {
197 if (it2->getFSysID() != -1) // disk has already been matched
198 continue;
199
200 int bSize = max(32, max(it1->getBlockSize(), it2->getBlockSize())
201 / 1024);
83a03aa @wagnerrp size_t type corrections within the scope of the autoexpirer
wagnerrp authored
202 int64_t diffSize = it1->getTotalSpace() - it2->getTotalSpace();
203 int64_t diffUsed = it1->getUsedSpace() - it2->getUsedSpace();
65e877a @wagnerrp Transition FileSystemInfo into a proper class rather than a glorified
wagnerrp authored
204
205 if (diffSize < 0)
206 diffSize = 0 - diffSize;
207 if (diffUsed < 0)
208 diffUsed = 0 - diffUsed;
209
83a03aa @wagnerrp size_t type corrections within the scope of the autoexpirer
wagnerrp authored
210 if ((diffSize <= bSize) && (diffUsed <= fuzz))
65e877a @wagnerrp Transition FileSystemInfo into a proper class rather than a glorified
wagnerrp authored
211 {
212 it2->setFSysID(it1->getFSysID());
213
214 if (merge)
215 {
216 if (!it1->getHostname().contains(it2->getHostname()))
217 it1->setHostname(it1->getHostname()
218 + "," + it2->getHostname());
219 it1->setPath(it1->getPath() + ","
220 + it2->getHostname().section(".", 0, 0) + ":"
221 + it2->getPath());
222 disks.erase(it2);
223 it2 = it1;
224 }
225 }
226 }
227 }
228 }
229
230 void FileSystemInfo::PopulateDiskSpace(void)
231 {
83a03aa @wagnerrp size_t type corrections within the scope of the autoexpirer
wagnerrp authored
232 int64_t total = -1, used = -1;
f894ecb @daniel-kristjansson Convert {to,from}Ascii to {to,from}Latin1.
daniel-kristjansson authored
233 getDiskSpace(getPath().toLatin1().constData(), total, used);
65e877a @wagnerrp Transition FileSystemInfo into a proper class rather than a glorified
wagnerrp authored
234 setTotalSpace(total);
235 setUsedSpace(used);
236 }
237
238 void FileSystemInfo::PopulateFSProp(void)
239 {
240 struct statfs statbuf;
241 memset(&statbuf, 0, sizeof(statbuf));
242
243 if (!statfs(getPath().toLocal8Bit().constData(), &statbuf))
244 {
245 #if CONFIG_DARWIN
246 char *fstypename = statbuf.f_fstypename;
247 if ((!strcmp(fstypename, "nfs")) || // NFS|FTP
248 (!strcmp(fstypename, "afpfs")) || // AppleShare
249 (!strcmp(fstypename, "smbfs"))) // SMB
250 setLocal(false);
251 #elif __linux__
252 long fstype = statbuf.f_type;
253 if ((fstype == 0x6969) || // NFS
254 (fstype == 0x517B) || // SMB
255 (fstype == (long)0xFF534D42)) // CIFS
256 setLocal(false);
257 #endif
258 setBlockSize(statbuf.f_bsize);
259 }
260 }
Something went wrong with that request. Please try again.