-
Notifications
You must be signed in to change notification settings - Fork 51
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
Replace boost.filesystem #207
Conversation
Create a minimally slim replacement for boost.filesystem for WIN32 and POSIX compliant systems.
CMakeLists.txt
Outdated
@@ -209,6 +210,24 @@ set(IO_SOURCE | |||
src/IO/HDF5/HDF5IOHandler.cpp | |||
src/IO/HDF5/ParallelHDF5IOHandler.cpp) | |||
|
|||
if(WIN32) |
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.
I think we can just do #if defined(_WIN32)
in-code and assume all others are POSIX.
Ref: https://stackoverflow.com/questions/5919996/how-to-detect-reliably-mac-os-x-ios-linux-windows-in-c-preprocessor
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.
Also, I think it's really unsave to set those vars globally and publicly, e.g. in the tests below ;-)
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.
I will push a commit to fix this...
list_directory(std::string const& path ); | ||
|
||
void | ||
create_directories( std::string const& path ); |
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.
docstring: this is mkdir -p
, right?
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 was the intention, to save the user from creating loops or copy-pasting.
create_directories( std::string const& path ); | ||
|
||
void | ||
remove_file( std::string const& path ); |
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.
docstring: this is equivalent to rm
with -r
or -f
or both or none?
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.
As this will be implemented with remove()
, at least -r
.
-f
should probably not be the default with void
- the user should be notified if the target does not exist. We could borrow from std::filesystem::remove()
, return bool
and then make -f
the default.
file_exists( std::string const& path ); | ||
|
||
std::vector< std::string > | ||
list_directory(std::string const& path ); |
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.
docstring: non-recursive, files-only?
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.
Non-recursive, every entry (i.e. directories and files).
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.
Makes sense. Best to just put this info in the doxygen
doc string I guess.
namespace auxiliary | ||
{ | ||
bool | ||
path_exists( std::string const& path ); |
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.
is this directory_exists
?
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.
Yes, it actually checks for a directory. This should be made more explicit.
test/AuxiliaryTest.cpp
Outdated
REQUIRE(path_exists("/home")); | ||
REQUIRE(!path_exists("/nonexistent_folder_in_root_directory")); | ||
#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.
missing end-of-file newline
We support either windows or POSIX systems. Keep the checks of defines local for it.
c32fa93
to
ed84f20
Compare
Nice, |
Knhihihi.
Do not be mistaken:
is a message by our own code: Lines 68 to 70 in b8e8de9
The current failures are probably because |
8bf6353
to
411228e
Compare
Ah ok, I thought it's triggered the other way around. Alright, you can pull again and work on it. |
@C0nsultant I think you should be able to traverse your Unix paths for nested creation of dirs like this: #include <iostream>
#include <sstream>
void iterEm(std::string input)
{
std::istringstream ss(input);
std::string token;
std::string iterPath;
if( input.find("/") == 0 )
iterPath += "/";
while( std::getline( ss, token, '/' ) ) {
if( token.size() > 0 )
iterPath += token + "/";
std::cout << iterPath << std::endl;
}
}
int main()
{
iterEm( "/abc/def/ghi" );
iterEm( "abc/def/ghi" );
iterEm( "abc/def/ghi/" );
} output:
|
Recursively delete directories (like `rm -r`). Recursively create directories (like `mkdir -p`).
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.
I am confused about deletes in cmake dirs ;)
test/AuxiliaryTest.cpp
Outdated
{ | ||
std::string del = "..\\CMakeFiles\\" + *dir_entries.begin(); | ||
while( !file_exists(del) && directory_exists(del) ) | ||
{ |
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.
:-o what are you removing here? xD
test/AuxiliaryTest.cpp
Outdated
dir_entries = list_directory(del); | ||
del += "/" + *dir_entries.begin(); | ||
} | ||
if( file_exists(del) ) |
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.
This looks scary and probably harmful, e.g. for make install.
The idea was to find a file to test deletion on realiably. Maybe we could delete some |
Use CI support script directory rather than CMake build directory
Nooo, don't let us mess with the cake build tree our even worse the source tree :-o
Just use c++ standard file IO to write a new text file for deletion.
|
I will push an update on this in the next hour. |
Create a simple file and remove it again. Avoids touching the CMake tree and source tree.
src/auxiliary/Filesystem.cpp
Outdated
|
||
bool success = true; | ||
#ifdef _WIN32 | ||
success = CreateDirectory(path.c_str(), NULL); |
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.
recursive?
https://stackoverflow.com/a/1530815/2719194
(or same as the solution below for POSIX)
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 or
https://msdn.microsoft.com/en-us/library/bb762130(v=vs.85).aspx
This function creates a file system folder whose fully qualified path is given by pszPath. If one or more of the intermediate folders do not exist, it creates them.
To set security attributes on a new folder, use SHCreateDirectoryEx.
src/auxiliary/Filesystem.cpp
Outdated
throw std::system_error(std::error_code(errno, std::system_category())); | ||
dirent* entry; | ||
while ((entry = readdir(directory)) != nullptr) | ||
if( strncmp(entry->d_name, ".", 1) && strncmp(entry->d_name, "..", 2) ) |
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.
This probably skips hidden folders starting with a .
we could need checks for both strncmp
if the strlen
is larger than 1 or 2 respectively.
Recursively delete directories (like `rm -r`). Recursively create directories (like `mkdir -p`).
src/auxiliary/Filesystem.cpp
Outdated
auto mk = [](std::string const& p) -> bool { return CreateDirectory(p.c_str(), nullptr); }; | ||
#else | ||
char seperator = '/'; | ||
auto mk = [](std::string const& p) -> bool { return (0 == mkdir(p.c_str(), 0777));}; |
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.
Maybe let us take the default, environment umask instead of 0777?
https://stackoverflow.com/questions/10147990/how-to-create-directory-with-right-permissons-using-c-on-posix
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.
This would actually make Windows and POSIX behaviour equivalent. Agreed!
test/AuxiliaryTest.cpp
Outdated
auto entries = list_directory("."); | ||
for( auto const& entry : entries ) | ||
std::cout << entry << std::endl; | ||
//REQUIRE(file_exists("AuxiliaryTests.exe")); |
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.
should these be enabled or removed?
should the output above be on or is it a debug leftover?
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.
These should be enabled, they just failed because I could not figure out the cwd in appveyor.
Also, the above std::cout
loop should be removed again.
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.
For appveyor, this should then be
REQUIRE(file_exists("Release\\AuxiliaryTests.exe"));
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.
Yes, depends if it's a Debug or Release build ("Configuration") in multi-config build systems such as VS that is used as Generator. Maybe just do not check for this exe and check for another file?
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.
Any file whose (relative) path we can reliably determine is fine, really.
Going from appveyor logs, the cwd is C:\projects\openpmd-api\build\bin
.
How about
REQUIRE(file_exists("..\\..\\CMakeLists.txt"));
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.
Yep, this is just by the WORKING_DIRECTORY
I add to the add_test
calls in CMakeLists.txt
. I am surprised it is not bin\Release\
...
Update: Actually REQUIRE(file_exists("AuxiliaryTests.exe"));
should work as is - can you try again?
Checking the CMakeLists.txt
this way is not possible, since it depends on the user-chosen location of the build directory. Let me see which file we can use...
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.
If Python bindings are built, it appears we could check for
REQUIRE(file_exists(".\\2_read_serial.py"));
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.
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.
perfect!
Once both of these minor issues are addressed, we should be ready to merge. 🎆 |
Yay, nice! 🚀 Do you want to push it or shall I do it? |
@ax3l Pull the trigger. |
We are free, freeee! |
Create a minimally slim replacement for boost.filesystem for WIN32 and
POSIX compliant systems.
Closes #70.