summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorCarl Hetherington <cth@carlh.net>2021-04-20 22:53:46 +0200
committerCarl Hetherington <cth@carlh.net>2021-04-23 17:44:14 +0200
commit87df63f0ca9cf1df4f99f5818dad45bbc4c6e3e3 (patch)
treeb8a8113e95e8577623ce8b686141031bbd89204b /src
parent151f5c81fade29e9bebea9904fd85975351b7b78 (diff)
Fix fopen() on windows to cope with long filenames (part of #1755).
Diffstat (limited to 'src')
-rw-r--r--src/lib/cross.h1
-rw-r--r--src/lib/cross_linux.cc8
-rw-r--r--src/lib/cross_osx.cc7
-rw-r--r--src/lib/cross_windows.cc35
-rw-r--r--src/lib/util.cc1
5 files changed, 50 insertions, 2 deletions
diff --git a/src/lib/cross.h b/src/lib/cross.h
index cc066488a..bdcae3537 100644
--- a/src/lib/cross.h
+++ b/src/lib/cross.h
@@ -67,6 +67,7 @@ extern bool running_32_on_64 ();
extern void unprivileged ();
extern boost::filesystem::path config_path ();
extern boost::filesystem::path directory_containing_executable ();
+extern boost::filesystem::path fix_long_path (boost::filesystem::path path);
namespace dcpomatic {
std::string get_process_id ();
}
diff --git a/src/lib/cross_linux.cc b/src/lib/cross_linux.cc
index 510bce8c3..816573230 100644
--- a/src/lib/cross_linux.cc
+++ b/src/lib/cross_linux.cc
@@ -420,3 +420,11 @@ dcpomatic::get_process_id ()
{
return dcp::raw_convert<string>(getpid());
}
+
+
+boost::filesystem::path
+fix_long_path (boost::filesystem::path path)
+{
+ return path;
+}
+
diff --git a/src/lib/cross_osx.cc b/src/lib/cross_osx.cc
index fc8ccd4a8..bd31541c5 100644
--- a/src/lib/cross_osx.cc
+++ b/src/lib/cross_osx.cc
@@ -607,3 +607,10 @@ dcpomatic::get_process_id ()
{
return dcp::raw_convert<string>(getpid());
}
+
+
+boost::filesystem::path
+fix_long_path (boost::filesystem::path path)
+{
+ return path;
+}
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());
}
diff --git a/src/lib/util.cc b/src/lib/util.cc
index 65bfd4534..8a039764d 100644
--- a/src/lib/util.cc
+++ b/src/lib/util.cc
@@ -1193,3 +1193,4 @@ start_of_thread (string)
}
#endif
+