Class: GlusterFS::File
- Inherits:
-
Object
- Object
- GlusterFS::File
- Defined in:
- lib/glusterfs/file.rb
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#volume ⇒ Object
readonly
Returns the value of attribute volume.
Instance Method Summary collapse
- #delete ⇒ Object
- #exists? ⇒ Boolean
-
#initialize(volume, path) ⇒ File
constructor
A new instance of File.
- #lstat ⇒ Object
- #read(buf_size = 4092) ⇒ Object
- #read_file(buf_size = 4092) ⇒ Object
- #size ⇒ Object
- #write(data, perms = 0644) ⇒ Object
- #write_file(file, perms = 0644, buffer_size = 4092) ⇒ Object
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
#path ⇒ Object (readonly)
Returns the value of attribute path.
3 4 5 |
# File 'lib/glusterfs/file.rb', line 3 def path @path end |
#volume ⇒ Object (readonly)
Returns the value of attribute volume.
3 4 5 |
# File 'lib/glusterfs/file.rb', line 3 def volume @volume end |
Instance Method Details
#delete ⇒ Object
63 64 65 |
# File 'lib/glusterfs/file.rb', line 63 def delete GlusterFS.unlink(@volume.fs, @path) end |
#exists? ⇒ Boolean
73 74 75 |
# File 'lib/glusterfs/file.rb', line 73 def exists? GlusterFS::Stat.file?(lstat) end |
#lstat ⇒ Object
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 |
#size ⇒ Object
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 |