net/ftp problem

G

Greg Brondo

I'm trying to recurse a directory and chmod the files on a remote ftp site.
What happens in the code works for the root directory then when it hits a
child directory and calls the chmod function recursively it hangs on the
next access of the 'conn' object. I cannot figure it out. Here's the code:

-----------------------------------
require 'net/ftp'

def print_info(msg)
puts Time.now.to_s + " :: #{msg}"
end

def chmod_files(conn, dir)
conn.chdir(dir)
pwd = conn.pwd()
conn.list() { |entry|
fileName = entry.split()[8]
print_info("CHMOD #{PERMISSIONS} #{fileName}")
conn.sendcmd("SITE CHMOD #{PERMISSIONS} #{fileName}")
if entry =~ /^d/ # It's a directory
chmod_files(conn, fileName)
conn.chdir("..")
end
}
end

## Main
HOST = ARGV[0] || "localhost"
PERMISSIONS = ARGV[1] || "0775"
STARTDIR = ARGV[2] || "."

print_info("Host = #{HOST}")
print_info("Permissions = #{PERMISSIONS}")
print_info("Start Dir = #{STARTDIR}")

Net::FTP.open(HOST) { |conn|
conn.login("blahID", "blahPass")
chmod_files(conn, STARTDIR)
}
 
T

ts

G> I'm trying to recurse a directory and chmod the files on a remote ftp site.
G> What happens in the code works for the root directory then when it hits a
G> child directory and calls the chmod function recursively it hangs on the
G> next access of the 'conn' object. I cannot figure it out. Here's the
G> code:

You can't mix 2 ftp commands

G> conn.list() { |entry|

you send the command LIST

G> fileName = entry.split()[8]
G> print_info("CHMOD #{PERMISSIONS} #{fileName}")
G> conn.sendcmd("SITE CHMOD #{PERMISSIONS} #{fileName}")
G> if entry =~ /^d/ # It's a directory
G> chmod_files(conn, fileName)

you call recursively chmod_files(). This mean that you'll send a command
LIST when the previous is not yet finished.

You must call #list without a block, to be sure that the command is
finished when you recurse

G> conn.chdir("..")
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,374
Latest member
anuragag27

Latest Threads

Top