Skip to content

armgit / nutch

Public Imported from GitHub
0

Apache Nutch is an extensible and scalable web crawler

Fix issues reported in NUTCH-46. Submitted by Piotr Kosiorowski.

Andrzej Bialecki6d50255deeb1
diff --git a/src/java/org/apache/nutch/fs/NDFSFileSystem.java b/src/java/org/apache/nutch/fs/NDFSFileSystem.java
index e994f26f5..e9bef1a77 100644
--- a/src/java/org/apache/nutch/fs/NDFSFileSystem.java
+++ b/src/java/org/apache/nutch/fs/NDFSFileSystem.java
@@ -19,7 +19,6 @@ package org.apache.nutch.fs;
import java.io.*;
import java.net.*;
import java.util.*;
-import java.text.*;
import org.apache.nutch.io.*;
import org.apache.nutch.ndfs.*;
@@ -44,15 +43,18 @@ public class NDFSFileSystem extends NutchFileSystem {
*/
public NDFSFileSystem(InetSocketAddress namenode) throws IOException {
this.ndfs = new NDFSClient(namenode);
- this.name = namenode.getHostName()+":"+namenode.getPort();
+ this.name = namenode.getHostName() + ":" + namenode.getPort();
}
public String getName() { return name; }
private UTF8 getPath(File file) {
- if (!file.isAbsolute())
- file = new File(HOME_DIR+file.getPath());
- return new UTF8(file.getPath());
+ File f = file;
+ String path = getNDFSPath(file);
+ if (!path.startsWith(NDFSFile.NDFS_FILE_SEPARATOR)) {
+ f = new File(HOME_DIR, path);
+ }
+ return new UTF8(getNDFSPath(f));
}
/**
@@ -290,4 +292,28 @@ public class NDFSFileSystem extends NutchFileSystem {
public NDFSClient getClient() {
return ndfs;
}
+
+ private String getNDFSPath(File f) {
+ List l = new ArrayList();
+ l.add(f.getName());
+ File parent = f.getParentFile();
+ while (parent != null) {
+ l.add(parent.getName());
+ parent = parent.getParentFile();
+ }
+ StringBuffer path = new StringBuffer();
+ String fname = (String) l.get(l.size() - 1);
+ if (!"".equals(fname)) {
+ path.append(fname); //handle not absolute paths
+ } else {
+ if (l.size() == 1)
+ path.append(NDFSFile.NDFS_FILE_SEPARATOR); //handle root path
+ }
+ for (int i = l.size() - 2; i >= 0; i--) {
+ fname = (String) l.get(i);
+ path.append(NDFSFile.NDFS_FILE_SEPARATOR);
+ path.append(fname);
+ }
+ return path.toString();
+ }
}
diff --git a/src/java/org/apache/nutch/ndfs/FSDirectory.java b/src/java/org/apache/nutch/ndfs/FSDirectory.java
index fa137d118..1cce81bda 100644
--- a/src/java/org/apache/nutch/ndfs/FSDirectory.java
+++ b/src/java/org/apache/nutch/ndfs/FSDirectory.java
@@ -118,11 +118,11 @@ public class FSDirectory implements FSConstants {
/**
*/
- INode addNode(String target, Block blocks[]) {
+ INode addNode(String target, Block blks[]) {
if (getNode(target) != null) {
return null;
} else {
- String parentName = new File(target).getParent();
+ String parentName = NDFSFile.getNDFSParent(target);
if (parentName == null) {
return null;
}
@@ -132,7 +132,7 @@ public class FSDirectory implements FSConstants {
return null;
} else {
String targetName = new File(target).getName();
- INode newItem = new INode(targetName, parentNode, blocks);
+ INode newItem = new INode(targetName, parentNode, blks);
parentNode.children.add(newItem);
return newItem;
}
@@ -462,7 +462,7 @@ public class FSDirectory implements FSConstants {
waitForReady();
// Always do an implicit mkdirs for parent directory tree
- mkdirs(new File(src.toString()).getParent());
+ mkdirs(NDFSFile.getNDFSParent(src.toString()));
if (unprotectedAddFile(src, blocks)) {
logEdit(OP_ADD, src, new ArrayWritable(Block.class, blocks));
return true;
@@ -686,13 +686,13 @@ public class FSDirectory implements FSConstants {
Vector v = new Vector();
// The dir itself
- File f = new File(src);
- v.add(f.getPath());
+ v.add(src);
// All its parents
- while (f.getParent() != null) {
- f = new File(f.getParent());
- v.add(f.getPath());
+ String parent = NDFSFile.getNDFSParent(src);
+ while (parent != null) {
+ v.add(parent);
+ parent = NDFSFile.getNDFSParent(parent);
}
// Now go backwards through list of dirs, creating along
diff --git a/src/java/org/apache/nutch/ndfs/NDFSFile.java b/src/java/org/apache/nutch/ndfs/NDFSFile.java
index d938f481c..8756f86a0 100644
--- a/src/java/org/apache/nutch/ndfs/NDFSFile.java
+++ b/src/java/org/apache/nutch/ndfs/NDFSFile.java
@@ -16,8 +16,6 @@
package org.apache.nutch.ndfs;
import java.io.*;
-import java.net.*;
-import java.util.*;
/*****************************************************************
@@ -29,6 +27,9 @@ import java.util.*;
public class NDFSFile extends File {
NDFSFileInfo info;
+ /** Separator used in NDFS filenames. */
+ public static final String NDFS_FILE_SEPARATOR = "/";
+
/**
*/
public NDFSFile(NDFSFileInfo info) {
@@ -76,4 +77,22 @@ public class NDFSFile extends File {
public long getContentsLength() {
return info.getContentsLen();
}
+
+ /**
+ * Retrieving parent path from NDFS path string
+ * @param path - NDFS path
+ * @return - parent path of NDFS path, or null if no parent exist.
+ */
+ public static String getNDFSParent(String path) {
+ if (path == null)
+ return null;
+ if (NDFS_FILE_SEPARATOR.equals(path))
+ return null;
+ int index = path.lastIndexOf(NDFS_FILE_SEPARATOR);
+ if (index == -1)
+ return null;
+ if (index == 0)
+ return NDFS_FILE_SEPARATOR;
+ return path.substring(0, index);
+ }
}
diff --git a/src/java/org/apache/nutch/ndfs/NDFSFileInfo.java b/src/java/org/apache/nutch/ndfs/NDFSFileInfo.java
index 46dc67b97..013b63ea4 100644
--- a/src/java/org/apache/nutch/ndfs/NDFSFileInfo.java
+++ b/src/java/org/apache/nutch/ndfs/NDFSFileInfo.java
@@ -16,10 +16,8 @@
package org.apache.nutch.ndfs;
import org.apache.nutch.io.*;
-import org.apache.nutch.util.*;
import java.io.*;
-import java.util.*;
/******************************************************
* NDFSFileInfo tracks info about remote files, including
@@ -50,7 +48,7 @@ public class NDFSFileInfo implements Writable {
/**
*/
public String getPath() {
- return new File(path.toString()).getPath();
+ return path.toString();
}
/**
@@ -62,7 +60,7 @@ public class NDFSFileInfo implements Writable {
/**
*/
public String getParent() {
- return new File(path.toString()).getParent();
+ return NDFSFile.getNDFSParent(path.toString());
}
/**