-
Notifications
You must be signed in to change notification settings - Fork 102
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add rcutils_list_directory function #249
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,7 +32,9 @@ extern "C" | |
#endif // _WIN32 | ||
|
||
#include "rcutils/format_string.h" | ||
#include "rcutils/logging_macros.h" | ||
#include "rcutils/repl_str.h" | ||
#include "rcutils/strdup.h" | ||
|
||
#ifdef _WIN32 | ||
# define RCUTILS_PATH_DELIMITER "\\" | ||
|
@@ -225,11 +227,11 @@ rcutils_calculate_directory_size(const char * directory_path, rcutils_allocator_ | |
char * path = rcutils_join_path(directory_path, "*", allocator); | ||
WIN32_FIND_DATA data; | ||
HANDLE handle = FindFirstFile(path, &data); | ||
allocator.deallocate(path, allocator.state); | ||
if (INVALID_HANDLE_VALUE == handle) { | ||
fprintf(stderr, "Can't open directory %s. Error code: %lu\n", directory_path, GetLastError()); | ||
return dir_size; | ||
} | ||
allocator.deallocate(path, allocator.state); | ||
if (handle != INVALID_HANDLE_VALUE) { | ||
do { | ||
// Skip over local folder handle (`.`) and parent folder (`..`) | ||
|
@@ -275,6 +277,91 @@ rcutils_get_file_size(const char * file_path) | |
return rc == 0 ? (size_t)(stat_buffer.st_size) : 0; | ||
} | ||
|
||
rcutils_ret_t | ||
rcutils_list_directory( | ||
const char * directory_path, | ||
rcutils_allocator_t allocator, | ||
rcutils_string_array_t * string_array) | ||
{ | ||
RCUTILS_CHECK_FOR_NULL_WITH_MSG( | ||
directory_path, "directory_path is null", return RCUTILS_RET_INVALID_ARGUMENT); | ||
RCUTILS_CHECK_FOR_NULL_WITH_MSG( | ||
string_array, "string_array is null", return RCUTILS_RET_INVALID_ARGUMENT); | ||
|
||
// Start with 8 entries | ||
rcutils_ret_t ret = rcutils_string_array_init(string_array, 8, &allocator); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would do check size 1st in the directory, and the allocate array what we need and store the data. this reduces array adjustment operation. I think this is cost effective compared to allocate/de-allocate. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think my biggest problem with that is that it introduces a race with the filesystem. Entries could be removed or added during or after the initial enumeration and then our size would be wrong. To handle this, we'd essentially need to have all the code already here, plus the initial enumeration to give us the size hint. I'm also not convinced that the performance would be better, but I could be convinced by some data on the topic. |
||
if (RCUTILS_RET_OK != ret) { | ||
return ret; | ||
} | ||
|
||
size_t count = 0; | ||
|
||
#ifdef _WIN32 | ||
char * path = rcutils_join_path(directory_path, "*", allocator); | ||
WIN32_FIND_DATA data; | ||
HANDLE handle = FindFirstFile(path, &data); | ||
allocator.deallocate(path, allocator.state); | ||
if (INVALID_HANDLE_VALUE == handle) { | ||
fprintf(stderr, "Can't open directory %s. Error code: %lu\n", directory_path, GetLastError()); | ||
ret = RCUTILS_RET_ERROR; | ||
goto fail; | ||
} | ||
if (handle != INVALID_HANDLE_VALUE) { | ||
do { | ||
if (count >= string_array->size) { | ||
ret = rcutils_string_array_resize(string_array, count * 2); | ||
if (RCUTILS_RET_OK != ret) { | ||
goto fail; | ||
} | ||
} | ||
|
||
string_array->data[count] = rcutils_strdup(data.cFileName, allocator); | ||
if (NULL == string_array->data[count]) { | ||
goto fail; | ||
} | ||
} while (++count, FindNextFile(handle, &data)); | ||
FindClose(handle); | ||
} | ||
#else | ||
DIR * dir = opendir(directory_path); | ||
if (NULL == dir) { | ||
fprintf(stderr, "Can't open directory %s. Error code: %d\n", directory_path, errno); | ||
ret = RCUTILS_RET_ERROR; | ||
goto fail; | ||
} | ||
struct dirent * entry; | ||
for (; NULL != (entry = readdir(dir)); ++count) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. counting There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I felt that we can leave it up to the consumer whether they want to ignore those entries rather than performing the comparisons here. The API proposed in #323 mentions this behavior in the API docs. |
||
if (count >= string_array->size) { | ||
ret = rcutils_string_array_resize(string_array, count * 2); | ||
if (RCUTILS_RET_OK != ret) { | ||
goto fail; | ||
} | ||
} | ||
|
||
string_array->data[count] = rcutils_strdup(entry->d_name, allocator); | ||
if (NULL == string_array->data[count]) { | ||
goto fail; | ||
} | ||
} | ||
closedir(dir); | ||
#endif | ||
|
||
// Shrink the array back down | ||
if (count != string_array->size) { | ||
ret = rcutils_string_array_resize(string_array, count); | ||
if (RCUTILS_RET_OK == ret) { | ||
return RCUTILS_RET_OK; | ||
} | ||
} | ||
|
||
fail: | ||
if (RCUTILS_RET_OK != rcutils_string_array_fini(string_array)) { | ||
RCUTILS_LOG_ERROR( | ||
"failed to clean up on error (leaking memory): '%s'", rcutils_get_error_string().str); | ||
} | ||
return ret; | ||
} | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rcutils_string_array_init also does this check.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's fair. To be pedantic, the argument name could be different in that function so the error message might not be correct, but it might be worth the performance since this is likely to be a rare error case.