1 : /******************************************************************************
2 : * $Id: gdalpamproxydb.cpp 22812 2011-07-25 04:50:23Z 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 22812 2011-07-25 04:50:23Z warmerdam $");
39 :
40 : /************************************************************************/
41 : /* ==================================================================== */
42 : /* GDALPamProxyDB */
43 : /* ==================================================================== */
44 : /************************************************************************/
45 :
46 : class GDALPamProxyDB
47 0 : {
48 : public:
49 2 : 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 14 : void GDALPamProxyDB::CheckLoadDB()
75 :
76 : {
77 14 : if( nUpdateCounter == -1 )
78 2 : LoadDB();
79 14 : }
80 :
81 : /************************************************************************/
82 : /* LoadDB() */
83 : /* */
84 : /* It is assumed the caller already holds the lock. */
85 : /************************************************************************/
86 :
87 2 : 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 2 : CPLFormFilename( osProxyDBDir, "gdal_pam_proxy", "dat" );
96 2 : VSILFILE *fpDB = VSIFOpenL( osDBName, "r" );
97 :
98 2 : nUpdateCounter = 0;
99 2 : if( fpDB == NULL )
100 : return;
101 :
102 : /* -------------------------------------------------------------------- */
103 : /* Read header, verify and extract update counter. */
104 : /* -------------------------------------------------------------------- */
105 : GByte abyHeader[100];
106 :
107 1 : 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 1 : nUpdateCounter = atoi((const char *) abyHeader + 10);
117 :
118 : /* -------------------------------------------------------------------- */
119 : /* Read the file in one gulp. */
120 : /* -------------------------------------------------------------------- */
121 : int nBufLength;
122 : char *pszDBData;
123 :
124 1 : VSIFSeekL( fpDB, 0, SEEK_END );
125 1 : nBufLength = (int) (VSIFTellL(fpDB) - 100);
126 :
127 1 : pszDBData = (char *) CPLCalloc(1,nBufLength+1);
128 1 : VSIFSeekL( fpDB, 100, SEEK_SET );
129 1 : VSIFReadL( pszDBData, 1, nBufLength, fpDB );
130 :
131 1 : VSIFCloseL( fpDB );
132 :
133 : /* -------------------------------------------------------------------- */
134 : /* Parse the list of in/out names. */
135 : /* -------------------------------------------------------------------- */
136 1 : int iNext = 0;
137 :
138 1 : while( iNext < nBufLength )
139 : {
140 2 : CPLString osOriginal, osProxy;
141 :
142 2 : osOriginal.assign( pszDBData + iNext );
143 :
144 2 : for( ; iNext < nBufLength && pszDBData[iNext] != '\0'; iNext++ ) {}
145 :
146 2 : if( iNext == nBufLength )
147 : break;
148 :
149 2 : iNext++;
150 :
151 2 : osProxy = osProxyDBDir;
152 2 : osProxy += "/";
153 2 : osProxy += pszDBData + iNext;
154 :
155 2 : for( ; iNext < nBufLength && pszDBData[iNext] != '\0'; iNext++ ) {}
156 2 : iNext++;
157 :
158 2 : aosOriginalFiles.push_back( osOriginal );
159 2 : aosProxyFiles.push_back( osProxy );
160 : }
161 :
162 1 : CPLFree( pszDBData );
163 : }
164 :
165 : /************************************************************************/
166 : /* SaveDB() */
167 : /************************************************************************/
168 :
169 2 : 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 2 : CPLFormFilename( osProxyDBDir, "gdal_pam_proxy", "dat" );
178 :
179 2 : void *hLock = CPLLockFile( osDBName, 1.0 );
180 :
181 : // proceed even if lock fails - we need CPLBreakLockFile()!
182 2 : 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 2 : VSILFILE *fpDB = VSIFOpenL( osDBName, "w" );
190 2 : 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 2 : memset( abyHeader, ' ', sizeof(abyHeader) );
207 2 : strncpy( (char *) abyHeader, "GDAL_PROXY", 10 );
208 2 : sprintf( (char *) abyHeader + 10, "%9d", nUpdateCounter );
209 :
210 2 : VSIFWriteL( abyHeader, 1, 100, fpDB );
211 :
212 : /* -------------------------------------------------------------------- */
213 : /* Write names. */
214 : /* -------------------------------------------------------------------- */
215 : unsigned int i;
216 :
217 5 : 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 3 : strlen(aosOriginalFiles[i].c_str())+1, fpDB );
224 :
225 3 : pszProxyFile = CPLGetFilename(aosProxyFiles[i]);
226 : nBytesWritten = VSIFWriteL( pszProxyFile, 1,
227 3 : strlen(pszProxyFile)+1, fpDB );
228 :
229 3 : 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 2 : VSIFCloseL( fpDB );
242 :
243 2 : if( hLock )
244 2 : CPLUnlockFile( hLock );
245 : }
246 :
247 :
248 : /************************************************************************/
249 : /* InitProxyDB() */
250 : /* */
251 : /* Initialize ProxyDB (if it isn't already initialized). */
252 : /************************************************************************/
253 :
254 7677 : static void InitProxyDB()
255 :
256 : {
257 7677 : if( !bProxyDBInitialized )
258 : {
259 429 : CPLMutexHolderD( &hProxyDBLock );
260 :
261 429 : if( !bProxyDBInitialized )
262 : {
263 : const char *pszProxyDir =
264 429 : CPLGetConfigOption( "GDAL_PAM_PROXY_DIR", NULL );
265 :
266 429 : if( pszProxyDir )
267 : {
268 2 : poProxyDB = new GDALPamProxyDB();
269 4 : poProxyDB->osProxyDBDir = pszProxyDir;
270 : }
271 : }
272 :
273 429 : bProxyDBInitialized = TRUE;
274 : }
275 7677 : }
276 :
277 : /************************************************************************/
278 : /* PamCleanProxyDB() */
279 : /************************************************************************/
280 :
281 542 : void PamCleanProxyDB()
282 :
283 : {
284 : {
285 542 : CPLMutexHolderD( &hProxyDBLock );
286 :
287 542 : bProxyDBInitialized = FALSE;
288 :
289 542 : delete poProxyDB;
290 542 : poProxyDB = NULL;
291 : }
292 :
293 542 : CPLDestroyMutex( hProxyDBLock );
294 542 : hProxyDBLock = NULL;
295 542 : }
296 :
297 : /************************************************************************/
298 : /* PamGetProxy() */
299 : /************************************************************************/
300 :
301 7671 : const char *PamGetProxy( const char *pszOriginal )
302 :
303 : {
304 7671 : InitProxyDB();
305 :
306 7671 : if( poProxyDB == NULL )
307 7659 : return NULL;
308 :
309 12 : CPLMutexHolderD( &hProxyDBLock );
310 : unsigned int i;
311 :
312 12 : poProxyDB->CheckLoadDB();
313 :
314 21 : for( i = 0; i < poProxyDB->aosOriginalFiles.size(); i++ )
315 : {
316 15 : if( strcmp( poProxyDB->aosOriginalFiles[i], pszOriginal ) == 0 )
317 6 : return poProxyDB->aosProxyFiles[i];
318 : }
319 :
320 6 : return NULL;
321 : }
322 :
323 : /************************************************************************/
324 : /* PamAllocateProxy() */
325 : /************************************************************************/
326 :
327 6 : const char *PamAllocateProxy( const char *pszOriginal )
328 :
329 : {
330 6 : InitProxyDB();
331 :
332 6 : if( poProxyDB == NULL )
333 4 : return NULL;
334 :
335 2 : CPLMutexHolderD( &hProxyDBLock );
336 :
337 2 : poProxyDB->CheckLoadDB();
338 :
339 : /* -------------------------------------------------------------------- */
340 : /* Form the proxy filename based on the original path if */
341 : /* possible, but dummy out any questionable characters, path */
342 : /* delimiters and such. This is intended to make the proxy */
343 : /* name be identifiable by folks digging around in the proxy */
344 : /* database directory. */
345 : /* */
346 : /* We also need to be careful about length. */
347 : /* -------------------------------------------------------------------- */
348 2 : CPLString osRevProxyFile;
349 : int i;
350 :
351 2 : i = strlen(pszOriginal) - 1;
352 50 : while( i >= 0 && osRevProxyFile.size() < 220 )
353 : {
354 46 : if( i > 6 && EQUALN(pszOriginal+i-5,":::OVR",6) )
355 1 : i -= 6;
356 :
357 : // make some effort to break long names at path delimiters.
358 46 : if( (pszOriginal[i] == '/' || pszOriginal[i] == '\\')
359 : && osRevProxyFile.size() > 200 )
360 0 : break;
361 :
362 186 : if( (pszOriginal[i] >= 'A' && pszOriginal[i] <= 'Z')
363 88 : || (pszOriginal[i] >= 'a' && pszOriginal[i] <= 'z')
364 4 : || (pszOriginal[i] >= '0' && pszOriginal[i] <= '9')
365 4 : || pszOriginal[i] == '.' )
366 44 : osRevProxyFile += pszOriginal[i];
367 : else
368 2 : osRevProxyFile += '_';
369 :
370 46 : i--;
371 : }
372 :
373 2 : CPLString osOriginal = pszOriginal;
374 2 : CPLString osProxy;
375 2 : CPLString osCounter;
376 :
377 2 : osProxy = poProxyDB->osProxyDBDir + "/";
378 :
379 2 : osCounter.Printf( "%06d_", poProxyDB->nUpdateCounter++ );
380 2 : osProxy += osCounter;
381 :
382 48 : for( i = osRevProxyFile.size()-1; i >= 0; i-- )
383 46 : osProxy += osRevProxyFile[i];
384 :
385 2 : if( osOriginal.find(":::OVR") != CPLString::npos )
386 1 : osProxy += ".ovr";
387 : else
388 1 : osProxy += ".aux.xml";
389 :
390 : /* -------------------------------------------------------------------- */
391 : /* Add the proxy and the original to the proxy list and resave */
392 : /* the database. */
393 : /* -------------------------------------------------------------------- */
394 2 : poProxyDB->aosOriginalFiles.push_back( osOriginal );
395 2 : poProxyDB->aosProxyFiles.push_back( osProxy );
396 :
397 2 : poProxyDB->SaveDB();
398 :
399 2 : return PamGetProxy( pszOriginal );
400 : }
|