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)


905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
# File 'lib/libcfruby/fileops.rb', line 905

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. It returns false when filename does not exist (i.e. symlink points to nothing).

In the case the symlink does not exist a FileOpsWrongFiletypeError is thrown.

Returns:

  • (Boolean)

Raises:



936
937
938
939
940
941
942
943
944
945
946
947
948
949
# File 'lib/libcfruby/fileops.rb', line 936

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

  return false if !filename.exist? 
  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



877
878
879
880
881
882
883
884
885
886
887
# File 'lib/libcfruby/fileops.rb', line 877

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



892
893
894
895
896
897
898
899
900
901
902
# File 'lib/libcfruby/fileops.rb', line 892

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