Class: LogStash::Setting::WritableDirectory

Inherits:
LogStash::Setting show all
Defined in:
lib/logstash/settings.rb

Instance Attribute Summary

Attributes inherited from LogStash::Setting

#default, #name

Instance Method Summary collapse

Methods inherited from LogStash::Setting

#==, #reset, #set, #set?, #strict?, #to_hash, #validate_value

Constructor Details

#initialize(name, default = nil, strict = false) ⇒ WritableDirectory

Returns a new instance of WritableDirectory.



480
481
482
# File 'lib/logstash/settings.rb', line 480

def initialize(name, default=nil, strict=false)
  super(name, ::String, default, strict)
end

Instance Method Details

#validate(path) ⇒ Object



484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
# File 'lib/logstash/settings.rb', line 484

def validate(path)
  super(path)

  if ::File.directory?(path)
    if !::File.writable?(path)
      raise ::ArgumentError.new("Path \"#{path}\" must be a writable directory. It is not writable.")
    end
  elsif ::File.symlink?(path)
    # TODO(sissel): I'm OK if we relax this restriction. My experience
    # is that it's usually easier and safer to just reject symlinks.
    raise ::ArgumentError.new("Path \"#{path}\" must be a writable directory. It cannot be a symlink.")
  elsif ::File.exist?(path)
    raise ::ArgumentError.new("Path \"#{path}\" must be a writable directory. It is not a directory.")
  else
    parent = ::File.dirname(path)
    if !::File.writable?(parent)
      raise ::ArgumentError.new("Path \"#{path}\" does not exist and I cannot create it because the parent path \"#{parent}\" is not writable.")
    end
  end

  # If we get here, the directory exists and is writable.
  true
end

#valueObject



508
509
510
511
512
513
514
515
516
517
518
519
520
521
# File 'lib/logstash/settings.rb', line 508

def value
  super.tap do |path|
    if !::File.directory?(path)
      # Create the directory if it doesn't exist.
      begin
        logger.info("Creating directory", setting: name, path: path)
        ::FileUtils.mkdir_p(path)
      rescue => e
        # TODO(sissel): Catch only specific exceptions?
        raise ::ArgumentError.new("Path \"#{path}\" does not exist, and I failed trying to create it: #{e.class.name} - #{e}")
      end
    end
  end
end