sup

A curses threads-with-tags style email client

sup.git

git clone https://supmua.dev/git/sup/
commit af6bd21638058767df75cb350e1de08794fcf2b3
parent f58a416802c2b34f6df0b0db289f1df43feffddf
Author: julien@macbook <julien.stechele@gmail.com>
Date:   Sun,  9 Mar 2014 16:55:54 +0100

Merge #234: remove the lockfile if sup isn't already launched

Fixes #233.

Squashed commit of the following:

commit 5dd0ffec3d2aba071c02816298ad6801d690e4ae
Author: julien@macbook 
Date:   Mon Feb 24 12:53:11 2014 +0100

    Sleep a couple of seconds before locking one more time

commit 235aff8327d8f2df8d41febbdabb481f5bd8fc9f
Author: julien@macbook 
Date:   Mon Feb 24 12:42:04 2014 +0100

    true need a return like false

commit bf0cc9bb9e8c137be50c29a85f5ccc44b899085e
Author: julien@macbook 
Date:   Wed Feb 19 16:21:38 2014 +0100

    false alone has no effect, use return with

commit 09f1034bc51ec6843f61f16db1ec2bd85cefc8d0
Author: julien@macbook 
Date:   Wed Feb 19 16:20:04 2014 +0100

    Message + false

commit 58a0248486373fe26c53cc094b13ec050c982f6b
Author: julien@macbook 
Date:   Wed Feb 19 16:15:06 2014 +0100

    Fix indentation

commit 072531a3e2d16bde21afb53b630f3c4f9031122a
Author: julien@macbook 
Date:   Mon Feb 17 16:28:56 2014 +0100

    issue-233: remove the lockfile if sup isn't already launched

Diffstat:
M History.txt | 1 +
M lib/sup/interactive_lock.rb | 79 +++++++++++++++++++++++++++++++++++++++++++++++--------------------------------
2 files changed, 48 insertions(+), 32 deletions(-)
diff --git a/History.txt b/History.txt
@@ -3,6 +3,7 @@
 * sup-sync-back-mbox removed.
 * safer mime-view attachment file name handling
 * show thread labels in thread-view-mode
+* remove lock file if there is no sup alive
 
 == 0.15.4 / 2014-02-06
 
diff --git a/lib/sup/interactive_lock.rb b/lib/sup/interactive_lock.rb
@@ -25,49 +25,64 @@ module InteractiveLock
     begin
       Index.lock
     rescue Index::LockError => e
-      stream.puts <<EOS
-Error: the index is locked by another process! User '#{e.user}' on
-host '#{e.host}' is running #{e.pname} with pid #{e.pid}.
-The process was alive as of at least #{time_ago_in_words e.mtime} ago.
+      begin
+        Process.kill 0, e.pid.to_i # 0 signal test the existence of PID
+        stream.puts <<EOS
+  Error: the index is locked by another process! User '#{e.user}' on
+  host '#{e.host}' is running #{e.pname} with pid #{e.pid}.
+  The process was alive as of at least #{time_ago_in_words e.mtime} ago.
 
 EOS
-      stream.print "Should I ask that process to kill itself (y/n)? "
-      stream.flush
-
-      success = if $stdin.gets =~ /^\s*y(es)?\s*$/i
-        stream.puts "Ok, trying to kill process..."
-
-        begin
+        stream.print "Should I ask that process to kill itself (y/n)? "
+        stream.flush
+        if $stdin.gets =~ /^\s*y(es)?\s*$/i
           Process.kill "TERM", e.pid.to_i
           sleep DELAY
-        rescue Errno::ESRCH # no such process
-          stream.puts "Hm, I couldn't kill it."
+          stream.puts "Let's try that again."
+          begin
+            Index.lock
+          rescue Index::LockError => e
+            stream.puts "I couldn't lock the index. The lockfile might just be stale."
+            stream.print "Should I just remove it and continue? (y/n) "
+            stream.flush
+            if $stdin.gets =~ /^\s*y(es)?\s*$/i
+              begin
+                FileUtils.rm e.path
+              rescue Errno::ENOENT
+                stream.puts "The lockfile doesn't exists. We continue."
+              end
+              stream.puts "Let's try that one more time."
+              begin
+                Index.lock
+              rescue Index::LockError => e
+                stream.puts "I couldn't unlock the index."
+                return false
+              end
+              return true
+            end
+          end
         end
-
-        stream.puts "Let's try that again."
+      rescue Errno::ESRCH # no such process
+        stream.puts "I couldn't lock the index. The lockfile might just be stale."
+        begin
+          FileUtils.rm e.path
+        rescue Errno::ENOENT
+          stream.puts "The lockfile doesn't exists. We continue."
+        end
+        stream.puts "Let's try that one more time."
         begin
+          sleep DELAY
           Index.lock
         rescue Index::LockError => e
-          stream.puts "I couldn't lock the index. The lockfile might just be stale."
-          stream.print "Should I just remove it and continue? (y/n) "
-          stream.flush
-
-          if $stdin.gets =~ /^\s*y(es)?\s*$/i
-            FileUtils.rm e.path
-
-            stream.puts "Let's try that one more time."
-            begin
-              Index.lock
-              true
-            rescue Index::LockError => e
-            end
-          end
+          stream.puts "I couldn't unlock the index."
+          return false
         end
+        return true
       end
-
-      stream.puts "Sorry, couldn't unlock the index." unless success
-      success
+      stream.puts "Sorry, couldn't unlock the index."
+      return false
     end
+    return true
   end
 end