int OGRFMECacheIndex::Lock() { if( pszPath == NULL ) return FALSE; hLock = CPLLockFile( pszPath, 5.0 ); return hLock != NULL; }
void GDALPamProxyDB::SaveDB() { /* -------------------------------------------------------------------- */ /* Open the database relating original names to proxy .aux.xml */ /* file names. */ /* -------------------------------------------------------------------- */ CPLString osDBName = CPLFormFilename( osProxyDBDir, "gdal_pam_proxy", "dat" ); void *hLock = CPLLockFile( osDBName, 1.0 ); // proceed even if lock fails - we need CPLBreakLockFile()! if( hLock == NULL ) { CPLError( CE_Warning, CPLE_AppDefined, "GDALPamProxyDB::SaveDB() - Failed to lock %s file, proceeding anyways.", osDBName.c_str() ); } FILE *fpDB = VSIFOpenL( osDBName, "w" ); if( fpDB == NULL ) { if( hLock ) CPLUnlockFile( hLock ); CPLError( CE_Failure, CPLE_AppDefined, "Failed to save %s Pam Proxy DB.\n%s", osDBName.c_str(), VSIStrerror( errno ) ); return; } /* -------------------------------------------------------------------- */ /* Write header. */ /* -------------------------------------------------------------------- */ GByte abyHeader[100]; memset( abyHeader, ' ', sizeof(abyHeader) ); strncpy( (char *) abyHeader, "GDAL_PROXY", 10 ); sprintf( (char *) abyHeader + 10, "%9d", nUpdateCounter ); VSIFWriteL( abyHeader, 1, 100, fpDB ); /* -------------------------------------------------------------------- */ /* Write names. */ /* -------------------------------------------------------------------- */ unsigned int i; for( i = 0; i < aosOriginalFiles.size(); i++ ) { size_t nBytesWritten; const char *pszProxyFile; VSIFWriteL( aosOriginalFiles[i].c_str(), 1, strlen(aosOriginalFiles[i].c_str())+1, fpDB ); pszProxyFile = CPLGetFilename(aosProxyFiles[i]); nBytesWritten = VSIFWriteL( pszProxyFile, 1, strlen(pszProxyFile)+1, fpDB ); if( nBytesWritten != strlen(pszProxyFile)+1 ) { CPLError( CE_Failure, CPLE_AppDefined, "Failed to write complete %s Pam Proxy DB.\n%s", osDBName.c_str(), VSIStrerror( errno ) ); VSIFCloseL( fpDB ); VSIUnlink( osDBName ); return; } } VSIFCloseL( fpDB ); if( hLock ) CPLUnlockFile( hLock ); }
void GDALPamProxyDB::SaveDB() { /* -------------------------------------------------------------------- */ /* Open the database relating original names to proxy .aux.xml */ /* file names. */ /* -------------------------------------------------------------------- */ CPLString osDBName = CPLFormFilename( osProxyDBDir, "gdal_pam_proxy", "dat" ); void *hLock = CPLLockFile( osDBName, 1.0 ); // proceed even if lock fails - we need CPLBreakLockFile()! if( hLock == NULL ) { CPLError( CE_Warning, CPLE_AppDefined, "GDALPamProxyDB::SaveDB() - " "Failed to lock %s file, proceeding anyways.", osDBName.c_str() ); } VSILFILE *fpDB = VSIFOpenL( osDBName, "w" ); if( fpDB == NULL ) { if( hLock ) CPLUnlockFile( hLock ); CPLError( CE_Failure, CPLE_AppDefined, "Failed to save %s Pam Proxy DB.\n%s", osDBName.c_str(), VSIStrerror( errno ) ); return; } /* -------------------------------------------------------------------- */ /* Write header. */ /* -------------------------------------------------------------------- */ const size_t nHeaderSize = 100; GByte abyHeader[nHeaderSize] = { '\0' }; memset( abyHeader, ' ', sizeof(abyHeader) ); memcpy( reinterpret_cast<char *>(abyHeader), "GDAL_PROXY", 10 ); snprintf( reinterpret_cast<char *>(abyHeader) + 10, sizeof(abyHeader) - 10, "%9d", nUpdateCounter ); if( VSIFWriteL( abyHeader, 1, nHeaderSize, fpDB ) != nHeaderSize ) { CPLError( CE_Failure, CPLE_AppDefined, "Failed to write complete %s Pam Proxy DB.\n%s", osDBName.c_str(), VSIStrerror( errno ) ); CPL_IGNORE_RET_VAL(VSIFCloseL( fpDB )); VSIUnlink( osDBName ); if( hLock ) CPLUnlockFile( hLock ); return; } /* -------------------------------------------------------------------- */ /* Write names. */ /* -------------------------------------------------------------------- */ for( unsigned int i = 0; i < aosOriginalFiles.size(); i++ ) { size_t nCount = VSIFWriteL( aosOriginalFiles[i].c_str(), strlen(aosOriginalFiles[i].c_str())+1, 1, fpDB ); const char *pszProxyFile = CPLGetFilename(aosProxyFiles[i]); nCount += VSIFWriteL( pszProxyFile, strlen(pszProxyFile)+1, 1, fpDB ); if( nCount != 2 ) { CPLError( CE_Failure, CPLE_AppDefined, "Failed to write complete %s Pam Proxy DB.\n%s", osDBName.c_str(), VSIStrerror( errno ) ); CPL_IGNORE_RET_VAL(VSIFCloseL( fpDB )); VSIUnlink( osDBName ); if( hLock ) CPLUnlockFile( hLock ); return; } } if( VSIFCloseL( fpDB ) != 0 ) { CPLError(CE_Failure, CPLE_FileIO, "I/O error"); } if( hLock ) CPLUnlockFile( hLock ); }