1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
|
/******************************************************************************
*
* Project: VSI Virtual File System
* Purpose: Implementation of sparse file virtual io driver.
* Author: Frank Warmerdam, warmerdam@pobox.com
*
******************************************************************************
* Copyright (c) 2010, Frank Warmerdam <warmerdam@pobox.com>
* Copyright (c) 2010-2013, Even Rouault <even dot rouault at spatialys.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
****************************************************************************/
#include "cpl_port.h"
#include "cpl_vsi.h"
#include <cerrno>
#include <cstddef>
#include <cstdlib>
#include <cstring>
#if HAVE_FCNTL_H
#include <fcntl.h>
#endif
#include <algorithm>
#include <map>
#include <memory>
#include <vector>
#include "cpl_conv.h"
#include "cpl_error.h"
#include "cpl_minixml.h"
#include "cpl_multiproc.h"
#include "cpl_string.h"
#include "cpl_vsi_virtual.h"
class SFRegion
{
public:
CPLString osFilename{};
VSILFILE *fp = nullptr;
GUIntBig nDstOffset = 0;
GUIntBig nSrcOffset = 0;
GUIntBig nLength = 0;
GByte byValue = 0;
bool bTriedOpen = false;
};
/************************************************************************/
/* ==================================================================== */
/* VSISparseFileHandle */
/* ==================================================================== */
/************************************************************************/
class VSISparseFileFilesystemHandler;
class VSISparseFileHandle : public VSIVirtualHandle
{
CPL_DISALLOW_COPY_ASSIGN(VSISparseFileHandle)
VSISparseFileFilesystemHandler *m_poFS = nullptr;
bool bEOF = false;
public:
explicit VSISparseFileHandle(VSISparseFileFilesystemHandler *poFS)
: m_poFS(poFS)
{
}
GUIntBig nOverallLength = 0;
GUIntBig nCurOffset = 0;
std::vector<SFRegion> aoRegions{};
int Seek(vsi_l_offset nOffset, int nWhence) override;
vsi_l_offset Tell() override;
size_t Read(void *pBuffer, size_t nSize, size_t nMemb) override;
size_t Write(const void *pBuffer, size_t nSize, size_t nMemb) override;
int Eof() override;
int Close() override;
};
/************************************************************************/
/* ==================================================================== */
/* VSISparseFileFilesystemHandler */
/* ==================================================================== */
/************************************************************************/
class VSISparseFileFilesystemHandler : public VSIFilesystemHandler
{
std::map<GIntBig, int> oRecOpenCount{};
CPL_DISALLOW_COPY_ASSIGN(VSISparseFileFilesystemHandler)
public:
VSISparseFileFilesystemHandler() = default;
~VSISparseFileFilesystemHandler() override = default;
int DecomposePath(const char *pszPath, CPLString &osFilename,
vsi_l_offset &nSparseFileOffset,
vsi_l_offset &nSparseFileSize);
// TODO(schwehr): Fix VSISparseFileFilesystemHandler::Stat to not need
// using.
using VSIFilesystemHandler::Open;
VSIVirtualHandle *Open(const char *pszFilename, const char *pszAccess,
bool bSetError,
CSLConstList /* papszOptions */) override;
int Stat(const char *pszFilename, VSIStatBufL *pStatBuf,
int nFlags) override;
int Unlink(const char *pszFilename) override;
int Mkdir(const char *pszDirname, long nMode) override;
int Rmdir(const char *pszDirname) override;
char **ReadDir(const char *pszDirname) override;
int GetRecCounter()
{
return oRecOpenCount[CPLGetPID()];
}
void IncRecCounter()
{
oRecOpenCount[CPLGetPID()]++;
}
void DecRecCounter()
{
oRecOpenCount[CPLGetPID()]--;
}
};
/************************************************************************/
/* ==================================================================== */
/* VSISparseFileHandle */
/* ==================================================================== */
/************************************************************************/
/************************************************************************/
/* Close() */
/************************************************************************/
int VSISparseFileHandle::Close()
{
for (unsigned int i = 0; i < aoRegions.size(); i++)
{
if (aoRegions[i].fp != nullptr)
CPL_IGNORE_RET_VAL(VSIFCloseL(aoRegions[i].fp));
}
return 0;
}
/************************************************************************/
/* Seek() */
/************************************************************************/
int VSISparseFileHandle::Seek(vsi_l_offset nOffset, int nWhence)
{
bEOF = false;
if (nWhence == SEEK_SET)
nCurOffset = nOffset;
else if (nWhence == SEEK_CUR)
{
nCurOffset += nOffset;
}
else if (nWhence == SEEK_END)
{
nCurOffset = nOverallLength + nOffset;
}
else
{
errno = EINVAL;
return -1;
}
return 0;
}
/************************************************************************/
/* Tell() */
/************************************************************************/
vsi_l_offset VSISparseFileHandle::Tell()
{
return nCurOffset;
}
/************************************************************************/
/* Read() */
/************************************************************************/
size_t VSISparseFileHandle::Read(void *pBuffer, size_t nSize, size_t nCount)
{
if (nCurOffset >= nOverallLength)
{
bEOF = true;
return 0;
}
/* -------------------------------------------------------------------- */
/* Find what region we are in, searching linearly from the */
/* start. */
/* -------------------------------------------------------------------- */
unsigned int iRegion = 0; // Used after for.
for (; iRegion < aoRegions.size(); iRegion++)
{
if (nCurOffset >= aoRegions[iRegion].nDstOffset &&
nCurOffset <
aoRegions[iRegion].nDstOffset + aoRegions[iRegion].nLength)
break;
}
size_t nBytesRequested = nSize * nCount;
if (nBytesRequested == 0)
{
return 0;
}
if (nCurOffset + nBytesRequested > nOverallLength)
{
nBytesRequested = static_cast<size_t>(nOverallLength - nCurOffset);
bEOF = true;
}
/* -------------------------------------------------------------------- */
/* Default to zeroing the buffer if no corresponding region was */
/* found. */
/* -------------------------------------------------------------------- */
if (iRegion == aoRegions.size())
{
memset(pBuffer, 0, nBytesRequested);
nCurOffset += nBytesRequested;
return nBytesRequested / nSize;
}
/* -------------------------------------------------------------------- */
/* If this request crosses region boundaries, split it into two */
/* requests. */
/* -------------------------------------------------------------------- */
size_t nBytesReturnCount = 0;
const GUIntBig nEndOffsetOfRegion =
aoRegions[iRegion].nDstOffset + aoRegions[iRegion].nLength;
if (nCurOffset + nBytesRequested > nEndOffsetOfRegion)
{
const size_t nExtraBytes = static_cast<size_t>(
nCurOffset + nBytesRequested - nEndOffsetOfRegion);
// Recurse to get the rest of the request.
const GUIntBig nCurOffsetSave = nCurOffset;
nCurOffset += nBytesRequested - nExtraBytes;
bool bEOFSave = bEOF;
bEOF = false;
const size_t nBytesRead = this->Read(static_cast<char *>(pBuffer) +
nBytesRequested - nExtraBytes,
1, nExtraBytes);
nCurOffset = nCurOffsetSave;
bEOF = bEOFSave;
nBytesReturnCount += nBytesRead;
nBytesRequested -= nExtraBytes;
}
/* -------------------------------------------------------------------- */
/* Handle a constant region. */
/* -------------------------------------------------------------------- */
if (aoRegions[iRegion].osFilename.empty())
{
memset(pBuffer, aoRegions[iRegion].byValue,
static_cast<size_t>(nBytesRequested));
nBytesReturnCount += nBytesRequested;
}
/* -------------------------------------------------------------------- */
/* Otherwise handle as a file. */
/* -------------------------------------------------------------------- */
else
{
if (aoRegions[iRegion].fp == nullptr)
{
if (!aoRegions[iRegion].bTriedOpen)
{
aoRegions[iRegion].fp =
VSIFOpenL(aoRegions[iRegion].osFilename, "r");
if (aoRegions[iRegion].fp == nullptr)
{
CPLDebug("/vsisparse/", "Failed to open '%s'.",
aoRegions[iRegion].osFilename.c_str());
}
aoRegions[iRegion].bTriedOpen = true;
}
if (aoRegions[iRegion].fp == nullptr)
{
return 0;
}
}
if (VSIFSeekL(aoRegions[iRegion].fp,
nCurOffset - aoRegions[iRegion].nDstOffset +
aoRegions[iRegion].nSrcOffset,
SEEK_SET) != 0)
return 0;
m_poFS->IncRecCounter();
const size_t nBytesRead =
VSIFReadL(pBuffer, 1, static_cast<size_t>(nBytesRequested),
aoRegions[iRegion].fp);
m_poFS->DecRecCounter();
nBytesReturnCount += nBytesRead;
}
nCurOffset += nBytesReturnCount;
return nBytesReturnCount / nSize;
}
/************************************************************************/
/* Write() */
/************************************************************************/
size_t VSISparseFileHandle::Write(const void * /* pBuffer */,
size_t /* nSize */, size_t /* nCount */)
{
errno = EBADF;
return 0;
}
/************************************************************************/
/* Eof() */
/************************************************************************/
int VSISparseFileHandle::Eof()
{
return bEOF ? 1 : 0;
}
/************************************************************************/
/* ==================================================================== */
/* VSISparseFileFilesystemHandler */
/* ==================================================================== */
/************************************************************************/
/************************************************************************/
/* Open() */
/************************************************************************/
VSIVirtualHandle *VSISparseFileFilesystemHandler::Open(
const char *pszFilename, const char *pszAccess, bool /* bSetError */,
CSLConstList /* papszOptions */)
{
if (!STARTS_WITH_CI(pszFilename, "/vsisparse/"))
return nullptr;
if (!EQUAL(pszAccess, "r") && !EQUAL(pszAccess, "rb"))
{
errno = EACCES;
return nullptr;
}
// Arbitrary number.
if (GetRecCounter() == 32)
return nullptr;
const CPLString osSparseFilePath = pszFilename + 11;
/* -------------------------------------------------------------------- */
/* Does this file even exist? */
/* -------------------------------------------------------------------- */
VSILFILE *fp = VSIFOpenL(osSparseFilePath, "r");
if (fp == nullptr)
return nullptr;
CPL_IGNORE_RET_VAL(VSIFCloseL(fp));
/* -------------------------------------------------------------------- */
/* Read the XML file. */
/* -------------------------------------------------------------------- */
CPLXMLNode *psXMLRoot = CPLParseXMLFile(osSparseFilePath);
if (psXMLRoot == nullptr)
return nullptr;
/* -------------------------------------------------------------------- */
/* Setup the file handle on this file. */
/* -------------------------------------------------------------------- */
VSISparseFileHandle *poHandle = new VSISparseFileHandle(this);
/* -------------------------------------------------------------------- */
/* Translate the desired fields out of the XML tree. */
/* -------------------------------------------------------------------- */
for (CPLXMLNode *psRegion = psXMLRoot->psChild; psRegion != nullptr;
psRegion = psRegion->psNext)
{
if (psRegion->eType != CXT_Element)
continue;
if (!EQUAL(psRegion->pszValue, "SubfileRegion") &&
!EQUAL(psRegion->pszValue, "ConstantRegion"))
continue;
SFRegion oRegion;
oRegion.osFilename = CPLGetXMLValue(psRegion, "Filename", "");
if (atoi(CPLGetXMLValue(psRegion, "Filename.relative", "0")) != 0)
{
const CPLString osSFPath = CPLGetPath(osSparseFilePath);
oRegion.osFilename =
CPLFormFilename(osSFPath, oRegion.osFilename, nullptr);
}
// TODO(schwehr): Symbolic constant and an explanation for 32.
oRegion.nDstOffset = CPLScanUIntBig(
CPLGetXMLValue(psRegion, "DestinationOffset", "0"), 32);
oRegion.nSrcOffset =
CPLScanUIntBig(CPLGetXMLValue(psRegion, "SourceOffset", "0"), 32);
oRegion.nLength =
CPLScanUIntBig(CPLGetXMLValue(psRegion, "RegionLength", "0"), 32);
oRegion.byValue =
static_cast<GByte>(atoi(CPLGetXMLValue(psRegion, "Value", "0")));
poHandle->aoRegions.push_back(oRegion);
}
/* -------------------------------------------------------------------- */
/* Get sparse file length, use maximum bound of regions if not */
/* explicit in file. */
/* -------------------------------------------------------------------- */
poHandle->nOverallLength =
CPLScanUIntBig(CPLGetXMLValue(psXMLRoot, "Length", "0"), 32);
if (poHandle->nOverallLength == 0)
{
for (unsigned int i = 0; i < poHandle->aoRegions.size(); i++)
{
poHandle->nOverallLength = std::max(
poHandle->nOverallLength, poHandle->aoRegions[i].nDstOffset +
poHandle->aoRegions[i].nLength);
}
}
CPLDestroyXMLNode(psXMLRoot);
return poHandle;
}
/************************************************************************/
/* Stat() */
/************************************************************************/
int VSISparseFileFilesystemHandler::Stat(const char *pszFilename,
VSIStatBufL *psStatBuf, int nFlags)
{
// TODO(schwehr): Fix this so that the using statement is not needed.
// Will just adding the bool for bSetError be okay?
VSIVirtualHandle *poFile = Open(pszFilename, "r");
memset(psStatBuf, 0, sizeof(VSIStatBufL));
if (poFile == nullptr)
return -1;
poFile->Seek(0, SEEK_END);
const size_t nLength = static_cast<size_t>(poFile->Tell());
delete poFile;
const int nResult =
VSIStatExL(pszFilename + strlen("/vsisparse/"), psStatBuf, nFlags);
psStatBuf->st_size = nLength;
return nResult;
}
/************************************************************************/
/* Unlink() */
/************************************************************************/
int VSISparseFileFilesystemHandler::Unlink(const char * /* pszFilename */)
{
errno = EACCES;
return -1;
}
/************************************************************************/
/* Mkdir() */
/************************************************************************/
int VSISparseFileFilesystemHandler::Mkdir(const char * /* pszPathname */,
long /* nMode */)
{
errno = EACCES;
return -1;
}
/************************************************************************/
/* Rmdir() */
/************************************************************************/
int VSISparseFileFilesystemHandler::Rmdir(const char * /* pszPathname */)
{
errno = EACCES;
return -1;
}
/************************************************************************/
/* ReadDir() */
/************************************************************************/
char **VSISparseFileFilesystemHandler::ReadDir(const char * /* pszPath */)
{
errno = EACCES;
return nullptr;
}
/************************************************************************/
/* VSIInstallSparseFileFilesystemHandler() */
/************************************************************************/
/*!
\brief Install /vsisparse/ virtual file handler.
\verbatim embed:rst
See :ref:`/vsisparse/ documentation <vsisparse>`
\endverbatim
*/
void VSIInstallSparseFileHandler()
{
VSIFileManager::InstallHandler("/vsisparse/",
new VSISparseFileFilesystemHandler);
}
|