Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/net/micode/fileexplorer/FileExplorerPreferenceActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package net.micode.fileexplorer;

import java.io.File;
import java.io.IOException;

import android.content.Context;
import android.content.SharedPreferences;
Expand Down Expand Up @@ -104,7 +105,22 @@ public static boolean isReadRoot(Context context) {
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);

boolean isReadRootFromSetting = settings.getBoolean(READ_ROOT, false);
boolean isReadRootWhenSettingPrimaryFolderWithoutSdCardPrefix = !getPrimaryFolder(context).startsWith(Util.getSdDirectory());
boolean isReadRootWhenSettingPrimaryFolderWithoutSdCardPrefix;

/* Modified by Nova, fix a bug.
* In huawei p6, Util.getSdDirectory() returns "/storage/emulated/0", primary folder is "/mnt/sdcard", they are the same,
* it make isReadRootWhenSettingPrimaryFolderWithoutSdCardPrefix get a wrong value.
*/
String primary, sd;
try {
primary = (new File(getPrimaryFolder(context))).getCanonicalPath();
sd = (new File(Util.getSdDirectory())).getCanonicalPath();
} catch (IOException e) {
e.printStackTrace();
primary = getPrimaryFolder(context);
sd = Util.getSdDirectory();
}
isReadRootWhenSettingPrimaryFolderWithoutSdCardPrefix = !primary.startsWith(sd);

return isReadRootFromSetting || isReadRootWhenSettingPrimaryFolderWithoutSdCardPrefix;
}
Expand Down
9 changes: 9 additions & 0 deletions src/net/micode/fileexplorer/FileViewActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package net.micode.fileexplorer;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Collections;
Expand Down Expand Up @@ -174,6 +175,14 @@ public void onClick(View v) {
currentDir = uri.getPath();
}
}

// added by Nova, for root path checking.
try {
currentDir = new File(currentDir).getCanonicalPath();
} catch (IOException e) {
Log.d(LOG_TAG, "fail to get CanonicalPath, currentDir = " + currentDir);
}

mFileViewInteractionHub.setCurrentPath(currentDir);
Log.i(LOG_TAG, "CurrentDir = " + currentDir);

Expand Down
15 changes: 14 additions & 1 deletion src/net/micode/fileexplorer/ServerControlActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -246,14 +246,27 @@ private void setText(int id, String text) {
OnClickListener startStopListener = new OnClickListener() {
public void onClick(View v) {
Globals.setLastError(null);
File chrootDir = new File(Defaults.chrootDir);

/*
* check whether ftp user can access root path according to the settings.
* added by Nova.
*/
File chrootDir;
if (FileExplorerPreferenceActivity.isReadRoot(mActivity)) {
chrootDir = new File(GlobalConsts.ROOT_PATH); // system root, "/"
} else {
chrootDir = new File(Defaults.chrootDir); // sd home
}

if (!chrootDir.isDirectory())
return;

Context context = mActivity.getApplicationContext();
Intent intent = new Intent(context, FTPServerService.class);

Globals.setChrootDir(chrootDir);
Globals.setPrimaryDir(new File(Defaults.chrootDir));

if (!FTPServerService.isRunning()) {
warnIfNoExternalStorage();
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
Expand Down
6 changes: 5 additions & 1 deletion src/org/swiftp/CmdPWD.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ public void run() {
File workingDir = sessionThread.getWorkingDir();
String currentDir = workingDir != null ? workingDir.getCanonicalPath() : Globals.getChrootDir()
.getCanonicalPath();
currentDir = currentDir.substring(Globals.getChrootDir().getCanonicalPath().length());

// modified by nova, if root path is "/", it requires special handling
if (!Globals.getChrootDir().getCanonicalPath().equals("/")) {
currentDir = currentDir.substring(Globals.getChrootDir().getCanonicalPath().length());
}
// The root directory requires special handling to restore its
// leading slash
if (currentDir.length() == 0) {
Expand Down
26 changes: 26 additions & 0 deletions src/org/swiftp/Globals.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class Globals {
private static Context context;
private static String lastError;
private static File chrootDir = new File(Defaults.chrootDir);
private static File primaryDir = chrootDir; // added by Nova, set primary directory for ftp user.
private static ProxyConnector proxyConnector = null;
private static String username = null;

Expand All @@ -50,6 +51,31 @@ public static File getChrootDir() {
public static void setChrootDir(File chrootDir) {
if(chrootDir.isDirectory()) {
Globals.chrootDir = chrootDir;
Globals.primaryDir = chrootDir;
}
}

/** Get primary directory for ftp user.
*/
public static File getPrimaryDir() {
return primaryDir;
}

/** Set primary directory for ftp user. when root directory is "/", we can change the primary directory of ftp user by setting this value.
* Notes:
* 1. It must be invoked after setChrootDir.
* 2. Globals.primaryDir must be subdirectory of Globals.chrootDir.
*/
public static void setPrimaryDir(File dir) {
if (dir.isDirectory()) {
try {
if (dir.getCanonicalPath().startsWith(Globals.getChrootDir().getPath())) {
Globals.primaryDir = dir; // the primary directory name must begin with the Globals.chrootDir.
}
return ;
} catch (Exception e) {
e.printStackTrace();
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/org/swiftp/SessionThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class SessionThread extends Thread {
protected boolean binaryMode = false;
protected Account account = new Account();
protected boolean authenticated = false;
protected File workingDir = Globals.getChrootDir();
protected File workingDir = Globals.getPrimaryDir();
// protected ServerSocket dataServerSocket = null;
protected Socket dataSocket = null;
// protected FTPServerService service;
Expand Down Expand Up @@ -247,7 +247,7 @@ protected InetAddress getLocalAddress() {

static int numNulls = 0;
public void run() {
myLog.l(Log.INFO, "SessionThread started");
myLog.l(Log.INFO, "SessionThread started. root = " + Globals.getChrootDir() + ", primary dir = " + Globals.getPrimaryDir());

if(sendWelcomeBanner) {
writeString("220 SwiFTP " + Util.getVersion() + " ready\r\n");
Expand Down