Skip to content

Commit 2dfd69e

Browse files
committed
always try to enable sparse file attribute to prevent UI freeze on windows, with safe fallback if not supported
1 parent 81ec8e1 commit 2dfd69e

1 file changed

Lines changed: 21 additions & 2 deletions

File tree

src/util/rsdir.cc

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@
3434
#include <openssl/sha.h>
3535
#include <iomanip>
3636
#include <sstream>
37-
#include <fstream>
3837
#include <stdexcept>
3938

39+
#include "util/rsdebug.h"
4040
#include "util/rsdir.h"
4141
#include "util/rsstring.h"
4242
#include "util/rsrandom.h"
@@ -52,6 +52,7 @@
5252
#include "util/rsstring.h"
5353
#include "wtypes.h"
5454
#include <winioctl.h>
55+
#include <io.h>
5556
#else
5657
#include <errno.h>
5758
#endif
@@ -885,7 +886,25 @@ FILE *RsDirUtil::rs_fopen(const char* filename, const char* mode)
885886
std::wstring wmode;
886887
librs::util::ConvertUtf8ToUtf16(mode, wmode);
887888

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

0 commit comments

Comments
 (0)