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