From 8171272d1fb56ef3509a82143c4aa1d4b31fa08e Mon Sep 17 00:00:00 2001 From: Georg Baum Date: Tue, 26 Jan 2016 21:49:08 +0100 Subject: [PATCH] Fix file locking problem on windows (bug 9925) External processes cannot access files which are open in LyX. Therefore the temp files created by the external inset need to be closed right after creation. The symptom was that the date inset did not produce any outout on windows (bug 9925). This change reverts a small part of f09a9fe2. Although the date inset is unimportant and will probably be removed, this change is important for all external insets that make use of temp files. --- src/insets/InsetExternal.cpp | 21 +++++++++++++++------ src/insets/InsetExternal.h | 6 +----- src/support/TempFile.h | 7 +++++++ 3 files changed, 23 insertions(+), 11 deletions(-) diff --git a/src/insets/InsetExternal.cpp b/src/insets/InsetExternal.cpp index 8b0f00c5de..097dc7d3e0 100644 --- a/src/insets/InsetExternal.cpp +++ b/src/insets/InsetExternal.cpp @@ -69,13 +69,20 @@ namespace Alert = frontend::Alert; namespace external { -TempName::TempName() : tempfile_(new support::TempFile("lyxextXXXXXX.tmp")) +TempName::TempName() { // must have an extension for the converter code to work correctly. + support::TempFile f("lyxextXXXXXX.tmp"); + // Let f go out of scope here and delete the file ourselves in + // ~TempName(), since otherwise external processes would not be able + // to use the file on windows (bug 9925). This is not as safe as + // keeping a support::TempFile member would be, but the best we can do. + f.setAutoRemove(false); + tempname_ = f.name(); } -TempName::TempName(TempName const & that) : tempfile_(0) +TempName::TempName(TempName const & that) { *this = that; } @@ -83,15 +90,17 @@ TempName::TempName(TempName const & that) : tempfile_(0) TempName::~TempName() { - delete tempfile_; + tempname_.removeFile(); } TempName & TempName::operator=(TempName const & other) { if (this != &other) { - delete tempfile_; - tempfile_ = new support::TempFile("lyxextXXXXXX.tmp"); + tempname_.removeFile(); + support::TempFile f("lyxextXXXXXX.tmp"); + f.setAutoRemove(false); + tempname_ = f.name(); } return *this; } @@ -99,7 +108,7 @@ TempName & TempName::operator=(TempName const & other) support::FileName TempName::operator()() const { - return tempfile_->name(); + return tempname_; } } // namespace external diff --git a/src/insets/InsetExternal.h b/src/insets/InsetExternal.h index 00546faf80..75131182b9 100644 --- a/src/insets/InsetExternal.h +++ b/src/insets/InsetExternal.h @@ -24,10 +24,6 @@ namespace lyx { -namespace support { -class TempFile; -} - namespace external { /** No two InsetExternalParams variables can have the same temporary file. @@ -45,7 +41,7 @@ public: TempName & operator=(TempName const &); support::FileName operator()() const; private: - support::TempFile * tempfile_; + support::FileName tempname_; }; } // namespace external diff --git a/src/support/TempFile.h b/src/support/TempFile.h index 53e91f0ae5..16efede3ab 100644 --- a/src/support/TempFile.h +++ b/src/support/TempFile.h @@ -24,6 +24,13 @@ class FileName; * The file is created in the constructor, and deleted in the destructor. * You may do anything with the file (including deletion), but the instance * of this class must stay alive as long as the file is needed. + * There is only one exception to this rule: + * If the file is supposed to be used by a different process then you need + * to be aware of OS specific file locking semantics: On windows, the file + * is opened with exclusive rights for the process which opened it. This + * is not the case on other OSes. Therefore, if the file is supposed to be + * used by a different process you need to sometheing similar to TempName + * in InsetExternal.cpp. */ class TempFile { /// noncopyable -- 2.39.5