implement and use portable implementation of canonical file paths, see #909, this time against dev branch
This commit is contained in:
@@ -35,8 +35,54 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
#if BOOST_FILESYSTEM_VERSION < 3
|
||||
#warning Boost Installation with Filesystem3 missing, activating workaround
|
||||
#include <cstdio>
|
||||
#endif
|
||||
|
||||
|
||||
namespace boost {
|
||||
namespace filesystem {
|
||||
|
||||
// adapted from: http://stackoverflow.com/questions/1746136/how-do-i-normalize-a-pathname-using-boostfilesystem
|
||||
inline boost::filesystem::path portable_canonical(
|
||||
const boost::filesystem::path & relative_path,
|
||||
const boost::filesystem::path & current_path = boost::filesystem::current_path())
|
||||
{
|
||||
const boost::filesystem::path absolute_path = boost::filesystem::absolute(
|
||||
relative_path,
|
||||
current_path
|
||||
);
|
||||
|
||||
boost::filesystem::path canonical_path;
|
||||
for(
|
||||
boost::filesystem::path::const_iterator path_iterator = absolute_path.begin();
|
||||
path_iterator!=absolute_path.end();
|
||||
++path_iterator
|
||||
) {
|
||||
if( ".." == path_iterator->string() ) {
|
||||
// /a/b/.. is not necessarily /a if b is a symbolic link
|
||||
if( boost::filesystem::is_symlink(canonical_path) ) {
|
||||
canonical_path /= *path_iterator;
|
||||
} else if( ".." == canonical_path.filename() ) {
|
||||
// /a/b/../.. is not /a/b/.. under most circumstances
|
||||
// We can end up with ..s in our result because of symbolic links
|
||||
canonical_path /= *path_iterator;
|
||||
} else {
|
||||
// Otherwise it should be safe to resolve the parent
|
||||
canonical_path = canonical_path.parent_path();
|
||||
}
|
||||
} else if( "." == path_iterator->string() ) {
|
||||
// Ignore
|
||||
} else {
|
||||
// Just cat other path entries
|
||||
canonical_path /= *path_iterator;
|
||||
}
|
||||
}
|
||||
BOOST_ASSERT( canonical_path.is_absolute() );
|
||||
BOOST_ASSERT( boost::filesystem::exists( canonical_path ) );
|
||||
return canonical_path;
|
||||
}
|
||||
|
||||
#if BOOST_FILESYSTEM_VERSION < 3
|
||||
|
||||
inline path temp_directory_path() {
|
||||
char * buffer;
|
||||
buffer = tmpnam (NULL);
|
||||
@@ -48,10 +94,9 @@ inline path unique_path(const path&) {
|
||||
return temp_directory_path();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
#ifndef BOOST_FILESYSTEM_VERSION
|
||||
#define BOOST_FILESYSTEM_VERSION 3
|
||||
|
||||
Reference in New Issue
Block a user