]> git.draconx.ca Git - homepage.git/blobdiff - lib/deadlinks.rb
Fix symlink handling in content directory.
[homepage.git] / lib / deadlinks.rb
diff --git a/lib/deadlinks.rb b/lib/deadlinks.rb
new file mode 100644 (file)
index 0000000..adcb25f
--- /dev/null
@@ -0,0 +1,49 @@
+# Nick's web site: Workarounds for Nanoc's busted handling of broken symlinks.
+#
+# Copyright © 2020 Nick Bowler
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <https://www.gnu.org/licenses/>.
+
+# Monkey patch the filesystem data source to adjust some strange behavoiur.
+class Nanoc::DataSources::Filesystem < Nanoc::DataSource
+    # The filesystem mtime_of crashes on broken symlinks.  Let's not do that,
+    # instead we'll fall back to the link mtime for broken links.
+    def mtime_of(*args)
+        mtimes = args.compact.map do |f|
+            File.stat(f).mtime
+        rescue Errno::ENOENT
+            File.lstat(f).mtime
+        end
+        mtimes.max
+    end
+
+    module Tools
+        @dummy_file = Tempfile.new('dummy')
+
+        # The original resolve_symlink helper uses a readlink loop which
+        # crashes on broken links.  I'm also fairly sure symlinks in path
+        # components are not correctly resolved.  Let's just use realpath
+        # instead and hope ruby itself doesn't have these bugs.
+        def resolve_symlink(filename, recursion_limit = nil)
+            File.realpath(filename)
+        rescue Errno::ENOENT
+            # Dead link.  Return a valid filename otherwise the filesystem
+            # source does bizarre things.  The actual items are created with
+            # the original filename so this dummy file does not appear to
+            # leak outside of the filesystem data source implementation.
+            @dummy_file.path
+        end
+        module_function :resolve_symlink
+    end
+end