Add and tweak some FileIO OS-specific functions

* fflush is a no-op wrapper but nice to have for consistency.
* ftruncateat will take a file read-write handle and truncate it at a
  given length. Useful if re-writing a file and the new data ends up
  being shorter than the oldd ata.
* Move in addition to Copy, and success/fail bool return on both.
This commit is contained in:
baldurk
2017-11-15 18:35:55 +00:00
parent 33ff48811b
commit b1c0de3016
4 changed files with 68 additions and 8 deletions
+6 -1
View File
@@ -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<PathEntry> 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);
+30 -4
View File
@@ -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);
+31 -2
View File
@@ -23,6 +23,7 @@
* THE SOFTWARE.
******************************************************************************/
#include <io.h>
#include <shlobj.h>
#include <shlwapi.h>
#include <stdio.h>
@@ -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);
+1 -1
View File
@@ -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;