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
This commit is contained in:
parent
6aee537109
commit
e26e59b006
|
@ -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 <glibmm/miscutils.h>
|
||||
#include <glibmm/fileutils.h>
|
||||
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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 ()));
|
||||
|
|
|
@ -435,7 +435,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 (PBD::sys::path_is_within (i->path, path)) {
|
||||
if (PBD::path_is_within (i->path, path)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,6 +19,9 @@
|
|||
|
||||
#include <algorithm>
|
||||
|
||||
#include <glib.h>
|
||||
#include <glib/gstdio.h>
|
||||
|
||||
#include <glibmm/fileutils.h>
|
||||
#include <glibmm/miscutils.h>
|
||||
#include <glibmm/pattern.h>
|
||||
|
@ -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<Gio::File> 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
|
||||
|
|
|
@ -211,41 +211,6 @@ extension (const path & p)
|
|||
|
||||
}
|
||||
|
||||
std::string
|
||||
get_absolute_path (const std::string & p)
|
||||
{
|
||||
Glib::RefPtr<Gio::File> 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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#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);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue