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

QSaveFile will empty file if no space left on device

    XMLWordPrintable

Details

    • Linux/X11
    • 92373d353 (dev), 09d44fdef (6.9), f3fc37146 (6.8), 510e94264 (tqtc/lts-6.5)

    Description

      Hi, I stumbled on the following bug: 
      When creating and committing a QSaveFile on a device that has no space left, the commit() will return true and empty the file.

      Expected behavior : commit() should return false and keep the original file.

      Tested on Ubuntu 18.04 and 22.04

      Example code with steps to reproduce in linux :

      #include <QCoreApplication>
      #include <QSaveFile>
      #include <QtDebug>
      
      /*
       * Reproduce:
       * - "mkdir -p /tmp/small && sudo mount -t tmpfs -o size=1m swap /tmp/small"
       * - run this program
       * - fill the disk : "dd if=/dev/zero count=10M bs=10 of=/tmp/small/bigfile"
       * - rerun this application;  "/tmp/small/file.conf" will now be empty
       */
      
      int main(int argc, char* argv[])
      {
          QCoreApplication a(argc, argv);
      
          QSaveFile saveFile("/tmp/small/file.conf");
          QByteArray data(1000, 'a');
          saveFile.open(QIODevice::WriteOnly);
          if(saveFile.write(data) != data.size())
          {
              qWarning() << "failed to write to file: " << saveFile.errorString();
              return 1;
          }
      
          if(!saveFile.commit())
          {
              qWarning() << "failed to commit file: " << saveFile.errorString();
          }
      
          return 0;
      } 

      Workaround:

      replace the line "saveFile.commit();" with :

      if(saveFile.flush())
      {
          saveFile.commit();
      }
      else
      {
          qWarning() << "Failed to flush file: " << saveFile.errorString();
      } 

      Attachments

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

        Activity

          People

            thiago Thiago Macieira
            1r0b1n0 Robin Vanhove
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews