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.



438
439
440
# File 'lib/logstash/settings.rb', line 438

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

Instance Method Details

#validate(path) ⇒ Object



442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
# File 'lib/logstash/settings.rb', line 442

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



466
467
468
469
470
471
472
473
474
475
476
477
478
479
# File 'lib/logstash/settings.rb', line 466

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