class RBT::ReplaceSymlinks

Constants

ARRAY_ALLOWED_DIRS
#

ARRAY_ALLOWED_DIRS

ARRAY_ALLOWED_DIRS specifies from which directories to run this script.

We only allow it from specific directories such as /usr/bin and others.

#
NAMESPACE
#

NAMESPACE

#
REMOVE_INSTEAD_OF_SYMLINKING
#

REMOVE_INSTEAD_OF_SYMLINKING

Set this to true if you want to remove rather than symlink.

It may be better to just symlink, rather than remove anyway, hence why this constant is usually kept at false.

#

Public Class Methods

new( this_dir = return_pwd, remove_instead_of_symlinking = REMOVE_INSTEAD_OF_SYMLINKING, run_already = true ) click to toggle source
#

initialize

“this_dir” is the allowed dir, our target.

#
# File lib/rbt/utility_scripts/replace_symlinks.rb, line 52
def initialize(
    this_dir                     = return_pwd, 
    remove_instead_of_symlinking = REMOVE_INSTEAD_OF_SYMLINKING,
    run_already                  = true
  )
  reset
  set_this_dir(this_dir)
  set_remove_symlink(remove_instead_of_symlinking)
  run if run_already
end

Public Instance Methods

allowed_dirs?() click to toggle source
#

allowed_dirs?

#
# File lib/rbt/utility_scripts/replace_symlinks.rb, line 133
def allowed_dirs?
  @array_allowed_dirs
end
append_this_directory_to_the_allowed_directories(i) click to toggle source
#

append_this_directory_to_the_allowed_directories

#
# File lib/rbt/utility_scripts/replace_symlinks.rb, line 126
def append_this_directory_to_the_allowed_directories(i)
  @array_allowed_dirs << i
end
collect_files() click to toggle source
#

collect_files

Collects all files from a given dir.

#
# File lib/rbt/utility_scripts/replace_symlinks.rb, line 159
def collect_files
  @array_files = Dir[@this_dir+'*'].select {|entry| File.file? entry }
  ppexit @array_files if @debug
end
feedback_allowed_directories() click to toggle source
#

feedback_allowed_directories

#
# File lib/rbt/utility_scripts/replace_symlinks.rb, line 191
def feedback_allowed_directories
  opnn; e "The #{salmon('allowed')} directories are:"
  print '  '; pp ARRAY_ALLOWED_DIRS
end
is_allowed_dir?(d) click to toggle source
#

is_allowed_dir?

Returns true if the dir is allowed, otherwise it returns false.

#
# File lib/rbt/utility_scripts/replace_symlinks.rb, line 117
def is_allowed_dir?(d)
  _ = false
  _ = true if @array_allowed_dirs.include? d
  return _
end
replace_files() click to toggle source
#

replace_files

Normally this is used to symlink, but we can also remove.

#
# File lib/rbt/utility_scripts/replace_symlinks.rb, line 169
def replace_files
  @array_files.each { |file|
    target_file = sysbin_directory?+File.basename(file)
    if File.exist? target_file
      if @remove_symlink
        opn; e "Removing #{sfile(file)}."
      else
        opn; e 'Replacing '+sfile(file)+' with '+
                sfile(target_file)+'.' # Wanna remove /bin/unlink.
      end
      if @simulate_only
      else
        remove(file) # Need to remove the old file.
        symlink(file, target_file) if @remove_symlink == false
      end
    end
  }
end
reset() click to toggle source
#

reset

#
Calls superclass method RBT::Base#reset
# File lib/rbt/utility_scripts/replace_symlinks.rb, line 66
def reset
  super()
  @namespace = NAMESPACE
  @debug = false
  @simulate_only = true
  @array_allowed_dirs = ARRAY_ALLOWED_DIRS
end
run() click to toggle source
#

run (run tag)

We collect the files, then we replace them.

#
# File lib/rbt/utility_scripts/replace_symlinks.rb, line 201
def run
  collect_files
  replace_files
end
set_this_dir(i) click to toggle source
#

set_this_dir

#
# File lib/rbt/utility_scripts/replace_symlinks.rb, line 77
def set_this_dir(i)
  i = i.first if i.is_a? Array
  i = Dir.pwd if i.nil?
  case i # case tag
  # ======================================================================= #
  # === replace_symlinks --help
  # ======================================================================= #
  when 'HELP',/-?-?help/
    show_help
    exit
  # ======================================================================= #
  # === replace_symlinks --directory=/opt
  # ======================================================================= #
  when /--directory=(.+)/ # This is allowed to bypass the allowed directory.
    i = $1.to_s.dup
    append_this_directory_to_the_allowed_directories(i)
  end
  unless is_allowed_dir? i # Verify that the directory is proper. Report otherwise.
    e 'Not allowed directory: '+sdir(i)
    feedback_allowed_directories
    e 'Consider changing or using one of the directories listed above.'
    e 'Hint: You can do so via --directory='
    raise NotAllowedDirectory, 'not allowed directory' 
  end
  i << '/' unless i.end_with? '/'
  @this_dir = i
end
show_help() click to toggle source
#

show_help (help tag)

#
# File lib/rbt/utility_scripts/replace_symlinks.rb, line 140
def show_help
  opn(:no_colon)
  e
  e
  e 'This class can either replace or remove symlinks.'
  e 'In order to use this, navigate to the target directory '\
    'in question'
  e 'so that it becomes the current working directory (cwd / pwd).'
  e
  e 'Alternatively, you can use the option --directory= to assign'
  e 'to a specific directory.'
  e
end