github.com/bgentry/go@v0.0.0-20150121062915-6cf5a733d54d/src/lib9/_p9dir.c (about) 1 // +build !plan9 2 3 /* 4 Plan 9 from User Space src/lib9/_p9dir.c 5 http://code.swtch.com/plan9port/src/tip/src/lib9/_p9dir.c 6 7 Copyright 2001-2007 Russ Cox. All Rights Reserved. 8 9 Permission is hereby granted, free of charge, to any person obtaining a copy 10 of this software and associated documentation files (the "Software"), to deal 11 in the Software without restriction, including without limitation the rights 12 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 copies of the Software, and to permit persons to whom the Software is 14 furnished to do so, subject to the following conditions: 15 16 The above copyright notice and this permission notice shall be included in 17 all copies or substantial portions of the Software. 18 19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 25 THE SOFTWARE. 26 */ 27 28 #include <u.h> 29 #define NOPLAN9DEFINES 30 #include <libc.h> 31 #include <sys/types.h> 32 #include <sys/stat.h> 33 #include <dirent.h> 34 35 /* 36 * Caching the last group and passwd looked up is 37 * a significant win (stupidly enough) on most systems. 38 * It's not safe for threaded programs, but neither is using 39 * getpwnam in the first place, so I'm not too worried. 40 */ 41 int 42 _p9dir(struct stat *lst, struct stat *st, char *name, Dir *d, char **str, char *estr) 43 { 44 char *s; 45 char tmp[20]; 46 int sz, fd; 47 48 #ifdef _WIN32 49 USED(lst); 50 #endif 51 fd = -1; 52 USED(fd); 53 sz = 0; 54 if(d) 55 memset(d, 0, sizeof *d); 56 57 /* name */ 58 s = strrchr(name, '/'); 59 if(s) 60 s++; 61 if(!s || !*s) 62 s = name; 63 if(*s == '/') 64 s++; 65 if(*s == 0) 66 s = "/"; 67 if(d){ 68 if(*str + strlen(s)+1 > estr) 69 d->name = "oops"; 70 else{ 71 strcpy(*str, s); 72 d->name = *str; 73 *str += strlen(*str)+1; 74 } 75 } 76 sz += (int)strlen(s)+1; 77 78 /* user */ 79 snprint(tmp, sizeof tmp, "%d", (int)st->st_uid); 80 s = tmp; 81 sz += (int)strlen(s)+1; 82 if(d){ 83 if(*str+strlen(s)+1 > estr) 84 d->uid = "oops"; 85 else{ 86 strcpy(*str, s); 87 d->uid = *str; 88 *str += strlen(*str)+1; 89 } 90 } 91 92 /* group */ 93 snprint(tmp, sizeof tmp, "%d", (int)st->st_gid); 94 s = tmp; 95 sz += (int)strlen(s)+1; 96 if(d){ 97 if(*str + strlen(s)+1 > estr) 98 d->gid = "oops"; 99 else{ 100 strcpy(*str, s); 101 d->gid = *str; 102 *str += strlen(*str)+1; 103 } 104 } 105 106 if(d){ 107 d->type = 'M'; 108 109 d->muid = ""; 110 d->qid.path = ((uvlong)st->st_dev<<32) | st->st_ino; 111 #ifdef _HAVESTGEN 112 d->qid.vers = st->st_gen; 113 #endif 114 if(d->qid.vers == 0) 115 d->qid.vers = (ulong)(st->st_mtime + st->st_ctime); 116 d->mode = st->st_mode&0777; 117 d->atime = (ulong)st->st_atime; 118 d->mtime = (ulong)st->st_mtime; 119 d->length = st->st_size; 120 121 if(S_ISDIR(st->st_mode)){ 122 d->length = 0; 123 d->mode |= DMDIR; 124 d->qid.type = QTDIR; 125 } 126 #ifdef S_ISLNK 127 if(S_ISLNK(lst->st_mode)) /* yes, lst not st */ 128 d->mode |= DMSYMLINK; 129 #endif 130 if(S_ISFIFO(st->st_mode)) 131 d->mode |= DMNAMEDPIPE; 132 #ifdef S_ISSOCK 133 if(S_ISSOCK(st->st_mode)) 134 d->mode |= DMSOCKET; 135 #endif 136 if(S_ISBLK(st->st_mode)){ 137 d->mode |= DMDEVICE; 138 d->qid.path = ('b'<<16)|st->st_rdev; 139 } 140 if(S_ISCHR(st->st_mode)){ 141 d->mode |= DMDEVICE; 142 d->qid.path = ('c'<<16)|st->st_rdev; 143 } 144 /* fetch real size for disks */ 145 if(S_ISBLK(st->st_mode) && (fd = open(name, O_RDONLY)) >= 0){ 146 d->length = 0; 147 close(fd); 148 } 149 #if defined(DIOCGMEDIASIZE) 150 if(isdisk(st)){ 151 int fd; 152 off_t mediasize; 153 154 if((fd = open(name, O_RDONLY)) >= 0){ 155 if(ioctl(fd, DIOCGMEDIASIZE, &mediasize) >= 0) 156 d->length = mediasize; 157 close(fd); 158 } 159 } 160 #elif defined(_HAVEDISKLABEL) 161 if(isdisk(st)){ 162 int fd, n; 163 struct disklabel lab; 164 165 if((fd = open(name, O_RDONLY)) < 0) 166 goto nosize; 167 if(ioctl(fd, DIOCGDINFO, &lab) < 0) 168 goto nosize; 169 n = minor(st->st_rdev)&7; 170 if(n >= lab.d_npartitions) 171 goto nosize; 172 173 d->length = (vlong)(lab.d_partitions[n].p_size) * lab.d_secsize; 174 175 nosize: 176 if(fd >= 0) 177 close(fd); 178 } 179 #endif 180 } 181 182 return sz; 183 } 184