Fix: macOS 한글 파일명 유니코드 정규화 문제 해결

macOS NFD와 Android NFC 유니코드 정규화 차이로 인한 한글 파일명 처리 오류 수정.
파일 검색 시 NFD/NFC 모두 지원하도록 개선하여 macOS에서 업로드/다운로드/삭제 작업이 정상 동작함.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-01-02 00:58:17 +09:00
parent a03300ed62
commit e6e4ef7df5
6 changed files with 412 additions and 175 deletions

Binary file not shown.

View File

@@ -1,9 +1,14 @@
package be.gyu.android.server.ftp; package be.gyu.android.server.ftp;
import android.content.Context;
import android.net.Uri;
import android.os.Environment; import android.os.Environment;
import android.util.Log; import android.util.Log;
import androidx.documentfile.provider.DocumentFile;
import java.io.File; import java.io.File;
import java.text.Normalizer;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
@@ -13,34 +18,41 @@ import java.util.Locale;
public class FTPFileSystem { public class FTPFileSystem {
private static final String TAG = "FTPFileSystem"; private static final String TAG = "FTPFileSystem";
private final File rootDirectory; private final Context context;
private File currentDirectory; private final DocumentFile rootDirectory;
private DocumentFile currentDirectory;
private final boolean useDocumentFile;
public FTPFileSystem() { public FTPFileSystem(Context context) {
this(null); this(context, null);
} }
public FTPFileSystem(String rootDirectoryPath) { public FTPFileSystem(Context context, Uri rootDirectoryUri) {
if (rootDirectoryPath != null && !rootDirectoryPath.isEmpty()) { this.context = context;
// Use user-specified directory
File userDir = new File(rootDirectoryPath); if (rootDirectoryUri != null) {
if (userDir.exists() && userDir.isDirectory()) { // Use user-specified directory via DocumentFile
DocumentFile userDir = DocumentFile.fromTreeUri(context, rootDirectoryUri);
if (userDir != null && userDir.exists() && userDir.isDirectory()) {
this.rootDirectory = userDir; this.rootDirectory = userDir;
Log.i(TAG, "Using user-specified root: " + rootDirectory.getAbsolutePath()); this.useDocumentFile = true;
Log.i(TAG, "Using user-specified root (DocumentFile): " + rootDirectoryUri);
} else { } else {
Log.w(TAG, "User-specified directory does not exist: " + rootDirectoryPath); Log.w(TAG, "User-specified directory does not exist: " + rootDirectoryUri);
this.rootDirectory = getDefaultRootDirectory(); this.rootDirectory = getDefaultRootDirectory();
this.useDocumentFile = false;
} }
} else { } else {
// Use default directory // Use default directory
this.rootDirectory = getDefaultRootDirectory(); this.rootDirectory = getDefaultRootDirectory();
this.useDocumentFile = false;
} }
this.currentDirectory = rootDirectory; this.currentDirectory = rootDirectory;
Log.d(TAG, "File system initialized. Root: " + rootDirectory.getAbsolutePath()); Log.d(TAG, "File system initialized. Root: " + getDisplayPath(rootDirectory));
} }
private File getDefaultRootDirectory() { private DocumentFile getDefaultRootDirectory() {
File externalStorage = Environment.getExternalStorageDirectory(); File externalStorage = Environment.getExternalStorageDirectory();
File ftpServerDir = new File(externalStorage, "FTPServer"); File ftpServerDir = new File(externalStorage, "FTPServer");
@@ -48,14 +60,22 @@ public class FTPFileSystem {
if (!ftpServerDir.exists()) { if (!ftpServerDir.exists()) {
if (ftpServerDir.mkdirs()) { if (ftpServerDir.mkdirs()) {
Log.i(TAG, "Root directory created: " + ftpServerDir.getAbsolutePath()); Log.i(TAG, "Root directory created: " + ftpServerDir.getAbsolutePath());
return ftpServerDir;
} else { } else {
Log.w(TAG, "Failed to create root directory, using external storage root"); Log.w(TAG, "Failed to create root directory, using external storage root");
return externalStorage; ftpServerDir = externalStorage;
} }
} else {
return ftpServerDir;
} }
return DocumentFile.fromFile(ftpServerDir);
}
private String getDisplayPath(DocumentFile file) {
if (file == null) return "null";
Uri uri = file.getUri();
if (uri != null) {
return uri.toString();
}
return file.getName();
} }
public String getCurrentPath() { public String getCurrentPath() {
@@ -64,71 +84,55 @@ public class FTPFileSystem {
} }
public boolean changeDirectory(String path) { public boolean changeDirectory(String path) {
File newDir; DocumentFile newDir;
if (path.startsWith("/")) { if (path.startsWith("/")) {
// Absolute path // Absolute path
newDir = new File(rootDirectory, path.substring(1)); newDir = findDocumentFile(rootDirectory, path.substring(1));
} else { } else {
// Relative path // Relative path
newDir = new File(currentDirectory, path); newDir = findDocumentFile(currentDirectory, path);
} }
try { if (newDir != null && newDir.exists() && newDir.isDirectory()) {
String canonicalPath = newDir.getCanonicalPath();
String rootPath = rootDirectory.getCanonicalPath();
// Security check: prevent escaping root directory // Security check: prevent escaping root directory
if (!canonicalPath.startsWith(rootPath)) { if (isSubDirectory(newDir, rootDirectory)) {
Log.w(TAG, "Attempted to escape root directory: " + canonicalPath);
return false;
}
if (newDir.exists() && newDir.isDirectory()) {
currentDirectory = newDir; currentDirectory = newDir;
Log.d(TAG, "Changed directory to: " + currentDirectory.getAbsolutePath()); Log.d(TAG, "Changed directory to: " + getDisplayPath(currentDirectory));
return true; return true;
} else { } else {
Log.w(TAG, "Directory does not exist: " + newDir.getAbsolutePath()); Log.w(TAG, "Attempted to escape root directory");
return false; return false;
} }
} catch (Exception e) { } else {
Log.e(TAG, "Error changing directory: " + e.getMessage()); Log.w(TAG, "Directory does not exist: " + path);
return false; return false;
} }
} }
public boolean changeToParentDirectory() { public boolean changeToParentDirectory() {
File parent = currentDirectory.getParentFile(); DocumentFile parent = currentDirectory.getParentFile();
if (parent == null) { if (parent == null) {
return false; return false;
} }
try { // Cannot go above root directory
String parentPath = parent.getCanonicalPath(); if (!isSubDirectory(parent, rootDirectory) && !isSameFile(parent, rootDirectory)) {
String rootPath = rootDirectory.getCanonicalPath();
// Cannot go above root directory
if (!parentPath.startsWith(rootPath)) {
return false;
}
currentDirectory = parent;
Log.d(TAG, "Changed to parent directory: " + currentDirectory.getAbsolutePath());
return true;
} catch (Exception e) {
Log.e(TAG, "Error changing to parent directory: " + e.getMessage());
return false; return false;
} }
currentDirectory = parent;
Log.d(TAG, "Changed to parent directory: " + getDisplayPath(currentDirectory));
return true;
} }
public List<File> listFiles() { public List<DocumentFile> listFiles() {
File[] files = currentDirectory.listFiles(); DocumentFile[] files = currentDirectory.listFiles();
List<File> fileList = new ArrayList<>(); List<DocumentFile> fileList = new ArrayList<>();
if (files != null) { if (files != null) {
for (File file : files) { for (DocumentFile file : files) {
fileList.add(file); fileList.add(file);
} }
} }
@@ -137,19 +141,19 @@ public class FTPFileSystem {
} }
public String formatFileList(boolean detailed) { public String formatFileList(boolean detailed) {
List<File> files = listFiles(); List<DocumentFile> files = listFiles();
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
if (detailed) { if (detailed) {
// LIST format: Unix-style detailed listing // LIST format: Unix-style detailed listing
SimpleDateFormat dateFormat = new SimpleDateFormat("MMM dd HH:mm", Locale.US); SimpleDateFormat dateFormat = new SimpleDateFormat("MMM dd HH:mm", Locale.US);
for (File file : files) { for (DocumentFile file : files) {
sb.append(formatDetailedFile(file, dateFormat)).append("\r\n"); sb.append(formatDetailedFile(file, dateFormat)).append("\r\n");
} }
} else { } else {
// NLST format: names only // NLST format: names only
for (File file : files) { for (DocumentFile file : files) {
sb.append(file.getName()).append("\r\n"); sb.append(file.getName()).append("\r\n");
} }
} }
@@ -157,7 +161,7 @@ public class FTPFileSystem {
return sb.toString(); return sb.toString();
} }
private String formatDetailedFile(File file, SimpleDateFormat dateFormat) { private String formatDetailedFile(DocumentFile file, SimpleDateFormat dateFormat) {
// Format: drwxrwxrwx 1 owner group size date name // Format: drwxrwxrwx 1 owner group size date name
String permissions = file.isDirectory() ? "drwxr-xr-x" : "-rw-r--r--"; String permissions = file.isDirectory() ? "drwxr-xr-x" : "-rw-r--r--";
String owner = "ftp"; String owner = "ftp";
@@ -171,123 +175,198 @@ public class FTPFileSystem {
} }
public boolean makeDirectory(String dirName) { public boolean makeDirectory(String dirName) {
File newDir = new File(currentDirectory, dirName); DocumentFile newDir = currentDirectory.createDirectory(dirName);
try { if (newDir != null) {
String canonicalPath = newDir.getCanonicalPath(); Log.i(TAG, "Directory created: " + dirName);
String rootPath = rootDirectory.getCanonicalPath(); return true;
} else {
if (!canonicalPath.startsWith(rootPath)) { Log.w(TAG, "Failed to create directory: " + dirName);
return false;
}
if (newDir.mkdir()) {
Log.i(TAG, "Directory created: " + newDir.getAbsolutePath());
return true;
} else {
Log.w(TAG, "Failed to create directory: " + newDir.getAbsolutePath());
return false;
}
} catch (Exception e) {
Log.e(TAG, "Error creating directory: " + e.getMessage());
return false; return false;
} }
} }
public boolean removeDirectory(String dirName) { public boolean removeDirectory(String dirName) {
File dir = new File(currentDirectory, dirName); DocumentFile dir = findFileWithNormalization(currentDirectory, dirName);
try { if (dir != null && dir.exists() && dir.isDirectory() && dir.delete()) {
String canonicalPath = dir.getCanonicalPath(); Log.i(TAG, "Directory removed: " + dirName);
String rootPath = rootDirectory.getCanonicalPath(); return true;
} else {
if (!canonicalPath.startsWith(rootPath)) { Log.w(TAG, "Failed to remove directory: " + dirName);
return false;
}
if (dir.exists() && dir.isDirectory() && dir.delete()) {
Log.i(TAG, "Directory removed: " + dir.getAbsolutePath());
return true;
} else {
Log.w(TAG, "Failed to remove directory: " + dir.getAbsolutePath());
return false;
}
} catch (Exception e) {
Log.e(TAG, "Error removing directory: " + e.getMessage());
return false; return false;
} }
} }
public boolean deleteFile(String fileName) { public boolean deleteFile(String fileName) {
File file = new File(currentDirectory, fileName); DocumentFile file = findFileWithNormalization(currentDirectory, fileName);
try { if (file != null && file.exists() && file.isFile() && file.delete()) {
String canonicalPath = file.getCanonicalPath(); Log.i(TAG, "File deleted: " + fileName);
String rootPath = rootDirectory.getCanonicalPath(); return true;
} else {
if (!canonicalPath.startsWith(rootPath)) { Log.w(TAG, "Failed to delete file: " + fileName);
return false;
}
if (file.exists() && file.isFile() && file.delete()) {
Log.i(TAG, "File deleted: " + file.getAbsolutePath());
return true;
} else {
Log.w(TAG, "Failed to delete file: " + file.getAbsolutePath());
return false;
}
} catch (Exception e) {
Log.e(TAG, "Error deleting file: " + e.getMessage());
return false; return false;
} }
} }
public File getFile(String fileName) { public DocumentFile getFile(String fileName) {
File file = new File(currentDirectory, fileName); return findFileWithNormalization(currentDirectory, fileName);
try {
String canonicalPath = file.getCanonicalPath();
String rootPath = rootDirectory.getCanonicalPath();
if (!canonicalPath.startsWith(rootPath)) {
return null;
}
return file.exists() ? file : null;
} catch (Exception e) {
Log.e(TAG, "Error getting file: " + e.getMessage());
return null;
}
} }
public long getFileSize(String fileName) { public long getFileSize(String fileName) {
File file = getFile(fileName); DocumentFile file = getFile(fileName);
return (file != null && file.isFile()) ? file.length() : -1; return (file != null && file.isFile()) ? file.length() : -1;
} }
private String getRelativePath(File file) { private String getRelativePath(DocumentFile file) {
try { if (file == null) {
String filePath = file.getCanonicalPath();
String rootPath = rootDirectory.getCanonicalPath();
if (filePath.equals(rootPath)) {
return "";
} else if (filePath.startsWith(rootPath + File.separator)) {
return filePath.substring(rootPath.length() + 1).replace(File.separator, "/");
} else {
return "";
}
} catch (Exception e) {
Log.e(TAG, "Error getting relative path: " + e.getMessage());
return ""; return "";
} }
if (isSameFile(file, rootDirectory)) {
return "";
}
List<String> pathParts = new ArrayList<>();
DocumentFile current = file;
while (current != null && !isSameFile(current, rootDirectory)) {
pathParts.add(0, current.getName());
current = current.getParentFile();
}
if (current == null) {
return "";
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < pathParts.size(); i++) {
sb.append(pathParts.get(i));
if (i < pathParts.size() - 1) {
sb.append("/");
}
}
return sb.toString();
} }
public File getRootDirectory() { private boolean isSameFile(DocumentFile file1, DocumentFile file2) {
if (file1 == null || file2 == null) {
return false;
}
return file1.getUri().equals(file2.getUri());
}
private boolean isSubDirectory(DocumentFile child, DocumentFile parent) {
if (child == null || parent == null) {
return false;
}
if (isSameFile(child, parent)) {
return true;
}
DocumentFile current = child;
while (current != null) {
if (isSameFile(current, parent)) {
return true;
}
current = current.getParentFile();
}
return false;
}
private DocumentFile findDocumentFile(DocumentFile parent, String path) {
if (path == null || path.isEmpty()) {
return parent;
}
String[] parts = path.split("/");
DocumentFile current = parent;
for (String part : parts) {
if (part.isEmpty() || part.equals(".")) {
continue;
}
if (part.equals("..")) {
DocumentFile parentFile = current.getParentFile();
if (parentFile != null && isSubDirectory(parentFile, rootDirectory)) {
current = parentFile;
}
continue;
}
DocumentFile next = findFileWithNormalization(current, part);
if (next == null) {
return null;
}
current = next;
}
return current;
}
public DocumentFile getRootDirectory() {
return rootDirectory; return rootDirectory;
} }
public File getCurrentDirectory() { public DocumentFile getCurrentDirectory() {
return currentDirectory; return currentDirectory;
} }
public Context getContext() {
return context;
}
/**
* Find a file with Unicode normalization handling for macOS compatibility.
* This method tries multiple approaches to find the file:
* 1. Direct match with given name
* 2. Try NFD normalization (macOS format)
* 3. Manual search with NFC normalization comparison
*/
private DocumentFile findFileWithNormalization(DocumentFile parent, String fileName) {
if (parent == null || fileName == null || fileName.isEmpty()) {
return null;
}
// Try to find file with exact name first
DocumentFile file = parent.findFile(fileName);
if (file != null && file.exists()) {
return file;
}
// If not found, try with NFD normalization (for macOS compatibility)
String nfdFileName = Normalizer.normalize(fileName, Normalizer.Form.NFD);
if (!nfdFileName.equals(fileName)) {
file = parent.findFile(nfdFileName);
if (file != null && file.exists()) {
return file;
}
}
// If still not found, manually search through all files
// This handles cases where filesystem has mixed normalization
DocumentFile[] files = parent.listFiles();
if (files != null) {
String normalizedFileName = Normalizer.normalize(fileName, Normalizer.Form.NFC);
for (DocumentFile f : files) {
String name = f.getName();
if (name != null) {
// Compare both NFC normalized forms
String normalizedName = Normalizer.normalize(name, Normalizer.Form.NFC);
if (normalizedName.equals(normalizedFileName)) {
Log.d(TAG, "Found file with normalized match: '" + name + "' == '" + fileName + "'");
return f;
}
}
}
}
Log.d(TAG, "File not found with any normalization: " + fileName);
return null;
}
} }

View File

@@ -1,5 +1,7 @@
package be.gyu.android.server.ftp; package be.gyu.android.server.ftp;
import android.content.Context;
import android.net.Uri;
import android.util.Log; import android.util.Log;
import java.io.IOException; import java.io.IOException;
@@ -19,19 +21,21 @@ public class FTPServer {
private Thread acceptThread; private Thread acceptThread;
private boolean isRunning = false; private boolean isRunning = false;
private int port; private int port;
private String rootDirectory; private Context context;
private Uri rootDirectoryUri;
public FTPServer() { public FTPServer(Context context) {
this(DEFAULT_PORT, null); this(context, DEFAULT_PORT, null);
} }
public FTPServer(int port) { public FTPServer(Context context, int port) {
this(port, null); this(context, port, null);
} }
public FTPServer(int port, String rootDirectory) { public FTPServer(Context context, int port, Uri rootDirectoryUri) {
this.context = context;
this.port = port; this.port = port;
this.rootDirectory = rootDirectory; this.rootDirectoryUri = rootDirectoryUri;
this.executorService = Executors.newFixedThreadPool(MAX_CONNECTIONS); this.executorService = Executors.newFixedThreadPool(MAX_CONNECTIONS);
} }
@@ -52,7 +56,7 @@ public class FTPServer {
Socket clientSocket = serverSocket.accept(); Socket clientSocket = serverSocket.accept();
Log.i(TAG, "New client connection from: " + clientSocket.getInetAddress()); Log.i(TAG, "New client connection from: " + clientSocket.getInetAddress());
FTPSession session = new FTPSession(clientSocket, rootDirectory); FTPSession session = new FTPSession(clientSocket, context, rootDirectoryUri);
executorService.execute(session); executorService.execute(session);
} catch (IOException e) { } catch (IOException e) {

View File

@@ -36,8 +36,12 @@ public class FTPService extends Service {
if (ACTION_START.equals(action)) { if (ACTION_START.equals(action)) {
int port = intent.getIntExtra("port", 2121); int port = intent.getIntExtra("port", 2121);
String rootDir = intent.getStringExtra("rootDir"); String rootDirUriString = intent.getStringExtra("rootDirUri");
startFTPServer(port, rootDir); android.net.Uri rootDirUri = null;
if (rootDirUriString != null && !rootDirUriString.isEmpty()) {
rootDirUri = android.net.Uri.parse(rootDirUriString);
}
startFTPServer(port, rootDirUri);
} else if (ACTION_STOP.equals(action)) { } else if (ACTION_STOP.equals(action)) {
stopFTPServer(); stopFTPServer();
} }
@@ -46,19 +50,19 @@ public class FTPService extends Service {
return START_STICKY; return START_STICKY;
} }
private void startFTPServer(int port, String rootDir) { private void startFTPServer(int port, android.net.Uri rootDirUri) {
if (ftpServer != null && ftpServer.isRunning()) { if (ftpServer != null && ftpServer.isRunning()) {
Log.w(TAG, "FTP Server is already running"); Log.w(TAG, "FTP Server is already running");
return; return;
} }
ftpServer = new FTPServer(port, rootDir); ftpServer = new FTPServer(this, port, rootDirUri);
ftpServer.start(); ftpServer.start();
Notification notification = createNotification("FTP Server is running on port " + ftpServer.getPort()); Notification notification = createNotification("FTP Server is running on port " + ftpServer.getPort());
startForeground(NOTIFICATION_ID, notification); startForeground(NOTIFICATION_ID, notification);
Log.i(TAG, "FTP Server started on port " + port + " with root: " + rootDir); Log.i(TAG, "FTP Server started on port " + port + " with root URI: " + rootDirUri);
} }
private void stopFTPServer() { private void stopFTPServer() {

View File

@@ -1,13 +1,21 @@
package be.gyu.android.server.ftp; package be.gyu.android.server.ftp;
import android.content.Context;
import android.net.Uri;
import android.util.Log; import android.util.Log;
import androidx.documentfile.provider.DocumentFile;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.BufferedWriter; import java.io.BufferedWriter;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter; import java.io.OutputStreamWriter;
import java.net.Socket; import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.text.Normalizer;
public class FTPSession implements Runnable { public class FTPSession implements Runnable {
private static final String TAG = "FTPSession"; private static final String TAG = "FTPSession";
@@ -22,22 +30,24 @@ public class FTPSession implements Runnable {
private FTPFileSystem fileSystem; private FTPFileSystem fileSystem;
private FTPDataConnection dataConnection; private FTPDataConnection dataConnection;
private String transferType = "A"; // A = ASCII, I = Binary private String transferType = "A"; // A = ASCII, I = Binary
private boolean useUtf8 = true; // UTF-8 enabled by default for better compatibility
public FTPSession(Socket socket) { public FTPSession(Socket socket, Context context) {
this(socket, null); this(socket, context, null);
} }
public FTPSession(Socket socket, String rootDirectory) { public FTPSession(Socket socket, Context context, Uri rootDirectoryUri) {
this.controlSocket = socket; this.controlSocket = socket;
this.fileSystem = new FTPFileSystem(rootDirectory); this.fileSystem = new FTPFileSystem(context, rootDirectoryUri);
this.dataConnection = null; this.dataConnection = null;
} }
@Override @Override
public void run() { public void run() {
try { try {
reader = new BufferedReader(new InputStreamReader(controlSocket.getInputStream())); // Use UTF-8 encoding for better international character support
writer = new BufferedWriter(new OutputStreamWriter(controlSocket.getOutputStream())); reader = new BufferedReader(new InputStreamReader(controlSocket.getInputStream(), StandardCharsets.UTF_8));
writer = new BufferedWriter(new OutputStreamWriter(controlSocket.getOutputStream(), StandardCharsets.UTF_8));
Log.d(TAG, "Client connected: " + controlSocket.getInetAddress()); Log.d(TAG, "Client connected: " + controlSocket.getInetAddress());
sendResponse(FTPResponse.SERVICE_READY, "FTP Server ready"); sendResponse(FTPResponse.SERVICE_READY, "FTP Server ready");
@@ -120,6 +130,12 @@ public class FTPSession implements Runnable {
case "NOOP": case "NOOP":
handleNoop(); handleNoop();
break; break;
case "FEAT":
handleFeat();
break;
case "OPTS":
handleOpts(argument);
break;
default: default:
sendResponse(FTPResponse.COMMAND_NOT_IMPLEMENTED_502, "Command not implemented"); sendResponse(FTPResponse.COMMAND_NOT_IMPLEMENTED_502, "Command not implemented");
break; break;
@@ -158,6 +174,8 @@ public class FTPSession implements Runnable {
return; return;
} }
String path = fileSystem.getCurrentPath(); String path = fileSystem.getCurrentPath();
// Convert path to NFD format for MacOS compatibility
path = denormalizeFilename(path);
sendResponse(FTPResponse.PATHNAME_CREATED, "\"" + path + "\" is current directory"); sendResponse(FTPResponse.PATHNAME_CREATED, "\"" + path + "\" is current directory");
} }
@@ -172,8 +190,14 @@ public class FTPSession implements Runnable {
return; return;
} }
// Normalize path for internal use (NFD -> NFC)
path = normalizeFilename(path);
if (fileSystem.changeDirectory(path)) { if (fileSystem.changeDirectory(path)) {
sendResponse(FTPResponse.REQUESTED_FILE_ACTION_OK, "Directory changed to " + fileSystem.getCurrentPath()); String currentPath = fileSystem.getCurrentPath();
// Convert path to NFD format for MacOS compatibility
currentPath = denormalizeFilename(currentPath);
sendResponse(FTPResponse.REQUESTED_FILE_ACTION_OK, "Directory changed to " + currentPath);
} else { } else {
sendResponse(FTPResponse.FILE_UNAVAILABLE, "Failed to change directory"); sendResponse(FTPResponse.FILE_UNAVAILABLE, "Failed to change directory");
} }
@@ -186,7 +210,10 @@ public class FTPSession implements Runnable {
} }
if (fileSystem.changeToParentDirectory()) { if (fileSystem.changeToParentDirectory()) {
sendResponse(FTPResponse.REQUESTED_FILE_ACTION_OK, "Directory changed to " + fileSystem.getCurrentPath()); String currentPath = fileSystem.getCurrentPath();
// Convert path to NFD format for MacOS compatibility
currentPath = denormalizeFilename(currentPath);
sendResponse(FTPResponse.REQUESTED_FILE_ACTION_OK, "Directory changed to " + currentPath);
} else { } else {
sendResponse(FTPResponse.FILE_UNAVAILABLE, "Already at root directory"); sendResponse(FTPResponse.FILE_UNAVAILABLE, "Already at root directory");
} }
@@ -204,6 +231,8 @@ public class FTPSession implements Runnable {
} }
String fileList = fileSystem.formatFileList(true); String fileList = fileSystem.formatFileList(true);
// Convert filenames to NFD format for MacOS compatibility
fileList = convertFileListToNFD(fileList);
sendResponse(FTPResponse.FILE_STATUS_OK, "Opening data connection for directory list"); sendResponse(FTPResponse.FILE_STATUS_OK, "Opening data connection for directory list");
@@ -235,6 +264,8 @@ public class FTPSession implements Runnable {
} }
String fileList = fileSystem.formatFileList(false); String fileList = fileSystem.formatFileList(false);
// Convert filenames to NFD format for MacOS compatibility
fileList = convertFileListToNFD(fileList);
sendResponse(FTPResponse.FILE_STATUS_OK, "Opening data connection for name list"); sendResponse(FTPResponse.FILE_STATUS_OK, "Opening data connection for name list");
@@ -265,8 +296,13 @@ public class FTPSession implements Runnable {
return; return;
} }
// Normalize filename for MacOS compatibility (NFD -> NFC for storage)
dirName = normalizeFilename(dirName);
if (fileSystem.makeDirectory(dirName)) { if (fileSystem.makeDirectory(dirName)) {
String newPath = fileSystem.getCurrentPath() + "/" + dirName; String newPath = fileSystem.getCurrentPath() + "/" + dirName;
// Convert path to NFD format for MacOS compatibility (NFC -> NFD for display)
newPath = denormalizeFilename(newPath);
sendResponse(FTPResponse.PATHNAME_CREATED, "\"" + newPath + "\" directory created"); sendResponse(FTPResponse.PATHNAME_CREATED, "\"" + newPath + "\" directory created");
} else { } else {
sendResponse(FTPResponse.FILE_UNAVAILABLE, "Failed to create directory"); sendResponse(FTPResponse.FILE_UNAVAILABLE, "Failed to create directory");
@@ -284,6 +320,9 @@ public class FTPSession implements Runnable {
return; return;
} }
// Normalize filename for MacOS compatibility
dirName = normalizeFilename(dirName);
if (fileSystem.removeDirectory(dirName)) { if (fileSystem.removeDirectory(dirName)) {
sendResponse(FTPResponse.REQUESTED_FILE_ACTION_OK, "Directory removed"); sendResponse(FTPResponse.REQUESTED_FILE_ACTION_OK, "Directory removed");
} else { } else {
@@ -302,6 +341,9 @@ public class FTPSession implements Runnable {
return; return;
} }
// Normalize filename for MacOS compatibility
fileName = normalizeFilename(fileName);
if (fileSystem.deleteFile(fileName)) { if (fileSystem.deleteFile(fileName)) {
sendResponse(FTPResponse.REQUESTED_FILE_ACTION_OK, "File deleted"); sendResponse(FTPResponse.REQUESTED_FILE_ACTION_OK, "File deleted");
} else { } else {
@@ -320,6 +362,9 @@ public class FTPSession implements Runnable {
return; return;
} }
// Normalize filename for MacOS compatibility
fileName = normalizeFilename(fileName);
long size = fileSystem.getFileSize(fileName); long size = fileSystem.getFileSize(fileName);
if (size >= 0) { if (size >= 0) {
sendResponse(FTPResponse.FILE_STATUS, String.valueOf(size)); sendResponse(FTPResponse.FILE_STATUS, String.valueOf(size));
@@ -395,7 +440,10 @@ public class FTPSession implements Runnable {
return; return;
} }
java.io.File file = fileSystem.getFile(fileName); // Normalize filename for MacOS compatibility
fileName = normalizeFilename(fileName);
DocumentFile file = fileSystem.getFile(fileName);
if (file == null || !file.exists() || !file.isFile()) { if (file == null || !file.exists() || !file.isFile()) {
sendResponse(FTPResponse.FILE_UNAVAILABLE, "File not found"); sendResponse(FTPResponse.FILE_UNAVAILABLE, "File not found");
dataConnection.close(); dataConnection.close();
@@ -407,14 +455,14 @@ public class FTPSession implements Runnable {
if (dataConnection.acceptConnection()) { if (dataConnection.acceptConnection()) {
try { try {
java.io.FileInputStream fis = new java.io.FileInputStream(file); InputStream fis = fileSystem.getContext().getContentResolver().openInputStream(file.getUri());
if (dataConnection.transferStream(fis)) { if (fis != null && dataConnection.transferStream(fis)) {
fis.close(); fis.close();
dataConnection.close(); dataConnection.close();
sendResponse(FTPResponse.CLOSING_DATA_CONNECTION, "Transfer complete"); sendResponse(FTPResponse.CLOSING_DATA_CONNECTION, "Transfer complete");
Log.i(TAG, "File sent: " + fileName + " (" + file.length() + " bytes)"); Log.i(TAG, "File sent: " + fileName + " (" + file.length() + " bytes)");
} else { } else {
fis.close(); if (fis != null) fis.close();
dataConnection.close(); dataConnection.close();
sendResponse(FTPResponse.CONNECTION_CLOSED, "Transfer failed"); sendResponse(FTPResponse.CONNECTION_CLOSED, "Transfer failed");
} }
@@ -447,20 +495,37 @@ public class FTPSession implements Runnable {
return; return;
} }
java.io.File file = new java.io.File(fileSystem.getCurrentDirectory(), fileName); // Normalize filename for MacOS compatibility
fileName = normalizeFilename(fileName);
// Create or get the file in the current directory
DocumentFile currentDir = fileSystem.getCurrentDirectory();
DocumentFile file = currentDir.findFile(fileName);
// If file doesn't exist, create it
if (file == null) {
file = currentDir.createFile("application/octet-stream", fileName);
}
if (file == null) {
sendResponse(FTPResponse.FILE_UNAVAILABLE, "Cannot create file");
dataConnection.close();
dataConnection = null;
return;
}
sendResponse(FTPResponse.FILE_STATUS_OK, "Opening data connection for " + fileName); sendResponse(FTPResponse.FILE_STATUS_OK, "Opening data connection for " + fileName);
if (dataConnection.acceptConnection()) { if (dataConnection.acceptConnection()) {
try { try {
java.io.FileOutputStream fos = new java.io.FileOutputStream(file); OutputStream fos = fileSystem.getContext().getContentResolver().openOutputStream(file.getUri(), "wt");
if (dataConnection.receiveStream(fos)) { if (fos != null && dataConnection.receiveStream(fos)) {
fos.close(); fos.close();
dataConnection.close(); dataConnection.close();
sendResponse(FTPResponse.CLOSING_DATA_CONNECTION, "Transfer complete"); sendResponse(FTPResponse.CLOSING_DATA_CONNECTION, "Transfer complete");
Log.i(TAG, "File received: " + fileName + " (" + file.length() + " bytes)"); Log.i(TAG, "File received: " + fileName + " (" + file.length() + " bytes)");
} else { } else {
fos.close(); if (fos != null) fos.close();
dataConnection.close(); dataConnection.close();
sendResponse(FTPResponse.CONNECTION_CLOSED, "Transfer failed"); sendResponse(FTPResponse.CONNECTION_CLOSED, "Transfer failed");
} }
@@ -481,6 +546,90 @@ public class FTPSession implements Runnable {
sendResponse(FTPResponse.COMMAND_OK, "OK"); sendResponse(FTPResponse.COMMAND_OK, "OK");
} }
private void handleFeat() throws IOException {
// Send list of supported features
writer.write("211-Features:\r\n");
writer.write(" UTF8\r\n");
writer.write(" SIZE\r\n");
writer.write(" PASV\r\n");
writer.write("211 End\r\n");
writer.flush();
Log.d(TAG, "FEAT command handled");
}
private void handleOpts(String options) throws IOException {
if (options.isEmpty()) {
sendResponse(FTPResponse.SYNTAX_ERROR_PARAMETERS, "No options specified");
return;
}
String[] parts = options.split("\\s+", 2);
String option = parts[0].toUpperCase();
String value = parts.length > 1 ? parts[1].toUpperCase() : "";
if (option.equals("UTF8")) {
if (value.equals("ON") || value.isEmpty()) {
useUtf8 = true;
sendResponse(FTPResponse.COMMAND_OK, "UTF8 enabled");
Log.d(TAG, "UTF-8 encoding enabled");
} else if (value.equals("OFF")) {
useUtf8 = false;
sendResponse(FTPResponse.COMMAND_OK, "UTF8 disabled");
Log.d(TAG, "UTF-8 encoding disabled");
} else {
sendResponse(FTPResponse.SYNTAX_ERROR_PARAMETERS, "Invalid UTF8 option");
}
} else {
sendResponse(FTPResponse.COMMAND_NOT_IMPLEMENTED_FOR_PARAMETER, "Option not supported");
}
}
/**
* Normalize filename from MacOS NFD (Decomposed) to NFC (Composed) format.
* MacOS uses NFD normalization for filenames, which can cause issues on Android/Windows.
* This method converts filenames to NFC format for compatibility.
* Used when RECEIVING filenames from client (STOR, RETR, DELE, etc.)
*/
private String normalizeFilename(String filename) {
if (filename == null || filename.isEmpty()) {
return filename;
}
// Normalize to NFC (Canonical Decomposition, followed by Canonical Composition)
String normalized = Normalizer.normalize(filename, Normalizer.Form.NFC);
Log.d(TAG, "Filename normalized NFD->NFC: '" + filename + "' -> '" + normalized + "'");
return normalized;
}
/**
* Normalize filename to NFD (Decomposed) format for MacOS compatibility.
* MacOS expects filenames in NFD format for proper display.
* This method converts filenames from NFC to NFD format.
* Used when SENDING filenames to client (LIST, NLST, PWD, etc.)
*/
private String denormalizeFilename(String filename) {
if (filename == null || filename.isEmpty()) {
return filename;
}
// Normalize to NFD (Canonical Decomposition) for MacOS
String normalized = Normalizer.normalize(filename, Normalizer.Form.NFD);
Log.d(TAG, "Filename normalized NFC->NFD: '" + filename + "' -> '" + normalized + "'");
return normalized;
}
/**
* Convert entire file list to NFD format for MacOS compatibility.
* This method normalizes all filenames in the file list string to NFD format.
*/
private String convertFileListToNFD(String fileList) {
if (fileList == null || fileList.isEmpty()) {
return fileList;
}
// Normalize entire string to NFD for MacOS
String normalized = Normalizer.normalize(fileList, Normalizer.Form.NFD);
Log.d(TAG, "File list converted to NFD for MacOS");
return normalized;
}
private void sendResponse(int code, String message) throws IOException { private void sendResponse(int code, String message) throws IOException {
String response = FTPResponse.format(code, message); String response = FTPResponse.format(code, message);
writer.write(response); writer.write(response);

View File

@@ -184,7 +184,8 @@ public class MainActivity extends AppCompatActivity {
} }
} }
if (!config.hasRootDirectory()) { String rootDirUri = config.getRootDirectoryUri();
if (rootDirUri == null || rootDirUri.isEmpty()) {
Toast.makeText(this, "Please select a root directory first", Toast.LENGTH_LONG).show(); Toast.makeText(this, "Please select a root directory first", Toast.LENGTH_LONG).show();
return; return;
} }
@@ -192,7 +193,7 @@ public class MainActivity extends AppCompatActivity {
Intent serviceIntent = new Intent(this, FTPService.class); Intent serviceIntent = new Intent(this, FTPService.class);
serviceIntent.setAction(FTPService.ACTION_START); serviceIntent.setAction(FTPService.ACTION_START);
serviceIntent.putExtra("port", config.getPort()); serviceIntent.putExtra("port", config.getPort());
serviceIntent.putExtra("rootDir", config.getRootDirectoryPath()); serviceIntent.putExtra("rootDirUri", rootDirUri);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(serviceIntent); startForegroundService(serviceIntent);