PathName SessionImpl::GetMyProgramFile(bool canonicalized) { // we do this once if (myProgramFile.Empty()) { #if defined(__APPLE__) CharBuffer<char> buf; uint32_t bufsize = buf.GetCapacity(); if (_NSGetExecutablePath(buf.GetData(), &bufsize) < 0) { buf.Reserve(bufsize); if (_NSGetExecutablePath(buf.GetData(), &bufsize) != 0) { MIKTEX_UNEXPECTED(); } } myProgramFile = buf.GetData(); #else string invocationName = initInfo.GetProgramInvocationName(); if (invocationName.empty()) { MIKTEX_FATAL_ERROR(T_("No invocation name has been set.")); } if (Utils::IsAbsolutePath(invocationName.c_str())) { myProgramFile = invocationName; } else if (invocationName.length() > 3 && (invocationName.substr(0, 2) == "./" || invocationName.substr(0, 3) == "../")) { myProgramFile = GetFullPath(invocationName.c_str()); } else if (!Utils::FindProgram(invocationName, myProgramFile)) { MIKTEX_FATAL_ERROR_2(T_("The invoked program could not be found in the PATH."), "invocationName", invocationName); } #endif myProgramFileCanon = myProgramFile; myProgramFileCanon.Canonicalize(); } if (canonicalized) { return myProgramFileCanon; } else { return myProgramFile; } }
// see Q246772 bool Utils::GetDefPrinter(string& printerName) { OSVERSIONINFOW osv; osv.dwOSVersionInfoSize = sizeof(OSVERSIONINFOW); GetVersionExW(&osv); if (osv.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) { #if defined(MIKTEX_SUPPORT_LEGACY_WINDOWS) unsigned long dwNeeded, dwReturned; EnumPrintersW(PRINTER_ENUM_DEFAULT, 0, 2, 0, 0, &dwNeeded, &dwReturned); if (dwNeeded == 0) { return false; } AutoGlobalMemory hMem(GlobalAlloc(GPTR, dwNeeded)); if (hMem.Get() == nullptr) { OUT_OF_MEMORY("GlobalAlloc"); } PRINTER_INFO_2W* ppi2 = reinterpret_cast<PRINTER_INFO_2W *>(hMem.Get()); if (!EnumPrintersW(PRINTER_ENUM_DEFAULT, 0, 2, reinterpret_cast<LPBYTE>(ppi2), dwNeeded, &dwNeeded, &dwReturned)) { return false; } size_t l = StrLen(ppi2->pPrinterName); if (l >= *pBufferSize) { *pBufferSize = l + 1; return false; } StringUtil::CopyString(pPrinterName, *pBufferSize, ppi2->pPrinterName); *pBufferSize = l + 1; return true; #else UNSUPPORTED_PLATFORM(); #endif } else { if (osv.dwPlatformId != VER_PLATFORM_WIN32_NT) { MIKTEX_UNEXPECTED(); } if (osv.dwMajorVersion >= 5) { CharBuffer<wchar_t> printerNameBuf; DWORD dwBufferSize = static_cast<DWORD>(printerNameBuf.GetCapacity()); BOOL bDone = GetDefaultPrinterW(printerNameBuf.GetData(), &dwBufferSize); if (!bDone) { if (::GetLastError() == ERROR_FILE_NOT_FOUND) { return false; } else { MIKTEX_FATAL_WINDOWS_ERROR("GetDefaultPrinterW"); } } else { printerName = WU_(printerNameBuf.GetData()); return true; } } else { #if defined(MIKTEX_SUPPORT_LEGACY_WINDOWS) wchar_t cBuffer[4096]; if (GetProfileStringW(L"windows", L"device", L",,,", cBuffer, 4096) <= 0) { return false; } Tokenizer tok(StringUtil::WideCharToUTF8(cBuffer), ","); if (tok.GetCurrent() == nullptr) { return false; } unsigned long l = static_cast<unsigned long>(StrLen(tok.GetCurrent())); if (l >= *pBufferSize) { *pBufferSize = l + 1; return false; } StringUtil::CopyString(pPrinterName, *pBufferSize, tok.GetCurrent()); *pBufferSize = l + 1; return true; #else UNSUPPORTED_PLATFORM(); #endif } } }
void TarExtractor::Extract (/*[in]*/ Stream * pStreamIn_, /*[in]*/ const PathName & destDir, /*[in]*/ bool makeDirectories, /*[in]*/ IExtractCallback * pCallback, /*[in]*/ const char * lpszPrefix) { try { pStreamIn = pStreamIn_; totalBytesRead = 0; traceStream->WriteFormattedLine ("libextractor", T_("extracting to %s (%s)"), Q_(destDir), (makeDirectories ? T_("make directories") : T_("don't make directories"))); size_t len; Header header; size_t prefixLen = (lpszPrefix == 0 ? 0 : StrLen(lpszPrefix)); unsigned fileCount = 0; bool checkHeader = true; CharBuffer<char> buffer; buffer.Reserve (1024 * 1024); while ((len = Read(&header, sizeof(header))) > 0) { // read next header if (len != sizeof(header)) { FATAL_EXTRACTOR_ERROR ("TarExtractor::Extract", T_("Invalid package archive file."), 0); } if (header.IsEndOfArchive()) { break; } if (checkHeader) { if (! header.Check()) { FATAL_EXTRACTOR_ERROR ("TarExtractor::Extract", T_("Invalid package archive file."), 0); } #if ! defined(MIKTEX_DEBUG) checkHeader = false; #endif } PathName dest = header.GetFileName(); size_t size = header.GetFileSize(); if (! header.IsNormalFile()) { if (header.GetType() == Header::LongName) { if (size >= BLOCKSIZE) { UNEXPECTED_CONDITION ("TarExtractor::Extract"); } char longNameData[BLOCKSIZE]; ReadBlock (longNameData); longNameData[size] = 0; longName = longNameData; haveLongName = true; } else { Skip (((size + sizeof(Header) - 1) / sizeof(Header)) * sizeof(Header)); } continue; } if (haveLongName) { dest = longName; haveLongName = false; } // skip directory prefix if (lpszPrefix != 0 && PathName::Compare(lpszPrefix, dest, prefixLen) == 0) { PathName tmp (dest); dest = tmp.Get() + prefixLen; } // make the destination path name PathName path (destDir); if (! makeDirectories) { dest.RemoveDirectorySpec (); } path += dest; // notify the client if (pCallback != 0) { pCallback->OnBeginFileExtraction (path.Get(), size); } // create the destination directory Directory::Create (PathName(path).RemoveFileSpec()); // remove the existing file if (File::Exists(path)) { File::Delete (path, true); } // extract the file FileStream streamOut (File::Open(path, FileMode::Create, FileAccess::Write, false)); size_t bytesRead = 0; while (bytesRead < size) { size_t remaining = size - bytesRead; size_t n = (remaining > buffer.GetCapacity() ? buffer.GetCapacity() : remaining); if (Read(buffer.GetBuffer(), n) != n) { FATAL_EXTRACTOR_ERROR ("TarExtractor::Extract", T_("Invalid package archive file."), 0); } streamOut.Write (buffer.Get(), n); bytesRead += n; } streamOut.Close (); // skip extra bytes if (bytesRead % sizeof(Header) > 0) { Skip (sizeof(Header) - bytesRead % sizeof(Header)); } fileCount += 1; // set time when the file was created time_t time = header.GetLastModificationTime(); File::SetTimes (path, time, time, time); #if 0 // set file attributes File::SetAttributes (path, todo); #endif // notify the client if (pCallback != 0) { pCallback->OnEndFileExtraction (0, size); } } traceStream->WriteFormattedLine ("libextractor", T_("extracted %u file(s)"), fileCount); } catch (const exception &) { traceStream->WriteFormattedLine ("libextractor", T_("%u bytes were read from the tar stream"), static_cast<unsigned>(totalBytesRead)); throw; } }