]> git.lyx.org Git - features.git/commitdiff
Fix file locking problem on windows (bug 9925)
authorGeorg Baum <baum@lyx.org>
Tue, 26 Jan 2016 20:49:08 +0000 (21:49 +0100)
committerGeorg Baum <baum@lyx.org>
Tue, 26 Jan 2016 20:49:08 +0000 (21:49 +0100)
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
src/insets/InsetExternal.h
src/support/TempFile.h

index 8b0f00c5def651cb63c8faeadacd5fa322ee5679..097dc7d3e06479395cde5682569918fe1ee6e50d 100644 (file)
@@ -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
index 00546faf80c2e0847a7e21a10ef858bd2cb620eb..75131182b982928eb2a29ec44b45cba77b821653 100644 (file)
 
 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
index 53e91f0ae56a81632eb5c053db52d04167555be3..16efede3ab57880e54bbefd7ac71a2037510db36 100644 (file)
@@ -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