Class: LogStash::Outputs::Swift::TemporaryFile

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/logstash/outputs/swift/temporary_file.rb

Overview

Wrap the actual file descriptor into an utility classe It make it more OOP and easier to reason with the paths.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, fd, temp_path) ⇒ TemporaryFile

Returns a new instance of TemporaryFile.



18
19
20
21
22
23
# File 'lib/logstash/outputs/swift/temporary_file.rb', line 18

def initialize(key, fd, temp_path)
  @fd = fd
  @key = key
  @temp_path = temp_path
  @created_at = Time.now
end

Instance Attribute Details

#fdObject (readonly)

Returns the value of attribute fd.



16
17
18
# File 'lib/logstash/outputs/swift/temporary_file.rb', line 16

def fd
  @fd
end

Class Method Details

.create_from_existing_file(file_path, temporary_folder) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/logstash/outputs/swift/temporary_file.rb', line 61

def self.create_from_existing_file(file_path, temporary_folder)
  key_parts = Pathname.new(file_path).relative_path_from(temporary_folder).to_s.split(::File::SEPARATOR)

  TemporaryFile.new(key_parts.slice(1, key_parts.size).join("/"),
                 ::File.open(file_path, "r"),
                 ::File.join(temporary_folder, key_parts.slice(0, 1)))
end

Instance Method Details

#ctimeObject



25
26
27
# File 'lib/logstash/outputs/swift/temporary_file.rb', line 25

def ctime
  @created_at
end

#delete!Object

Each temporary file is made inside a directory named with an UUID, instead of deleting the file directly and having the risk of deleting other files we delete the root of the UUID, using a UUID also remove the risk of deleting unwanted file, it acts as a sandbox.



52
53
54
55
# File 'lib/logstash/outputs/swift/temporary_file.rb', line 52

def delete!
  @fd.close rescue IOError # force close anyway
  FileUtils.rm_r(@temp_path, :secure => true)
end

#empty?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/logstash/outputs/swift/temporary_file.rb', line 57

def empty?
  size == 0
end

#keyObject



44
45
46
# File 'lib/logstash/outputs/swift/temporary_file.rb', line 44

def key
  @key.gsub(/^\//, "")
end

#sizeObject



33
34
35
36
37
38
39
40
41
42
# File 'lib/logstash/outputs/swift/temporary_file.rb', line 33

def size
  # Use the fd size to get the accurate result,
  # so we dont have to deal with fsync
  # if the file is close we will use the File::size
  begin
    @fd.size
  rescue IOError
    ::File.size(path)
  end
end

#temp_pathObject



29
30
31
# File 'lib/logstash/outputs/swift/temporary_file.rb', line 29

def temp_path
  @temp_path
end