bool CopyFileTo(const tstring& sFrom, const tstring& sTo, bool bOverride) { if (IsFile(sTo) && bOverride) ::DeleteFile(convert_to_wstring(sTo).c_str()); return !!CopyFile(convert_to_wstring(sFrom).c_str(), convert_to_wstring(sTo).c_str(), true); }
tvector<tstring> ListDirectory(const tstring& sDirectory, bool bDirectories) { tvector<tstring> asResult; wchar_t szPath[MAX_PATH]; _swprintf(szPath, L"%s\\*", convert_to_wstring(sDirectory).c_str()); WIN32_FIND_DATA fd; HANDLE hFind = FindFirstFile(szPath, &fd); if (hFind != INVALID_HANDLE_VALUE) { int count = 0; do { if (!bDirectories && (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) continue; // Duh. if (wcscmp(fd.cFileName, L".") == 0 || wcscmp(fd.cFileName, L"..") == 0) continue; asResult.push_back(convert_from_wstring(fd.cFileName)); } while(FindNextFile(hFind, &fd)); FindClose(hFind); } return asResult; }
tstring GetAppDataDirectory(const tstring& sDirectory, const tstring& sFile) { size_t iSize; _wgetenv_s(&iSize, NULL, 0, L"APPDATA"); tstring sSuffix; sSuffix.append(sDirectory).append("\\").append(sFile); if (!iSize) return sSuffix; wchar_t* pszVar = (wchar_t*)malloc(iSize * sizeof(wchar_t)); if (!pszVar) return sSuffix; _wgetenv_s(&iSize, pszVar, iSize, L"APPDATA"); tstring sReturn = convert_from_wstring(pszVar); free(pszVar); CreateDirectory(convert_to_wstring(tstring(sReturn).append("\\").append(sDirectory)).c_str(), NULL); sReturn.append("\\").append(sSuffix); return sReturn; }
void rename(std::string const& inf, std::string const& newf, error_code& ec) { ec.clear(); #if TORRENT_USE_WSTRING && defined TORRENT_WINDOWS std::wstring f1 = convert_to_wstring(inf); std::wstring f2 = convert_to_wstring(newf); if (_wrename(f1.c_str(), f2.c_str()) < 0) #else std::string f1 = convert_to_native(inf); std::string f2 = convert_to_native(newf); if (::rename(f1.c_str(), f2.c_str()) < 0) #endif { ec.assign(errno, boost::system::get_generic_category()); return; } }
bool IsFile(const tstring& sPath) { WIN32_FIND_DATA fd; HANDLE hFind = FindFirstFile(convert_to_wstring(sPath).c_str(), &fd); if (hFind == INVALID_HANDLE_VALUE) return false; if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) return false; return true; }
tstring FindAbsolutePath(const tstring& sPath) { wchar_t szPath[MAX_PATH]; std::wstring swPath; if (!sPath.length()) swPath = L"."; else swPath = convert_to_wstring(sPath); GetFullPathName(swPath.c_str(), MAX_PATH, szPath, nullptr); return convert_from_wstring(szPath); }
void stat_file(std::string inf, file_status* s , error_code& ec, int flags) { ec.clear(); #ifdef TORRENT_WINDOWS // apparently windows doesn't expect paths // to directories to ever end with a \ or / if (!inf.empty() && (inf[inf.size() - 1] == '\\' || inf[inf.size() - 1] == '/')) inf.resize(inf.size() - 1); #endif #if TORRENT_USE_WSTRING && defined TORRENT_WINDOWS std::wstring f = convert_to_wstring(inf); #else std::string f = convert_to_native(inf); #endif #ifdef TORRENT_WINDOWS struct _stati64 ret; #if TORRENT_USE_WSTRING if (_wstati64(f.c_str(), &ret) < 0) #else if (_stati64(f.c_str(), &ret) < 0) #endif { ec.assign(errno, boost::system::get_generic_category()); return; } #else struct stat ret; int retval; if (flags & dont_follow_links) retval = ::lstat(f.c_str(), &ret); else retval = ::stat(f.c_str(), &ret); if (retval < 0) { ec.assign(errno, boost::system::get_generic_category()); return; } #endif // TORRENT_WINDOWS s->file_size = ret.st_size; s->atime = ret.st_atime; s->mtime = ret.st_mtime; s->ctime = ret.st_ctime; s->mode = ret.st_mode; }
directory::directory(std::string const& path, error_code& ec) : m_done(false) { ec.clear(); #ifdef TORRENT_WINDOWS // the path passed to FindFirstFile() must be // a pattern std::string f = convert_separators(path); if (!f.empty() && f[f.size()-1] != '\\') f += "\\*"; else f += "*"; #if TORRENT_USE_WSTRING #define FindFirstFile_ FindFirstFileW std::wstring p = convert_to_wstring(f); #else #define FindFirstFile_ FindFirstFileA std::string p = convert_to_native(f); #endif m_handle = FindFirstFile_(p.c_str(), &m_fd); if (m_handle == INVALID_HANDLE_VALUE) { ec.assign(GetLastError(), boost::system::get_system_category()); m_done = true; return; } #else memset(&m_dirent, 0, sizeof(dirent)); m_name[0] = 0; // the path passed to opendir() may not // end with a / std::string p = path; if (!path.empty() && path[path.size()-1] == '/') p.resize(path.size()-1); p = convert_to_native(p); m_handle = opendir(p.c_str()); if (m_handle == 0) { ec.assign(errno, boost::system::get_generic_category()); m_done = true; return; } // read the first entry next(ec); #endif }
bool IsDirectory(const tstring& sPath) { tstring sPathNoSep = sPath; while (sPathNoSep.substr(sPathNoSep.length()-1) == DIR_SEP) sPathNoSep = sPathNoSep.substr(0, sPathNoSep.length()-1); WIN32_FIND_DATA fd; HANDLE hFind = FindFirstFile(convert_to_wstring(sPathNoSep).c_str(), &fd); if (hFind == INVALID_HANDLE_VALUE) return false; if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) return true; return false; }
void remove(std::string const& inf, error_code& ec) { ec.clear(); #ifdef TORRENT_WINDOWS // windows does not allow trailing / or \ in // the path when removing files std::string pruned; if (inf[inf.size() - 1] == '/' || inf[inf.size() - 1] == '\\') pruned = inf.substr(0, inf.size() - 1); else pruned = inf; #if TORRENT_USE_WSTRING #define DeleteFile_ DeleteFileW #define RemoveDirectory_ RemoveDirectoryW std::wstring f = convert_to_wstring(pruned); #else #define DeleteFile_ DeleteFileA #define RemoveDirectory_ RemoveDirectoryA std::string f = convert_to_native(pruned); #endif if (DeleteFile_(f.c_str()) == 0) { if (GetLastError() == ERROR_ACCESS_DENIED) { if (RemoveDirectory_(f.c_str()) != 0) return; } ec.assign(GetLastError(), boost::system::get_system_category()); return; } #else // TORRENT_WINDOWS std::string f = convert_to_native(inf); if (::remove(f.c_str()) < 0) { ec.assign(errno, boost::system::get_generic_category()); return; } #endif // TORRENT_WINDOWS }
void create_directory(std::string const& f, error_code& ec) { ec.clear(); #if defined TORRENT_WINDOWS && TORRENT_USE_WSTRING #define CreateDirectory_ CreateDirectoryW std::wstring n = convert_to_wstring(f); #else #define CreateDirectory_ CreateDirectoryA std::string n = convert_to_native(f); #endif #ifdef TORRENT_WINDOWS if (CreateDirectory_(n.c_str(), 0) == 0 && GetLastError() != ERROR_ALREADY_EXISTS) ec.assign(GetLastError(), boost::system::get_system_category()); #else int ret = mkdir(n.c_str(), 0777); if (ret < 0 && errno != EEXIST) ec.assign(errno, boost::system::get_generic_category()); #endif }
void Alert(const tstring& sMessage) { MessageBox(NULL, convert_to_wstring(sMessage).c_str(), L"Alert", MB_ICONWARNING|MB_OK); }
void OpenExplorer(const tstring& sDirectory) { ShellExecute(NULL, L"open", convert_to_wstring(sDirectory).c_str(), NULL, NULL, SW_SHOWNORMAL); }
void OpenBrowser(const tstring& sURL) { ShellExecute(NULL, L"open", convert_to_wstring(sURL).c_str(), NULL, NULL, SW_SHOWNORMAL); }
void DebugPrint(const char* pszText) { OutputDebugString(convert_to_wstring(pszText).c_str()); }
void copy_file(std::string const& inf, std::string const& newf, error_code& ec) { ec.clear(); #if TORRENT_USE_WSTRING && defined TORRENT_WINDOWS #define CopyFile_ CopyFileW std::wstring f1 = convert_to_wstring(inf); std::wstring f2 = convert_to_wstring(newf); #else #define CopyFile_ CopyFileA std::string f1 = convert_to_native(inf); std::string f2 = convert_to_native(newf); #endif #ifdef TORRENT_WINDOWS if (CopyFile_(f1.c_str(), f2.c_str(), false) == 0) ec.assign(GetLastError(), boost::system::get_system_category()); #elif defined __APPLE__ && defined __MACH__ && MAC_OS_X_VERSION_MIN_REQUIRED >= 1050 // this only works on 10.5 copyfile_state_t state = copyfile_state_alloc(); if (copyfile(f1.c_str(), f2.c_str(), state, COPYFILE_ALL) < 0) ec.assign(errno, boost::system::get_generic_category()); copyfile_state_free(state); #else int infd = ::open(inf.c_str(), O_RDONLY); if (infd < 0) { ec.assign(errno, boost::system::get_generic_category()); return; } // rely on default umask to filter x and w permissions // for group and others // TODO: copy the mode from the source file int permissions = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH; int outfd = ::open(newf.c_str(), O_WRONLY | O_CREAT, permissions); if (outfd < 0) { close(infd); ec.assign(errno, boost::system::get_generic_category()); return; } char buffer[4096]; for (;;) { int num_read = read(infd, buffer, sizeof(buffer)); if (num_read == 0) break; if (num_read < 0) { ec.assign(errno, boost::system::get_generic_category()); break; } int num_written = write(outfd, buffer, num_read); if (num_written < num_read) { ec.assign(errno, boost::system::get_generic_category()); break; } if (num_read < int(sizeof(buffer))) break; } close(infd); close(outfd); #endif // TORRENT_WINDOWS }
void CreateDirectoryNonRecursive(const tstring& sPath) { CreateDirectory(convert_to_wstring(sPath).c_str(), NULL); }
void CreateMinidump(void* pInfo, tchar* pszDirectory) { #ifndef _DEBUG time_t currTime = ::time( NULL ); struct tm * pTime = ::localtime( &currTime ); wchar_t szModule[MAX_PATH]; ::GetModuleFileName( NULL, szModule, sizeof(szModule) / sizeof(tchar) ); wchar_t *pModule = wcsrchr( szModule, '.' ); if ( pModule ) *pModule = 0; pModule = wcsrchr( szModule, '\\' ); if ( pModule ) pModule++; else pModule = L"unknown"; wchar_t szFileName[MAX_PATH]; _snwprintf( szFileName, sizeof(szFileName) / sizeof(tchar), L"%s_%d.%.2d.%2d.%.2d.%.2d.%.2d_%d.mdmp", pModule, pTime->tm_year + 1900, pTime->tm_mon + 1, pTime->tm_mday, pTime->tm_hour, pTime->tm_min, pTime->tm_sec, g_iMinidumpsWritten++ ); HANDLE hFile = CreateFile( convert_to_wstring(GetAppDataDirectory(pszDirectory, convert_from_wstring(szFileName))).c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL ); if( ( hFile != NULL ) && ( hFile != INVALID_HANDLE_VALUE ) ) { MINIDUMP_EXCEPTION_INFORMATION mdei; mdei.ThreadId = GetCurrentThreadId(); mdei.ExceptionPointers = (EXCEPTION_POINTERS*)pInfo; mdei.ClientPointers = FALSE; MINIDUMP_CALLBACK_INFORMATION mci; mci.CallbackRoutine = NULL; mci.CallbackParam = 0; MINIDUMP_TYPE mdt = (MINIDUMP_TYPE)(MiniDumpWithIndirectlyReferencedMemory | MiniDumpScanMemory); BOOL rv = MiniDumpWriteDump( GetCurrentProcess(), GetCurrentProcessId(), hFile, mdt, (pInfo != 0) ? &mdei : 0, 0, &mci ); if( rv ) { // Success... message to user? } CloseHandle( hFile ); } #endif }