Beispiel #1
0
CSemaphoreImpl::CSemaphoreImpl(int n, int max): _n(n), _max(max)
{
	poco_assert (n >= 0 && max > 0 && n <= max);

	if (pthread_mutex_init(&_mutex, NULL))
		throw CSystemException("cannot create semaphore (mutex)");
	if (pthread_cond_init(&_cond, NULL))
		throw CSystemException("cannot create semaphore (condition)");
}
Beispiel #2
0
CRWLockImpl::CRWLockImpl(): _readers(0), _writers(0)
{
	_mutex = CreateMutexW(NULL, FALSE, NULL);
	if (_mutex == NULL)
		throw CSystemException("cannot create reader/writer lock");

	_readEvent = CreateEventW(NULL, TRUE, TRUE, NULL);
	if (_readEvent == NULL)
		throw CSystemException("cannot create reader/writer lock");

	_writeEvent = CreateEventW(NULL, TRUE, TRUE, NULL);
	if (_writeEvent == NULL)
		throw CSystemException("cannot create reader/writer lock");
}
Beispiel #3
0
void CSemaphoreImpl::waitImpl()
{
	if (pthread_mutex_lock(&_mutex))
		throw CSystemException("wait for semaphore failed (lock)"); 
	while (_n < 1) 
	{
		if (pthread_cond_wait(&_cond, &_mutex))
		{
			pthread_mutex_unlock(&_mutex);
			throw CSystemException("wait for semaphore failed");
		}
	}
	--_n;
	pthread_mutex_unlock(&_mutex);
}
Beispiel #4
0
tstring CRegConfig::GetStringValue(TCHAR* Name, const TCHAR* strDefaultValue)
{
	DWORD err, type, size;
	TCHAR *pbuffer;

	size = 0;
	err = RegQueryValueEx(m_hKey, Name, NULL,
		&type, NULL, &size);
	if(ERROR_SUCCESS != err)
	{
		//throw CSystemException(err, "Failed to query configuration value.");
		return tstring(strDefaultValue);
	}

	if(type != REG_SZ)
	{
		throw std::exception("Failed to query configuration value (type mismatch).");
	}

	pbuffer = new TCHAR[(size - 1) / sizeof(TCHAR)];
	err = RegQueryValueEx(m_hKey, Name, NULL,
		&type, reinterpret_cast<LPBYTE>(pbuffer), &size);
	if(ERROR_SUCCESS != err)
	{
		throw CSystemException(err, "Failed to query configuration value.");
	}

	return tstring(pbuffer);
}
Beispiel #5
0
CSemaphoreImpl::CSemaphoreImpl(int n, int max)
{
	poco_assert (n >= 0 && max > 0 && n <= max);

	_sema = CreateSemaphoreW(NULL, n, max, NULL);
	if (!_sema)
	{
		throw CSystemException("cannot create semaphore");
	}
}
Beispiel #6
0
void CSemaphoreImpl::waitImpl()
{
	switch (WaitForSingleObject(_sema, INFINITE))
	{
	case WAIT_OBJECT_0:
		return;
	default:
		throw CSystemException("wait for semaphore failed");
	}
}
Beispiel #7
0
void CRegConfig::SetValue(TCHAR* Name, float fValue)
{
	DWORD err;

	err = RegSetValueEx(m_hKey, Name, NULL,
		REG_DWORD, reinterpret_cast<LPBYTE>(&fValue), sizeof(fValue));
	if(ERROR_SUCCESS != err)
	{
		throw CSystemException(err, "Failed to set configuration value.");
	}
}
Beispiel #8
0
void CRegConfig::SetValue(TCHAR* Name, const TCHAR* strValue)
{
	DWORD err;

	err = RegSetValueEx(m_hKey, Name, NULL,
		REG_SZ, reinterpret_cast<const BYTE*>(strValue),
		(_tcslen(strValue) + 1) * sizeof(TCHAR));
	if(ERROR_SUCCESS != err)
	{
		throw CSystemException(err, "Failed to set configuration value.");
	}
}
Beispiel #9
0
bool CSemaphoreImpl::waitImpl(long milliseconds)
{
	switch (WaitForSingleObject(_sema, milliseconds + 1))
	{
	case WAIT_TIMEOUT:
		return false;
	case WAIT_OBJECT_0:
		return true;
	default:
		throw CSystemException("wait for semaphore failed");		
	}
}
Beispiel #10
0
inline void CRWLockImpl::addWriter()
{
	switch (WaitForSingleObject(_mutex, INFINITE))
	{
	case WAIT_OBJECT_0:
		if (++_writers == 1) ResetEvent(_readEvent);
		ReleaseMutex(_mutex);
		break;
	default:
		throw CSystemException("cannot lock reader/writer lock");
	}
}
Beispiel #11
0
void CRWLockImpl::unlockImpl()
{
	switch (WaitForSingleObject(_mutex, INFINITE))
	{
	case WAIT_OBJECT_0:
		if (_writers == 0) SetEvent(_readEvent);
		if (--_readers == 0) SetEvent(_writeEvent);
		ReleaseMutex(_mutex);
		break;
	default:
		throw CSystemException("cannot unlock reader/writer lock");
	}
}
Beispiel #12
0
bool CSemaphoreImpl::waitImpl(long milliseconds)
{
	int rc = 0;
	struct timespec abstime;

#if defined(__VMS)
	struct timespec delta;
	delta.tv_sec  = milliseconds / 1000;
	delta.tv_nsec = (milliseconds % 1000)*1000000;
	pthread_get_expiration_np(&delta, &abstime);
#else
	struct timeval tv;
	gettimeofday(&tv, NULL);
	abstime.tv_sec  = tv.tv_sec + milliseconds / 1000;
	abstime.tv_nsec = tv.tv_usec*1000 + (milliseconds % 1000)*1000000;
	if (abstime.tv_nsec >= 1000000000)
	{
		abstime.tv_nsec -= 1000000000;
		abstime.tv_sec++;
	}
#endif

	if (pthread_mutex_lock(&_mutex) != 0)
		throw CSystemException("wait for semaphore failed (lock)"); 
	while (_n < 1) 
	{
		if ((rc = pthread_cond_timedwait(&_cond, &_mutex, &abstime)))
		{
			if (rc == ETIMEDOUT) break;
			pthread_mutex_unlock(&_mutex);
			throw CSystemException("cannot wait for semaphore");
		}
	}
	if (rc == 0) --_n;
	pthread_mutex_unlock(&_mutex);
	return rc == 0;
}
Beispiel #13
0
void COutArchive::WriteLocalHeader(const CLocalItem &item)
{
  SeekTo(m_BasePosition);
  
  bool isZip64 = m_IsZip64 || item.PackSize >= 0xFFFFFFFF || item.UnPackSize >= 0xFFFFFFFF;
  
  WriteUInt32(NSignature::kLocalFileHeader);
  {
    Byte ver = item.ExtractVersion.Version;
    if (isZip64 && ver < NFileHeader::NCompressionMethod::kExtractVersion_Zip64)
      ver = NFileHeader::NCompressionMethod::kExtractVersion_Zip64;
    WriteByte(ver);
  }
  WriteByte(item.ExtractVersion.HostOS);
  WriteUInt16(item.Flags);
  WriteUInt16(item.CompressionMethod);
  WriteUInt32(item.Time);
  WriteUInt32(item.FileCRC);
  WriteUInt32(isZip64 ? 0xFFFFFFFF: (UInt32)item.PackSize);
  WriteUInt32(isZip64 ? 0xFFFFFFFF: (UInt32)item.UnPackSize);
  WriteUInt16((UInt16)item.Name.Length());
  {
    UInt16 localExtraSize = (UInt16)((isZip64 ? (4 + 16): 0) + item.LocalExtra.GetSize());
    if (localExtraSize > m_ExtraSize)
      throw CSystemException(E_FAIL);
  }
  WriteUInt16((UInt16)m_ExtraSize); // test it;
  WriteBytes((const char *)item.Name, item.Name.Length());

  UInt32 extraPos = 0;
  if (isZip64)
  {
    extraPos += 4 + 16;
    WriteUInt16(NFileHeader::NExtraID::kZip64);
    WriteUInt16(16);
    WriteUInt64(item.UnPackSize);
    WriteUInt64(item.PackSize);
  }

  WriteExtra(item.LocalExtra);
  extraPos += (UInt32)item.LocalExtra.GetSize();
  for (; extraPos < m_ExtraSize; extraPos++)
    WriteByte(0);

  m_OutBuffer.FlushWithCheck();
  MoveBasePosition(item.PackSize);
  SeekTo(m_BasePosition);
}
Beispiel #14
0
void CRegConfig::SetPlugin(TCHAR *pszName)
{
	TCHAR path[512];
	DWORD err, disp;

	CloseKey();

	_tcscpy(path, REGCONFIG_PLUGINSPATH);
	_tcscat(path, pszName);
	err = RegCreateKeyEx(
		HKEY_CURRENT_USER, path, 0, _T(""),
		REG_OPTION_NON_VOLATILE, 0, NULL, &m_hKey, &disp);
	if(ERROR_SUCCESS != err)
	{
		throw CSystemException(err, "Failed to create plugin's registry key.");
	}
}
Beispiel #15
0
void CRWLockImpl::readLockImpl()
{
	HANDLE h[2];
	h[0] = _mutex;
	h[1] = _readEvent;
	switch (WaitForMultipleObjects(2, h, TRUE, INFINITE))
	{
	case WAIT_OBJECT_0:
	case WAIT_OBJECT_0 + 1:
		++_readers;
		ResetEvent(_writeEvent);
		ReleaseMutex(_mutex);
		break;
	default:
		throw CSystemException("cannot lock reader/writer lock");
	}
}
Beispiel #16
0
static HRESULT ExtractGroupCommand(const UStringVector &arcPaths,
    bool showDialog, const UString &outFolder, bool testMode)
{
  HRESULT result;
  MY_TRY_BEGIN
  CREATE_CODECS

  CExtractOptions eo;
  eo.OutputDir = outFolder;
  eo.TestMode = testMode;

  CExtractCallbackImp *ecs = new CExtractCallbackImp;
  CMyComPtr<IFolderArchiveExtractCallback> extractCallback = ecs;
  
  ecs->Init();
  
  // eo.CalcCrc = options.CalcCrc;
  
  UStringVector arcPathsSorted;
  UStringVector arcFullPathsSorted;
  {
    NWildcard::CCensor acrCensor;
    for (int i = 0; i < arcPaths.Size(); i++)
      acrCensor.AddItem(true, arcPaths[i], false);
    EnumerateDirItemsAndSort(acrCensor, arcPathsSorted, arcFullPathsSorted);
  }
  
  CIntVector formatIndices;

  NWildcard::CCensor censor;
  censor.AddItem(true, L"*", false);
  
  bool messageWasDisplayed = false;
  result = ExtractGUI(codecs, formatIndices, arcPathsSorted, arcFullPathsSorted,
      censor.Pairs.Front().Head, eo, showDialog, messageWasDisplayed, ecs, g_HWND);
  if (result != S_OK)
  {
    if (result != E_ABORT && messageWasDisplayed)
      return E_FAIL;
    throw CSystemException(result);
  }
  return ecs->IsOK() ? S_OK : E_FAIL;
  MY_TRY_FINISH
  return result;
}
Beispiel #17
0
void CInArchive::SafeReadBytes(void *data, unsigned size)
{
  size_t processed = size;
  if (_inBufMode)
  {
    processed = _inBuffer.ReadBytes((Byte *)data, size);
    m_Position += processed;
  }
  else
  {
    HRESULT result = ReadStream(Stream, data, &processed);
    m_Position += processed;
    if (result != S_OK)
      throw CSystemException(result);
  }
  if (processed != size)
    throw CUnexpectEnd();
}
Beispiel #18
0
bool CRWLockImpl::tryReadLockImpl()
{
	HANDLE h[2];
	h[0] = _mutex;
	h[1] = _readEvent;
	switch (WaitForMultipleObjects(2, h, TRUE, 1))
	{
	case WAIT_OBJECT_0:
	case WAIT_OBJECT_0 + 1:
		++_readers;
		ResetEvent(_writeEvent);
		ReleaseMutex(_mutex);
		return true;
	case WAIT_TIMEOUT:
		return false;
	default:
		throw CSystemException("cannot lock reader/writer lock");
	}
}
Beispiel #19
0
HRESULT CompressFiles(
    const UString &arcPathPrefix,
    const UString &arcName,
    const UString &arcType,
    const UStringVector &names,
    bool email, bool showDialog, bool /* waitFinish */)
{


  //::MessageBoxW(NULL, (LPCWSTR)L"CompressCall2::CompressFiles", (LPCWSTR)L"mooo", MB_OK);

  HRESULT result;
  MY_TRY_BEGIN
  CREATE_CODECS

  CUpdateCallbackGUI callback;
  
  callback.Init();

  CUpdateOptions uo;
  uo.EMailMode = email;
  uo.SetAddActionCommand();

  CIntVector formatIndices;
  if (!codecs->FindFormatForArchiveType(arcType, formatIndices))
  {
    ErrorLangMessage(IDS_UNSUPPORTED_ARCHIVE_TYPE, 0x0200060D);
    return E_FAIL;
  }
  if (!uo.Init(codecs, formatIndices, arcPathPrefix + arcName))
  {
    ErrorLangMessage(IDS_UPDATE_NOT_SUPPORTED, 0x02000601);
    return E_FAIL;
  }

  NWildcard::CCensor censor;
  for (int i = 0; i < names.Size(); i++)
    censor.AddItem(true, names[i], false);

  //::MessageBoxW(NULL, (LPCWSTR)L"CompressCall2::CompressFiles2", (LPCWSTR)L"mooo", MB_OK);

  bool messageWasDisplayed = false;
  result = UpdateGUI(codecs, censor, uo, showDialog, messageWasDisplayed, &callback, g_HWND);

  //::MessageBoxW(NULL, (LPCWSTR)L"CompressCall2::CompressFiles3", (LPCWSTR)L"mooo", MB_OK);

  if (result != S_OK)
  {
    if (result != E_ABORT && messageWasDisplayed)
      return E_FAIL;
    throw CSystemException(result);
  }
  if (callback.FailedFiles.Size() > 0)
  {
    if (!messageWasDisplayed)
      throw CSystemException(E_FAIL);
    return E_FAIL;
  }
  MY_TRY_FINISH
  return S_OK;
}
Beispiel #20
0
void COutArchive::SeekTo(UInt64 offset)
{
  HRESULT res = m_Stream->Seek(offset, STREAM_SEEK_SET, NULL);
  if (res != S_OK)
    throw CSystemException(res);
}