diff options
| author | Carl Hetherington <cth@carlh.net> | 2021-04-20 22:53:46 +0200 |
|---|---|---|
| committer | Carl Hetherington <cth@carlh.net> | 2021-04-23 17:44:14 +0200 |
| commit | 87df63f0ca9cf1df4f99f5818dad45bbc4c6e3e3 (patch) | |
| tree | b8a8113e95e8577623ce8b686141031bbd89204b /src/lib/cross_windows.cc | |
| parent | 151f5c81fade29e9bebea9904fd85975351b7b78 (diff) | |
Fix fopen() on windows to cope with long filenames (part of #1755).
Diffstat (limited to 'src/lib/cross_windows.cc')
| -rw-r--r-- | src/lib/cross_windows.cc | 35 |
1 files changed, 33 insertions, 2 deletions
diff --git a/src/lib/cross_windows.cc b/src/lib/cross_windows.cc index 04ee26271..d97550ca9 100644 --- a/src/lib/cross_windows.cc +++ b/src/lib/cross_windows.cc @@ -26,6 +26,7 @@ #include "config.h" #include "exceptions.h" #include "dcpomatic_assert.h" +#include "util.h" #include <dcp/raw_convert.h> #include <glib.h> extern "C" { @@ -234,6 +235,36 @@ disk_writer_path () #endif +/** Windows can't "by default" cope with paths longer than 260 characters, so if you pass such a path to + * any boost::filesystem method it will fail. There is a "fix" for this, which is to prepend + * the string \\?\ to the path. This will make it work, so long as: + * - the path is absolute. + * - the path only uses backslashes. + * - individual path components are "short enough" (probably less than 255 characters) + * + * See https://www.boost.org/doc/libs/1_57_0/libs/filesystem/doc/reference.html under + * "Warning: Long paths on Windows" for some details. + * + * Our fopen_boost uses this method to get this fix, but any other calls to boost::filesystem + * will not unless this method is explicitly called to pre-process the pathname. + */ +boost::filesystem::path +fix_long_path (boost::filesystem::path long_path) +{ + using namespace boost::filesystem; + path fixed = "\\\\?\\"; + /* We have to make the path canonical but we can't call canonical() on the long path + * as it will fail. So we'll sort of do it ourselves (possibly badly). + */ + if (long_path.is_absolute()) { + fixed += long_path.make_preferred(); + } else { + fixed += boost::filesystem::current_path() / long_path.make_preferred(); + } + return fixed; +} + + /* Apparently there is no way to create an ofstream using a UTF-8 filename under Windows. We are hence reduced to using fopen with this wrapper. @@ -242,8 +273,8 @@ FILE * fopen_boost (boost::filesystem::path p, string t) { wstring w (t.begin(), t.end()); - /* c_str() here should give a UTF-16 string */ - return _wfopen (p.c_str(), w.c_str ()); + /* c_str() on fixed here should give a UTF-16 string */ + return _wfopen (fix_long_path(p).c_str(), w.c_str()); } |
