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

📄 os.c

📁 sqlite源码wince移植版
💻 C
📖 第 1 页 / 共 5 页
字号:
	s_tm.tm_min  = ls.wMinute;
	s_tm.tm_hour = ls.wHour;
	s_tm.tm_mday = ls.wDay;
	s_tm.tm_mon  = ls.wMonth -1;
	s_tm.tm_year = ls.wYear - 1900;
	s_tm.tm_wday = ls.wDayOfWeek;
	// Return pointer to static data
	return &s_tm;
}
/* --------------------------------------------------------------------- *//*** WinCE File Locking routines.**** These are just helpers for the sqliteOsFileLock family of functions.** The idea is to have a named counter, made with a named mutex and a named** semaphore. The name of this resources is generated from the file path, so** that they are unique for each database opened.**** I'm sure there are many other ways to acomplish the same thing, and maybe** some are better. If you know any better way, just say so, and I will be** happy to use it.*/#if defined(_WIN32_WCE) && ! defined(SQLITE_WCE_OMIT_FILELOCK)// I think this is more than enough for Windows CE#define MAX_FILE_READERS 64static BOOL wceport_OpenFileLock( OsFile * id ){	WCHAR aux[256];	WCHAR muxName[256] = L"SqliteMux";	WCHAR semName[256] = L"SqliteSem";	int i = 0, j = 0;	for ( ; id->filePath[i]; ++i )		switch ( id->filePath[i] )		{		case '\\':		case '/':		break;		default:		aux[j] = id->filePath[i];						++j;		}	aux[j] = '\0';	wcscat( muxName, aux );	id->hMux = CreateMutexW( NULL, FALSE, muxName );	if ( id->hMux == NULL )		return FALSE;	wcscat( semName, aux );	id->hSem = CreateSemaphoreW( NULL, MAX_FILE_READERS,								MAX_FILE_READERS, semName );	if ( id->hSem == 0 )	{		CloseHandle( id->hMux );		return FALSE;	}	id->locked = 0;	return TRUE;}static void wceport_CloseFileLock( OsFile * id ){	if ( id->hMux )		CloseHandle( id->hMux );	if ( id->hSem )		CloseHandle( id->hSem );}// Lock access to file by all processes.// Returns TRUE on success, FALSE on failure.static BOOL wceport_LockFileMutex( HANDLE hMux ){	DWORD res = WaitForSingleObject( hMux, 250 );	// I don't know very well what I have to do in this case.	// The MSDN says that this case is when a thread terminates without	// releasing the mutex. So I have to release it and try again	if ( res == WAIT_ABANDONED )	{		ReleaseMutex( hMux );		res = WaitForSingleObject( hMux, 250 );	}	// success ?	if ( res != WAIT_OBJECT_0 )	{		return FALSE;	// someone is taking a long time	}	return TRUE;}static int wceport_IncrementSemaphoreN( HANDLE hSem, unsigned count ){	DWORD prevCount;	BOOL res = ReleaseSemaphore( hSem, count, &prevCount );	// this can only fail on a program bug or operating sistem error	// what should we do if that happens ?	assert( res != FALSE );	return prevCount;}static BOOL wceport_DecrementSemaphore( HANDLE hSem ){	DWORD res = WaitForSingleObject( hSem, 250 );	// I don't know very well what I have to do in this case.	// Does this apply to semaphores ??!!...//	if ( res == WAIT_ABANDONED )//		....	// success ?	if ( res != WAIT_OBJECT_0 )	{		return FALSE;	// someone is taking a long time	}	return TRUE;}// Decrement semaphore by a specified amount.// If failure, restore the original semaphore count.static BOOL wceport_DecrementSemaphoreN( HANDLE hSem, unsigned count ){	unsigned i;	for ( i = 0; i < count; ++i )	{		if ( ! wceport_DecrementSemaphore(hSem) )		{	// Restore original count			if ( i > 0 )				wceport_IncrementSemaphoreN( hSem, i );			return FALSE;		}	}	return TRUE;}#endif // UNDER_CE && ! SQLITE_WCE_OMIT_FILELOCK/* --------------------------------------------------------------------- *//*** Delete the named file*/int sqliteOsDelete(const char *zFilename){#if OS_UNIX  unlink(zFilename);#endif#if OS_WIN# ifndef _WIN32_WCE  DeleteFile(zFilename);# else  wchar_t * aux = wStrDup( zFilename );  DeleteFileW( aux );  free( aux );# endif#endif#if OS_MAC  unlink(zFilename);#endif  return SQLITE_OK;}/*** Return TRUE if the named file exists.*/int sqliteOsFileExists(const char *zFilename){#if OS_UNIX  return access(zFilename, 0)==0;#endif#if OS_WIN# ifndef _WIN32_WCE  return GetFileAttributes(zFilename) != 0xffffffff;# else  wchar_t * aux = wStrDup( zFilename );  int ret = GetFileAttributesW( aux ) != 0xFFFFFFFF;  free( aux );  return ret;# endif#endif#if OS_MAC  return access(zFilename, 0)==0;#endif}#if 0 /* NOT USED *//*** Change the name of an existing file.*/int sqliteOsFileRename(const char *zOldName, const char *zNewName){#if OS_UNIX  if( link(zOldName, zNewName) ){    return SQLITE_ERROR;  }  unlink(zOldName);  return SQLITE_OK;#endif#if OS_WIN  if( !MoveFile(zOldName, zNewName) ){    return SQLITE_ERROR;  }  return SQLITE_OK;#endif#if OS_MAC  /**** FIX ME ***/  return SQLITE_ERROR;#endif}#endif /* NOT USED *//*** Attempt to open a file for both reading and writing.  If that** fails, try opening it read-only.  If the file does not exist,** try to create it.**** On success, a handle for the open file is written to *id** and *pReadonly is set to 0 if the file was opened for reading and** writing or 1 if the file was opened read-only.  The function returns** SQLITE_OK.**** On failure, the function returns SQLITE_CANTOPEN and leaves** *id and *pReadonly unchanged.*/int sqliteOsOpenReadWrite(  const char *zFilename,  OsFile *id,  int *pReadonly){#if OS_UNIX  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# ifndef _WIN32_WCE  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;# else  WCHAR * wFilename = wStrDup( zFilename );  // Try to open for read/write  HANDLE h = CreateFileW( wFilename, GENERIC_READ | GENERIC_WRITE,      FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_ALWAYS,      FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS, NULL );  // If it fails, try to open read-only  if ( h==INVALID_HANDLE_VALUE )  {    h = CreateFileW( wFilename, GENERIC_READ, FILE_SHARE_READ, NULL,          OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS,          NULL );    if ( h==INVALID_HANDLE_VALUE )	{      free( wFilename );      return SQLITE_CANTOPEN;	}    *pReadonly = 1;  }  else  {    *pReadonly = 0;  }  id->h = h;  id->delOnClose = 0;  id->filePath = wFilename;#  ifndef SQLITE_WCE_OMIT_FILELOCK  // Prepare file lock mechanism  if ( ! wceport_OpenFileLock( id ) )  {     // Failled to initialize file lock mechanism     CloseHandle( id->h );     /* NOTE: the spec says that *id and *pReadOnly should be left              unchanged on failure, but I don't see any problem on              ignoring this here. Should this be fixed ? */     return SQLITE_CANTOPEN;  }#  else // SQLITE_OMIT_FILELOCK  id->hMux   = 0;  id->hSem   = 0;  id->locked = 0;#  endif // SQLITE_OMIT_FILELOCK# endif  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# ifndef _WIN32_WCE  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;# else  WCHAR * wFilename = wStrDup( zFilename );  HANDLE h = CreateFileW( wFilename, GENERIC_READ | GENERIC_WRITE, 0,                NULL, CREATE_ALWAYS, FILE_FLAG_RANDOM_ACCESS, NULL );  if ( h == INVALID_HANDLE_VALUE )  {    free( wFilename );    return SQLITE_CANTOPEN;  }  id->h = h;  id->delOnClose = delFlag;  id->filePath = wFilename;  id->hMux   = 0;	// Not shared, so no need to lock file  id->hSem   = 0;  id->locked = 0;# endif  OpenCounter(+1);  return SQLITE_OK;

⌨️ 快捷键说明

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