File::rename
- Added a rename method to permit safe file writes.
This commit is contained in:
17
src/Path.cpp
17
src/Path.cpp
@@ -32,6 +32,7 @@
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <pwd.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <Path.h>
|
||||
#include <cmake.h>
|
||||
@@ -170,6 +171,22 @@ bool Path::executable () const
|
||||
return access (_data.c_str (), X_OK) ? false : true;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
bool Path::rename (const std::string& new_name)
|
||||
{
|
||||
std::string expanded = expand (new_name);
|
||||
if (_data != expanded)
|
||||
{
|
||||
if (::rename (_data.c_str (), expanded.c_str ()) == 0)
|
||||
{
|
||||
_data = expanded;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// ~ --> /home/user
|
||||
// ~foo/x --> /home/foo/s
|
||||
|
||||
Reference in New Issue
Block a user