From 76f3074e8b57495e2b34dad71a00cfbe4155596e Mon Sep 17 00:00:00 2001 From: Eric Toombs Date: Sat, 14 Mar 2009 18:53:00 -0400 Subject: [PATCH] Improved error reporting in poppler-glib. --- glib/poppler-document.cc | 14 +++++++++++--- poppler/PDFDoc.cc | 44 +++++++++++++++++++++++--------------------- poppler/PDFDoc.h | 7 +++++++ 3 files changed, 41 insertions(+), 24 deletions(-) diff --git a/glib/poppler-document.cc b/glib/poppler-document.cc index 387f7d1..3387bec 100644 --- a/glib/poppler-document.cc +++ b/glib/poppler-document.cc @@ -16,6 +16,8 @@ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ +#include + #include #include #include @@ -73,12 +75,18 @@ _poppler_document_new_from_pdfdoc (PDFDoc *newDoc, document = (PopplerDocument *) g_object_new (POPPLER_TYPE_DOCUMENT, NULL, NULL); if (!newDoc->isOk()) { + int fopen_errno; switch (newDoc->getErrorCode()) { case errOpenFile: - g_set_error (error, POPPLER_ERROR, - POPPLER_ERROR_OPEN_FILE, - "Failed to open the PDF file"); + // If there was an error opening the file, count it as a G_FILE_ERROR + // and set the GError parameters accordingly. (this assumes that the + // only way to get an errOpenFile error is if newDoc was created using + // a filename and thus fopen was called, which right now is true. + fopen_errno = newDoc->getFopenErrno(); + g_set_error (error, G_FILE_ERROR, + g_file_error_from_errno (fopen_errno), + strerror(fopen_errno)); break; case errBadCatalog: g_set_error (error, POPPLER_ERROR, diff --git a/poppler/PDFDoc.cc b/poppler/PDFDoc.cc index 1be1261..8292bf8 100644 --- a/poppler/PDFDoc.cc +++ b/poppler/PDFDoc.cc @@ -32,6 +32,7 @@ #include #include +#include #include #include #include @@ -71,7 +72,6 @@ PDFDoc::PDFDoc(GooString *fileNameA, GooString *ownerPassword, GooString *userPassword, void *guiDataA) { Object obj; - GooString *fileName1, *fileName2; ok = gFalse; errCode = errNone; @@ -87,33 +87,35 @@ PDFDoc::PDFDoc(GooString *fileNameA, GooString *ownerPassword, #endif fileName = fileNameA; - fileName1 = fileName; - // try to open file - fileName2 = NULL; + { //scope for temporary variables having to do with the loop. + unsigned trial; + GooString *fn = fileName->copy(); //a modifiable copy of fileName + for (trial = 1; trial <= 3; trial++) { #ifdef VMS - if (!(file = fopen(fileName1->getCString(), "rb", "ctx=stm"))) { - error(-1, "Couldn't open file '%s'", fileName1->getCString()); - errCode = errOpenFile; - return; - } + file = fopen(fn->getCString(), "rb", "ctx=stm"); #else - if (!(file = fopen(fileName1->getCString(), "rb"))) { - fileName2 = fileName->copy(); - fileName2->lowerCase(); - if (!(file = fopen(fileName2->getCString(), "rb"))) { - fileName2->upperCase(); - if (!(file = fopen(fileName2->getCString(), "rb"))) { - error(-1, "Couldn't open file '%s'", fileName->getCString()); - delete fileName2; - errCode = errOpenFile; - return; + file = fopen(fn->getCString(), "rb"); +#endif + if (file != NULL) + break; + if (errno != ENOENT || trial == 3) { + error(-1, "Couldn't open file '%s': %s.", fn->getCString(), + strerror(errno)); + errCode = errOpenFile; + // Keep a copy of the errno returned by fopen so that it can be + // referred to later. + fopenErrno = errno; + return; } + if (trial == 1) + fn->lowerCase(); + if (trial == 2) + fn->upperCase(); } - delete fileName2; + delete fn; } -#endif // create stream obj.initNull(); diff --git a/poppler/PDFDoc.h b/poppler/PDFDoc.h index 5bc1953..46d8424 100644 --- a/poppler/PDFDoc.h +++ b/poppler/PDFDoc.h @@ -77,6 +77,10 @@ public: // Get the error code (if isOk() returns false). int getErrorCode() { return errCode; } + // Get the error code returned by fopen() (if getErrorCode() == + // errOpenFile). + int getFopenErrno() { return fopenErrno; } + // Get file name. GooString *getFileName() { return fileName; } @@ -238,6 +242,9 @@ private: GBool ok; int errCode; + //If there is an error opening the PDF file with fopen() in the constructor, + //then the POSIX errno will be here. + int fopenErrno; }; #endif -- 1.6.0.6