Class: Qfs::Client

Inherits:
BaseClient show all
Defined in:
lib/qfs.rb

Overview

A higher-level Client to interact with QFS. This attempts to use a similar interface to ruby’s native IO functionality.

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseClient

#exists, #initialize, #isdirectory, #isfile, #release, #rename

Constructor Details

This class inherits a constructor from Qfs::BaseClient

Class Method Details

.with_client(host, port) {|Client| ... } ⇒ Object

Open a connection on the specified host and post, and yield it to a block

Examples:

Open a connection and yield to a block

Qfs::Client.with_client('localhost', 10000) do |client|
  client.write('/file', '')
end

Parameters:

  • host (String)

    the hostname to connect to

  • port (Int)

    the port to connect to

Yields:



67
68
69
70
71
72
73
74
# File 'lib/qfs.rb', line 67

def self.with_client(host, port)
  c = new(host, port)
  begin
    yield c
  ensure
    c.release
  end
end

Instance Method Details

#cd(path) ⇒ Object

Change the current working directory

Parameters:

  • path (String)

    The directory to change to



253
254
255
# File 'lib/qfs.rb', line 253

def cd(path)
  super(path)
end

#chmod(path, mode_int, options = {}) ⇒ Object

Change the permissions of a file or directory

Parameters:

  • path (String)

    Path to the file to chmod

  • mode_int (Int)

    The permissions to set

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :recursive (Bool)

    Set to recursively chmod the directory



218
219
220
221
# File 'lib/qfs.rb', line 218

def chmod(path, mode_int, options = {})
  return chmod_r(path, mode_int) if options[:recursive]
  super(path, mode_int)
end

#cwdString

Return the current working directory

Parameters:

  • len (Int)

    The length of the buffer that should be allocated to store the cwd. An exception will be thrown if it is too small.

Returns:

  • (String)

    The current working directory



271
272
273
# File 'lib/qfs.rb', line 271

def cwd
  super
end

#mkdir(path, mode = 0o600) ⇒ Bool

Create a directory

Parameters:

  • path (String)

    the path to the directory to make

  • mode (Int) (defaults to: 0o600)

    the permissions to set on the new directory

Returns:

  • (Bool)

    if the directory was created



103
104
105
# File 'lib/qfs.rb', line 103

def mkdir(path, mode = 0o600)
  super(path, mode)
end

#mkdir_p(path, mode = 0o600) ⇒ Bool

Create a directory and create parent directories if needed

Parameters:

  • path (String)

    the path to the directory to make

  • mode (Int) (defaults to: 0o600)

    the permissions to set on the new directory

Returns:

  • (Bool)

    if the directory was created



113
114
115
# File 'lib/qfs.rb', line 113

def mkdir_p(path, mode = 0o600)
  super(path, mode)
end

#move(old, new) ⇒ Object

Move a file to a new path.

Parameters:

  • old (String)

    The path to the file to move

  • new (String)

    The new destination



246
247
248
# File 'lib/qfs.rb', line 246

def move(old, new)
  rename(old, new)
end

#open(path, mode_str, options = {}) {|File| ... } ⇒ File

Open a file on QFS. This method uses a very similar interface to the ‘open’ method standard in ruby.

Modes

* 'r': Read only
* 'w': Write only, overwrite or create new file
* 'a': Append to the file

Parameters:

  • path (String)

    the path to the file

  • mode_str (Int)

    One of the mode types above

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :mode (Int)

    permissions to set on the file

  • :params (String)

Yields:

  • (File)

    a Qfs::File

Returns:

  • (File)

    a Qfs::File

Raises:



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/qfs.rb', line 34

def open(path, mode_str, options = {})
  flags = mode_to_flags(mode_str)
  raise Qfs::Error, "#{mode_str} is not a valid mode string" if flags.nil?

  mode ||= options[:mode]
  params ||= options[:params]
  f = super(path, flags, mode, params)

  # If the file was opened in append mode, seek to the end to simulate
  # O_APPEND behavior
  f.seek(0, IO::SEEK_END) if mode_str == 'a'

  return f unless block_given?

  begin
    yield f
  ensure
    f.close
  end
end

#read(path, len = nil) ⇒ String

Read from a file and return the data

Parameters:

  • path (String)

    the path to the file

  • len (Int) (defaults to: nil)

    the number of bytes to read

Returns:

  • (String)

    the data from the file



170
171
172
# File 'lib/qfs.rb', line 170

def read(path, len = nil)
  open(path, 'r') { |f| f.read(len) }
end

#readdir(path) {|Attr| ... } ⇒ Array<Attr>

Read from a directory, optionally outputting a list of Attr objects or yielding to a block

Examples:

Usage with a block:

client.readdir('/') do |paths|
  puts paths.filename
end

Usage without a block:

client.readdir('/').each do |paths|
  puts paths.filename
end

Parameters:

  • path (String)

    the path to read from

Yields:

  • (Attr)

    Attr objects

Returns:

  • (Array<Attr>)

    a list of Attr objects



202
203
204
205
206
207
208
209
210
211
# File 'lib/qfs.rb', line 202

def readdir(path)
  attrs = []
  super(path) do |attr|
    unless current_or_previous_dir?(attr.filename)
      block_given? ? yield(attr) : attrs.push(attr)
    end
  end

  return attrs unless block_given?
end

#remove(path, force = false) ⇒ Bool

Remove a regular file. Pass ‘true’ to stop exceptions from being thrown if the file doesn’t exist.

Parameters:

  • path (String)

    the path to the file

  • force (Bool) (defaults to: false)

    Wheather or not to throw an exception if file doesn’t exist.

Returns:

  • (Bool)

    if or if not the method succeeded

Raises:

  • (Error)

    if force=false



93
94
95
# File 'lib/qfs.rb', line 93

def remove(path, force = false)
  force_remove(force) { super(path) }
end

#rm_rf(path, force = false) ⇒ Bool

Recursively remove directories and files.

Parameters:

  • path (String)

    the path to the file

  • force (Bool) (defaults to: false)

    Whether or not to throw an exception if the directory doesn’t exist.

Returns:

  • (Bool)

    if or if not the method succeeded

Raises:

  • (Error)

    if force=false



152
153
154
155
156
157
158
159
160
161
162
# File 'lib/qfs.rb', line 152

def rm_rf(path, force = false)
  force_remove(force) do
    return remove(path) if file?(path)
    readdir(path) do |f|
      fpath = ::File.join(path, f.filename)
      rm_rf(fpath) if directory?(fpath)
      remove(fpath) if file?(fpath)
    end
    rmdir(path)
  end
end

#rmdir(path, force = false) ⇒ Bool

Remove a directory

Parameters:

  • path (String)

    the path to the file

  • force (Bool) (defaults to: false)

    Whether or not to throw an exception if the operation fails

Returns:

  • (Bool)

    if or if not the method succeeded

Raises:

  • (Error)

    if force=false



126
127
128
# File 'lib/qfs.rb', line 126

def rmdir(path, force = false)
  force_remove(force) { super(path) }
end

#rmdirs(path, force = false) ⇒ Bool

Remove a directory recursively

Parameters:

  • path (String)

    the path to the file

  • force (Bool) (defaults to: false)

    Whether or not to throw an exception if the directory doesn’t exist.

Returns:

  • (Bool)

    if or if not the method succeeded

Raises:

  • (Error)

    if force=false



139
140
141
# File 'lib/qfs.rb', line 139

def rmdirs(path, force = false)
  force_remove(force) { super(path) }
end

#setwd(path) ⇒ Object

Set the current working directory

Parameters:

  • path (String)

    The directory to change to



260
261
262
# File 'lib/qfs.rb', line 260

def setwd(path)
  super(path)
end

#stat(path, options = {}) ⇒ Attr

Get an Attr object for the file at the specified path

Note that this method will cache it’s result for a specific file for the entire lifetime of a Client object. If you need to get an updated Attr for a file/directory, you need to create a new Client.

and reopened to guarantee stat returns updated values

Parameters:

  • path (String)

    The path to the file or directory to stat

  • options (Bool) (defaults to: {})

    :refresh If this is set, the file will be opened

Returns:

  • (Attr)

    An attr object



234
235
236
237
238
239
240
# File 'lib/qfs.rb', line 234

def stat(path, options = {})
  # close the file and repoen it guarantee that the newest data is present
  # opening in a block will guarantee it is closed
  open(path, 'w') { } if options[:refresh]

  super(path)
end

#write(path, data) ⇒ String

Write to a file

Parameters:

  • path (String)

    the path to the file

  • data (String)

    the data to be written

Returns:

  • (String)

    the number of bytes written



180
181
182
# File 'lib/qfs.rb', line 180

def write(path, data)
  open(path, 'w') { |f| f.write(data) }
end