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



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

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

#exists?Boolean

Returns:

  • (Boolean)


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

def exists?
  lstat[:st_blocks] > 0
end

#lstatObject



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

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

#read(buf_size = 4092) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/glusterfs/file.rb', line 24

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
# File 'lib/glusterfs/file.rb', line 9

def read_file(buf_size = 4092)
  check_exists
  fd = GlusterFS.open(@volume.fs, @path, 0)
  temp = Tempfile.new(path.gsub('/', '-'))
  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



71
72
73
# File 'lib/glusterfs/file.rb', line 71

def size
  lstat[:st_size]
end

#write(data, perms = 0644) ⇒ Object



50
51
52
53
54
55
# File 'lib/glusterfs/file.rb', line 50

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



39
40
41
42
43
44
45
46
47
48
# File 'lib/glusterfs/file.rb', line 39

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