~ubuntu-branches/ubuntu/natty/kdeadmin/natty-proposed

« back to all changes in this revision

Viewing changes to lilo-config/common/Disks.cc

  • Committer: Bazaar Package Importer
  • Author(s): Alessandro Ghersi
  • Date: 2010-02-27 21:36:45 UTC
  • mfrom: (1.2.40 upstream)
  • Revision ID: james.westby@ubuntu.com-20100227213645-ronst6mahkgf6j9w
Tags: 4:4.4.1-0ubuntu1
* New upstream release
  - Bump build-depends
  - Update kcron.install

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Disks.cc
2
 
**
3
 
** Copyright (C) 2000,2001 by Bernhard Rosenkraenzer
4
 
**
5
 
** Contributions by A. Seigo and W. Bastian.
6
 
**
7
 
*/
8
 
 
9
 
/*
10
 
** This program is free software; you can redistribute it and/or modify
11
 
** it under the terms of the GNU General Public License as published by
12
 
** the Free Software Foundation; either version 2 of the License, or
13
 
** (at your option) any later version.
14
 
**
15
 
** This program is distributed in the hope that it will be useful,
16
 
** but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
 
** GNU General Public License for more details.
19
 
**
20
 
** You should have received a copy of the GNU General Public License
21
 
** along with this program in a file called COPYING; if not, write to
22
 
** the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
23
 
** MA 02110-1301, USA.
24
 
*/
25
 
 
26
 
/*
27
 
** Bug reports and questions can be sent to <kde-devel@kde.org>
28
 
*/
29
 
 
30
 
#include "Disks.h"
31
 
#include <unistd.h>
32
 
#include <stdlib.h>
33
 
#include <stdio.h>
34
 
#include <fcntl.h>
35
 
#include <ctype.h>
36
 
#include <string.h>
37
 
 
38
 
StringList ptable::disklist()
39
 
{
40
 
        /* The basics behind scanning for a disk are simple: If it can be     *
41
 
         * opened, it exists.                                                 *
42
 
         * Using access() is not sufficient - that's successful if the device *
43
 
         * node exists but there's no actual device.                          *
44
 
         * Checking /proc/devices would work, but it wouldn't autoload modules*
45
 
         * so chances are we'd be missing something.                          */
46
 
        StringList dsk;
47
 
        int fd;
48
 
        bool finished=false;
49
 
        #define CHECK_DEV(d)    if((fd=open(d, O_RDONLY))<0) { \
50
 
                                        finished=true; \
51
 
                                        continue; \
52
 
                                } \
53
 
                                close(fd); \
54
 
                                dsk+=d
55
 
        String dev="/dev/hd";   // IDE devices: /dev/hd[a-t]
56
 
        for(char a='a'; a<='t'; a++) {
57
 
                // We can't use finished for IDE drives - it's perfectly OK
58
 
                // to have a /dev/hdc but no /dev/hdb.
59
 
                CHECK_DEV(dev+a);
60
 
        }
61
 
        finished=false;
62
 
        dev="/dev/sd";          // SCSI disks 0-25
63
 
        for(char a='a'; a<='z' && !finished; a++) {
64
 
                CHECK_DEV(dev+a);
65
 
        }
66
 
        for(char prefix='a'; prefix <='d' && !finished; prefix++) {
67
 
                dev="/dev/sd" + prefix; // SCSI disks 26-127
68
 
                for(char a='a'; (a<='x' || (a<='z' && prefix<'d')) && !finished; a++) {
69
 
                        CHECK_DEV(dev+a);
70
 
                }
71
 
        }
72
 
        finished=false;
73
 
        dev="/dev/i2o/hd";      // I2O disks 0-25: /dev/i2o/hd[a-z]
74
 
        for(char a='a'; a<='z' && !finished; a++) {
75
 
                CHECK_DEV(dev+a);
76
 
        }
77
 
        for(char prefix='a'; prefix <='d' && !finished; prefix++) {
78
 
                dev="/dev/i2o/hd" + prefix; // I2O disks 26-127
79
 
                for(char a='a'; (a<='x' || (a<='z' && prefix<'d')) && !finished; a++) {
80
 
                        CHECK_DEV(dev+a);
81
 
                }
82
 
        }
83
 
        finished=false;
84
 
        dev="/dev/pd";          // Parallel port disks: /dev/pd[a-d]
85
 
        for(char a='a'; a<='d' && !finished; a++) {
86
 
                CHECK_DEV(dev+a);
87
 
        }
88
 
        finished=false;
89
 
        for(unsigned int i=0; i<=31 && !finished; i++) { // Software RAID
90
 
                String device;
91
 
                device.sprintf("/dev/md%u", i);
92
 
                CHECK_DEV(device);
93
 
        }
94
 
        finished=false;
95
 
        for(unsigned int i=0; i<=7; i++) { //Compaq Smart Array
96
 
                for(unsigned int j=0; j<=15 && !finished; j++) {
97
 
                        String device;
98
 
                        device.sprintf("/dev/ida/c%ud%u", i, j);
99
 
                        CHECK_DEV(device);
100
 
                }
101
 
        }
102
 
        finished=false;
103
 
        for(unsigned int i=0; i<=7; i++) { // Mylex DAC960
104
 
                for(unsigned int j=0; j<=31 && !finished; j++) {
105
 
                        String device;
106
 
                        device.sprintf("/dev/rd/c%ud%u", i, j);
107
 
                        CHECK_DEV(device);
108
 
                }
109
 
        }
110
 
        finished=false;
111
 
        dev="/dev/ed";          // MCA ESDI harddisks: /dev/ed[ab]
112
 
        for(char a='a'; a<='b' && !finished; a++) {
113
 
                CHECK_DEV(dev+a);
114
 
        }
115
 
        finished=false;
116
 
        dev="/dev/xd";          // XT (8-bit) harddisks: /dev/xd[ab]
117
 
        for(char a='a'; a<='b' && !finished; a++) {
118
 
                CHECK_DEV(dev+a);
119
 
        }
120
 
        return dsk;
121
 
}
122
 
StringList ptable::partlist()
123
 
{
124
 
        StringList s;
125
 
        StringList d=disklist();
126
 
        for(StringList::const_iterator it=d.begin(); it!=d.end(); it++) {
127
 
                for(int i=1; i<32; i++) {
128
 
                        String drive;
129
 
                        drive.sprintf("%s%u", (*it).cstr(), i);
130
 
                        int fd=open(drive, O_RDONLY);
131
 
                        if(fd>=0) {
132
 
                                char test;
133
 
                                if(read(fd, &test, 1)>0)
134
 
                                        s += drive;
135
 
                                close(fd);
136
 
                        } else
137
 
                                break;
138
 
                }
139
 
        }
140
 
        return s;
141
 
}
142
 
ptable::ptable(StringList const &disks)
143
 
{
144
 
        partition.clear();
145
 
        id.clear();
146
 
        mountpt.clear();
147
 
        for(StringList::const_iterator it=disks.begin(); it!=disks.end(); it++)
148
 
                scandisk(*it);
149
 
}
150
 
ptable::ptable(String const &disk)
151
 
{
152
 
        partition.clear();
153
 
        id.clear();
154
 
        mountpt.clear();
155
 
        scandisk(disk);
156
 
}
157
 
void ptable::scandisk(String const &disk)
158
 
{
159
 
        String cmd;
160
 
        cmd.sprintf("fdisk -l %s 2>&1", (char*)disk);
161
 
        FILE *fdisk=popen(cmd, "r");
162
 
        char *buf=(char *) malloc(1024);
163
 
        String dev;
164
 
        while(fgets(buf, 1024, fdisk)) {
165
 
                if(strncmp("/dev/", buf, 5)==0) { // Partition entry
166
 
                        // We don't care about active vs. non-active partitions.
167
 
                        // Remove the sign.
168
 
                        while(strchr(buf, '*')) *strchr(buf, '*')=' ';
169
 
                        // blanks are blanks...
170
 
                        while(strchr(buf, '\t')) *strchr(buf, '\t')=' ';
171
 
                        // Get the device
172
 
                        *strchr(buf, ' ')=0;
173
 
                        dev=buf;
174
 
                        partition += buf;
175
 
                        // And figure out where/if it's mounted
176
 
                        mountpt[dev]=mountpoint(dev);
177
 
                        // Lastly, get the partition type.
178
 
                        strcpy(buf, buf+strlen(buf)+1);
179
 
                        while(isspace(*buf)) strcpy(buf, buf+1);
180
 
                        strcpy(buf, strchr(buf, ' ')); // skip Start
181
 
                        while(isspace(*buf)) strcpy(buf, buf+1);
182
 
                        strcpy(buf, strchr(buf, ' ')); // skip End
183
 
                        while(isspace(*buf)) strcpy(buf, buf+1);
184
 
                        strcpy(buf, strchr(buf, ' ')); // skip Blocks
185
 
                        while(isspace(*buf)) strcpy(buf, buf+1);
186
 
                        id[dev]=strtol(buf, NULL, 16);
187
 
                }
188
 
        }
189
 
        pclose(fdisk);
190
 
        free(buf);
191
 
}
192
 
String ptable::mountpoint(String const &device, bool fstab_fallback)
193
 
{
194
 
        char *buf=new char[1024];
195
 
        FILE *f=fopen("/etc/mtab", "r");
196
 
        String mp="";
197
 
        while(fgets(buf, 1024, f)) {
198
 
                if(strchr(buf,' '))
199
 
                        *strchr(buf, ' ')=0;
200
 
                if(strchr(buf,'\t'))
201
 
                        *strchr(buf, '\t')=0;
202
 
                if(device.cmp(buf)) {
203
 
                        strcpy(buf, buf+strlen(buf)+1);
204
 
                        while(isspace(*buf))
205
 
                                strcpy(buf, buf+1);
206
 
                        if(strchr(buf,' '))
207
 
                                *strchr(buf, ' ')=0;
208
 
                        if(strchr(buf,'\t'))
209
 
                                *strchr(buf, '\t')=0;
210
 
                        mp=buf;
211
 
                        mp=mp.simplifyWhiteSpace();
212
 
                        break;
213
 
                }
214
 
        }
215
 
        fclose(f);
216
 
        if(mp.empty() && fstab_fallback) {
217
 
                f=fopen("/etc/fstab", "r");
218
 
                while(fgets(buf, 1024, f)) {
219
 
                        if(strchr(buf,' '))
220
 
                                *strchr(buf, ' ')=0;
221
 
                        if(strchr(buf,'\t'))
222
 
                                *strchr(buf, '\t')=0;
223
 
                        if(device.cmp(buf)) {
224
 
                                strcpy(buf, buf+strlen(buf)+1);
225
 
                                while(isspace(*buf))
226
 
                                        strcpy(buf, buf+1);
227
 
                                if(strchr(buf,' '))
228
 
                                        *strchr(buf, ' ')=0;
229
 
                                if(strchr(buf,'\t'))
230
 
                                        *strchr(buf, '\t')=0;
231
 
                                mp=buf;
232
 
                                mp=mp.simplifyWhiteSpace();
233
 
                                break;
234
 
                        }
235
 
                }
236
 
                fclose(f);
237
 
        }
238
 
        delete [] buf;
239
 
        return mp;
240
 
}
241
 
String ptable::device(String const &mountpt, bool fstab_fallback)
242
 
{
243
 
        char *buf=new char[1024];
244
 
        FILE *f=fopen("/etc/mtab", "r");
245
 
        String dev="";
246
 
        while(fgets(buf, 1024, f)) {
247
 
                if(strchr(buf,' '))
248
 
                        *strchr(buf, ' ')=0;
249
 
                if(strchr(buf,'\t'))
250
 
                        *strchr(buf, '\t')=0;
251
 
                String device=buf;
252
 
                strcpy(buf, buf+strlen(buf)+1);
253
 
                while(isspace(*buf))
254
 
                        strcpy(buf, buf+1);
255
 
                if(strchr(buf,' '))
256
 
                        *strchr(buf, ' ')=0;
257
 
                if(strchr(buf,'\t'))
258
 
                        *strchr(buf, '\t')=0;
259
 
                String mp=buf;
260
 
                mp=mp.simplifyWhiteSpace();
261
 
                if(mp==mountpt) {
262
 
                        dev=device;
263
 
                        break;
264
 
                }
265
 
        }
266
 
        fclose(f);
267
 
 
268
 
        if(dev.empty() && fstab_fallback) {
269
 
                // The FS is not mounted - maybe it could be, though.
270
 
                f=fopen("/etc/fstab", "r");
271
 
                while(fgets(buf, 1024, f)) {
272
 
                        if(strchr(buf,' '))
273
 
                                *strchr(buf, ' ')=0;
274
 
                        if(strchr(buf,'\t'))
275
 
                                *strchr(buf, '\t')=0;
276
 
                        String device=buf;
277
 
                        strcpy(buf, buf+strlen(buf)+1);
278
 
                        while(isspace(*buf))
279
 
                                strcpy(buf, buf+1);
280
 
                        if(strchr(buf,' '))
281
 
                                *strchr(buf, ' ')=0;
282
 
                        if(strchr(buf,'\t'))
283
 
                                *strchr(buf, '\t')=0;
284
 
                        String mp=buf;
285
 
                        mp=mp.simplifyWhiteSpace();
286
 
                        if(mp==mountpt) {
287
 
                                dev=device;
288
 
                                break;
289
 
                        }
290
 
                }
291
 
                fclose(f);
292
 
        }
293
 
        delete [] buf;
294
 
        return dev;
295
 
}