Class: Qfs::Client
- Inherits:
-
BaseClient
- Object
- BaseClient
- Qfs::Client
- 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
-
.with_client(host, port) {|Client| ... } ⇒ Object
Open a connection on the specified host and post, and yield it to a block.
Instance Method Summary collapse
-
#cd(path) ⇒ Object
Change the current working directory.
-
#chmod(path, mode_int, options = {}) ⇒ Object
Change the permissions of a file or directory.
-
#cwd ⇒ String
Return the current working directory.
-
#mkdir(path, mode = 0o600) ⇒ Bool
Create a directory.
-
#mkdir_p(path, mode = 0o600) ⇒ Bool
Create a directory and create parent directories if needed.
-
#move(old, new) ⇒ Object
Move a file to a new path.
-
#open(path, mode_str, options = {}) {|File| ... } ⇒ File
Open a file on QFS.
-
#read(path, len = nil) ⇒ String
Read from a file and return the data.
-
#readdir(path) {|Attr| ... } ⇒ Array<Attr>
Read from a directory, optionally outputting a list of Attr objects or yielding to a block.
-
#remove(path, force = false) ⇒ Bool
Remove a regular file.
-
#rm_rf(path, force = false) ⇒ Bool
Recursively remove directories and files.
-
#rmdir(path, force = false) ⇒ Bool
Remove a directory.
-
#rmdirs(path, force = false) ⇒ Bool
Remove a directory recursively.
-
#setwd(path) ⇒ Object
Set the current working directory.
-
#stat(path, options = {}) ⇒ Attr
Get an Attr object for the file at the specified path.
-
#write(path, data) ⇒ String
Write to a file.
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
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
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
218 219 220 221 |
# File 'lib/qfs.rb', line 218 def chmod(path, mode_int, = {}) return chmod_r(path, mode_int) if [:recursive] super(path, mode_int) end |
#cwd ⇒ String
Return 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
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
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.
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
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, = {}) flags = mode_to_flags(mode_str) raise Qfs::Error, "#{mode_str} is not a valid mode string" if flags.nil? mode ||= [:mode] params ||= [: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
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
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.
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.
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
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
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
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
234 235 236 237 238 239 240 |
# File 'lib/qfs.rb', line 234 def stat(path, = {}) # 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 [:refresh] super(path) end |
#write(path, data) ⇒ String
Write to a file
180 181 182 |
# File 'lib/qfs.rb', line 180 def write(path, data) open(path, 'w') { |f| f.write(data) } end |