static int _hasValidCache(const char *path, struct stat *stbuf) { int status; char cachePath[MAX_NAME_LEN]; struct stat stbufCache; if (path == NULL || stbuf == NULL) { rodsLog (LOG_ERROR, "_hasValidCache: input path or stbuf is NULL"); return (SYS_INTERNAL_NULL_INPUT_ERR); } if ((status = _getCachePath(path, cachePath)) < 0) { return status; } if ((status = stat(cachePath, &stbufCache)) < 0) { return status; } // compare stbufs if (stbufCache.st_size != stbuf->st_size) { return -1; // invalid size } if (stbufCache.st_mtime != stbuf->st_mtime) { return -1; // invalid mod time } return (0); }
static int _invalidateCache(const char *path) { int status; char cachePath[MAX_NAME_LEN]; char cacheWorkPath[MAX_NAME_LEN]; if (path == NULL) { rodsLog (LOG_ERROR, "_invalidateCache: input path is NULL"); return (SYS_INTERNAL_NULL_INPUT_ERR); } if ((status = _getCacheWorkPath(path, cacheWorkPath)) < 0) { return status; } if ((status = _getCachePath(path, cachePath)) < 0) { return status; } if (isDirectory(cachePath) == 0) { // directory status = removeDirRecursive(cachePath); } else { // file // remove incomplete preload cache if exists unlink(cacheWorkPath); status = unlink(cachePath); } return status; }
int moveToPreloadedDir (const char *path, const char *iRODSPath) { int status; char preloadCachePath[MAX_NAME_LEN]; if (path == NULL || iRODSPath == NULL) { rodsLog (LOG_ERROR, "moveToPreloadedDir: input path or iRODSPath is NULL"); return (SYS_INTERNAL_NULL_INPUT_ERR); } status = _getCachePath(iRODSPath, preloadCachePath); if(status < 0) { rodsLogError(LOG_ERROR, status, "moveToPreloadedDir: _getCachePath error."); rodsLog (LOG_ERROR, "moveToPreloadedDir: failed to get cache path - %s", path); return status; } // make dir prepareDir(preloadCachePath); // move the file status = rename(path, preloadCachePath); if(status < 0) { rodsLogError(LOG_ERROR, status, "moveToPreloadedDir: rename error."); return status; } return (0); }
static int _renameCache(const char *fromPath, const char *toPath) { int status; char fromCachePath[MAX_NAME_LEN]; char toCachePath[MAX_NAME_LEN]; rodsLog (LOG_DEBUG, "_renameCache: %s -> %s", fromPath, toPath); if ((status = _getCachePath(fromPath, fromCachePath)) < 0) { return status; } if ((status = _getCachePath(toPath, toCachePath)) < 0) { return status; } rodsLog (LOG_DEBUG, "_renameCache (local): %s -> %s", fromCachePath, toCachePath); status = rename(fromCachePath, toCachePath); return status; }
static int _truncateCache(const char *path, off_t size) { int status; char cachePath[MAX_NAME_LEN]; rodsLog (LOG_DEBUG, "_truncateCache: %s, %ld", path, size); if ((status = _getCachePath(path, cachePath)) < 0) { return status; } status = truncate(cachePath, size); return status; }
int moveToPreloadedDir (const char *path, const char *iRODSPath) { int status; char preloadCachePath[MAX_NAME_LEN]; off_t cacheSize; if (path == NULL || iRODSPath == NULL) { rodsLog (LOG_DEBUG, "moveToPreloadedDir: input path or iRODSPath is NULL"); return (SYS_INTERNAL_NULL_INPUT_ERR); } status = _getCachePath(iRODSPath, preloadCachePath); if(status < 0) { rodsLog (LOG_DEBUG, "moveToPreloadedDir: failed to get cache path - %s", path); return status; } // make dir makeParentDirs(preloadCachePath); // move the file status = rename(path, preloadCachePath); if(status < 0) { rodsLog (LOG_DEBUG, "moveToPreloadedDir: rename error : %d", status); return status; } // check whether preload cache exceeds limit if(PreloadConfig.cacheMaxSize > 0) { cacheSize = getFileSizeRecursive(PreloadConfig.cachePath); if(cacheSize > (off_t)PreloadConfig.cacheMaxSize) { // evict? status = _evictOldCache(cacheSize - (off_t)PreloadConfig.cacheMaxSize); if(status < 0) { rodsLog (LOG_DEBUG, "moveToPreloadedDir: failed to evict old cache"); return status; } } } return (0); }
static int _hasCache(const char *path) { int status; char cachePath[MAX_NAME_LEN]; struct stat stbufCache; if (path == NULL) { rodsLog (LOG_DEBUG, "_hasCache: input inPath is NULL"); return (SYS_INTERNAL_NULL_INPUT_ERR); } if ((status = _getCachePath(path, cachePath)) < 0) { rodsLog (LOG_DEBUG, "_hasCache: _getCachePath error : %d", status); return status; } if ((status = stat(cachePath, &stbufCache)) < 0) { //rodsLog (LOG_DEBUG, "_hasCache: stat error for %s", cachePath); return status; } return (0); }
static int _download(const char *path, struct stat *stbufIn) { int status; rcComm_t *conn; rodsPathInp_t rodsPathInp; rErrMsg_t errMsg; char preloadCachePath[MAX_NAME_LEN]; char preloadCacheWorkPath[MAX_NAME_LEN]; // set path for getUtil status = _getCachePath(path, preloadCachePath); if(status < 0) { rodsLogError(LOG_ERROR, status, "_download: _getCachePath error."); rodsLog (LOG_ERROR, "_download: failed to get cache path - %s", path); return status; } status = _getCacheWorkPath(path, preloadCacheWorkPath); if(status < 0) { rodsLogError(LOG_ERROR, status, "_download: _getCacheWorkPath error."); rodsLog (LOG_ERROR, "_download: failed to get cache work path - %s", path); return status; } rodsLog (LOG_DEBUG, "_download: download %s to %s", path, preloadCachePath); // set src path memset( &rodsPathInp, 0, sizeof( rodsPathInp_t ) ); addSrcInPath( &rodsPathInp, (char*)path ); status = parseRodsPath (&rodsPathInp.srcPath[0], PreloadRodsEnv); if(status < 0) { rodsLogError(LOG_ERROR, status, "_download: parseRodsPath error."); return status; } // set dest path rodsPathInp.destPath = ( rodsPath_t* )malloc( sizeof( rodsPath_t ) ); memset( rodsPathInp.destPath, 0, sizeof( rodsPath_t ) ); rstrcpy( rodsPathInp.destPath->inPath, preloadCacheWorkPath, MAX_NAME_LEN ); status = parseLocalPath (rodsPathInp.destPath); if(status < 0) { rodsLogError(LOG_ERROR, status, "_download: parseLocalPath error."); return status; } // Connect conn = rcConnect (PreloadRodsEnv->rodsHost, PreloadRodsEnv->rodsPort, PreloadRodsEnv->rodsUserName, PreloadRodsEnv->rodsZone, RECONN_TIMEOUT, &errMsg); if (conn == NULL) { rodsLog (LOG_ERROR, "_download: error occurred while connecting to irods"); return -EPIPE; } // Login if (strcmp (PreloadRodsEnv->rodsUserName, PUBLIC_USER_NAME) != 0) { status = clientLogin(conn); if (status != 0) { rodsLogError(LOG_ERROR, status, "_download: ClientLogin error."); rcDisconnect(conn); return status; } } // make dir prepareDir(preloadCachePath); // Preload rodsLog (LOG_DEBUG, "_download: download %s", path); status = getUtil (&conn, PreloadRodsEnv, PreloadRodsArgs, &rodsPathInp); rodsLog (LOG_DEBUG, "_download: complete downloading %s", path); // Disconnect rcDisconnect(conn); if(status < 0) { rodsLogError(LOG_ERROR, status, "_download: getUtil error."); return status; } // be careful when using Lock LOCK(PreloadLock); status = _completeDownload(preloadCacheWorkPath, preloadCachePath, stbufIn); if(status < 0) { rodsLogError(LOG_ERROR, status, "_download: _completeDownload error."); UNLOCK(PreloadLock); return status; } UNLOCK(PreloadLock); return 0; }
int openPreloadedFile (const char *path) { int status; char iRODSPath[MAX_NAME_LEN]; char preloadCachePath[MAX_NAME_LEN]; preloadFileHandleInfo_t *preloadFileHandleInfo = NULL; int desc; status = _getiRODSPath(path, iRODSPath); if(status < 0) { rodsLogError(LOG_ERROR, status, "openPreloadedFile: _getiRODSPath error."); rodsLog (LOG_ERROR, "openPreloadedFile: failed to get iRODS path - %s", path); return status; } status = _getCachePath(iRODSPath, preloadCachePath); if(status < 0) { rodsLogError(LOG_ERROR, status, "openPreloadedFile: _getCachePath error."); rodsLog (LOG_ERROR, "openPreloadedFile: failed to get cache path - %s", path); return status; } LOCK(PreloadLock); desc = -1; preloadFileHandleInfo = (preloadFileHandleInfo_t *)lookupFromHashTable(PreloadFileHandleTable, iRODSPath); if(preloadFileHandleInfo != NULL) { // has preload file handle opened if(preloadFileHandleInfo->handle > 0) { // reuse handle desc = preloadFileHandleInfo->handle; rodsLog (LOG_DEBUG, "openPreloadedFile: file is already opened - %s", iRODSPath); } else { // reuse handleinfo if(_hasCache(iRODSPath) >= 0) { desc = open (preloadCachePath, O_RDONLY); if(desc > 0) { preloadFileHandleInfo->handle = desc; rodsLog (LOG_DEBUG, "openPreloadedFile: opens a file handle - %s", iRODSPath); } } } } else { // if preloaded cache file is not opened // open new if(_hasCache(iRODSPath) >= 0) { desc = open (preloadCachePath, O_RDONLY); rodsLog (LOG_DEBUG, "openPreloadedFile: open a preloaded cache path - %s", iRODSPath); if(desc > 0) { preloadFileHandleInfo = (preloadFileHandleInfo_t *)malloc(sizeof(preloadFileHandleInfo_t)); preloadFileHandleInfo->path = strdup(iRODSPath); preloadFileHandleInfo->handle = desc; INIT_STRUCT_LOCK((*preloadFileHandleInfo)); insertIntoHashTable(PreloadFileHandleTable, iRODSPath, preloadFileHandleInfo); } } } UNLOCK(PreloadLock); return desc; }