]> git.e2factory.org Git - e2factory.git/commitdiff
e2build: do not re-create last link pointing to the same dir
authorTobias Ulmer <tu@emlix.com>
Tue, 7 May 2019 16:49:16 +0000 (18:49 +0200)
committerTobias Ulmer <tu@emlix.com>
Tue, 7 May 2019 16:49:16 +0000 (18:49 +0200)
Signed-off-by: Tobias Ulmer <tu@emlix.com>
local/e2build.lua

index 307ac58ccb7d303919018be63635402bd4783003..d85aabcdadf9b011d4a301d505d2e94f78fb0c72 100644 (file)
@@ -1159,6 +1159,8 @@ function e2build.build_process_class:_linklast(res, rbs)
 
         return true
     else -- otherwise create a symlink
+        local createsymlink = true
+
         dst, re = cache.fetch_file_path(cache.cache(), server, location)
         if not dst then
             return false, e:cat(re)
@@ -1166,19 +1168,49 @@ function e2build.build_process_class:_linklast(res, rbs)
 
         dst = e2lib.dirname(dst) -- we only care about the directory
 
-        -- create the last link
+        -- create directory to 'last' if it doesn't already exist
         rc, re = e2lib.mkdir_recursive(e2lib.dirname(lnk))
         if not rc then
             return false, e:cat(re)
         end
 
-        if e2lib.lstat(lnk) then
-            e2lib.unlink_recursive(lnk) -- ignore errors, symlink will catch it
+        -- last symlink or directory (or file) exists?
+        rc, re = e2lib.lstat(lnk)
+        if rc then
+            if rc.type == 'directory' then
+                rc, re = e2lib.unlink_recursive(lnk)
+                if not rc then
+                    return false, e:cat(re)
+                end
+            else
+                local st_tgt, st_dst
+
+                st_dst, re = e2lib.lstat(dst)
+                if not st_dst then
+                    return false, e:cat(re)
+                end
+
+                -- Follow link to target.
+                st_tgt, re = e2lib.stat(lnk)
+                if st_tgt and
+                    st_tgt.dev == st_dst.dev and st_tgt.ino == st_dst.ino then
+                    createsymlink = false
+                else
+                    -- If target of the link doesn't exists, delete the link.
+                    -- If target and dst are not identical, delete the link.
+                    rc, re = e2lib.unlink(lnk)
+                    if not rc then
+                        return false, e:cat(re)
+                    end
+                end
+            end
         end
 
-        rc, re = e2lib.symlink(dst, lnk)
-        if not rc then
-            return false, e:cat(re)
+        if createsymlink then
+            rc, re = e2lib.symlink(dst, lnk)
+            if not rc then
+                return false, e:cat(re)
+            end
         end
     end