一、源码下载
从网站下载sqlite-src-3330000.zip.
https://www.sqlite.org/download.html
二、sqlite3_open
在linux系统下,当应用程序调用sqlite3_open时,最终会调用open函数创建或打开一个数据库文件,如下图所示:
三、xOpen
在sqlite3OsOpen()函数中,调用linux vfs的xOpen函数:
rc = pVfs->xOpen(pVfs, zPath, pFile, flags & 0x1087f7f, pFlagsOut);
pVfs在sqlite3_vfs_find()函数中会被指定为unix, pVfs-xOpen实际为unixOpen:
int sqlite3_os_init(void){
#define UNIXVFS(VFSNAME, FINDER) { \
3, /* iVersion */ \
sizeof(unixFile), /* szOsFile */ \
MAX_PATHNAME, /* mxPathname */ \
0, /* pNext */ \
VFSNAME, /* zName */ \
(void*)&FINDER, /* pAppData */ \
unixOpen, /* xOpen */ \
unixDelete, /* xDelete */ \
unixAccess, /* xAccess */ \
unixFullPathname, /* xFullPathname */ \
unixDlOpen, /* xDlOpen */ \
unixDlError, /* xDlError */ \
unixDlSym, /* xDlSym */ \
unixDlClose, /* xDlClose */ \
unixRandomness, /* xRandomness */ \
unixSleep, /* xSleep */ \
unixCurrentTime, /* xCurrentTime */ \
unixGetLastError, /* xGetLastError */ \
unixCurrentTimeInt64, /* xCurrentTimeInt64 */ \
unixSetSystemCall, /* xSetSystemCall */ \
unixGetSystemCall, /* xGetSystemCall */ \
unixNextSystemCall, /* xNextSystemCall */ \
}
unixOpen()调用robust_open:
fd = robust_open(zName, openFlags, openMode);
robust_open()调用osOpen():
if( osOpen("/dev/null", O_RDONLY, m)<0 ) break;
osOpen()被定义为posixOpen():
//Os_unix.c
static struct unix_syscall {
const char *zName; /* Name of the system call */
sqlite3_syscall_ptr pCurrent; /* Current value of the system call */
sqlite3_syscall_ptr pDefault; /* Default value */
} aSyscall[] = {
{ "open", (sqlite3_syscall_ptr)posixOpen, 0 },
#define osOpen ((int(*)(const char*,int,int))aSyscall[0].pCurrent)
{ "close", (sqlite3_syscall_ptr)close, 0 },
#define osClose ((int(*)(int))aSyscall[1].pCurrent)
posixOpen调用linux系统调用open:
static int posixOpen(const char *zFile, int flags, int mode){
return open(zFile, flags, mode);
}