diff options
Diffstat (limited to 'mbbsd/cache.c')
-rw-r--r-- | mbbsd/cache.c | 164 |
1 files changed, 1 insertions, 163 deletions
diff --git a/mbbsd/cache.c b/mbbsd/cache.c index c824b4f6..7c4a1992 100644 --- a/mbbsd/cache.c +++ b/mbbsd/cache.c @@ -1,4 +1,4 @@ -/* $Id: cache.c,v 1.48 2002/08/19 14:47:41 in2 Exp $ */ +/* $Id: cache.c,v 1.49 2002/08/20 02:42:36 in2 Exp $ */ #include "bbs.h" #ifndef __FreeBSD__ @@ -1066,165 +1066,3 @@ hbflcheck(int bid, int uid) } return 1; } - -#ifdef MDCACHE -char * -cachepath(const char *fpath) -{ - static char cpath[128]; - char *ptr; - snprintf(cpath, sizeof(cpath), "cache/%s", fpath); - for (ptr = &cpath[6]; *ptr != 0; ++ptr) - if (*ptr == '/') - *ptr = '.'; - return cpath; -} - -int -updatemdcache(const char *CPATH, const char *fpath) -{ - /* - * save file to mdcache with *cpath and *fpath, return: -1 if error - * else the fd - */ - int len, sourcefd, targetfd; - char buf[1024], *cpath; - cpath = (CPATH == NULL) ? cachepath(fpath) : (char *)CPATH; - if ((sourcefd = open(fpath, O_RDONLY)) < 0) - return -1; - if ((targetfd = open(cpath, O_RDWR | O_CREAT | O_TRUNC, 0600)) < 0) - /* md is full? */ - return -1; - while ((len = read(sourcefd, buf, sizeof(buf))) > 0) - if (write(targetfd, buf, len) < len) { - /* md is full? */ - close(targetfd); - unlink(cpath); - lseek(sourcefd, 0, SEEK_SET); - return sourcefd; - } - close(sourcefd); - lseek(targetfd, 0, SEEK_SET); - return targetfd; -} - -int -mdcacheopen(char *fpath) -{ - int fd; - char *cpath; - if (strncmp(fpath, "boards/", 7) && strncmp(fpath, "etc/", 4)) - return open(fpath, O_RDONLY); - -#ifdef MDCACHEHITRATE - ++GLOBE[0]; -#endif - if ((fd = open((cpath = cachepath(fpath)), O_RDONLY)) < 0) - return updatemdcache(cpath, fpath); -#ifdef MDCACHEHITRATE - else - ++GLOBE[1]; -#endif - - return fd; -} -#endif - -#ifdef OUTTA_CACHE -#include <err.h> -static ssize_t Read(int fd, void *BUF, size_t nbytes) -{ - char *buf = (char *)BUF; - ssize_t thisgot, totalgot = nbytes; - while( nbytes > 0 ){ - if( (thisgot = read(fd, buf, nbytes)) <= 0 ) - err(1, "read from socket: "); - nbytes -= thisgot; - buf += thisgot; - } - return totalgot; -} - -static ssize_t Write(int fd, void *BUF, size_t nbytes) -{ - char *buf = (char *)BUF; - ssize_t thisgot, totalgot = nbytes; - while( nbytes > 0 ){ - if( (thisgot = write(fd, buf, nbytes)) <= 0 ) - err(1, "read from socket: "); - nbytes -= thisgot; - buf += thisgot; - } - return totalgot; -} - -int connectserver(char *host, int port) -{ - struct sockaddr_in servaddr; - int fd; - - if( (fd = socket(AF_INET, SOCK_STREAM, 0)) < 0 ) - err(1, "socket"); - bzero(&servaddr, sizeof(servaddr)); - servaddr.sin_family = AF_INET; - inet_pton(AF_INET, host, &servaddr.sin_addr); - servaddr.sin_port = htons(port); - if( connect(fd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0 ) - err(1, "connect"); - return fd; -} - -void *outta_malloc(size_t size, char id) -{ - OCbuf_t *ptr = (OCbuf_t *)malloc(OC_HEADERLEN + size); -#ifdef DEBUG - vmsg("outta_malloc(%d, %c)", size, id); -#endif - ptr->key.pid = getpid(); - ptr->key.cacheid = id; - ptr->length = size; - return ptr->buf; -} - -void outta_swapout(void **inptr) -{ - OCbuf_t *ptr = (OCbuf_t *)(*inptr - OC_HEADERLEN); - int fd, len; - fd = connectserver("10.2.1.2", 1477); - len = ptr->length + OC_HEADERLEN; -#ifdef DEBUG - vmsg("outta_swapout(%d)", len); -#endif - Write(fd, &len, sizeof(len)); - Write(fd, ptr, len); - close(fd); - free(ptr); - *inptr = NULL; -} - -void *outta_swapin(void **inptr, char cacheid) -{ - char buf[OC_HEADERLEN]; - OCbuf_t *ptr = (OCbuf_t *)buf; - int fd, len; - - fd = connectserver("10.2.1.2", 1477); - ptr->key.pid = getpid() + OC_pidadd; - ptr->key.cacheid = cacheid; - ptr->length = 0; - len = OC_HEADERLEN; - Write(fd, &len, sizeof(len)); - Write(fd, ptr, OC_HEADERLEN); - - Read(fd, &len, sizeof(len)); -#ifdef DEBUG - vmsg("outta_swapin(%d)", len); -#endif - ptr = (OCbuf_t *)malloc(len); - Read(fd, ptr, len); - close(fd); - - return (*inptr = ptr->buf); -} - -#endif |