Move file utility function into pbd/file_utils.h and into PBD namespace

git-svn-id: svn://localhost/ardour2/branches/3.0@12865 d708f5d6-7413-0410-9779-e7cbd77b26cf
This commit is contained in:
Tim Mayberry
2012-06-23 05:08:19 +00:00
parent 878acbee14
commit 6b1659a29b
5 changed files with 35 additions and 37 deletions

View File

@@ -521,7 +521,7 @@ Session::create (const string& session_template, BusProfile* bus_profile)
return -1;
}
_writable = sys::exists_and_writable (_path);
_writable = exists_and_writable (_path);
if (!session_template.empty()) {
std::string in_path = session_template_dir_to_file (session_template);
@@ -894,7 +894,7 @@ Session::load_state (string snapshot_name)
set_dirty();
_writable = sys::exists_and_writable (xmlpath.to_string());
_writable = exists_and_writable (xmlpath.to_string());
if (!state_tree->read (xmlpath.to_string())) {
error << string_compose(_("Could not understand ardour file %1"), xmlpath.to_string()) << endmsg;

View File

@@ -207,4 +207,34 @@ path_is_within (std::string const & haystack, std::string needle)
return false;
}
bool
exists_and_writable (const std::string & p)
{
/* writable() really reflects the whole folder, but if for any
reason the session state file can't be written to, still
make us unwritable.
*/
struct stat statbuf;
if (g_stat (p.c_str(), &statbuf) != 0) {
/* doesn't exist - not writable */
return false;
} else {
if (!(statbuf.st_mode & S_IWUSR)) {
/* exists and is not writable */
return false;
}
/* filesystem may be mounted read-only, so even though file
* permissions permit access, the mount status does not.
* access(2) seems like the best test for this.
*/
if (g_access (p.c_str(), W_OK) != 0) {
return false;
}
}
return true;
}
} // namespace PBD

View File

@@ -93,37 +93,6 @@ exists (const path & p)
return Glib::file_test (p.to_string(), Glib::FILE_TEST_EXISTS);
}
bool
exists_and_writable (const std::string & p)
{
/* writable() really reflects the whole folder, but if for any
reason the session state file can't be written to, still
make us unwritable.
*/
struct stat statbuf;
if (g_stat (p.c_str(), &statbuf) != 0) {
/* doesn't exist - not writable */
return false;
} else {
if (!(statbuf.st_mode & S_IWUSR)) {
/* exists and is not writable */
return false;
}
/* filesystem may be mounted read-only, so even though file
* permissions permit access, the mount status does not.
* access(2) seems like the best test for this.
*/
if (g_access (p.to_string().c_str(), W_OK) != 0) {
return false;
}
}
return true;
}
bool
is_directory (const path & p)
{

View File

@@ -127,6 +127,9 @@ bool path_is_within (const std::string &, std::string);
*/
bool equivalent_paths (const std::string &p1, const std::string &p2);
/// @return true if path at p exists and is writable, false otherwise
bool exists_and_writable(const std::string & p);
} // namespace PBD
#endif

View File

@@ -119,10 +119,6 @@ inline path operator/ (const path& lhs, const path& rhs)
/// @return true if path at p exists
bool exists(const path & p);
/// @return true if path at p exists and is writable, false otherwise
bool exists_and_writable(const std::string & p);
/// @return true if path at p is a directory.
bool is_directory(const path & p);