wasCSharpSQLite – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | namespace Community.CsharpSqlite |
2 | { |
||
3 | public partial class Sqlite3 |
||
4 | { |
||
5 | /* |
||
6 | ** 2007 August 22 |
||
7 | ** |
||
8 | ** The author disclaims copyright to this source code. In place of |
||
9 | ** a legal notice, here is a blessing: |
||
10 | ** |
||
11 | ** May you do good and not evil. |
||
12 | ** May you find forgiveness for yourself and forgive others. |
||
13 | ** May you share freely, never taking more than you give. |
||
14 | ** |
||
15 | ************************************************************************* |
||
16 | ** |
||
17 | ** This file implements a special kind of sqlite3_file object used |
||
18 | ** by SQLite to create journal files if the atomic-write optimization |
||
19 | ** is enabled. |
||
20 | ** |
||
21 | ** The distinctive characteristic of this sqlite3_file is that the |
||
22 | ** actual on disk file is created lazily. When the file is created, |
||
23 | ** the caller specifies a buffer size for an in-memory buffer to |
||
24 | ** be used to service read() and write() requests. The actual file |
||
25 | ** on disk is not created or populated until either: |
||
26 | ** |
||
27 | ** 1) The in-memory representation grows too large for the allocated |
||
28 | ** buffer, or |
||
29 | ** 2) The sqlite3JournalCreate() function is called. |
||
30 | ************************************************************************* |
||
31 | ** Included in SQLite3 port to C#-SQLite; 2008 Noah B Hart |
||
32 | ** C#-SQLite is an independent reimplementation of the SQLite software library |
||
33 | ** |
||
34 | ** SQLITE_SOURCE_ID: 2010-08-23 18:52:01 42537b60566f288167f1b5864a5435986838e3a3 |
||
35 | ** |
||
36 | ************************************************************************* |
||
37 | */ |
||
38 | #if SQLITE_ENABLE_ATOMIC_WRITE |
||
39 | //#include "sqliteInt.h" |
||
40 | |||
41 | /* |
||
42 | ** A JournalFile object is a subclass of sqlite3_file used by |
||
43 | ** as an open file handle for journal files. |
||
44 | */ |
||
45 | struct JournalFile { |
||
46 | sqlite3_io_methods pMethod; /* I/O methods on journal files */ |
||
47 | int nBuf; /* Size of zBuf[] in bytes */ |
||
48 | string zBuf; /* Space to buffer journal writes */ |
||
49 | int iSize; /* Amount of zBuf[] currently used */ |
||
50 | int flags; /* xOpen flags */ |
||
51 | sqlite3_vfs pVfs; /* The "real" underlying VFS */ |
||
52 | sqlite3_file pReal; /* The "real" underlying file descriptor */ |
||
53 | string zJournal; /* Name of the journal file */ |
||
54 | }; |
||
55 | typedef struct JournalFile JournalFile; |
||
56 | |||
57 | /* |
||
58 | ** If it does not already exists, create and populate the on-disk file |
||
59 | ** for JournalFile p. |
||
60 | */ |
||
61 | static int createFile(JournalFile p){ |
||
62 | int rc = SQLITE_OK; |
||
63 | if( null==p.pReal ){ |
||
64 | sqlite3_file pReal = (sqlite3_file )&p[1]; |
||
65 | rc = sqlite3OsOpen(p.pVfs, p.zJournal, pReal, p.flags, 0); |
||
66 | if( rc==SQLITE_OK ){ |
||
67 | p.pReal = pReal; |
||
68 | if( p.iSize>0 ){ |
||
69 | Debug.Assert(p.iSize<=p.nBuf); |
||
70 | rc = sqlite3OsWrite(p.pReal, p.zBuf, p.iSize, 0); |
||
71 | } |
||
72 | } |
||
73 | } |
||
74 | return rc; |
||
75 | } |
||
76 | |||
77 | /* |
||
78 | ** Close the file. |
||
79 | */ |
||
80 | static int jrnlClose(sqlite3_file pJfd){ |
||
81 | JournalFile p = (JournalFile )pJfd; |
||
82 | if( p.pReal ){ |
||
83 | sqlite3OsClose(p.pReal); |
||
84 | } |
||
85 | sqlite3DbFree(db,p.zBuf); |
||
86 | return SQLITE_OK; |
||
87 | } |
||
88 | |||
89 | /* |
||
90 | ** Read data from the file. |
||
91 | */ |
||
92 | static int jrnlRead( |
||
93 | sqlite3_file *pJfd, /* The journal file from which to read */ |
||
94 | void *zBuf, /* Put the results here */ |
||
95 | int iAmt, /* Number of bytes to read */ |
||
96 | sqlite_int64 iOfst /* Begin reading at this offset */ |
||
97 | ){ |
||
98 | int rc = SQLITE_OK; |
||
99 | JournalFile *p = (JournalFile )pJfd; |
||
100 | if( p->pReal ){ |
||
101 | rc = sqlite3OsRead(p->pReal, zBuf, iAmt, iOfst); |
||
102 | }else if( (iAmt+iOfst)>p->iSize ){ |
||
103 | rc = SQLITE_IOERR_SHORT_READ; |
||
104 | }else{ |
||
105 | memcpy(zBuf, &p->zBuf[iOfst], iAmt); |
||
106 | } |
||
107 | return rc; |
||
108 | } |
||
109 | |||
110 | /* |
||
111 | ** Write data to the file. |
||
112 | */ |
||
113 | static int jrnlWrite( |
||
114 | sqlite3_file pJfd, /* The journal file into which to write */ |
||
115 | string zBuf, /* Take data to be written from here */ |
||
116 | int iAmt, /* Number of bytes to write */ |
||
117 | sqlite_int64 iOfst /* Begin writing at this offset into the file */ |
||
118 | ){ |
||
119 | int rc = SQLITE_OK; |
||
120 | JournalFile p = (JournalFile )pJfd; |
||
121 | if( null==p.pReal && (iOfst+iAmt)>p.nBuf ){ |
||
122 | rc = createFile(p); |
||
123 | } |
||
124 | if( rc==SQLITE_OK ){ |
||
125 | if( p.pReal ){ |
||
126 | rc = sqlite3OsWrite(p.pReal, zBuf, iAmt, iOfst); |
||
127 | }else{ |
||
128 | memcpy(p.zBuf[iOfst], zBuf, iAmt); |
||
129 | if( p.iSize<(iOfst+iAmt) ){ |
||
130 | p.iSize = (iOfst+iAmt); |
||
131 | } |
||
132 | } |
||
133 | } |
||
134 | return rc; |
||
135 | } |
||
136 | |||
137 | /* |
||
138 | ** Truncate the file. |
||
139 | */ |
||
140 | static int jrnlTruncate(sqlite3_file pJfd, sqlite_int64 size){ |
||
141 | int rc = SQLITE_OK; |
||
142 | JournalFile p = (JournalFile )pJfd; |
||
143 | if( p.pReal ){ |
||
144 | rc = sqlite3OsTruncate(p.pReal, size); |
||
145 | }else if( size<p.iSize ){ |
||
146 | p.iSize = size; |
||
147 | } |
||
148 | return rc; |
||
149 | } |
||
150 | |||
151 | /* |
||
152 | ** Sync the file. |
||
153 | */ |
||
154 | static int jrnlSync(sqlite3_file pJfd, int flags){ |
||
155 | int rc; |
||
156 | JournalFile p = (JournalFile )pJfd; |
||
157 | if( p.pReal ){ |
||
158 | rc = sqlite3OsSync(p.pReal, flags); |
||
159 | }else{ |
||
160 | rc = SQLITE_OK; |
||
161 | } |
||
162 | return rc; |
||
163 | } |
||
164 | |||
165 | /* |
||
166 | ** Query the size of the file in bytes. |
||
167 | */ |
||
168 | static int jrnlFileSize(sqlite3_file pJfd, sqlite_int64 pSize){ |
||
169 | int rc = SQLITE_OK; |
||
170 | JournalFile p = (JournalFile )pJfd; |
||
171 | if( p.pReal ){ |
||
172 | rc = sqlite3OsFileSize(p.pReal, pSize); |
||
173 | }else{ |
||
174 | pSize = (sqlite_int64) p.iSize; |
||
175 | } |
||
176 | return rc; |
||
177 | } |
||
178 | |||
179 | /* |
||
180 | ** Table of methods for JournalFile sqlite3_file object. |
||
181 | */ |
||
182 | static struct sqlite3_io_methods JournalFileMethods = { |
||
183 | 1, /* iVersion */ |
||
184 | jrnlClose, /* xClose */ |
||
185 | jrnlRead, /* xRead */ |
||
186 | jrnlWrite, /* xWrite */ |
||
187 | jrnlTruncate, /* xTruncate */ |
||
188 | jrnlSync, /* xSync */ |
||
189 | jrnlFileSize, /* xFileSize */ |
||
190 | 0, /* xLock */ |
||
191 | 0, /* xUnlock */ |
||
192 | 0, /* xCheckReservedLock */ |
||
193 | 0, /* xFileControl */ |
||
194 | 0, /* xSectorSize */ |
||
195 | 0, /* xDeviceCharacteristics */ |
||
196 | 0, /* xShmMap */ |
||
197 | 0, /* xShmLock */ |
||
198 | 0, /* xShmBarrier */ |
||
199 | |||
200 | }; |
||
201 | |||
202 | /* |
||
203 | ** Open a journal file. |
||
204 | */ |
||
205 | int sqlite3JournalOpen( |
||
206 | sqlite3_vfs pVfs, /* The VFS to use for actual file I/O */ |
||
207 | string zName, /* Name of the journal file */ |
||
208 | sqlite3_file pJfd, /* Preallocated, blank file handle */ |
||
209 | int flags, /* Opening flags */ |
||
210 | int nBuf /* Bytes buffered before opening the file */ |
||
211 | ){ |
||
212 | JournalFile p = (JournalFile )pJfd; |
||
213 | memset(p, 0, sqlite3JournalSize(pVfs)); |
||
214 | if( nBuf>0 ){ |
||
215 | p.zBuf = sqlite3MallocZero(nBuf); |
||
216 | if( null==p.zBuf ){ |
||
217 | return SQLITE_NOMEM; |
||
218 | } |
||
219 | }else{ |
||
220 | return sqlite3OsOpen(pVfs, zName, pJfd, flags, 0); |
||
221 | } |
||
222 | p.pMethod = JournalFileMethods; |
||
223 | p.nBuf = nBuf; |
||
224 | p.flags = flags; |
||
225 | p.zJournal = zName; |
||
226 | p.pVfs = pVfs; |
||
227 | return SQLITE_OK; |
||
228 | } |
||
229 | |||
230 | /* |
||
231 | ** If the argument p points to a JournalFile structure, and the underlying |
||
232 | ** file has not yet been created, create it now. |
||
233 | */ |
||
234 | int sqlite3JournalCreate(sqlite3_file p){ |
||
235 | if( p.pMethods!=&JournalFileMethods ){ |
||
236 | return SQLITE_OK; |
||
237 | } |
||
238 | return createFile((JournalFile )p); |
||
239 | } |
||
240 | |||
241 | /* |
||
242 | ** Return the number of bytes required to store a JournalFile that uses vfs |
||
243 | ** pVfs to create the underlying on-disk files. |
||
244 | */ |
||
245 | int sqlite3JournalSize(sqlite3_vfs pVfs){ |
||
246 | return (pVfs->szOsFile+sizeof(JournalFile)); |
||
247 | } |
||
248 | #endif |
||
249 | } |
||
250 | } |