int32_t CFileSystemServiceImpl::beginWatchFolder(const ::std::wstring& dir, bool recursive, const ::std::wstring& extraInfo) { PClient_JSFS bclient = this->bclient.lock(); if (!bclient) { throw BException(EX_CANCELLED, L"Application already stopped"); } std::wstring watchDirectory = makeValidPath(dir); PWatchDir existingWatcher; for (map<int32_t, PWatchDir>::iterator it = watchers.begin(); it != watchers.end(); it++) { PWatchDir w = (*it).second; wstring wdir = w->getDirectory(); if (_wcsicmp(wdir.c_str(), watchDirectory.c_str()) == 0) { existingWatcher = w; break; } } if (!existingWatcher) { existingWatcher = PWatchDir(new CWatchDir(bclient, ++nextNotifyId, extraInfo, makeValidPath(dir), recursive)); existingWatcher->start(); } int32_t handle = existingWatcher->getWatchHandle(); watchers[handle] = existingWatcher; return handle; }
BINLINE void BMessageHeader::read(BBuffer& bbuf) { bbuf.setByteOrder(BBIG_ENDIAN); bool cmpr = bbuf.setCompressInteger(false); bbuf.serialize(magic); switch(magic) { case BMAGIC_BINARY_STREAM: break; case BMAGIC_BINARY_STREAM_LE: magic = BMAGIC_BINARY_STREAM; bbuf.setByteOrder(BLITTLE_ENDIAN); break; default: throw BException(BExceptionC::CORRUPT, L"Stream must start with BYPS or SPYB"); } bbuf.serialize(error); bbuf.serialize(flags); if (flags & BHEADER_FLAG_BYPS_VERSION) { bbuf.serialize(bversion); } bbuf.serialize(version); targetId.serialize(bbuf, bversion); bbuf.serialize(messageId); bbuf.setCompressInteger(cmpr); if (bversion >= BHEADER_BYPS_VERSION_WITH_SESSIONID) { sessionId = BTargetId::readSessionId(bbuf); } }
void shellExecute(const wstring& fileToOpen, const wstring& params) { SHELLEXECUTEINFO execInfo = {0}; execInfo.cbSize = sizeof(execInfo); execInfo.lpVerb = L"open"; execInfo.nShow = SW_SHOW; execInfo.lpFile = fileToOpen.c_str(); execInfo.lpParameters = params.c_str(); execInfo.fMask = SEE_MASK_NOCLOSEPROCESS; if (!::ShellExecuteEx(&execInfo)) { DWORD err = ::GetLastError(); throw CFileSystemServiceImpl::createException(L"Failed to execute " + fileToOpen, err); } DWORD wait = ::WaitForSingleObject(execInfo.hProcess, INFINITE); DWORD err = ::GetLastError(); ::CloseHandle(execInfo.hProcess); if (wait == WAIT_TIMEOUT) { throw BException(EX_TIMEOUT, L"Timeout while waiting for " + args->at(0)); } else if (wait != WAIT_OBJECT_0) { throw CFileSystemServiceImpl::createException(L"Error while waiting for " + args->at(0), err); } }
BException CFileSystemServiceImpl::createException(const wstring& msg, DWORD winErrorCode) { LPWSTR lpMsgBuf = NULL; FormatMessageW( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, winErrorCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPWSTR)&lpMsgBuf, 0, NULL ); wstring serr = lpMsgBuf; if (serr.length()) { wchar_t c = 0; while ((c = serr[serr.length()-1]), c == L'\n' || c == L'\r') { serr.resize(serr.length()-1); } } LocalFree(lpMsgBuf); return BException(BEXCEPTION_CODE_MIN + winErrorCode, msg, serr); }
wstring CFileSystemServiceImpl::checkValidEncoding(const wstring& encoding) { if (encoding == L"UTF-8") return encoding; if (encoding == L"UTF-16LE") return encoding; if (encoding == L"UNICODE") return encoding; wstringstream wss; wss << L"Unsupported encoding=" << encoding << L", must be UTF-8, UTF-16LE or UNICODE."; throw BException(EX_INTERNAL, wss.str()); //return encoding; }
void CFileSystemServiceImpl::internalExecute(const PArrayString& args, const PExecuteOptions& opts, bool notify) { if (!args || args->length() == 0) throw BException(EX_INTERNAL, L"Parameter args must not be empty"); PRunnable job(new WaitForProcessExit_Job(shared_from_this(), args, opts, notify)); if (notify) { tpool->execute(job); } else { job->run(); } }
virtual void validate(const AResult & ares) override { auto &d = static_cast<const g_type&>(ares.get_taskdata()).get_const(); if (std::is_sorted(d.cbegin(), d.cend()) != true) throw BException("sorting check failed"); }
void createProcess(const wstring& fileToOpen, const wstring& params, const wstring& standardInput, pwstring pstandardOutput, pwstring pstandardError) { STARTUPINFO startupInfo = {0}; PROCESS_INFORMATION processInfo = {0}; startupInfo.cb = sizeof(startupInfo); HANDLE hWriteStdinFinished = NULL; HANDLE hReadStdoutFinished = NULL; HANDLE hReadStderrFinished = NULL; if (standardInput.size()) { hWriteStdinFinished = ::CreateEvent(NULL, TRUE, FALSE, NULL); byps_ptr<WriteStringToPipe_Job> job(new WriteStringToPipe_Job(standardInput, hWriteStdinFinished)); startupInfo.hStdInput = job->start(jsfs->tpool); startupInfo.dwFlags |= STARTF_USESTDHANDLES; } if (pstandardOutput) { hReadStdoutFinished = ::CreateEvent(NULL, TRUE, FALSE, NULL); byps_ptr<ReadStringFromPipe_Job> job(new ReadStringFromPipe_Job(pstandardOutput, hReadStdoutFinished)); startupInfo.hStdOutput = job->start(jsfs->tpool); startupInfo.dwFlags |= STARTF_USESTDHANDLES; } if (pstandardError) { hReadStderrFinished = ::CreateEvent(NULL, TRUE, FALSE, NULL); byps_ptr<ReadStringFromPipe_Job> job(new ReadStringFromPipe_Job(pstandardError, hReadStderrFinished)); startupInfo.hStdError = job->start(jsfs->tpool); startupInfo.dwFlags |= STARTF_USESTDHANDLES; } DWORD cmdlen = fileToOpen.length() + 1 + params.length() + 1; LPWSTR cmd = new WCHAR[cmdlen]; wcscpy_s(cmd, cmdlen, fileToOpen.c_str()); wcscat_s(cmd, cmdlen, L" "); wcscat_s(cmd, cmdlen, params.c_str()); BOOL succ = ::CreateProcess(NULL, cmd, NULL, NULL, TRUE, CREATE_NO_WINDOW, NULL, NULL, &startupInfo, &processInfo); DWORD err = ::GetLastError(); delete[] cmd; if (startupInfo.hStdInput) ::CloseHandle(startupInfo.hStdInput); if (startupInfo.hStdOutput) ::CloseHandle(startupInfo.hStdOutput); if (startupInfo.hStdError) ::CloseHandle(startupInfo.hStdError); if (!succ) { throw CFileSystemServiceImpl::createException(L"Failed to execute " + fileToOpen, err); } HANDLE waitHandles[4] = { 0 }; DWORD nbOfHandles = 0; waitHandles[nbOfHandles++] = processInfo.hProcess; if (hReadStdoutFinished) waitHandles[nbOfHandles++] = hReadStdoutFinished; if (hReadStderrFinished) waitHandles[nbOfHandles++] = hReadStderrFinished; if (hWriteStdinFinished) waitHandles[nbOfHandles++] = hWriteStdinFinished; DWORD wait = ::WaitForMultipleObjects(nbOfHandles, waitHandles, TRUE, INFINITE); err = ::GetLastError(); ::CloseHandle(processInfo.hProcess); ::CloseHandle(processInfo.hThread); if (hWriteStdinFinished) ::CloseHandle(hWriteStdinFinished); if (hReadStdoutFinished) ::CloseHandle(hReadStdoutFinished); if (hReadStderrFinished) ::CloseHandle(hReadStderrFinished); if (wait == WAIT_TIMEOUT) { throw BException(EX_TIMEOUT, L"Timeout while waiting for " + args->at(0)); } else if (wait == WAIT_FAILED) { throw CFileSystemServiceImpl::createException(L"Error while waiting for " + args->at(0), err); } }