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

📄 os_unix.c

📁 sqlite的最新源码 This ZIP archive contains preprocessed C code for the SQLite library as individual sour
💻 C
📖 第 1 页 / 共 5 页
字号:
  /* Unix cannot, but some systems may return SQLITE_FULL from here. This  ** line is to test that doing so does not cause any problems.  */  SimulateDiskfullError( return SQLITE_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 );  SimulateIOError( return SQLITE_IOERR_TRUNCATE );  rc = ftruncate(((unixFile*)id)->h, (off_t)nByte);  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;  /* When opening a zero-size database, the findLockInfo() procedure  ** writes a single byte into that file in order to work around a bug  ** in the OS-X msdos filesystem.  In order to avoid problems with upper  ** layers, we need to report this file size as zero even though it is  ** really 1.   Ticket #3260.  */  if( *pSize==1 ) *pSize = 0;  return SQLITE_OK;}/*** This routine translates a standard POSIX errno code into something** useful to the clients of the sqlite3 functions.  Specifically, it is** intended to translate a variety of "try again" errors into SQLITE_BUSY** and a variety of "please close the file descriptor NOW" errors into ** SQLITE_IOERR** ** Errors during initialization of locks, or file system support for locks,** should handle ENOLCK, ENOTSUP, EOPNOTSUPP separately.*/static int sqliteErrorFromPosixError(int posixError, int sqliteIOErr) {  switch (posixError) {  case 0:     return SQLITE_OK;      case EAGAIN:  case ETIMEDOUT:  case EBUSY:  case EINTR:  case ENOLCK:      /* random NFS retry error, unless during file system support      * introspection, in which it actually means what it says */    return SQLITE_BUSY;      case EACCES:     /* EACCES is like EAGAIN during locking operations, but not any other time*/    if( (sqliteIOErr == SQLITE_IOERR_LOCK) || 	(sqliteIOErr == SQLITE_IOERR_UNLOCK) || 	(sqliteIOErr == SQLITE_IOERR_RDLOCK) ||	(sqliteIOErr == SQLITE_IOERR_CHECKRESERVEDLOCK) ){      return SQLITE_BUSY;    }    /* else fall through */  case EPERM:     return SQLITE_PERM;      case EDEADLK:    return SQLITE_IOERR_BLOCKED;    #if EOPNOTSUPP!=ENOTSUP  case EOPNOTSUPP:     /* something went terribly awry, unless during file system support      * introspection, in which it actually means what it says */#endif#ifdef ENOTSUP  case ENOTSUP:     /* invalid fd, unless during file system support introspection, in which      * it actually means what it says */#endif  case EIO:  case EBADF:  case EINVAL:  case ENOTCONN:  case ENODEV:  case ENXIO:  case ENOENT:  case ESTALE:  case ENOSYS:    /* these should force the client to close the file and reconnect */      default:     return sqliteIOErr;  }}/*** 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, set *pResOut** to a non-zero value otherwise *pResOut is set to zero.  The return value** is set to SQLITE_OK unless an I/O error occurs during lock checking.*/static int unixCheckReservedLock(sqlite3_file *id, int *pResOut){  int rc = SQLITE_OK;  int reserved = 0;  unixFile *pFile = (unixFile*)id;  SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );  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 ){    reserved = 1;  }  /* Otherwise see if some other process holds it.  */  if( !reserved ){    struct flock lock;    lock.l_whence = SEEK_SET;    lock.l_start = RESERVED_BYTE;    lock.l_len = 1;    lock.l_type = F_WRLCK;    if (-1 == fcntl(pFile->h, F_GETLK, &lock)) {      int tErrno = errno;      rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK);      pFile->lastErrno = tErrno;    } else if( lock.l_type!=F_UNLCK ){      reserved = 1;    }  }    leaveMutex();  OSTRACE4("TEST WR-LOCK %d %d %d\n", pFile->h, rc, reserved);  *pResOut = reserved;  return rc;}/*** 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) ){      int tErrno = errno;      rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);      if( IS_LOCK_ERROR(rc) ){        pFile->lastErrno = tErrno;      }      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 ){    int tErrno = 0;    assert( pLock->cnt==0 );    assert( pLock->locktype==0 );    /* Now get the read-lock */    lock.l_start = SHARED_FIRST;    lock.l_len = SHARED_SIZE;    if( (s = fcntl(pFile->h, F_SETLK, &lock))==(-1) ){      tErrno = errno;    }    /* 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 ){      if( s != -1 ){        /* This could happen with a network mount */        tErrno = errno;         rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);         if( IS_LOCK_ERROR(rc) ){          pFile->lastErrno = tErrno;        }        goto end_lock;      }    }    if( s==(-1) ){      rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);      if( IS_LOCK_ERROR(rc) ){        pFile->lastErrno = tErrno;      }    }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);    }    s = fcntl(pFile->h, F_SETLK, &lock);    if( s==(-1) ){      int tErrno = errno;      rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);      if( IS_LOCK_ERROR(rc) ){        pFile->lastErrno = tErrno;      }    }  }    if( rc==SQLITE_OK ){    pFile->locktype = locktype;    pLock->locktype = locktype;  }else if( locktype==EXCLUSIVE_LOCK ){    pFile->locktype = PENDING_LOCK;    pLock->locktype = PENDING_LOCK;  }end_lock:  leaveMutex();  OSTRACE4("LOCK    %d %s %s\n", pFile->h, locktypeName(locktype),       rc==SQLITE_OK ? "ok" : "failed");  return rc;}

⌨️ 快捷键说明

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