Class: GlusterFS::File

Inherits:
Object
  • Object
show all
Defined in:
lib/glusterfs/file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(volume, path) ⇒ File

Returns a new instance of File.



4
5
6
7
# File 'lib/glusterfs/file.rb', line 4

def initialize(volume, path)
  @volume = volume
  @path = path
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



3
4
5
# File 'lib/glusterfs/file.rb', line 3

def path
  @path
end

#volumeObject (readonly)

Returns the value of attribute volume.



3
4
5
# File 'lib/glusterfs/file.rb', line 3

def volume
  @volume
end

Instance Method Details

#deleteObject



63
64
65
# File 'lib/glusterfs/file.rb', line 63

def delete
  GlusterFS.unlink(@volume.fs, @path)
end

#exists?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/glusterfs/file.rb', line 73

def exists?
  GlusterFS::Stat.file?(lstat)
end

#lstatObject



67
68
69
70
71
# File 'lib/glusterfs/file.rb', line 67

def lstat
  data = GlusterFS::Stat.new
  GlusterFS.lstat(@volume.fs, @path, data)
  data
end

#read(buf_size = 4092) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/glusterfs/file.rb', line 30

def read(buf_size = 4092)
  check_exists
  fd = GlusterFS.open(@volume.fs, @path, 0)
  temp = ''
  buff = FFI::MemoryPointer.new(:char, buf_size)
  res = 1
  lstat
  while res > 0
    res = GlusterFS.read(fd, buff, buf_size, 0)
    temp << buff.get_bytes(0, res) if res > 0
  end
  GlusterFS.close(fd)
  temp
end

#read_file(buf_size = 4092) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/glusterfs/file.rb', line 9

def read_file(buf_size = 4092)
  check_exists
  fd = GlusterFS.open(@volume.fs, @path, 0)
  if path.include?('.')
    ext = '.' << path.split('.').pop
    temp = Tempfile.new([path.gsub('/', '-'), ext])
  else
    temp = Tempfile.new(path.gsub('/', '-'))
  end
  temp.binmode
  buff = FFI::MemoryPointer.new(:char, buf_size)
  res = 1
  while res > 0
    res = GlusterFS.read(fd, buff, buf_size, 0)
    temp.write(buff.get_bytes(0, res)) if res > 0
  end
  GlusterFS.close(fd)
  temp.rewind
  temp
end

#sizeObject



77
78
79
# File 'lib/glusterfs/file.rb', line 77

def size
  lstat[:st_size]
end

#write(data, perms = 0644) ⇒ Object



56
57
58
59
60
61
# File 'lib/glusterfs/file.rb', line 56

def write(data, perms = 0644)
  fd = GlusterFS.creat(@volume.fs, path, 2, perms)
  res = GlusterFS.write(fd, data, data.size, 0)
  GlusterFS.close(fd)
  res
end

#write_file(file, perms = 0644, buffer_size = 4092) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/glusterfs/file.rb', line 45

def write_file(file, perms = 0644, buffer_size = 4092)
  fd = GlusterFS.creat(@volume.fs, path, 2, perms)
  res = 0
  until file.eof?
    chunk = file.read(buffer_size)
    res += GlusterFS.write(fd, chunk, chunk.size , 0)
  end
  GlusterFS.close(fd)
  res
end