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

📄 os.c

📁 sqlite数据库源码
💻 C
📖 第 1 页 / 共 4 页
字号:
  int rc;  id->dirfd = -1;  id->fd = open(zFilename, O_RDWR|O_CREAT|O_LARGEFILE|O_BINARY, 0644);  if( id->fd<0 ){#ifdef EISDIR    if( errno==EISDIR ){      return SQLITE_CANTOPEN;    }#endif    id->fd = open(zFilename, O_RDONLY|O_LARGEFILE|O_BINARY);    if( id->fd<0 ){      return SQLITE_CANTOPEN;     }    *pReadonly = 1;  }else{    *pReadonly = 0;  }  sqliteOsEnterMutex();  rc = findLockInfo(id->fd, &id->pLock, &id->pOpen);  sqliteOsLeaveMutex();  if( rc ){    close(id->fd);    return SQLITE_NOMEM;  }  id->locked = 0;  TRACE3("OPEN    %-3d %s\n", id->fd, zFilename);  OpenCounter(+1);  return SQLITE_OK;#endif#if OS_WIN  HANDLE h = CreateFile(zFilename,     GENERIC_READ | GENERIC_WRITE,     FILE_SHARE_READ | FILE_SHARE_WRITE,     NULL,     OPEN_ALWAYS,     FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS,     NULL  );  if( h==INVALID_HANDLE_VALUE ){    h = CreateFile(zFilename,       GENERIC_READ,       FILE_SHARE_READ,       NULL,       OPEN_ALWAYS,       FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS,       NULL    );    if( h==INVALID_HANDLE_VALUE ){      return SQLITE_CANTOPEN;    }    *pReadonly = 1;  }else{    *pReadonly = 0;  }  id->h = h;  id->locked = 0;  OpenCounter(+1);  return SQLITE_OK;#endif#if OS_MAC  FSSpec fsSpec;# ifdef _LARGE_FILE  HFSUniStr255 dfName;  FSRef fsRef;  if( __path2fss(zFilename, &fsSpec) != noErr ){    if( HCreate(fsSpec.vRefNum, fsSpec.parID, fsSpec.name, 'SQLI', cDocumentFile) != noErr )      return SQLITE_CANTOPEN;  }  if( FSpMakeFSRef(&fsSpec, &fsRef) != noErr )    return SQLITE_CANTOPEN;  FSGetDataForkName(&dfName);  if( FSOpenFork(&fsRef, dfName.length, dfName.unicode,                 fsRdWrShPerm, &(id->refNum)) != noErr ){    if( FSOpenFork(&fsRef, dfName.length, dfName.unicode,                   fsRdWrPerm, &(id->refNum)) != noErr ){      if (FSOpenFork(&fsRef, dfName.length, dfName.unicode,                   fsRdPerm, &(id->refNum)) != noErr )        return SQLITE_CANTOPEN;      else        *pReadonly = 1;    } else      *pReadonly = 0;  } else    *pReadonly = 0;# else  __path2fss(zFilename, &fsSpec);  if( !sqliteOsFileExists(zFilename) ){    if( HCreate(fsSpec.vRefNum, fsSpec.parID, fsSpec.name, 'SQLI', cDocumentFile) != noErr )      return SQLITE_CANTOPEN;  }  if( HOpenDF(fsSpec.vRefNum, fsSpec.parID, fsSpec.name, fsRdWrShPerm, &(id->refNum)) != noErr ){    if( HOpenDF(fsSpec.vRefNum, fsSpec.parID, fsSpec.name, fsRdWrPerm, &(id->refNum)) != noErr ){      if( HOpenDF(fsSpec.vRefNum, fsSpec.parID, fsSpec.name, fsRdPerm, &(id->refNum)) != noErr )        return SQLITE_CANTOPEN;      else        *pReadonly = 1;    } else      *pReadonly = 0;  } else    *pReadonly = 0;# endif  if( HOpenRF(fsSpec.vRefNum, fsSpec.parID, fsSpec.name, fsRdWrShPerm, &(id->refNumRF)) != noErr){    id->refNumRF = -1;  }  id->locked = 0;  id->delOnClose = 0;  OpenCounter(+1);  return SQLITE_OK;#endif}/*** Attempt to open a new file for exclusive access by this process.** The file will be opened for both reading and writing.  To avoid** a potential security problem, we do not allow the file to have** previously existed.  Nor do we allow the file to be a symbolic** link.**** If delFlag is true, then make arrangements to automatically delete** the file when it is closed.**** On success, write the file handle into *id and return SQLITE_OK.**** On failure, return SQLITE_CANTOPEN.*/int sqliteOsOpenExclusive(const char *zFilename, OsFile *id, int delFlag){#if OS_UNIX  int rc;  if( access(zFilename, 0)==0 ){    return SQLITE_CANTOPEN;  }  id->dirfd = -1;  id->fd = open(zFilename,                O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW|O_LARGEFILE|O_BINARY, 0600);  if( id->fd<0 ){    return SQLITE_CANTOPEN;  }  sqliteOsEnterMutex();  rc = findLockInfo(id->fd, &id->pLock, &id->pOpen);  sqliteOsLeaveMutex();  if( rc ){    close(id->fd);    unlink(zFilename);    return SQLITE_NOMEM;  }  id->locked = 0;  if( delFlag ){    unlink(zFilename);  }  TRACE3("OPEN-EX %-3d %s\n", id->fd, zFilename);  OpenCounter(+1);  return SQLITE_OK;#endif#if OS_WIN  HANDLE h;  int fileflags;  if( delFlag ){    fileflags = FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_RANDOM_ACCESS                      | FILE_FLAG_DELETE_ON_CLOSE;  }else{    fileflags = FILE_FLAG_RANDOM_ACCESS;  }  h = CreateFile(zFilename,     GENERIC_READ | GENERIC_WRITE,     0,     NULL,     CREATE_ALWAYS,     fileflags,     NULL  );  if( h==INVALID_HANDLE_VALUE ){    return SQLITE_CANTOPEN;  }  id->h = h;  id->locked = 0;  OpenCounter(+1);  return SQLITE_OK;#endif#if OS_MAC  FSSpec fsSpec;# ifdef _LARGE_FILE  HFSUniStr255 dfName;  FSRef fsRef;  __path2fss(zFilename, &fsSpec);  if( HCreate(fsSpec.vRefNum, fsSpec.parID, fsSpec.name, 'SQLI', cDocumentFile) != noErr )    return SQLITE_CANTOPEN;  if( FSpMakeFSRef(&fsSpec, &fsRef) != noErr )    return SQLITE_CANTOPEN;  FSGetDataForkName(&dfName);  if( FSOpenFork(&fsRef, dfName.length, dfName.unicode,                 fsRdWrPerm, &(id->refNum)) != noErr )    return SQLITE_CANTOPEN;# else  __path2fss(zFilename, &fsSpec);  if( HCreate(fsSpec.vRefNum, fsSpec.parID, fsSpec.name, 'SQLI', cDocumentFile) != noErr )    return SQLITE_CANTOPEN;  if( HOpenDF(fsSpec.vRefNum, fsSpec.parID, fsSpec.name, fsRdWrPerm, &(id->refNum)) != noErr )    return SQLITE_CANTOPEN;# endif  id->refNumRF = -1;  id->locked = 0;  id->delOnClose = delFlag;  if (delFlag)    id->pathToDel = sqliteOsFullPathname(zFilename);  OpenCounter(+1);  return SQLITE_OK;#endif}/*** Attempt to open a new file for read-only access.**** On success, write the file handle into *id and return SQLITE_OK.**** On failure, return SQLITE_CANTOPEN.*/int sqliteOsOpenReadOnly(const char *zFilename, OsFile *id){#if OS_UNIX  int rc;  id->dirfd = -1;  id->fd = open(zFilename, O_RDONLY|O_LARGEFILE|O_BINARY);  if( id->fd<0 ){    return SQLITE_CANTOPEN;  }  sqliteOsEnterMutex();  rc = findLockInfo(id->fd, &id->pLock, &id->pOpen);  sqliteOsLeaveMutex();  if( rc ){    close(id->fd);    return SQLITE_NOMEM;  }  id->locked = 0;  TRACE3("OPEN-RO %-3d %s\n", id->fd, zFilename);  OpenCounter(+1);  return SQLITE_OK;#endif#if OS_WIN  HANDLE h = CreateFile(zFilename,     GENERIC_READ,     0,     NULL,     OPEN_EXISTING,     FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS,     NULL  );  if( h==INVALID_HANDLE_VALUE ){    return SQLITE_CANTOPEN;  }  id->h = h;  id->locked = 0;  OpenCounter(+1);  return SQLITE_OK;#endif#if OS_MAC  FSSpec fsSpec;# ifdef _LARGE_FILE  HFSUniStr255 dfName;  FSRef fsRef;  if( __path2fss(zFilename, &fsSpec) != noErr )    return SQLITE_CANTOPEN;  if( FSpMakeFSRef(&fsSpec, &fsRef) != noErr )    return SQLITE_CANTOPEN;  FSGetDataForkName(&dfName);  if( FSOpenFork(&fsRef, dfName.length, dfName.unicode,                 fsRdPerm, &(id->refNum)) != noErr )    return SQLITE_CANTOPEN;# else  __path2fss(zFilename, &fsSpec);  if( HOpenDF(fsSpec.vRefNum, fsSpec.parID, fsSpec.name, fsRdPerm, &(id->refNum)) != noErr )    return SQLITE_CANTOPEN;# endif  if( HOpenRF(fsSpec.vRefNum, fsSpec.parID, fsSpec.name, fsRdWrShPerm, &(id->refNumRF)) != noErr){    id->refNumRF = -1;  }  id->locked = 0;  id->delOnClose = 0;  OpenCounter(+1);  return SQLITE_OK;#endif}/*** Attempt to open a file descriptor for the directory that contains a** file.  This file descriptor can be used to fsync() the directory** in order to make sure the creation of a new file is actually written** to disk.**** This routine is only meaningful for Unix.  It is a no-op under** windows since windows does not support hard links.**** On success, a handle for a previously open file is at *id is** updated with the new directory file descriptor and SQLITE_OK is** returned.**** On failure, the function returns SQLITE_CANTOPEN and leaves** *id unchanged.*/int sqliteOsOpenDirectory(  const char *zDirname,  OsFile *id){#if OS_UNIX  if( id->fd<0 ){    /* Do not open the directory if the corresponding file is not already    ** open. */    return SQLITE_CANTOPEN;  }  assert( id->dirfd<0 );  id->dirfd = open(zDirname, O_RDONLY|O_BINARY, 0644);  if( id->dirfd<0 ){    return SQLITE_CANTOPEN;   }  TRACE3("OPENDIR %-3d %s\n", id->dirfd, zDirname);#endif  return SQLITE_OK;}/*** If the following global variable points to a string which is the** name of a directory, then that directory will be used to store** temporary files.*/const char *sqlite_temp_directory = 0;/*** Create a temporary file name in zBuf.  zBuf must be big enough to** hold at least SQLITE_TEMPNAME_SIZE characters.*/int sqliteOsTempFileName(char *zBuf){#if OS_UNIX  static const char *azDirs[] = {     0,     "/var/tmp",     "/usr/tmp",     "/tmp",     ".",  };  static unsigned char zChars[] =    "abcdefghijklmnopqrstuvwxyz"    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"    "0123456789";  int i, j;  struct stat buf;  const char *zDir = ".";  azDirs[0] = sqlite_temp_directory;  for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); i++){    if( azDirs[i]==0 ) continue;    if( stat(azDirs[i], &buf) ) continue;    if( !S_ISDIR(buf.st_mode) ) continue;    if( access(azDirs[i], 07) ) continue;    zDir = azDirs[i];    break;  }  do{    sprintf(zBuf, "%s/"TEMP_FILE_PREFIX, zDir);    j = strlen(zBuf);    sqliteRandomness(15, &zBuf[j]);    for(i=0; i<15; i++, j++){      zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];    }    zBuf[j] = 0;  }while( access(zBuf,0)==0 );#endif#if OS_WIN  static char zChars[] =    "abcdefghijklmnopqrstuvwxyz"    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"    "0123456789";  int i, j;  const char *zDir;  char zTempPath[SQLITE_TEMPNAME_SIZE];  if( sqlite_temp_directory==0 ){    GetTempPath(SQLITE_TEMPNAME_SIZE-30, zTempPath);    for(i=strlen(zTempPath); i>0 && zTempPath[i-1]=='\\'; i--){}    zTempPath[i] = 0;    zDir = zTempPath;  }else{    zDir = sqlite_temp_directory;  }  for(;;){    sprintf(zBuf, "%s\\"TEMP_FILE_PREFIX, zDir);    j = strlen(zBuf);    sqliteRandomness(15, &zBuf[j]);    for(i=0; i<15; i++, j++){      zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];    }    zBuf[j] = 0;    if( !sqliteOsFileExists(zBuf) ) break;  }#endif#if OS_MAC  static char zChars[] =    "abcdefghijklmnopqrstuvwxyz"    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"    "0123456789";  int i, j;  char *zDir;  char zTempPath[SQLITE_TEMPNAME_SIZE];  char zdirName[32];  CInfoPBRec infoRec;  Str31 dirName;  memset(&infoRec, 0, sizeof(infoRec));  memset(zTempPath, 0, SQLITE_TEMPNAME_SIZE);  if( sqlite_temp_directory!=0 ){    zDir = sqlite_temp_directory;  }else if( FindFolder(kOnSystemDisk, kTemporaryFolderType,  kCreateFolder,       &(infoRec.dirInfo.ioVRefNum), &(infoRec.dirInfo.ioDrParID)) == noErr ){    infoRec.dirInfo.ioNamePtr = dirName;    do{      infoRec.dirInfo.ioFDirIndex = -1;      infoRec.dirInfo.ioDrDirID = infoRec.dirInfo.ioDrParID;      if( PBGetCatInfoSync(&infoRec) == noErr ){        CopyPascalStringToC(dirName, zdirName);        i = strlen(zdirName);        memmove(&(zTempPath[i+1]), zTempPath, strlen(zTempPath));        strcpy(zTempPath, zdirName);        zTempPath[i] = ':';      }else{        *zTempPath = 0;        break;      }    } while( infoRec.dirInfo.ioDrDirID != fsRtDirID );    zDir = zTempPath;  }  if( zDir[0]==0 ){    getcwd(zTempPath, SQLITE_TEMPNAME_SIZE-24);    zDir = zTempPath;  }  for(;;){    sprintf(zBuf, "%s"TEMP_FILE_PREFIX, zDir);    j = strlen(zBuf);    sqliteRandomness(15, &zBuf[j]);    for(i=0; i<15; i++, j++){      zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];    }    zBuf[j] = 0;    if( !sqliteOsFileExists(zBuf) ) break;  }#endif  return SQLITE_OK; }/*** Close a file.*/int sqliteOsClose(OsFile *id){#if OS_UNIX  sqliteOsUnlock(id);  if( id->dirfd>=0 ) close(id->dirfd);  id->dirfd = -1;  sqliteOsEnterMutex();  if( id->pOpen->nLock ){    /* If there are outstanding locks, do not actually close the file just    ** yet because that would clear those locks.  Instead, add the file    ** descriptor to pOpen->aPending.  It will be automatically closed when    ** the last lock is cleared.    */    int *aNew;    struct openCnt *pOpen = id->pOpen;    pOpen->nPending++;    aNew = sqliteRealloc( pOpen->aPending, pOpen->nPending*sizeof(int) );

⌨️ 快捷键说明

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