From 6c412e9e90e2ddb1e011adbb78cafacb2a7619b3 Mon Sep 17 00:00:00 2001 From: Pavel Sanda Date: Sat, 31 Jan 2009 17:10:56 +0000 Subject: [PATCH] VCS: add svn locking mechanism git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@28302 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/VCBackend.cpp | 89 +++++++++++++++++++++++++++++++++++++++++++++-- src/VCBackend.h | 8 +++++ 2 files changed, 95 insertions(+), 2 deletions(-) diff --git a/src/VCBackend.cpp b/src/VCBackend.cpp index e0c5a144b9..6e8b565554 100644 --- a/src/VCBackend.cpp +++ b/src/VCBackend.cpp @@ -419,8 +419,10 @@ bool CVS::toggleReadOnlyEnabled() SVN::SVN(FileName const & m, FileName const & f) { + owner_ = 0; master_ = m; file_ = f; + locked_mode_ = 0; scanMaster(); } @@ -460,6 +462,46 @@ void SVN::scanMaster() } +bool SVN::checkLockMode() +{ + FileName tmpf = FileName::tempName("lyxvcout"); + if (tmpf.empty()){ + LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf); + return N_("Error: Could not generate logfile."); + } + + LYXERR(Debug::LYXVC, "Detecting locking mode..."); + if (doVCCommandCall("svn proplist " + quoteName(file_.onlyFileName()) + + " > " + quoteName(tmpf.toFilesystemEncoding()), + file_.onlyPath())) + return false; + + ifstream ifs(tmpf.toFilesystemEncoding().c_str()); + string line; + bool ret = false; + + while (ifs) { + getline(ifs, line); + LYXERR(Debug::LYXVC, line); + if (contains(line, "svn:needs-lock")) + ret = true; + } + LYXERR(Debug::LYXVC, "Locking enabled: " << ret); + ifs.close(); + locked_mode_ = ret; + return ret; + +} + + +bool SVN::isLocked() +{ + //refresh file info + FileName file(file_.absFilename()); + return !file.isReadOnly(); +} + + void SVN::registrer(string const & /*msg*/) { doVCCommand("svn add -q " + quoteName(onlyFilename(owner_->absFileName())), @@ -487,6 +529,9 @@ string SVN::checkIn(string const & msg) _("Error when commiting to repository.\n" "You have to manually resolve the problem.\n" "After pressing OK, LyX will reopen the document.")); + else + fileLock(false, tmpf, log); + tmpf.erase(); return "SVN: " + log; } @@ -494,9 +539,13 @@ string SVN::checkIn(string const & msg) bool SVN::checkInEnabled() { - return true; + if (locked_mode_) + return isLocked(); + else + return true; } + // FIXME Correctly return code should be checked instead of this. // This would need another solution than just plain startscript. // Hint from Andre': QProcess::readAllStandardError()... @@ -519,6 +568,36 @@ string SVN::scanLogFile(FileName const & f, string & status) } +void SVN::fileLock(bool lock, FileName const & tmpf, string &status) +{ + if (!locked_mode_ || (isLocked() == lock)) + return; + + string arg = lock ? "lock " : "unlock "; + doVCCommand("svn "+ arg + quoteName(onlyFilename(owner_->absFileName())) + + " > " + quoteName(tmpf.toFilesystemEncoding()), + FileName(owner_->filePath())); + + ifstream ifs(tmpf.toFilesystemEncoding().c_str()); + string line; + while (ifs) { + getline(ifs, line); + if (!line.empty()) status += line + "; "; + } + ifs.close(); + + if (!isLocked() && lock) + frontend::Alert::error(_("Revision control error."), + _("Error when acquiring write lock.\n" + "Most probably some other user edit the current document now!\n" + "Check also the access to the repository.")); + if (isLocked() && !lock) + frontend::Alert::error(_("Revision control error."), + _("Error when releasing write lock.\n" + "Check the access to the repository.")); +} + + string SVN::checkOut() { FileName tmpf = FileName::tempName("lyxvcout"); @@ -539,6 +618,9 @@ string SVN::checkOut() "You have to manually resolve the conflicts NOW!\n'%1$s'.\n\n" "After pressing OK, LyX will try to reopen resolved document."), from_local8bit(res))); + + fileLock(true, tmpf, log); + tmpf.erase(); return "SVN: " + log; } @@ -546,7 +628,10 @@ string SVN::checkOut() bool SVN::checkOutEnabled() { - return true; + if (locked_mode_) + return !isLocked(); + else + return true; } diff --git a/src/VCBackend.h b/src/VCBackend.h index e86f258d70..23bb162eb5 100644 --- a/src/VCBackend.h +++ b/src/VCBackend.h @@ -225,9 +225,17 @@ protected: virtual void scanMaster(); /// Check for messages in svn output. Returns error. std::string scanLogFile(support::FileName const & f, std::string & status); + /// checks locking policy and setup locked_mode_ + bool checkLockMode(); + /// is the loaded file locked? + bool isLocked(); + /// acquire/release write lock for the current file + void fileLock(bool lock, support::FileName const & tmpf, std::string & status); private: support::FileName file_; + /// is the loaded file under locking policy? + bool locked_mode_; }; } // namespace lyx -- 2.39.2