Skip to content

Robust File Operations with VAPOR and Qt

Samuel Li edited this page Apr 23, 2018 · 5 revisions

Why do we need robust file operations

Applications like VAPOR has a number of file operations. These operations could be specifying a data set to read, specifying an image file to write, and even taking in a path/file for a future use. Unfortunately, file operations tend to have a good chance to fail. Thus, a good developer would take extra caution to make sure his file operations are robust. Users then don't have to deal with surprises such as "where is my saved image?"

What could go wrong with a file operation

  • If it's a read operation, then all of these steps can go wrong:
    • the specified file/directory does not exist;
    • the specified file/directory has a wrong type (e.g., user selects a file while you're expecting a directory, or vice versa);
    • the specified file/directory is not readable.
    • the specified directory is not executable, thus cannot read into it.
  • If it's a write operation, then the following steps can go wrong:
    • the specified file/directory has a wrong type (e.g., user selects a file while you're expecting a directory, or vice versa);
    • the specified file/directory is not writable.
    • the specified file/directory is not able to be created.
    • the specified file does not have the correct suffix.
  • If it's a read AND write operation (e.g., specifying a session file), then all the steps above are prone to mistakes.

Robust file operations in VAPOR

VAPOR has provided a convenient class, FileOperationChecker, for this purposes. Specifically, four static functions are provided to facilitate examining if a user input is ready for further file operations:

  1. static bool DirectoryGoodToRead( const QString& filename ).
    • This function could be used to, for example, verify the default search path provided by the user.
  2. static bool FileGoodToRead( const QString& filename ).
  3. static bool FileGoodToWrite( const QString& filename ).
  4. static bool FileHasCorrectSuffix( const QString& filename, const QString& expectedSuffix ).
    • This function could be used to verify if the user input has the expected suffix.

Of course, the GUI programmer needs to deal with any failure returned by these functions (e.g., report to the user, roll back to the old value, etc.) Also, FileOperationChecker provides the following function to retrieve the most recent file operation failure message.

  1. static QString GetLastErrorMessage()

What infrastructure does QT provide

QT has an excellent class, QFileInfo class, that tell us everything about a file. It then becomes the developer's responsibility to make sure a file operation does not go wrong. Here is a list of QFileInfo member functions that are particularly useful to examine the pitfalls in What could go wrong with a file operation.

  • bool QFileInfo::exists() const
  • bool QFileInfo::isDir() const
  • bool QFileInfo::isFile() const
  • bool QFileInfo::isReadable() const
  • bool QFileInfo::isWritable() const
  • bool QFileInfo::isExecutable() const
  • QString QFileInfo::suffix() const