Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to stop file copying
Forum Updated to NodeBB v4.3 + New Features

How to stop file copying

Scheduled Pinned Locked Moved Unsolved General and Desktop
19 Posts 5 Posters 932 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • JoeCFDJ Offline
    JoeCFDJ Offline
    JoeCFD
    wrote on last edited by
    #1

    It is easy to handle if there are a lot of files. What is the best way to stop in the middle of copying in case of a single large file(for example: a few GB). It may not be a good idea to simply kill the thread for QFile::copy.

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Don't use QFile::copy() then but your own implementation.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      JoeCFDJ 1 Reply Last reply
      4
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by
        #3

        I remember there was a push to separate QFile as QIODevice from QFile as file system manager but I don't know what happened to it, can't even find the ticket anymore

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply
        1
        • Christian EhrlicherC Christian Ehrlicher

          Don't use QFile::copy() then but your own implementation.

          JoeCFDJ Offline
          JoeCFDJ Offline
          JoeCFD
          wrote on last edited by JoeCFD
          #4

          @Christian-Ehrlicher good call.
          Found something which may be helpful for others as well.
          https://stackoverflow.com/questions/10195343/copy-a-file-in-a-sane-safe-and-efficient-way

          VRoninV Christian EhrlicherC 2 Replies Last reply
          0
          • JoeCFDJ JoeCFD

            @Christian-Ehrlicher good call.
            Found something which may be helpful for others as well.
            https://stackoverflow.com/questions/10195343/copy-a-file-in-a-sane-safe-and-efficient-way

            VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by
            #5

            @JoeCFD said in How to stop file copying:

            Found something which may be helpful for others as well.

            std::filesystem::copy is always an option

            @JoeCFD said in How to stop file copying:

            QFile::copy can not even copy large size files. A bit disappointed.

            You can do it with QFile, instead of using the copy method open a file for reading and a file for writing and move them across in chunks

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            JoeCFDJ 2 Replies Last reply
            0
            • VRoninV VRonin

              @JoeCFD said in How to stop file copying:

              Found something which may be helpful for others as well.

              std::filesystem::copy is always an option

              @JoeCFD said in How to stop file copying:

              QFile::copy can not even copy large size files. A bit disappointed.

              You can do it with QFile, instead of using the copy method open a file for reading and a file for writing and move them across in chunks

              JoeCFDJ Offline
              JoeCFDJ Offline
              JoeCFD
              wrote on last edited by JoeCFD
              #6

              @VRonin my os(for production) does not support std::filesystem::copy unluckily and I can not change that. It is kind of lame. It seems I do not have other choices than your suggestion. Thanks.

              1 Reply Last reply
              0
              • JoeCFDJ JoeCFD

                @Christian-Ehrlicher good call.
                Found something which may be helpful for others as well.
                https://stackoverflow.com/questions/10195343/copy-a-file-in-a-sane-safe-and-efficient-way

                Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @JoeCFD said in How to stop file copying:

                QFile::copy can not even copy large size files.

                I don't understand this - why should QFile::copy() can not copy 'large' files ?

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                JoeCFDJ 1 Reply Last reply
                1
                • Christian EhrlicherC Christian Ehrlicher

                  @JoeCFD said in How to stop file copying:

                  QFile::copy can not even copy large size files.

                  I don't understand this - why should QFile::copy() can not copy 'large' files ?

                  JoeCFDJ Offline
                  JoeCFDJ Offline
                  JoeCFD
                  wrote on last edited by
                  #8

                  @Christian-Ehrlicher Sorry. It takes long. I tried a large file and somehow the usb light is off. I thought QFile::copy has problem.

                  1 Reply Last reply
                  0
                  • VRoninV VRonin

                    @JoeCFD said in How to stop file copying:

                    Found something which may be helpful for others as well.

                    std::filesystem::copy is always an option

                    @JoeCFD said in How to stop file copying:

                    QFile::copy can not even copy large size files. A bit disappointed.

                    You can do it with QFile, instead of using the copy method open a file for reading and a file for writing and move them across in chunks

                    JoeCFDJ Offline
                    JoeCFDJ Offline
                    JoeCFD
                    wrote on last edited by
                    #9

                    @VRonin I did this with buffer size. However, QFile close() call takes very long. It turns out that the copied data is cached at first. In closing, QFile flushes the data to the disk. Tried to flush after each buffer write and no success. Stop still does not work. May take a look at how QSaveFile works.

                    Christian EhrlicherC 1 Reply Last reply
                    0
                    • JoeCFDJ JoeCFD

                      @VRonin I did this with buffer size. However, QFile close() call takes very long. It turns out that the copied data is cached at first. In closing, QFile flushes the data to the disk. Tried to flush after each buffer write and no success. Stop still does not work. May take a look at how QSaveFile works.

                      Christian EhrlicherC Offline
                      Christian EhrlicherC Offline
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @JoeCFD said in How to stop file copying:

                      In closing, QFile flushes the data to the disk.

                      It's not QFile but the OS.

                      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                      Visit the Qt Academy at https://academy.qt.io/catalog

                      JoeCFDJ 1 Reply Last reply
                      1
                      • Christian EhrlicherC Christian Ehrlicher

                        @JoeCFD said in How to stop file copying:

                        In closing, QFile flushes the data to the disk.

                        It's not QFile but the OS.

                        JoeCFDJ Offline
                        JoeCFDJ Offline
                        JoeCFD
                        wrote on last edited by
                        #11

                        @Christian-Ehrlicher True. It seems it is not doable on Linux to make a progress bar or stop in the middle of copying when file size is big. Just tested a transfer of a file with size 260MB with rsync. Caching time is less than 1s. But flushing takes more than 1 minute.

                        jsulmJ 1 Reply Last reply
                        0
                        • JoeCFDJ JoeCFD

                          @Christian-Ehrlicher True. It seems it is not doable on Linux to make a progress bar or stop in the middle of copying when file size is big. Just tested a transfer of a file with size 260MB with rsync. Caching time is less than 1s. But flushing takes more than 1 minute.

                          jsulmJ Online
                          jsulmJ Online
                          jsulm
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          @JoeCFD said in How to stop file copying:

                          It seems it is not doable on Linux to make a progress bar

                          It is doable if you implement it manually (instead of using QFile::copy()). I mean: read the original file in blocks and write each block to the new file. Between each block you can update the progress bar.

                          https://forum.qt.io/topic/113070/qt-code-of-conduct

                          JoeCFDJ 1 Reply Last reply
                          2
                          • jsulmJ jsulm

                            @JoeCFD said in How to stop file copying:

                            It seems it is not doable on Linux to make a progress bar

                            It is doable if you implement it manually (instead of using QFile::copy()). I mean: read the original file in blocks and write each block to the new file. Between each block you can update the progress bar.

                            JoeCFDJ Offline
                            JoeCFDJ Offline
                            JoeCFD
                            wrote on last edited by JoeCFD
                            #13

                            @jsulm I did it with some source code in QFile::copy(). No help. Flushing happens when close() is called. This is how copy works on Linux.
                            char block[ 131072 ];
                            qint64 total_read{ 0 };

                                        while ( false == source_file.atEnd() ) {
                                            qint64 read_in = source_file.read( block, sizeof( block ) );
                                            if ( read_in <= 0 ) {
                                                break;
                                            }
                            
                                            total_read += read_in;
                                            if ( read_in != destination_file.write( block, read_in ) ) {
                                                break;
                                            }
                            
                                            value += read_in / 1024.0;
                                            progress_dialog.setValue( static_cast< int >( value ) );
                            
                                            if ( true == progress_dialog.wasCanceled() ) {
                                                break;
                                            }
                                        }
                            
                                        source_file.close();
                                        destination_file.close();
                            
                            Christian EhrlicherC jsulmJ JonBJ 3 Replies Last reply
                            0
                            • JoeCFDJ JoeCFD

                              @jsulm I did it with some source code in QFile::copy(). No help. Flushing happens when close() is called. This is how copy works on Linux.
                              char block[ 131072 ];
                              qint64 total_read{ 0 };

                                          while ( false == source_file.atEnd() ) {
                                              qint64 read_in = source_file.read( block, sizeof( block ) );
                                              if ( read_in <= 0 ) {
                                                  break;
                                              }
                              
                                              total_read += read_in;
                                              if ( read_in != destination_file.write( block, read_in ) ) {
                                                  break;
                                              }
                              
                                              value += read_in / 1024.0;
                                              progress_dialog.setValue( static_cast< int >( value ) );
                              
                                              if ( true == progress_dialog.wasCanceled() ) {
                                                  break;
                                              }
                                          }
                              
                                          source_file.close();
                                          destination_file.close();
                              
                              Christian EhrlicherC Offline
                              Christian EhrlicherC Offline
                              Christian Ehrlicher
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

                              @JoeCFD said in How to stop file copying:

                              This is how copy works on Linux.

                              On every os where the caching is not explicitly disabled (which is not by default) - that's what a cache is for.

                              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                              Visit the Qt Academy at https://academy.qt.io/catalog

                              1 Reply Last reply
                              0
                              • JoeCFDJ JoeCFD

                                @jsulm I did it with some source code in QFile::copy(). No help. Flushing happens when close() is called. This is how copy works on Linux.
                                char block[ 131072 ];
                                qint64 total_read{ 0 };

                                            while ( false == source_file.atEnd() ) {
                                                qint64 read_in = source_file.read( block, sizeof( block ) );
                                                if ( read_in <= 0 ) {
                                                    break;
                                                }
                                
                                                total_read += read_in;
                                                if ( read_in != destination_file.write( block, read_in ) ) {
                                                    break;
                                                }
                                
                                                value += read_in / 1024.0;
                                                progress_dialog.setValue( static_cast< int >( value ) );
                                
                                                if ( true == progress_dialog.wasCanceled() ) {
                                                    break;
                                                }
                                            }
                                
                                            source_file.close();
                                            destination_file.close();
                                
                                jsulmJ Online
                                jsulmJ Online
                                jsulm
                                Lifetime Qt Champion
                                wrote on last edited by
                                #15

                                @JoeCFD said in How to stop file copying:

                                Flushing happens when close() is called

                                I did not say the opposite, I was talking about progress bar...

                                https://forum.qt.io/topic/113070/qt-code-of-conduct

                                JoeCFDJ 1 Reply Last reply
                                0
                                • JoeCFDJ JoeCFD

                                  @jsulm I did it with some source code in QFile::copy(). No help. Flushing happens when close() is called. This is how copy works on Linux.
                                  char block[ 131072 ];
                                  qint64 total_read{ 0 };

                                              while ( false == source_file.atEnd() ) {
                                                  qint64 read_in = source_file.read( block, sizeof( block ) );
                                                  if ( read_in <= 0 ) {
                                                      break;
                                                  }
                                  
                                                  total_read += read_in;
                                                  if ( read_in != destination_file.write( block, read_in ) ) {
                                                      break;
                                                  }
                                  
                                                  value += read_in / 1024.0;
                                                  progress_dialog.setValue( static_cast< int >( value ) );
                                  
                                                  if ( true == progress_dialog.wasCanceled() ) {
                                                      break;
                                                  }
                                              }
                                  
                                              source_file.close();
                                              destination_file.close();
                                  
                                  JonBJ Offline
                                  JonBJ Offline
                                  JonB
                                  wrote on last edited by JonB
                                  #16

                                  @JoeCFD
                                  I don't understand what you are saying. Yes, flushing happens when close() is called. Yes that can take a while. In particular it depends what the device is, and how much buffering is going on in memory. Which you may be able to affect/change, e.g. via some system configuration.

                                  If you are saying you want more time spent during the copying and less time spent at the end of the copying, try using Linux man sync(2). That's what you need to flush. You will probably want to call int syncfs(int fd); on the file descriptor you are copying to. Don't know how/whether you get to that from Qt, have a look through QFile, or you can use level 2 Linux calls to do the whole file copy. The syncfs() call needs to go inside your loop.

                                  You should see a noticeable effect of syncing especially if you use a slow device like a USB stick.

                                  1 Reply Last reply
                                  2
                                  • jsulmJ jsulm

                                    @JoeCFD said in How to stop file copying:

                                    Flushing happens when close() is called

                                    I did not say the opposite, I was talking about progress bar...

                                    JoeCFDJ Offline
                                    JoeCFDJ Offline
                                    JoeCFD
                                    wrote on last edited by
                                    #17

                                    @jsulm If flushing takes more time than write, progress bar display does not make a lot of sense.

                                    JonBJ 1 Reply Last reply
                                    0
                                    • JoeCFDJ JoeCFD

                                      @jsulm If flushing takes more time than write, progress bar display does not make a lot of sense.

                                      JonBJ Offline
                                      JonBJ Offline
                                      JonB
                                      wrote on last edited by JonB
                                      #18

                                      @JoeCFD
                                      That is true, and there are many apps which are guilty of this. How many of them reach 100% fairly quickly and then sit there for ages? I'm looking at you, Microsoft, in particular!

                                      Other than trying to calculate what the residue will be at the end, and allowing for that during your progress, do try the syncing, I think this will give you more accuracy. P.S. Don't do the sync too frequently. If you write one byte at a time and sync between each your operation will be slow! :) Your block is ~128K, that might be fine.

                                      JoeCFDJ 1 Reply Last reply
                                      0
                                      • JonBJ JonB

                                        @JoeCFD
                                        That is true, and there are many apps which are guilty of this. How many of them reach 100% fairly quickly and then sit there for ages? I'm looking at you, Microsoft, in particular!

                                        Other than trying to calculate what the residue will be at the end, and allowing for that during your progress, do try the syncing, I think this will give you more accuracy. P.S. Don't do the sync too frequently. If you write one byte at a time and sync between each your operation will be slow! :) Your block is ~128K, that might be fine.

                                        JoeCFDJ Offline
                                        JoeCFDJ Offline
                                        JoeCFD
                                        wrote on last edited by
                                        #19

                                        @JonB WIll check it out. Thanks.

                                        1 Reply Last reply
                                        0

                                        • Login

                                        • Login or register to search.
                                        • First post
                                          Last post
                                        0
                                        • Categories
                                        • Recent
                                        • Tags
                                        • Popular
                                        • Users
                                        • Groups
                                        • Search
                                        • Get Qt Extensions
                                        • Unsolved