From e26e59b00634d5a66f39e40dea71d56a8dea0f2a Mon Sep 17 00:00:00 2001 From: Tim Mayberry Date: Sat, 23 Jun 2012 05:08:14 +0000 Subject: Move three file utility functions from pbd/filesystem.h to pbd/file_utils.h git-svn-id: svn://localhost/ardour2/branches/3.0@12863 d708f5d6-7413-0410-9779-e7cbd77b26cf --- libs/ardour/file_source.cc | 4 ++-- libs/ardour/session.cc | 2 +- libs/ardour/session_directory.cc | 3 ++- libs/ardour/session_state.cc | 2 +- libs/pbd/file_utils.cc | 38 ++++++++++++++++++++++++++++++++++++++ libs/pbd/filesystem.cc | 35 ----------------------------------- libs/pbd/pbd/file_utils.h | 22 ++++++++++++++++++++++ libs/pbd/pbd/filesystem.h | 22 ---------------------- libs/pbd/test/filesystem_test.cc | 25 +++++++++++++------------ 9 files changed, 79 insertions(+), 74 deletions(-) diff --git a/libs/ardour/file_source.cc b/libs/ardour/file_source.cc index ba2b809dbe..2f7ad2caa8 100644 --- a/libs/ardour/file_source.cc +++ b/libs/ardour/file_source.cc @@ -32,7 +32,7 @@ #include "pbd/strsplit.h" #include "pbd/shortpath.h" #include "pbd/enumwriter.h" -#include "pbd/filesystem.h" +#include "pbd/file_utils.h" #include #include @@ -277,7 +277,7 @@ FileSource::find (Session& s, DataType type, const string& path, bool must_exist ++j; while (j != hits.end()) { - if (PBD::sys::equivalent_paths (*i, *j)) { + if (PBD::equivalent_paths (*i, *j)) { /* *i and *j are the same file; break out of the loop early */ break; } diff --git a/libs/ardour/session.cc b/libs/ardour/session.cc index ed90bccb7f..271bb406b0 100644 --- a/libs/ardour/session.cc +++ b/libs/ardour/session.cc @@ -4528,7 +4528,7 @@ Session::ensure_search_path_includes (const string& path, DataType type) On Windows, I think we could just do if (*i == path) here. */ - if (PBD::sys::equivalent_paths (*i, path)) { + if (PBD::equivalent_paths (*i, path)) { return; } } diff --git a/libs/ardour/session_directory.cc b/libs/ardour/session_directory.cc index fca3950669..6eacc8a814 100644 --- a/libs/ardour/session_directory.cc +++ b/libs/ardour/session_directory.cc @@ -21,6 +21,7 @@ #include "pbd/error.h" #include "pbd/compose.h" +#include "pbd/file_utils.h" #include "pbd/filesystem.h" #include "ardour/directory_names.h" @@ -97,7 +98,7 @@ SessionDirectory::sources_root () const path p = m_root_path; if (p.leaf() == ".") { - p = PBD::sys::get_absolute_path (m_root_path); + p = PBD::get_absolute_path (m_root_path); } const string legalized_root (legalize_for_path (p.leaf ())); diff --git a/libs/ardour/session_state.cc b/libs/ardour/session_state.cc index 58a3d1075b..1305653c54 100644 --- a/libs/ardour/session_state.cc +++ b/libs/ardour/session_state.cc @@ -435,7 +435,7 @@ bool Session::path_is_within_session (const std::string& path) { for (vector::const_iterator i = session_dirs.begin(); i != session_dirs.end(); ++i) { - if (PBD::sys::path_is_within (i->path, path)) { + if (PBD::path_is_within (i->path, path)) { return true; } } diff --git a/libs/pbd/file_utils.cc b/libs/pbd/file_utils.cc index f323ebcb69..31db84e88f 100644 --- a/libs/pbd/file_utils.cc +++ b/libs/pbd/file_utils.cc @@ -19,6 +19,9 @@ #include +#include +#include + #include #include #include @@ -169,4 +172,39 @@ copy_files(const std::string & from_path, const std::string & to_dir) } } +std::string +get_absolute_path (const std::string & p) +{ + Glib::RefPtr f = Gio::File::create_for_path (p); + return f->get_path (); +} + +bool +equivalent_paths (const std::string& a, const std::string& b) +{ + struct stat bA; + int const rA = g_stat (a.c_str(), &bA); + struct stat bB; + int const rB = g_stat (b.c_str(), &bB); + + return (rA == 0 && rB == 0 && bA.st_dev == bB.st_dev && bA.st_ino == bB.st_ino); +} + +bool +path_is_within (std::string const & haystack, std::string needle) +{ + while (1) { + if (equivalent_paths (haystack, needle)) { + return true; + } + + needle = Glib::path_get_dirname (needle); + if (needle == "." || needle == "/") { + break; + } + } + + return false; +} + } // namespace PBD diff --git a/libs/pbd/filesystem.cc b/libs/pbd/filesystem.cc index 4e39374b98..e2b8b37ba7 100644 --- a/libs/pbd/filesystem.cc +++ b/libs/pbd/filesystem.cc @@ -211,41 +211,6 @@ extension (const path & p) } -std::string -get_absolute_path (const std::string & p) -{ - Glib::RefPtr f = Gio::File::create_for_path (p); - return f->get_path (); -} - -bool -equivalent_paths (const std::string& a, const std::string& b) -{ - struct stat bA; - int const rA = g_stat (a.c_str(), &bA); - struct stat bB; - int const rB = g_stat (b.c_str(), &bB); - - return (rA == 0 && rB == 0 && bA.st_dev == bB.st_dev && bA.st_ino == bB.st_ino); -} - -bool -path_is_within (std::string const & haystack, std::string needle) -{ - while (1) { - if (equivalent_paths (haystack, needle)) { - return true; - } - - needle = Glib::path_get_dirname (needle); - if (needle == "." || needle == "/") { - break; - } - } - - return false; -} - } // namespace sys } // namespace PBD diff --git a/libs/pbd/pbd/file_utils.h b/libs/pbd/pbd/file_utils.h index 475ddb9828..184e644bba 100644 --- a/libs/pbd/pbd/file_utils.h +++ b/libs/pbd/pbd/file_utils.h @@ -105,6 +105,28 @@ bool copy_file(const std::string & from_path, const std::string & to_path); */ void copy_files(const std::string & from_path, const std::string & to_dir); +/** + * Take a (possibly) relative path and make it absolute + * @return An absolute path + */ +std::string get_absolute_path (const std::string &); + +/** + * Find out if `needle' is a file or directory within the + * directory `haystack'. + * @return true if it is. + */ +bool path_is_within (const std::string &, std::string); + +/** + * @return true if p1 and p2 both resolve to the same file + * @param p1 a file path. + * @param p2 a file path. + * + * Uses g_stat to check for identical st_dev and st_ino values. + */ +bool equivalent_paths (const std::string &p1, const std::string &p2); + } // namespace PBD #endif diff --git a/libs/pbd/pbd/filesystem.h b/libs/pbd/pbd/filesystem.h index f09c400f2e..74baaa3707 100644 --- a/libs/pbd/pbd/filesystem.h +++ b/libs/pbd/pbd/filesystem.h @@ -188,28 +188,6 @@ std::string basename (const path& p); */ std::string extension (const path& p); -/** - * Take a (possibly) relative path and make it absolute - * @return An absolute path - */ -std::string get_absolute_path (const std::string &); - -/** - * Find out if `needle' is a file or directory within the - * directory `haystack'. - * @return true if it is. - */ -bool path_is_within (const std::string &, std::string); - -/** - * @return true if p1 and p2 both resolve to the same file - * @param p1 a file path. - * @param p2 a file path. - * - * Uses g_stat to check for identical st_dev and st_ino values. - */ -bool equivalent_paths (const std::string &p1, const std::string &p2); - } // namespace sys } // namespace PBD diff --git a/libs/pbd/test/filesystem_test.cc b/libs/pbd/test/filesystem_test.cc index 11371c8cb6..38c985d122 100644 --- a/libs/pbd/test/filesystem_test.cc +++ b/libs/pbd/test/filesystem_test.cc @@ -1,6 +1,7 @@ #include #include #include "filesystem_test.h" +#include "pbd/file_utils.h" #include "pbd/filesystem.h" using namespace std; @@ -13,23 +14,23 @@ FilesystemTest::testPathIsWithin () system ("rm -r foo"); PBD::sys::create_directories ("foo/bar/baz"); - CPPUNIT_ASSERT (PBD::sys::path_is_within ("foo/bar/baz", "foo/bar/baz")); - CPPUNIT_ASSERT (PBD::sys::path_is_within ("foo/bar", "foo/bar/baz")); - CPPUNIT_ASSERT (PBD::sys::path_is_within ("foo", "foo/bar/baz")); - CPPUNIT_ASSERT (PBD::sys::path_is_within ("foo/bar", "foo/bar/baz")); - CPPUNIT_ASSERT (PBD::sys::path_is_within ("foo/bar", "foo/bar")); + CPPUNIT_ASSERT (PBD::path_is_within ("foo/bar/baz", "foo/bar/baz")); + CPPUNIT_ASSERT (PBD::path_is_within ("foo/bar", "foo/bar/baz")); + CPPUNIT_ASSERT (PBD::path_is_within ("foo", "foo/bar/baz")); + CPPUNIT_ASSERT (PBD::path_is_within ("foo/bar", "foo/bar/baz")); + CPPUNIT_ASSERT (PBD::path_is_within ("foo/bar", "foo/bar")); - CPPUNIT_ASSERT (PBD::sys::path_is_within ("foo/bar/baz", "frobozz") == false); + CPPUNIT_ASSERT (PBD::path_is_within ("foo/bar/baz", "frobozz") == false); int const r = symlink ("bar", "foo/jim"); CPPUNIT_ASSERT (r == 0); - CPPUNIT_ASSERT (PBD::sys::path_is_within ("foo/bar/baz", "foo/bar/baz")); - CPPUNIT_ASSERT (PBD::sys::path_is_within ("foo/bar", "foo/bar/baz")); - CPPUNIT_ASSERT (PBD::sys::path_is_within ("foo", "foo/bar/baz")); - CPPUNIT_ASSERT (PBD::sys::path_is_within ("foo/bar", "foo/bar/baz")); - CPPUNIT_ASSERT (PBD::sys::path_is_within ("foo/bar", "foo/bar")); + CPPUNIT_ASSERT (PBD::path_is_within ("foo/bar/baz", "foo/bar/baz")); + CPPUNIT_ASSERT (PBD::path_is_within ("foo/bar", "foo/bar/baz")); + CPPUNIT_ASSERT (PBD::path_is_within ("foo", "foo/bar/baz")); + CPPUNIT_ASSERT (PBD::path_is_within ("foo/bar", "foo/bar/baz")); + CPPUNIT_ASSERT (PBD::path_is_within ("foo/bar", "foo/bar")); - CPPUNIT_ASSERT (PBD::sys::path_is_within ("foo/jim/baz", "frobozz") == false); + CPPUNIT_ASSERT (PBD::path_is_within ("foo/jim/baz", "frobozz") == false); } -- cgit v1.2.3