summaryrefslogtreecommitdiff
path: root/libs/pbd/filesystem.cc
diff options
context:
space:
mode:
authorCarl Hetherington <carl@carlh.net>2012-05-28 16:32:41 +0000
committerCarl Hetherington <carl@carlh.net>2012-05-28 16:32:41 +0000
commit7eb8a33910bfdab21b98d794dc731bc8e8557cb9 (patch)
tree8a22b28badb6f4bdfd9c9660c50f6e5c1605e8c9 /libs/pbd/filesystem.cc
parentd825f20a324dc05fae612b748c579c22df88a471 (diff)
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
Diffstat (limited to 'libs/pbd/filesystem.cc')
-rw-r--r--libs/pbd/filesystem.cc33
1 files changed, 33 insertions, 0 deletions
diff --git a/libs/pbd/filesystem.cc b/libs/pbd/filesystem.cc
index a4bf781802..819a044a7b 100644
--- a/libs/pbd/filesystem.cc
+++ b/libs/pbd/filesystem.cc
@@ -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