~ubuntu-branches/ubuntu/lucid/9base/lucid

« back to all changes in this revision

Viewing changes to lib9/_p9dir.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Baumann
  • Date: 2009-08-20 17:34:06 UTC
  • mfrom: (6.2.2 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090820173406-xpwqa9ruyevvc0ut
Tags: 1:3-3
* Updating maintainer field.
* Updating vcs fields.
* Updating package to standards version 3.8.3.
* Updatin variables writing in rules to consistent style.

Show diffs side-by-side

added added

removed removed

Lines of Context:
7
7
#include <pwd.h>
8
8
#include <grp.h>
9
9
 
10
 
#if defined(__FreeBSD__) || defined(__OpenBSD__)
11
 
#include <sys/disklabel.h>
12
 
#include <sys/ioctl.h>
13
 
static int diskdev[] = {
14
 
        151,    /* aacd */
15
 
        116,    /* ad */
16
 
        157,    /* ar */
17
 
        118,    /* afd */
18
 
        133,    /* amrd */
19
 
        13,     /* da */
20
 
        102,    /* fla */
21
 
        109,    /* idad */
22
 
        95,     /* md */
23
 
        131,    /* mlxd */
24
 
        168,    /* pst */
25
 
        147,    /* twed */
26
 
        43,     /* vn */
27
 
        3,      /* wd */
28
 
        87,     /* wfd */
29
 
};
30
 
static int
31
 
isdisk(struct stat *st)
32
 
{
33
 
        int i, dev;
 
10
#if defined(__APPLE__)
 
11
#define _HAVESTGEN
 
12
#include <sys/disk.h>
 
13
static vlong
 
14
disksize(int fd, struct stat *st)
 
15
{
 
16
        u64int bc;
 
17
        u32int bs;
 
18
 
 
19
        bs = 0;
 
20
        bc = 0;
 
21
        ioctl(fd, DKIOCGETBLOCKSIZE, &bs);
 
22
        ioctl(fd, DKIOCGETBLOCKCOUNT, &bc);
 
23
        if(bs >0 && bc > 0)
 
24
                return bc*bs;
 
25
        return 0;
 
26
}
 
27
 
 
28
#elif defined(__FreeBSD__)
 
29
#define _HAVESTGEN
 
30
#include <sys/disk.h>
 
31
#include <sys/disklabel.h>
 
32
#include <sys/ioctl.h>
 
33
static vlong
 
34
disksize(int fd, struct stat *st)
 
35
{
 
36
        off_t mediasize;
 
37
        
 
38
        if(ioctl(fd, DIOCGMEDIASIZE, &mediasize) >= 0)
 
39
                return mediasize;
 
40
        return 0;
 
41
}
 
42
 
 
43
#elif defined(__OpenBSD__)
 
44
#define _HAVESTGEN
 
45
#include <sys/disklabel.h>
 
46
#include <sys/ioctl.h>
 
47
static vlong
 
48
disksize(int fd, struct stat *st)
 
49
{
 
50
        struct disklabel lab;
 
51
        int n;
34
52
 
35
53
        if(!S_ISCHR(st->st_mode))
36
54
                return 0;
37
 
        dev = major(st->st_rdev);
38
 
        for(i=0; i<nelem(diskdev); i++)
39
 
                if(diskdev[i] == dev)
40
 
                        return 1;
41
 
        return 0;
 
55
        if(ioctl(fd, DIOCGDINFO, &lab) < 0)
 
56
                return 0;
 
57
        n = minor(st->st_rdev)&7;
 
58
        if(n >= lab.d_npartitions)
 
59
                return 0;
 
60
        return (vlong)lab.d_partitions[n].p_size * lab.d_secsize;
42
61
}
43
 
#define _HAVEDISKLABEL
44
 
#endif
45
62
 
46
 
#if defined(__linux__)
 
63
#elif defined(__linux__)
47
64
#include <linux/hdreg.h>
48
65
#include <linux/fs.h>
49
66
#include <sys/ioctl.h>
50
67
#undef major
51
68
#define major(dev) ((int)(((dev) >> 8) & 0xff))
52
69
static vlong
53
 
disksize(int fd, int dev)
 
70
disksize(int fd, struct stat *st)
54
71
{
55
72
        u64int u64;
56
73
        long l;
64
81
                return u64;
65
82
#endif
66
83
        if(ioctl(fd, BLKGETSIZE, &l) >= 0)
67
 
                return (vlong)l*512;
 
84
                return l*512;
68
85
        if(ioctl(fd, HDIO_GETGEO, &geo) >= 0)
69
86
                return (vlong)geo.heads*geo.sectors*geo.cylinders*512;
70
87
        return 0;
71
88
}
72
 
#define _HAVEDISKSIZE
73
 
#endif
74
 
 
75
 
#if !defined(__linux__) && !defined(__sun__)
76
 
#define _HAVESTGEN
77
 
#endif
78
 
 
 
89
 
 
90
#else
 
91
static vlong
 
92
disksize(int fd, struct stat *st)
 
93
{
 
94
        return 0;
 
95
}
 
96
#endif
 
97
 
 
98
int _p9usepwlibrary = 1;
79
99
/*
80
100
 * Caching the last group and passwd looked up is
81
101
 * a significant win (stupidly enough) on most systems.
122
142
        /* user */
123
143
        if(p && st->st_uid == uid && p->pw_uid == uid)
124
144
                ;
125
 
        else{
 
145
        else if(_p9usepwlibrary){
126
146
                p = getpwuid(st->st_uid);
127
147
                uid = st->st_uid;
128
148
        }
129
 
        if(p == nil){
 
149
        if(p == nil || st->st_uid != uid || p->pw_uid != uid){
130
150
                snprint(tmp, sizeof tmp, "%d", (int)st->st_uid);
131
151
                s = tmp;
132
152
        }else
145
165
        /* group */
146
166
        if(g && st->st_gid == gid && g->gr_gid == gid)
147
167
                ;
148
 
        else{
 
168
        else if(_p9usepwlibrary){
149
169
                g = getgrgid(st->st_gid);
150
170
                gid = st->st_gid;
151
171
        }
152
 
        if(g == nil){
 
172
        if(g == nil || st->st_gid != gid || g->gr_gid != gid){
153
173
                snprint(tmp, sizeof tmp, "%d", (int)st->st_gid);
154
174
                s = tmp;
155
175
        }else
173
193
#ifdef _HAVESTGEN
174
194
                d->qid.vers = st->st_gen;
175
195
#endif
 
196
                if(d->qid.vers == 0)
 
197
                        d->qid.vers = st->st_mtime + st->st_ctime;
176
198
                d->mode = st->st_mode&0777;
177
199
                d->atime = st->st_atime;
178
200
                d->mtime = st->st_mtime;
179
201
                d->length = st->st_size;
180
202
 
181
 
                if(S_ISDIR(st->st_mode)){
 
203
                if(S_ISLNK(lst->st_mode)){      /* yes, lst not st */
 
204
                        d->mode |= DMSYMLINK;
 
205
                        d->length = lst->st_size;
 
206
                }
 
207
                else if(S_ISDIR(st->st_mode)){
182
208
                        d->length = 0;
183
209
                        d->mode |= DMDIR;
184
210
                        d->qid.type = QTDIR;
185
211
                }
186
 
                if(S_ISLNK(lst->st_mode))       /* yes, lst not st */
187
 
                        d->mode |= DMSYMLINK;
188
 
                if(S_ISFIFO(st->st_mode))
 
212
                else if(S_ISFIFO(st->st_mode))
189
213
                        d->mode |= DMNAMEDPIPE;
190
 
                if(S_ISSOCK(st->st_mode))
 
214
                else if(S_ISSOCK(st->st_mode))
191
215
                        d->mode |= DMSOCKET;
192
 
                if(S_ISBLK(st->st_mode)){
 
216
                else if(S_ISBLK(st->st_mode)){
193
217
                        d->mode |= DMDEVICE;
194
218
                        d->qid.path = ('b'<<16)|st->st_rdev;
195
219
                }
196
 
                if(S_ISCHR(st->st_mode)){
 
220
                else if(S_ISCHR(st->st_mode)){
197
221
                        d->mode |= DMDEVICE;
198
222
                        d->qid.path = ('c'<<16)|st->st_rdev;
199
223
                }
200
224
                /* fetch real size for disks */
201
 
#ifdef _HAVEDISKSIZE
202
 
                if(S_ISBLK(st->st_mode) && (fd = open(name, O_RDONLY)) >= 0){
203
 
                        d->length = disksize(fd, major(st->st_dev));
204
 
                        close(fd);
205
 
                }
206
 
#endif
207
 
#ifdef _HAVEDISKLABEL
208
 
                if(isdisk(st)){
209
 
                        int fd, n;
210
 
                        struct disklabel lab;
211
 
 
212
 
                        if((fd = open(name, O_RDONLY)) < 0)
213
 
                                goto nosize;
214
 
                        if(ioctl(fd, DIOCGDINFO, &lab) < 0)
215
 
                                goto nosize;
216
 
                        n = minor(st->st_rdev)&7;
217
 
                        if(n >= lab.d_npartitions)
218
 
                                goto nosize;
219
 
 
220
 
                        d->length = (vlong)(lab.d_partitions[n].p_size) * lab.d_secsize;
221
 
 
222
 
                nosize:
223
 
                        if(fd >= 0)
 
225
                if(S_ISBLK(lst->st_mode) || S_ISCHR(lst->st_mode)){
 
226
                        if((fd = open(name, O_RDONLY)) >= 0){
 
227
                                d->length = disksize(fd, st);
224
228
                                close(fd);
 
229
                        }
225
230
                }
226
 
#endif
227
231
        }
228
232
 
229
233
        return sz;