Class: RIO::FTP::FS

Inherits:
Object show all
Includes:
RIO::FS::Str
Defined in:
lib/rio/ftp/fs.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from RIO::FS::Str

#basename, #cleanpath, #dirname, #extname, #fnmatch?, #join

Constructor Details

#initialize(uri) ⇒ FS

Returns a new instance of FS.



45
46
47
48
49
# File 'lib/rio/ftp/fs.rb', line 45

def initialize(uri)
  @uri = uri.clone
  @file = ::File
  @conn = nil
end

Instance Attribute Details

#uriObject (readonly)

Returns the value of attribute uri.



44
45
46
# File 'lib/rio/ftp/fs.rb', line 44

def uri
  @uri
end

Class Method Details

.create(*args) ⇒ Object



50
51
52
# File 'lib/rio/ftp/fs.rb', line 50

def self.create(*args)
  new(*args)
end

Instance Method Details

#chdir(url, &block) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/rio/ftp/fs.rb', line 84

def chdir(url,&block)
  if block_given?
    wd = conn.pwd
    conn.chdir(remote_path(url))
    begin
      rtn = yield remote_path(url)
    ensure
      conn.chdir(wd)
    end
    return rtn
  else
    conn.chdir(remote_path(url))
  end
end

#connObject



56
57
58
# File 'lib/rio/ftp/fs.rb', line 56

def conn
  @conn ||= ConnCache.instance.connect(@uri)
end

#cwdObject



74
75
76
77
78
79
80
# File 'lib/rio/ftp/fs.rb', line 74

def cwd()
  remote_wd = self.pwd
  remote_rel = remote_wd.sub(/^#{self.remote_root}/,'')
  wduri = uri.clone
  wduri.path = remote_rel
  wduri.to_s
end

#directory?(url) ⇒ Boolean

Returns:

  • (Boolean)


142
143
144
# File 'lib/rio/ftp/fs.rb', line 142

def directory?(url)
  get_ftype(url) == 'dir'
end

#exist?(url) ⇒ Boolean

Returns:

  • (Boolean)


145
146
147
# File 'lib/rio/ftp/fs.rb', line 145

def exist?(url)
  get_ftype(url) != 'nada'
end

#file?(url) ⇒ Boolean

Returns:

  • (Boolean)


139
140
141
# File 'lib/rio/ftp/fs.rb', line 139

def file?(url)
  get_ftype(url) == 'file'
end

#get_ftype(url) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/rio/ftp/fs.rb', line 120

def get_ftype(url)
  pth = remote_path(url)
  ftype = nil
  begin
    conn.mdtm(pth)
    ftype = 'file'
  rescue Net::FTPPermError
    wd = conn.pwd
    begin
      conn.chdir(pth)
      ftype = 'dir'
    rescue Net::FTPPermError
      ftype = 'nada'
    ensure
      conn.chdir(wd)
    end
  end
  ftype
end

#getwdObject



71
72
73
# File 'lib/rio/ftp/fs.rb', line 71

def getwd()
  self.pwd
end

#mkdir(url) ⇒ Object



98
99
100
# File 'lib/rio/ftp/fs.rb', line 98

def mkdir(url)
  conn.mkdir(remote_path(url))
end

#mkpath(url) ⇒ Object



151
152
153
154
155
156
157
158
159
# File 'lib/rio/ftp/fs.rb', line 151

def mkpath(url)
  tpath = rio(url)
  tmprio = tpath.root
  pathparts = tpath.path.split('/')[1..-1]
  pathparts.each do |part|
    tmprio.join!(part)
    tmprio.mkdir
  end
end

#mtime(url) ⇒ Object



110
111
112
# File 'lib/rio/ftp/fs.rb', line 110

def mtime(url)
  conn.mtime(remote_path(url))
end

#mv(src_url, dst_url) ⇒ Object



101
102
103
# File 'lib/rio/ftp/fs.rb', line 101

def mv(src_url,dst_url)
  conn.rename(remote_path(src_url),remote_path(dst_url))
end

#pwdObject



70
# File 'lib/rio/ftp/fs.rb', line 70

def pwd() conn.pwd end

#remote_path(url) ⇒ Object



81
82
83
# File 'lib/rio/ftp/fs.rb', line 81

def remote_path(url)
  self.remote_root+URI(url).path
end

#remote_rootObject



53
54
55
# File 'lib/rio/ftp/fs.rb', line 53

def remote_root
  conn.remote_root
end

#rm(url) ⇒ Object



116
117
118
# File 'lib/rio/ftp/fs.rb', line 116

def rm(url)
  conn.delete(remote_path(url))
end

#rmdir(url) ⇒ Object



113
114
115
# File 'lib/rio/ftp/fs.rb', line 113

def rmdir(url)
  conn.rmdir(remote_path(url))
end

#rmtree(url) ⇒ Object



160
161
162
163
# File 'lib/rio/ftp/fs.rb', line 160

def rmtree(url)
  ario = rio(url)
  _rment(ario)
end

#rootObject



62
63
64
65
66
# File 'lib/rio/ftp/fs.rb', line 62

def root()
  uri = @uri.clone
  uri.path = '/'
  uri.to_s
end

#size(url) ⇒ Object



104
105
106
# File 'lib/rio/ftp/fs.rb', line 104

def size(url)
  conn.size(remote_path(url))
end

#symlink?(url) ⇒ Boolean

Returns:

  • (Boolean)


148
149
150
# File 'lib/rio/ftp/fs.rb', line 148

def symlink?(url)
  false
end

#zero?(url) ⇒ Boolean

Returns:

  • (Boolean)


107
108
109
# File 'lib/rio/ftp/fs.rb', line 107

def zero?(url)
  size(url) == 0
end