mirror of
https://github.com/rakshasa/rtorrent.git
synced 2026-10-06 22:29:23 +00:00
committed by
Jari Sundell
parent
cfb9e083d2
commit
31196ff5f0
@@ -3,6 +3,7 @@
|
||||
#include "globals.h"
|
||||
|
||||
#include <cstdlib>
|
||||
#include <stdlib.h>
|
||||
#include <torrent/exceptions.h>
|
||||
|
||||
rpc::ip_table_list ip_tables;
|
||||
@@ -28,3 +29,32 @@ expand_path(const std::string& path) {
|
||||
|
||||
return path;
|
||||
}
|
||||
|
||||
// Resolves a path to a canonical one with no symlinks or relative components,
|
||||
// so it can safely be handed to an external script. Returns an empty string if
|
||||
// the path does not name an existing file or directory.
|
||||
std::string
|
||||
resolve_path(const std::string& path) {
|
||||
if (path.empty())
|
||||
return std::string();
|
||||
|
||||
char* resolved = ::realpath(expand_path(path).c_str(), nullptr);
|
||||
|
||||
if (resolved == nullptr)
|
||||
return std::string();
|
||||
|
||||
std::string result(resolved);
|
||||
std::free(resolved);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
std::string
|
||||
resolve_path_or_throw(const std::string& path) {
|
||||
auto result = resolve_path(path);
|
||||
|
||||
if (result.empty())
|
||||
throw torrent::input_error("Could not resolve path: '" + path + "'.");
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user