Subject: tar-untar-buffer doesn't support symlinks

From: [email-masked]
Date: Sat, 17 Dec 2016 19:25:47 -0500

Severity: wishlist

tar-untar-buffer silently converts symlinks into regular files.  There
might be some portability trouble with supporting them, but at least we
should warn about low fidelity unpacking, as in the patch below.

This initially turned up as a yasnippet bug, because the package's
tarball was using symlinks.  Of course that has to be fixed by avoiding
symlinks (https://github.com/joaotavora/yasnippet/issues/748).

>From 43d59f8d90604cb8d65a28d99f90156da70d4718 Mon Sep 17 00:00:00 2001
From: Noam Postavsky <[email-masked]>
Date: Sat, 17 Dec 2016 18:01:52 -0500
Subject: [PATCH] Warn about incomplete untarring of link files

The current tar-mode is doesn't really support unpacking symlinks, it
simply creates an empty file of the same name.

* lisp/tar-mode.el (tar--describe-as-link): New function extracted from
`tar--check-descriptor'.
(tar-untar-buffer): Use it to warn about imperfectly untarred link
files.
---
 lisp/tar-mode.el | 31 ++++++++++++++++++++-----------
 1 file changed, 20 insertions(+), 11 deletions(-)

diff --git a/lisp/tar-mode.el b/lisp/tar-mode.el
index 0520369..cafd0ad 100644
--- a/lisp/tar-mode.el
+++ b/lisp/tar-mode.el
@@ -544,6 +544,7 @@ tar-untar-buffer
                (dir (if (eq (tar-header-link-type descriptor) 5)
                         name
                       (file-name-directory name)))
+               (link-desc (tar--describe-as-link descriptor))
                (start (tar-header-data-start descriptor))
                (end (+ start (tar-header-size descriptor))))
           (unless (file-directory-p name)
@@ -552,6 +553,10 @@ tar-untar-buffer
                 (make-directory dir t))
             (unless (file-directory-p name)
 	      (let ((coding-system-for-write 'no-conversion))
+                (when link-desc
+                  (lwarn '(tar link) :warning
+                         "Extracted `%s', %s, as a normal file"
+                         name link-desc))
 		(write-region start end name)))
             (set-file-modes name (tar-header-mode descriptor))))))))
 
@@ -816,19 +821,23 @@ tar-current-descriptor
 	  nil
 	  (error "This line does not describe a tar-file entry"))))
 
-(defun tar--check-descriptor (descriptor)
+(defun tar--describe-as-link (descriptor)
   (let ((link-p (tar-header-link-type descriptor)))
     (if link-p
-	(error "This is %s, not a real file"
-	       (cond ((eq link-p 5) "a directory")
-		     ((eq link-p 20) "a tar directory header")
-		     ((eq link-p 28) "a next has longname")
-		     ((eq link-p 29) "a multivolume-continuation")
-		     ((eq link-p 35) "a sparse entry")
-		     ((eq link-p 38) "a volume header")
-		     ((eq link-p 55) "a pax global extended header")
-		     ((eq link-p 72) "a pax extended header")
-		     (t "a link"))))))
+	(cond ((eq link-p 5) "a directory")
+              ((eq link-p 20) "a tar directory header")
+              ((eq link-p 28) "a next has longname")
+              ((eq link-p 29) "a multivolume-continuation")
+              ((eq link-p 35) "a sparse entry")
+              ((eq link-p 38) "a volume header")
+              ((eq link-p 55) "a pax global extended header")
+              ((eq link-p 72) "a pax extended header")
+              (t "a link")))))
+
+(defun tar--check-descriptor (descriptor)
+  (let ((link-desc (tar--describe-as-link descriptor)))
+    (when link-desc
+      (error "This is %s, not a real file" link-desc))))
 
 (defun tar-get-descriptor ()
   (let* ((descriptor (tar-current-descriptor))
-- 
2.9.3