Skip to content

Commit e4a8098

Browse files
authored
Fix exception when permission denied on symlinks (#340)
Fix exception when permission denied on symlinks
2 parents 4befc68 + 6b2cbdc commit e4a8098

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

lib/colorls/core.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,8 @@ def long_info(content)
270270
def symlink_info(content)
271271
return '' unless @long && content.symlink?
272272

273-
link_info = " ⇒ #{content.link_target}"
273+
target = content.link_target.nil? ? '…' : content.link_target
274+
link_info = " ⇒ #{target}"
274275
if content.dead?
275276
"#{link_info} [Dead link]".colorize(@colors[:dead_link])
276277
else

lib/colorls/fileinfo.rb

+10-4
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@ def initialize(path, link_info=true)
1616
@name = File.basename(path)
1717
@stats = File.lstat(path)
1818

19-
return unless link_info && @stats.symlink?
20-
21-
@dead = !File.exist?(path)
22-
@target = File.readlink(path)
19+
handle_symlink(path) if link_info && @stats.symlink?
2320
end
2421

2522
def self.info(path)
@@ -58,5 +55,14 @@ def to_s
5855
end
5956

6057
def_delegators :@stats, :directory?, :socket?, :chardev?, :symlink?, :blockdev?, :mtime, :nlink, :size, :owned?
58+
59+
private
60+
61+
def handle_symlink(path)
62+
@target = File.readlink(path)
63+
@dead = !File.exist?(path)
64+
rescue SystemCallError => e
65+
STDERR.puts "cannot read symbolic link: #{e}"
66+
end
6167
end
6268
end

0 commit comments

Comments
 (0)