[poppler] glib/poppler-document.cc poppler/PDFDoc.cc poppler/PDFDoc.h

Albert Astals Cid aacid at kemper.freedesktop.org
Sun Mar 22 14:51:06 PDT 2009


 glib/poppler-document.cc |   14 +++++++++---
 poppler/PDFDoc.cc        |   54 +++++++++++++++++++++++++++--------------------
 poppler/PDFDoc.h         |    8 ++++++
 3 files changed, 51 insertions(+), 25 deletions(-)

New commits:
commit 441a9cd56935bfe2d8fddc5d3bc2c0104aeffaca
Author: Eric Toombs <ewtoombs at uwaterloo.ca>
Date:   Sun Mar 22 22:50:14 2009 +0100

    Improved error reporting of ErrOpenFile errors
    
    See bug #20660 for more information

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 <string.h>
+
 #include <goo/GooList.h>
 #include <splash/SplashBitmap.h>
 #include <GlobalParams.h>
@@ -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..e83652c 100644
--- a/poppler/PDFDoc.cc
+++ b/poppler/PDFDoc.cc
@@ -18,6 +18,7 @@
 // Copyright (C) 2008 Julien Rebetez <julienr at svn.gnome.org>
 // Copyright (C) 2008 Pino Toscano <pino at kde.org>
 // Copyright (C) 2008 Carlos Garcia Campos <carlosgc at gnome.org>
+// Copyright (C) 2009 Eric Toombs <ewtoombs at uwaterloo.ca>
 //
 // To see a description of the changes please see the Changelog file that
 // came with your tarball or type make ChangeLog if you are building from git
@@ -32,6 +33,7 @@
 
 #include <locale.h>
 #include <stdio.h>
+#include <errno.h>
 #include <stdlib.h>
 #include <stddef.h>
 #include <string.h>
@@ -71,7 +73,6 @@
 PDFDoc::PDFDoc(GooString *fileNameA, GooString *ownerPassword,
 	       GooString *userPassword, void *guiDataA) {
   Object obj;
-  GooString *fileName1, *fileName2;
 
   ok = gFalse;
   errCode = errNone;
@@ -87,33 +88,42 @@ PDFDoc::PDFDoc(GooString *fileNameA, GooString *ownerPassword,
 #endif
 
   fileName = fileNameA;
-  fileName1 = fileName;
-
 
   // try to open file
-  fileName2 = NULL;
+  GooString *fn = fileName->copy(); //a modifiable copy of fileName
+  for (int 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)
+      // fopen() has succeeded!
+      break;
+
+    // fopen() has failed.
+    if (errno != ENOENT || trial == 3) {
+      /*
+       * Either an error has occurred other than "No such file or 
+       * directory", or we are on trial 3 and we are out of alternative file 
+       * names.
+       */
+      error(-1, "Couldn't open file '%s': %s.", fileName->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;
     }
-    delete fileName2;
+
+    // fn wasn't found.
+    if (trial == 1)
+      fn->lowerCase();
+    else //if (trial == 2) implicit; 3 and 1 have already been checked for.
+      fn->upperCase();
   }
-#endif
+  delete fn;
 
   // create stream
   obj.initNull();
diff --git a/poppler/PDFDoc.h b/poppler/PDFDoc.h
index 5bc1953..94229b7 100644
--- a/poppler/PDFDoc.h
+++ b/poppler/PDFDoc.h
@@ -18,6 +18,7 @@
 // Copyright (C) 2008 Julien Rebetez <julienr at svn.gnome.org>
 // Copyright (C) 2008 Pino Toscano <pino at kde.org>
 // Copyright (C) 2008 Carlos Garcia Campos <carlosgc at gnome.org>
+// Copyright (C) 2009 Eric Toombs <ewtoombs at uwaterloo.ca>
 //
 // To see a description of the changes please see the Changelog file that
 // came with your tarball or type make ChangeLog if you are building from git
@@ -77,6 +78,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 +243,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


More information about the poppler mailing list