From 2478c7bc33b37867d8fd17e8199f524d0111f69d Mon Sep 17 00:00:00 2001 From: shugo Date: Sat, 12 Sep 2015 14:09:45 +0000 Subject: [PATCH] * lib/net/ftp.rb (file?, directory?, appendable?, creatable?, 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 --- ChangeLog | 6 ++ lib/net/ftp.rb | 108 ++++++++++++++++++++++++++- test/net/ftp/test_buffered_socket.rb | 2 +- test/net/ftp/test_mlsx_entry.rb | 96 ++++++++++++++++++++++++ 4 files changed, 210 insertions(+), 2 deletions(-) create mode 100644 test/net/ftp/test_mlsx_entry.rb diff --git a/ChangeLog b/ChangeLog index bb9401f7a0..365b7a7cf0 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +Sat Sep 12 23:06:51 2015 Shugo Maeda + + * 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 * lib/net/ftp.rb (FACT_PARSERS): support system dependent facts diff --git a/lib/net/ftp.rb b/lib/net/ftp.rb index d3bb450a93..cec2f80b14 100644 --- a/lib/net/ftp.rb +++ b/lib/net/ftp.rb @@ -768,7 +768,113 @@ module Net 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 } diff --git a/test/net/ftp/test_buffered_socket.rb b/test/net/ftp/test_buffered_socket.rb index f9eefcd988..8d7cc2ed52 100644 --- a/test/net/ftp/test_buffered_socket.rb +++ b/test/net/ftp/test_buffered_socket.rb @@ -3,7 +3,7 @@ require "test/unit" 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) diff --git a/test/net/ftp/test_mlsx_entry.rb b/test/net/ftp/test_mlsx_entry.rb new file mode 100644 index 0000000000..865cf911b9 --- /dev/null +++ b/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