Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-9149

Qfile::error() returns an error code inconsistent with QFile::errorString()

    XMLWordPrintable

Details

    • Bug
    • Resolution: Out of scope
    • Not Evaluated
    • None
    • 4.6.2
    • Core: I/O
    • None
    • gentoo linux, x86-64, gcc 4.4.3, Qt 4.6.2

    Description

      QFile has an error() function which will return an error code representing why a given file operation failed. In addition to this it has (inherited from QIODevice) an errorString(). errorString() is returning a QString which accurately describes the error while error() is returning an error code which is far more general (while still accurate). QFile::FileError does have proper values for the various error situations, it is just not returning them.

      For example, the following code:

      #include <QFile>
      #include <QDebug>
      
      QString fileErrorString(QFile::FileError error) {
      	switch(error) {
      	case QFile::NoError:			return "No error occurred.";
      	case QFile::ReadError:			return "An error occurred when reading from the file.";
      	case QFile::WriteError:			return "An error occurred when writing to the file.";
      	case QFile::FatalError:			return "A fatal error occurred.";
      	case QFile::ResourceError:		return "A resource error occured.";
      	case QFile::OpenError:			return "The file could not be opened.";
      	case QFile::AbortError:			return "The operation was aborted.";
      	case QFile::TimeOutError:		return "A timeout occurred.";
      	case QFile::RemoveError:		return "The file could not be removed.";
      	case QFile::RenameError:		return "The file could not be renamed.";
      	case QFile::PositionError:		return "The position in the file could not be changed.";
      	case QFile::ResizeError:		return "The file could not be resized.";
      	case QFile::PermissionsError:	return "The file could not be accessed.";
      	case QFile::CopyError:			return "The file could not be copied.";
      	default:
      	case QFile::UnspecifiedError:	return "An unspecified error occurred.";
      	}
      }
      
      int main() {
      	// a regular user shouldn't be able to open this file...
      	QFile file("/etc/shadow");
      	if(!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
      		qDebug() << file.errorString();
      		qDebug() << fileErrorString(file.error()); // I expect this to print  "The file could not be accessed."
      	}
      }
      

      yields the following (when run as an unprivileged user):

      "Permission denied"
      "The file could not be opened."

      Since QFile::FileError has enough enumerations to be more specific, I would expect it's value to represent the same error as errorString() returns.

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            biochimia João Abecasis
            eteran Evan Teran
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes