namespace Community.CsharpSqlite { public partial class Sqlite3 { /* ** 2007 August 22 ** ** The author disclaims copyright to this source code. In place of ** a legal notice, here is a blessing: ** ** May you do good and not evil. ** May you find forgiveness for yourself and forgive others. ** May you share freely, never taking more than you give. ** ************************************************************************* ** ** This file implements a special kind of sqlite3_file object used ** by SQLite to create journal files if the atomic-write optimization ** is enabled. ** ** The distinctive characteristic of this sqlite3_file is that the ** actual on disk file is created lazily. When the file is created, ** the caller specifies a buffer size for an in-memory buffer to ** be used to service read() and write() requests. The actual file ** on disk is not created or populated until either: ** ** 1) The in-memory representation grows too large for the allocated ** buffer, or ** 2) The sqlite3JournalCreate() function is called. ************************************************************************* ** Included in SQLite3 port to C#-SQLite; 2008 Noah B Hart ** C#-SQLite is an independent reimplementation of the SQLite software library ** ** SQLITE_SOURCE_ID: 2010-08-23 18:52:01 42537b60566f288167f1b5864a5435986838e3a3 ** ************************************************************************* */ #if SQLITE_ENABLE_ATOMIC_WRITE //#include "sqliteInt.h" /* ** A JournalFile object is a subclass of sqlite3_file used by ** as an open file handle for journal files. */ struct JournalFile { sqlite3_io_methods pMethod; /* I/O methods on journal files */ int nBuf; /* Size of zBuf[] in bytes */ string zBuf; /* Space to buffer journal writes */ int iSize; /* Amount of zBuf[] currently used */ int flags; /* xOpen flags */ sqlite3_vfs pVfs; /* The "real" underlying VFS */ sqlite3_file pReal; /* The "real" underlying file descriptor */ string zJournal; /* Name of the journal file */ }; typedef struct JournalFile JournalFile; /* ** If it does not already exists, create and populate the on-disk file ** for JournalFile p. */ static int createFile(JournalFile p){ int rc = SQLITE_OK; if( null==p.pReal ){ sqlite3_file pReal = (sqlite3_file )&p[1]; rc = sqlite3OsOpen(p.pVfs, p.zJournal, pReal, p.flags, 0); if( rc==SQLITE_OK ){ p.pReal = pReal; if( p.iSize>0 ){ Debug.Assert(p.iSize<=p.nBuf); rc = sqlite3OsWrite(p.pReal, p.zBuf, p.iSize, 0); } } } return rc; } /* ** Close the file. */ static int jrnlClose(sqlite3_file pJfd){ JournalFile p = (JournalFile )pJfd; if( p.pReal ){ sqlite3OsClose(p.pReal); } sqlite3DbFree(db,p.zBuf); return SQLITE_OK; } /* ** Read data from the file. */ static int jrnlRead( sqlite3_file *pJfd, /* The journal file from which to read */ void *zBuf, /* Put the results here */ int iAmt, /* Number of bytes to read */ sqlite_int64 iOfst /* Begin reading at this offset */ ){ int rc = SQLITE_OK; JournalFile *p = (JournalFile )pJfd; if( p->pReal ){ rc = sqlite3OsRead(p->pReal, zBuf, iAmt, iOfst); }else if( (iAmt+iOfst)>p->iSize ){ rc = SQLITE_IOERR_SHORT_READ; }else{ memcpy(zBuf, &p->zBuf[iOfst], iAmt); } return rc; } /* ** Write data to the file. */ static int jrnlWrite( sqlite3_file pJfd, /* The journal file into which to write */ string zBuf, /* Take data to be written from here */ int iAmt, /* Number of bytes to write */ sqlite_int64 iOfst /* Begin writing at this offset into the file */ ){ int rc = SQLITE_OK; JournalFile p = (JournalFile )pJfd; if( null==p.pReal && (iOfst+iAmt)>p.nBuf ){ rc = createFile(p); } if( rc==SQLITE_OK ){ if( p.pReal ){ rc = sqlite3OsWrite(p.pReal, zBuf, iAmt, iOfst); }else{ memcpy(p.zBuf[iOfst], zBuf, iAmt); if( p.iSize<(iOfst+iAmt) ){ p.iSize = (iOfst+iAmt); } } } return rc; } /* ** Truncate the file. */ static int jrnlTruncate(sqlite3_file pJfd, sqlite_int64 size){ int rc = SQLITE_OK; JournalFile p = (JournalFile )pJfd; if( p.pReal ){ rc = sqlite3OsTruncate(p.pReal, size); }else if( size