#include #include #include #include "ftp.h" #include "dirinfo.h" #include "fhandle.h" // Size 2 Cache DIRINFO * cache[2] = {NULL,NULL} ; FtpFh cacheFtpFh [2] = {(FtpFh) NULL, (FtpFh) NULL}; int DirLRU = 0; // Invalidates the Caches void InvalidateCache(void) { int i; for(i=0; i<2; i++) { if (cache[i]) DirInfo_Destroy(cache[i]); cache[i] = NULL; cacheFtpFh[i] = (FtpFh) NULL; } } // Returns negative if a file does not exist int CacheValidPath(FtpFh ftpfh, char *path) { int doRet; SOCKET socket; printf("CacheValidPath %s ", path); if (!ftpfh) return -1; // Check the caches if (ftpfh == cacheFtpFh[0]) { printf("Hit\n"); DirLRU = 1; // advance path if (strchr(path, '/')) path = strrchr(path, '/')+1; return DirInfo_Find(cache[0], path); } if (ftpfh == cacheFtpFh[1]) { printf("Hit\n"); DirLRU = 0; // advance path if (strchr(path, '/')) path = strrchr(path, '/')+1; return DirInfo_Find(cache[1], path); } //Missed... printf("Miss\n"); //Do the nessesary FTP stuff and rebuild cache if (cache[DirLRU]) DirInfo_Destroy(cache[DirLRU]); cache[DirLRU] = DirInfo_Create(); cacheFtpFh[DirLRU] = ftpfh; socket = ConnList_GetSocket(cacheFtpFh[DirLRU]->connlist, cacheFtpFh[DirLRU]->connindex); doRet = DoDirList2(socket, cacheFtpFh[DirLRU]->fullpath, cache[DirLRU]); if (!doRet) { // if zero reconnect socket = ConnList_Connect(cacheFtpFh[DirLRU]->connlist, cacheFtpFh[DirLRU]->connindex); doRet = DoDirList2(socket, cacheFtpFh[DirLRU]->fullpath, cache[DirLRU]); } if (doRet != FTP_COMPLETE) return -1; DirLRU = DirLRU?0:1; return CacheValidPath(ftpfh, path); // execute } // Returns the DIRINFO for the directory specified // by dirftp. DIRINFO * CacheDirList2(FtpFh dirftpfh) { SOCKET socket; int doRet; printf("CacheDirList2(%d) cache=%d ",dirftpfh->fh, (cacheFtpFh[DirLRU])?cacheFtpFh[DirLRU]->fh:-999); // Check the caches... if (dirftpfh == cacheFtpFh[0]) { DirLRU = 1; return cache[0]; } if (dirftpfh == cacheFtpFh[1]) { DirLRU = 0; return cache[1]; } printf("Miss\n"); // Do the nessesary FTP stuff and rebuild the cache. if (cache[DirLRU]) DirInfo_Destroy(cache[DirLRU]); cache[DirLRU] = DirInfo_Create(); cacheFtpFh[DirLRU] = dirftpfh; socket = ConnList_GetSocket(cacheFtpFh[DirLRU]->connlist, cacheFtpFh[DirLRU]->connindex), doRet = DoDirList2(socket, cacheFtpFh[DirLRU]->fullpath, cache[DirLRU]); if (!doRet) { socket = ConnList_Connect(cacheFtpFh[DirLRU]->connlist, cacheFtpFh[DirLRU]->connindex); doRet = DoDirList2(socket, cacheFtpFh[DirLRU]->fullpath, cache[DirLRU]); } if (doRet != FTP_COMPLETE) return NULL; DirLRU = DirLRU?0:1; return cache[DirLRU?0:1]; }