// FIXME: could become a skins2 OS factory function or a vlc core function std::string ThemeLoader::getTmpDir( ) { #if defined( _WIN32 ) wchar_t *tmpdir = _wtempnam( NULL, L"vlt" ); if( tmpdir == NULL ) return ""; char* utf8 = FromWide( tmpdir ); free( tmpdir ); std::string tempPath( utf8 ? utf8 : "" ); free( utf8 ); return tempPath; #elif defined( __OS2__ ) char *tmpdir = tempnam( NULL, "vlt" ); if( tmpdir == NULL ) return ""; std::string tempPath( sFromLocale( tmpdir )); free( tmpdir ); return tempPath; #else char templ[] = "/tmp/vltXXXXXX"; char *tmpdir = mkdtemp( templ ); return std::string( tmpdir ? tmpdir : ""); #endif }
FILE *tmpfile(const wchar_t *prefix) { std::wstring sprefix = strutil::format(L"%s.%d.", prefix, GetCurrentProcessId()); wchar_t *tmpname = _wtempnam(0, sprefix.c_str()); std::shared_ptr<wchar_t> tmpname_p(tmpname, std::free); HANDLE fh = CreateFileW(prefixed_path(tmpname).c_str(), GENERIC_READ | GENERIC_WRITE, 0, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE, 0); if (fh == INVALID_HANDLE_VALUE) throw_error(tmpname, GetLastError()); int fd = _open_osfhandle(reinterpret_cast<intptr_t>(fh), _O_BINARY|_O_RDWR); if (fd == -1) { CloseHandle(fh); util::throw_crt_error("win32::tmpfile: open_osfhandle()"); } FILE *fp = _fdopen(fd, "w+"); if (!fp) { _close(fd); util::throw_crt_error("win32::tmpfile: _fdopen()"); } return fp; }
GF_EXPORT FILE *gf_temp_file_new(char ** const fileName) { FILE *res = NULL; #if defined(_WIN32_WCE) TCHAR pPath[MAX_PATH+1]; TCHAR pTemp[MAX_PATH+1]; if (!GetTempPath(MAX_PATH, pPath)) { pPath[0] = '.'; pPath[1] = '.'; } if (GetTempFileName(pPath, TEXT("git"), 0, pTemp)) res = _wfopen(pTemp, TEXT("w+b")); #elif defined(WIN32) res = tmpfile(); if (!res) { wchar_t tmp[MAX_PATH]; GF_LOG(GF_LOG_INFO, GF_LOG_CORE, ("[Win32] system failure for tmpfile(): 0x%08x\n", GetLastError())); /*tmpfile() may fail under vista ...*/ if (GetEnvironmentVariableW(L"TEMP", tmp, MAX_PATH)) { wchar_t tmp2[MAX_PATH], *t_file; char* mbs_t_file; gf_rand_init(GF_FALSE); swprintf(tmp2, MAX_PATH, L"gpac_%d_%08x_", _getpid(), gf_rand()); t_file = _wtempnam(tmp, tmp2); mbs_t_file = wcs_to_utf8(t_file); if (!mbs_t_file) return 0; res = gf_fopen(mbs_t_file, "w+b"); if (res) { gpac_file_handles--; if (fileName) { *fileName = gf_strdup(mbs_t_file); } else { GF_LOG(GF_LOG_WARNING, GF_LOG_CORE, ("[Win32] temporary file %s won't be deleted - contact the GPAC team\n", mbs_t_file)); } } gf_free(mbs_t_file); free(t_file); } } #else res = tmpfile(); #endif if (res) { gpac_file_handles++; } return res; }
/** * @return returns temporary filename. */ std::string digidoc::util::File::tempFileName() { #ifdef _WIN32 // requires TMP environment variable to be set wchar_t *fileName = _wtempnam(0, 0); // TODO: static buffer, not thread-safe if ( !fileName ) THROW_IOEXCEPTION("Failed to create a temporary file name."); #else char *fileName = tempnam(0, 0); if ( !fileName ) THROW_IOEXCEPTION("Failed to create a temporary file name."); #endif std::string path = decodeName(fileName); free(fileName); tempFiles.push(path); return path; }
int TempnamW( _Outptr_result_maybenull_z_ wchar_t **output ) { *output = NULL; #if defined(_MSC_VER) *output = _wtempnam(NULL,NULL); #else char *Path = tempnam(NULL,NULL); if(Path == NULL ) return -1; AsciiToUCS2( Path, output) ; free(Path); #endif if( *output == NULL) return -1; return 0; }
fs::path TempFolder::GetNewTempFolderPath() { #ifdef _WIN32 // The specified "c:\\temp" path will only be used // when the TMP environment variable is undefined // http://msdn.microsoft.com/en-us/library/hs3e7355.aspx wchar_t *tmp_name = _wtempnam( L"c:\\temp", L"unused" ); fs::path main_temp_folder = fs::path( tmp_name ).parent_path(); free( tmp_name ); #else // GCC bitches and moans if we use tempnam(), so // we'll just use the P_tmpdir macro. We can't use // that on Win because it points to the drive root there // instead of the system temp folder // http://www.delorie.com/gnu/docs/glibc/libc_295.html fs::path main_temp_folder = fs::path( P_tmpdir ); #endif return main_temp_folder / fs::path( FC_TEMP_FOLDER ) / fs::unique_path( UNIQUE_PATH_MODEL ); }