Skip to content

Commit

Permalink
Add GTIFF_FORK_SAFE option
Browse files Browse the repository at this point in the history
  • Loading branch information
talaj committed Dec 14, 2017
1 parent b140a48 commit 9282374
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
14 changes: 14 additions & 0 deletions gdal/frmts/gtiff/geotiff.cpp
Expand Up @@ -12420,6 +12420,20 @@ GDALDataset *GTiffDataset::Open( GDALOpenInfo * poOpenInfo )
poDS->LoadGeoreferencingAndPamIfNeeded();
}

if ( CPLTestBool(CPLGetConfigOption("GTIFF_FORK_SAFE", "NO")) )
{
if ( VSI_TIFFCloseFile(l_hTIFF) != 0 )
{
CPLError(
CE_Warning, CPLE_AppDefined,
"Cannot close the TIFF file. The close operation is "
"requested by GTIFF_FORK_SAFE option." );
delete poDS;
return NULL;
}
poDS->fpL = NULL;
}

return poDS;
}

Expand Down
38 changes: 38 additions & 0 deletions gdal/frmts/gtiff/tifvsi.cpp
Expand Up @@ -81,13 +81,24 @@ typedef struct
void** ppCachedData;
vsi_l_offset* panCachedOffsets;
size_t* panCachedSizes;

char* filename;
char* mode;
bool closeFile;
} GDALTiffHandle;

static tsize_t
_tiffReadProc( thandle_t th, tdata_t buf, tsize_t size )
{
GDALTiffHandle* psGTH = reinterpret_cast<GDALTiffHandle *>(th);

// Reopen
if( psGTH->fpL == NULL )
{
psGTH->fpL = VSIFOpenL( psGTH->filename, psGTH->mode );
psGTH->closeFile = true;
}

if( psGTH->nCachedRanges )
{
const vsi_l_offset nCurOffset = VSIFTellL( psGTH->fpL );
Expand Down Expand Up @@ -185,6 +196,13 @@ _tiffSeekProc( thandle_t th, toff_t off, int whence )
{
GDALTiffHandle* psGTH = reinterpret_cast<GDALTiffHandle *>( th );

// Reopen
if( psGTH->fpL == NULL )
{
psGTH->fpL = VSIFOpenL( psGTH->filename, psGTH->mode );
psGTH->closeFile = true;
}

// Optimization: if we are already at end, then no need to
// issue a VSIFSeekL().
if( whence == SEEK_END )
Expand Down Expand Up @@ -228,6 +246,10 @@ _tiffCloseProc( thandle_t th )
CPLFree(psGTH->ppCachedData);
CPLFree(psGTH->panCachedOffsets);
CPLFree(psGTH->panCachedSizes);
CPLFree(psGTH->filename);
CPLFree(psGTH->mode);
if( psGTH->closeFile )
VSIFCloseL(psGTH->fpL);
CPLFree(psGTH);
return 0;
}
Expand Down Expand Up @@ -351,6 +373,9 @@ TIFF* VSI_TIFFOpen( const char* name, const char* mode,
psGTH->fpL = fpL;
psGTH->nExpectedPos = 0;
psGTH->bAtEndOfFile = false;
psGTH->filename = VSIStrdup(name);
psGTH->mode = VSIStrdup(mode);
psGTH->closeFile = false;

// No need to buffer on /vsimem/
bool bAllocBuffer = !bReadOnly;
Expand Down Expand Up @@ -378,7 +403,20 @@ TIFF* VSI_TIFFOpen( const char* name, const char* mode,
_tiffSeekProc, _tiffCloseProc, _tiffSizeProc,
_tiffMapProc, _tiffUnmapProc );
if( tif == NULL )
{
CPLFree(psGTH->filename);
CPLFree(psGTH->mode);
CPLFree(psGTH);
}

return tif;
}

int VSI_TIFFCloseFile( TIFF* tif )
{
GDALTiffHandle* psGTH = static_cast<GDALTiffHandle *>(
TIFFClientdata(tif) );
int ret = VSIFCloseL(psGTH->fpL);
psGTH->fpL = NULL;
return ret;
}
1 change: 1 addition & 0 deletions gdal/frmts/gtiff/tifvsi.h
Expand Up @@ -45,5 +45,6 @@ void VSI_TIFFSetCachedRanges( thandle_t th, int nRanges,
void ** ppData, // memory pointed by ppData[i] must be kept alive by caller
const vsi_l_offset* panOffsets,
const size_t* panSizes );
int VSI_TIFFCloseFile( TIFF* tif );

#endif // TIFVSI_H_INCLUDED

0 comments on commit 9282374

Please sign in to comment.