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

Methods included from Util::Loggable

included, #logger, #slow_logger

Constructor Details

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

Returns a new instance of WritableDirectory.



411
412
413
# File 'lib/logstash/settings.rb', line 411

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

Instance Method Details

#validate(path) ⇒ Object



415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
# File 'lib/logstash/settings.rb', line 415

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



439
440
441
442
443
444
445
446
447
448
449
450
451
452
# File 'lib/logstash/settings.rb', line 439

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