os_rtthread.c 89 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718
  1. /*
  2. ** 2004 May 22
  3. **
  4. ** The author disclaims copyright to this source code. In place of
  5. ** a legal notice, here is a blessing:
  6. **
  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.
  10. **
  11. ******************************************************************************
  12. **
  13. ** This file contains code that is specific to Windows.
  14. */
  15. #include "sqliteInt.h"
  16. #if SQLITE_OS_RTTHREAD /* This file is used for rt-thread only */
  17. #include <rtthread.h>
  18. #include <dfs_posix.h>
  19. /*
  20. ** Include code that is common to all os_*.c files
  21. */
  22. #include "os_common.h"
  23. #ifndef RT_USING_NEWLIB
  24. #ifndef EINTR
  25. #define EINTR 4 /* Interrupted system call */
  26. #endif
  27. #ifndef ENOLCK
  28. #define ENOLCK 46 /* No record locks available */
  29. #endif
  30. #ifndef EACCES
  31. #define EACCES 13 /* Permission denied */
  32. #endif
  33. #ifndef EPERM
  34. #define EPERM 1 /* Operation not permitted */
  35. #endif
  36. #ifndef ETIMEDOUT
  37. #define ETIMEDOUT 145 /* Connection timed out */
  38. #endif
  39. #ifndef ENOTCONN
  40. #define ENOTCONN 134 /* Transport endpoint is not connected */
  41. #endif
  42. #if defined(__GNUC__) || defined(__ADSPBLACKFIN__)
  43. int _gettimeofday(struct timeval *tp, void *ignore) __attribute__((weak));
  44. int _gettimeofday(struct timeval *tp, void *ignore)
  45. #elif defined(__CC_ARM)
  46. __weak int _gettimeofday(struct timeval *tp, void *ignore)
  47. #elif defined(__IAR_SYSTEMS_ICC__)
  48. #if __VER__ > 540
  49. __weak
  50. #endif
  51. int _gettimeofday(struct timeval *tp, void *ignore)
  52. #else
  53. int _gettimeofday(struct timeval *tp, void *ignore)
  54. #endif
  55. {
  56. return 0;
  57. }
  58. #endif /* RT_USING_NEWLIB */
  59. /*
  60. ** Compiling and using WAL mode requires several APIs that are not
  61. ** available in rt-thread.
  62. */
  63. #if !defined(SQLITE_OMIT_WAL)
  64. # error "WAL mode requires not support from the rt-thread, compile\
  65. with SQLITE_OMIT_WAL."
  66. #endif
  67. /*
  68. ** Are most of the rtthread ANSI APIs available (i.e. with certain exceptions
  69. ** based on the sub-platform)?
  70. */
  71. #if !defined(SQLITE_RTTHREAD_NO_ANSI)
  72. # warning "please ensure rtthread ANSI APIs is available, otherwise compile with\
  73. SQLITE_RTTHREAD_NO_ANSI"
  74. # define SQLITE_RTTHREAD_HAS_ANSI
  75. #endif
  76. /*
  77. ** Are most of the rtthread Unicode APIs available (i.e. with certain exceptions
  78. ** based on the sub-platform)?
  79. */
  80. #if !defined(SQLITE_RTTHREAD_NO_WIDE)
  81. # error "rtthread not support Unicode APIs"
  82. # define SQLITE_RTTHREAD_HAS_WIDE
  83. #endif
  84. /*
  85. ** Make sure at least one set of rtthread APIs is available.
  86. */
  87. #if !defined(SQLITE_RTTHREAD_HAS_ANSI) && !defined(SQLITE_RTTHREAD_HAS_WIDE)
  88. # error "At least one of SQLITE_RTTHREAD_HAS_ANSI and SQLITE_RTTHREAD_HAS_WIDE\
  89. must be defined."
  90. #endif
  91. /*
  92. ** Maximum pathname length (in chars) for rtthread. This should normally be
  93. ** MAX_PATH.
  94. */
  95. #ifndef SQLITE_RTTHREAD_MAX_PATH_CHARS
  96. # warning "default Maximum pathname length be 255, otherwise compile with\
  97. SQLITE_RTTHREAD_MAX_PATH_CHARS=?"
  98. # define SQLITE_RTTHREAD_MAX_PATH_CHARS (255)
  99. #endif
  100. /*
  101. ** Maximum supported path-length.
  102. */
  103. #define MAX_PATHNAME 512
  104. /*
  105. ** Returns non-zero if the character should be treated as a directory
  106. ** separator.
  107. */
  108. #ifndef rtthreadIsDirSep
  109. # define rtthreadIsDirSep(a) ((a) == '/')
  110. #endif
  111. /*
  112. ** This macro is used when a local variable is set to a value that is
  113. ** [sometimes] not used by the code (e.g. via conditional compilation).
  114. */
  115. #ifndef UNUSED_VARIABLE_VALUE
  116. # define UNUSED_VARIABLE_VALUE(x) (void)(x)
  117. #endif
  118. /*
  119. ** Returns the string that should be used as the directory separator.
  120. */
  121. #ifndef rtthreadGetDirDep
  122. # define rtthreadGetDirDep() "/"
  123. #endif
  124. /*
  125. ** The winFile structure is a subclass of sqlite3_file* specific to the win32
  126. ** portability layer.
  127. */
  128. typedef struct rtthreadFile rtthreadFile;
  129. struct rtthreadFile {
  130. sqlite3_io_methods const *pMethod; /* Always the first entry */
  131. sqlite3_vfs *pVfs; /* The VFS that created this rtthreadFile */
  132. int h; /* The file descriptor */
  133. unsigned short int ctrlFlags; /* Behavioral bits. UNIXFILE_* flags */
  134. unsigned char eFileLock; /* The type of lock held on this fd */
  135. int lastErrno; /* The unix errno from last I/O error */
  136. void *lockingContext; /* Locking style specific state */
  137. const char *zPath; /* Name of the file */
  138. int szChunk; /* Configured by FCNTL_CHUNK_SIZE */
  139. #if SQLITE_ENABLE_LOCKING_STYLE
  140. int openFlags; /* The flags specified at open() */
  141. #endif
  142. #if SQLITE_ENABLE_LOCKING_STYLE
  143. unsigned fsFlags; /* cached details from statfs() */
  144. #endif
  145. #ifdef SQLITE_DEBUG
  146. /* The next group of variables are used to track whether or not the
  147. ** transaction counter in bytes 24-27 of database files are updated
  148. ** whenever any part of the database changes. An assertion fault will
  149. ** occur if a file is updated without also updating the transaction
  150. ** counter. This test is made to avoid new problems similar to the
  151. ** one described by ticket #3584.
  152. */
  153. unsigned char transCntrChng; /* True if the transaction counter changed */
  154. unsigned char dbUpdate; /* True if any part of database file changed */
  155. unsigned char inNormalWrite; /* True if in a normal write operation */
  156. #endif
  157. #ifdef SQLITE_TEST
  158. /* In test mode, increase the size of this structure a bit so that
  159. ** it is larger than the struct CrashFile defined in test6.c.
  160. */
  161. char aPadding[32];
  162. #endif
  163. };
  164. /*
  165. ** Allowed values for the rtthreadFile.ctrlFlags bitmask:
  166. */
  167. #define UNIXFILE_EXCL 0x01 /* Connections from one process only */
  168. #define UNIXFILE_RDONLY 0x02 /* Connection is read only */
  169. #define UNIXFILE_PERSIST_WAL 0x04 /* Persistent WAL mode */
  170. #ifndef SQLITE_DISABLE_DIRSYNC
  171. # define UNIXFILE_DIRSYNC 0x08 /* Directory sync needed */
  172. #else
  173. # define UNIXFILE_DIRSYNC 0x00
  174. #endif
  175. #define UNIXFILE_PSOW 0x10 /* SQLITE_IOCAP_POWERSAFE_OVERWRITE */
  176. #define UNIXFILE_DELETE 0x20 /* Delete on close */
  177. #define UNIXFILE_URI 0x40 /* Filename might have query parameters */
  178. #define UNIXFILE_NOLOCK 0x80 /* Do no file locking */
  179. #define UNIXFILE_WARNED 0x0100 /* verifyDbFile() warnings have been issued */
  180. /*
  181. ** The following variable is (normally) set once and never changes
  182. ** thereafter. It records whether the operating system is Win9x
  183. ** or WinNT.
  184. **
  185. ** 0: Operating system unknown.
  186. ** 1: Operating system is rtthread.
  187. **
  188. ** In order to facilitate testing on a rtthread system, the test fixture
  189. ** can manually set this value to 1 to emulate Win98 behavior.
  190. */
  191. #ifdef SQLITE_TEST
  192. int sqlite3_os_type = 0;
  193. #elif !SQLITE_OS_RTTHREAD && \
  194. defined(SQLITE_RTTHREAD_HAS_ANSI) && defined(SQLITE_RTTHREAD_HAS_WIDE)
  195. static int sqlite3_os_type = 0;
  196. #endif
  197. #ifndef SYSCALL
  198. # define SYSCALL sqlite3_syscall_ptr
  199. #endif
  200. #include <dfs_posix.h>
  201. static int _Access(const char *pathname, int mode)
  202. {
  203. int fd;
  204. fd = open(pathname, O_RDONLY, mode);
  205. if (fd >= 0)
  206. {
  207. close(fd);
  208. return 0;
  209. }
  210. return -1;
  211. }
  212. /*
  213. ** Invoke open(). Do so multiple times, until it either succeeds or
  214. ** fails for some reason other than EINTR.
  215. **
  216. ** If the file creation mode "m" is 0 then set it to the default for
  217. ** SQLite. The default is SQLITE_DEFAULT_FILE_PERMISSIONS (normally
  218. ** 0644) as modified by the system umask. If m is not 0, then
  219. ** make the file creation mode be exactly m ignoring the umask.
  220. **
  221. ** The m parameter will be non-zero only when creating -wal, -journal,
  222. ** and -shm files. We want those files to have *exactly* the same
  223. ** permissions as their original database, unadulterated by the umask.
  224. ** In that way, if a database file is -rw-rw-rw or -rw-rw-r-, and a
  225. ** transaction crashes and leaves behind hot journals, then any
  226. ** process that is able to write to the database will also be able to
  227. ** recover the hot journals.
  228. */
  229. static int robust_open(const char *z, int f, mode_t m);
  230. /*
  231. ** Open a file descriptor to the directory containing file zFilename.
  232. ** If successful, *pFd is set to the opened file descriptor and
  233. ** SQLITE_OK is returned. If an error occurs, either SQLITE_NOMEM
  234. ** or SQLITE_CANTOPEN is returned and *pFd is set to an undefined
  235. ** value.
  236. **
  237. ** The directory file descriptor is used for only one thing - to
  238. ** fsync() a directory to make sure file creation and deletion events
  239. ** are flushed to disk. Such fsyncs are not needed on newer
  240. ** journaling filesystems, but are required on older filesystems.
  241. **
  242. ** This routine can be overridden using the xSetSysCall interface.
  243. ** The ability to override this routine was added in support of the
  244. ** chromium sandbox. Opening a directory is a security risk (we are
  245. ** told) so making it overrideable allows the chromium sandbox to
  246. ** replace this routine with a harmless no-op. To make this routine
  247. ** a no-op, replace it with a stub that returns SQLITE_OK but leaves
  248. ** *pFd set to a negative number.
  249. **
  250. ** If SQLITE_OK is returned, the caller is responsible for closing
  251. ** the file descriptor *pFd using close().
  252. */
  253. static int openDirectory(const char *zFilename, int *pFd);
  254. /*
  255. ** Many system calls are accessed through pointer-to-functions so that
  256. ** they may be overridden at runtime to facilitate fault injection during
  257. ** testing and sandboxing. The following array holds the names and pointers
  258. ** to all overrideable system calls.
  259. */
  260. static struct rtthread_syscall {
  261. const char *zName; /* Name of the system call */
  262. sqlite3_syscall_ptr pCurrent; /* Current value of the system call */
  263. sqlite3_syscall_ptr pDefault; /* Default value */
  264. } aSyscall[] = {
  265. {"sleep", (sqlite3_syscall_ptr)rt_thread_delay, 0},
  266. #define osSleep ((rt_err_t(*)(rt_tick_t))aSyscall[0].pCurrent)
  267. { "open", (sqlite3_syscall_ptr)open, 0 },
  268. #define osOpen ((int(*)(const char*,int,int))aSyscall[1].pCurrent)
  269. { "close", (sqlite3_syscall_ptr)close, 0 },
  270. #define osClose ((int(*)(int))aSyscall[2].pCurrent)
  271. { "getcwd", (sqlite3_syscall_ptr)getcwd, 0 },
  272. #define osGetcwd ((char*(*)(char*,size_t))aSyscall[3].pCurrent)
  273. { "stat", (sqlite3_syscall_ptr)stat, 0 },
  274. #define osStat ((int(*)(const char*,struct stat*))aSyscall[4].pCurrent)
  275. { "fstat", (sqlite3_syscall_ptr)fstat, 0 },
  276. #define osFstat ((int(*)(int,struct stat*))aSyscall[5].pCurrent)
  277. { "read", (sqlite3_syscall_ptr)read, 0 },
  278. #define osRead ((int(*)(int,void*,size_t))aSyscall[6].pCurrent)
  279. { "write", (sqlite3_syscall_ptr)write, 0 },
  280. #define osWrite ((int(*)(int,const void*,size_t))aSyscall[7].pCurrent)
  281. { "unlink", (sqlite3_syscall_ptr)unlink, 0 },
  282. #define osUnlink ((int(*)(const char*))aSyscall[8].pCurrent)
  283. { "openDirectory", (sqlite3_syscall_ptr)openDirectory, 0 },
  284. #define osOpenDirectory ((int(*)(const char*,int*))aSyscall[9].pCurrent)
  285. { "mkdir", (sqlite3_syscall_ptr)mkdir, 0 },
  286. #define osMkdir ((int(*)(const char*,mode_t))aSyscall[10].pCurrent)
  287. { "rmdir", (sqlite3_syscall_ptr)rmdir, 0 },
  288. #define osRmdir ((int(*)(const char*))aSyscall[11].pCurrent)
  289. {"access", (sqlite3_syscall_ptr)_Access, 0 },
  290. #define osAccess ((int(*)(const char*, int))aSyscall[12].pCurrent)
  291. }; /* End of the overrideable system calls */
  292. /*
  293. **
  294. ** This function - unixLogError_x(), is only ever called via the macro
  295. ** unixLogError().
  296. **
  297. ** It is invoked after an error occurs in an OS function and errno has been
  298. ** set. It logs a message using sqlite3_log() containing the current value of
  299. ** errno and, if possible, the human-readable equivalent from strerror() or
  300. ** strerror_r().
  301. **
  302. ** The first argument passed to the macro should be the error code that
  303. ** will be returned to SQLite (e.g. SQLITE_IOERR_DELETE, SQLITE_CANTOPEN).
  304. ** The two subsequent arguments should be the name of the OS function that
  305. ** failed (e.g. "unlink", "open") and the associated file-system path,
  306. ** if any.
  307. */
  308. #define rtthreadLogError(a,b,c) rtthreadLogErrorAtLine(a,b,c,__LINE__)
  309. static int rtthreadLogErrorAtLine(
  310. int errcode, /* SQLite error code */
  311. const char *zFunc, /* Name of OS function that failed */
  312. const char *zPath, /* File path associated with error */
  313. int iLine /* Source line number where error occurred */
  314. ){
  315. char *zErr; /* Message from strerror() or equivalent */
  316. int iErrno = errno; /* Saved syscall error number */
  317. /* If this is not a threadsafe build (SQLITE_THREADSAFE==0), then use
  318. ** the strerror() function to obtain the human-readable error message
  319. ** equivalent to errno. Otherwise, use strerror_r().
  320. */
  321. #if SQLITE_THREADSAFE && defined(HAVE_STRERROR_R)
  322. char aErr[80];
  323. memset(aErr, 0, sizeof(aErr));
  324. zErr = aErr;
  325. /* If STRERROR_R_CHAR_P (set by autoconf scripts) or __USE_GNU is defined,
  326. ** assume that the system provides the GNU version of strerror_r() that
  327. ** returns a pointer to a buffer containing the error message. That pointer
  328. ** may point to aErr[], or it may point to some static storage somewhere.
  329. ** Otherwise, assume that the system provides the POSIX version of
  330. ** strerror_r(), which always writes an error message into aErr[].
  331. **
  332. ** If the code incorrectly assumes that it is the POSIX version that is
  333. ** available, the error message will often be an empty string. Not a
  334. ** huge problem. Incorrectly concluding that the GNU version is available
  335. ** could lead to a segfault though.
  336. */
  337. #if defined(STRERROR_R_CHAR_P) || defined(__USE_GNU)
  338. zErr =
  339. # endif
  340. strerror_r(iErrno, aErr, sizeof(aErr)-1);
  341. #elif SQLITE_THREADSAFE
  342. /* This is a threadsafe build, but strerror_r() is not available. */
  343. zErr = "";
  344. #else
  345. /* Non-threadsafe build, use strerror(). */
  346. zErr = strerror(iErrno);
  347. #endif
  348. if( zPath==0 ) zPath = "";
  349. sqlite3_log(errcode,
  350. "os_rtthread.c:%d: (%d) %s(%s) - %s",
  351. iLine, iErrno, zFunc, zPath, zErr
  352. );
  353. return errcode;
  354. }
  355. /*
  356. ** Do not accept any file descriptor less than this value, in order to avoid
  357. ** opening database file using file descriptors that are commonly used for
  358. ** standard input, output, and error.
  359. */
  360. #ifndef SQLITE_MINIMUM_FILE_DESCRIPTOR
  361. # define SQLITE_MINIMUM_FILE_DESCRIPTOR 3
  362. #endif
  363. /*
  364. ** Invoke open(). Do so multiple times, until it either succeeds or
  365. ** fails for some reason other than EINTR.
  366. **
  367. ** If the file creation mode "m" is 0 then set it to the default for
  368. ** SQLite. The default is SQLITE_DEFAULT_FILE_PERMISSIONS (normally
  369. ** 0644) as modified by the system umask. If m is not 0, then
  370. ** make the file creation mode be exactly m ignoring the umask.
  371. **
  372. ** The m parameter will be non-zero only when creating -wal, -journal,
  373. ** and -shm files. We want those files to have *exactly* the same
  374. ** permissions as their original database, unadulterated by the umask.
  375. ** In that way, if a database file is -rw-rw-rw or -rw-rw-r-, and a
  376. ** transaction crashes and leaves behind hot journals, then any
  377. ** process that is able to write to the database will also be able to
  378. ** recover the hot journals.
  379. */
  380. static int robust_open(const char *z, int f, mode_t m){
  381. int fd;
  382. mode_t m2 = m ;
  383. while(1){
  384. #if defined(O_CLOEXEC)
  385. fd = osOpen(z,f|O_CLOEXEC,m2);
  386. #else
  387. fd = osOpen(z,f,m2);
  388. #endif
  389. if( fd<0 ){
  390. if( errno==EINTR ) continue;
  391. break;
  392. }
  393. if( fd>=SQLITE_MINIMUM_FILE_DESCRIPTOR ) break;
  394. osClose(fd);
  395. sqlite3_log(SQLITE_WARNING,
  396. "attempt to open \"%s\" as file descriptor %d", z, fd);
  397. fd = -1;
  398. if( osOpen("/000111sql.test111000", f, m)<0 ) break;
  399. }
  400. return fd;
  401. }
  402. /*
  403. ** Open a file descriptor to the directory containing file zFilename.
  404. ** If successful, *pFd is set to the opened file descriptor and
  405. ** SQLITE_OK is returned. If an error occurs, either SQLITE_NOMEM
  406. ** or SQLITE_CANTOPEN is returned and *pFd is set to an undefined
  407. ** value.
  408. **
  409. ** The directory file descriptor is used for only one thing - to
  410. ** fsync() a directory to make sure file creation and deletion events
  411. ** are flushed to disk. Such fsyncs are not needed on newer
  412. ** journaling filesystems, but are required on older filesystems.
  413. **
  414. ** This routine can be overridden using the xSetSysCall interface.
  415. ** The ability to override this routine was added in support of the
  416. ** chromium sandbox. Opening a directory is a security risk (we are
  417. ** told) so making it overrideable allows the chromium sandbox to
  418. ** replace this routine with a harmless no-op. To make this routine
  419. ** a no-op, replace it with a stub that returns SQLITE_OK but leaves
  420. ** *pFd set to a negative number.
  421. **
  422. ** If SQLITE_OK is returned, the caller is responsible for closing
  423. ** the file descriptor *pFd using close().
  424. */
  425. static int openDirectory(const char *zFilename, int *pFd){
  426. int ii;
  427. int fd = -1;
  428. char zDirname[MAX_PATHNAME+1];
  429. sqlite3_snprintf(MAX_PATHNAME, zDirname, "%s", zFilename);
  430. for(ii=(int)strlen(zDirname); ii>1 && zDirname[ii]!='/'; ii--);
  431. if( ii>0 ){
  432. zDirname[ii] = '\0';
  433. fd = robust_open(zDirname, O_RDONLY|O_BINARY, 0);
  434. if( fd>=0 ){
  435. OSTRACE(("OPENDIR %-3d %s\n", fd, zDirname));
  436. }
  437. }
  438. *pFd = fd;
  439. return (fd>=0?SQLITE_OK:rtthreadLogError(SQLITE_CANTOPEN_BKPT, "open", zDirname));
  440. }
  441. /*
  442. ** This is the xSetSystemCall() method of sqlite3_vfs for all of the
  443. ** "win32" VFSes. Return SQLITE_OK opon successfully updating the
  444. ** system call pointer, or SQLITE_NOTFOUND if there is no configurable
  445. ** system call named zName.
  446. */
  447. static int rtthreadSetSystemCall(
  448. sqlite3_vfs *pNotUsed, /* The VFS pointer. Not used */
  449. const char *zName, /* Name of system call to override */
  450. sqlite3_syscall_ptr pNewFunc /* Pointer to new system call value */
  451. ){
  452. unsigned int i;
  453. int rc = SQLITE_NOTFOUND;
  454. UNUSED_PARAMETER(pNotUsed);
  455. if( zName==0 ){
  456. /* If no zName is given, restore all system calls to their default
  457. ** settings and return NULL
  458. */
  459. rc = SQLITE_OK;
  460. for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
  461. if( aSyscall[i].pDefault ){
  462. aSyscall[i].pCurrent = aSyscall[i].pDefault;
  463. }
  464. }
  465. }else{
  466. /* If zName is specified, operate on only the one system call
  467. ** specified.
  468. */
  469. for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
  470. if( strcmp(zName, aSyscall[i].zName)==0 ){
  471. if( aSyscall[i].pDefault==0 ){
  472. aSyscall[i].pDefault = aSyscall[i].pCurrent;
  473. }
  474. rc = SQLITE_OK;
  475. if( pNewFunc==0 ) pNewFunc = aSyscall[i].pDefault;
  476. aSyscall[i].pCurrent = pNewFunc;
  477. break;
  478. }
  479. }
  480. }
  481. return rc;
  482. }
  483. /*
  484. ** Return the value of a system call. Return NULL if zName is not a
  485. ** recognized system call name. NULL is also returned if the system call
  486. ** is currently undefined.
  487. */
  488. static sqlite3_syscall_ptr rtthreadGetSystemCall(
  489. sqlite3_vfs *pNotUsed,
  490. const char *zName
  491. ){
  492. unsigned int i;
  493. UNUSED_PARAMETER(pNotUsed);
  494. for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
  495. if( strcmp(zName, aSyscall[i].zName)==0 ) return aSyscall[i].pCurrent;
  496. }
  497. return 0;
  498. }
  499. /*
  500. ** Return the name of the first system call after zName. If zName==NULL
  501. ** then return the name of the first system call. Return NULL if zName
  502. ** is the last system call or if zName is not the name of a valid
  503. ** system call.
  504. */
  505. static const char* rtthreadNextSystemCall(sqlite3_vfs *p, const char *zName){
  506. int i = -1;
  507. UNUSED_PARAMETER(p);
  508. if( zName ){
  509. for(i=0; i<ArraySize(aSyscall)-1; i++){
  510. if( strcmp(zName, aSyscall[i].zName)==0 ) break;
  511. }
  512. }
  513. for(i++; i<ArraySize(aSyscall); i++){
  514. if( aSyscall[i].pCurrent!=0 ) return aSyscall[i].zName;
  515. }
  516. return 0;
  517. }
  518. /*
  519. ** The following routine suspends the current thread for at least ms
  520. ** milliseconds. This is equivalent to the Win32 Sleep() interface.
  521. */
  522. void sqlite3_rtthread_sleep(int milliseconds){
  523. rt_tick_t sleep_tick;
  524. if (milliseconds <= 0)
  525. return;
  526. sleep_tick = rt_tick_from_millisecond(milliseconds);
  527. osSleep(sleep_tick);
  528. }
  529. /*
  530. ** Helper functions to obtain and relinquish the global mutex. The
  531. ** global mutex is used to protect the unixInodeInfo and
  532. ** vxworksFileId objects used by this file, all of which may be
  533. ** shared by multiple threads.
  534. **
  535. ** Function unixMutexHeld() is used to assert() that the global mutex
  536. ** is held when required. This function is only used as part of assert()
  537. ** statements. e.g.
  538. **
  539. ** unixEnterMutex()
  540. ** assert( unixMutexHeld() );
  541. ** unixEnterLeave()
  542. */
  543. static void rtthreadEnterMutex(void){
  544. sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
  545. }
  546. static void rtthreadLeaveMutex(void){
  547. sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
  548. }
  549. #ifdef SQLITE_DEBUG
  550. static int rtthreadMutexHeld(void) {
  551. return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
  552. }
  553. #endif
  554. #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
  555. /*
  556. ** Helper function for printing out trace information from debugging
  557. ** binaries. This returns the string represetation of the supplied
  558. ** integer lock-type.
  559. */
  560. static const char *azFileLock(int eFileLock){
  561. switch( eFileLock ){
  562. case NO_LOCK: return "NONE";
  563. case SHARED_LOCK: return "SHARED";
  564. case RESERVED_LOCK: return "RESERVED";
  565. case PENDING_LOCK: return "PENDING";
  566. case EXCLUSIVE_LOCK: return "EXCLUSIVE";
  567. }
  568. return "ERROR";
  569. }
  570. #endif
  571. /*
  572. ** This routine translates a standard POSIX errno code into something
  573. ** useful to the clients of the sqlite3 functions. Specifically, it is
  574. ** intended to translate a variety of "try again" errors into SQLITE_BUSY
  575. ** and a variety of "please close the file descriptor NOW" errors into
  576. ** SQLITE_IOERR
  577. **
  578. ** Errors during initialization of locks, or file system support for locks,
  579. ** should handle ENOLCK, ENOTSUP, EOPNOTSUPP separately.
  580. */
  581. static int sqliteErrorFromPosixError(int posixError, int sqliteIOErr) {
  582. switch (posixError) {
  583. #if 0
  584. /* At one point this code was not commented out. In theory, this branch
  585. ** should never be hit, as this function should only be called after
  586. ** a locking-related function (i.e. fcntl()) has returned non-zero with
  587. ** the value of errno as the first argument. Since a system call has failed,
  588. ** errno should be non-zero.
  589. **
  590. ** Despite this, if errno really is zero, we still don't want to return
  591. ** SQLITE_OK. The system call failed, and *some* SQLite error should be
  592. ** propagated back to the caller. Commenting this branch out means errno==0
  593. ** will be handled by the "default:" case below.
  594. */
  595. case 0:
  596. return SQLITE_OK;
  597. #endif
  598. case DFS_STATUS_EAGAIN:
  599. case ETIMEDOUT:
  600. case DFS_STATUS_EBUSY:
  601. case EINTR:
  602. case ENOLCK:
  603. /* random NFS retry error, unless during file system support
  604. * introspection, in which it actually means what it says */
  605. return SQLITE_BUSY;
  606. case EACCES:
  607. /* EACCES is like EAGAIN during locking operations, but not any other time*/
  608. if( (sqliteIOErr == SQLITE_IOERR_LOCK) ||
  609. (sqliteIOErr == SQLITE_IOERR_UNLOCK) ||
  610. (sqliteIOErr == SQLITE_IOERR_RDLOCK) ||
  611. (sqliteIOErr == SQLITE_IOERR_CHECKRESERVEDLOCK) ){
  612. return SQLITE_BUSY;
  613. }
  614. /* else fall through */
  615. case EPERM:
  616. return SQLITE_PERM;
  617. /* EDEADLK is only possible if a call to fcntl(F_SETLKW) is made. And
  618. ** this module never makes such a call. And the code in SQLite itself
  619. ** asserts that SQLITE_IOERR_BLOCKED is never returned. For these reasons
  620. ** this case is also commented out. If the system does set errno to EDEADLK,
  621. ** the default SQLITE_IOERR_XXX code will be returned. */
  622. #if 0
  623. case EDEADLK:
  624. return SQLITE_IOERR_BLOCKED;
  625. #endif
  626. #if EOPNOTSUPP!=ENOTSUP
  627. case EOPNOTSUPP:
  628. /* something went terribly awry, unless during file system support
  629. * introspection, in which it actually means what it says */
  630. #endif
  631. #ifdef ENOTSUP
  632. case ENOTSUP:
  633. /* invalid fd, unless during file system support introspection, in which
  634. * it actually means what it says */
  635. #endif
  636. case DFS_STATUS_EIO:
  637. case DFS_STATUS_EBADF:
  638. case DFS_STATUS_EINVAL:
  639. case ENOTCONN:
  640. case DFS_STATUS_ENODEV:
  641. case DFS_STATUS_ENXIO:
  642. case DFS_STATUS_ENOENT:
  643. #ifdef ESTALE /* ESTALE is not defined on Interix systems */
  644. case ESTALE:
  645. #endif
  646. case DFS_STATUS_ENOSYS:
  647. /* these should force the client to close the file and reconnect */
  648. default:
  649. return sqliteIOErr;
  650. }
  651. }
  652. static int robust_ftruncate(int h, sqlite3_int64 sz){
  653. int rc;
  654. rc = -1;
  655. return rc;
  656. }
  657. /*
  658. ** Close a file descriptor.
  659. **
  660. ** We assume that close() almost always works, since it is only in a
  661. ** very sick application or on a very sick platform that it might fail.
  662. ** If it does fail, simply leak the file descriptor, but do log the
  663. ** error.
  664. **
  665. ** Note that it is not safe to retry close() after EINTR since the
  666. ** file descriptor might have already been reused by another thread.
  667. ** So we don't even try to recover from an EINTR. Just log the error
  668. ** and move on.
  669. */
  670. static void robust_close(rtthreadFile *pFile, int h, int lineno){
  671. if( osClose(h) ){
  672. rtthreadLogErrorAtLine(SQLITE_IOERR_CLOSE, "close",
  673. pFile ? pFile->zPath : 0, lineno);
  674. }
  675. }
  676. /*
  677. ** Check a rtthreadFile that is a database. Verify the following:
  678. **
  679. ** (1) There is exactly one hard link on the file
  680. ** (2) The file is not a symbolic link
  681. ** (3) The file has not been renamed or unlinked
  682. **
  683. ** Issue sqlite3_log(SQLITE_WARNING,...) messages if anything is not right.
  684. */
  685. static void verifyDbFile(rtthreadFile *pFile){
  686. struct stat buf;
  687. int rc;
  688. if( pFile->ctrlFlags & UNIXFILE_WARNED ){
  689. /* One or more of the following warnings have already been issued. Do not
  690. ** repeat them so as not to clutter the error log */
  691. return;
  692. }
  693. rc = osFstat(pFile->h, &buf);
  694. if( rc!=0 ){
  695. sqlite3_log(SQLITE_WARNING, "cannot fstat db file %s", pFile->zPath);
  696. pFile->ctrlFlags |= UNIXFILE_WARNED;
  697. return;
  698. }
  699. #warning " struct \"stat\" has no field \"st_nlink\""
  700. #ifndef RT_USING_SQLITE
  701. if( buf.st_nlink==0 && (pFile->ctrlFlags & UNIXFILE_DELETE)==0 ){
  702. sqlite3_log(SQLITE_WARNING, "file unlinked while open: %s", pFile->zPath);
  703. pFile->ctrlFlags |= UNIXFILE_WARNED;
  704. return;
  705. }
  706. #endif
  707. }
  708. /*
  709. ** This function performs the parts of the "close file" operation
  710. ** common to all locking schemes. It closes the directory and file
  711. ** handles, if they are valid, and sets all fields of the rtthreadFile
  712. ** structure to 0.
  713. **
  714. ** It is *not* necessary to hold the mutex when this routine is called,
  715. ** even on VxWorks. A mutex will be acquired on VxWorks by the
  716. ** vxworksReleaseFileId() routine.
  717. */
  718. static int closeRtthreadFile(sqlite3_file *id){
  719. rtthreadFile *pFile = (rtthreadFile*)id;
  720. if( pFile->h>=0 ){
  721. robust_close(pFile, pFile->h, __LINE__);
  722. pFile->h = -1;
  723. }
  724. OSTRACE(("CLOSE %-3d\n", pFile->h));
  725. OpenCounter(-1);
  726. memset(pFile, 0, sizeof(rtthreadFile));
  727. return SQLITE_OK;
  728. }
  729. /************** End of the posix advisory lock implementation *****************
  730. ******************************************************************************/
  731. /******************************************************************************
  732. ****************************** No-op Locking **********************************
  733. **
  734. ** Of the various locking implementations available, this is by far the
  735. ** simplest: locking is ignored. No attempt is made to lock the database
  736. ** file for reading or writing.
  737. **
  738. ** This locking mode is appropriate for use on read-only databases
  739. ** (ex: databases that are burned into CD-ROM, for example.) It can
  740. ** also be used if the application employs some external mechanism to
  741. ** prevent simultaneous access of the same database by two or more
  742. ** database connections. But there is a serious risk of database
  743. ** corruption if this locking mode is used in situations where multiple
  744. ** database connections are accessing the same database file at the same
  745. ** time and one or more of those connections are writing.
  746. */
  747. static int nolockCheckReservedLock(sqlite3_file *NotUsed, int *pResOut){
  748. UNUSED_PARAMETER(NotUsed);
  749. *pResOut = 0;
  750. return SQLITE_OK;
  751. }
  752. static int nolockLock(sqlite3_file *NotUsed, int NotUsed2){
  753. UNUSED_PARAMETER2(NotUsed, NotUsed2);
  754. return SQLITE_OK;
  755. }
  756. static int nolockUnlock(sqlite3_file *NotUsed, int NotUsed2){
  757. UNUSED_PARAMETER2(NotUsed, NotUsed2);
  758. return SQLITE_OK;
  759. }
  760. /*
  761. ** Close the file.
  762. */
  763. static int nolockClose(sqlite3_file *id) {
  764. return closeRtthreadFile(id);
  765. }
  766. /******************* End of the no-op lock implementation *********************
  767. ******************************************************************************/
  768. /******************************************************************************
  769. ************************* Begin dot-file Locking ******************************
  770. **
  771. ** The dotfile locking implementation uses the existence of separate lock
  772. ** files (really a directory) to control access to the database. This works
  773. ** on just about every filesystem imaginable. But there are serious downsides:
  774. **
  775. ** (1) There is zero concurrency. A single reader blocks all other
  776. ** connections from reading or writing the database.
  777. **
  778. ** (2) An application crash or power loss can leave stale lock files
  779. ** sitting around that need to be cleared manually.
  780. **
  781. ** Nevertheless, a dotlock is an appropriate locking mode for use if no
  782. ** other locking strategy is available.
  783. **
  784. ** Dotfile locking works by creating a subdirectory in the same directory as
  785. ** the database and with the same name but with a ".lock" extension added.
  786. ** The existence of a lock directory implies an EXCLUSIVE lock. All other
  787. ** lock types (SHARED, RESERVED, PENDING) are mapped into EXCLUSIVE.
  788. */
  789. /*
  790. ** The file suffix added to the data base filename in order to create the
  791. ** lock directory.
  792. */
  793. #define DOTLOCK_SUFFIX ".lock"
  794. /*
  795. ** Only set the lastErrno if the error code is a real error and not
  796. ** a normal expected return code of SQLITE_BUSY or SQLITE_OK
  797. */
  798. #define IS_LOCK_ERROR(x) (((x) != SQLITE_OK) && ((x) != SQLITE_BUSY))
  799. /*
  800. ** This routine checks if there is a RESERVED lock held on the specified
  801. ** file by this or any other process. If such a lock is held, set *pResOut
  802. ** to a non-zero value otherwise *pResOut is set to zero. The return value
  803. ** is set to SQLITE_OK unless an I/O error occurs during lock checking.
  804. **
  805. ** In dotfile locking, either a lock exists or it does not. So in this
  806. ** variation of CheckReservedLock(), *pResOut is set to true if any lock
  807. ** is held on the file and false if the file is unlocked.
  808. */
  809. static int dotlockCheckReservedLock(sqlite3_file *id, int *pResOut) {
  810. int rc = SQLITE_OK;
  811. int reserved = 0;
  812. rtthreadFile *pFile = (rtthreadFile*)id;
  813. SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
  814. assert( pFile );
  815. /* Check if a thread in this process holds such a lock */
  816. if( pFile->eFileLock>SHARED_LOCK ){
  817. /* Either this connection or some other connection in the same process
  818. ** holds a lock on the file. No need to check further. */
  819. reserved = 1;
  820. }else{
  821. /* The lock is held if and only if the lockfile exists */
  822. const char *zLockFile = (const char*)pFile->lockingContext;
  823. reserved = 0;
  824. }
  825. OSTRACE(("TEST WR-LOCK %d %d %d (dotlock)\n", pFile->h, rc, reserved));
  826. *pResOut = reserved;
  827. return rc;
  828. }
  829. /*
  830. ** Lock the file with the lock specified by parameter eFileLock - one
  831. ** of the following:
  832. **
  833. ** (1) SHARED_LOCK
  834. ** (2) RESERVED_LOCK
  835. ** (3) PENDING_LOCK
  836. ** (4) EXCLUSIVE_LOCK
  837. **
  838. ** Sometimes when requesting one lock state, additional lock states
  839. ** are inserted in between. The locking might fail on one of the later
  840. ** transitions leaving the lock state different from what it started but
  841. ** still short of its goal. The following chart shows the allowed
  842. ** transitions and the inserted intermediate states:
  843. **
  844. ** UNLOCKED -> SHARED
  845. ** SHARED -> RESERVED
  846. ** SHARED -> (PENDING) -> EXCLUSIVE
  847. ** RESERVED -> (PENDING) -> EXCLUSIVE
  848. ** PENDING -> EXCLUSIVE
  849. **
  850. ** This routine will only increase a lock. Use the sqlite3OsUnlock()
  851. ** routine to lower a locking level.
  852. **
  853. ** With dotfile locking, we really only support state (4): EXCLUSIVE.
  854. ** But we track the other locking levels internally.
  855. */
  856. static int dotlockLock(sqlite3_file *id, int eFileLock) {
  857. rtthreadFile *pFile = (rtthreadFile*)id;
  858. char *zLockFile = (char *)pFile->lockingContext;
  859. int rc = SQLITE_OK;
  860. /* If we have any lock, then the lock file already exists. All we have
  861. ** to do is adjust our internal record of the lock level.
  862. */
  863. if( pFile->eFileLock > NO_LOCK ){
  864. pFile->eFileLock = eFileLock;
  865. return SQLITE_OK;
  866. }
  867. /* grab an exclusive lock */
  868. rc = osMkdir(zLockFile, 0777);
  869. if( rc<0 ){
  870. /* failed to open/create the lock directory */
  871. int tErrno = errno;
  872. if( DFS_STATUS_EEXIST == tErrno ){
  873. rc = SQLITE_BUSY;
  874. } else {
  875. rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
  876. if( IS_LOCK_ERROR(rc) ){
  877. pFile->lastErrno = tErrno;
  878. }
  879. }
  880. return rc;
  881. }
  882. /* got it, set the type and return ok */
  883. pFile->eFileLock = eFileLock;
  884. return rc;
  885. }
  886. /*
  887. ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
  888. ** must be either NO_LOCK or SHARED_LOCK.
  889. **
  890. ** If the locking level of the file descriptor is already at or below
  891. ** the requested locking level, this routine is a no-op.
  892. **
  893. ** When the locking level reaches NO_LOCK, delete the lock file.
  894. */
  895. static int dotlockUnlock(sqlite3_file *id, int eFileLock) {
  896. rtthreadFile *pFile = (rtthreadFile*)id;
  897. char *zLockFile = (char *)pFile->lockingContext;
  898. int rc;
  899. assert( pFile );
  900. OSTRACE(("UNLOCK %d %d was %d tnm=%s (dotlock)\n", pFile->h, eFileLock,
  901. pFile->eFileLock, rt_thread_self()->name ));
  902. assert( eFileLock<=SHARED_LOCK );
  903. /* no-op if possible */
  904. if( pFile->eFileLock==eFileLock ){
  905. return SQLITE_OK;
  906. }
  907. /* To downgrade to shared, simply update our internal notion of the
  908. ** lock state. No need to mess with the file on disk.
  909. */
  910. if( eFileLock==SHARED_LOCK ){
  911. pFile->eFileLock = SHARED_LOCK;
  912. return SQLITE_OK;
  913. }
  914. /* To fully unlock the database, delete the lock file */
  915. assert( eFileLock==NO_LOCK );
  916. rc = osRmdir(zLockFile);
  917. if( rc<0 && errno==DFS_STATUS_ENOTDIR ) rc = osUnlink(zLockFile);
  918. if( rc<0 ){
  919. int tErrno = errno;
  920. rc = 0;
  921. if( DFS_STATUS_ENOENT != tErrno ){
  922. rc = SQLITE_IOERR_UNLOCK;
  923. }
  924. if( IS_LOCK_ERROR(rc) ){
  925. pFile->lastErrno = tErrno;
  926. }
  927. return rc;
  928. }
  929. pFile->eFileLock = NO_LOCK;
  930. return SQLITE_OK;
  931. }
  932. /*
  933. ** Close a file. Make sure the lock has been released before closing.
  934. */
  935. static int dotlockClose(sqlite3_file *id) {
  936. int rc = SQLITE_OK;
  937. if( id ){
  938. rtthreadFile *pFile = (rtthreadFile*)id;
  939. dotlockUnlock(id, NO_LOCK);
  940. sqlite3_free(pFile->lockingContext);
  941. rc = closeRtthreadFile(id);
  942. }
  943. return rc;
  944. }
  945. /****************** End of the dot-file lock implementation *******************
  946. ******************************************************************************/
  947. /******************************************************************************
  948. ************************** Begin flock Locking ********************************
  949. **
  950. ** Use the flock() system call to do file locking.
  951. **
  952. ** flock() locking is like dot-file locking in that the various
  953. ** fine-grain locking levels supported by SQLite are collapsed into
  954. ** a single exclusive lock. In other words, SHARED, RESERVED, and
  955. ** PENDING locks are the same thing as an EXCLUSIVE lock. SQLite
  956. ** still works when you do this, but concurrency is reduced since
  957. ** only a single process can be reading the database at a time.
  958. **
  959. ** Omit this section if SQLITE_ENABLE_LOCKING_STYLE is turned off or if
  960. ** compiling for VXWORKS.
  961. */
  962. #if SQLITE_ENABLE_LOCKING_STYLE
  963. #warning "rtthread file lock not available"
  964. /*
  965. ** Retry flock() calls that fail with EINTR
  966. */
  967. static int robust_flock(int fd, int op){
  968. int rc = 0;
  969. return rc;
  970. }
  971. /*
  972. ** This routine checks if there is a RESERVED lock held on the specified
  973. ** file by this or any other process. If such a lock is held, set *pResOut
  974. ** to a non-zero value otherwise *pResOut is set to zero. The return value
  975. ** is set to SQLITE_OK unless an I/O error occurs during lock checking.
  976. */
  977. static int flockCheckReservedLock(sqlite3_file *id, int *pResOut){
  978. int rc = SQLITE_OK;
  979. int reserved = 0;
  980. rtthreadFile *pFile = (rtthreadFile*)id;
  981. SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
  982. assert( pFile );
  983. /* Check if a thread in this process holds such a lock */
  984. if( pFile->eFileLock>SHARED_LOCK ){
  985. reserved = 1;
  986. }
  987. /* Otherwise see if some other process holds it. */
  988. if( !reserved ){
  989. /* attempt to get the lock */
  990. int lrc = robust_flock(pFile->h, LOCK_EX | LOCK_NB);
  991. if( !lrc ){
  992. /* got the lock, unlock it */
  993. lrc = robust_flock(pFile->h, LOCK_UN);
  994. if ( lrc ) {
  995. int tErrno = errno;
  996. /* unlock failed with an error */
  997. lrc = SQLITE_IOERR_UNLOCK;
  998. if( IS_LOCK_ERROR(lrc) ){
  999. pFile->lastErrno = tErrno;
  1000. rc = lrc;
  1001. }
  1002. }
  1003. } else {
  1004. int tErrno = errno;
  1005. reserved = 1;
  1006. /* someone else might have it reserved */
  1007. lrc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
  1008. if( IS_LOCK_ERROR(lrc) ){
  1009. pFile->lastErrno = tErrno;
  1010. rc = lrc;
  1011. }
  1012. }
  1013. }
  1014. OSTRACE(("TEST WR-LOCK %d %d %d (flock)\n", pFile->h, rc, reserved));
  1015. #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
  1016. if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){
  1017. rc = SQLITE_OK;
  1018. reserved=1;
  1019. }
  1020. #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
  1021. *pResOut = reserved;
  1022. return rc;
  1023. }
  1024. /*
  1025. ** Lock the file with the lock specified by parameter eFileLock - one
  1026. ** of the following:
  1027. **
  1028. ** (1) SHARED_LOCK
  1029. ** (2) RESERVED_LOCK
  1030. ** (3) PENDING_LOCK
  1031. ** (4) EXCLUSIVE_LOCK
  1032. **
  1033. ** Sometimes when requesting one lock state, additional lock states
  1034. ** are inserted in between. The locking might fail on one of the later
  1035. ** transitions leaving the lock state different from what it started but
  1036. ** still short of its goal. The following chart shows the allowed
  1037. ** transitions and the inserted intermediate states:
  1038. **
  1039. ** UNLOCKED -> SHARED
  1040. ** SHARED -> RESERVED
  1041. ** SHARED -> (PENDING) -> EXCLUSIVE
  1042. ** RESERVED -> (PENDING) -> EXCLUSIVE
  1043. ** PENDING -> EXCLUSIVE
  1044. **
  1045. ** flock() only really support EXCLUSIVE locks. We track intermediate
  1046. ** lock states in the sqlite3_file structure, but all locks SHARED or
  1047. ** above are really EXCLUSIVE locks and exclude all other processes from
  1048. ** access the file.
  1049. **
  1050. ** This routine will only increase a lock. Use the sqlite3OsUnlock()
  1051. ** routine to lower a locking level.
  1052. */
  1053. static int flockLock(sqlite3_file *id, int eFileLock) {
  1054. int rc = SQLITE_OK;
  1055. rtthreadFile *pFile = (rtthreadFile*)id;
  1056. assert( pFile );
  1057. /* if we already have a lock, it is exclusive.
  1058. ** Just adjust level and punt on outta here. */
  1059. if (pFile->eFileLock > NO_LOCK) {
  1060. pFile->eFileLock = eFileLock;
  1061. return SQLITE_OK;
  1062. }
  1063. /* grab an exclusive lock */
  1064. if (robust_flock(pFile->h, LOCK_EX | LOCK_NB)) {
  1065. int tErrno = errno;
  1066. /* didn't get, must be busy */
  1067. rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
  1068. if( IS_LOCK_ERROR(rc) ){
  1069. pFile->lastErrno = tErrno;
  1070. }
  1071. } else {
  1072. /* got it, set the type and return ok */
  1073. pFile->eFileLock = eFileLock;
  1074. }
  1075. OSTRACE(("LOCK %d %s %s (flock)\n", pFile->h, azFileLock(eFileLock),
  1076. rc==SQLITE_OK ? "ok" : "failed"));
  1077. #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
  1078. if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){
  1079. rc = SQLITE_BUSY;
  1080. }
  1081. #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
  1082. return rc;
  1083. }
  1084. /*
  1085. ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
  1086. ** must be either NO_LOCK or SHARED_LOCK.
  1087. **
  1088. ** If the locking level of the file descriptor is already at or below
  1089. ** the requested locking level, this routine is a no-op.
  1090. */
  1091. static int flockUnlock(sqlite3_file *id, int eFileLock) {
  1092. rtthreadFile *pFile = (rtthreadFile*)id;
  1093. assert( pFile );
  1094. OSTRACE(("UNLOCK %d %d was %d tnm=%s (flock)\n", pFile->h, eFileLock,
  1095. pFile->eFileLock, rt_thread_self()->name));
  1096. assert( eFileLock<=SHARED_LOCK );
  1097. /* no-op if possible */
  1098. if( pFile->eFileLock==eFileLock ){
  1099. return SQLITE_OK;
  1100. }
  1101. /* shared can just be set because we always have an exclusive */
  1102. if (eFileLock==SHARED_LOCK) {
  1103. pFile->eFileLock = eFileLock;
  1104. return SQLITE_OK;
  1105. }
  1106. /* no, really, unlock. */
  1107. if( robust_flock(pFile->h, LOCK_UN) ){
  1108. #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
  1109. return SQLITE_OK;
  1110. #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
  1111. return SQLITE_IOERR_UNLOCK;
  1112. }else{
  1113. pFile->eFileLock = NO_LOCK;
  1114. return SQLITE_OK;
  1115. }
  1116. }
  1117. /*
  1118. ** Close a file.
  1119. */
  1120. static int flockClose(sqlite3_file *id) {
  1121. int rc = SQLITE_OK;
  1122. if( id ){
  1123. flockUnlock(id, NO_LOCK);
  1124. rc = closeRtthreadFile(id);
  1125. }
  1126. return rc;
  1127. }
  1128. #endif /* SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORK */
  1129. /******************* End of the flock lock implementation *********************
  1130. ******************************************************************************/
  1131. /******************************************************************************
  1132. **************** Non-locking sqlite3_file methods *****************************
  1133. **
  1134. ** The next division contains implementations for all methods of the
  1135. ** sqlite3_file object other than the locking methods. The locking
  1136. ** methods were defined in divisions above (one locking method per
  1137. ** division). Those methods that are common to all locking modes
  1138. ** are gather together into this division.
  1139. */
  1140. /*
  1141. ** Seek to the offset passed as the second argument, then read cnt
  1142. ** bytes into pBuf. Return the number of bytes actually read.
  1143. **
  1144. ** NB: If you define USE_PREAD or USE_PREAD64, then it might also
  1145. ** be necessary to define _XOPEN_SOURCE to be 500. This varies from
  1146. ** one system to another. Since SQLite does not define USE_PREAD
  1147. ** any any form by default, we will not attempt to define _XOPEN_SOURCE.
  1148. ** See tickets #2741 and #2681.
  1149. **
  1150. ** To avoid stomping the errno value on a failed read the lastErrno value
  1151. ** is set before returning.
  1152. */
  1153. static int seekAndRead(rtthreadFile *id, sqlite3_int64 offset, void *pBuf, int cnt){
  1154. int got;
  1155. int prior = 0;
  1156. #if (!defined(USE_PREAD) && !defined(USE_PREAD64))
  1157. i64 newOffset;
  1158. #endif
  1159. TIMER_START;
  1160. assert( cnt==(cnt&0x1ffff) );
  1161. assert( id->h>2 );
  1162. cnt &= 0x1ffff;
  1163. do{
  1164. #if defined(USE_PREAD)
  1165. #error "rtthread pread not support"
  1166. got = osPread(id->h, pBuf, cnt, offset);
  1167. SimulateIOError( got = -1 );
  1168. #elif defined(USE_PREAD64)
  1169. #error "rtthread pread64 not support"
  1170. got = osPread64(id->h, pBuf, cnt, offset);
  1171. SimulateIOError( got = -1 );
  1172. #else
  1173. newOffset = lseek(id->h, offset, SEEK_SET);
  1174. SimulateIOError( newOffset-- );
  1175. if( newOffset!=offset ){
  1176. if( newOffset == -1 ){
  1177. ((rtthreadFile*)id)->lastErrno = errno;
  1178. }else{
  1179. ((rtthreadFile*)id)->lastErrno = 0;
  1180. }
  1181. return -1;
  1182. }
  1183. got = osRead(id->h, pBuf, cnt);
  1184. #endif
  1185. if( got==cnt ) break;
  1186. if( got<0 ){
  1187. if( errno==EINTR ){ got = 1; continue; }
  1188. prior = 0;
  1189. ((rtthreadFile*)id)->lastErrno = errno;
  1190. break;
  1191. }else if( got>0 ){
  1192. cnt -= got;
  1193. offset += got;
  1194. prior += got;
  1195. pBuf = (void*)(got + (char*)pBuf);
  1196. }
  1197. }while( got>0 );
  1198. TIMER_END;
  1199. OSTRACE(("READ %-3d %5d %7lld %llu\n",
  1200. id->h, got+prior, offset-prior, TIMER_ELAPSED));
  1201. return got+prior;
  1202. }
  1203. /*
  1204. ** Read data from a file into a buffer. Return SQLITE_OK if all
  1205. ** bytes were read successfully and SQLITE_IOERR if anything goes
  1206. ** wrong.
  1207. */
  1208. static int rtthreadRead(
  1209. sqlite3_file *id,
  1210. void *pBuf,
  1211. int amt,
  1212. sqlite3_int64 offset
  1213. ){
  1214. rtthreadFile *pFile = (rtthreadFile *)id;
  1215. int got;
  1216. assert( id );
  1217. assert( offset>=0 );
  1218. assert( amt>0 );
  1219. got = seekAndRead(pFile, offset, pBuf, amt);
  1220. if( got==amt ){
  1221. return SQLITE_OK;
  1222. }else if( got<0 ){
  1223. /* lastErrno set by seekAndRead */
  1224. return SQLITE_IOERR_READ;
  1225. }else{
  1226. pFile->lastErrno = 0; /* not a system error */
  1227. /* Unread parts of the buffer must be zero-filled */
  1228. memset(&((char*)pBuf)[got], 0, amt-got);
  1229. return SQLITE_IOERR_SHORT_READ;
  1230. }
  1231. }
  1232. /*
  1233. ** Attempt to seek the file-descriptor passed as the first argument to
  1234. ** absolute offset iOff, then attempt to write nBuf bytes of data from
  1235. ** pBuf to it. If an error occurs, return -1 and set *piErrno. Otherwise,
  1236. ** return the actual number of bytes written (which may be less than
  1237. ** nBuf).
  1238. */
  1239. static int seekAndWriteFd(
  1240. int fd, /* File descriptor to write to */
  1241. i64 iOff, /* File offset to begin writing at */
  1242. const void *pBuf, /* Copy data from this buffer to the file */
  1243. int nBuf, /* Size of buffer pBuf in bytes */
  1244. int *piErrno /* OUT: Error number if error occurs */
  1245. ){
  1246. int rc = 0; /* Value returned by system call */
  1247. assert( nBuf==(nBuf&0x1ffff) );
  1248. assert( fd>2 );
  1249. nBuf &= 0x1ffff;
  1250. TIMER_START;
  1251. #if defined(USE_PREAD)
  1252. do{ rc = osPwrite(fd, pBuf, nBuf, iOff); }while( rc<0 && errno==EINTR );
  1253. #elif defined(USE_PREAD64)
  1254. do{ rc = osPwrite64(fd, pBuf, nBuf, iOff);}while( rc<0 && errno==EINTR);
  1255. #else
  1256. do{
  1257. i64 iSeek = lseek(fd, iOff, SEEK_SET);
  1258. SimulateIOError( iSeek-- );
  1259. if( iSeek!=iOff ){
  1260. if( piErrno ) *piErrno = (iSeek==-1 ? errno : 0);
  1261. return -1;
  1262. }
  1263. rc = osWrite(fd, pBuf, nBuf);
  1264. }while( rc<0 && errno==EINTR );
  1265. #endif
  1266. TIMER_END;
  1267. OSTRACE(("WRITE %-3d %5d %7lld %llu\n", fd, rc, iOff, TIMER_ELAPSED));
  1268. if( rc<0 && piErrno ) *piErrno = errno;
  1269. return rc;
  1270. }
  1271. /*
  1272. ** Seek to the offset in id->offset then read cnt bytes into pBuf.
  1273. ** Return the number of bytes actually read. Update the offset.
  1274. **
  1275. ** To avoid stomping the errno value on a failed write the lastErrno value
  1276. ** is set before returning.
  1277. */
  1278. static int seekAndWrite(rtthreadFile *id, i64 offset, const void *pBuf, int cnt){
  1279. return seekAndWriteFd(id->h, offset, pBuf, cnt, &id->lastErrno);
  1280. }
  1281. /*
  1282. ** Write data from a buffer into a file. Return SQLITE_OK on success
  1283. ** or some other error code on failure.
  1284. */
  1285. static int rtthreadWrite(
  1286. sqlite3_file *id,
  1287. const void *pBuf,
  1288. int amt,
  1289. sqlite3_int64 offset
  1290. ){
  1291. rtthreadFile *pFile = (rtthreadFile*)id;
  1292. int wrote = 0;
  1293. assert( id );
  1294. assert( amt>0 );
  1295. #ifdef SQLITE_DEBUG
  1296. /* If we are doing a normal write to a database file (as opposed to
  1297. ** doing a hot-journal rollback or a write to some file other than a
  1298. ** normal database file) then record the fact that the database
  1299. ** has changed. If the transaction counter is modified, record that
  1300. ** fact too.
  1301. */
  1302. if( pFile->inNormalWrite ){
  1303. pFile->dbUpdate = 1; /* The database has been modified */
  1304. if( offset<=24 && offset+amt>=27 ){
  1305. int rc;
  1306. char oldCntr[4];
  1307. SimulateIOErrorBenign(1);
  1308. rc = seekAndRead(pFile, 24, oldCntr, 4);
  1309. SimulateIOErrorBenign(0);
  1310. if( rc!=4 || memcmp(oldCntr, &((char*)pBuf)[24-offset], 4)!=0 ){
  1311. pFile->transCntrChng = 1; /* The transaction counter has changed */
  1312. }
  1313. }
  1314. }
  1315. #endif
  1316. while( amt>0 && (wrote = seekAndWrite(pFile, offset, pBuf, amt))>0 ){
  1317. amt -= wrote;
  1318. offset += wrote;
  1319. pBuf = &((char*)pBuf)[wrote];
  1320. }
  1321. SimulateIOError(( wrote=(-1), amt=1 ));
  1322. SimulateDiskfullError(( wrote=0, amt=1 ));
  1323. if( amt>0 ){
  1324. if( wrote<0 && pFile->lastErrno!=DFS_STATUS_ENOSPC ){
  1325. /* lastErrno set by seekAndWrite */
  1326. return SQLITE_IOERR_WRITE;
  1327. }else{
  1328. pFile->lastErrno = 0; /* not a system error */
  1329. return SQLITE_FULL;
  1330. }
  1331. }
  1332. return SQLITE_OK;
  1333. }
  1334. #ifdef SQLITE_TEST
  1335. /*
  1336. ** Count the number of fullsyncs and normal syncs. This is used to test
  1337. ** that syncs and fullsyncs are occurring at the right times.
  1338. */
  1339. int sqlite3_sync_count = 0;
  1340. int sqlite3_fullsync_count = 0;
  1341. #endif
  1342. /*
  1343. ** We do not trust systems to provide a working fdatasync(). Some do.
  1344. ** Others do no. To be safe, we will stick with the (slightly slower)
  1345. ** fsync(). If you know that your system does support fdatasync() correctly,
  1346. ** then simply compile with -Dfdatasync=fdatasync
  1347. */
  1348. #if !defined(fdatasync)
  1349. #include "dfs.h"
  1350. #include "dfs_file.h"
  1351. int fdatasync(fd)
  1352. {
  1353. struct dfs_fd *dfs_fd;
  1354. dfs_fd = fd_get(fd);
  1355. return dfs_file_flush(dfs_fd);
  1356. }
  1357. #endif
  1358. /*
  1359. ** Define HAVE_FULLFSYNC to 0 or 1 depending on whether or not
  1360. ** the F_FULLFSYNC macro is defined. F_FULLFSYNC is currently
  1361. ** only available on Mac OS X. But that could change.
  1362. */
  1363. #ifdef F_FULLFSYNC
  1364. # define HAVE_FULLFSYNC 0
  1365. #endif
  1366. /*
  1367. ** The fsync() system call does not work as advertised on many
  1368. ** unix systems. The following procedure is an attempt to make
  1369. ** it work better.
  1370. **
  1371. ** The SQLITE_NO_SYNC macro disables all fsync()s. This is useful
  1372. ** for testing when we want to run through the test suite quickly.
  1373. ** You are strongly advised *not* to deploy with SQLITE_NO_SYNC
  1374. ** enabled, however, since with SQLITE_NO_SYNC enabled, an OS crash
  1375. ** or power failure will likely corrupt the database file.
  1376. **
  1377. ** SQLite sets the dataOnly flag if the size of the file is unchanged.
  1378. ** The idea behind dataOnly is that it should only write the file content
  1379. ** to disk, not the inode. We only set dataOnly if the file size is
  1380. ** unchanged since the file size is part of the inode. However,
  1381. ** Ted Ts'o tells us that fdatasync() will also write the inode if the
  1382. ** file size has changed. The only real difference between fdatasync()
  1383. ** and fsync(), Ted tells us, is that fdatasync() will not flush the
  1384. ** inode if the mtime or owner or other inode attributes have changed.
  1385. ** We only care about the file size, not the other file attributes, so
  1386. ** as far as SQLite is concerned, an fdatasync() is always adequate.
  1387. ** So, we always use fdatasync() if it is available, regardless of
  1388. ** the value of the dataOnly flag.
  1389. */
  1390. static int full_fsync(int fd, int fullSync, int dataOnly){
  1391. int rc;
  1392. /* The following "ifdef/elif/else/" block has the same structure as
  1393. ** the one below. It is replicated here solely to avoid cluttering
  1394. ** up the real code with the UNUSED_PARAMETER() macros.
  1395. */
  1396. #ifdef SQLITE_NO_SYNC
  1397. UNUSED_PARAMETER(fd);
  1398. UNUSED_PARAMETER(fullSync);
  1399. UNUSED_PARAMETER(dataOnly);
  1400. #elif HAVE_FULLFSYNC
  1401. UNUSED_PARAMETER(dataOnly);
  1402. #else
  1403. UNUSED_PARAMETER(fullSync);
  1404. UNUSED_PARAMETER(dataOnly);
  1405. #endif
  1406. /* Record the number of times that we do a normal fsync() and
  1407. ** FULLSYNC. This is used during testing to verify that this procedure
  1408. ** gets called with the correct arguments.
  1409. */
  1410. #ifdef SQLITE_TEST
  1411. if( fullSync ) sqlite3_fullsync_count++;
  1412. sqlite3_sync_count++;
  1413. #endif
  1414. /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
  1415. ** no-op
  1416. */
  1417. #ifdef SQLITE_NO_SYNC
  1418. rc = SQLITE_OK;
  1419. #elif HAVE_FULLFSYNC
  1420. #error "rtthread not support FULLFSYNC"
  1421. #else
  1422. rc = fdatasync(fd);
  1423. #endif /* ifdef SQLITE_NO_SYNC elif HAVE_FULLFSYNC */
  1424. return rc;
  1425. }
  1426. /*
  1427. ** Make sure all writes to a particular file are committed to disk.
  1428. **
  1429. ** If dataOnly==0 then both the file itself and its metadata (file
  1430. ** size, access time, etc) are synced. If dataOnly!=0 then only the
  1431. ** file data is synced.
  1432. **
  1433. ** Under Rtthread, also make sure that the directory entry for the file
  1434. ** has been created by fsync-ing the directory that contains the file.
  1435. ** If we do not do this and we encounter a power failure, the directory
  1436. ** entry for the journal might not exist after we reboot. The next
  1437. ** SQLite to access the file will not know that the journal exists (because
  1438. ** the directory entry for the journal was never created) and the transaction
  1439. ** will not roll back - possibly leading to database corruption.
  1440. */
  1441. static int rtthreadSync(sqlite3_file *id, int flags){
  1442. int rc;
  1443. rtthreadFile *pFile = (rtthreadFile*)id;
  1444. int isDataOnly = (flags&SQLITE_SYNC_DATAONLY);
  1445. int isFullsync = (flags&0x0F)==SQLITE_SYNC_FULL;
  1446. /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */
  1447. assert((flags&0x0F)==SQLITE_SYNC_NORMAL
  1448. || (flags&0x0F)==SQLITE_SYNC_FULL
  1449. );
  1450. /* Rtthread cannot, but some systems may return SQLITE_FULL from here. This
  1451. ** line is to test that doing so does not cause any problems.
  1452. */
  1453. SimulateDiskfullError( return SQLITE_FULL );
  1454. assert( pFile );
  1455. OSTRACE(("SYNC %-3d\n", pFile->h));
  1456. rc = full_fsync(pFile->h, isFullsync, isDataOnly);
  1457. SimulateIOError( rc=1 );
  1458. if( rc ){
  1459. pFile->lastErrno = errno;
  1460. return rtthreadLogError(SQLITE_IOERR_FSYNC, "full_fsync", pFile->zPath);
  1461. }
  1462. /* Also fsync the directory containing the file if the DIRSYNC flag
  1463. ** is set. This is a one-time occurrence. Many systems (examples: AIX)
  1464. ** are unable to fsync a directory, so ignore errors on the fsync.
  1465. */
  1466. if( pFile->ctrlFlags & UNIXFILE_DIRSYNC ){
  1467. int dirfd;
  1468. OSTRACE(("DIRSYNC %s (have_fullfsync=%d fullsync=%d)\n", pFile->zPath,
  1469. HAVE_FULLFSYNC, isFullsync));
  1470. rc = osOpenDirectory(pFile->zPath, &dirfd);
  1471. if( rc==SQLITE_OK && dirfd>=0 ){
  1472. full_fsync(dirfd, 0, 0);
  1473. robust_close(pFile, dirfd, __LINE__);
  1474. }else if( rc==SQLITE_CANTOPEN ){
  1475. rc = SQLITE_OK;
  1476. }
  1477. pFile->ctrlFlags &= ~UNIXFILE_DIRSYNC;
  1478. }
  1479. return rc;
  1480. }
  1481. /*
  1482. ** Truncate an open file to a specified size
  1483. */
  1484. static int rtthreadTruncate(sqlite3_file *id, i64 nByte){
  1485. rtthreadFile *pFile = (rtthreadFile *)id;
  1486. int rc;
  1487. assert( pFile );
  1488. SimulateIOError( return SQLITE_IOERR_TRUNCATE );
  1489. /* If the user has configured a chunk-size for this file, truncate the
  1490. ** file so that it consists of an integer number of chunks (i.e. the
  1491. ** actual file size after the operation may be larger than the requested
  1492. ** size).
  1493. */
  1494. if( pFile->szChunk>0 ){
  1495. nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
  1496. }
  1497. rc = robust_ftruncate(pFile->h, (off_t)nByte);
  1498. if( rc ){
  1499. pFile->lastErrno = errno;
  1500. return rtthreadLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath);
  1501. }else{
  1502. #ifdef SQLITE_DEBUG
  1503. /* If we are doing a normal write to a database file (as opposed to
  1504. ** doing a hot-journal rollback or a write to some file other than a
  1505. ** normal database file) and we truncate the file to zero length,
  1506. ** that effectively updates the change counter. This might happen
  1507. ** when restoring a database using the backup API from a zero-length
  1508. ** source.
  1509. */
  1510. if( pFile->inNormalWrite && nByte==0 ){
  1511. pFile->transCntrChng = 1;
  1512. }
  1513. #endif
  1514. return SQLITE_OK;
  1515. }
  1516. }
  1517. /*
  1518. ** Determine the current size of a file in bytes
  1519. */
  1520. static int rtthreadFileSize(sqlite3_file *id, i64 *pSize){
  1521. int rc;
  1522. struct stat buf;
  1523. assert( id );
  1524. rc = osFstat(((rtthreadFile*)id)->h, &buf);
  1525. SimulateIOError( rc=1 );
  1526. if( rc!=0 ){
  1527. ((rtthreadFile*)id)->lastErrno = errno;
  1528. return SQLITE_IOERR_FSTAT;
  1529. }
  1530. *pSize = buf.st_size;
  1531. /* When opening a zero-size database, the findInodeInfo() procedure
  1532. ** writes a single byte into that file in order to work around a bug
  1533. ** in the OS-X msdos filesystem. In order to avoid problems with upper
  1534. ** layers, we need to report this file size as zero even though it is
  1535. ** really 1. Ticket #3260.
  1536. */
  1537. if( *pSize==1 ) *pSize = 0;
  1538. return SQLITE_OK;
  1539. }
  1540. /*
  1541. ** This function is called to handle the SQLITE_FCNTL_SIZE_HINT
  1542. ** file-control operation. Enlarge the database to nBytes in size
  1543. ** (rounded up to the next chunk-size). If the database is already
  1544. ** nBytes or larger, this routine is a no-op.
  1545. */
  1546. static int fcntlSizeHint(rtthreadFile *pFile, i64 nByte){
  1547. if( pFile->szChunk>0 ){
  1548. i64 nSize; /* Required file size */
  1549. struct stat buf; /* Used to hold return values of fstat() */
  1550. if( osFstat(pFile->h, &buf) ) return SQLITE_IOERR_FSTAT;
  1551. nSize = ((nByte+pFile->szChunk-1) / pFile->szChunk) * pFile->szChunk;
  1552. if( nSize>(i64)buf.st_size ){
  1553. /* If the OS does not have posix_fallocate(), fake it. First use
  1554. ** ftruncate() to set the file size, then write a single byte to
  1555. ** the last byte in each block within the extended region. This
  1556. ** is the same technique used by glibc to implement posix_fallocate()
  1557. ** on systems that do not have a real fallocate() system call.
  1558. */
  1559. int nBlk = 4096; // no blksize in RT-Thread, use 4096. /* File-system block size */
  1560. i64 iWrite; /* Next offset to write to */
  1561. if( robust_ftruncate(pFile->h, nSize) ){
  1562. pFile->lastErrno = errno;
  1563. return rtthreadLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath);
  1564. }
  1565. iWrite = ((buf.st_size + 2*nBlk - 1)/nBlk)*nBlk-1;
  1566. while( iWrite<nSize ){
  1567. int nWrite = seekAndWrite(pFile, iWrite, "", 1);
  1568. if( nWrite!=1 ) return SQLITE_IOERR_WRITE;
  1569. iWrite += nBlk;
  1570. }
  1571. }
  1572. }
  1573. return SQLITE_OK;
  1574. }
  1575. /*
  1576. ** If *pArg is inititially negative then this is a query. Set *pArg to
  1577. ** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set.
  1578. **
  1579. ** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags.
  1580. */
  1581. static void rtthreadModeBit(rtthreadFile *pFile, unsigned char mask, int *pArg){
  1582. if( *pArg<0 ){
  1583. *pArg = (pFile->ctrlFlags & mask)!=0;
  1584. }else if( (*pArg)==0 ){
  1585. pFile->ctrlFlags &= ~mask;
  1586. }else{
  1587. pFile->ctrlFlags |= mask;
  1588. }
  1589. }
  1590. /* Forward declaration */
  1591. static int rtthreadGetTempname(int nBuf, char *zBuf);
  1592. /*
  1593. ** Information and control of an open file handle.
  1594. */
  1595. static int rtthreadFileControl(sqlite3_file *id, int op, void *pArg){
  1596. rtthreadFile *pFile = (rtthreadFile*)id;
  1597. switch( op ){
  1598. case SQLITE_FCNTL_LOCKSTATE: {
  1599. *(int*)pArg = pFile->eFileLock;
  1600. return SQLITE_OK;
  1601. }
  1602. case SQLITE_LAST_ERRNO: {
  1603. *(int*)pArg = pFile->lastErrno;
  1604. return SQLITE_OK;
  1605. }
  1606. case SQLITE_FCNTL_CHUNK_SIZE: {
  1607. pFile->szChunk = *(int *)pArg;
  1608. return SQLITE_OK;
  1609. }
  1610. case SQLITE_FCNTL_SIZE_HINT: {
  1611. int rc;
  1612. SimulateIOErrorBenign(1);
  1613. rc = fcntlSizeHint(pFile, *(i64 *)pArg);
  1614. SimulateIOErrorBenign(0);
  1615. return rc;
  1616. }
  1617. case SQLITE_FCNTL_PERSIST_WAL: {
  1618. rtthreadModeBit(pFile, UNIXFILE_PERSIST_WAL, (int*)pArg);
  1619. return SQLITE_OK;
  1620. }
  1621. case SQLITE_FCNTL_POWERSAFE_OVERWRITE: {
  1622. rtthreadModeBit(pFile, UNIXFILE_PSOW, (int*)pArg);
  1623. return SQLITE_OK;
  1624. }
  1625. case SQLITE_FCNTL_VFSNAME: {
  1626. *(char**)pArg = sqlite3_mprintf("%s", pFile->pVfs->zName);
  1627. return SQLITE_OK;
  1628. }
  1629. case SQLITE_FCNTL_TEMPFILENAME: {
  1630. char *zTFile = sqlite3_malloc( pFile->pVfs->mxPathname );
  1631. if( zTFile ){
  1632. rtthreadGetTempname(pFile->pVfs->mxPathname, zTFile);
  1633. *(char**)pArg = zTFile;
  1634. }
  1635. return SQLITE_OK;
  1636. }
  1637. #ifdef SQLITE_DEBUG
  1638. /* The pager calls this method to signal that it has done
  1639. ** a rollback and that the database is therefore unchanged and
  1640. ** it hence it is OK for the transaction change counter to be
  1641. ** unchanged.
  1642. */
  1643. case SQLITE_FCNTL_DB_UNCHANGED: {
  1644. ((rtthreadFile*)id)->dbUpdate = 0;
  1645. return SQLITE_OK;
  1646. }
  1647. #endif
  1648. }
  1649. return SQLITE_NOTFOUND;
  1650. }
  1651. /*
  1652. ** Return the sector size in bytes of the underlying block device for
  1653. ** the specified file. This is almost always 512 bytes, but may be
  1654. ** larger for some devices.
  1655. **
  1656. ** SQLite code assumes this function cannot fail. It also assumes that
  1657. ** if two files are created in the same file-system directory (i.e.
  1658. ** a database and its journal file) that the sector size will be the
  1659. ** same for both.
  1660. */
  1661. static int rtthreadSectorSize(sqlite3_file *NotUsed){
  1662. UNUSED_PARAMETER(NotUsed);
  1663. return SQLITE_DEFAULT_SECTOR_SIZE;
  1664. }
  1665. /*
  1666. ** Return the device characteristics for the file.
  1667. **
  1668. ** This VFS is set up to return SQLITE_IOCAP_POWERSAFE_OVERWRITE by default.
  1669. ** However, that choice is contraversial since technically the underlying
  1670. ** file system does not always provide powersafe overwrites. (In other
  1671. ** words, after a power-loss event, parts of the file that were never
  1672. ** written might end up being altered.) However, non-PSOW behavior is very,
  1673. ** very rare. And asserting PSOW makes a large reduction in the amount
  1674. ** of required I/O for journaling, since a lot of padding is eliminated.
  1675. ** Hence, while POWERSAFE_OVERWRITE is on by default, there is a file-control
  1676. ** available to turn it off and URI query parameter available to turn it off.
  1677. */
  1678. static int rtthreadDeviceCharacteristics(sqlite3_file *id){
  1679. rtthreadFile *p = (rtthreadFile*)id;
  1680. int rc = 0;
  1681. if( p->ctrlFlags & UNIXFILE_PSOW ){
  1682. rc |= SQLITE_IOCAP_POWERSAFE_OVERWRITE;
  1683. }
  1684. return rc;
  1685. }
  1686. #ifndef SQLITE_OMIT_WAL
  1687. # error "WAL mode requires not support from the rt-thread, compile\
  1688. with SQLITE_OMIT_WAL."
  1689. #else
  1690. # define rtthreadShmMap 0
  1691. # define rtthreadShmLock 0
  1692. # define rtthreadShmBarrier 0
  1693. # define rtthreadShmUnmap 0
  1694. #endif /* #ifndef SQLITE_OMIT_WAL */
  1695. #if SQLITE_MAX_MMAP_SIZE>0
  1696. #error "rtthread not spportt mmap"
  1697. #endif /* SQLITE_MAX_MMAP_SIZE>0 */
  1698. /*
  1699. ** If possible, return a pointer to a mapping of file fd starting at offset
  1700. ** iOff. The mapping must be valid for at least nAmt bytes.
  1701. **
  1702. ** If such a pointer can be obtained, store it in *pp and return SQLITE_OK.
  1703. ** Or, if one cannot but no error occurs, set *pp to 0 and return SQLITE_OK.
  1704. ** Finally, if an error does occur, return an SQLite error code. The final
  1705. ** value of *pp is undefined in this case.
  1706. **
  1707. ** If this function does return a pointer, the caller must eventually
  1708. ** release the reference by calling unixUnfetch().
  1709. */
  1710. static int rtthreadFetch(sqlite3_file *fd, i64 iOff, int nAmt, void **pp){
  1711. *pp = 0;
  1712. return SQLITE_OK;
  1713. }
  1714. /*
  1715. ** If the third argument is non-NULL, then this function releases a
  1716. ** reference obtained by an earlier call to unixFetch(). The second
  1717. ** argument passed to this function must be the same as the corresponding
  1718. ** argument that was passed to the unixFetch() invocation.
  1719. **
  1720. ** Or, if the third argument is NULL, then this function is being called
  1721. ** to inform the VFS layer that, according to POSIX, any existing mapping
  1722. ** may now be invalid and should be unmapped.
  1723. */
  1724. static int rtthreadUnfetch(sqlite3_file *fd, i64 iOff, void *p){
  1725. rtthreadFile *pFd = (rtthreadFile *)fd; /* The underlying database file */
  1726. UNUSED_PARAMETER(iOff);
  1727. return SQLITE_OK;
  1728. }
  1729. /*
  1730. ** Here ends the implementation of all sqlite3_file methods.
  1731. **
  1732. ********************** End sqlite3_file Methods *******************************
  1733. ******************************************************************************/
  1734. /*
  1735. ** This division contains definitions of sqlite3_io_methods objects that
  1736. ** implement various file locking strategies. It also contains definitions
  1737. ** of "finder" functions. A finder-function is used to locate the appropriate
  1738. ** sqlite3_io_methods object for a particular database file. The pAppData
  1739. ** field of the sqlite3_vfs VFS objects are initialized to be pointers to
  1740. ** the correct finder-function for that VFS.
  1741. **
  1742. ** Most finder functions return a pointer to a fixed sqlite3_io_methods
  1743. ** object. The only interesting finder-function is autolockIoFinder, which
  1744. ** looks at the filesystem type and tries to guess the best locking
  1745. ** strategy from that.
  1746. **
  1747. ** For finder-funtion F, two objects are created:
  1748. **
  1749. ** (1) The real finder-function named "FImpt()".
  1750. **
  1751. ** (2) A constant pointer to this function named just "F".
  1752. **
  1753. **
  1754. ** A pointer to the F pointer is used as the pAppData value for VFS
  1755. ** objects. We have to do this instead of letting pAppData point
  1756. ** directly at the finder-function since C90 rules prevent a void*
  1757. ** from be cast into a function pointer.
  1758. **
  1759. **
  1760. ** Each instance of this macro generates two objects:
  1761. **
  1762. ** * A constant sqlite3_io_methods object call METHOD that has locking
  1763. ** methods CLOSE, LOCK, UNLOCK, CKRESLOCK.
  1764. **
  1765. ** * An I/O method finder function called FINDER that returns a pointer
  1766. ** to the METHOD object in the previous bullet.
  1767. */
  1768. #define IOMETHODS(FINDER, METHOD, VERSION, CLOSE, LOCK, UNLOCK, CKLOCK) \
  1769. static const sqlite3_io_methods METHOD = { \
  1770. VERSION, /* iVersion */ \
  1771. CLOSE, /* xClose */ \
  1772. rtthreadRead, /* xRead */ \
  1773. rtthreadWrite, /* xWrite */ \
  1774. rtthreadTruncate, /* xTruncate */ \
  1775. rtthreadSync, /* xSync */ \
  1776. rtthreadFileSize, /* xFileSize */ \
  1777. LOCK, /* xLock */ \
  1778. UNLOCK, /* xUnlock */ \
  1779. CKLOCK, /* xCheckReservedLock */ \
  1780. rtthreadFileControl, /* xFileControl */ \
  1781. rtthreadSectorSize, /* xSectorSize */ \
  1782. rtthreadDeviceCharacteristics, /* xDeviceCapabilities */ \
  1783. rtthreadShmMap, /* xShmMap */ \
  1784. rtthreadShmLock, /* xShmLock */ \
  1785. rtthreadShmBarrier, /* xShmBarrier */ \
  1786. rtthreadShmUnmap, /* xShmUnmap */ \
  1787. rtthreadFetch, /* xFetch */ \
  1788. rtthreadUnfetch, /* xUnfetch */ \
  1789. }; \
  1790. static const sqlite3_io_methods *FINDER##Impl(const char *z, rtthreadFile *p){ \
  1791. UNUSED_PARAMETER(z); UNUSED_PARAMETER(p); \
  1792. return &METHOD; \
  1793. } \
  1794. static const sqlite3_io_methods *(*const FINDER)(const char*,rtthreadFile *p) \
  1795. = FINDER##Impl;
  1796. /*
  1797. ** Here are all of the sqlite3_io_methods objects for each of the
  1798. ** locking strategies. Functions that return pointers to these methods
  1799. ** are also created.
  1800. */
  1801. IOMETHODS(
  1802. nolockIoFinder, /* Finder function name */
  1803. nolockIoMethods, /* sqlite3_io_methods object name */
  1804. 1, /* shared memory is disabled */
  1805. nolockClose, /* xClose method */
  1806. nolockLock, /* xLock method */
  1807. nolockUnlock, /* xUnlock method */
  1808. nolockCheckReservedLock /* xCheckReservedLock method */
  1809. )
  1810. IOMETHODS(
  1811. dotlockIoFinder, /* Finder function name */
  1812. dotlockIoMethods, /* sqlite3_io_methods object name */
  1813. 1, /* shared memory is disabled */
  1814. dotlockClose, /* xClose method */
  1815. dotlockLock, /* xLock method */
  1816. dotlockUnlock, /* xUnlock method */
  1817. dotlockCheckReservedLock /* xCheckReservedLock method */
  1818. )
  1819. #if SQLITE_ENABLE_LOCKING_STYLE
  1820. IOMETHODS(
  1821. flockIoFinder, /* Finder function name */
  1822. flockIoMethods, /* sqlite3_io_methods object name */
  1823. 1, /* shared memory is disabled */
  1824. flockClose, /* xClose method */
  1825. flockLock, /* xLock method */
  1826. flockUnlock, /* xUnlock method */
  1827. flockCheckReservedLock /* xCheckReservedLock method */
  1828. )
  1829. #endif
  1830. /*
  1831. ** An abstract type for a pointer to a IO method finder function:
  1832. */
  1833. typedef const sqlite3_io_methods *(*finder_type)(const char*,rtthreadFile*);
  1834. /****************************************************************************
  1835. **************************** sqlite3_vfs methods ****************************
  1836. **
  1837. ** This division contains the implementation of methods on the
  1838. ** sqlite3_vfs object.
  1839. */
  1840. /*
  1841. ** Initialize the contents of the rtthreadFile structure pointed to by pId.
  1842. */
  1843. static int fillInRtthreadFile(
  1844. sqlite3_vfs *pVfs, /* Pointer to vfs object */
  1845. int h, /* Open file descriptor of file being opened */
  1846. sqlite3_file *pId, /* Write to the rtthreadFile structure here */
  1847. const char *zFilename, /* Name of the file being opened */
  1848. int ctrlFlags /* Zero or more UNIXFILE_* values */
  1849. ){
  1850. const sqlite3_io_methods *pLockingStyle;
  1851. rtthreadFile *pNew = (rtthreadFile *)pId;
  1852. int rc = SQLITE_OK;
  1853. assert( pNew->pInode==NULL );
  1854. /* Usually the path zFilename should not be a relative pathname. The
  1855. ** exception is when opening the proxy "conch" file in builds that
  1856. ** include the special Apple locking styles.
  1857. */
  1858. assert( zFilename==0 || zFilename[0]=='/' );
  1859. /* No locking occurs in temporary files */
  1860. assert( zFilename!=0 || (ctrlFlags & UNIXFILE_NOLOCK)!=0 );
  1861. OSTRACE(("OPEN %-3d %s\n", h, zFilename));
  1862. pNew->h = h;
  1863. pNew->pVfs = pVfs;
  1864. pNew->zPath = zFilename;
  1865. pNew->ctrlFlags = (u8)ctrlFlags;
  1866. if( sqlite3_uri_boolean(((ctrlFlags & UNIXFILE_URI) ? zFilename : 0),
  1867. "psow", SQLITE_POWERSAFE_OVERWRITE) ){
  1868. pNew->ctrlFlags |= UNIXFILE_PSOW;
  1869. }
  1870. if( strcmp(pVfs->zName,"unix-excl")==0 ){
  1871. pNew->ctrlFlags |= UNIXFILE_EXCL;
  1872. }
  1873. if( ctrlFlags & UNIXFILE_NOLOCK ){
  1874. pLockingStyle = &nolockIoMethods;
  1875. }else{
  1876. pLockingStyle = (**(finder_type*)pVfs->pAppData)(zFilename, pNew);
  1877. #if SQLITE_ENABLE_LOCKING_STYLE
  1878. /* Cache zFilename in the locking context (AFP and dotlock override) for
  1879. ** proxyLock activation is possible (remote proxy is based on db name)
  1880. ** zFilename remains valid until file is closed, to support */
  1881. pNew->lockingContext = (void*)zFilename;
  1882. #endif
  1883. }
  1884. if( pLockingStyle == &dotlockIoMethods ){
  1885. /* Dotfile locking uses the file path so it needs to be included in
  1886. ** the dotlockLockingContext
  1887. */
  1888. char *zLockFile;
  1889. int nFilename;
  1890. assert( zFilename!=0 );
  1891. nFilename = (int)strlen(zFilename) + 6;
  1892. zLockFile = (char *)sqlite3_malloc(nFilename);
  1893. if( zLockFile==0 ){
  1894. rc = SQLITE_NOMEM;
  1895. }else{
  1896. sqlite3_snprintf(nFilename, zLockFile, "%s" DOTLOCK_SUFFIX, zFilename);
  1897. }
  1898. pNew->lockingContext = zLockFile;
  1899. }
  1900. pNew->lastErrno = 0;
  1901. if( rc!=SQLITE_OK ){
  1902. if( h>=0 ) robust_close(pNew, h, __LINE__);
  1903. }else{
  1904. pNew->pMethod = pLockingStyle;
  1905. OpenCounter(+1);
  1906. verifyDbFile(pNew);
  1907. }
  1908. return rc;
  1909. }
  1910. /*
  1911. ** Return the name of a directory in which to put temporary files.
  1912. ** If no suitable temporary file directory can be found, return NULL.
  1913. */
  1914. static const char* rtthreadTempFileDir(void){
  1915. static const char *azDirs[] = {
  1916. 0,
  1917. "/sql",
  1918. "/sql/tmp"
  1919. "/tmp",
  1920. 0 /* List terminator */
  1921. };
  1922. unsigned int i;
  1923. struct stat buf;
  1924. const char *zDir = 0;
  1925. azDirs[0] = sqlite3_temp_directory;
  1926. for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); zDir=azDirs[i++]){
  1927. if( zDir==0 ) continue;
  1928. if( osStat(zDir, &buf) ) continue;
  1929. if( !S_ISDIR(buf.st_mode) ) continue;
  1930. break;
  1931. }
  1932. return zDir;
  1933. }
  1934. /*
  1935. ** Create a temporary file name in zBuf. zBuf must be allocated
  1936. ** by the calling process and must be big enough to hold at least
  1937. ** pVfs->mxPathname bytes.
  1938. */
  1939. static int rtthreadGetTempname(int nBuf, char *zBuf){
  1940. static const unsigned char zChars[] =
  1941. "abcdefghijklmnopqrstuvwxyz"
  1942. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  1943. "0123456789";
  1944. unsigned int i, j;
  1945. const char *zDir;
  1946. /* It's odd to simulate an io-error here, but really this is just
  1947. ** using the io-error infrastructure to test that SQLite handles this
  1948. ** function failing.
  1949. */
  1950. SimulateIOError( return SQLITE_IOERR );
  1951. zDir = rtthreadTempFileDir();
  1952. if( zDir==0 ) zDir = ".";
  1953. /* Check that the output buffer is large enough for the temporary file
  1954. ** name. If it is not, return SQLITE_ERROR.
  1955. */
  1956. if( (strlen(zDir) + strlen(SQLITE_TEMP_FILE_PREFIX) + 18) >= (size_t)nBuf ){
  1957. return SQLITE_ERROR;
  1958. }
  1959. do{
  1960. sqlite3_snprintf(nBuf-18, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX, zDir);
  1961. j = (int)strlen(zBuf);
  1962. sqlite3_randomness(15, &zBuf[j]);
  1963. for(i=0; i<15; i++, j++){
  1964. zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
  1965. }
  1966. zBuf[j] = 0;
  1967. zBuf[j+1] = 0;
  1968. }while( osAccess(zBuf,0)==0 );
  1969. return SQLITE_OK;
  1970. }
  1971. /*
  1972. ** Open the file zPath.
  1973. **
  1974. ** Previously, the SQLite OS layer used three functions in place of this
  1975. ** one:
  1976. **
  1977. ** sqlite3OsOpenReadWrite();
  1978. ** sqlite3OsOpenReadOnly();
  1979. ** sqlite3OsOpenExclusive();
  1980. **
  1981. ** These calls correspond to the following combinations of flags:
  1982. **
  1983. ** ReadWrite() -> (READWRITE | CREATE)
  1984. ** ReadOnly() -> (READONLY)
  1985. ** OpenExclusive() -> (READWRITE | CREATE | EXCLUSIVE)
  1986. **
  1987. ** The old OpenExclusive() accepted a boolean argument - "delFlag". If
  1988. ** true, the file was configured to be automatically deleted when the
  1989. ** file handle closed. To achieve the same effect using this new
  1990. ** interface, add the DELETEONCLOSE flag to those specified above for
  1991. ** OpenExclusive().
  1992. */
  1993. static int rtthreadOpen(
  1994. sqlite3_vfs *pVfs, /* The VFS for which this is the xOpen method */
  1995. const char *zPath, /* Pathname of file to be opened */
  1996. sqlite3_file *pFile, /* The file descriptor to be filled in */
  1997. int flags, /* Input flags to control the opening */
  1998. int *pOutFlags /* Output flags returned to SQLite core */
  1999. ){
  2000. rtthreadFile *p = (rtthreadFile *)pFile;
  2001. int fd = -1; /* File descriptor returned by open() */
  2002. int openFlags = 0; /* Flags to pass to open() */
  2003. int eType = flags&0xFFFFFF00; /* Type of file to open */
  2004. int noLock; /* True to omit locking primitives */
  2005. int rc = SQLITE_OK; /* Function Return Code */
  2006. int ctrlFlags = 0; /* UNIXFILE_* flags */
  2007. int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE);
  2008. int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE);
  2009. int isCreate = (flags & SQLITE_OPEN_CREATE);
  2010. int isReadonly = (flags & SQLITE_OPEN_READONLY);
  2011. int isReadWrite = (flags & SQLITE_OPEN_READWRITE);
  2012. #if SQLITE_ENABLE_LOCKING_STYLE
  2013. int isAutoProxy = (flags & SQLITE_OPEN_AUTOPROXY);
  2014. #endif
  2015. /* If creating a master or main-file journal, this function will open
  2016. ** a file-descriptor on the directory too. The first time unixSync()
  2017. ** is called the directory file descriptor will be fsync()ed and close()d.
  2018. */
  2019. int syncDir = (isCreate && (
  2020. eType==SQLITE_OPEN_MASTER_JOURNAL
  2021. || eType==SQLITE_OPEN_MAIN_JOURNAL
  2022. || eType==SQLITE_OPEN_WAL
  2023. ));
  2024. /* If argument zPath is a NULL pointer, this function is required to open
  2025. ** a temporary file. Use this buffer to store the file name in.
  2026. */
  2027. char zTmpname[MAX_PATHNAME+2];
  2028. const char *zName = zPath;
  2029. /* Check the following statements are true:
  2030. **
  2031. ** (a) Exactly one of the READWRITE and READONLY flags must be set, and
  2032. ** (b) if CREATE is set, then READWRITE must also be set, and
  2033. ** (c) if EXCLUSIVE is set, then CREATE must also be set.
  2034. ** (d) if DELETEONCLOSE is set, then CREATE must also be set.
  2035. */
  2036. assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly));
  2037. assert(isCreate==0 || isReadWrite);
  2038. assert(isExclusive==0 || isCreate);
  2039. assert(isDelete==0 || isCreate);
  2040. /* The main DB, main journal, WAL file and master journal are never
  2041. ** automatically deleted. Nor are they ever temporary files. */
  2042. assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB );
  2043. assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL );
  2044. assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MASTER_JOURNAL );
  2045. assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL );
  2046. /* Assert that the upper layer has set one of the "file-type" flags. */
  2047. assert( eType==SQLITE_OPEN_MAIN_DB || eType==SQLITE_OPEN_TEMP_DB
  2048. || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL
  2049. || eType==SQLITE_OPEN_SUBJOURNAL || eType==SQLITE_OPEN_MASTER_JOURNAL
  2050. || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL
  2051. );
  2052. memset(p, 0, sizeof(rtthreadFile));
  2053. if( !zName ){
  2054. /* If zName is NULL, the upper layer is requesting a temp file. */
  2055. assert(isDelete && !syncDir);
  2056. rc = rtthreadGetTempname(MAX_PATHNAME+2, zTmpname);
  2057. if( rc!=SQLITE_OK ){
  2058. return rc;
  2059. }
  2060. zName = zTmpname;
  2061. /* Generated temporary filenames are always double-zero terminated
  2062. ** for use by sqlite3_uri_parameter(). */
  2063. assert( zName[strlen(zName)+1]==0 );
  2064. }
  2065. /* Determine the value of the flags parameter passed to POSIX function
  2066. ** open(). These must be calculated even if open() is not called, as
  2067. ** they may be stored as part of the file handle and used by the
  2068. ** 'conch file' locking functions later on. */
  2069. if( isReadonly ) openFlags |= O_RDONLY;
  2070. if( isReadWrite ) openFlags |= O_RDWR;
  2071. if( isCreate ) openFlags |= O_CREAT;
  2072. if( isExclusive ) openFlags |= (O_EXCL|0/*O_NOFOLLOW8*/);
  2073. openFlags |= (0/*O_LARGEFILE*/|O_BINARY);
  2074. if( fd<0 ){
  2075. mode_t openMode = 0; /* Permissions to create file with */
  2076. fd = robust_open(zName, openFlags, openMode);
  2077. OSTRACE(("OPENX %-3d %s 0%o\n", fd, zName, openFlags));
  2078. if( fd<0 && errno!=DFS_STATUS_EISDIR && isReadWrite && !isExclusive ){
  2079. /* Failed to open the file for read/write access. Try read-only. */
  2080. flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
  2081. openFlags &= ~(O_RDWR|O_CREAT);
  2082. flags |= SQLITE_OPEN_READONLY;
  2083. openFlags |= O_RDONLY;
  2084. isReadonly = 1;
  2085. fd = robust_open(zName, openFlags, openMode);
  2086. }
  2087. if( fd<0 ){
  2088. rc = rtthreadLogError(SQLITE_CANTOPEN_BKPT, "open", zName);
  2089. goto open_finished;
  2090. }
  2091. }
  2092. assert( fd>=0 );
  2093. if( pOutFlags ){
  2094. *pOutFlags = flags;
  2095. }
  2096. if( isDelete ){
  2097. osUnlink(zName);
  2098. }
  2099. #if SQLITE_ENABLE_LOCKING_STYLE
  2100. else{
  2101. p->openFlags = openFlags;
  2102. }
  2103. #endif
  2104. noLock = eType!=SQLITE_OPEN_MAIN_DB;
  2105. /* Set up appropriate ctrlFlags */
  2106. if( isDelete ) ctrlFlags |= UNIXFILE_DELETE;
  2107. if( isReadonly ) ctrlFlags |= UNIXFILE_RDONLY;
  2108. if( noLock ) ctrlFlags |= UNIXFILE_NOLOCK;
  2109. if( syncDir ) ctrlFlags |= UNIXFILE_DIRSYNC;
  2110. if( flags & SQLITE_OPEN_URI ) ctrlFlags |= UNIXFILE_URI;
  2111. rc = fillInRtthreadFile(pVfs, fd, pFile, zPath, ctrlFlags);
  2112. open_finished:
  2113. return rc;
  2114. }
  2115. /*
  2116. ** Delete the file at zPath. If the dirSync argument is true, fsync()
  2117. ** the directory after deleting the file.
  2118. */
  2119. static int rtthreadDelete(
  2120. sqlite3_vfs *NotUsed, /* VFS containing this as the xDelete method */
  2121. const char *zPath, /* Name of file to be deleted */
  2122. int dirSync /* If true, fsync() directory after deleting file */
  2123. ){
  2124. int rc = SQLITE_OK;
  2125. UNUSED_PARAMETER(NotUsed);
  2126. SimulateIOError(return SQLITE_IOERR_DELETE);
  2127. if( osUnlink(zPath)==(-1) ){
  2128. if( errno==DFS_STATUS_ENOENT ){
  2129. rc = SQLITE_IOERR_DELETE_NOENT;
  2130. }else{
  2131. rc = rtthreadLogError(SQLITE_IOERR_DELETE, "unlink", zPath);
  2132. }
  2133. return rc;
  2134. }
  2135. #ifndef SQLITE_DISABLE_DIRSYNC
  2136. if( (dirSync & 1)!=0 ){
  2137. int fd;
  2138. rc = osOpenDirectory(zPath, &fd);
  2139. if( rc==SQLITE_OK ){
  2140. robust_close(0, fd, __LINE__);
  2141. }else if( rc==SQLITE_CANTOPEN ){
  2142. rc = SQLITE_OK;
  2143. }
  2144. }
  2145. #endif
  2146. return rc;
  2147. }
  2148. /*
  2149. ** Test the existence of or access permissions of file zPath. The
  2150. ** test performed depends on the value of flags:
  2151. **
  2152. ** SQLITE_ACCESS_EXISTS: Return 1 if the file exists
  2153. ** SQLITE_ACCESS_READWRITE: Return 1 if the file is read and writable.
  2154. ** SQLITE_ACCESS_READONLY: Return 1 if the file is readable.
  2155. **
  2156. ** Otherwise return 0.
  2157. */
  2158. #ifndef F_OK
  2159. # define F_OK 0
  2160. #endif
  2161. #ifndef R_OK
  2162. # define R_OK 4
  2163. #endif
  2164. #ifndef W_OK
  2165. # define W_OK 2
  2166. #endif
  2167. static int rtthreadAccess(
  2168. sqlite3_vfs *NotUsed, /* The VFS containing this xAccess method */
  2169. const char *zPath, /* Path of the file to examine */
  2170. int flags, /* What do we want to learn about the zPath file? */
  2171. int *pResOut /* Write result boolean here */
  2172. ){
  2173. int amode = 0;
  2174. UNUSED_PARAMETER(NotUsed);
  2175. SimulateIOError( return SQLITE_IOERR_ACCESS; );
  2176. switch( flags ){
  2177. case SQLITE_ACCESS_EXISTS:
  2178. amode = F_OK;
  2179. break;
  2180. case SQLITE_ACCESS_READWRITE:
  2181. amode = W_OK|R_OK;
  2182. break;
  2183. case SQLITE_ACCESS_READ:
  2184. amode = R_OK;
  2185. break;
  2186. default:
  2187. assert(!"Invalid flags argument");
  2188. }
  2189. *pResOut = (osAccess(zPath, amode)==0);
  2190. if( flags==SQLITE_ACCESS_EXISTS && *pResOut ){
  2191. struct stat buf;
  2192. if( 0==osStat(zPath, &buf) && buf.st_size==0 ){
  2193. *pResOut = 0;
  2194. }
  2195. }
  2196. return SQLITE_OK;
  2197. }
  2198. /*
  2199. ** Turn a relative pathname into a full pathname. The relative path
  2200. ** is stored as a nul-terminated string in the buffer pointed to by
  2201. ** zPath.
  2202. **
  2203. ** zOut points to a buffer of at least sqlite3_vfs.mxPathname bytes
  2204. ** (in this case, MAX_PATHNAME bytes). The full-path is written to
  2205. ** this buffer before returning.
  2206. */
  2207. static int rtthreadFullPathname(
  2208. sqlite3_vfs *pVfs, /* Pointer to vfs object */
  2209. const char *zPath, /* Possibly relative input path */
  2210. int nOut, /* Size of output buffer in bytes */
  2211. char *zOut /* Output buffer */
  2212. ){
  2213. /* It's odd to simulate an io-error here, but really this is just
  2214. ** using the io-error infrastructure to test that SQLite handles this
  2215. ** function failing. This function could fail if, for example, the
  2216. ** current working directory has been unlinked.
  2217. */
  2218. SimulateIOError( return SQLITE_ERROR );
  2219. assert( pVfs->mxPathname==MAX_PATHNAME );
  2220. UNUSED_PARAMETER(pVfs);
  2221. zOut[nOut-1] = '\0';
  2222. if( zPath[0]=='/' ){
  2223. sqlite3_snprintf(nOut, zOut, "%s", zPath);
  2224. }else{
  2225. int nCwd;
  2226. if( osGetcwd(zOut, nOut-1)==0 ){
  2227. return rtthreadLogError(SQLITE_CANTOPEN_BKPT, "getcwd", zPath);
  2228. }
  2229. nCwd = (int)strlen(zOut);
  2230. sqlite3_snprintf(nOut-nCwd, &zOut[nCwd], "/%s", zPath);
  2231. }
  2232. return SQLITE_OK;
  2233. }
  2234. #ifndef SQLITE_OMIT_LOAD_EXTENSION
  2235. # error "rtthread not support load extension, compile with SQLITE_OMIT_WAL."
  2236. #else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
  2237. #define rtthreadDlOpen 0
  2238. #define rtthreadDlError 0
  2239. #define rtthreadDlSym 0
  2240. #define rtthreadDlClose 0
  2241. #endif
  2242. /*
  2243. ** Write nBuf bytes of random data to the supplied buffer zBuf.
  2244. */
  2245. static int rtthreadRandomness(sqlite3_vfs *NotUsed, int nBuf, char *zBuf){
  2246. UNUSED_PARAMETER(NotUsed);
  2247. assert((size_t)nBuf>=(sizeof(time_t)+sizeof(int)));
  2248. /* We have to initialize zBuf to prevent valgrind from reporting
  2249. ** errors. The reports issued by valgrind are incorrect - we would
  2250. ** prefer that the randomness be increased by making use of the
  2251. ** uninitialized space in zBuf - but valgrind errors tend to worry
  2252. ** some users. Rather than argue, it seems easier just to initialize
  2253. ** the whole array and silence valgrind, even if that means less randomness
  2254. ** in the random seed.
  2255. **
  2256. ** When testing, initializing zBuf[] to zero is all we do. That means
  2257. ** that we always use the same random number sequence. This makes the
  2258. ** tests repeatable.
  2259. */
  2260. memset(zBuf, 0, nBuf);
  2261. {
  2262. int i;
  2263. char tick8, tick16;
  2264. tick8 = (char)rt_tick_get();
  2265. tick16 = (char)(rt_tick_get() >> 8);
  2266. for (i=0; i<nBuf; i++)
  2267. {
  2268. zBuf[i] = (char)(i ^ tick8 ^ tick16);
  2269. tick8 = zBuf[i];
  2270. tick16 = ~(tick8 ^ tick16);
  2271. }
  2272. }
  2273. return nBuf;
  2274. }
  2275. /*
  2276. ** Sleep for a little while. Return the amount of time slept.
  2277. ** The argument is the number of microseconds we want to sleep.
  2278. ** The return value is the number of microseconds of sleep actually
  2279. ** requested from the underlying operating system, a number which
  2280. ** might be greater than or equal to the argument, but not less
  2281. ** than the argument.
  2282. */
  2283. static int rtthreadSleep(sqlite3_vfs *NotUsed, int microseconds){
  2284. int seconds = (microseconds+999999)/1000000;
  2285. osSleep(seconds * 1000);
  2286. UNUSED_PARAMETER(NotUsed);
  2287. return seconds*1000000;
  2288. }
  2289. /*
  2290. ** The following variable, if set to a non-zero value, is interpreted as
  2291. ** the number of seconds since 1970 and is used to set the result of
  2292. ** sqlite3OsCurrentTime() during testing.
  2293. */
  2294. #ifdef SQLITE_TEST
  2295. int sqlite3_current_time = 0; /* Fake system time in seconds since 1970. */
  2296. #endif
  2297. #ifndef NO_GETTOD
  2298. #define NO_GETTOD 1
  2299. #endif
  2300. /*
  2301. ** Find the current time (in Universal Coordinated Time). Write into *piNow
  2302. ** the current time and date as a Julian Day number times 86_400_000. In
  2303. ** other words, write into *piNow the number of milliseconds since the Julian
  2304. ** epoch of noon in Greenwich on November 24, 4714 B.C according to the
  2305. ** proleptic Gregorian calendar.
  2306. **
  2307. ** On success, return SQLITE_OK. Return SQLITE_ERROR if the time and date
  2308. ** cannot be found.
  2309. */
  2310. static int rtthreadCurrentTimeInt64(sqlite3_vfs *NotUsed, sqlite3_int64 *piNow){
  2311. static const sqlite3_int64 rtthreadEpoch = 24405875*(sqlite3_int64)8640000;
  2312. int rc = SQLITE_OK;
  2313. #if defined(NO_GETTOD)
  2314. time_t t;
  2315. time(&t);
  2316. *piNow = ((sqlite3_int64)t)*1000 + rtthreadEpoch;
  2317. #else
  2318. struct timeval sNow;
  2319. if( gettimeofday(&sNow, 0)==0 ){
  2320. *piNow = rtthreadEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_usec/1000;
  2321. }else{
  2322. rc = SQLITE_ERROR;
  2323. }
  2324. #endif
  2325. #ifdef SQLITE_TEST
  2326. if( sqlite3_current_time ){
  2327. *piNow = 1000*(sqlite3_int64)sqlite3_current_time + rtthreadEpoch;
  2328. }
  2329. #endif
  2330. UNUSED_PARAMETER(NotUsed);
  2331. return rc;
  2332. }
  2333. /*
  2334. ** Find the current time (in Universal Coordinated Time). Write the
  2335. ** current time and date as a Julian Day number into *prNow and
  2336. ** return 0. Return 1 if the time and date cannot be found.
  2337. */
  2338. static int rtthreadCurrentTime(sqlite3_vfs *NotUsed, double *prNow){
  2339. sqlite3_int64 i = 0;
  2340. int rc;
  2341. UNUSED_PARAMETER(NotUsed);
  2342. rc = rtthreadCurrentTimeInt64(0, &i);
  2343. *prNow = i/86400000.0;
  2344. return rc;
  2345. }
  2346. /*
  2347. ** We added the xGetLastError() method with the intention of providing
  2348. ** better low-level error messages when operating-system problems come up
  2349. ** during SQLite operation. But so far, none of that has been implemented
  2350. ** in the core. So this routine is never called. For now, it is merely
  2351. ** a place-holder.
  2352. */
  2353. static int rtthreadGetLastError(sqlite3_vfs *NotUsed, int NotUsed2, char *NotUsed3){
  2354. UNUSED_PARAMETER(NotUsed);
  2355. UNUSED_PARAMETER(NotUsed2);
  2356. UNUSED_PARAMETER(NotUsed3);
  2357. return 0;
  2358. }
  2359. /*
  2360. ************************ End of sqlite3_vfs methods ***************************
  2361. ******************************************************************************/
  2362. /*
  2363. ** Initialize the operating system interface.
  2364. **
  2365. ** This routine registers all VFS implementations for unix-like operating
  2366. ** systems. This routine, and the sqlite3_os_end() routine that follows,
  2367. ** should be the only routines in this file that are visible from other
  2368. ** files.
  2369. **
  2370. ** This routine is called once during SQLite initialization and by a
  2371. ** single thread. The memory allocation and mutex subsystems have not
  2372. ** necessarily been initialized when this routine is called, and so they
  2373. ** should not be used.
  2374. */
  2375. /*
  2376. ** The following macro defines an initializer for an sqlite3_vfs object.
  2377. ** The name of the VFS is NAME. The pAppData is a pointer to a pointer
  2378. ** to the "finder" function. (pAppData is a pointer to a pointer because
  2379. ** silly C90 rules prohibit a void* from being cast to a function pointer
  2380. ** and so we have to go through the intermediate pointer to avoid problems
  2381. ** when compiling with -pedantic-errors on GCC.)
  2382. **
  2383. ** The FINDER parameter to this macro is the name of the pointer to the
  2384. ** finder-function. The finder-function returns a pointer to the
  2385. ** sqlite_io_methods object that implements the desired locking
  2386. ** behaviors. See the division above that contains the IOMETHODS
  2387. ** macro for addition information on finder-functions.
  2388. **
  2389. ** Most finders simply return a pointer to a fixed sqlite3_io_methods
  2390. ** object. But the "autolockIoFinder" available on MacOSX does a little
  2391. ** more than that; it looks at the filesystem type that hosts the
  2392. ** database file and tries to choose an locking method appropriate for
  2393. ** that filesystem time.
  2394. */
  2395. #define UNIXVFS(VFSNAME, FINDER) { \
  2396. 3, /* iVersion */ \
  2397. sizeof(rtthreadFile), /* szOsFile */ \
  2398. MAX_PATHNAME, /* mxPathname */ \
  2399. 0, /* pNext */ \
  2400. VFSNAME, /* zName */ \
  2401. (void*)&FINDER, /* pAppData */ \
  2402. rtthreadOpen, /* xOpen */ \
  2403. rtthreadDelete, /* xDelete */ \
  2404. rtthreadAccess, /* xAccess */ \
  2405. rtthreadFullPathname, /* xFullPathname */ \
  2406. rtthreadDlOpen, /* xDlOpen */ \
  2407. rtthreadDlError, /* xDlError */ \
  2408. rtthreadDlSym, /* xDlSym */ \
  2409. rtthreadDlClose, /* xDlClose */ \
  2410. rtthreadRandomness, /* xRandomness */ \
  2411. rtthreadSleep, /* xSleep */ \
  2412. rtthreadCurrentTime, /* xCurrentTime */ \
  2413. rtthreadGetLastError, /* xGetLastError */ \
  2414. rtthreadCurrentTimeInt64, /* xCurrentTimeInt64 */ \
  2415. rtthreadSetSystemCall, /* xSetSystemCall */ \
  2416. rtthreadGetSystemCall, /* xGetSystemCall */ \
  2417. rtthreadNextSystemCall, /* xNextSystemCall */ \
  2418. }
  2419. int sqlite3_os_init(void){
  2420. /*
  2421. ** All default VFSes for unix are contained in the following array.
  2422. **
  2423. ** Note that the sqlite3_vfs.pNext field of the VFS object is modified
  2424. ** by the SQLite core when the VFS is registered. So the following
  2425. ** array cannot be const.
  2426. */
  2427. static sqlite3_vfs aVfs[] = {
  2428. UNIXVFS("unix-none", nolockIoFinder ),
  2429. UNIXVFS("unix-dotfile", dotlockIoFinder ),
  2430. #if SQLITE_ENABLE_LOCKING_STYLE
  2431. UNIXVFS("unix-flock", flockIoFinder ),
  2432. #endif
  2433. };
  2434. unsigned int i; /* Loop counter */
  2435. /* Double-check that the aSyscall[] array has been constructed
  2436. ** correctly. See ticket [bb3a86e890c8e96ab] */
  2437. assert( ArraySize(aSyscall)==24 );
  2438. /* Register all VFSes defined in the aVfs[] array */
  2439. for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){
  2440. sqlite3_vfs_register(&aVfs[i], i==0);
  2441. }
  2442. return SQLITE_OK;
  2443. }
  2444. /*
  2445. ** Shutdown the operating system interface.
  2446. **
  2447. ** Some operating systems might need to do some cleanup in this routine,
  2448. ** to release dynamically allocated objects. But not on unix.
  2449. ** This routine is a no-op for unix.
  2450. */
  2451. int sqlite3_os_end(void){
  2452. return SQLITE_OK;
  2453. }
  2454. #endif /* SQLITE_OS_RTTHREAD */