lib/rio/ftp/fs.rb in rio-0.3.8 vs lib/rio/ftp/fs.rb in rio-0.3.9

- old
+ new

@@ -1,8 +1,8 @@ #-- # =============================================================================== -# Copyright (c) 2005, 2006 Christopher Kleckner +# Copyright (c) 2005,2006,2007 Christopher Kleckner # All rights reserved # # This file is part of the Rio library for ruby. # # Rio is free software; you can redistribute it and/or modify @@ -21,112 +21,118 @@ # =============================================================================== #++ # # To create the documentation for Rio run the command # ruby build_doc.rb -# from the distribution directory. Then point your browser at the 'doc/rdoc' directory. +# from the distribution directory. # # Suggested Reading # * RIO::Doc::SYNOPSIS # * RIO::Doc::INTRO # * RIO::Doc::HOWTO +# * RIO::Doc::EXAMPLES # * RIO::Rio # -# <b>Rio is pre-alpha software. -# The documented interface and behavior is subject to change without notice.</b> require 'net/ftp' require 'uri' require 'rio/fs/native' require 'rio/ftp/conncache' module RIO module FTP class FS - attr_reader :uri,:conn,:remote_root + attr_reader :uri def initialize(uri) @uri = uri.clone - @conn = ConnCache.instance.connect(@uri) - @remote_root = @conn.remote_root - @remote_root = '' if @remote_root == '/' @file = ::File + @conn = nil end def self.create(*args) new(*args) end + def remote_root + conn.remote_root + end + def conn + @conn ||= ConnCache.instance.connect(@uri) + end + + + def root() uri = @uri.clone uri.path = '/' uri.to_s end include RIO::FS::Str - def pwd() @conn.pwd end + def pwd() conn.pwd end def getwd() self.pwd end def cwd() remote_wd = self.pwd - remote_rel = remote_wd.sub(/^#{@remote_root}/,'') + remote_rel = remote_wd.sub(/^#{self.remote_root}/,'') wduri = uri.clone wduri.path = remote_rel wduri.to_s end def remote_path(url) self.remote_root+URI(url).path end def chdir(url,&block) if block_given? - wd = @conn.pwd - @conn.chdir(remote_path(url)) + wd = conn.pwd + conn.chdir(remote_path(url)) begin rtn = yield remote_path(url) ensure - @conn.chdir(wd) + conn.chdir(wd) end return rtn else - @conn.chdir(remote_path(url)) + conn.chdir(remote_path(url)) end end def mkdir(url) - @conn.mkdir(remote_path(url)) + conn.mkdir(remote_path(url)) end def mv(src_url,dst_url) - @conn.rename(remote_path(src_url),remote_path(dst_url)) + conn.rename(remote_path(src_url),remote_path(dst_url)) end def size(url) - @conn.size(remote_path(url)) + conn.size(remote_path(url)) end def zero?(url) size(url) == 0 end def mtime(url) - @conn.mtime(remote_path(url)) + conn.mtime(remote_path(url)) end def rmdir(url) - @conn.rmdir(remote_path(url)) + conn.rmdir(remote_path(url)) end def rm(url) - @conn.delete(remote_path(url)) + conn.delete(remote_path(url)) end def get_ftype(url) pth = remote_path(url) ftype = nil begin - @conn.mdtm(pth) + conn.mdtm(pth) ftype = 'file' rescue Net::FTPPermError - wd = @conn.pwd + wd = conn.pwd begin - @conn.chdir(pth) + conn.chdir(pth) ftype = 'dir' rescue Net::FTPPermError ftype = 'nada' ensure - @conn.chdir(wd) + conn.chdir(wd) end end ftype end def file?(url)