diff options
author | Paul Davis <paul@linuxaudiosystems.com> | 2015-03-16 13:00:28 -0500 |
---|---|---|
committer | Paul Davis <paul@linuxaudiosystems.com> | 2015-03-16 13:00:28 -0500 |
commit | 8ff477ab1a3d9392dc6d68e15eaa24c887fa2e0f (patch) | |
tree | 30cecbef5909274cedb73b4d68d23c29be2ceee0 | |
parent | 11a702149ea2e4a7bc858a2aeb4ea8efe12502ba (diff) |
add PBD::toggle_file_existence()
-rw-r--r-- | libs/pbd/file_utils.cc | 13 | ||||
-rw-r--r-- | libs/pbd/pbd/file_utils.h | 7 |
2 files changed, 20 insertions, 0 deletions
diff --git a/libs/pbd/file_utils.cc b/libs/pbd/file_utils.cc index 5a0ffa928f..804f825b94 100644 --- a/libs/pbd/file_utils.cc +++ b/libs/pbd/file_utils.cc @@ -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 diff --git a/libs/pbd/pbd/file_utils.h b/libs/pbd/pbd/file_utils.h index bd22f341d8..761d8d407f 100644 --- a/libs/pbd/pbd/file_utils.h +++ b/libs/pbd/pbd/file_utils.h @@ -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 |