Skip to content

Commit

Permalink
* lib/net/ftp.rb (file?, directory?, appendable?, creatable?,
Browse files Browse the repository at this point in the history
  deletable?, enterable?, renamable?, listable?, directory_makable?,
  purgeable?, readable?, writable?): new methods.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51843 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
  • Loading branch information
shugo committed Sep 12, 2015
1 parent 2ec793a commit 2478c7b
Show file tree
Hide file tree
Showing 4 changed files with 210 additions and 2 deletions.
6 changes: 6 additions & 0 deletions ChangeLog
@@ -1,3 +1,9 @@
Sat Sep 12 23:06:51 2015 Shugo Maeda <shugo@ruby-lang.org>

* lib/net/ftp.rb (file?, directory?, appendable?, creatable?,
deletable?, enterable?, renamable?, listable?, directory_makable?,
purgeable?, readable?, writable?): new methods.

Sat Sep 12 21:27:22 2015 Shugo Maeda <shugo@ruby-lang.org>

* lib/net/ftp.rb (FACT_PARSERS): support system dependent facts
Expand Down
108 changes: 107 additions & 1 deletion lib/net/ftp.rb
Expand Up @@ -768,7 +768,113 @@ def list(*args, &block) # :yield: line
alias ls list
alias dir list

MLSxEntry = Struct.new(:facts, :pathname)
#
# MLSxEntry represents an entry in responses of MLST/MLSD.
# Each entry has the facts (e.g., size, last modification time, etc.)
# and the pathname.
#
class MLSxEntry
attr_reader :facts, :pathname

def initialize(facts, pathname)
@facts = facts
@pathname = pathname
end

#
# Returns +true+ if the entry is a file (i.e., the value of the type
# fact is file).
#
def file?
return facts["type"] == "file"
end

#
# Returns +true+ if the entry is a directory (i.e., the value of the
# type fact is dir, cdir, or pdir).
#
def directory?
if /\A[cp]?dir\z/.match(facts["type"])
return true
else
return false
end
end

#
# Returns +true+ if the APPE command may be applied to the file.
#
def appendable?
return facts["perm"].include?(?a)
end

#
# Returns +true+ if files may be created in the directory by STOU,
# STOR, APPE, and RNTO.
#
def creatable?
return facts["perm"].include?(?c)
end

#
# Returns +true+ if the file or directory may be deleted by DELE/RMD.
#
def deletable?
return facts["perm"].include?(?d)
end

#
# Returns +true+ if the directory may be entered by CWD/CDUP.
#
def enterable?
return facts["perm"].include?(?e)
end

#
# Returns +true+ if the file or directory may be renamed by RNFR.
#
def renamable?
return facts["perm"].include?(?f)
end

#
# Returns +true+ if the listing commands, LIST, NLST, and MLSD are
# applied to the directory.
#
def listable?
return facts["perm"].include?(?l)
end

#
# Returns +true+ if the MKD command may be used to create a new
# directory within the directory.
#
def directory_makable?
return facts["perm"].include?(?m)
end

#
# Returns +true+ if the objects in the directory may be deleted, or
# the directory may be purged.
#
def purgeable?
return facts["perm"].include?(?p)
end

#
# Returns +true+ if the RETR command may be applied to the file.
#
def readable?
return facts["perm"].include?(?r)
end

#
# Returns +true+ if the STOR command may be applied to the file.
#
def writable?
return facts["perm"].include?(?w)
end
end

CASE_DEPENDENT_PARSER = ->(value) { value }
CASE_INDEPENDENT_PARSER = ->(value) { value.downcase }
Expand Down
2 changes: 1 addition & 1 deletion test/net/ftp/test_buffered_socket.rb
Expand Up @@ -3,7 +3,7 @@
require "ostruct"
require "stringio"

class FTPTest < Test::Unit::TestCase
class BufferedSocketTest < Test::Unit::TestCase
def test_gets_empty
sock = create_buffered_socket("")
assert_equal(nil, sock.gets)
Expand Down
96 changes: 96 additions & 0 deletions test/net/ftp/test_mlsx_entry.rb
@@ -0,0 +1,96 @@
require "net/ftp"
require "test/unit"
require "ostruct"
require "stringio"

class MLSxEntryTest < Test::Unit::TestCase
def test_file?
assert_equal(true, Net::FTP::MLSxEntry.new({"type"=>"file"}, "foo").file?)
assert_equal(false, Net::FTP::MLSxEntry.new({"type"=>"dir"}, "foo").file?)
assert_equal(false, Net::FTP::MLSxEntry.new({"type"=>"cdir"}, "foo").file?)
assert_equal(false, Net::FTP::MLSxEntry.new({"type"=>"pdir"}, "foo").file?)
end

def test_directory?
assert_equal(false,
Net::FTP::MLSxEntry.new({"type"=>"file"}, "foo").directory?)
assert_equal(true,
Net::FTP::MLSxEntry.new({"type"=>"dir"}, "foo").directory?)
assert_equal(true,
Net::FTP::MLSxEntry.new({"type"=>"cdir"}, "foo").directory?)
assert_equal(true,
Net::FTP::MLSxEntry.new({"type"=>"pdir"}, "foo").directory?)
end

def test_appendable?
assert_equal(true,
Net::FTP::MLSxEntry.new({"perm"=>"a"}, "foo").appendable?)
assert_equal(false,
Net::FTP::MLSxEntry.new({"perm"=>""}, "foo").appendable?)
end

def test_creatable?
assert_equal(true,
Net::FTP::MLSxEntry.new({"perm"=>"c"}, "foo").creatable?)
assert_equal(false,
Net::FTP::MLSxEntry.new({"perm"=>""}, "foo").creatable?)
end

def test_deletable?
assert_equal(true,
Net::FTP::MLSxEntry.new({"perm"=>"d"}, "foo").deletable?)
assert_equal(false,
Net::FTP::MLSxEntry.new({"perm"=>""}, "foo").deletable?)
end

def test_enterable?
assert_equal(true,
Net::FTP::MLSxEntry.new({"perm"=>"e"}, "foo").enterable?)
assert_equal(false,
Net::FTP::MLSxEntry.new({"perm"=>""}, "foo").enterable?)
end

def test_renamable?
assert_equal(true,
Net::FTP::MLSxEntry.new({"perm"=>"f"}, "foo").renamable?)
assert_equal(false,
Net::FTP::MLSxEntry.new({"perm"=>""}, "foo").renamable?)
end

def test_listable?
assert_equal(true,
Net::FTP::MLSxEntry.new({"perm"=>"l"}, "foo").listable?)
assert_equal(false,
Net::FTP::MLSxEntry.new({"perm"=>""}, "foo").listable?)
end

def test_directory_makable?
assert_equal(true,
Net::FTP::MLSxEntry.new({"perm"=>"m"}, "foo").
directory_makable?)
assert_equal(false,
Net::FTP::MLSxEntry.new({"perm"=>""}, "foo").
directory_makable?)
end

def test_purgeable?
assert_equal(true,
Net::FTP::MLSxEntry.new({"perm"=>"p"}, "foo").purgeable?)
assert_equal(false,
Net::FTP::MLSxEntry.new({"perm"=>""}, "foo").purgeable?)
end

def test_readable?
assert_equal(true,
Net::FTP::MLSxEntry.new({"perm"=>"r"}, "foo").readable?)
assert_equal(false,
Net::FTP::MLSxEntry.new({"perm"=>""}, "foo").readable?)
end

def test_writable?
assert_equal(true,
Net::FTP::MLSxEntry.new({"perm"=>"w"}, "foo").writable?)
assert_equal(false,
Net::FTP::MLSxEntry.new({"perm"=>""}, "foo").writable?)
end
end

0 comments on commit 2478c7b

Please sign in to comment.