测试程序可以使用

This commit is contained in:
2026-07-21 23:05:20 +08:00
parent 46a74f24f3
commit 038a2542af
4 changed files with 361 additions and 40 deletions

View File

@@ -350,7 +350,14 @@ static int cmd_pass(lftpd_client_t *client, const char *arg)
static int cmd_pasv(lftpd_client_t *client, const char *arg)
{
int listener_socket = lftpd_inet_listen(0);
int listener_socket = -1;
int retry;
for (retry = 0; retry < 3; retry++) {
listener_socket = lftpd_inet_listen(0);
if (listener_socket >= 0) break;
vTaskDelay(pdMS_TO_TICKS(200));
}
if (listener_socket < 0) {
send_simple_response(client->socket, 425, STATUS_425);
return -1;

View File

@@ -6,6 +6,27 @@
#include "ff.h"
#include "private/lftpd_io.h"
#define DBG_TAG "[LFT_IO]"
#include "dbg_log.h"
/*
* FatFS 在 FF_USE_LFN=0 时不接受 "/" 前缀路径。
* 剥离前导 "/" 后用 FatFS 兼容路径,根目录用 "." 替代空字符串。
*/
static const char *to_fatfs_path(const char *path)
{
if (path == NULL || *path == '\0') {
return ".";
}
if (*path == '/') {
path++;
if (*path == '\0') {
return ".";
}
}
return path;
}
static int s_file_open = 0;
static FIL s_ftp_file;
@@ -74,9 +95,13 @@ char *lftpd_io_canonicalize_path(const char *base, const char *name)
int lftpd_io_open_read(const char *path)
{
if (s_file_open) {
DBG_ERROR("open_read '%s': already open", path);
return -1;
}
if (f_open(&s_ftp_file, path, FA_READ) != FR_OK) {
const char *fpath = to_fatfs_path(path);
FRESULT fr = f_open(&s_ftp_file, fpath, FA_READ);
if (fr != FR_OK) {
DBG_ERROR("open_read '%s': f_open err=%d", fpath, (int)fr);
return -1;
}
s_file_open = 1;
@@ -86,9 +111,13 @@ int lftpd_io_open_read(const char *path)
int lftpd_io_open_write(const char *path)
{
if (s_file_open) {
DBG_ERROR("open_write '%s': already open", path);
return -1;
}
if (f_open(&s_ftp_file, path, FA_WRITE | FA_CREATE_ALWAYS) != FR_OK) {
const char *fpath = to_fatfs_path(path);
FRESULT fr = f_open(&s_ftp_file, fpath, FA_WRITE | FA_CREATE_ALWAYS);
if (fr != FR_OK) {
DBG_ERROR("open_write '%s': f_open err=%d", fpath, (int)fr);
return -1;
}
s_file_open = 1;
@@ -130,8 +159,9 @@ void lftpd_io_close(int handle)
int lftpd_io_stat(const char *path, uint32_t *p_size, int *p_is_dir)
{
FILINFO fno;
const char *fpath = to_fatfs_path(path);
if (f_stat(path, &fno) != FR_OK) {
if (f_stat(fpath, &fno) != FR_OK) {
return -1;
}
@@ -147,7 +177,8 @@ int lftpd_io_stat(const char *path, uint32_t *p_size, int *p_is_dir)
int lftpd_io_unlink(const char *path)
{
if (f_unlink(path) == FR_OK) {
const char *fpath = to_fatfs_path(path);
if (f_unlink(fpath) == FR_OK) {
return 0;
}
return -1;
@@ -159,7 +190,8 @@ void *lftpd_io_opendir(const char *path)
if (dp == NULL) {
return NULL;
}
if (f_opendir(dp, path) != FR_OK) {
const char *fpath = to_fatfs_path(path);
if (f_opendir(dp, fpath) != FR_OK) {
free(dp);
return NULL;
}