-
Notifications
You must be signed in to change notification settings - Fork 9.6k
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
Check if datadir exists: fixes #4364 #4365
base: main
Are you sure you want to change the base?
Conversation
@@ -146,6 +146,10 @@ static void ExtractFontName(const char* filename, std::string* fontname) { | |||
*/ | |||
static void addAvailableLanguages(const std::string &datadir, | |||
std::vector<std::string> *langs) { | |||
if (!std::filesystem::exists(datadir)) { |
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 check is not sufficient. It passes if datadir
exists and is a file (which still raises an exception). I suggest to catch the exception which is thrown by std::filesystem::recursive_directory_iterator
.
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.
touch a.txt
tesseract --list-langs --tessdata-dir a.txt
Error: The directory 'a.txt/' does not exist.
List of available languages in "a.txt/" (0):
@stweil: Can you please provide a scenario / OS where this patch fails?
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 causes that datadir
is always handled as directory.
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.
On macOS:
% touch a.txt
% tesseract --list-langs --tessdata-dir a.txt
libc++abi: terminating due to uncaught exception of type std::__1::__fs::filesystem::filesystem_error: filesystem error: in recursive_directory_iterator: Not a directory ["a.txt/"]
zsh: abort tesseract --list-langs --tessdata-dir a.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.
We don't have main() wrapped into try catch?
Error text is ok, we need only catch std::exception globally.
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.
Fixed that in 0a9df94
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.
@stweil : I tested this PR on linux (g++ 11.3.0) and it does not produce "uncaught exception" (without egorpugin commit). Do you still need to add something like this?
diff --git a/src/api/baseapi.cpp b/src/api/baseapi.cpp
index 667ead67..e684c7fe 100644
--- a/src/api/baseapi.cpp
+++ b/src/api/baseapi.cpp
@@ -146,6 +146,10 @@ static void ExtractFontName(const char* filename, std::string* fontname) {
*/
static void addAvailableLanguages(const std::string &datadir,
std::vector<std::string> *langs) {
+ if (!std::filesystem::is_directory(datadir)) {
+ std::cerr << "Error: '" << datadir << "' is not directory.\n";
+ return;
+ }
if (!std::filesystem::exists(datadir)) {
std::cerr << "Error: The directory '" << datadir << "' does not exist.\n";
return;
Or should be make these checks at ccutils.cpp and fail if directory not 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.
This changes the error messages.
Old
libc++abi: terminating due to uncaught exception of type std::__1::__fs::filesystem::filesystem_error: filesystem error: in recursive_directory_iterator: No such file or directory ["tessdata/"]
libc++abi: terminating due to uncaught exception of type std::__1::__fs::filesystem::filesystem_error: filesystem error: in recursive_directory_iterator: Not a directory ["tessdata/"]
New:
exception: filesystem error: in recursive_directory_iterator: Not a directory ["tessdata/"]
exception: filesystem error: in recursive_directory_iterator: No such file or directory ["tessdata/"]
The new error messages are nicer, but are they user friendly? And do we want to introduce C++ exceptions in the C / C++ API? If the answers are no, we must either catch or avoid the exception in addAvailableLanguages
and print a user friendly error message.
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.
- C API should be removed later.
- We want c++ exceptions in api. Nothing wrong here.
- Errors are fine.
recursive_directory_iterator in addAvailableLanguages crashes if datadir does not exist.