bool RequestHandler::handle_request(const Http::Request& req, Http::Reply& rep) { using namespace Http; if (!enabled_) { fill_reply_disabled(rep); return true; } try { const PlaylistID playlist_id = getPlaylistID(req.uri); if (IPlaylistUpdateManager* playlist_update_manager = dynamic_cast<IPlaylistUpdateManager*>(&aimp_manager_)) { playlist_update_manager->lockPlaylist(playlist_id); } auto unlock_playlist = [this] (PlaylistID playlist_id) { if (IPlaylistUpdateManager* playlist_update_manager = dynamic_cast<IPlaylistUpdateManager*>(&aimp_manager_)) { playlist_update_manager->unlockPlaylist(playlist_id); } }; ON_BLOCK_EXIT(unlock_playlist, playlist_id); for (auto field_it : req.mpfd_parser->GetFieldsMap()) { const MPFD::Field& field_const = *field_it.second; MPFD::Field& field = const_cast<MPFD::Field&>(field_const); switch (field.GetType()) { case MPFD::Field::FileType: { const std::wstring filename = StringEncoding::utf8_to_utf16( field.GetFileName() ); const fs::wpath path = fs::path(field.GetTempFileName()).parent_path() / filename; if (!fileTypeSupported(path.extension().native(), aimp_manager_)) { continue; } fs::copy_file(field.GetTempFileName(), path, fs::copy_option::overwrite_if_exists); // can't use rename here since parser will close file in Field's dtor. aimp_manager_.addFileToPlaylist(path, playlist_id); // we should not erase file since AIMP will use it. //fs::remove(path); break; } case MPFD::Field::TextType: { aimp_manager_.addURLToPlaylist(field.GetTextTypeContent(), playlist_id); break; } default: assert(!"unexpected type"); break; } } rep = Reply::stock_reply(Reply::ok); } catch (MPFD::Exception&) { rep = Reply::stock_reply(Reply::bad_request); } catch (std::exception& e) { (void)e; rep = Reply::stock_reply(Reply::forbidden); } return true; }
/** * Construct a new host folder PIDL with the fields initialised. */ inline winapi::shell::pidl::cpidl_t create_host_itemid( const std::wstring& host, const std::wstring& user, const boost::filesystem::wpath& path, int port, const std::wstring& label=std::wstring()) { // We create the item on the stack and then clone it into // a CoTaskMemAllocated pidl when we return it as a cpidl_t detail::host_item_template item; std::memset(&item, 0, sizeof(item)); item.id.cb = sizeof(item.id); item.id.dwFingerprint = detail::host_item_id::FINGERPRINT; #pragma warning(push) #pragma warning(disable:4996) host.copy(item.id.wszHost, MAX_HOSTNAME_LENZ); item.id.wszHost[MAX_HOSTNAME_LENZ - 1] = wchar_t(); user.copy(item.id.wszUser, MAX_USERNAME_LENZ); item.id.wszUser[MAX_USERNAME_LENZ - 1] = wchar_t(); path.string().copy(item.id.wszPath, MAX_PATH_LENZ); item.id.wszPath[MAX_PATH_LENZ - 1] = wchar_t(); label.copy(item.id.wszLabel, MAX_LABEL_LENZ); item.id.wszLabel[MAX_LABEL_LENZ - 1] = wchar_t(); #pragma warning(pop) item.id.uPort = boost::numeric_cast<USHORT>(port); assert(item.terminator.cb == 0); return winapi::shell::pidl::cpidl_t( reinterpret_cast<PCITEMID_CHILD>(&item)); }
void torrent_handle::rename_file(int index, fs::wpath const& new_name) const { INVARIANT_CHECK; std::string utf8; wchar_utf8(new_name.string(), utf8); TORRENT_FORWARD(rename_file(index, utf8)); }
inline std::wstring wfname(const boost::filesystem::wpath & path) { #if BOOST_WINDOWS #if BOOST_FILESYSTEM_VERSION >= 3 return path.native(); #else return path.external_file_string(); #endif #else #if BOOST_FILESYSTEM_VERSION >= 3 return deutf8(path.native()); #else return deutf8(path.external_file_string()); #endif #endif }
void torrent_handle::move_storage( fs::wpath const& save_path) const { INVARIANT_CHECK; std::string utf8; wchar_utf8(save_path.string(), utf8); TORRENT_FORWARD(move_storage(utf8)); }
void appendPathToPathEnvironmentVariable(boost::filesystem::wpath path) { const LPCTSTR path_env = L"PATH"; const DWORD buffer_size = GetEnvironmentVariable(path_env, nullptr, 0); if (buffer_size > 0) { std::wstring original_path(buffer_size - 1, '\0'); GetEnvironmentVariable(path_env, const_cast<LPWSTR>(original_path.c_str()), buffer_size); std::wstring new_path = path.normalize().native() + L";" + original_path; SetEnvironmentVariable(path_env, new_path.c_str()); } }
void appendPathToPathEnvironmentVariable(boost::filesystem::wpath path) { const LPCTSTR path_env = L"PATH"; const DWORD buffer_size = GetEnvironmentVariable(path_env, nullptr, 0); if (buffer_size > 0) { std::wstring value(buffer_size - 1, '\0'); GetEnvironmentVariable(path_env, const_cast<LPWSTR>(value.c_str()), buffer_size); value += ';'; value += path.normalize().native(); SetEnvironmentVariable( path_env, value.c_str() ); } }
void AIMPControlPlugin::ensureWorkDirectoryExists() { namespace fs = boost::filesystem; // check Plugins directory first, use it if it's writable. const fs::wpath plugins_subdirectory = getPluginDirectoryPath( getAimpPluginsPath() ), profile_subdirectory = getPluginDirectoryPath( getAimpProfilePath() ); if ( isDirectoryWriteEnabled(plugins_subdirectory) ) { plugin_work_directory_ = plugins_subdirectory; } else if ( isDirectoryWriteEnabled(profile_subdirectory) ) { plugin_work_directory_ = profile_subdirectory; } else { plugin_work_directory_ = plugins_subdirectory; // set work directory in any case. using namespace StringEncoding; // work directory is not accessible for writing or does not exist. // TODO: send log to aimp internal logger. BOOST_LOG_SEV(logger(), error) << "Neither \"" << utf16_to_system_ansi_encoding_safe( plugins_subdirectory.native() ) << "\", nor \"" << utf16_to_system_ansi_encoding_safe( profile_subdirectory.native() ) << "\" are accessible for writing. Use plugins subdirectory as work directory."; } }
boost::filesystem::wpath Platform::resolveShortcut(const boost::filesystem::wpath& path_to_shortcut) { try { CComPtr<IShellLink> psl; HRESULT hr = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (void**) &psl); if (FAILED(hr)) { throw_GetLastError("resolveShortcut (CoCreateInstance(CLSID_ShellLink))"); } CComQIPtr<IPersistFile> ppf(psl); if (!ppf) throw std::runtime_error("IPersistFile"); if (FAILED(ppf->Load(path_to_shortcut.file_string().c_str(), STGM_READ))) throw_GetLastError("resolveShortcut (IPersistFile::Load)"); WCHAR resPath[MAX_PATH]; if (FAILED(psl->GetPath(resPath, MAX_PATH, NULL, 0 /*todo: might need a flag here*/ ))) throw_GetLastError("resolveShortcut (IShellLink::GetPath)"); return boost::filesystem::wpath(resPath); } catch (const std::exception& e) { throw boost::filesystem::wfilesystem_error(e.what(), path_to_shortcut, boost::system::errc::make_error_code(boost::system::errc::bad_address)); } }
void load_app_config(const boost::filesystem::wpath &_path) { assert(!config_); if (!fs::exists(_path)) { config_.reset(new app_config()); return; } pt::ptree options; pt::ini_parser::read_ini(_path.string(), Out options); const auto disable_server_history = options.get<bool>("history.disable_server_history", false); auto forced_dpi = options.get<int>("gui.force_dpi", 0); if (valid_dpi_values().count(forced_dpi) == 0) { forced_dpi = 0; } config_.reset(new app_config(!disable_server_history, forced_dpi)); }
static int run(int argc, wchar_t** argv){ scoped_message message; try{ if(argc == 0){ message<<L"Error: Zero arguments."<<endl; return 1; }else if(argc != 2){ message<<L"Usage: "<<fs::system_complete(argv[0]).leaf()<<L" <filename>"<<endl<<endl; return 1; } const fs::wpath source_file = fs::system_complete(argv[1]); fs::wpath target_dir = fs::system_complete(argv[0]).branch_path(); if(!fs::exists(source_file)){ message<<source_file<<endl<<L"File not found."<<endl; return 1; } archive_type content = read_archive_content_sd7(source_file); if(content == R_OTHER){ content = read_archive_content_sdz(source_file); } if(content == R_OTHER){ message<<L"'"<<source_file.leaf()<<L"' is not a valid map/mod " L"it may be corrupted, try to redownload it."<<endl; return 1; } //for(fs::wpath test_path = source_file; test_path.has_root_directory(); ){ // if(fs::equivalent(test_path, target_dir)){ // message<<L"'"<<source_file.leaf()<<L"' already exists in the Spring directory."<<endl; // return 1; // } // test_path = test_path.branch_path(); //} if(content == R_MAP){ //message<<"isMap: "<<filename<<endl; target_dir /= L"maps"; }else if(content == R_MOD){ //message<<"isMod: "<<filename<<endl; target_dir /= L"mods"; }else{ assert(false); } if(!fs::exists(target_dir)){ message<<L"The target directory '"<<target_dir<<L"' doesn't exist."<<endl; return 1; } // stash existing files away if (fs::exists(target_dir / source_file.leaf())) { int i = 0; fs::wpath target_test; do { std::wstring stashed = (source_file.leaf() + L".old"); if (i > 0) { std::wstringstream tmp; tmp << i; stashed += L"."+tmp.str(); } target_test = target_dir / stashed; ++i; } while (fs::exists(target_test)); fs::rename(target_dir / source_file.leaf(), target_test); message << "File with same name found. It has been moved to " << endl << target_test << endl << endl; } target_dir /= source_file.leaf(); fs::rename(source_file, target_dir); message<<L"The "<<(content == R_MAP? L"map '" : L"mod '")<<source_file.leaf() <<L"' has been saved succesfully to '"<<target_dir.branch_path()<<L"'."<<endl <<L"Use the reload mods/maps button in the lobby to make Spring find it."<<endl; }catch(fs::filesystem_wpath_error& error){ fs::errno_type error_nr = fs::lookup_errno(error.system_error()); message<<L"Cannot move file: "; switch(error_nr){ case EPERM: case EACCES:{ message<<L"Permission denied."; break; } case ENOSPC:{ message<<L"Not enough free disk space."; break; } case ENOENT:{ message<<L"Invalid path."; break; } case EROFS:{ message<<L"Target path is read only."; break; } case EEXIST:{ message<<L"Target folder already contains a file named '"<<error.path1().leaf()<<L"'."; break; } default:{ message<<strerror(error_nr)<<L"."; } } message<<endl<<endl; message<<L"Source file: "<<error.path1()<<endl; message<<L"Target folder: "<<error.path2().branch_path()<<endl; }catch(fs::filesystem_error& error){ message<<L"Filesystem error in: "<<error.what()<<endl; } catch(std::exception& error){ message<<L"Found an exception with: "<<error.what()<<endl; } catch(...){ message<<L"Found an unknown exception."<<endl; } return 0; }
bool Platform::pathIsSystem(const boost::filesystem::wpath& path_to_investigate) { DWORD attr = GetFileAttributesW(path_to_investigate.file_string().c_str()); if (attr == INVALID_FILE_ATTRIBUTES) return false; return (attr & FILE_ATTRIBUTE_SYSTEM); }
static pattern_type split_pattern(boost::filesystem::wpath path) { if (boost::filesystem::is_directory(path)) return pattern_type(path, _T("")); return pattern_type(path.branch_path(), path.filename()); }
static std::wstring get_filename(boost::filesystem::wpath path) { return path.filename(); }
static boost::filesystem::wpath get_path(boost::filesystem::wpath path) { return path.branch_path(); }
icon::icon(boost::filesystem::wpath& path) { icon_.reset( reinterpret_cast<HICON>( LoadImageW(NULL,path.native().c_str(),IMAGE_ICON,0,0,LR_DEFAULTSIZE | LR_LOADFROMFILE))); }
bool Platform::pathIsShortcut(const boost::filesystem::wpath& path_to_investigate) { std::string ext = FB::wstring_to_utf8(path_to_investigate.extension()); std::transform(ext.begin(), ext.end(), ext.begin(), ::tolower); return (ext == ".lnk"); }
bool Platform::pathIsHidden(const boost::filesystem::wpath& path_to_investigate) { // Same in Linux and Mac FYI std::wstring fn = path_to_investigate.filename(); return (fn.empty() || fn[0] == L'.'); }