FTP: Better error reporting, ignore hidden directories

This commit is contained in:
Lorenz Meier
2015-05-16 11:37:41 +02:00
parent 97b8e7a20e
commit 2432418751
+28 -11
View File
@@ -1,6 +1,6 @@
/**************************************************************************** /****************************************************************************
* *
* Copyright (c) 2014 PX4 Development Team. All rights reserved. * Copyright (c) 2014, 2015 PX4 Development Team. All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions * modification, are permitted provided that the following conditions
@@ -301,23 +301,32 @@ MavlinkFTP::_reply(mavlink_file_transfer_protocol_t* ftp_req)
MavlinkFTP::ErrorCode MavlinkFTP::ErrorCode
MavlinkFTP::_workList(PayloadHeader* payload) MavlinkFTP::_workList(PayloadHeader* payload)
{ {
char dirPath[kMaxDataLength]; char dirPath[kMaxDataLength];
strncpy(dirPath, _data_as_cstring(payload), kMaxDataLength); strncpy(dirPath, _data_as_cstring(payload), kMaxDataLength);
ErrorCode errorCode = kErrNone;
unsigned offset = 0;
DIR *dp = opendir(dirPath); DIR *dp = opendir(dirPath);
if (dp == nullptr) { if (dp == nullptr) {
warnx("FTP: can't open path '%s'", dirPath); _mavlink->send_statustext_critical("FTP: can't open path (file system corrupted?)");
return kErrFailErrno; _mavlink->send_statustext_critical(dirPath);
// this is not an FTP error, abort directory read and continue
payload->data[offset++] = kDirentSkip;
*((char *)&payload->data[offset]) = '\0';
offset++;
payload->size = offset;
return errorCode;
} }
#ifdef MAVLINK_FTP_DEBUG #ifdef MAVLINK_FTP_DEBUG
warnx("FTP: list %s offset %d", dirPath, payload->offset); warnx("FTP: list %s offset %d", dirPath, payload->offset);
#endif #endif
ErrorCode errorCode = kErrNone;
struct dirent entry, *result = nullptr; struct dirent entry, *result = nullptr;
unsigned offset = 0;
// move to the requested offset // move to the requested offset
seekdir(dp, payload->offset); seekdir(dp, payload->offset);
@@ -325,9 +334,16 @@ MavlinkFTP::_workList(PayloadHeader* payload)
for (;;) { for (;;) {
// read the directory entry // read the directory entry
if (readdir_r(dp, &entry, &result)) { if (readdir_r(dp, &entry, &result)) {
warnx("FTP: list %s readdir_r failure\n", dirPath); _mavlink->send_statustext_critical("FTP: list readdir_r failure");
errorCode = kErrFailErrno; _mavlink->send_statustext_critical(dirPath);
break;
payload->data[offset++] = kDirentSkip;
*((char *)&payload->data[offset]) = '\0';
offset++;
payload->size = offset;
closedir(dp);
return errorCode;
} }
// no more entries? // no more entries?
@@ -357,7 +373,8 @@ MavlinkFTP::_workList(PayloadHeader* payload)
} }
break; break;
case DTYPE_DIRECTORY: case DTYPE_DIRECTORY:
if (strcmp(entry.d_name, ".") == 0 || strcmp(entry.d_name, "..") == 0) { // XXX @DonLakeFlyer: Remove the first condition for the test setup
if ((entry.d_name[0] == '.') || strcmp(entry.d_name, ".") == 0 || strcmp(entry.d_name, "..") == 0) {
// Don't bother sending these back // Don't bother sending these back
direntType = kDirentSkip; direntType = kDirentSkip;
} else { } else {