13
0

add PBD::toggle_file_existence()

This commit is contained in:
Paul Davis 2015-03-16 13:00:28 -05:00
parent 11a702149e
commit 8ff477ab1a
2 changed files with 20 additions and 0 deletions

View File

@ -484,4 +484,17 @@ tmp_writable_directory (const char* domain, const string& prefix)
return new_test_dir;
}
int
toggle_file_existence (string const & path)
{
if (Glib::file_test (path, Glib::FILE_TEST_IS_REGULAR)) {
return g_unlink (path.c_str());
}
int fd = g_open (path.c_str(), O_CREAT|O_TRUNC|O_RDWR, 0666);
GError* err;
g_close (fd, &err);
return !(fd >= 0);
}
} // namespace PBD

View File

@ -249,6 +249,13 @@ LIBPBD_API void remove_directory (const std::string& dir);
*/
LIBPBD_API std::string tmp_writable_directory (const char* domain, const std::string& prefix);
/** If @param path exists, unlink it. If it doesn't exist, create it.
*
* @return zero if required action was successful, non-zero otherwise.
*/
LIBPBD_API int toggle_file_existence (std::string const &);
} // namespace PBD
#endif