Class: RStyx::Server::FileOnDisk

Inherits:
SFile
  • Object
show all
Defined in:
lib/rstyx/server.rb

Instance Attribute Summary

Attributes inherited from SFile

#atime, #gid, #mtime, #muid, #name, #parent, #permissions, #uid, #version

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from SFile

#add_changelistener, #add_client, #appendonly?, #auth?, #can_setlength?, #can_setmode?, #can_setmtime?, #can_setname?, #client, #client_connected, #client_disconnected, #contents_changed, #directory?, #exclusive?, #filetype, #full_path, #length, #length=, #mode, #mode=, #num_clients, #qid, #remove, #remove_client, #remove_dead_clients, #rename, #reply_read, #reply_write, #set_mtime, #stat, #uuid, #version_incr

Constructor Details

#initialize(path, name, perm) ⇒ FileOnDisk

Hidden initialize method. See self.new for the real thing.



1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
# File 'lib/rstyx/server.rb', line 1923

def initialize(path, name, perm)
  @name = name
  @path = File.expand_path(path)

  s = File.stat(@path)
  pwent = Etc.getpwuid(s.uid)
  grent = Etc.getgrgid(s.gid)
  argv = { :permissions => perm & s.mode, :apponly => false,
    :excl => false, :uid => pwent.name, :gid => grent.name }
  super(@name, argv)
end

Class Method Details

.new(path, name = nil, perm = nil) ⇒ Object

Create a new FileOnDisk whose path on the local filesystem is path, whose name as it appears on the server’s namespace is name, whose base permissions (which are ANDed with the file’s real permissions mask on the underlying filesystem) are perm. The file must already exist on the local filesystem. If name is not specified, it defaults to the basename of path. If perm is not specified, it defaults to 0666. If the path specified is actually a directory, returns a DirectoryOnDisk instance instead.

– FIXME: should create a DirectoryOnDisk instance instead if path actually represents a directory. ++



1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
# File 'lib/rstyx/server.rb', line 1899

def self.new(path, name=nil, perm=nil)
  unless File.exists?(path)
    raise StyxException.new("file #{path} does not exist on local filesystem")
  end

  if name.nil?
    name = File.basename(path)
  end

  if File.directory?(path)
    perm ||= 0777
    return(DirectoryOnDisk.new(path, name, perm))
  end

  perm ||= 0666
  obj = allocate
  obj.send(:initialize, path, name, perm)
  return(obj)
end

Instance Method Details

#deleteObject

Deletes the underlying file from the disk.



2001
2002
2003
2004
2005
# File 'lib/rstyx/server.rb', line 2001

def delete
  if File.exists?(@path)
    File.delete(@path)
  end
end

#read(client, offset, count) ⇒ Object

Read data from the file.

client

the SFileClient object representing the client reading from this file.

offset

the offset the client wants to read from

count

the number of bytes that the client wishes to read



1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
# File 'lib/rstyx/server.rb', line 1943

def read(client, offset, count)
  begin
    File.open(@path, "r") do |fp|
      fp.seek(offset)
      data = fp.read(count)
      if data.nil?
        data = ""
      end
      return(reply_read(data))
    end
  rescue Exception => e
    raise StyxException.new("An error of class #{e.class} occurred when trying to read from #{@path}: #{e.message}")
  end

end

#refreshObject

Refreshes the file’s stat information based on a real stat call



1992
1993
1994
1995
1996
# File 'lib/rstyx/server.rb', line 1992

def refresh
  s = File.stat(@path)
  @mtime = s.mtime
  @atime = s.atime
end

#write(client, offset, data, truncate) ⇒ Object

Writes data to the file.

client

the SFileClient object representing the client writing to this file

offset

the offset the client wants to write to

data

the data that the client wishes to write

truncate

true or false depending on whether the file is to be truncated.



1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
# File 'lib/rstyx/server.rb', line 1969

def write(client, offset, data, truncate)
  unless File.exists?(@path)
    # The underlying file was removed out from under us!
    self.remove
    raise StyxException.new("The file #{@path} was removed")
  end

  begin
    File.open(@path, "r+") do |fp|
      fp.seek(offset)
      count = fp.write(data)
      if truncate
        fp.truncate(offset + data.length)
      end
      reply_write(count, client.session)
    end
  rescue Exception => e
    raise StyxException.new("An error of class #{e.class} occurred when trying to write to #{@path}: #{e.message}")
  end
end