string CoreUnix::GetDefaultMountPointPrefix () const { const char *envPrefix = getenv ("TRUECRYPT64_MOUNT_PREFIX"); if (envPrefix && !string (envPrefix).empty()) return envPrefix; if (FilesystemPath ("/media").IsDirectory()) return "/media/truecrypt64"; if (FilesystemPath ("/mnt").IsDirectory()) return "/mnt/truecrypt64"; return GetTempDirectory() + "/truecrypt64_mnt"; }
string CoreUnix::GetDefaultMountPointPrefix () const { const char *envPrefix = getenv ("CIPHERSHED_MOUNT_PREFIX"); if (envPrefix && !string (envPrefix).empty()) return envPrefix; if (FilesystemPath ("/media").IsDirectory()) return "/media/ciphershed"; if (FilesystemPath ("/mnt").IsDirectory()) return "/mnt/ciphershed"; return GetTempDirectory() + "/ciphershed_mnt"; }
ff::String ff::CreateTempFile(StringRef base, StringRef extension) { LockMutex crit(GCS_FILE_UTIL); // STATIC_DATA (pod) static UINT nUnique = 0; const int maxTries = 1000; String szFinal; static StaticString defaultBase(L"TempFile"); static StaticString defaultExt(L"tmp"); String szBase = base.size() ? szBase : defaultBase; String szExtension = extension.size() ? szExtension : defaultExt; for (int nTry = 0; nTry < maxTries; nTry++, nUnique++) { wchar_t szUnique[20]; _snwprintf_s(szUnique, _countof(szUnique), _TRUNCATE, L"%u", nUnique); ff::String szTry = GetTempDirectory(); AppendPathTail(szTry, szBase); szTry += szUnique; szTry += L"."; szTry += szExtension; if (!FileExists(szTry)) { szFinal = szTry; break; } } if (szFinal.size()) { nUnique++; // Create the file to reserve it File file; if (!file.OpenWrite(szFinal)) { szFinal.empty(); } } assertSz(szFinal.size(), L"Can't create a file in the temp folder"); return szFinal; }
std::string MakeUniqueTempDirectory() { std::string tmpStr = GetTempDirectory(); if (!tmpStr.empty() && *--tmpStr.end() != util::DIR_SEP) { tmpStr += util::DIR_SEP; } tmpStr += "usdir-XXXXXX"; std::vector<char> tmpChars(tmpStr.c_str(), tmpStr.c_str() + tmpStr.length() + 1); errno = 0; if (!mkdtemps_compat(tmpChars.data(), 0)) throw std::runtime_error(util::GetLastCErrorStr()); return tmpChars.data(); }
String UnixFileSystemBase::CreateTempFilename() { String prefix = Path::Combine(GetTempDirectory(), (String)(int)getpid() + "-"); Random rand(DefaultTraits::Hash(GetTimestamp())); static const char* cs = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; int cl = strlen(cs); while (true) { String tmp = String::Create(8); for (int i = 0; i < tmp.Length(); i++) tmp[i] = cs[rand.NextInt(cl)]; String result = prefix + tmp; struct stat attributes; if (stat(result.Ptr(), &attributes) == -1) return result; } }
void SessionImpl::SetEnvironmentVariables () { #if MIKTEX_WINDOWS string str; // Ghostscript Utils::SetEnvironmentString ("GSC", MIKTEX_GS_EXE); PathName root1 = GetSpecialPath(SpecialPath::CommonInstallRoot); PathName root2 = GetSpecialPath(SpecialPath::UserInstallRoot); PathName gsbase1 = root1; gsbase1 += "ghostscript"; gsbase1 += "base"; PathName gsbase2 = root2; gsbase2 += "ghostscript"; gsbase2 += "base"; str = gsbase1.Get(); if (gsbase1 != gsbase2) { str += PATH_DELIMITER; str += gsbase2.Get(); } PathName fonts1 = root1; fonts1 += "fonts"; PathName fonts2 = root2; fonts2 += "fonts"; str += PATH_DELIMITER; str += fonts1.Get(); if (fonts1 != fonts2) { str += PATH_DELIMITER; str += fonts2.Get(); } Utils::SetEnvironmentString ("MIKTEX_GS_LIB", str.c_str()); #endif PathName path = GetTempDirectory(); if (! HaveEnvironmentString("TEMPDIR") || IsMiKTeXPortable()) { Utils::SetEnvironmentString ("TEMPDIR", path.Get()); } if (! HaveEnvironmentString("TMPDIR") || IsMiKTeXPortable()) { Utils::SetEnvironmentString ("TMPDIR", path.Get()); } if (! HaveEnvironmentString("TEMP") || IsMiKTeXPortable()) { Utils::SetEnvironmentString ("TEMP", path.Get()); } if (! HaveEnvironmentString("TMP") || IsMiKTeXPortable()) { Utils::SetEnvironmentString ("TMP", path.Get()); } if (! HaveEnvironmentString("HOME")) { Utils::SetEnvironmentString ("HOME", GetHomeDirectory().Get()); } SetCWDEnv (); }
shared_ptr <VolumeInfo> CoreUnix::MountVolume (MountOptions &options) { CoalesceSlotNumberAndMountPoint (options); if (IsVolumeMounted (*options.Path)) throw VolumeAlreadyMounted (SRC_POS); Cipher::EnableHwSupport (!options.NoHardwareCrypto); shared_ptr <Volume> volume; while (true) { try { volume = OpenVolume ( options.Path, options.PreserveTimestamps, options.Password, options.Keyfiles, options.Protection, options.ProtectionPassword, options.ProtectionKeyfiles, options.SharedAccessAllowed, VolumeType::Unknown, options.UseBackupHeaders, options.PartitionInSystemEncryptionScope ); options.Password.reset(); } catch (SystemException &e) { if (options.Protection != VolumeProtection::ReadOnly && (e.GetErrorCode() == EROFS || e.GetErrorCode() == EACCES || e.GetErrorCode() == EPERM)) { // Read-only filesystem options.Protection = VolumeProtection::ReadOnly; continue; } throw; } break; } if (options.Path->IsDevice()) { if (volume->GetFile()->GetDeviceSectorSize() != volume->GetSectorSize()) throw ParameterIncorrect (SRC_POS); #if defined (TC_LINUX) if (volume->GetSectorSize() != TC_SECTOR_SIZE_LEGACY) { if (options.Protection == VolumeProtection::HiddenVolumeReadOnly) throw UnsupportedSectorSizeHiddenVolumeProtection(); if (options.NoKernelCrypto) throw UnsupportedSectorSizeNoKernelCrypto(); } #endif } // Find a free mount point for FUSE service MountedFilesystemList mountedFilesystems = GetMountedFilesystems (); string fuseMountPoint; for (int i = 1; true; i++) { stringstream path; path << GetTempDirectory() << "/" << GetFuseMountDirPrefix() << i; FilesystemPath fsPath (path.str()); bool inUse = false; foreach_ref (const MountedFilesystem &mf, mountedFilesystems) { if (mf.MountPoint == path.str()) { inUse = true; break; } } if (!inUse) { try { if (fsPath.IsDirectory()) fsPath.Delete(); throw_sys_sub_if (mkdir (path.str().c_str(), S_IRUSR | S_IXUSR) == -1, path.str()); fuseMountPoint = fsPath; break; } catch (...) { if (i > 255) throw TemporaryDirectoryFailure (SRC_POS, StringConverter::ToWide (path.str())); } } } try { FuseService::Mount (volume, options.SlotNumber, fuseMountPoint); } catch (...) { try { DirectoryPath (fuseMountPoint).Delete(); } catch (...) { } throw; } try { // Create a mount directory if a default path has been specified bool mountDirCreated = false; string mountPoint; if (!options.NoFilesystem && options.MountPoint) { mountPoint = *options.MountPoint; #ifndef TC_MACOSX if (mountPoint.find (GetDefaultMountPointPrefix()) == 0 && !options.MountPoint->IsDirectory()) { Directory::Create (*options.MountPoint); try { throw_sys_sub_if (chown (mountPoint.c_str(), GetRealUserId(), GetRealGroupId()) == -1, mountPoint); } catch (ParameterIncorrect&) { } mountDirCreated = true; } #endif } try { try { MountVolumeNative (volume, options, fuseMountPoint); } catch (NotApplicable&) { MountAuxVolumeImage (fuseMountPoint, options); } } catch (...) { if (mountDirCreated) remove (mountPoint.c_str()); throw; } } catch (...) { try { VolumeInfoList mountedVolumes = GetMountedVolumes (*options.Path); if (mountedVolumes.size() > 0) { shared_ptr <VolumeInfo> mountedVolume (mountedVolumes.front()); DismountVolume (mountedVolume); } } catch (...) { } throw; } VolumeInfoList mountedVolumes = GetMountedVolumes (*options.Path); if (mountedVolumes.size() != 1) throw ParameterIncorrect (SRC_POS); VolumeEventArgs eventArgs (mountedVolumes.front()); VolumeMountedEvent.Raise (eventArgs); return mountedVolumes.front(); }
// generates a random filename in the temp directory void GetTempFilename(string& tempFilename) { uint64_t value = 0; //char hostname[256]; #ifdef WIN32 value += _getpid(); #else timeval ft; gettimeofday(&ft, NULL); value += ((uint64_t) ft.tv_usec << 16) ^ ft.tv_sec ^ getpid(); #endif // seed the random number generator with supplied seed and time // define our set of random characters static const char letters[]= "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; //static const char letters[]= "abcdefghijklmnopqrstuvwxyz0123456789"; // get our temporary directory string tempDirectory; GetTempDirectory( tempDirectory ); // get host name //int ghnVal = gethostname( hostname, sizeof(hostname) ); //if (ghnVal == 0) //{ // tempFilename = tempDirectory + hostname + "XXXXXX"; //} //else //{ // tempFilename = tempDirectory + "XXXXXX"; //} tempFilename = tempDirectory + "XXXXXX"; bool filenameExists = true; unsigned int count = 0; unsigned int length = tempFilename.size(); while(static_cast<int>(count++) < MAX_TMP_TRYING_TIME) { uint64_t tempValue = value; // build our random filename for(unsigned int i = 0; i != 6; ++i) { tempFilename[length - 6 + i] = letters[tempValue % 62]; tempValue /= 62; } // check if the file exists filenameExists = CheckTempFile(tempFilename.c_str(), false); if (filenameExists) { #ifdef WIN32 value += 7777; #else gettimeofday(&ft, NULL); value += ((uint64_t) ft.tv_usec << 16) ^ ft.tv_sec ^ getpid(); #endif }else return; } // exceed the maximum trying time // report an error cout << "Can not create a temporary file: " << tempFilename << endl; exit(1); }