touch_file, remove_file: ignore non-existent files

This commit is contained in:
Andrew Ayer
2015-02-07 13:22:30 -08:00
parent 18d3cfeca9
commit 85635ae0b1
3 changed files with 16 additions and 6 deletions

View File

@@ -280,14 +280,14 @@ int exit_status (int wait_status)
void touch_file (const std::string& filename)
{
if (utimes(filename.c_str(), NULL) == -1) {
if (utimes(filename.c_str(), NULL) == -1 && errno != ENOENT) {
throw System_error("utimes", filename, errno);
}
}
void remove_file (const std::string& filename)
{
if (unlink(filename.c_str()) == -1) {
if (unlink(filename.c_str()) == -1 && errno != ENOENT) {
throw System_error("unlink", filename, errno);
}
}

View File

@@ -325,7 +325,12 @@ void touch_file (const std::string& filename)
{
HANDLE fh = CreateFileA(filename.c_str(), FILE_WRITE_ATTRIBUTES, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
if (fh == INVALID_HANDLE_VALUE) {
throw System_error("CreateFileA", filename, GetLastError());
DWORD error = GetLastError();
if (error == ERROR_FILE_NOT_FOUND) {
return;
} else {
throw System_error("CreateFileA", filename, error);
}
}
SYSTEMTIME system_time;
GetSystemTime(&system_time);
@@ -343,7 +348,12 @@ void touch_file (const std::string& filename)
void remove_file (const std::string& filename)
{
if (!DeleteFileA(filename.c_str())) {
throw System_error("DeleteFileA", filename, GetLastError());
DWORD error = GetLastError();
if (error == ERROR_FILE_NOT_FOUND) {
return;
} else {
throw System_error("DeleteFileA", filename, error);
}
}
}

View File

@@ -65,8 +65,8 @@ int exec_command (const std::vector<std::string>&, std::ostream& output);
int exec_command_with_input (const std::vector<std::string>&, const char* p, size_t len);
int exit_status (int wait_status); // returns -1 if process did not exit (but was signaled, etc.)
inline bool successful_exit (int wait_status) { return exit_status(wait_status) == 0; }
void touch_file (const std::string&);
void remove_file (const std::string&);
void touch_file (const std::string&); // ignores non-existent files
void remove_file (const std::string&); // ignores non-existent files
std::string escape_shell_arg (const std::string&);
uint32_t load_be32 (const unsigned char*);
void store_be32 (unsigned char*, uint32_t);