| 1 |
/* $Id$ */ |
| 2 |
/* |
| 3 |
* Copyright (c) 1995-1996 Sam Leffler |
| 4 |
* Copyright (c) 1995-1996 Silicon Graphics, Inc. |
| 5 |
* HylaFAX is a trademark of Silicon Graphics |
| 6 |
* |
| 7 |
* Permission to use, copy, modify, distribute, and sell this software and |
| 8 |
* its documentation for any purpose is hereby granted without fee, provided |
| 9 |
* that (i) the above copyright notices and this permission notice appear in |
| 10 |
* all copies of the software and related documentation, and (ii) the names of |
| 11 |
* Sam Leffler and Silicon Graphics may not be used in any advertising or |
| 12 |
* publicity relating to the software without the specific, prior written |
| 13 |
* permission of Sam Leffler and Silicon Graphics. |
| 14 |
* |
| 15 |
* THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, |
| 16 |
* EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY |
| 17 |
* WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. |
| 18 |
* |
| 19 |
* IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR |
| 20 |
* ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, |
| 21 |
* OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, |
| 22 |
* WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF |
| 23 |
* LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE |
| 24 |
* OF THIS SOFTWARE. |
| 25 |
*/ |
| 26 |
#include "FileCache.h" |
| 27 |
#include "Sys.h" |
| 28 |
|
| 29 |
u_int FileCache::master = 0; // master serial number |
| 30 |
FileCache* FileCache::cache[4096]; // cache of stat results |
| 31 |
#define CACHESIZE (sizeof (cache) / sizeof (cache[0])) |
| 32 |
|
| 33 |
// statistics |
| 34 |
u_int FileCache::lookups = 0; // total # lookups |
| 35 |
u_int FileCache::hits = 0; // # lookups that hit in the cache |
| 36 |
u_int FileCache::probes = 0; // total # probes during lookups |
| 37 |
u_int FileCache::displaced = 0; // # entries reused |
| 38 |
u_int FileCache::flushed = 0; // # entries flushed |
| 39 |
|
| 40 |
void |
| 41 |
FileCache::printStats(FILE* fd) |
| 42 |
{ |
| 43 |
fprintf(fd, " File cache: %u lookups, %u hits (%.1f%%), %.1f avg probes\r\n" |
| 44 |
, lookups |
| 45 |
, hits |
| 46 |
#define NZ(v) ((v) == 0 ? 1 : (v)) |
| 47 |
, (100.*hits)/NZ(lookups) |
| 48 |
, float(lookups+probes)/float(NZ(lookups)) |
| 49 |
); |
| 50 |
u_int n = 0; |
| 51 |
u_int space = 0; |
| 52 |
for (u_int i = 0; i < CACHESIZE; i++) { |
| 53 |
const FileCache* fi = cache[i]; |
| 54 |
if (fi) { |
| 55 |
n++; |
| 56 |
space += sizeof (*fi) + fi->name.length(); |
| 57 |
} |
| 58 |
} |
| 59 |
fprintf(fd, " %u entries (%.1f KB), %u entries displaced, %u entries flushed\r\n" |
| 60 |
, n |
| 61 |
, space / 1024. |
| 62 |
, displaced |
| 63 |
, flushed |
| 64 |
); |
| 65 |
} |
| 66 |
|
| 67 |
FileCache::FileCache() {} |
| 68 |
FileCache::~FileCache() {} |
| 69 |
|
| 70 |
void |
| 71 |
FileCache::reset(void) |
| 72 |
{ |
| 73 |
for (u_int i = 0; i < CACHESIZE; i++) |
| 74 |
delete cache[i]; |
| 75 |
memset(cache, 0, sizeof (cache)); |
| 76 |
master = 0; // doesn't matter??? |
| 77 |
} |
| 78 |
|
| 79 |
u_int |
| 80 |
FileCache::hash(const char* pathname) |
| 81 |
{ |
| 82 |
u_int h = 0; |
| 83 |
while (*pathname) |
| 84 |
h ^= *pathname++; |
| 85 |
return (h % CACHESIZE); |
| 86 |
} |
| 87 |
|
| 88 |
bool |
| 89 |
FileCache::lookup(const char* pathname, struct stat& sb, bool addToCache) |
| 90 |
{ |
| 91 |
lookups++; |
| 92 |
u_int h = hash(pathname); |
| 93 |
u_int maxprobes = 5; |
| 94 |
FileCache* fi = cache[h]; |
| 95 |
FileCache* oldest = fi; |
| 96 |
while (fi && --maxprobes) { |
| 97 |
probes++; |
| 98 |
if (fi->name == pathname) { |
| 99 |
fi->serial = master++; |
| 100 |
sb = fi->sb; |
| 101 |
hits++; |
| 102 |
return (true); |
| 103 |
} |
| 104 |
if (fi->serial < oldest->serial) |
| 105 |
oldest = fi; |
| 106 |
h = (u_int)(h*h) % CACHESIZE; |
| 107 |
fi = cache[h]; |
| 108 |
} |
| 109 |
/* |
| 110 |
* Pathname not found in the cache. |
| 111 |
*/ |
| 112 |
if (Sys::stat(pathname, sb) < 0) |
| 113 |
return (false); |
| 114 |
if (addToCache && pathname[0] != '.') { |
| 115 |
if (fi) { |
| 116 |
fi = oldest; |
| 117 |
displaced++; |
| 118 |
} else |
| 119 |
fi = cache[h] = new FileCache; |
| 120 |
fi->name = pathname; |
| 121 |
fi->serial = master++; |
| 122 |
fi->sb = sb; |
| 123 |
} |
| 124 |
return (true); |
| 125 |
} |
| 126 |
|
| 127 |
/* |
| 128 |
* Update the file mode for any in-cache entry. |
| 129 |
*/ |
| 130 |
bool |
| 131 |
FileCache::chmod(const char* pathname, mode_t mode) |
| 132 |
{ |
| 133 |
if (Sys::chmod(pathname, mode) < 0) |
| 134 |
return (false); |
| 135 |
lookups++; |
| 136 |
u_int h = hash(pathname); |
| 137 |
u_int maxprobes = 5; |
| 138 |
FileCache* fi = cache[h]; |
| 139 |
while (fi && --maxprobes) { |
| 140 |
probes++; |
| 141 |
if (fi->name == pathname) { |
| 142 |
hits++; |
| 143 |
fi->sb.st_mode = (fi->sb.st_mode&~0777) | (mode&0777); |
| 144 |
break; |
| 145 |
} |
| 146 |
h = (u_int)(h*h) % CACHESIZE; |
| 147 |
fi = cache[h]; |
| 148 |
} |
| 149 |
return (true); |
| 150 |
} |
| 151 |
|
| 152 |
/* |
| 153 |
* Update the file ownership for any in-cache entry. |
| 154 |
*/ |
| 155 |
bool |
| 156 |
FileCache::chown(const char* pathname, uid_t uid, gid_t gid) |
| 157 |
{ |
| 158 |
/* |
| 159 |
* For BSD-based systems chown is only permitted |
| 160 |
* by the super-user. We could optimize this work |
| 161 |
* to not swap the effective uid on System V-based |
| 162 |
* systems but it's not worth it since the majority |
| 163 |
* the calls to chown a file will be done only for |
| 164 |
* BSD-based systems (to deal with the filesystem |
| 165 |
* semantics forcing us to manually set the gid on |
| 166 |
* newly created files. |
| 167 |
*/ |
| 168 |
uid_t ouid = geteuid(); |
| 169 |
(void) seteuid(0); |
| 170 |
bool ok = (Sys::chown(pathname, uid, gid) >= 0); |
| 171 |
(void) seteuid(ouid); |
| 172 |
if (ok) { |
| 173 |
lookups++; |
| 174 |
u_int h = hash(pathname); |
| 175 |
u_int maxprobes = 5; |
| 176 |
FileCache* fi = cache[h]; |
| 177 |
while (fi && --maxprobes) { |
| 178 |
probes++; |
| 179 |
if (fi->name == pathname) { |
| 180 |
hits++; |
| 181 |
fi->sb.st_uid = uid; |
| 182 |
fi->sb.st_gid = gid; |
| 183 |
break; |
| 184 |
} |
| 185 |
h = (u_int)(h*h) % CACHESIZE; |
| 186 |
fi = cache[h]; |
| 187 |
} |
| 188 |
} |
| 189 |
return (ok); |
| 190 |
} |
| 191 |
|
| 192 |
/* |
| 193 |
* Like lookup, but if found in the cache, re-do the stat. |
| 194 |
*/ |
| 195 |
bool |
| 196 |
FileCache::update(const char* pathname, struct stat& sb, bool addToCache) |
| 197 |
{ |
| 198 |
lookups++; |
| 199 |
u_int h = hash(pathname); |
| 200 |
u_int maxprobes = 5; |
| 201 |
FileCache* fi = cache[h]; |
| 202 |
FileCache* oldest = fi; |
| 203 |
while (fi && --maxprobes) { |
| 204 |
probes++; |
| 205 |
if (fi->name == pathname) { |
| 206 |
if (Sys::stat(pathname, sb) >= 0) { |
| 207 |
hits++; |
| 208 |
fi->serial = master++; |
| 209 |
fi->sb = sb; |
| 210 |
return (true); |
| 211 |
} else { |
| 212 |
flushed++; |
| 213 |
cache[h] = NULL; |
| 214 |
delete fi; |
| 215 |
return (false); |
| 216 |
} |
| 217 |
} |
| 218 |
if (fi->serial < oldest->serial) |
| 219 |
oldest = fi; |
| 220 |
h = (u_int)(h*h) % CACHESIZE; |
| 221 |
fi = cache[h]; |
| 222 |
} |
| 223 |
/* |
| 224 |
* Pathname not found in the cache. |
| 225 |
*/ |
| 226 |
if (Sys::stat(pathname, sb) < 0) |
| 227 |
return (false); |
| 228 |
if (addToCache && pathname[0] != '.') { |
| 229 |
if (fi) { |
| 230 |
fi = oldest; |
| 231 |
displaced++; |
| 232 |
} else |
| 233 |
fi = cache[h] = new FileCache; |
| 234 |
fi->name = pathname; |
| 235 |
fi->serial = master++; |
| 236 |
fi->sb = sb; |
| 237 |
} |
| 238 |
return (true); |
| 239 |
} |
| 240 |
|
| 241 |
void |
| 242 |
FileCache::flush(const char* pathname) |
| 243 |
{ |
| 244 |
u_int h = hash(pathname); |
| 245 |
u_int maxprobes = 5; |
| 246 |
FileCache* fi = cache[h]; |
| 247 |
while (fi && --maxprobes) { |
| 248 |
if (fi->name == pathname) { |
| 249 |
flushed++; |
| 250 |
cache[h] = NULL; |
| 251 |
delete fi; |
| 252 |
break; |
| 253 |
} |
| 254 |
h = (u_int)(h*h) % CACHESIZE; |
| 255 |
fi = cache[h]; |
| 256 |
} |
| 257 |
} |