diff --git a/renderdoc/os/os_specific.h b/renderdoc/os/os_specific.h index 5e28d76ae..0452d1926 100644 --- a/renderdoc/os/os_specific.h +++ b/renderdoc/os/os_specific.h @@ -253,7 +253,8 @@ void GetExecutableFilename(string &selfName); uint64_t GetModifiedTimestamp(const string &filename); -void Copy(const char *from, const char *to, bool allowOverwrite); +bool Copy(const char *from, const char *to, bool allowOverwrite); +bool Move(const char *from, const char *to, bool allowOverwrite); void Delete(const char *path); std::vector GetFilesInDirectory(const char *path); @@ -271,6 +272,10 @@ std::string getline(FILE *f); uint64_t ftell64(FILE *f); void fseek64(FILE *f, uint64_t offset, int origin); +void ftruncateat(FILE *f, uint64_t length); + +bool fflush(FILE *f); + bool feof(FILE *f); int fclose(FILE *f); diff --git a/renderdoc/os/posix/posix_stringio.cpp b/renderdoc/os/posix/posix_stringio.cpp index c216b1cb2..2d282fa44 100644 --- a/renderdoc/os/posix/posix_stringio.cpp +++ b/renderdoc/os/posix/posix_stringio.cpp @@ -255,17 +255,17 @@ uint64_t GetModifiedTimestamp(const string &filename) return 0; } -void Copy(const char *from, const char *to, bool allowOverwrite) +bool Copy(const char *from, const char *to, bool allowOverwrite) { if(from[0] == 0 || to[0] == 0) - return; + return false; FILE *ff = ::fopen(from, "r"); if(!ff) { RDCERR("Can't open source file for copy '%s'", from); - return; + return false; } FILE *tf = ::fopen(to, "r"); @@ -275,7 +275,7 @@ void Copy(const char *from, const char *to, bool allowOverwrite) RDCERR("Destination file for non-overwriting copy '%s' already exists", from); ::fclose(ff); ::fclose(tf); - return; + return false; } if(tf) @@ -287,6 +287,7 @@ void Copy(const char *from, const char *to, bool allowOverwrite) { ::fclose(ff); RDCERR("Can't open destination file for copy '%s'", to); + return false; } char buffer[BUFSIZ]; @@ -299,6 +300,19 @@ void Copy(const char *from, const char *to, bool allowOverwrite) ::fclose(ff); ::fclose(tf); + + return true; +} + +bool Move(const char *from, const char *to, bool allowOverwrite) +{ + if(exists(to)) + { + if(!allowOverwrite) + return false; + } + + return ::rename(from, to) == 0; } void Delete(const char *path) @@ -434,6 +448,18 @@ bool feof(FILE *f) return ::feof(f) != 0; } +void ftruncateat(FILE *f, uint64_t length) +{ + ::fflush(f); + int fd = ::fileno(f); + ::ftruncate(fd, (off_t)length); +} + +bool fflush(FILE *f) +{ + return ::fflush(f) == 0; +} + int fclose(FILE *f) { return ::fclose(f); diff --git a/renderdoc/os/win32/win32_stringio.cpp b/renderdoc/os/win32/win32_stringio.cpp index 037db0d5d..5e45f29ea 100644 --- a/renderdoc/os/win32/win32_stringio.cpp +++ b/renderdoc/os/win32/win32_stringio.cpp @@ -23,6 +23,7 @@ * THE SOFTWARE. ******************************************************************************/ +#include #include #include #include @@ -394,12 +395,28 @@ uint64_t GetModifiedTimestamp(const string &filename) return 0; } -void Copy(const char *from, const char *to, bool allowOverwrite) +bool Copy(const char *from, const char *to, bool allowOverwrite) { wstring wfrom = StringFormat::UTF82Wide(string(from)); wstring wto = StringFormat::UTF82Wide(string(to)); - ::CopyFileW(wfrom.c_str(), wto.c_str(), allowOverwrite == false); + return ::CopyFileW(wfrom.c_str(), wto.c_str(), allowOverwrite == false) != 0; +} + +bool Move(const char *from, const char *to, bool allowOverwrite) +{ + wstring wfrom = StringFormat::UTF82Wide(string(from)); + wstring wto = StringFormat::UTF82Wide(string(to)); + + if(exists(to)) + { + if(allowOverwrite) + Delete(to); + else + return false; + } + + return ::MoveFileW(wfrom.c_str(), wto.c_str()) != 0; } void Delete(const char *path) @@ -591,6 +608,18 @@ bool feof(FILE *f) return ::feof(f) != 0; } +void ftruncateat(FILE *f, uint64_t length) +{ + ::fflush(f); + int fd = ::_fileno(f); + ::_chsize_s(fd, (int64_t)length); +} + +bool fflush(FILE *f) +{ + return ::fflush(f) == 0; +} + int fclose(FILE *f) { return ::fclose(f); diff --git a/renderdoc/serialise/streamio.h b/renderdoc/serialise/streamio.h index 6716f3f5c..13c73ee9a 100644 --- a/renderdoc/serialise/streamio.h +++ b/renderdoc/serialise/streamio.h @@ -401,7 +401,7 @@ public: if(m_Compressor) return m_Compressor->Finish(); else if(m_File) - return fflush(m_File) == 0; + return FileIO::fflush(m_File); else if(m_Sock) return true;