if (debug != null) {
debug.println("Listing:\n" + listdir.toString());
}
final GridFTPClient client =
new GridFTPClient(listdir.getHost(), listdir.getPort());
if (identityAuthorization == null) {
client.setAuthorization(HostAuthorization.getInstance());
if (debug != null) {
debug.println(
"Using host-based authorization of remote server");
}
} else {
final IdentityAuthorization idA =
new IdentityAuthorization(identityAuthorization);
client.setAuthorization(idA);
if (debug != null) {
debug.println("Using identity-based authorization of remote " +
"server: '" + identityAuthorization + "'");
}
}
if (debug != null) {
debug.println("Authenticating.");
}
client.authenticate(null);
client.setType(Session.TYPE_ASCII);
client.setPassive();
client.setLocalActive();
if (debug != null) {
debug.println("Changing directory (CWD).");
}
client.changeDir(listdir.getPath());
if (debug != null) {
debug.println("Listing.");
}
final Vector v = client.mlsd(null);
int len = v.size();
if (debug != null) {
debug.println("Size of list return vector: " + len);
}
final ArrayList files = new ArrayList(len);
while (! v.isEmpty()) {
final MlsxEntry f = (MlsxEntry)v.remove(0);
if (f == null) {
if (debug != null) {
debug.println("null MlsxEntry received (?)");
}
continue;
}
final String fileName = f.getFileName();
if (fileName == null) {
if (debug != null) {
debug.println("no MlsxEntry filename (?)");
}
continue;
}
if (fileName.equals(".")) {
len -= 1;
if (debug != null) {
debug.println("'.' is not an interesting file");
}
continue;
}
if (fileName.equals("..")) {
len -= 1;
if (debug != null) {
debug.println("'..' is not an interesting file");
}
continue;
}
final FileListing fl = new FileListing();
fl.setName(f.getFileName());
final String sizeStr = f.get("size");
if (sizeStr == null) {
fl.setSize(-1);
} else {
long x = -1;
try {
x = Long.parseLong(sizeStr);
} catch (NumberFormatException e) {
// pass.
}
fl.setSize(x);
}
final String modified = f.get("modify");
// 20080522161726
if (modified == null || modified.length() != 14) {
throw new Exception("cannot parse modified time");
}
fl.setDate(parseDate(modified));
fl.setTime(parseTime(modified));
final String type = f.get("type");
if (type != null && type.equals("dir")) {
fl.setDirectory(true);
}
// If user is root and perms are group no-write and all no-write,
// we can, because of several conventions, safely say that the user
// only has read-only access.
final String owner = f.get("unix.owner");
final String mode = f.get("unix.mode");
if (mode == null) {
fl.setReadWrite(false);
} else if (mode.substring(3,4).equals("6")) {
fl.setReadWrite(true);
} else if (mode.substring(1,2).equals("6")) {
if (owner != null && owner.equals("root")) {
fl.setReadWrite(false);
} else {
fl.setReadWrite(true);
}
} else if (mode.substring(2,3).equals("6")) {
fl.setReadWrite(true); // unknown to be actually true.
}
files.add(fl);
}
client.close();
return (FileListing[]) files.toArray(new FileListing[files.size()]);
}