From d03611763ae5329a3a354bb95b03a732de2e5e11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ot=C3=A1vio?= Date: Sat, 3 Sep 2016 08:45:40 -0300 Subject: [PATCH] Use readdir instead of readdir_r (#5421) readdir_r is deprecated since glibc version 2.24 and glibc manual recomends usage of readdir instead of readdir_r. Replacing readdir_r by readdir will also not be a problem for Nuttx, because readdir_r is using readdir and filling parameters with the return information. Signed-off-by: Otavio Pontes --- src/modules/mavlink/mavlink_ftp.cpp | 43 ++++++++------- src/modules/mavlink/mavlink_log_handler.cpp | 59 ++++++++------------- 2 files changed, 45 insertions(+), 57 deletions(-) diff --git a/src/modules/mavlink/mavlink_ftp.cpp b/src/modules/mavlink/mavlink_ftp.cpp index ece85facba..e6fe801d5e 100644 --- a/src/modules/mavlink/mavlink_ftp.cpp +++ b/src/modules/mavlink/mavlink_ftp.cpp @@ -327,32 +327,35 @@ MavlinkFTP::_workList(PayloadHeader *payload, bool list_hidden) warnx("FTP: list %s offset %d", dirPath, payload->offset); #endif - struct dirent entry, *result = nullptr; + struct dirent *result = nullptr; // move to the requested offset seekdir(dp, payload->offset); for (;;) { + errno = 0; + result = readdir(dp); + // read the directory entry - if (readdir_r(dp, &entry, &result)) { + if (result == nullptr) { + if (errno) { #ifdef MAVLINK_FTP_UNIT_TEST - warnx("readdir_r failed"); + warnx("readdir failed"); #else - _mavlink->send_statustext_critical("FTP: list readdir_r failure"); - _mavlink->send_statustext_critical(dirPath); + _mavlink->send_statustext_critical("FTP: list readdir failure"); + _mavlink->send_statustext_critical(dirPath); #endif - payload->data[offset++] = kDirentSkip; - *((char *)&payload->data[offset]) = '\0'; - offset++; - payload->size = offset; - closedir(dp); + payload->data[offset++] = kDirentSkip; + *((char *)&payload->data[offset]) = '\0'; + offset++; + payload->size = offset; + closedir(dp); - return errorCode; - } + return errorCode; + } - // no more entries? - if (result == nullptr) { + // no more entries? if (payload->offset != 0 && offset == 0) { // User is requesting subsequent dir entries but there were none. This means the user asked // to seek past EOF. @@ -368,7 +371,7 @@ MavlinkFTP::_workList(PayloadHeader *payload, bool list_hidden) char direntType; // Determine the directory entry type - switch (entry.d_type) { + switch (result->d_type) { #ifdef __PX4_NUTTX case DTYPE_FILE: @@ -377,7 +380,7 @@ MavlinkFTP::_workList(PayloadHeader *payload, bool list_hidden) #endif // For files we get the file size as well direntType = kDirentFile; - snprintf(buf, sizeof(buf), "%s/%s", dirPath, entry.d_name); + snprintf(buf, sizeof(buf), "%s/%s", dirPath, result->d_name); struct stat st; if (stat(buf, &st) == 0) { @@ -391,8 +394,8 @@ MavlinkFTP::_workList(PayloadHeader *payload, bool list_hidden) #else case DT_DIR: #endif - if ((!list_hidden && (strncmp(entry.d_name, ".", 1) == 0)) || - strcmp(entry.d_name, ".") == 0 || strcmp(entry.d_name, "..") == 0) { + if ((!list_hidden && (strncmp(result->d_name, ".", 1) == 0)) || + strcmp(result->d_name, ".") == 0 || strcmp(result->d_name, "..") == 0) { // Don't bother sending these back direntType = kDirentSkip; @@ -413,11 +416,11 @@ MavlinkFTP::_workList(PayloadHeader *payload, bool list_hidden) } else if (direntType == kDirentFile) { // Files send filename and file length - snprintf(buf, sizeof(buf), "%s\t%d", entry.d_name, fileSize); + snprintf(buf, sizeof(buf), "%s\t%d", result->d_name, fileSize); } else { // Everything else just sends name - strncpy(buf, entry.d_name, sizeof(buf)); + strncpy(buf, result->d_name, sizeof(buf)); buf[sizeof(buf) - 1] = 0; } diff --git a/src/modules/mavlink/mavlink_log_handler.cpp b/src/modules/mavlink/mavlink_log_handler.cpp index 7a477d2d62..d589e0878c 100644 --- a/src/modules/mavlink/mavlink_log_handler.cpp +++ b/src/modules/mavlink/mavlink_log_handler.cpp @@ -278,18 +278,13 @@ MavlinkLogHandler::_log_request_erase(const mavlink_message_t * /*msg*/) DIR *dp = opendir(kSDRoot); if (dp) { - struct dirent entry, *result = nullptr; + struct dirent *result = nullptr; - while (readdir_r(dp, &entry, &result) == 0) { - // no more entries? - if (!result) { - break; - } - - if (entry.d_type == PX4LOG_REGULAR_FILE) { - if (!memcmp(entry.d_name, "msgs_", 5)) { + while ((result = readdir(dp))) { + if (result->d_type == PX4LOG_REGULAR_FILE) { + if (!memcmp(result->d_name, "msgs_", 5)) { char msg_path[128]; - snprintf(msg_path, sizeof(msg_path), "%s%s", kSDRoot, entry.d_name); + snprintf(msg_path, sizeof(msg_path), "%s%s", kSDRoot, result->d_name); if (unlink(msg_path)) { PX4LOG_WARN("MavlinkLogHandler::_log_request_erase Error deleting %s\n", msg_path); @@ -510,20 +505,15 @@ LogListHelper::_init() } // Scan directory and collect log files - struct dirent entry, *result = nullptr; + struct dirent *result = nullptr; - while (readdir_r(dp, &entry, &result) == 0) { - // no more entries? - if (result == nullptr) { - break; - } - - if (entry.d_type == PX4LOG_DIRECTORY) { + while ((result = readdir(dp))) { + if (result->d_type == PX4LOG_DIRECTORY) { time_t tt = 0; char log_path[128]; - snprintf(log_path, sizeof(log_path), "%s/%s", kLogRoot, entry.d_name); + snprintf(log_path, sizeof(log_path), "%s/%s", kLogRoot, result->d_name); - if (_get_session_date(log_path, entry.d_name, tt)) { + if (_get_session_date(log_path, result->d_name, tt)) { _scan_logs(f, log_path, tt); } } @@ -579,22 +569,17 @@ LogListHelper::_scan_logs(FILE *f, const char *dir, time_t &date) DIR *dp = opendir(dir); if (dp) { - struct dirent entry, *result = nullptr; + struct dirent *result = nullptr; - while (readdir_r(dp, &entry, &result) == 0) { - // no more entries? - if (result == nullptr) { - break; - } - - if (entry.d_type == PX4LOG_REGULAR_FILE) { + while ((result = readdir(dp))) { + if (result->d_type == PX4LOG_REGULAR_FILE) { time_t ldate = date; uint32_t size = 0; char log_file_path[128]; - snprintf(log_file_path, sizeof(log_file_path), "%s/%s", dir, entry.d_name); + snprintf(log_file_path, sizeof(log_file_path), "%s/%s", dir, result->d_name); - if (_get_log_time_size(log_file_path, entry.d_name, ldate, size)) { - //-- Write entry out to list file + if (_get_log_time_size(log_file_path, result->d_name, ldate, size)) { + //-- Write result->out to list file fprintf(f, "%u %u %s\n", (unsigned)ldate, (unsigned)size, log_file_path); log_count++; } @@ -652,17 +637,17 @@ LogListHelper::delete_all(const char *dir) return; } - struct dirent entry, *result = nullptr; + struct dirent *result = nullptr; - while (readdir_r(dp, &entry, &result) == 0) { + while ((result = readdir(dp))) { // no more entries? if (result == nullptr) { break; } - if (entry.d_type == PX4LOG_DIRECTORY && entry.d_name[0] != '.') { + if (result->d_type == PX4LOG_DIRECTORY && result->d_name[0] != '.') { char log_path[128]; - snprintf(log_path, sizeof(log_path), "%s/%s", dir, entry.d_name); + snprintf(log_path, sizeof(log_path), "%s/%s", dir, result->d_name); LogListHelper::delete_all(log_path); if (rmdir(log_path)) { @@ -670,9 +655,9 @@ LogListHelper::delete_all(const char *dir) } } - if (entry.d_type == PX4LOG_REGULAR_FILE) { + if (result->d_type == PX4LOG_REGULAR_FILE) { char log_path[128]; - snprintf(log_path, sizeof(log_path), "%s/%s", dir, entry.d_name); + snprintf(log_path, sizeof(log_path), "%s/%s", dir, result->d_name); if (unlink(log_path)) { PX4LOG_WARN("MavlinkLogHandler::delete_all Error deleting %s\n", log_path);