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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
|
/* Disks.cpp
**
** Copyright (C) 2000,2001 by Bernhard Rosenkraenzer
**
** Contributions by A. Seigo and W. Bastian.
**
*/
/*
** 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 of the License, 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 this program in a file called COPYING; if not, write to
** the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
** MA 02110-1301, USA.
*/
/*
** Bug reports and questions can be sent to kde-devel@kde.org
*/
#include "Disks.h"
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
StringList ptable::disklist()
{
/* The basics behind scanning for a disk are simple: If it can be *
* opened, it exists. *
* Using access() is not sufficient - that's successful if the device *
* node exists but there's no actual device. *
* Checking /proc/devices would work, but it wouldn't autoload modules*
* so chances are we'd be missing something. */
StringList dsk;
int fd;
bool finished=false;
#define CHECK_DEV(d) if((fd=open(d, O_RDONLY))<0) { \
finished=true; \
continue; \
} \
close(fd); \
dsk+=d
String dev="/dev/hd"; // IDE devices: /dev/hd[a-t]
for(char a='a'; a<='t'; a++) {
// We can't use finished for IDE drives - it's perfectly OK
// to have a /dev/hdc but no /dev/hdb.
CHECK_DEV(dev+a);
}
finished=false;
dev="/dev/sd"; // SCSI disks 0-25
for(char a='a'; a<='z' && !finished; a++) {
CHECK_DEV(dev+a);
}
for(char prefix='a'; prefix <='d' && !finished; prefix++) {
dev="/dev/sd" + prefix; // SCSI disks 26-127
for(char a='a'; (a<='x' || (a<='z' && prefix<'d')) && !finished; a++) {
CHECK_DEV(dev+a);
}
}
finished=false;
dev="/dev/i2o/hd"; // I2O disks 0-25: /dev/i2o/hd[a-z]
for(char a='a'; a<='z' && !finished; a++) {
CHECK_DEV(dev+a);
}
for(char prefix='a'; prefix <='d' && !finished; prefix++) {
dev="/dev/i2o/hd" + prefix; // I2O disks 26-127
for(char a='a'; (a<='x' || (a<='z' && prefix<'d')) && !finished; a++) {
CHECK_DEV(dev+a);
}
}
finished=false;
dev="/dev/pd"; // Parallel port disks: /dev/pd[a-d]
for(char a='a'; a<='d' && !finished; a++) {
CHECK_DEV(dev+a);
}
finished=false;
for(unsigned int i=0; i<=31 && !finished; i++) { // Software RAID
String device;
device.sprintf("/dev/md%u", i);
CHECK_DEV(device);
}
finished=false;
for(unsigned int i=0; i<=7; i++) { //Compaq Smart Array
for(unsigned int j=0; j<=15 && !finished; j++) {
String device;
device.sprintf("/dev/ida/c%ud%u", i, j);
CHECK_DEV(device);
}
}
finished=false;
for(unsigned int i=0; i<=7; i++) { // Mylex DAC960
for(unsigned int j=0; j<=31 && !finished; j++) {
String device;
device.sprintf("/dev/rd/c%ud%u", i, j);
CHECK_DEV(device);
}
}
finished=false;
dev="/dev/ed"; // MCA ESDI harddisks: /dev/ed[ab]
for(char a='a'; a<='b' && !finished; a++) {
CHECK_DEV(dev+a);
}
finished=false;
dev="/dev/xd"; // XT (8-bit) harddisks: /dev/xd[ab]
for(char a='a'; a<='b' && !finished; a++) {
CHECK_DEV(dev+a);
}
return dsk;
}
StringList ptable::partlist()
{
StringList s;
StringList d=disklist();
for(StringList::const_iterator it=d.begin(); it!=d.end(); it++) {
for(int i=1; i<32; i++) {
String drive;
drive.sprintf("%s%u", (*it).cstr(), i);
int fd=open(drive, O_RDONLY);
if(fd>=0) {
char test;
if(read(fd, &test, 1)>0)
s += drive;
close(fd);
} else
break;
}
}
return s;
}
ptable::ptable(StringList const &disks)
{
partition.clear();
id.clear();
mountpt.clear();
for(StringList::const_iterator it=disks.begin(); it!=disks.end(); it++)
scandisk(*it);
}
ptable::ptable(String const &disk)
{
partition.clear();
id.clear();
mountpt.clear();
scandisk(disk);
}
void ptable::scandisk(String const &disk)
{
String cmd;
cmd.sprintf("fdisk -l %s 2>&1", (char*)disk);
FILE *fdisk=popen(cmd, "r");
char *buf=(char *) malloc(1024);
String dev;
while(fgets(buf, 1024, fdisk)) {
if(strncmp("/dev/", buf, 5)==0) { // Partition entry
// We don't care about active vs. non-active partitions.
// Remove the sign.
while(strchr(buf, '*')) *strchr(buf, '*')=' ';
// blanks are blanks...
while(strchr(buf, '\t')) *strchr(buf, '\t')=' ';
// Get the device
*strchr(buf, ' ')=0;
dev=buf;
partition += buf;
// And figure out where/if it's mounted
mountpt[dev]=mountpoint(dev);
// Lastly, get the partition type.
strcpy(buf, buf+strlen(buf)+1);
while(isspace(*buf)) strcpy(buf, buf+1);
strcpy(buf, strchr(buf, ' ')); // skip Start
while(isspace(*buf)) strcpy(buf, buf+1);
strcpy(buf, strchr(buf, ' ')); // skip End
while(isspace(*buf)) strcpy(buf, buf+1);
strcpy(buf, strchr(buf, ' ')); // skip Blocks
while(isspace(*buf)) strcpy(buf, buf+1);
id[dev]=strtol(buf, NULL, 16);
}
}
pclose(fdisk);
free(buf);
}
String ptable::mountpoint(String const &device, bool fstab_fallback)
{
char *buf=new char[1024];
FILE *f=fopen("/etc/mtab", "r");
String mp="";
while(fgets(buf, 1024, f)) {
if(strchr(buf,' '))
*strchr(buf, ' ')=0;
if(strchr(buf,'\t'))
*strchr(buf, '\t')=0;
if(device.cmp(buf)) {
strcpy(buf, buf+strlen(buf)+1);
while(isspace(*buf))
strcpy(buf, buf+1);
if(strchr(buf,' '))
*strchr(buf, ' ')=0;
if(strchr(buf,'\t'))
*strchr(buf, '\t')=0;
mp=buf;
mp=mp.simplifyWhiteSpace();
break;
}
}
fclose(f);
if(mp.empty() && fstab_fallback) {
f=fopen("/etc/fstab", "r");
while(fgets(buf, 1024, f)) {
if(strchr(buf,' '))
*strchr(buf, ' ')=0;
if(strchr(buf,'\t'))
*strchr(buf, '\t')=0;
if(device.cmp(buf)) {
strcpy(buf, buf+strlen(buf)+1);
while(isspace(*buf))
strcpy(buf, buf+1);
if(strchr(buf,' '))
*strchr(buf, ' ')=0;
if(strchr(buf,'\t'))
*strchr(buf, '\t')=0;
mp=buf;
mp=mp.simplifyWhiteSpace();
break;
}
}
fclose(f);
}
delete buf;
return mp;
}
String ptable::device(String const &mountpt, bool fstab_fallback)
{
char *buf=new char[1024];
FILE *f=fopen("/etc/mtab", "r");
String dev="";
while(fgets(buf, 1024, f)) {
if(strchr(buf,' '))
*strchr(buf, ' ')=0;
if(strchr(buf,'\t'))
*strchr(buf, '\t')=0;
String device=buf;
strcpy(buf, buf+strlen(buf)+1);
while(isspace(*buf))
strcpy(buf, buf+1);
if(strchr(buf,' '))
*strchr(buf, ' ')=0;
if(strchr(buf,'\t'))
*strchr(buf, '\t')=0;
String mp=buf;
mp=mp.simplifyWhiteSpace();
if(mp==mountpt) {
dev=device;
break;
}
}
fclose(f);
if(dev.empty() && fstab_fallback) {
// The FS is not mounted - maybe it could be, though.
f=fopen("/etc/fstab", "r");
while(fgets(buf, 1024, f)) {
if(strchr(buf,' '))
*strchr(buf, ' ')=0;
if(strchr(buf,'\t'))
*strchr(buf, '\t')=0;
String device=buf;
strcpy(buf, buf+strlen(buf)+1);
while(isspace(*buf))
strcpy(buf, buf+1);
if(strchr(buf,' '))
*strchr(buf, ' ')=0;
if(strchr(buf,'\t'))
*strchr(buf, '\t')=0;
String mp=buf;
mp=mp.simplifyWhiteSpace();
if(mp==mountpt) {
dev=device;
break;
}
}
fclose(f);
}
delete buf;
return dev;
}
|