]> Sergey Matveev's repositories - nnn.git/commitdiff
Find a parent when CWD disappears
author0xACE <0xaced@gmail.com>
Thu, 11 Jul 2019 22:45:55 +0000 (00:45 +0200)
committer0xACE <0xaced@gmail.com>
Thu, 11 Jul 2019 22:52:25 +0000 (00:52 +0200)
The previous behaviour would exit nnn when the CWD disappeared.

src/nnn.c

index dbd54f2852ad2eca0be947d7c96c7725aa59542e..7a133ef09624f4bc463987a93f23e575c334ab4f 100644 (file)
--- a/src/nnn.c
+++ b/src/nnn.c
@@ -3297,11 +3297,36 @@ nochange:
                if (getppid() == 1)
                        _exit(0);
 
-               /* Check if CWD is deleted to  avoid hang, bad idea */
+               /* Check if CWD is deleted and find a existing parent */
                if (access(path, F_OK)) {
                        DPRINTF_S("dir deleted or moved");
-                       cfg.badexit = 1;
-                       return;
+
+                       /* Save last working directory */
+                       xstrlcpy(lastdir, path, PATH_MAX);
+
+                       /* Save history */
+                       xstrlcpy(lastname, xbasename(path), NAME_MAX + 1);
+
+                       xstrlcpy(newpath, path, PATH_MAX);
+                       while (true) {
+                               dir = visit_parent(path, newpath, &presel);
+                               if (istopdir(path) || istopdir(newpath)) {
+                                       if (!dir)
+                                               dir = dirname(newpath);
+                                       break;
+                               } else if (!dir) {
+                                       xstrlcpy(path, newpath, PATH_MAX);
+                                       continue;
+                               }
+                               break;
+                       }
+
+                       xstrlcpy(path, dir, PATH_MAX);
+
+                       setdirwatch();
+                       mvprintw(xlines - 1, 0, "folder disappeared\n");
+                       xdelay();
+                       goto begin;
                }
 
                sel = nextsel(presel);