Module: Cfruby::FileOps::SymlinkHandler

Defined in:
lib/libcfruby/fileops.rb

Overview

Methods for standard operations involving symbolic links

Class Method Summary collapse

Class Method Details

.broken?(symlink) ⇒ Boolean

Returns true if a file is a broken symlink

Returns:

  • (Boolean)


712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
# File 'lib/libcfruby/fileops.rb', line 712

def SymlinkHandler.broken?(symlink)
 if(!symlink.kind_of?(Pathname))
   symlink = Pathname.new(symlink.to_s)
  end
  
  if(!symlink.symlink?())
    return(false)
   end
  
  # expand the path and catch the ensuing error in the case of a broken link
  begin
    symlink.realpath()
   rescue
     if($!.kind_of?(Errno::ENOENT) and $!.to_s =~ /^no such file/i)
       return(true)
      else
        raise($!)
      end
    end
    
    return(false)
end

.points_to?(symlink, filename) ⇒ Boolean

Returns whether a symlink is actually pointing to filename. Both parameters may be strings or File objects. This method is used by Cfenjin to ascertain that when a symlink exists it points to the right file

Returns:

  • (Boolean)

Raises:



739
740
741
742
743
744
745
746
747
748
749
750
751
# File 'lib/libcfruby/fileops.rb', line 739

def SymlinkHandler.points_to?(symlink, filename)
  if(!filename.kind_of?(Pathname))
    filename = Pathname.new(filename.to_s)
   end

  if(!symlink.kind_of?(Pathname))
    symlink = Pathname.new(symlink.to_s)
   end

	raise FileOpsWrongFiletypeError if !symlink.symlink?

	return filename.realpath.to_s == symlink.realpath.to_s
end

.stat(filename) ⇒ Object

Returns File.stat unless it is a symbolic link not pointing to an existing file - in that case it returns File.lstat



684
685
686
687
688
689
690
691
692
693
694
# File 'lib/libcfruby/fileops.rb', line 684

def SymlinkHandler.stat(filename)
  if(!filename.kind_of?(Pathname))
    filename = Pathname.new(filename.to_s)
   end

	if(filename.symlink? and broken?(filename))
		return File.lstat(filename)
	end
	
	return(File.stat(filename))
end

the stdlib Pathname.unlink balks when removing a symlink - this method will call File.unlink instead when dealing with a symlink



699
700
701
702
703
704
705
706
707
708
709
# File 'lib/libcfruby/fileops.rb', line 699

def SymlinkHandler.unlink(filename)
  if(!filename.kind_of?(Pathname))
    filename = Pathname.new(filename.to_s)
   end

	if filename.symlink?()
		File.unlink filename.expand_path
	else
		filename.unlink()
	end
end