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