You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by ng...@apache.org on 2009/05/18 22:00:52 UTC
svn commit: r776069 - in /mina/ftpserver/trunk/core/src:
main/java/org/apache/ftpserver/filesystem/nativefs/impl/
test/java/org/apache/ftpserver/filesystem/nativefs/impl/
Author: ngn
Date: Mon May 18 20:00:52 2009
New Revision: 776069
URL: http://svn.apache.org/viewvc?rev=776069&view=rev
Log:
Move getPhysicalName() to the view where it belongs
Modified:
mina/ftpserver/trunk/core/src/main/java/org/apache/ftpserver/filesystem/nativefs/impl/NativeFileSystemView.java
mina/ftpserver/trunk/core/src/main/java/org/apache/ftpserver/filesystem/nativefs/impl/NativeFtpFile.java
mina/ftpserver/trunk/core/src/test/java/org/apache/ftpserver/filesystem/nativefs/impl/NativeFileSystemViewTest.java
mina/ftpserver/trunk/core/src/test/java/org/apache/ftpserver/filesystem/nativefs/impl/NativeFtpFileTest.java
Modified: mina/ftpserver/trunk/core/src/main/java/org/apache/ftpserver/filesystem/nativefs/impl/NativeFileSystemView.java
URL: http://svn.apache.org/viewvc/mina/ftpserver/trunk/core/src/main/java/org/apache/ftpserver/filesystem/nativefs/impl/NativeFileSystemView.java?rev=776069&r1=776068&r2=776069&view=diff
==============================================================================
--- mina/ftpserver/trunk/core/src/main/java/org/apache/ftpserver/filesystem/nativefs/impl/NativeFileSystemView.java (original)
+++ mina/ftpserver/trunk/core/src/main/java/org/apache/ftpserver/filesystem/nativefs/impl/NativeFileSystemView.java Mon May 18 20:00:52 2009
@@ -20,6 +20,7 @@
package org.apache.ftpserver.filesystem.nativefs.impl;
import java.io.File;
+import java.util.StringTokenizer;
import org.apache.ftpserver.filesystem.nativefs.NativeFileSystemFactory;
import org.apache.ftpserver.ftplet.FileSystemView;
@@ -81,10 +82,8 @@
// add last '/' if necessary
String rootDir = user.getHomeDirectory();
- rootDir = NativeFtpFile.normalizeSeparateChar(rootDir);
- if (!rootDir.endsWith("/")) {
- rootDir += '/';
- }
+ rootDir = normalizeSeparateChar(rootDir);
+ rootDir = appendSlash(rootDir);
LOG.debug("Native filesystem view created for user \"{}\" with root \"{}\"", user.getName(), rootDir);
@@ -124,7 +123,7 @@
public FtpFile getFile(String file) {
// get actual file object
- String physicalName = NativeFtpFile.getPhysicalName(rootDir,
+ String physicalName = getPhysicalName(rootDir,
currDir, file, caseInsensitive);
File fileObj = new File(physicalName);
@@ -139,7 +138,7 @@
public boolean changeWorkingDirectory(String dir) {
// not a directory - return false
- dir = NativeFtpFile.getPhysicalName(rootDir, currDir, dir,
+ dir = getPhysicalName(rootDir, currDir, dir,
caseInsensitive);
File dirObj = new File(dir);
if (!dirObj.isDirectory()) {
@@ -168,4 +167,153 @@
*/
public void dispose() {
}
+
+ /**
+ * Get the physical canonical file name. It works like
+ * File.getCanonicalPath().
+ *
+ * @param rootDir
+ * The root directory.
+ * @param currDir
+ * The current directory. It will always be with respect to the
+ * root directory.
+ * @param fileName
+ * The input file name.
+ * @return The return string will always begin with the root directory. It
+ * will never be null.
+ */
+ protected String getPhysicalName(final String rootDir,
+ final String currDir, final String fileName,
+ final boolean caseInsensitive) {
+
+ // normalize root dir
+ String normalizedRootDir = normalizeSeparateChar(rootDir);
+ normalizedRootDir = appendSlash(normalizedRootDir);
+
+ // normalize file name
+ String normalizedFileName = normalizeSeparateChar(fileName);
+ String result;
+
+ // if file name is relative, set resArg to root dir + curr dir
+ // if file name is absolute, set resArg to root dir
+ if (normalizedFileName.charAt(0) != '/') {
+ // file name is relative
+ String normalizedCurrDir = normalize(currDir, "/");
+
+ result = normalizedRootDir + normalizedCurrDir.substring(1);
+ } else {
+ result = normalizedRootDir;
+ }
+
+ // strip last '/'
+ result = trimTrailingSlash(result);
+
+ // replace ., ~ and ..
+ // in this loop resArg will never end with '/'
+ StringTokenizer st = new StringTokenizer(normalizedFileName, "/");
+ while (st.hasMoreTokens()) {
+ String tok = st.nextToken();
+
+ // . => current directory
+ if (tok.equals(".")) {
+ // ignore and move on
+ } else if (tok.equals("..")) {
+ // .. => parent directory (if not root)
+ if (result.startsWith(normalizedRootDir)) {
+ int slashIndex = result.lastIndexOf('/');
+ if (slashIndex != -1) {
+ result = result.substring(0, slashIndex);
+ }
+ }
+ } else if (tok.equals("~")) {
+ // ~ => home directory (in this case the root directory)
+ result = trimTrailingSlash(normalizedRootDir);
+ continue;
+ } else {
+ // token is normal directory name
+
+ if(caseInsensitive) {
+ // we're case insensitive, find a directory with the name, ignoring casing
+ File[] matches = new File(result)
+ .listFiles(new NameEqualsFileFilter(tok, true));
+
+ if (matches != null && matches.length > 0) {
+ // found a file matching tok, replace tok for get the right casing
+ tok = matches[0].getName();
+ }
+ }
+
+ result = result + '/' + tok;
+
+ }
+ }
+
+ // add last slash if necessary
+ if ((result.length()) + 1 == normalizedRootDir.length()) {
+ result += '/';
+ }
+
+ // make sure we did not end up above root dir
+ if (!result.startsWith(normalizedRootDir)) {
+ result = normalizedRootDir;
+ }
+
+ return result;
+ }
+
+ /**
+ * Append trailing slash ('/') if missing
+ */
+ private String appendSlash(String path) {
+ if (path.charAt(path.length() - 1) != '/') {
+ return path + '/';
+ } else {
+ return path;
+ }
+ }
+
+ /**
+ * Prepend leading slash ('/') if missing
+ */
+ private String prependSlash(String path) {
+ if (path.charAt(0) != '/') {
+ return '/' + path;
+ } else {
+ return path;
+ }
+ }
+
+ /**
+ * Trim trailing slash ('/') if existing
+ */
+ private String trimTrailingSlash(String path) {
+ if (path.charAt(path.length() - 1) == '/') {
+ return path.substring(0, path.length() - 1);
+ } else {
+ return path;
+ }
+ }
+
+ /**
+ * Normalize separate character. Separate character should be '/' always.
+ */
+ private String normalizeSeparateChar(final String pathName) {
+ String normalizedPathName = pathName.replace(File.separatorChar, '/');
+ normalizedPathName = normalizedPathName.replace('\\', '/');
+ return normalizedPathName;
+ }
+
+ /**
+ * Normalize separator char, append and prepend slashes. Default to
+ * defaultPath if null or empty
+ */
+ private String normalize(String path, String defaultPath) {
+ if(path == null || path.trim().length() == 0) {
+ path = defaultPath;
+ }
+
+ path = normalizeSeparateChar(path);
+ path = prependSlash(appendSlash(path));
+ return path;
+ }
}
Modified: mina/ftpserver/trunk/core/src/main/java/org/apache/ftpserver/filesystem/nativefs/impl/NativeFtpFile.java
URL: http://svn.apache.org/viewvc/mina/ftpserver/trunk/core/src/main/java/org/apache/ftpserver/filesystem/nativefs/impl/NativeFtpFile.java?rev=776069&r1=776068&r2=776069&view=diff
==============================================================================
--- mina/ftpserver/trunk/core/src/main/java/org/apache/ftpserver/filesystem/nativefs/impl/NativeFtpFile.java (original)
+++ mina/ftpserver/trunk/core/src/main/java/org/apache/ftpserver/filesystem/nativefs/impl/NativeFtpFile.java Mon May 18 20:00:52 2009
@@ -393,159 +393,9 @@
};
}
- /**
- * Normalize separate character. Separate character should be '/' always.
- */
- public final static String normalizeSeparateChar(final String pathName) {
- String normalizedPathName = pathName.replace(File.separatorChar, '/');
- normalizedPathName = normalizedPathName.replace('\\', '/');
- return normalizedPathName;
- }
-
- /**
- * Get the physical canonical file name. It works like
- * File.getCanonicalPath().
- *
- * @param rootDir
- * The root directory.
- * @param currDir
- * The current directory. It will always be with respect to the
- * root directory.
- * @param fileName
- * The input file name.
- * @return The return string will always begin with the root directory. It
- * will never be null.
- */
- public final static String getPhysicalName(final String rootDir,
- final String currDir, final String fileName) {
- return getPhysicalName(rootDir, currDir, fileName, false);
- }
-
- public final static String getPhysicalName(final String rootDir,
- final String currDir, final String fileName,
- final boolean caseInsensitive) {
-
- // normalize root dir
- String normalizedRootDir = normalizeSeparateChar(rootDir);
- normalizedRootDir = appendSlash(normalizedRootDir);
-
- // normalize file name
- String normalizedFileName = normalizeSeparateChar(fileName);
- String result;
-
- // if file name is relative, set resArg to root dir + curr dir
- // if file name is absolute, set resArg to root dir
- if (normalizedFileName.charAt(0) != '/') {
- // file name is relative
- String normalizedCurrDir = normalize(currDir, "/");
-
- result = normalizedRootDir + normalizedCurrDir.substring(1);
- } else {
- result = normalizedRootDir;
- }
- // strip last '/'
- result = trimTrailingSlash(result);
- // replace ., ~ and ..
- // in this loop resArg will never end with '/'
- StringTokenizer st = new StringTokenizer(normalizedFileName, "/");
- while (st.hasMoreTokens()) {
- String tok = st.nextToken();
-
- // . => current directory
- if (tok.equals(".")) {
- // ignore and move on
- } else if (tok.equals("..")) {
- // .. => parent directory (if not root)
- if (result.startsWith(normalizedRootDir)) {
- int slashIndex = result.lastIndexOf('/');
- if (slashIndex != -1) {
- result = result.substring(0, slashIndex);
- }
- }
- } else if (tok.equals("~")) {
- // ~ => home directory (in this case the root directory)
- result = trimTrailingSlash(normalizedRootDir);
- continue;
- } else {
- // token is normal directory name
-
- if(caseInsensitive) {
- // we're case insensitive, find a directory with the name, ignoring casing
- File[] matches = new File(result)
- .listFiles(new NameEqualsFileFilter(tok, true));
-
- if (matches != null && matches.length > 0) {
- // found a file matching tok, replace tok for get the right casing
- tok = matches[0].getName();
- }
- }
-
- result = result + '/' + tok;
-
- }
- }
-
- // add last slash if necessary
- if ((result.length()) + 1 == normalizedRootDir.length()) {
- result += '/';
- }
- // make sure we did not end up above root dir
- if (!result.startsWith(normalizedRootDir)) {
- result = normalizedRootDir;
- }
-
- return result;
- }
-
- /**
- * Append trailing slash ('/') if missing
- */
- private static String appendSlash(String path) {
- if (path.charAt(path.length() - 1) != '/') {
- return path + '/';
- } else {
- return path;
- }
- }
-
- /**
- * Prepend leading slash ('/') if missing
- */
- private static String prependSlash(String path) {
- if (path.charAt(0) != '/') {
- return '/' + path;
- } else {
- return path;
- }
- }
-
- /**
- * Trim trailing slash ('/') if existing
- */
- private static String trimTrailingSlash(String path) {
- if (path.charAt(path.length() - 1) == '/') {
- return path.substring(0, path.length() - 1);
- } else {
- return path;
- }
- }
-
- /**
- * Normalize separator char, append and prepend slashes. Default to
- * defaultPath if null or empty
- */
- private static String normalize(String path, String defaultPath) {
- if(path == null || path.trim().length() == 0) {
- path = defaultPath;
- }
-
- path = normalizeSeparateChar(path);
- path = prependSlash(appendSlash(path));
- return path;
- }
@Override
public boolean equals(Object obj) {
Modified: mina/ftpserver/trunk/core/src/test/java/org/apache/ftpserver/filesystem/nativefs/impl/NativeFileSystemViewTest.java
URL: http://svn.apache.org/viewvc/mina/ftpserver/trunk/core/src/test/java/org/apache/ftpserver/filesystem/nativefs/impl/NativeFileSystemViewTest.java?rev=776069&r1=776068&r2=776069&view=diff
==============================================================================
--- mina/ftpserver/trunk/core/src/test/java/org/apache/ftpserver/filesystem/nativefs/impl/NativeFileSystemViewTest.java (original)
+++ mina/ftpserver/trunk/core/src/test/java/org/apache/ftpserver/filesystem/nativefs/impl/NativeFileSystemViewTest.java Mon May 18 20:00:52 2009
@@ -38,10 +38,22 @@
private static final File TEST_DIR1 = new File(ROOT_DIR, DIR1_NAME);
+ private static final File TEST_FILE2_IN_DIR1 = new File(TEST_DIR1, "file2");
+
+ private static final String ROOT_DIR_PATH = ROOT_DIR.getAbsolutePath()
+ .replace(File.separatorChar, '/');
+
+ private static final String FULL_PATH = ROOT_DIR_PATH + "/"
+ + TEST_DIR1.getName() + "/" + TEST_FILE2_IN_DIR1.getName();
+
+ private static final String FULL_PATH_NO_CURRDIR = ROOT_DIR_PATH + "/"
+ + TEST_FILE2_IN_DIR1.getName();
+
protected void setUp() throws Exception {
initDirs();
TEST_DIR1.mkdirs();
+ TEST_FILE2_IN_DIR1.createNewFile();
user.setHomeDirectory(ROOT_DIR.getAbsolutePath());
}
@@ -70,6 +82,82 @@
}
}
+ public void testGetPhysicalName() throws FtpException {
+ NativeFileSystemView view = new NativeFileSystemView(user);
+
+ assertEquals(FULL_PATH, view.getPhysicalName(ROOT_DIR_PATH + "/", "/"
+ + TEST_DIR1.getName() + "/", TEST_FILE2_IN_DIR1.getName(), false));
+
+ assertEquals("No trailing slash on rootDir", FULL_PATH, view
+ .getPhysicalName(ROOT_DIR_PATH,
+ "/" + TEST_DIR1.getName() + "/", TEST_FILE2_IN_DIR1
+ .getName(), false));
+
+ assertEquals("No leading slash on currDir", FULL_PATH,
+ view.getPhysicalName(ROOT_DIR_PATH + "/", TEST_DIR1
+ .getName()
+ + "/", TEST_FILE2_IN_DIR1.getName(), false));
+ assertEquals("No trailing slash on currDir", FULL_PATH,
+ view.getPhysicalName(ROOT_DIR_PATH + "/", "/"
+ + TEST_DIR1.getName(), TEST_FILE2_IN_DIR1.getName(), false));
+ assertEquals("No slashes on currDir", FULL_PATH, view
+ .getPhysicalName(ROOT_DIR_PATH + "/", TEST_DIR1.getName(),
+ TEST_FILE2_IN_DIR1.getName(), false));
+ assertEquals("Backslashes in rootDir", FULL_PATH, view
+ .getPhysicalName(ROOT_DIR.getAbsolutePath() + "/", "/"
+ + TEST_DIR1.getName() + "/", TEST_FILE2_IN_DIR1
+ .getName(), false));
+ assertEquals("Null currDir", FULL_PATH_NO_CURRDIR, view
+ .getPhysicalName(ROOT_DIR.getAbsolutePath() + "/", null,
+ TEST_FILE2_IN_DIR1.getName(), false));
+ assertEquals("Empty currDir", FULL_PATH_NO_CURRDIR, view
+ .getPhysicalName(ROOT_DIR.getAbsolutePath() + "/", "",
+ TEST_FILE2_IN_DIR1.getName(), false));
+ assertEquals("Absolute fileName in root", FULL_PATH_NO_CURRDIR,
+ view
+ .getPhysicalName(ROOT_DIR.getAbsolutePath() + "/",
+ TEST_DIR1.getName(), "/"
+ + TEST_FILE2_IN_DIR1.getName(), false));
+ assertEquals("Absolute fileName in dir1", FULL_PATH, view
+ .getPhysicalName(ROOT_DIR.getAbsolutePath() + "/", null, "/"
+ + TEST_DIR1.getName() + "/"
+ + TEST_FILE2_IN_DIR1.getName(), false));
+
+ assertEquals(". in currDir", FULL_PATH, view.getPhysicalName(
+ ROOT_DIR.getAbsolutePath(), TEST_DIR1.getName() + "/./", "/"
+ + TEST_DIR1.getName() + "/"
+ + TEST_FILE2_IN_DIR1.getName(), false));
+
+ }
+
+ public void testGetPhysicalNameWithRelative() throws FtpException {
+ NativeFileSystemView view = new NativeFileSystemView(user);
+
+ assertEquals(".. in fileName", FULL_PATH_NO_CURRDIR, view
+ .getPhysicalName(ROOT_DIR.getAbsolutePath(), TEST_DIR1
+ .getName(), "/../" + TEST_FILE2_IN_DIR1.getName(), false));
+ assertEquals(".. beyond rootDir", FULL_PATH_NO_CURRDIR, view
+ .getPhysicalName(ROOT_DIR.getAbsolutePath(), TEST_DIR1
+ .getName(), "/../../" + TEST_FILE2_IN_DIR1.getName(), false));
+ }
+
+ public void testGetPhysicalNameWithTilde() throws FtpException {
+ NativeFileSystemView view = new NativeFileSystemView(user);
+
+ assertEquals(FULL_PATH_NO_CURRDIR, view.getPhysicalName(
+ ROOT_DIR.getAbsolutePath(), TEST_DIR1.getName(), "/~/"
+ + TEST_FILE2_IN_DIR1.getName(), false));
+ }
+
+ public void testGetPhysicalNameCaseInsensitive() throws FtpException {
+ NativeFileSystemView view = new NativeFileSystemView(user);
+
+ assertEquals(FULL_PATH, view.getPhysicalName(ROOT_DIR
+ .getAbsolutePath(), TEST_DIR1.getName(), TEST_FILE2_IN_DIR1
+ .getName().toUpperCase(), true));
+
+ }
+
/*
* (non-Javadoc)
*
Modified: mina/ftpserver/trunk/core/src/test/java/org/apache/ftpserver/filesystem/nativefs/impl/NativeFtpFileTest.java
URL: http://svn.apache.org/viewvc/mina/ftpserver/trunk/core/src/test/java/org/apache/ftpserver/filesystem/nativefs/impl/NativeFtpFileTest.java?rev=776069&r1=776068&r2=776069&view=diff
==============================================================================
--- mina/ftpserver/trunk/core/src/test/java/org/apache/ftpserver/filesystem/nativefs/impl/NativeFtpFileTest.java (original)
+++ mina/ftpserver/trunk/core/src/test/java/org/apache/ftpserver/filesystem/nativefs/impl/NativeFtpFileTest.java Mon May 18 20:00:52 2009
@@ -59,14 +59,8 @@
FILE_MAPPINGS.put(" \t", TEST_FILE2_IN_DIR1);
}
- private static final String ROOT_DIR_PATH = ROOT_DIR.getAbsolutePath()
- .replace(File.separatorChar, '/');
- private static final String FULL_PATH = ROOT_DIR_PATH + "/"
- + TEST_DIR1.getName() + "/" + TEST_FILE2_IN_DIR1.getName();
- private static final String FULL_PATH_NO_CURRDIR = ROOT_DIR_PATH + "/"
- + TEST_FILE2_IN_DIR1.getName();
protected void setUp() throws Exception {
initDirs();
@@ -81,75 +75,6 @@
return new NativeFtpFile(fileName, FILE_MAPPINGS.get(fileName), user);
}
- public void testGetPhysicalName() {
-
- assertEquals(FULL_PATH, NativeFtpFile.getPhysicalName(ROOT_DIR_PATH
- + "/", "/" + TEST_DIR1.getName() + "/", TEST_FILE2_IN_DIR1
- .getName()));
- assertEquals("No trailing slash on rootDir", FULL_PATH,
- NativeFtpFile.getPhysicalName(ROOT_DIR_PATH, "/"
- + TEST_DIR1.getName() + "/", TEST_FILE2_IN_DIR1
- .getName()));
- assertEquals("No leading slash on currDir", FULL_PATH,
- NativeFtpFile.getPhysicalName(ROOT_DIR_PATH + "/", TEST_DIR1
- .getName()
- + "/", TEST_FILE2_IN_DIR1.getName()));
- assertEquals("No trailing slash on currDir", FULL_PATH,
- NativeFtpFile.getPhysicalName(ROOT_DIR_PATH + "/", "/"
- + TEST_DIR1.getName(), TEST_FILE2_IN_DIR1.getName()));
- assertEquals("No slashes on currDir", FULL_PATH, NativeFtpFile
- .getPhysicalName(ROOT_DIR_PATH + "/", TEST_DIR1.getName(),
- TEST_FILE2_IN_DIR1.getName()));
- assertEquals("Backslashes in rootDir", FULL_PATH, NativeFtpFile
- .getPhysicalName(ROOT_DIR.getAbsolutePath() + "/", "/"
- + TEST_DIR1.getName() + "/", TEST_FILE2_IN_DIR1
- .getName()));
- assertEquals("Null currDir", FULL_PATH_NO_CURRDIR, NativeFtpFile
- .getPhysicalName(ROOT_DIR.getAbsolutePath() + "/", null,
- TEST_FILE2_IN_DIR1.getName()));
- assertEquals("Empty currDir", FULL_PATH_NO_CURRDIR, NativeFtpFile
- .getPhysicalName(ROOT_DIR.getAbsolutePath() + "/", "",
- TEST_FILE2_IN_DIR1.getName()));
- assertEquals("Absolute fileName in root", FULL_PATH_NO_CURRDIR,
- NativeFtpFile.getPhysicalName(ROOT_DIR.getAbsolutePath()
- + "/", TEST_DIR1.getName(), "/"
- + TEST_FILE2_IN_DIR1.getName()));
- assertEquals("Absolute fileName in dir1", FULL_PATH, NativeFtpFile
- .getPhysicalName(ROOT_DIR.getAbsolutePath() + "/", null, "/"
- + TEST_DIR1.getName() + "/"
- + TEST_FILE2_IN_DIR1.getName()));
-
- assertEquals(". in currDir", FULL_PATH, NativeFtpFile
- .getPhysicalName(ROOT_DIR.getAbsolutePath(), TEST_DIR1
- .getName()
- + "/./", "/" + TEST_DIR1.getName() + "/"
- + TEST_FILE2_IN_DIR1.getName()));
-
- }
-
- public void testGetPhysicalNameWithRelative() {
- assertEquals(".. in fileName", FULL_PATH_NO_CURRDIR, NativeFtpFile
- .getPhysicalName(ROOT_DIR.getAbsolutePath(), TEST_DIR1
- .getName(), "/../" + TEST_FILE2_IN_DIR1.getName()));
- assertEquals(".. beyond rootDir", FULL_PATH_NO_CURRDIR,
- NativeFtpFile.getPhysicalName(ROOT_DIR.getAbsolutePath(),
- TEST_DIR1.getName(), "/../../"
- + TEST_FILE2_IN_DIR1.getName()));
- }
-
- public void testGetPhysicalNameWithTilde() {
- assertEquals(FULL_PATH_NO_CURRDIR, NativeFtpFile.getPhysicalName(
- ROOT_DIR.getAbsolutePath(), TEST_DIR1.getName(), "/~/"
- + TEST_FILE2_IN_DIR1.getName()));
- }
-
- public void testGetPhysicalNameCaseInsensitive() {
- assertEquals(FULL_PATH, NativeFtpFile.getPhysicalName(ROOT_DIR
- .getAbsolutePath(), TEST_DIR1.getName(), TEST_FILE2_IN_DIR1
- .getName().toUpperCase(), true));
-
- }
-
public void testConstructorWithNullFile() {
try {
new NativeFtpFile("foo", null, USER);