Branching mogilefs-client to version 1.2.1
[ruby-mogilefs-client.git] / lib / mogilefs / nfsfile.rb
blobd4040c250e25c32db1515a283bb1e47adca987cc
1 require 'mogilefs/backend'
3 ##
4 # NFSFile wraps up the new file operations for storing files onto an NFS
5 # storage node.
7 # You really don't want to create an NFSFile by hand.  Instead you want to
8 # create a new file using MogileFS::MogileFS.new_file.
10 class MogileFS::NFSFile < File
12   ##
13   # The path of this file not including the local mount point.
15   attr_reader :path
17   ##
18   # The key for this file.  This key won't represent a real file until you've
19   # called #close.
21   attr_reader :key
23   ##
24   # The class of this file.
26   attr_reader :class
28   class << self
30     ##
31     # Wraps up File.new with MogileFS-specific data.  Use
32     # MogileFS::MogileFS#new_file instead of this method.
34     def new(mg, fid, path, devid, klass, key)
35       fp = super join(mg.root, path), 'w+'
36       fp.send :setup, mg, fid, path, devid, klass, key
37       return fp
38     end
40     ##
41     # Wraps up File.open with MogileFS-specific data.  Use
42     # MogileFS::MogileFS#new_file instead of this method.
44     def open(mg, fid, path, devid, klass, key, &block)
45       fp = new mg, fid, path, devid, klass, key
47       return fp if block.nil?
49       begin
50         yield fp
51       ensure
52         fp.close
53       end
54     end
56   end
58   ##
59   # Closes the file handle and marks it as closed in MogileFS.
61   def close
62     super
63     @mg.backend.create_close(:fid => @fid, :devid => @devid,
64                              :domain => @mg.domain, :key => @key,
65                              :path => @path)
66     return nil
67   end
69   private
71   def setup(mg, fid, path, devid, klass, key)
72     @mg = mg
73     @fid = fid
74     @path = path
75     @devid = devid
76     @klass = klass
77     @key = key
78   end
80 end