Add path_is_within to decide if a path is within a given

directory, taking symlinks into account, and use it to
decide whether a file is within the session folder.  Should
fix #4552.


git-svn-id: svn://localhost/ardour2/branches/3.0@12468 d708f5d6-7413-0410-9779-e7cbd77b26cf
This commit is contained in:
Carl Hetherington
2012-05-28 16:32:41 +00:00
parent d825f20a32
commit 7eb8a33910
8 changed files with 43 additions and 17 deletions

View File

@@ -61,7 +61,6 @@ std::string path_expand (std::string); /* single file path */
std::string search_path_expand (std::string); /* colon-separated search path */
std::string region_name_from_path (std::string path, bool strip_channels, bool add_channel_suffix = false, uint32_t total = 0, uint32_t this_one = 0);
bool path_is_paired (std::string path, std::string& pair_base);
bool inodes_same (const std::string &, const std::string &);
void compute_equal_power_fades (ARDOUR::framecnt_t nframes, float* in, float* out);

View File

@@ -33,6 +33,7 @@
#include "pbd/strsplit.h"
#include "pbd/shortpath.h"
#include "pbd/enumwriter.h"
#include "pbd/filesystem.h"
#include <glibmm/miscutils.h>
#include <glibmm/fileutils.h>
@@ -277,7 +278,7 @@ FileSource::find (Session& s, DataType type, const string& path, bool must_exist
++j;
while (j != hits.end()) {
if (inodes_same (*i, *j)) {
if (PBD::sys::inodes_same (*i, *j)) {
/* *i and *j are the same file; break out of the loop early */
break;
}

View File

@@ -4478,7 +4478,7 @@ Session::ensure_search_path_includes (const string& path, DataType type)
search_path = config.get_midi_search_path ();
break;
}
split (search_path, dirs, ':');
for (vector<string>::iterator i = dirs.begin(); i != dirs.end(); ++i) {
@@ -4488,7 +4488,7 @@ Session::ensure_search_path_includes (const string& path, DataType type)
On Windows, I think we could just do if (*i == path) here.
*/
if (inodes_same (*i, path)) {
if (PBD::sys::inodes_same (*i, path)) {
return;
}
}

View File

@@ -428,7 +428,7 @@ bool
Session::path_is_within_session (const std::string& path)
{
for (vector<space_and_path>::const_iterator i = session_dirs.begin(); i != session_dirs.end(); ++i) {
if (path.find ((*i).path) == 0) {
if (PBD::sys::path_is_within (i->path, path)) {
return true;
}
}

View File

@@ -745,18 +745,6 @@ double slider_position_to_gain_with_max (double g, double max_gain)
return slider_position_to_gain (g * max_gain/2.0);
}
/** @return true if files a and b have the same inode */
bool
inodes_same (const string& a, const string& b)
{
struct stat bA;
int const rA = stat (a.c_str(), &bA);
struct stat bB;
int const rB = stat (b.c_str(), &bB);
return (rA == 0 && rB == 0 && bA.st_ino == bB.st_ino);
}
extern "C" {
void c_stacktrace() { stacktrace (cerr); }
}

View File

@@ -254,6 +254,39 @@ get_absolute_path (const path & p)
return f->get_path ();
}
/** @return true if a and b have the same inode */
bool
inodes_same (const path& a, const path& b)
{
struct stat bA;
int const rA = stat (a.to_string().c_str(), &bA);
struct stat bB;
int const rB = stat (b.to_string().c_str(), &bB);
return (rA == 0 && rB == 0 && bA.st_ino == bB.st_ino);
}
/** Find out if `needle' is a file or directory within the
* directory `haystack'.
* @return true if it is.
*/
bool
path_is_within (path const & haystack, path needle)
{
while (1) {
if (inodes_same (haystack, needle)) {
return true;
}
needle = needle.branch_path ();
if (needle.to_string().empty() || needle.to_string() == "/") {
break;
}
}
return false;
}
} // namespace sys
} // namespace PBD

View File

@@ -205,6 +205,10 @@ std::string extension (const path& p);
path get_absolute_path (const path &);
bool path_is_within (const path &, path);
bool inodes_same (const path &, const path &);
} // namespace sys
} // namespace PBD

View File

@@ -139,6 +139,7 @@ def build(bld):
test/scalar_properties.cc
test/signals_test.cc
test/convert_test.cc
test/filesystem_test.cc
'''.split()
testobj.target = 'run-tests'
testobj.includes = obj.includes + ['test', '../pbd']