⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 os_unix.c

📁 这是一个开源的数据库系统,值得学习啊, 里面用了SQL语句,与微软的SQL SERVIER,差不了多少
💻 C
📖 第 1 页 / 共 5 页
字号:
  assert( amt>0 );  while( amt>0 && (wrote = seekAndWrite((unixFile*)id, offset, pBuf, amt))>0 ){    amt -= wrote;    offset += wrote;    pBuf = &((char*)pBuf)[wrote];  }  SimulateIOError(( wrote=(-1), amt=1 ));  SimulateDiskfullError(( wrote=0, amt=1 ));  if( amt>0 ){    if( wrote<0 ){      return SQLITE_IOERR_WRITE;    }else{      return SQLITE_FULL;    }  }  return SQLITE_OK;}#ifdef SQLITE_TEST/*** Count the number of fullsyncs and normal syncs.  This is used to test** that syncs and fullsyncs are occuring at the right times.*/int sqlite3_sync_count = 0;int sqlite3_fullsync_count = 0;#endif/*** Use the fdatasync() API only if the HAVE_FDATASYNC macro is defined.** Otherwise use fsync() in its place.*/#ifndef HAVE_FDATASYNC# define fdatasync fsync#endif/*** Define HAVE_FULLFSYNC to 0 or 1 depending on whether or not** the F_FULLFSYNC macro is defined.  F_FULLFSYNC is currently** only available on Mac OS X.  But that could change.*/#ifdef F_FULLFSYNC# define HAVE_FULLFSYNC 1#else# define HAVE_FULLFSYNC 0#endif/*** The fsync() system call does not work as advertised on many** unix systems.  The following procedure is an attempt to make** it work better.**** The SQLITE_NO_SYNC macro disables all fsync()s.  This is useful** for testing when we want to run through the test suite quickly.** You are strongly advised *not* to deploy with SQLITE_NO_SYNC** enabled, however, since with SQLITE_NO_SYNC enabled, an OS crash** or power failure will likely corrupt the database file.*/static int full_fsync(int fd, int fullSync, int dataOnly){  int rc;  /* Record the number of times that we do a normal fsync() and   ** FULLSYNC.  This is used during testing to verify that this procedure  ** gets called with the correct arguments.  */#ifdef SQLITE_TEST  if( fullSync ) sqlite3_fullsync_count++;  sqlite3_sync_count++;#endif  /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a  ** no-op  */#ifdef SQLITE_NO_SYNC  rc = SQLITE_OK;#else#if HAVE_FULLFSYNC  if( fullSync ){    rc = fcntl(fd, F_FULLFSYNC, 0);  }else{    rc = 1;  }  /* If the FULLFSYNC failed, fall back to attempting an fsync().   * It shouldn't be possible for fullfsync to fail on the local    * file system (on OSX), so failure indicates that FULLFSYNC   * isn't supported for this file system. So, attempt an fsync    * and (for now) ignore the overhead of a superfluous fcntl call.     * It'd be better to detect fullfsync support once and avoid    * the fcntl call every time sync is called.   */  if( rc ) rc = fsync(fd);#else   if( dataOnly ){    rc = fdatasync(fd);  }else{    rc = fsync(fd);  }#endif /* HAVE_FULLFSYNC */#endif /* defined(SQLITE_NO_SYNC) */  return rc;}/*** Make sure all writes to a particular file are committed to disk.**** If dataOnly==0 then both the file itself and its metadata (file** size, access time, etc) are synced.  If dataOnly!=0 then only the** file data is synced.**** Under Unix, also make sure that the directory entry for the file** has been created by fsync-ing the directory that contains the file.** If we do not do this and we encounter a power failure, the directory** entry for the journal might not exist after we reboot.  The next** SQLite to access the file will not know that the journal exists (because** the directory entry for the journal was never created) and the transaction** will not roll back - possibly leading to database corruption.*/static int unixSync(sqlite3_file *id, int flags){  int rc;  unixFile *pFile = (unixFile*)id;  int isDataOnly = (flags&SQLITE_SYNC_DATAONLY);  int isFullsync = (flags&0x0F)==SQLITE_SYNC_FULL;  /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */  assert((flags&0x0F)==SQLITE_SYNC_NORMAL      || (flags&0x0F)==SQLITE_SYNC_FULL  );  assert( pFile );  OSTRACE2("SYNC    %-3d\n", pFile->h);  rc = full_fsync(pFile->h, isFullsync, isDataOnly);  SimulateIOError( rc=1 );  if( rc ){    return SQLITE_IOERR_FSYNC;  }  if( pFile->dirfd>=0 ){    OSTRACE4("DIRSYNC %-3d (have_fullfsync=%d fullsync=%d)\n", pFile->dirfd,            HAVE_FULLFSYNC, isFullsync);#ifndef SQLITE_DISABLE_DIRSYNC    /* The directory sync is only attempted if full_fsync is    ** turned off or unavailable.  If a full_fsync occurred above,    ** then the directory sync is superfluous.    */    if( (!HAVE_FULLFSYNC || !isFullsync) && full_fsync(pFile->dirfd,0,0) ){       /*       ** We have received multiple reports of fsync() returning       ** errors when applied to directories on certain file systems.       ** A failed directory sync is not a big deal.  So it seems       ** better to ignore the error.  Ticket #1657       */       /* return SQLITE_IOERR; */    }#endif    close(pFile->dirfd);  /* Only need to sync once, so close the directory */    pFile->dirfd = -1;    /* when we are done. */  }  return SQLITE_OK;}/*** Truncate an open file to a specified size*/static int unixTruncate(sqlite3_file *id, i64 nByte){  int rc;  assert( id );  rc = ftruncate(((unixFile*)id)->h, (off_t)nByte);  SimulateIOError( rc=1 );  if( rc ){    return SQLITE_IOERR_TRUNCATE;  }else{    return SQLITE_OK;  }}/*** Determine the current size of a file in bytes*/static int unixFileSize(sqlite3_file *id, i64 *pSize){  int rc;  struct stat buf;  assert( id );  rc = fstat(((unixFile*)id)->h, &buf);  SimulateIOError( rc=1 );  if( rc!=0 ){    return SQLITE_IOERR_FSTAT;  }  *pSize = buf.st_size;  return SQLITE_OK;}/*** This routine checks if there is a RESERVED lock held on the specified** file by this or any other process. If such a lock is held, return** non-zero.  If the file is unlocked or holds only SHARED locks, then** return zero.*/static int unixCheckReservedLock(sqlite3_file *id){  int r = 0;  unixFile *pFile = (unixFile*)id;  assert( pFile );  enterMutex(); /* Because pFile->pLock is shared across threads */  /* Check if a thread in this process holds such a lock */  if( pFile->pLock->locktype>SHARED_LOCK ){    r = 1;  }  /* Otherwise see if some other process holds it.  */  if( !r ){    struct flock lock;    lock.l_whence = SEEK_SET;    lock.l_start = RESERVED_BYTE;    lock.l_len = 1;    lock.l_type = F_WRLCK;    fcntl(pFile->h, F_GETLK, &lock);    if( lock.l_type!=F_UNLCK ){      r = 1;    }  }    leaveMutex();  OSTRACE3("TEST WR-LOCK %d %d\n", pFile->h, r);  return r;}/*** Lock the file with the lock specified by parameter locktype - one** of the following:****     (1) SHARED_LOCK**     (2) RESERVED_LOCK**     (3) PENDING_LOCK**     (4) EXCLUSIVE_LOCK**** Sometimes when requesting one lock state, additional lock states** are inserted in between.  The locking might fail on one of the later** transitions leaving the lock state different from what it started but** still short of its goal.  The following chart shows the allowed** transitions and the inserted intermediate states:****    UNLOCKED -> SHARED**    SHARED -> RESERVED**    SHARED -> (PENDING) -> EXCLUSIVE**    RESERVED -> (PENDING) -> EXCLUSIVE**    PENDING -> EXCLUSIVE**** This routine will only increase a lock.  Use the sqlite3OsUnlock()** routine to lower a locking level.*/static int unixLock(sqlite3_file *id, int locktype){  /* The following describes the implementation of the various locks and  ** lock transitions in terms of the POSIX advisory shared and exclusive  ** lock primitives (called read-locks and write-locks below, to avoid  ** confusion with SQLite lock names). The algorithms are complicated  ** slightly in order to be compatible with windows systems simultaneously  ** accessing the same database file, in case that is ever required.  **  ** Symbols defined in os.h indentify the 'pending byte' and the 'reserved  ** byte', each single bytes at well known offsets, and the 'shared byte  ** range', a range of 510 bytes at a well known offset.  **  ** To obtain a SHARED lock, a read-lock is obtained on the 'pending  ** byte'.  If this is successful, a random byte from the 'shared byte  ** range' is read-locked and the lock on the 'pending byte' released.  **  ** A process may only obtain a RESERVED lock after it has a SHARED lock.  ** A RESERVED lock is implemented by grabbing a write-lock on the  ** 'reserved byte'.   **  ** A process may only obtain a PENDING lock after it has obtained a  ** SHARED lock. A PENDING lock is implemented by obtaining a write-lock  ** on the 'pending byte'. This ensures that no new SHARED locks can be  ** obtained, but existing SHARED locks are allowed to persist. A process  ** does not have to obtain a RESERVED lock on the way to a PENDING lock.  ** This property is used by the algorithm for rolling back a journal file  ** after a crash.  **  ** An EXCLUSIVE lock, obtained after a PENDING lock is held, is  ** implemented by obtaining a write-lock on the entire 'shared byte  ** range'. Since all other locks require a read-lock on one of the bytes  ** within this range, this ensures that no other locks are held on the  ** database.   **  ** The reason a single byte cannot be used instead of the 'shared byte  ** range' is that some versions of windows do not support read-locks. By  ** locking a random byte from a range, concurrent SHARED locks may exist  ** even if the locking primitive used is always a write-lock.  */  int rc = SQLITE_OK;  unixFile *pFile = (unixFile*)id;  struct lockInfo *pLock = pFile->pLock;  struct flock lock;  int s;  assert( pFile );  OSTRACE7("LOCK    %d %s was %s(%s,%d) pid=%d\n", pFile->h,      locktypeName(locktype), locktypeName(pFile->locktype),      locktypeName(pLock->locktype), pLock->cnt , getpid());  /* If there is already a lock of this type or more restrictive on the  ** unixFile, do nothing. Don't use the end_lock: exit path, as  ** enterMutex() hasn't been called yet.  */  if( pFile->locktype>=locktype ){    OSTRACE3("LOCK    %d %s ok (already held)\n", pFile->h,            locktypeName(locktype));    return SQLITE_OK;  }  /* Make sure the locking sequence is correct  */  assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );  assert( locktype!=PENDING_LOCK );  assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );  /* This mutex is needed because pFile->pLock is shared across threads  */  enterMutex();  /* Make sure the current thread owns the pFile.  */  rc = transferOwnership(pFile);  if( rc!=SQLITE_OK ){    leaveMutex();    return rc;  }  pLock = pFile->pLock;  /* If some thread using this PID has a lock via a different unixFile*  ** handle that precludes the requested lock, return BUSY.  */  if( (pFile->locktype!=pLock->locktype &&           (pLock->locktype>=PENDING_LOCK || locktype>SHARED_LOCK))  ){    rc = SQLITE_BUSY;    goto end_lock;  }  /* If a SHARED lock is requested, and some thread using this PID already  ** has a SHARED or RESERVED lock, then increment reference counts and  ** return SQLITE_OK.  */  if( locktype==SHARED_LOCK &&       (pLock->locktype==SHARED_LOCK || pLock->locktype==RESERVED_LOCK) ){    assert( locktype==SHARED_LOCK );    assert( pFile->locktype==0 );    assert( pLock->cnt>0 );    pFile->locktype = SHARED_LOCK;    pLock->cnt++;    pFile->pOpen->nLock++;    goto end_lock;  }  lock.l_len = 1L;  lock.l_whence = SEEK_SET;  /* A PENDING lock is needed before acquiring a SHARED lock and before  ** acquiring an EXCLUSIVE lock.  For the SHARED lock, the PENDING will  ** be released.  */  if( locktype==SHARED_LOCK       || (locktype==EXCLUSIVE_LOCK && pFile->locktype<PENDING_LOCK)  ){    lock.l_type = (locktype==SHARED_LOCK?F_RDLCK:F_WRLCK);    lock.l_start = PENDING_BYTE;    s = fcntl(pFile->h, F_SETLK, &lock);    if( s==(-1) ){      rc = (errno==EINVAL) ? SQLITE_NOLFS : SQLITE_BUSY;      goto end_lock;    }  }  /* If control gets to this point, then actually go ahead and make  ** operating system calls for the specified lock.  */  if( locktype==SHARED_LOCK ){    assert( pLock->cnt==0 );    assert( pLock->locktype==0 );    /* Now get the read-lock */    lock.l_start = SHARED_FIRST;    lock.l_len = SHARED_SIZE;    s = fcntl(pFile->h, F_SETLK, &lock);    /* Drop the temporary PENDING lock */    lock.l_start = PENDING_BYTE;    lock.l_len = 1L;    lock.l_type = F_UNLCK;    if( fcntl(pFile->h, F_SETLK, &lock)!=0 ){      rc = SQLITE_IOERR_UNLOCK;  /* This should never happen */      goto end_lock;    }    if( s==(-1) ){      rc = (errno==EINVAL) ? SQLITE_NOLFS : SQLITE_BUSY;    }else{      pFile->locktype = SHARED_LOCK;      pFile->pOpen->nLock++;      pLock->cnt = 1;    }  }else if( locktype==EXCLUSIVE_LOCK && pLock->cnt>1 ){    /* We are trying for an exclusive lock but another thread in this    ** same process is still holding a shared lock. */    rc = SQLITE_BUSY;  }else{    /* The request was for a RESERVED or EXCLUSIVE lock.  It is    ** assumed that there is a SHARED or greater lock on the file    ** already.    */    assert( 0!=pFile->locktype );    lock.l_type = F_WRLCK;    switch( locktype ){      case RESERVED_LOCK:        lock.l_start = RESERVED_BYTE;        break;      case EXCLUSIVE_LOCK:        lock.l_start = SHARED_FIRST;        lock.l_len = SHARED_SIZE;        break;      default:        assert(0);    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -