Skip to content

Commit a21d059

Browse files
authored
Merge pull request #251 from jolavillette/fix/windows-sparse-file-support
always try to enable sparse file attribute to prevent UI freeze on windows, with safe fallback if not supported
2 parents 81ec8e1 + db6a69a commit a21d059

1 file changed

Lines changed: 22 additions & 2 deletions

File tree

src/util/rsdir.cc

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,16 @@
2828
#include <fcntl.h>
2929
#include <unistd.h>
3030
#include <iostream>
31+
#include <fstream>
3132
#include <algorithm>
3233
#include <stdio.h>
3334
#include <dirent.h>
3435
#include <openssl/sha.h>
3536
#include <iomanip>
3637
#include <sstream>
37-
#include <fstream>
3838
#include <stdexcept>
3939

40+
#include "util/rsdebug.h"
4041
#include "util/rsdir.h"
4142
#include "util/rsstring.h"
4243
#include "util/rsrandom.h"
@@ -52,6 +53,7 @@
5253
#include "util/rsstring.h"
5354
#include "wtypes.h"
5455
#include <winioctl.h>
56+
#include <io.h>
5557
#else
5658
#include <errno.h>
5759
#endif
@@ -885,7 +887,25 @@ FILE *RsDirUtil::rs_fopen(const char* filename, const char* mode)
885887
std::wstring wmode;
886888
librs::util::ConvertUtf8ToUtf16(mode, wmode);
887889

888-
return _wfopen(wfilename.c_str(), wmode.c_str());
890+
FILE *f = _wfopen(wfilename.c_str(), wmode.c_str());
891+
892+
if(f)
893+
{
894+
// Attempt to set sparse flag
895+
int fd = _fileno(f);
896+
HANDLE hChunkFile = (HANDLE) _get_osfhandle(fd);
897+
898+
if (hChunkFile != INVALID_HANDLE_VALUE)
899+
{
900+
DWORD dwTemp;
901+
if (!DeviceIoControl(hChunkFile, FSCTL_SET_SPARSE, NULL, 0, NULL, 0, &dwTemp, NULL))
902+
{
903+
// Warn but don't fail, as it might just be a filesystem not supporting it (e.g. FAT32)
904+
RsDbg() << "FILESYSTEM RsDirUtil::rs_fopen: Warning: Failed to set sparse flag for " << filename << ". Error: " << GetLastError();
905+
}
906+
}
907+
}
908+
return f;
889909
#else
890910
return fopen64(filename, mode);
891911
#endif

0 commit comments

Comments
 (0)