os_os2.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013
  1. /*
  2. ** 2006 Feb 14
  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 OS/2.
  14. */
  15. #include "sqliteInt.h"
  16. #if OS_OS2
  17. /*
  18. ** A Note About Memory Allocation:
  19. **
  20. ** This driver uses malloc()/free() directly rather than going through
  21. ** the SQLite-wrappers sqlite3_malloc()/sqlite3_free(). Those wrappers
  22. ** are designed for use on embedded systems where memory is scarce and
  23. ** malloc failures happen frequently. OS/2 does not typically run on
  24. ** embedded systems, and when it does the developers normally have bigger
  25. ** problems to worry about than running out of memory. So there is not
  26. ** a compelling need to use the wrappers.
  27. **
  28. ** But there is a good reason to not use the wrappers. If we use the
  29. ** wrappers then we will get simulated malloc() failures within this
  30. ** driver. And that causes all kinds of problems for our tests. We
  31. ** could enhance SQLite to deal with simulated malloc failures within
  32. ** the OS driver, but the code to deal with those failure would not
  33. ** be exercised on Linux (which does not need to malloc() in the driver)
  34. ** and so we would have difficulty writing coverage tests for that
  35. ** code. Better to leave the code out, we think.
  36. **
  37. ** The point of this discussion is as follows: When creating a new
  38. ** OS layer for an embedded system, if you use this file as an example,
  39. ** avoid the use of malloc()/free(). Those routines work ok on OS/2
  40. ** desktops but not so well in embedded systems.
  41. */
  42. /*
  43. ** Macros used to determine whether or not to use threads.
  44. */
  45. #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE
  46. # define SQLITE_OS2_THREADS 1
  47. #endif
  48. /*
  49. ** Include code that is common to all os_*.c files
  50. */
  51. #include "os_common.h"
  52. /*
  53. ** The os2File structure is subclass of sqlite3_file specific for the OS/2
  54. ** protability layer.
  55. */
  56. typedef struct os2File os2File;
  57. struct os2File {
  58. const sqlite3_io_methods *pMethod; /* Always the first entry */
  59. HFILE h; /* Handle for accessing the file */
  60. int delOnClose; /* True if file is to be deleted on close */
  61. char* pathToDel; /* Name of file to delete on close */
  62. unsigned char locktype; /* Type of lock currently held on this file */
  63. };
  64. /*****************************************************************************
  65. ** The next group of routines implement the I/O methods specified
  66. ** by the sqlite3_io_methods object.
  67. ******************************************************************************/
  68. /*
  69. ** Close a file.
  70. */
  71. int os2Close( sqlite3_file *id ){
  72. APIRET rc = NO_ERROR;
  73. os2File *pFile;
  74. if( id && (pFile = (os2File*)id) != 0 ){
  75. OSTRACE2( "CLOSE %d\n", pFile->h );
  76. rc = DosClose( pFile->h );
  77. pFile->locktype = NO_LOCK;
  78. if( pFile->delOnClose != 0 ){
  79. rc = DosForceDelete( (PSZ)pFile->pathToDel );
  80. }
  81. if( pFile->pathToDel ){
  82. free( pFile->pathToDel );
  83. }
  84. id = 0;
  85. OpenCounter( -1 );
  86. }
  87. return rc == NO_ERROR ? SQLITE_OK : SQLITE_IOERR;
  88. }
  89. /*
  90. ** Read data from a file into a buffer. Return SQLITE_OK if all
  91. ** bytes were read successfully and SQLITE_IOERR if anything goes
  92. ** wrong.
  93. */
  94. int os2Read(
  95. sqlite3_file *id, /* File to read from */
  96. void *pBuf, /* Write content into this buffer */
  97. int amt, /* Number of bytes to read */
  98. sqlite3_int64 offset /* Begin reading at this offset */
  99. ){
  100. ULONG fileLocation = 0L;
  101. ULONG got;
  102. os2File *pFile = (os2File*)id;
  103. assert( id!=0 );
  104. SimulateIOError( return SQLITE_IOERR_READ );
  105. OSTRACE3( "READ %d lock=%d\n", pFile->h, pFile->locktype );
  106. if( DosSetFilePtr(pFile->h, offset, FILE_BEGIN, &fileLocation) != NO_ERROR ){
  107. return SQLITE_IOERR;
  108. }
  109. if( DosRead( pFile->h, pBuf, amt, &got ) != NO_ERROR ){
  110. return SQLITE_IOERR_READ;
  111. }
  112. if( got == (ULONG)amt )
  113. return SQLITE_OK;
  114. else {
  115. memset(&((char*)pBuf)[got], 0, amt-got);
  116. return SQLITE_IOERR_SHORT_READ;
  117. }
  118. }
  119. /*
  120. ** Write data from a buffer into a file. Return SQLITE_OK on success
  121. ** or some other error code on failure.
  122. */
  123. int os2Write(
  124. sqlite3_file *id, /* File to write into */
  125. const void *pBuf, /* The bytes to be written */
  126. int amt, /* Number of bytes to write */
  127. sqlite3_int64 offset /* Offset into the file to begin writing at */
  128. ){
  129. ULONG fileLocation = 0L;
  130. APIRET rc = NO_ERROR;
  131. ULONG wrote;
  132. os2File *pFile = (os2File*)id;
  133. assert( id!=0 );
  134. SimulateIOError( return SQLITE_IOERR_WRITE );
  135. SimulateDiskfullError( return SQLITE_FULL );
  136. OSTRACE3( "WRITE %d lock=%d\n", pFile->h, pFile->locktype );
  137. if( DosSetFilePtr(pFile->h, offset, FILE_BEGIN, &fileLocation) != NO_ERROR ){
  138. return SQLITE_IOERR;
  139. }
  140. assert( amt>0 );
  141. while( amt > 0 &&
  142. (rc = DosWrite( pFile->h, (PVOID)pBuf, amt, &wrote )) &&
  143. wrote > 0
  144. ){
  145. amt -= wrote;
  146. pBuf = &((char*)pBuf)[wrote];
  147. }
  148. return ( rc != NO_ERROR || amt > (int)wrote ) ? SQLITE_FULL : SQLITE_OK;
  149. }
  150. /*
  151. ** Truncate an open file to a specified size
  152. */
  153. int os2Truncate( sqlite3_file *id, i64 nByte ){
  154. APIRET rc = NO_ERROR;
  155. ULONG filePosition = 0L;
  156. os2File *pFile = (os2File*)id;
  157. OSTRACE3( "TRUNCATE %d %lld\n", pFile->h, nByte );
  158. SimulateIOError( return SQLITE_IOERR_TRUNCATE );
  159. rc = DosSetFilePtr( pFile->h, nByte, FILE_BEGIN, &filePosition );
  160. if( rc != NO_ERROR ){
  161. return SQLITE_IOERR;
  162. }
  163. rc = DosSetFilePtr( pFile->h, 0L, FILE_END, &filePosition );
  164. return rc == NO_ERROR ? SQLITE_OK : SQLITE_IOERR;
  165. }
  166. #ifdef SQLITE_TEST
  167. /*
  168. ** Count the number of fullsyncs and normal syncs. This is used to test
  169. ** that syncs and fullsyncs are occuring at the right times.
  170. */
  171. int sqlite3_sync_count = 0;
  172. int sqlite3_fullsync_count = 0;
  173. #endif
  174. /*
  175. ** Make sure all writes to a particular file are committed to disk.
  176. */
  177. int os2Sync( sqlite3_file *id, int flags ){
  178. os2File *pFile = (os2File*)id;
  179. OSTRACE3( "SYNC %d lock=%d\n", pFile->h, pFile->locktype );
  180. #ifdef SQLITE_TEST
  181. if( flags & SQLITE_SYNC_FULL){
  182. sqlite3_fullsync_count++;
  183. }
  184. sqlite3_sync_count++;
  185. #endif
  186. return DosResetBuffer( pFile->h ) == NO_ERROR ? SQLITE_OK : SQLITE_IOERR;
  187. }
  188. /*
  189. ** Determine the current size of a file in bytes
  190. */
  191. int os2FileSize( sqlite3_file *id, sqlite3_int64 *pSize ){
  192. APIRET rc = NO_ERROR;
  193. FILESTATUS3 fsts3FileInfo;
  194. memset(&fsts3FileInfo, 0, sizeof(fsts3FileInfo));
  195. assert( id!=0 );
  196. SimulateIOError( return SQLITE_IOERR );
  197. rc = DosQueryFileInfo( ((os2File*)id)->h, FIL_STANDARD, &fsts3FileInfo, sizeof(FILESTATUS3) );
  198. if( rc == NO_ERROR ){
  199. *pSize = fsts3FileInfo.cbFile;
  200. return SQLITE_OK;
  201. }else{
  202. return SQLITE_IOERR;
  203. }
  204. }
  205. /*
  206. ** Acquire a reader lock.
  207. */
  208. static int getReadLock( os2File *pFile ){
  209. FILELOCK LockArea,
  210. UnlockArea;
  211. APIRET res;
  212. memset(&LockArea, 0, sizeof(LockArea));
  213. memset(&UnlockArea, 0, sizeof(UnlockArea));
  214. LockArea.lOffset = SHARED_FIRST;
  215. LockArea.lRange = SHARED_SIZE;
  216. UnlockArea.lOffset = 0L;
  217. UnlockArea.lRange = 0L;
  218. res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, 2000L, 1L );
  219. OSTRACE3( "GETREADLOCK %d res=%d\n", pFile->h, res );
  220. return res;
  221. }
  222. /*
  223. ** Undo a readlock
  224. */
  225. static int unlockReadLock( os2File *id ){
  226. FILELOCK LockArea,
  227. UnlockArea;
  228. APIRET res;
  229. memset(&LockArea, 0, sizeof(LockArea));
  230. memset(&UnlockArea, 0, sizeof(UnlockArea));
  231. LockArea.lOffset = 0L;
  232. LockArea.lRange = 0L;
  233. UnlockArea.lOffset = SHARED_FIRST;
  234. UnlockArea.lRange = SHARED_SIZE;
  235. res = DosSetFileLocks( id->h, &UnlockArea, &LockArea, 2000L, 1L );
  236. OSTRACE3( "UNLOCK-READLOCK file handle=%d res=%d?\n", id->h, res );
  237. return res;
  238. }
  239. /*
  240. ** Lock the file with the lock specified by parameter locktype - one
  241. ** of the following:
  242. **
  243. ** (1) SHARED_LOCK
  244. ** (2) RESERVED_LOCK
  245. ** (3) PENDING_LOCK
  246. ** (4) EXCLUSIVE_LOCK
  247. **
  248. ** Sometimes when requesting one lock state, additional lock states
  249. ** are inserted in between. The locking might fail on one of the later
  250. ** transitions leaving the lock state different from what it started but
  251. ** still short of its goal. The following chart shows the allowed
  252. ** transitions and the inserted intermediate states:
  253. **
  254. ** UNLOCKED -> SHARED
  255. ** SHARED -> RESERVED
  256. ** SHARED -> (PENDING) -> EXCLUSIVE
  257. ** RESERVED -> (PENDING) -> EXCLUSIVE
  258. ** PENDING -> EXCLUSIVE
  259. **
  260. ** This routine will only increase a lock. The os2Unlock() routine
  261. ** erases all locks at once and returns us immediately to locking level 0.
  262. ** It is not possible to lower the locking level one step at a time. You
  263. ** must go straight to locking level 0.
  264. */
  265. int os2Lock( sqlite3_file *id, int locktype ){
  266. int rc = SQLITE_OK; /* Return code from subroutines */
  267. APIRET res = NO_ERROR; /* Result of an OS/2 lock call */
  268. int newLocktype; /* Set pFile->locktype to this value before exiting */
  269. int gotPendingLock = 0;/* True if we acquired a PENDING lock this time */
  270. FILELOCK LockArea,
  271. UnlockArea;
  272. os2File *pFile = (os2File*)id;
  273. memset(&LockArea, 0, sizeof(LockArea));
  274. memset(&UnlockArea, 0, sizeof(UnlockArea));
  275. assert( pFile!=0 );
  276. OSTRACE4( "LOCK %d %d was %d\n", pFile->h, locktype, pFile->locktype );
  277. /* If there is already a lock of this type or more restrictive on the
  278. ** os2File, do nothing. Don't use the end_lock: exit path, as
  279. ** sqlite3OsEnterMutex() hasn't been called yet.
  280. */
  281. if( pFile->locktype>=locktype ){
  282. OSTRACE3( "LOCK %d %d ok (already held)\n", pFile->h, locktype );
  283. return SQLITE_OK;
  284. }
  285. /* Make sure the locking sequence is correct
  286. */
  287. assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
  288. assert( locktype!=PENDING_LOCK );
  289. assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
  290. /* Lock the PENDING_LOCK byte if we need to acquire a PENDING lock or
  291. ** a SHARED lock. If we are acquiring a SHARED lock, the acquisition of
  292. ** the PENDING_LOCK byte is temporary.
  293. */
  294. newLocktype = pFile->locktype;
  295. if( pFile->locktype==NO_LOCK
  296. || (locktype==EXCLUSIVE_LOCK && pFile->locktype==RESERVED_LOCK)
  297. ){
  298. int cnt = 3;
  299. LockArea.lOffset = PENDING_BYTE;
  300. LockArea.lRange = 1L;
  301. UnlockArea.lOffset = 0L;
  302. UnlockArea.lRange = 0L;
  303. while( cnt-->0 && ( res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, 2000L, 1L) )
  304. != NO_ERROR
  305. ){
  306. /* Try 3 times to get the pending lock. The pending lock might be
  307. ** held by another reader process who will release it momentarily.
  308. */
  309. OSTRACE2( "LOCK could not get a PENDING lock. cnt=%d\n", cnt );
  310. DosSleep(1);
  311. }
  312. if( res == NO_ERROR){
  313. gotPendingLock = 1;
  314. OSTRACE3( "LOCK %d pending lock boolean set. res=%d\n", pFile->h, res );
  315. }
  316. }
  317. /* Acquire a shared lock
  318. */
  319. if( locktype==SHARED_LOCK && res == NO_ERROR ){
  320. assert( pFile->locktype==NO_LOCK );
  321. res = getReadLock(pFile);
  322. if( res == NO_ERROR ){
  323. newLocktype = SHARED_LOCK;
  324. }
  325. OSTRACE3( "LOCK %d acquire shared lock. res=%d\n", pFile->h, res );
  326. }
  327. /* Acquire a RESERVED lock
  328. */
  329. if( locktype==RESERVED_LOCK && res == NO_ERROR ){
  330. assert( pFile->locktype==SHARED_LOCK );
  331. LockArea.lOffset = RESERVED_BYTE;
  332. LockArea.lRange = 1L;
  333. UnlockArea.lOffset = 0L;
  334. UnlockArea.lRange = 0L;
  335. res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, 2000L, 1L );
  336. if( res == NO_ERROR ){
  337. newLocktype = RESERVED_LOCK;
  338. }
  339. OSTRACE3( "LOCK %d acquire reserved lock. res=%d\n", pFile->h, res );
  340. }
  341. /* Acquire a PENDING lock
  342. */
  343. if( locktype==EXCLUSIVE_LOCK && res == NO_ERROR ){
  344. newLocktype = PENDING_LOCK;
  345. gotPendingLock = 0;
  346. OSTRACE2( "LOCK %d acquire pending lock. pending lock boolean unset.\n", pFile->h );
  347. }
  348. /* Acquire an EXCLUSIVE lock
  349. */
  350. if( locktype==EXCLUSIVE_LOCK && res == NO_ERROR ){
  351. assert( pFile->locktype>=SHARED_LOCK );
  352. res = unlockReadLock(pFile);
  353. OSTRACE2( "unreadlock = %d\n", res );
  354. LockArea.lOffset = SHARED_FIRST;
  355. LockArea.lRange = SHARED_SIZE;
  356. UnlockArea.lOffset = 0L;
  357. UnlockArea.lRange = 0L;
  358. res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, 2000L, 1L );
  359. if( res == NO_ERROR ){
  360. newLocktype = EXCLUSIVE_LOCK;
  361. }else{
  362. OSTRACE2( "OS/2 error-code = %d\n", res );
  363. getReadLock(pFile);
  364. }
  365. OSTRACE3( "LOCK %d acquire exclusive lock. res=%d\n", pFile->h, res );
  366. }
  367. /* If we are holding a PENDING lock that ought to be released, then
  368. ** release it now.
  369. */
  370. if( gotPendingLock && locktype==SHARED_LOCK ){
  371. int r;
  372. LockArea.lOffset = 0L;
  373. LockArea.lRange = 0L;
  374. UnlockArea.lOffset = PENDING_BYTE;
  375. UnlockArea.lRange = 1L;
  376. r = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, 2000L, 1L );
  377. OSTRACE3( "LOCK %d unlocking pending/is shared. r=%d\n", pFile->h, r );
  378. }
  379. /* Update the state of the lock has held in the file descriptor then
  380. ** return the appropriate result code.
  381. */
  382. if( res == NO_ERROR ){
  383. rc = SQLITE_OK;
  384. }else{
  385. OSTRACE4( "LOCK FAILED %d trying for %d but got %d\n", pFile->h,
  386. locktype, newLocktype );
  387. rc = SQLITE_BUSY;
  388. }
  389. pFile->locktype = newLocktype;
  390. OSTRACE3( "LOCK %d now %d\n", pFile->h, pFile->locktype );
  391. return rc;
  392. }
  393. /*
  394. ** This routine checks if there is a RESERVED lock held on the specified
  395. ** file by this or any other process. If such a lock is held, return
  396. ** non-zero, otherwise zero.
  397. */
  398. int os2CheckReservedLock( sqlite3_file *id ){
  399. APIRET rc = NO_ERROR;
  400. os2File *pFile = (os2File*)id;
  401. assert( pFile!=0 );
  402. if( pFile->locktype>=RESERVED_LOCK ){
  403. rc = 1;
  404. OSTRACE3( "TEST WR-LOCK %d %d (local)\n", pFile->h, rc );
  405. }else{
  406. FILELOCK LockArea,
  407. UnlockArea;
  408. memset(&LockArea, 0, sizeof(LockArea));
  409. memset(&UnlockArea, 0, sizeof(UnlockArea));
  410. LockArea.lOffset = RESERVED_BYTE;
  411. LockArea.lRange = 1L;
  412. UnlockArea.lOffset = 0L;
  413. UnlockArea.lRange = 0L;
  414. rc = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, 2000L, 1L );
  415. OSTRACE3( "TEST WR-LOCK %d lock reserved byte rc=%d\n", pFile->h, rc );
  416. if( rc == NO_ERROR ){
  417. int r;
  418. LockArea.lOffset = 0L;
  419. LockArea.lRange = 0L;
  420. UnlockArea.lOffset = RESERVED_BYTE;
  421. UnlockArea.lRange = 1L;
  422. r = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, 2000L, 1L );
  423. OSTRACE3( "TEST WR-LOCK %d unlock reserved byte r=%d\n", pFile->h, r );
  424. }
  425. OSTRACE3( "TEST WR-LOCK %d %d (remote)\n", pFile->h, rc );
  426. }
  427. return rc;
  428. }
  429. /*
  430. ** Lower the locking level on file descriptor id to locktype. locktype
  431. ** must be either NO_LOCK or SHARED_LOCK.
  432. **
  433. ** If the locking level of the file descriptor is already at or below
  434. ** the requested locking level, this routine is a no-op.
  435. **
  436. ** It is not possible for this routine to fail if the second argument
  437. ** is NO_LOCK. If the second argument is SHARED_LOCK then this routine
  438. ** might return SQLITE_IOERR;
  439. */
  440. int os2Unlock( sqlite3_file *id, int locktype ){
  441. int type;
  442. os2File *pFile = (os2File*)id;
  443. APIRET rc = SQLITE_OK;
  444. APIRET res = NO_ERROR;
  445. FILELOCK LockArea,
  446. UnlockArea;
  447. memset(&LockArea, 0, sizeof(LockArea));
  448. memset(&UnlockArea, 0, sizeof(UnlockArea));
  449. assert( pFile!=0 );
  450. assert( locktype<=SHARED_LOCK );
  451. OSTRACE4( "UNLOCK %d to %d was %d\n", pFile->h, locktype, pFile->locktype );
  452. type = pFile->locktype;
  453. if( type>=EXCLUSIVE_LOCK ){
  454. LockArea.lOffset = 0L;
  455. LockArea.lRange = 0L;
  456. UnlockArea.lOffset = SHARED_FIRST;
  457. UnlockArea.lRange = SHARED_SIZE;
  458. res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, 2000L, 1L );
  459. OSTRACE3( "UNLOCK %d exclusive lock res=%d\n", pFile->h, res );
  460. if( locktype==SHARED_LOCK && getReadLock(pFile) != NO_ERROR ){
  461. /* This should never happen. We should always be able to
  462. ** reacquire the read lock */
  463. OSTRACE3( "UNLOCK %d to %d getReadLock() failed\n", pFile->h, locktype );
  464. rc = SQLITE_IOERR_UNLOCK;
  465. }
  466. }
  467. if( type>=RESERVED_LOCK ){
  468. LockArea.lOffset = 0L;
  469. LockArea.lRange = 0L;
  470. UnlockArea.lOffset = RESERVED_BYTE;
  471. UnlockArea.lRange = 1L;
  472. res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, 2000L, 1L );
  473. OSTRACE3( "UNLOCK %d reserved res=%d\n", pFile->h, res );
  474. }
  475. if( locktype==NO_LOCK && type>=SHARED_LOCK ){
  476. res = unlockReadLock(pFile);
  477. OSTRACE5( "UNLOCK %d is %d want %d res=%d\n", pFile->h, type, locktype, res );
  478. }
  479. if( type>=PENDING_LOCK ){
  480. LockArea.lOffset = 0L;
  481. LockArea.lRange = 0L;
  482. UnlockArea.lOffset = PENDING_BYTE;
  483. UnlockArea.lRange = 1L;
  484. res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, 2000L, 1L );
  485. OSTRACE3( "UNLOCK %d pending res=%d\n", pFile->h, res );
  486. }
  487. pFile->locktype = locktype;
  488. OSTRACE3( "UNLOCK %d now %d\n", pFile->h, pFile->locktype );
  489. return rc;
  490. }
  491. /*
  492. ** Control and query of the open file handle.
  493. */
  494. static int os2FileControl(sqlite3_file *id, int op, void *pArg){
  495. switch( op ){
  496. case SQLITE_FCNTL_LOCKSTATE: {
  497. *(int*)pArg = ((os2File*)id)->locktype;
  498. OSTRACE3( "FCNTL_LOCKSTATE %d lock=%d\n", ((os2File*)id)->h, ((os2File*)id)->locktype );
  499. return SQLITE_OK;
  500. }
  501. }
  502. return SQLITE_ERROR;
  503. }
  504. /*
  505. ** Return the sector size in bytes of the underlying block device for
  506. ** the specified file. This is almost always 512 bytes, but may be
  507. ** larger for some devices.
  508. **
  509. ** SQLite code assumes this function cannot fail. It also assumes that
  510. ** if two files are created in the same file-system directory (i.e.
  511. ** a database and its journal file) that the sector size will be the
  512. ** same for both.
  513. */
  514. static int os2SectorSize(sqlite3_file *id){
  515. return SQLITE_DEFAULT_SECTOR_SIZE;
  516. }
  517. /*
  518. ** Return a vector of device characteristics.
  519. */
  520. static int os2DeviceCharacteristics(sqlite3_file *id){
  521. return 0;
  522. }
  523. /*
  524. ** This vector defines all the methods that can operate on an
  525. ** sqlite3_file for os2.
  526. */
  527. static const sqlite3_io_methods os2IoMethod = {
  528. 1, /* iVersion */
  529. os2Close,
  530. os2Read,
  531. os2Write,
  532. os2Truncate,
  533. os2Sync,
  534. os2FileSize,
  535. os2Lock,
  536. os2Unlock,
  537. os2CheckReservedLock,
  538. os2FileControl,
  539. os2SectorSize,
  540. os2DeviceCharacteristics
  541. };
  542. /***************************************************************************
  543. ** Here ends the I/O methods that form the sqlite3_io_methods object.
  544. **
  545. ** The next block of code implements the VFS methods.
  546. ****************************************************************************/
  547. /*
  548. ** Open a file.
  549. */
  550. static int os2Open(
  551. sqlite3_vfs *pVfs, /* Not used */
  552. const char *zName, /* Name of the file */
  553. sqlite3_file *id, /* Write the SQLite file handle here */
  554. int flags, /* Open mode flags */
  555. int *pOutFlags /* Status return flags */
  556. ){
  557. HFILE h;
  558. ULONG ulFileAttribute = 0;
  559. ULONG ulOpenFlags = 0;
  560. ULONG ulOpenMode = 0;
  561. os2File *pFile = (os2File*)id;
  562. APIRET rc = NO_ERROR;
  563. ULONG ulAction;
  564. memset(pFile, 0, sizeof(*pFile));
  565. OSTRACE2( "OPEN want %d\n", flags );
  566. //ulOpenMode = flags & SQLITE_OPEN_READWRITE ? OPEN_ACCESS_READWRITE : OPEN_ACCESS_READONLY;
  567. if( flags & SQLITE_OPEN_READWRITE ){
  568. ulOpenMode |= OPEN_ACCESS_READWRITE;
  569. OSTRACE1( "OPEN read/write\n" );
  570. }else{
  571. ulOpenMode |= OPEN_ACCESS_READONLY;
  572. OSTRACE1( "OPEN read only\n" );
  573. }
  574. //ulOpenFlags = flags & SQLITE_OPEN_CREATE ? OPEN_ACTION_CREATE_IF_NEW : OPEN_ACTION_FAIL_IF_NEW;
  575. if( flags & SQLITE_OPEN_CREATE ){
  576. ulOpenFlags |= OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_CREATE_IF_NEW;
  577. OSTRACE1( "OPEN open new/create\n" );
  578. }else{
  579. ulOpenFlags |= OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_FAIL_IF_NEW;
  580. OSTRACE1( "OPEN open existing\n" );
  581. }
  582. //ulOpenMode |= flags & SQLITE_OPEN_MAIN_DB ? OPEN_SHARE_DENYNONE : OPEN_SHARE_DENYWRITE;
  583. if( flags & SQLITE_OPEN_MAIN_DB ){
  584. ulOpenMode |= OPEN_SHARE_DENYNONE;
  585. OSTRACE1( "OPEN share read/write\n" );
  586. }else{
  587. ulOpenMode |= OPEN_SHARE_DENYWRITE;
  588. OSTRACE1( "OPEN share read only\n" );
  589. }
  590. if( flags & (SQLITE_OPEN_TEMP_DB | SQLITE_OPEN_TEMP_JOURNAL
  591. | SQLITE_OPEN_SUBJOURNAL) ){
  592. //ulFileAttribute = FILE_HIDDEN; //for debugging, we want to make sure it is deleted
  593. ulFileAttribute = FILE_NORMAL;
  594. pFile->delOnClose = 1;
  595. pFile->pathToDel = (char*)malloc(sizeof(char) * pVfs->mxPathname);
  596. sqlite3OsFullPathname(pVfs, zName, pVfs->mxPathname, pFile->pathToDel);
  597. OSTRACE1( "OPEN hidden/delete on close file attributes\n" );
  598. }else{
  599. ulFileAttribute = FILE_ARCHIVED | FILE_NORMAL;
  600. pFile->delOnClose = 0;
  601. pFile->pathToDel = NULL;
  602. OSTRACE1( "OPEN normal file attribute\n" );
  603. }
  604. //ulOpenMode |= flags & (SQLITE_OPEN_MAIN_DB | SQLITE_OPEN_TEMP_DB) ?
  605. // OPEN_FLAGS_RANDOM : OPEN_FLAGS_SEQUENTIAL;
  606. if( flags & (SQLITE_OPEN_MAIN_DB | SQLITE_OPEN_TEMP_DB) ){
  607. ulOpenMode |= OPEN_FLAGS_RANDOM;
  608. OSTRACE1( "OPEN random access\n" );
  609. }else{
  610. ulOpenMode |= OPEN_FLAGS_SEQUENTIAL;
  611. OSTRACE1( "OPEN sequential access\n" );
  612. }
  613. ulOpenMode |= OPEN_FLAGS_FAIL_ON_ERROR;
  614. rc = DosOpen( (PSZ)zName,
  615. &h,
  616. &ulAction,
  617. 0L,
  618. ulFileAttribute,
  619. ulOpenFlags,
  620. ulOpenMode,
  621. (PEAOP2)NULL );
  622. if( rc != NO_ERROR ){
  623. OSTRACE7( "OPEN Invalid handle rc=%d: zName=%s, ulAction=%#lx, ulAttr=%#lx, ulFlags=%#lx, ulMode=%#lx\n",
  624. rc, zName, ulAction, ulFileAttribute, ulOpenFlags, ulOpenMode );
  625. if( flags & SQLITE_OPEN_READWRITE ){
  626. OSTRACE2( "OPEN %d Invalid handle\n", ((flags | SQLITE_OPEN_READONLY) & ~SQLITE_OPEN_READWRITE) );
  627. return os2Open( 0, zName, id,
  628. ((flags | SQLITE_OPEN_READONLY) & ~SQLITE_OPEN_READWRITE),
  629. pOutFlags );
  630. }else{
  631. return SQLITE_CANTOPEN;
  632. }
  633. }
  634. if( pOutFlags ){
  635. *pOutFlags = flags & SQLITE_OPEN_READWRITE ? SQLITE_OPEN_READWRITE : SQLITE_OPEN_READONLY;
  636. }
  637. pFile->pMethod = &os2IoMethod;
  638. pFile->h = h;
  639. OpenCounter(+1);
  640. OSTRACE3( "OPEN %d pOutFlags=%d\n", pFile->h, pOutFlags );
  641. return SQLITE_OK;
  642. }
  643. /*
  644. ** Delete the named file.
  645. */
  646. int os2Delete(
  647. sqlite3_vfs *pVfs, /* Not used on os2 */
  648. const char *zFilename, /* Name of file to delete */
  649. int syncDir /* Not used on os2 */
  650. ){
  651. APIRET rc = NO_ERROR;
  652. SimulateIOError(return SQLITE_IOERR_DELETE);
  653. rc = DosDelete( (PSZ)zFilename );
  654. OSTRACE2( "DELETE \"%s\"\n", zFilename );
  655. return rc == NO_ERROR ? SQLITE_OK : SQLITE_IOERR;
  656. }
  657. /*
  658. ** Check the existance and status of a file.
  659. */
  660. static int os2Access(
  661. sqlite3_vfs *pVfs, /* Not used on os2 */
  662. const char *zFilename, /* Name of file to check */
  663. int flags /* Type of test to make on this file */
  664. ){
  665. FILESTATUS3 fsts3ConfigInfo;
  666. APIRET rc = NO_ERROR;
  667. memset(&fsts3ConfigInfo, 0, sizeof(fsts3ConfigInfo));
  668. rc = DosQueryPathInfo( (PSZ)zFilename, FIL_STANDARD,
  669. &fsts3ConfigInfo, sizeof(FILESTATUS3) );
  670. OSTRACE4( "ACCESS fsts3ConfigInfo.attrFile=%d flags=%d rc=%d\n",
  671. fsts3ConfigInfo.attrFile, flags, rc );
  672. switch( flags ){
  673. case SQLITE_ACCESS_READ:
  674. case SQLITE_ACCESS_EXISTS:
  675. rc = (rc == NO_ERROR);
  676. OSTRACE3( "ACCESS %s access of read and exists rc=%d\n", zFilename, rc );
  677. break;
  678. case SQLITE_ACCESS_READWRITE:
  679. rc = (fsts3ConfigInfo.attrFile & FILE_READONLY) == 0;
  680. OSTRACE3( "ACCESS %s access of read/write rc=%d\n", zFilename, rc );
  681. break;
  682. default:
  683. assert( !"Invalid flags argument" );
  684. }
  685. return rc;
  686. }
  687. /*
  688. ** Create a temporary file name in zBuf. zBuf must be big enough to
  689. ** hold at pVfs->mxPathname characters.
  690. */
  691. static int os2GetTempname( sqlite3_vfs *pVfs, int nBuf, char *zBuf ){
  692. static const unsigned char zChars[] =
  693. "abcdefghijklmnopqrstuvwxyz"
  694. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  695. "0123456789";
  696. int i, j;
  697. PSZ zTempPath = "";
  698. if( DosScanEnv( (PSZ)"TEMP", &zTempPath ) ){
  699. if( DosScanEnv( (PSZ)"TMP", &zTempPath ) ){
  700. if( DosScanEnv( (PSZ)"TMPDIR", &zTempPath ) ){
  701. ULONG ulDriveNum = 0, ulDriveMap = 0;
  702. DosQueryCurrentDisk( &ulDriveNum, &ulDriveMap );
  703. sprintf( (char*)zTempPath, "%c:", (char)( 'A' + ulDriveNum - 1 ) );
  704. }
  705. }
  706. }
  707. /* strip off a trailing slashes or backslashes, otherwise we would get *
  708. * multiple (back)slashes which causes DosOpen() to fail */
  709. j = strlen(zTempPath);
  710. while( j > 0 && ( zTempPath[j-1] == '\\' || zTempPath[j-1] == '/' ) ){
  711. j--;
  712. }
  713. zTempPath[j] = '\0';
  714. assert( nBuf>=pVfs->mxPathname );
  715. sqlite3_snprintf( pVfs->mxPathname-30, zBuf,
  716. "%s\\"SQLITE_TEMP_FILE_PREFIX, zTempPath );
  717. j = strlen( zBuf );
  718. sqlite3Randomness( 20, &zBuf[j] );
  719. for( i = 0; i < 20; i++, j++ ){
  720. zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
  721. }
  722. zBuf[j] = 0;
  723. OSTRACE2( "TEMP FILENAME: %s\n", zBuf );
  724. return SQLITE_OK;
  725. }
  726. /*
  727. ** Turn a relative pathname into a full pathname. Write the full
  728. ** pathname into zFull[]. zFull[] will be at least pVfs->mxPathname
  729. ** bytes in size.
  730. */
  731. static int os2FullPathname(
  732. sqlite3_vfs *pVfs, /* Pointer to vfs object */
  733. const char *zRelative, /* Possibly relative input path */
  734. int nFull, /* Size of output buffer in bytes */
  735. char *zFull /* Output buffer */
  736. ){
  737. if( strchr(zRelative, ':') ){
  738. sqlite3SetString( &zFull, zRelative, (char*)0 );
  739. }else{
  740. ULONG ulDriveNum = 0;
  741. ULONG ulDriveMap = 0;
  742. ULONG cbzBufLen = SQLITE_TEMPNAME_SIZE;
  743. char zDrive[2];
  744. char *zBuff = (char*)malloc( cbzBufLen );
  745. if( zBuff == 0 ){
  746. return SQLITE_NOMEM;
  747. }
  748. DosQueryCurrentDisk( &ulDriveNum, &ulDriveMap );
  749. if( DosQueryCurrentDir( ulDriveNum, (PBYTE)zBuff, &cbzBufLen ) == NO_ERROR ){
  750. sprintf( zDrive, "%c", (char)('A' + ulDriveNum - 1) );
  751. sqlite3SetString( &zFull, zDrive, ":\\", zBuff,
  752. "\\", zRelative, (char*)0 );
  753. }
  754. free( zBuff );
  755. }
  756. return SQLITE_OK;
  757. }
  758. #ifndef SQLITE_OMIT_LOAD_EXTENSION
  759. /*
  760. ** Interfaces for opening a shared library, finding entry points
  761. ** within the shared library, and closing the shared library.
  762. */
  763. /*
  764. ** Interfaces for opening a shared library, finding entry points
  765. ** within the shared library, and closing the shared library.
  766. */
  767. static void *os2DlOpen(sqlite3_vfs *pVfs, const char *zFilename){
  768. UCHAR loadErr[256];
  769. HMODULE hmod;
  770. APIRET rc;
  771. rc = DosLoadModule((PSZ)loadErr, sizeof(loadErr), zFilename, &hmod);
  772. return rc != NO_ERROR ? 0 : (void*)hmod;
  773. }
  774. /*
  775. ** A no-op since the error code is returned on the DosLoadModule call.
  776. ** os2Dlopen returns zero if DosLoadModule is not successful.
  777. */
  778. static void os2DlError(sqlite3_vfs *pVfs, int nBuf, char *zBufOut){
  779. /* no-op */
  780. }
  781. void *os2DlSym(sqlite3_vfs *pVfs, void *pHandle, const char *zSymbol){
  782. PFN pfn;
  783. APIRET rc;
  784. rc = DosQueryProcAddr((HMODULE)pHandle, 0L, zSymbol, &pfn);
  785. if( rc != NO_ERROR ){
  786. /* if the symbol itself was not found, search again for the same
  787. * symbol with an extra underscore, that might be needed depending
  788. * on the calling convention */
  789. char _zSymbol[256] = "_";
  790. strncat(_zSymbol, zSymbol, 255);
  791. rc = DosQueryProcAddr((HMODULE)pHandle, 0L, _zSymbol, &pfn);
  792. }
  793. return rc != NO_ERROR ? 0 : (void*)pfn;
  794. }
  795. void os2DlClose(sqlite3_vfs *pVfs, void *pHandle){
  796. DosFreeModule((HMODULE)pHandle);
  797. }
  798. #else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
  799. #define os2DlOpen 0
  800. #define os2DlError 0
  801. #define os2DlSym 0
  802. #define os2DlClose 0
  803. #endif
  804. /*
  805. ** Write up to nBuf bytes of randomness into zBuf.
  806. */
  807. static int os2Randomness(sqlite3_vfs *pVfs, int nBuf, char *zBuf ){
  808. ULONG sizeofULong = sizeof(ULONG);
  809. int n = 0;
  810. if( sizeof(DATETIME) <= nBuf - n ){
  811. DATETIME x;
  812. DosGetDateTime(&x);
  813. memcpy(&zBuf[n], &x, sizeof(x));
  814. n += sizeof(x);
  815. }
  816. if( sizeofULong <= nBuf - n ){
  817. PPIB ppib;
  818. DosGetInfoBlocks(NULL, &ppib);
  819. memcpy(&zBuf[n], &ppib->pib_ulpid, sizeofULong);
  820. n += sizeofULong;
  821. }
  822. if( sizeofULong <= nBuf - n ){
  823. PTIB ptib;
  824. DosGetInfoBlocks(&ptib, NULL);
  825. memcpy(&zBuf[n], &ptib->tib_ptib2->tib2_ultid, sizeofULong);
  826. n += sizeofULong;
  827. }
  828. /* if we still haven't filled the buffer yet the following will */
  829. /* grab everything once instead of making several calls for a single item */
  830. if( sizeofULong <= nBuf - n ){
  831. ULONG ulSysInfo[QSV_MAX];
  832. DosQuerySysInfo(1L, QSV_MAX, ulSysInfo, sizeofULong * QSV_MAX);
  833. memcpy(&zBuf[n], &ulSysInfo[QSV_MS_COUNT - 1], sizeofULong);
  834. n += sizeofULong;
  835. if( sizeofULong <= nBuf - n ){
  836. memcpy(&zBuf[n], &ulSysInfo[QSV_TIMER_INTERVAL - 1], sizeofULong);
  837. n += sizeofULong;
  838. }
  839. if( sizeofULong <= nBuf - n ){
  840. memcpy(&zBuf[n], &ulSysInfo[QSV_TIME_LOW - 1], sizeofULong);
  841. n += sizeofULong;
  842. }
  843. if( sizeofULong <= nBuf - n ){
  844. memcpy(&zBuf[n], &ulSysInfo[QSV_TIME_HIGH - 1], sizeofULong);
  845. n += sizeofULong;
  846. }
  847. if( sizeofULong <= nBuf - n ){
  848. memcpy(&zBuf[n], &ulSysInfo[QSV_TOTAVAILMEM - 1], sizeofULong);
  849. n += sizeofULong;
  850. }
  851. }
  852. return n;
  853. }
  854. /*
  855. ** Sleep for a little while. Return the amount of time slept.
  856. ** The argument is the number of microseconds we want to sleep.
  857. ** The return value is the number of microseconds of sleep actually
  858. ** requested from the underlying operating system, a number which
  859. ** might be greater than or equal to the argument, but not less
  860. ** than the argument.
  861. */
  862. static int os2Sleep( sqlite3_vfs *pVfs, int microsec ){
  863. DosSleep( (microsec/1000) );
  864. return microsec;
  865. }
  866. /*
  867. ** The following variable, if set to a non-zero value, becomes the result
  868. ** returned from sqlite3OsCurrentTime(). This is used for testing.
  869. */
  870. #ifdef SQLITE_TEST
  871. int sqlite3_current_time = 0;
  872. #endif
  873. /*
  874. ** Find the current time (in Universal Coordinated Time). Write the
  875. ** current time and date as a Julian Day number into *prNow and
  876. ** return 0. Return 1 if the time and date cannot be found.
  877. */
  878. int os2CurrentTime( sqlite3_vfs *pVfs, double *prNow ){
  879. double now;
  880. SHORT minute; /* needs to be able to cope with negative timezone offset */
  881. USHORT second, hour,
  882. day, month, year;
  883. DATETIME dt;
  884. DosGetDateTime( &dt );
  885. second = (USHORT)dt.seconds;
  886. minute = (SHORT)dt.minutes + dt.timezone;
  887. hour = (USHORT)dt.hours;
  888. day = (USHORT)dt.day;
  889. month = (USHORT)dt.month;
  890. year = (USHORT)dt.year;
  891. /* Calculations from http://www.astro.keele.ac.uk/~rno/Astronomy/hjd.html
  892. http://www.astro.keele.ac.uk/~rno/Astronomy/hjd-0.1.c */
  893. /* Calculate the Julian days */
  894. now = day - 32076 +
  895. 1461*(year + 4800 + (month - 14)/12)/4 +
  896. 367*(month - 2 - (month - 14)/12*12)/12 -
  897. 3*((year + 4900 + (month - 14)/12)/100)/4;
  898. /* Add the fractional hours, mins and seconds */
  899. now += (hour + 12.0)/24.0;
  900. now += minute/1440.0;
  901. now += second/86400.0;
  902. *prNow = now;
  903. #ifdef SQLITE_TEST
  904. if( sqlite3_current_time ){
  905. *prNow = sqlite3_current_time/86400.0 + 2440587.5;
  906. }
  907. #endif
  908. return 0;
  909. }
  910. /*
  911. ** Return a pointer to the sqlite3DefaultVfs structure. We use
  912. ** a function rather than give the structure global scope because
  913. ** some compilers (MSVC) do not allow forward declarations of
  914. ** initialized structures.
  915. */
  916. sqlite3_vfs *sqlite3OsDefaultVfs(void){
  917. static sqlite3_vfs os2Vfs = {
  918. 1, /* iVersion */
  919. sizeof(os2File), /* szOsFile */
  920. CCHMAXPATH, /* mxPathname */
  921. 0, /* pNext */
  922. "os2", /* zName */
  923. 0, /* pAppData */
  924. os2Open, /* xOpen */
  925. os2Delete, /* xDelete */
  926. os2Access, /* xAccess */
  927. os2GetTempname, /* xGetTempname */
  928. os2FullPathname, /* xFullPathname */
  929. os2DlOpen, /* xDlOpen */
  930. os2DlError, /* xDlError */
  931. os2DlSym, /* xDlSym */
  932. os2DlClose, /* xDlClose */
  933. os2Randomness, /* xRandomness */
  934. os2Sleep, /* xSleep */
  935. os2CurrentTime /* xCurrentTime */
  936. };
  937. return &os2Vfs;
  938. }
  939. #endif /* OS_OS2 */