4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
7 ** May you do good and not evil.
8 ** May you find forgiveness for yourself and forgive others.
9 ** May you share freely, never taking more than you give.
11 *************************************************************************
13 ** This file implements a special kind of sqlite3_file object used
14 ** by SQLite to create journal files if the atomic-write optimization
17 ** The distinctive characteristic of this sqlite3_file is that the
18 ** actual on disk file is created lazily. When the file is created,
19 ** the caller specifies a buffer size for an in-memory buffer to
20 ** be used to service read() and write() requests. The actual file
21 ** on disk is not created or populated until either:
23 ** 1) The in-memory representation grows too large for the allocated
25 ** 2) The sqlite3JournalCreate() function is called.
27 #ifdef SQLITE_ENABLE_ATOMIC_WRITE
28 #include "sqliteInt.h"
32 ** A JournalFile object is a subclass of sqlite3_file used by
33 ** as an open file handle for journal files.
36 sqlite3_io_methods
*pMethod
; /* I/O methods on journal files */
37 int nBuf
; /* Size of zBuf[] in bytes */
38 char *zBuf
; /* Space to buffer journal writes */
39 int iSize
; /* Amount of zBuf[] currently used */
40 int flags
; /* xOpen flags */
41 sqlite3_vfs
*pVfs
; /* The "real" underlying VFS */
42 sqlite3_file
*pReal
; /* The "real" underlying file descriptor */
43 const char *zJournal
; /* Name of the journal file */
45 typedef struct JournalFile JournalFile
;
48 ** If it does not already exists, create and populate the on-disk file
51 static int createFile(JournalFile
*p
){
54 sqlite3_file
*pReal
= (sqlite3_file
*)&p
[1];
55 rc
= sqlite3OsOpen(p
->pVfs
, p
->zJournal
, pReal
, p
->flags
, 0);
59 assert(p
->iSize
<=p
->nBuf
);
60 rc
= sqlite3OsWrite(p
->pReal
, p
->zBuf
, p
->iSize
, 0);
70 static int jrnlClose(sqlite3_file
*pJfd
){
71 JournalFile
*p
= (JournalFile
*)pJfd
;
73 sqlite3OsClose(p
->pReal
);
75 sqlite3_free(p
->zBuf
);
80 ** Read data from the file.
83 sqlite3_file
*pJfd
, /* The journal file from which to read */
84 void *zBuf
, /* Put the results here */
85 int iAmt
, /* Number of bytes to read */
86 sqlite_int64 iOfst
/* Begin reading at this offset */
89 JournalFile
*p
= (JournalFile
*)pJfd
;
91 rc
= sqlite3OsRead(p
->pReal
, zBuf
, iAmt
, iOfst
);
92 }else if( (iAmt
+iOfst
)>p
->iSize
){
93 rc
= SQLITE_IOERR_SHORT_READ
;
95 memcpy(zBuf
, &p
->zBuf
[iOfst
], iAmt
);
101 ** Write data to the file.
103 static int jrnlWrite(
104 sqlite3_file
*pJfd
, /* The journal file into which to write */
105 const void *zBuf
, /* Take data to be written from here */
106 int iAmt
, /* Number of bytes to write */
107 sqlite_int64 iOfst
/* Begin writing at this offset into the file */
110 JournalFile
*p
= (JournalFile
*)pJfd
;
111 if( !p
->pReal
&& (iOfst
+iAmt
)>p
->nBuf
){
116 rc
= sqlite3OsWrite(p
->pReal
, zBuf
, iAmt
, iOfst
);
118 memcpy(&p
->zBuf
[iOfst
], zBuf
, iAmt
);
119 if( p
->iSize
<(iOfst
+iAmt
) ){
120 p
->iSize
= (iOfst
+iAmt
);
128 ** Truncate the file.
130 static int jrnlTruncate(sqlite3_file
*pJfd
, sqlite_int64 size
){
132 JournalFile
*p
= (JournalFile
*)pJfd
;
134 rc
= sqlite3OsTruncate(p
->pReal
, size
);
135 }else if( size
<p
->iSize
){
144 static int jrnlSync(sqlite3_file
*pJfd
, int flags
){
146 JournalFile
*p
= (JournalFile
*)pJfd
;
148 rc
= sqlite3OsSync(p
->pReal
, flags
);
156 ** Query the size of the file in bytes.
158 static int jrnlFileSize(sqlite3_file
*pJfd
, sqlite_int64
*pSize
){
160 JournalFile
*p
= (JournalFile
*)pJfd
;
162 rc
= sqlite3OsFileSize(p
->pReal
, pSize
);
164 *pSize
= (sqlite_int64
) p
->iSize
;
170 ** Table of methods for JournalFile sqlite3_file object.
172 static struct sqlite3_io_methods JournalFileMethods
= {
174 jrnlClose
, /* xClose */
175 jrnlRead
, /* xRead */
176 jrnlWrite
, /* xWrite */
177 jrnlTruncate
, /* xTruncate */
178 jrnlSync
, /* xSync */
179 jrnlFileSize
, /* xFileSize */
182 0, /* xCheckReservedLock */
183 0, /* xFileControl */
185 0, /* xDeviceCharacteristics */
193 ** Open a journal file.
195 int sqlite3JournalOpen(
196 sqlite3_vfs
*pVfs
, /* The VFS to use for actual file I/O */
197 const char *zName
, /* Name of the journal file */
198 sqlite3_file
*pJfd
, /* Preallocated, blank file handle */
199 int flags
, /* Opening flags */
200 int nBuf
/* Bytes buffered before opening the file */
202 JournalFile
*p
= (JournalFile
*)pJfd
;
203 memset(p
, 0, sqlite3JournalSize(pVfs
));
205 p
->zBuf
= sqlite3MallocZero(nBuf
);
210 return sqlite3OsOpen(pVfs
, zName
, pJfd
, flags
, 0);
212 p
->pMethod
= &JournalFileMethods
;
221 ** If the argument p points to a JournalFile structure, and the underlying
222 ** file has not yet been created, create it now.
224 int sqlite3JournalCreate(sqlite3_file
*p
){
225 if( p
->pMethods
!=&JournalFileMethods
){
228 return createFile((JournalFile
*)p
);
232 ** Return the number of bytes required to store a JournalFile that uses vfs
233 ** pVfs to create the underlying on-disk files.
235 int sqlite3JournalSize(sqlite3_vfs
*pVfs
){
236 return (pVfs
->szOsFile
+sizeof(JournalFile
));