1 : /******************************************************************************
2 : * $Id: gdalpamproxydb.cpp 18063 2009-11-21 21:11:49Z warmerdam $
3 : *
4 : * Project: GDAL Core
5 : * Purpose: Implementation of the GDAL PAM Proxy database interface.
6 : * The proxy db is used to associate .aux.xml files in a temp
7 : * directory - used for files for which aux.xml files can't be
8 : * created (ie. read-only file systems).
9 : * Author: Frank Warmerdam, warmerdam@pobox.com
10 : *
11 : ******************************************************************************
12 : * Copyright (c) 2005, Frank Warmerdam <warmerdam@pobox.com>
13 : *
14 : * Permission is hereby granted, free of charge, to any person obtaining a
15 : * copy of this software and associated documentation files (the "Software"),
16 : * to deal in the Software without restriction, including without limitation
17 : * the rights to use, copy, modify, merge, publish, distribute, sublicense,
18 : * and/or sell copies of the Software, and to permit persons to whom the
19 : * Software is furnished to do so, subject to the following conditions:
20 : *
21 : * The above copyright notice and this permission notice shall be included
22 : * in all copies or substantial portions of the Software.
23 : *
24 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
25 : * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
27 : * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
30 : * DEALINGS IN THE SOFTWARE.
31 : ****************************************************************************/
32 :
33 : #include "gdal_pam.h"
34 : #include "cpl_string.h"
35 : #include "ogr_spatialref.h"
36 : #include "cpl_multiproc.h"
37 :
38 : CPL_CVSID("$Id: gdalpamproxydb.cpp 18063 2009-11-21 21:11:49Z warmerdam $");
39 :
40 : /************************************************************************/
41 : /* ==================================================================== */
42 : /* GDALPamProxyDB */
43 : /* ==================================================================== */
44 : /************************************************************************/
45 :
46 : class GDALPamProxyDB
47 0 : {
48 : public:
49 0 : GDALPamProxyDB() { nUpdateCounter = -1; }
50 :
51 : CPLString osProxyDBDir;
52 :
53 : int nUpdateCounter;
54 :
55 : std::vector<CPLString> aosOriginalFiles;
56 : std::vector<CPLString> aosProxyFiles;
57 :
58 : void CheckLoadDB();
59 : void LoadDB();
60 : void SaveDB();
61 : };
62 :
63 : static int bProxyDBInitialized = FALSE;
64 : static GDALPamProxyDB *poProxyDB = NULL;
65 : static void *hProxyDBLock = NULL;
66 :
67 : /************************************************************************/
68 : /* CheckLoadDB() */
69 : /* */
70 : /* Eventually we want to check if the file has changed, and if */
71 : /* so, force it to be reloaded. TODO: */
72 : /************************************************************************/
73 :
74 0 : void GDALPamProxyDB::CheckLoadDB()
75 :
76 : {
77 0 : if( nUpdateCounter == -1 )
78 0 : LoadDB();
79 0 : }
80 :
81 : /************************************************************************/
82 : /* LoadDB() */
83 : /* */
84 : /* It is assumed the caller already holds the lock. */
85 : /************************************************************************/
86 :
87 0 : void GDALPamProxyDB::LoadDB()
88 :
89 : {
90 : /* -------------------------------------------------------------------- */
91 : /* Open the database relating original names to proxy .aux.xml */
92 : /* file names. */
93 : /* -------------------------------------------------------------------- */
94 : CPLString osDBName =
95 0 : CPLFormFilename( osProxyDBDir, "gdal_pam_proxy", "dat" );
96 0 : FILE *fpDB = VSIFOpenL( osDBName, "r" );
97 :
98 0 : nUpdateCounter = 0;
99 0 : if( fpDB == NULL )
100 : return;
101 :
102 : /* -------------------------------------------------------------------- */
103 : /* Read header, verify and extract update counter. */
104 : /* -------------------------------------------------------------------- */
105 : GByte abyHeader[100];
106 :
107 0 : if( VSIFReadL( abyHeader, 1, 100, fpDB ) != 100
108 : || strncmp( (const char *) abyHeader, "GDAL_PROXY", 10 ) != 0 )
109 : {
110 : CPLError( CE_Failure, CPLE_AppDefined,
111 : "Problem reading %s header - short or corrupt?",
112 0 : osDBName.c_str() );
113 : return;
114 : }
115 :
116 0 : nUpdateCounter = atoi((const char *) abyHeader + 10);
117 :
118 : /* -------------------------------------------------------------------- */
119 : /* Read the file in one gulp. */
120 : /* -------------------------------------------------------------------- */
121 : int nBufLength;
122 : char *pszDBData;
123 :
124 0 : VSIFSeekL( fpDB, 0, SEEK_END );
125 0 : nBufLength = (int) (VSIFTellL(fpDB) - 100);
126 :
127 0 : pszDBData = (char *) CPLCalloc(1,nBufLength+1);
128 0 : VSIFSeekL( fpDB, 100, SEEK_SET );
129 0 : VSIFReadL( pszDBData, 1, nBufLength, fpDB );
130 :
131 0 : VSIFCloseL( fpDB );
132 :
133 : /* -------------------------------------------------------------------- */
134 : /* Parse the list of in/out names. */
135 : /* -------------------------------------------------------------------- */
136 0 : int iNext = 0;
137 :
138 0 : while( iNext < nBufLength )
139 : {
140 0 : CPLString osOriginal, osProxy;
141 :
142 0 : osOriginal.assign( pszDBData + iNext );
143 :
144 0 : for( ; iNext < nBufLength && pszDBData[iNext] != '\0'; iNext++ ) {}
145 :
146 0 : if( iNext == nBufLength )
147 : break;
148 :
149 0 : iNext++;
150 :
151 0 : osProxy = osProxyDBDir;
152 0 : osProxy += "/";
153 0 : osProxy += pszDBData + iNext;
154 :
155 0 : for( ; iNext < nBufLength && pszDBData[iNext] != '\0'; iNext++ ) {}
156 0 : iNext++;
157 :
158 0 : aosOriginalFiles.push_back( osOriginal );
159 0 : aosProxyFiles.push_back( osProxy );
160 : }
161 :
162 0 : CPLFree( pszDBData );
163 : }
164 :
165 : /************************************************************************/
166 : /* SaveDB() */
167 : /************************************************************************/
168 :
169 0 : void GDALPamProxyDB::SaveDB()
170 :
171 : {
172 : /* -------------------------------------------------------------------- */
173 : /* Open the database relating original names to proxy .aux.xml */
174 : /* file names. */
175 : /* -------------------------------------------------------------------- */
176 : CPLString osDBName =
177 0 : CPLFormFilename( osProxyDBDir, "gdal_pam_proxy", "dat" );
178 :
179 0 : void *hLock = CPLLockFile( osDBName, 1.0 );
180 :
181 : // proceed even if lock fails - we need CPLBreakLockFile()!
182 0 : if( hLock == NULL )
183 : {
184 : CPLError( CE_Warning, CPLE_AppDefined,
185 : "GDALPamProxyDB::SaveDB() - Failed to lock %s file, proceeding anyways.",
186 0 : osDBName.c_str() );
187 : }
188 :
189 0 : FILE *fpDB = VSIFOpenL( osDBName, "w" );
190 0 : if( fpDB == NULL )
191 : {
192 0 : if( hLock )
193 0 : CPLUnlockFile( hLock );
194 : CPLError( CE_Failure, CPLE_AppDefined,
195 : "Failed to save %s Pam Proxy DB.\n%s",
196 : osDBName.c_str(),
197 0 : VSIStrerror( errno ) );
198 : return;
199 : }
200 :
201 : /* -------------------------------------------------------------------- */
202 : /* Write header. */
203 : /* -------------------------------------------------------------------- */
204 : GByte abyHeader[100];
205 :
206 0 : memset( abyHeader, ' ', sizeof(abyHeader) );
207 0 : strncpy( (char *) abyHeader, "GDAL_PROXY", 10 );
208 0 : sprintf( (char *) abyHeader + 10, "%9d", nUpdateCounter );
209 :
210 0 : VSIFWriteL( abyHeader, 1, 100, fpDB );
211 :
212 : /* -------------------------------------------------------------------- */
213 : /* Write names. */
214 : /* -------------------------------------------------------------------- */
215 : unsigned int i;
216 :
217 0 : for( i = 0; i < aosOriginalFiles.size(); i++ )
218 : {
219 : size_t nBytesWritten;
220 : const char *pszProxyFile;
221 :
222 : VSIFWriteL( aosOriginalFiles[i].c_str(), 1,
223 0 : strlen(aosOriginalFiles[i].c_str())+1, fpDB );
224 :
225 0 : pszProxyFile = CPLGetFilename(aosProxyFiles[i]);
226 : nBytesWritten = VSIFWriteL( pszProxyFile, 1,
227 0 : strlen(pszProxyFile)+1, fpDB );
228 :
229 0 : if( nBytesWritten != strlen(pszProxyFile)+1 )
230 : {
231 : CPLError( CE_Failure, CPLE_AppDefined,
232 : "Failed to write complete %s Pam Proxy DB.\n%s",
233 : osDBName.c_str(),
234 0 : VSIStrerror( errno ) );
235 0 : VSIFCloseL( fpDB );
236 0 : VSIUnlink( osDBName );
237 : return;
238 : }
239 : }
240 :
241 0 : VSIFCloseL( fpDB );
242 :
243 0 : if( hLock )
244 0 : CPLUnlockFile( hLock );
245 : }
246 :
247 :
248 : /************************************************************************/
249 : /* InitProxyDB() */
250 : /* */
251 : /* Initialize ProxyDB (if it isn't already initialized). */
252 : /************************************************************************/
253 :
254 4669 : static void InitProxyDB()
255 :
256 : {
257 4669 : if( !bProxyDBInitialized )
258 : {
259 317 : CPLMutexHolderD( &hProxyDBLock );
260 :
261 317 : if( !bProxyDBInitialized )
262 : {
263 : const char *pszProxyDir =
264 317 : CPLGetConfigOption( "GDAL_PAM_PROXY_DIR", NULL );
265 :
266 317 : if( pszProxyDir )
267 : {
268 0 : poProxyDB = new GDALPamProxyDB();
269 0 : poProxyDB->osProxyDBDir = pszProxyDir;
270 : }
271 : }
272 :
273 317 : bProxyDBInitialized = TRUE;
274 : }
275 4669 : }
276 :
277 : /************************************************************************/
278 : /* PamCleanProxyDB() */
279 : /************************************************************************/
280 :
281 325 : void PamCleanProxyDB()
282 :
283 : {
284 325 : CPLMutexHolderD( &hProxyDBLock );
285 :
286 325 : bProxyDBInitialized = FALSE;
287 :
288 325 : delete poProxyDB;
289 325 : poProxyDB = NULL;
290 325 : }
291 :
292 : /************************************************************************/
293 : /* PamGetProxy() */
294 : /************************************************************************/
295 :
296 4667 : const char *PamGetProxy( const char *pszOriginal )
297 :
298 : {
299 4667 : InitProxyDB();
300 :
301 4667 : if( poProxyDB == NULL )
302 4667 : return NULL;
303 :
304 0 : CPLMutexHolderD( &hProxyDBLock );
305 : unsigned int i;
306 :
307 0 : poProxyDB->CheckLoadDB();
308 :
309 0 : for( i = 0; i < poProxyDB->aosOriginalFiles.size(); i++ )
310 : {
311 0 : if( strcmp( poProxyDB->aosOriginalFiles[i], pszOriginal ) == 0 )
312 0 : return poProxyDB->aosProxyFiles[i];
313 : }
314 :
315 0 : return NULL;
316 : }
317 :
318 : /************************************************************************/
319 : /* PamAllocateProxy() */
320 : /************************************************************************/
321 :
322 2 : const char *PamAllocateProxy( const char *pszOriginal )
323 :
324 : {
325 2 : InitProxyDB();
326 :
327 2 : if( poProxyDB == NULL )
328 2 : return NULL;
329 :
330 0 : CPLMutexHolderD( &hProxyDBLock );
331 :
332 0 : poProxyDB->CheckLoadDB();
333 :
334 : /* -------------------------------------------------------------------- */
335 : /* Form the proxy filename based on the original path if */
336 : /* possible, but dummy out any questionable characters, path */
337 : /* delimiters and such. This is intended to make the proxy */
338 : /* name be identifiable by folks digging around in the proxy */
339 : /* database directory. */
340 : /* */
341 : /* We also need to be careful about length. */
342 : /* -------------------------------------------------------------------- */
343 0 : CPLString osRevProxyFile;
344 : int i;
345 :
346 0 : i = strlen(pszOriginal) - 1;
347 0 : while( i >= 0 && osRevProxyFile.size() < 220 )
348 : {
349 0 : if( i > 6 && EQUALN(pszOriginal+i-5,":::OVR",6) )
350 0 : i -= 6;
351 :
352 : // make some effort to break long names at path delimiters.
353 0 : if( (pszOriginal[i] == '/' || pszOriginal[i] == '\\')
354 : && osRevProxyFile.size() > 200 )
355 0 : break;
356 :
357 0 : if( (pszOriginal[i] >= 'A' && pszOriginal[i] <= 'Z')
358 0 : || (pszOriginal[i] >= 'a' && pszOriginal[i] <= 'z')
359 0 : || (pszOriginal[i] >= '0' && pszOriginal[i] <= '9')
360 0 : || pszOriginal[i] == '.' )
361 0 : osRevProxyFile += pszOriginal[i];
362 : else
363 0 : osRevProxyFile += '_';
364 :
365 0 : i--;
366 : }
367 :
368 0 : CPLString osOriginal = pszOriginal;
369 0 : CPLString osProxy;
370 0 : CPLString osCounter;
371 :
372 0 : osProxy = poProxyDB->osProxyDBDir + "/";
373 :
374 0 : osCounter.Printf( "%06d_", poProxyDB->nUpdateCounter++ );
375 0 : osProxy += osCounter;
376 :
377 0 : for( i = osRevProxyFile.size()-1; i >= 0; i-- )
378 0 : osProxy += osRevProxyFile[i];
379 :
380 0 : if( osOriginal.find(":::OVR") != CPLString::npos )
381 0 : osProxy += ".ovr";
382 : else
383 0 : osProxy += ".aux.xml";
384 :
385 : /* -------------------------------------------------------------------- */
386 : /* Add the proxy and the original to the proxy list and resave */
387 : /* the database. */
388 : /* -------------------------------------------------------------------- */
389 0 : poProxyDB->aosOriginalFiles.push_back( osOriginal );
390 0 : poProxyDB->aosProxyFiles.push_back( osProxy );
391 :
392 0 : poProxyDB->SaveDB();
393 :
394 0 : return PamGetProxy( pszOriginal );
395 : }
|