- Main Page
- Classes
- Files
- Directories
- Related Pages

- File List
- File Members

# cpl_vsi.h File Reference

`#include "cpl_port.h"`
`#include <unistd.h>`
`#include <sys/stat.h>`

Go to the source code of this file.

## Defines
#define | VSI_ISLNK(x) S_ISLNK(x)
#define | VSI_ISREG(x) S_ISREG(x)
#define | VSI_ISDIR(x) S_ISDIR(x)
#define | VSI_ISCHR(x) S_ISCHR(x)
#define | VSI_ISBLK(x) S_ISBLK(x)
#define | VSIDebug4(f, a1, a2, a3, a4) {}
#define | VSIDebug3(f, a1, a2, a3) {}
#define | VSIDebug2(f, a1, a2) {}
#define | VSIDebug1(f, a1) {}
## Typedefs
typedef stat | VSIStatBuf
typedef GUIntBig | vsi_l_offset
typedef VSI_STAT64_T | VSIStatBufL
## Functions
FILE * | VSIFOpen (const char *, const char *)
int | VSIFClose (FILE *)
int | VSIFSeek (FILE *, long, int)
long | VSIFTell (FILE *)
void | VSIRewind (FILE *)
void | VSIFFlush (FILE *)
size_t | VSIFRead (void *, size_t, size_t, FILE *)
size_t | VSIFWrite (const void *, size_t, size_t, FILE *)
char * | VSIFGets (char *, int, FILE *)
int | VSIFPuts (const char *, FILE *)
int | VSIFPrintf (FILE *, const char *,...)
int | VSIFGetc (FILE *)
int | VSIFPutc (int, FILE *)
int | VSIUngetc (int, FILE *)
int | VSIFEof (FILE *)
int | VSIStat (const char *, VSIStatBuf *)
FILE * | VSIFOpenL (const char *, const char *)
| Open file.
int | VSIFCloseL (FILE *)
| Close file.
int | VSIFSeekL (FILE *, vsi_l_offset, int)
| Seek to requested offset.
vsi_l_offset | VSIFTellL (FILE *)
| Tell current file offset.
void | VSIRewindL (FILE *)
size_t | VSIFReadL (void *, size_t, size_t, FILE *)
| Read bytes from file.
size_t | VSIFWriteL (const void *, size_t, size_t, FILE *)
| Write bytes to file.
int | VSIFEofL (FILE *)
| Test for end of file.
int | VSIFFlushL (FILE *)
| Flush pending writes to disk.
int | VSIStatL (const char *, VSIStatBufL *)
| Get filesystem object info.
void * | VSICalloc (size_t, size_t)
void * | VSIMalloc (size_t)
void | VSIFree (void *)
void * | VSIRealloc (void *, size_t)
char * | VSIStrdup (const char *)
int | VSIMkdir (const char *pathname, long mode)
| Create a directory.
int | VSIRmdir (const char *pathname)
| Delete a directory.
int | VSIUnlink (const char *pathname)
| Delete a file.
int | VSIRename (const char *oldpath, const char *newpath)
| Rename a file.
char * | VSIStrerror (int)
void | VSIInstallMemFileHandler (void)
| Install "memory" file system handler.
void | VSIInstallLargeFileHandler (void)
void | VSICleanupFileManager (void)
FILE * | VSIFileFromMemBuffer (const char *pszFilename, GByte *pabyData, vsi_l_offset nDataLength, int bTakeOwnership)
| Create memory "file" from a buffer.
GByte * | VSIGetMemFileBuffer (const char *pszFilename, vsi_l_offset *pnDataLength, int bUnlinkAndSeize)
| Fetch buffer underlying memory file.
unsigned long | VSITime (unsigned long *)
const char * | VSICTime (unsigned long)
tm * | VSIGMTime (const time_t *pnTime, struct tm *poBrokenTime)
tm * | VSILocalTime (const time_t *pnTime, struct tm *poBrokenTime)

## Detailed Description

Standard C Covers

The VSI functions are intended to be hookable aliases for Standard C I/O, memory allocation and other system functions. They are intended to allow virtualization of disk I/O so that non file data sources can be made to appear as files, and so that additional error trapping and reporting can be interested. The memory access API is aliased so that special application memory management services can be used.

Is is intended that each of these functions retains exactly the same calling pattern as the original Standard C functions they relate to. This means we don't have to provide custom documentation, and also means that the default implementation is very simple.

## Function Documentation

int VSIFCloseL | ( | FILE * | fp | ) |

  | Close file. This function closes the indicated file. This method goes through the VSIFileHandler virtualization and may work on unusual filesystems such as in memory. Analog of the POSIX fclose() function. Parameters:
  | fp | file handle opened with VSIOpenL().
Returns: 0 on success or -1 on failure.

int VSIFEofL | ( | FILE * | fp | ) |

  | Test for end of file. Returns TRUE (non-zero) if the file read/write offset is currently at the end of the file. This method goes through the VSIFileHandler virtualization and may work on unusual filesystems such as in memory. Analog of the POSIX feof() call. Parameters:
  | fp | file handle opened with VSIOpenL().
Returns: TRUE if at EOF else FALSE.

int VSIFFlushL | ( | FILE * | fp | ) |

  | Flush pending writes to disk. For files in write or update mode and on filesystem types where it is applicable, all pending output on the file is flushed to the physical disk. This method goes through the VSIFileHandler virtualization and may work on unusual filesystems such as in memory. Analog of the POSIX fflush() call. Parameters:
  | fp | file handle opened with VSIOpenL().
Returns: 0 on success or -1 on error.

FILE* VSIFileFromMemBuffer | ( | const char * | pszFilename,
|  | GByte * | pabyData,
|  | vsi_l_offset | nDataLength,
|  | int | bTakeOwnership
| ) |

  | Create memory "file" from a buffer. A virtual memory file is created from the passed buffer with the indicated filename. Under normal conditions the filename would need to be absolute and within the /vsimem/ portion of the filesystem. If bTakeOwnership is TRUE, then the memory file system handler will take ownership of the buffer, freeing it when the file is deleted. Otherwise it remains the responsibility of the caller, but should not be freed as long as it might be accessed as a file. In no circumstances does this function take a copy of the pabyData contents. Parameters:
  | pszFilename | the filename to be created.
  | pabyData | the data buffer for the file.
  | nDataLength | the length of buffer in bytes.
  | bTakeOwnership | TRUE to transfer "ownership" of buffer or FALSE.
Returns: open file handle on created file (see VSIFOpenL()).

FILE* VSIFOpenL | ( | const char * | pszFilename,
|  | const char * | pszAccess
| ) |

  | Open file. This function opens a file with the desired access. Large files (larger than 2GB) should be supported. Binary access is always implied and the "b" does not need to be included in the pszAccess string. Note that the "FILE *" returned by this function is not really a standard C library FILE *, and cannot be used with any functions other than the "VSI*L" family of functions. They aren't "real" FILE objects. This method goes through the VSIFileHandler virtualization and may work on unusual filesystems such as in memory. Analog of the POSIX fopen() function. Parameters:
  | pszFilename | the file to open.
  | pszAccess | access requested (ie. "r", "r+", "w".
Returns: NULL on failure, or the file handle.

size_t VSIFReadL | ( | void * | pBuffer,
|  | size_t | nSize,
|  | size_t | nCount,
|  | FILE * | fp
| ) |

  | Read bytes from file. Reads nCount objects of nSize bytes from the indicated file at the current offset into the indicated buffer. This method goes through the VSIFileHandler virtualization and may work on unusual filesystems such as in memory. Analog of the POSIX fread() call. Parameters:
  | pBuffer | the buffer into which the data should be read (at least nCount * nSize bytes in size.
  | nSize | size of objects to read in bytes.
  | nCount | number of objects to read.
  | fp | file handle opened with VSIOpenL().
Returns: number of objects successfully read.

int VSIFSeekL | ( | FILE * | fp,
|  | vsi_l_offset | nOffset,
|  | int | nWhence
| ) |

  | Seek to requested offset. Seek to the desired offset (nOffset) in the indicated file. This method goes through the VSIFileHandler virtualization and may work on unusual filesystems such as in memory. Analog of the POSIX fseek() call. Parameters:
  | fp | file handle opened with VSIOpenL().
  | nOffset | offset in bytes.
  | nWhence | one of SEEK_SET, SEEK_CUR or SEEK_END.
Returns: 0 on success or -1 one failure.

vsi_l_offset VSIFTellL | ( | FILE * | fp | ) |

  | Tell current file offset. Returns the current file read/write offset in bytes from the beginning of the file. This method goes through the VSIFileHandler virtualization and may work on unusual filesystems such as in memory. Analog of the POSIX ftell() call. Parameters:
  | fp | file handle opened with VSIOpenL().
Returns: file offset in bytes.

size_t VSIFWriteL | ( | const void * | pBuffer,
|  | size_t | nSize,
|  | size_t | nCount,
|  | FILE * | fp
| ) |

  | Write bytes to file. Writess nCount objects of nSize bytes to the indicated file at the current offset into the indicated buffer. This method goes through the VSIFileHandler virtualization and may work on unusual filesystems such as in memory. Analog of the POSIX fwrite() call. Parameters:
  | pBuffer | the buffer from which the data should be written (at least nCount * nSize bytes in size.
  | nSize | size of objects to read in bytes.
  | nCount | number of objects to read.
  | fp | file handle opened with VSIOpenL().
Returns: number of objects successfully written.

GByte* VSIGetMemFileBuffer | ( | const char * | pszFilename,
|  | vsi_l_offset * | pnDataLength,
|  | int | bUnlinkAndSeize
| ) |

  | Fetch buffer underlying memory file. This function returns a pointer to the memory buffer underlying a virtual "in memory" file. If bUnlinkAndSeize is TRUE the filesystem object will be deleted, and ownership of the buffer will pass to the caller otherwise the underlying file will remain in existance. Parameters:
  | pszFilename | the name of the file to grab the buffer of.
  | buffer | (file) length returned in this variable.
  | bUnlinkAndSeize | TRUE to remove the file, or FALSE to leave unaltered.
Returns: pointer to memory buffer or NULL on failure.

void VSIInstallMemFileHandler | ( | void |  | ) |

| Install "memory" file system handler. A special file handler is installed that allows block of memory to be treated as files. All portions of the file system underneath the base path "/vsimem/" will be handled by this driver. Normal VSI*L functions can be used freely to create and destroy memory arrays treating them as if they were real file system objects. Some additional methods exist to efficient create memory file system objects without duplicating original copies of the data or to "steal" the block of memory associated with a memory file. At this time the memory handler does not properly handle directory semantics for the memory portion of the filesystem. The VSIReadDir() function is not supported though this will be corrected in the future. Calling this function repeatedly should do no harm, though it is not necessary. It is already called the first time a virtualizable file access function (ie. VSIFOpenL(), VSIMkDir(), etc) is called. This code example demonstrates using GDAL to translate from one memory buffer to another.
     GByte *ConvertBufferFormat( GByte *pabyInData, vsi_l_offset nInDataLength,
                                 vsi_l_offset *pnOutDataLength )
     {
         // create memory file system object from buffer.
         VSIFCloseL( VSIFileFromMemBuffer( "/vsimem/work.dat", pabyInData,
                                           nInDataLength, FALSE ) );

         // Open memory buffer for read.
         GDALDatasetH hDS = GDALOpen( "/vsimem/work.dat", GA_ReadOnly );

         // Get output format driver.
         GDALDriverH hDriver = GDALGetDriverByName( "GTiff" );
         GDALDatasetH hOutDS;

         hOutDS = GDALCreateCopy( hDriver, "/vsimem/out.tif", hDS, TRUE, NULL,
                                  NULL, NULL );

         // close source file, and "unlink" it.
         GDALClose( hDS );
         VSIUnlink( "/vsimem/work.dat" );

         // seize the buffer associated with the output file.

         return VSIGetMemFileBuffer( "/vsimem/out.tif", pnOutDataLength, TRUE );
     }

int VSIMkdir | ( | const char * | pszPathname,
|  | long | mode
| ) |

  | Create a directory. Create a new directory with the indicated mode. The mode is ignored on some platforms. A reasonable default mode value would be 0666. This method goes through the VSIFileHandler virtualization and may work on unusual filesystems such as in memory. Analog of the POSIX mkdir() function. Parameters:
  | pszPathname | the path to the directory to create.
  | mode | the permissions mode.
Returns: 0 on success or -1 on an error.

int VSIRename | ( | const char * | oldpath,
|  | const char * | newpath
| ) |

  | Rename a file. Renames a file object in the file system. It should be possible to rename a file onto a new filesystem, but it is safest if this function is only used to rename files that remain in the same directory. This method goes through the VSIFileHandler virtualization and may work on unusual filesystems such as in memory. Analog of the POSIX rename() function. Parameters:
  | oldpath | the name of the file to be renamed.
  | newpath | the name the file should be given.
Returns: 0 on success or -1 on an error.

int VSIRmdir | ( | const char * | pszDirname | ) |

  | Delete a directory. Deletes a directory object from the file system. On some systems the directory must be empty before it can be deleted. This method goes through the VSIFileHandler virtualization and may work on unusual filesystems such as in memory. Analog of the POSIX rmdir() function. Parameters:
  | pszDirname | the path of the directory to be deleted.
Returns: 0 on success or -1 on an error.

int VSIStatL | ( | const char * | pszFilename,
|  | VSIStatBufL * | psStatBuf
| ) |

  | Get filesystem object info. Fetches status information about a filesystem object (file, directory, etc). The returned information is placed in the VSIStatBufL structure. For portability only the st_size (size in bytes), and st_mode (file type). This method is similar to VSIStat(), but will work on large files on systems where this requires special calls. This method goes through the VSIFileHandler virtualization and may work on unusual filesystems such as in memory. Analog of the POSIX stat() function. Parameters:
  | pszFilename | the path of the filesystem object to be queried.
  | psStatBuf | the structure to load with information.
Returns: 0 on success or -1 on an error.

int VSIUnlink | ( | const char * | pszFilename | ) |

  | Delete a file. Deletes a file object from the file system. This method goes through the VSIFileHandler virtualization and may work on unusual filesystems such as in memory. Analog of the POSIX unlink() function. Parameters:
  | pszFilename | the path of the file to be deleted.
Returns: 0 on success or -1 on an error.

Generated for GDAL by 1.4.5.