Page 1 of 2 12 LastLast
Results 1 to 20 of 37

Thread: QFile::copy copies only 3kb

  1. #1
    Join Date
    Mar 2011
    Posts
    53
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QFile::copy copies only 3kb

    Hi I've noticed a very strange problem a use QFileSystemWatcher to monitor one folder, when new files are copied to this folder the signal directoryChanged is emitted. I wrote a slot, which works well, it gives me the confidence that when ALL files are copied to the folder only then the right action is taken:

    Qt Code:
    1. void DServer::newFiles_()
    2. {
    3. QDir input(QDir::homePath() + "/INPUT DICOM");
    4.  
    5. if(input.count() > lastInput)
    6. {
    7. QTextStream(stdout)<<"not all files" << lastInput <<endl;
    8. lastInput = input.count();
    9. return;
    10. }
    11. else
    12. {
    13. QDir inputDir(QDir::homePath() + "/INPUT DICOM");
    14. QTextStream(stdout) << "BEFORE IF "<<inputDir.count() <<endl;
    15. if(inputDir.count() > 1)
    16. {
    17. QTextStream(stdout)<<"Got all files"<< lastInput <<endl;
    18. inputFiles = getPaths(QDir::homePath() + "/INPUT DICOM");
    19. emit this->_moveFiless();
    20. lastPatientName.clear();
    21. lastInput = 1;
    22. }
    23. else
    24. return;
    25. }
    26. }
    To copy to clipboard, switch view to plain text mode 

    However this slot
    Qt Code:
    1. void DServer::moveFiless_()
    2. {
    3. QString input("/INPUT DICOM");
    4. QDate date = QDate::currentDate();
    5. QTime time = QTime::currentTime();
    6.  
    7. for(int i=0; i<inputFiles.count(); i++)
    8. {
    9. QString old_input_list = inputFiles[i];
    10. inputFiles[i].remove(0,(QDir::homePath().count()) + input.count());
    11.  
    12. int length = inputFiles[i].length();
    13. int startindex = inputFiles[i].lastIndexOf("/",-1) + 1;
    14. QString dir_path = inputFiles[i];
    15.  
    16. dir_path.remove(startindex, length);
    17.  
    18. QDir dir;
    19. if(dir.mkpath(QDir::homePath() + "/DICOM STORAGE/" + date.toString("dd.MM.yyyy") + "/" + time.toString("hh:mm:ss") + dir_path) == true)
    20. {
    21. //anonymize here
    22. anonymous("/" + date.toString("dd.MM.yyyy") + "/" + time.toString("hh:mm:ss") + inputFiles[i], old_input_list);
    23. dir.mkpath(QDir::homePath() + "/TEMPORARY STORAGE/" + date.toString("dd.MM.yyyy") + "/" + time.toString("hh:mm:ss") + dir_path);
    24. date.toString("dd.MM.yyyy") + "/" + time.toString("hh:mm:ss") + input_list[i] + ".tmp");
    25.  
    26. QFile::copy(old_input_list, (QDir::homePath() + "/TEMPORARY STORAGE/" + date.toString("dd.MM.yyyy") + "/" + time.toString("hh:mm:ss") + inputFiles[i] + ".tmp"));
    27. QFile::copy(old_input_list, (QDir::homePath() + "/DICOM STORAGE/" + date.toString("dd.MM.yyyy") + "/" + time.toString("hh:mm:ss") + inputFiles[i]));
    28. QFile::remove(old_input_list);
    29. QTextStream(stdout)<<"source file: "<< old_input_list <<endl;
    30. dir.rmpath(QDir::homePath() + "/INPUT DICOM" + dir_path);
    31. }
    32. else
    33. {
    34. QTextStream(stdout) <<"Couldn't make this dir: ' "<< QDir::homePath() + "/DICOM STORAGE" + dir_path <<endl;
    35. }
    36. }
    37. }
    To copy to clipboard, switch view to plain text mode 
    doesn't work well, the files from source are copied but they only have from 2.6-3.0 kB, when the original ones have about 80kB, that means the QFile::copy works but not good.

  2. #2
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QFile::copy copies only 3kb

    Just reread your post..

    Is "if(input.count() > lastInput)" really correct? Don't you expect the input.count() to increase until all files have been copied?

    In that case you have to keep waiting while it is below the threshold lastinput.

    Joh

  3. #3
    Join Date
    Mar 2011
    Posts
    53
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QFile::copy copies only 3kb

    But the signal directoryChanged() is emitted many times during copying the files to the monitored folder and as far as I know I'm interested in the last one becouse it indicates that the last file occured in the watched folder and after that I am performing the copy action. if(input.count > lastinput) informs if current number of files in the monitored directory is greater then it was during the last run of this slot, so that means the files are still being copied to the folder.

    I tried even sleep for 5 seconds to be sure that all files are in the directory but still all files are copied but only 2.8kB of them.
    Last edited by camol; 8th April 2011 at 13:02.

  4. #4
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QFile::copy copies only 3kb

    Ah! Sorry about that. Now I get it. But it wont work like that, because QFileSystemWatcher fires also when a file is modified. Thus during the copy process your method will be executed multiple times for each file. The watcher supresses to frequent updates but nonetheless: You will get updates where the filecount has not changed but only the file has been modified (a few additional bytes have been copied). Thus you think the file-list is already copied completely and make a copy of incomplete files.

    Joh

  5. #5
    Join Date
    Mar 2011
    Posts
    53
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QFile::copy copies only 3kb

    Do you have any idea to solve that? I really need this functionality which I have described above

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QFile::copy copies only 3kb

    Connect a timer to the file watcher and reset the timer whenever you receive a signal from the watcher. At some point you will stop receiving signals and the timer will timeout. At this point you will know that for some time there haven't been any updates to the file. This is not foul proof but should be enough for you.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Mar 2011
    Posts
    53
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QFile::copy copies only 3kb

    Can you bring some example code of this becouse I am trying to imagine the situation but I can't figure how it should work this. Big thanks in advance for your time here for both of you.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QFile::copy copies only 3kb

    Qt Code:
    1. void Class::setup() {
    2. timer.setSingleShot(true);
    3. timer.setInterval(1000); // 1s
    4. connect(...);
    5. }
    6.  
    7. void Class::onSignalFromFileSystemWatcher() {
    8. timer.start();
    9. }
    10.  
    11. void Class::onTimerTimeout() {
    12. QFile::copy(...);
    13. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Mar 2011
    Posts
    53
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QFile::copy copies only 3kb

    It didn't work still is copied only about 3kB of each file, and also this solution makes sth that I wanted to prevent my application from. Becouse I copy the files from monitored folder to folder created which name is the current time and now for example if I put many files in the monitored folder the whole operation lasts long and the result of that is 2-3 folders with names with different times. My code above didn't do this it only created one folder for each operation of putting files to the monitored folder

  10. #10
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QFile::copy copies only 3kb

    I would suggest, you drop this. And provide the user with a simple button, where he can trigger an import after having copied all files.

    Your concept of monitoring will fail because of so many things. How can you be sure, that the user will not copy files in several packets? That the source medium won't take 10secs to spin up. That the user realizes he has got the wrong files...

    What is this for anyway?

    Joh

  11. #11
    Join Date
    Mar 2011
    Posts
    53
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QFile::copy copies only 3kb

    I am building a server aplication which runs and stays on all the time. It stores files in hierachy of folders thats why when a user brings new files for the server the server is automaticaly archiving them in the right order without any other actions from the user becouse it is an non-gui application. That it is why I need it so much.

    I've tried this at the beginning of the slot connected to directoryChanged:
    this->inputWatcher->blockSignals(true); //to prevent more the one signal
    sleep(40); //to make sure that all files are in the watched folder


    but still it doesn't work


    I don't get it I tried even to make this functionality with a timer but it still the same
    Last edited by camol; 9th April 2011 at 17:37.

  12. #12
    Join Date
    Mar 2011
    Posts
    53
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QFile::copy copies only 3kb

    Does anybody know what is going on? I really need this.

  13. #13
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: QFile::copy copies only 3kb

    Try simplifying your code dramatically so it only copies a single file. Or just write a small test program to do that, without the timers and other drivers. See if that works. Then build up to what you have now.

    I'd also examine the path strings you're constructing, although it sounds like those probably work if the files are produced at all.

    Reworking the copy routine in plain vanilla C++ is another alternative that would involve less than a dozen lines of code.

  14. #14
    Join Date
    Mar 2011
    Posts
    53
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QFile::copy copies only 3kb

    I think file by file won't work also becouse I've tried putting files in folders and in subfolders etc, and the signal directory change wasn't emited as many times, and also doing file by file will create many time name folders like I described above. Strings of paths are done ok becouse the files are succesfully copied to the destination folder in right order but only the size of copied files is invalid.

  15. #15
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: QFile::copy copies only 3kb

    The point is to break your procedure down into small, testable pieces and verify that all of the individual bits are working as expected. You could also try replacing the QFile.copy() call with a system call to your OS's 'cp' or 'mv' command, with the same intention. I'd also try printing out every filename as your innermost loop is executed; it probably won't reveal any flaws, but it costs you nothing to try and it might illuminate something.

  16. #16
    Join Date
    Mar 2011
    Posts
    53
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QFile::copy copies only 3kb

    Still the same I've tried the system cp but the result was the same. Even many different combinations of sleep and blocking the signal from QFileSystemWatcher also didn't help I'm doomed I really need this functionality ;(

  17. #17
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QFile::copy copies only 3kb

    To me it seems you are just starting the copy operation too early. You get correct timestamps but wrong behaviour. Use your filesystem api (QFileInfo::created() and friends) to determine the time but copy the file when it is not being written to anymore.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  18. #18
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QFile::copy copies only 3kb

    Assuming the user copies files only by one thread, the following would be a brute force solution to this problem:

    The idea is to find out which file has actually been added from the current call to the last one. If one has been added, you can safely copy all others.

    To do this, I suggest you store the list of files at the end of each call. When the current file count increased compared to that previously stored file list, you know, that all files of the previous file list are finished writing and can be copied. Maintain a list of already copied files to, to easily find out, which ones you actually need to copy at each step.

    Joh
    Last edited by JohannesMunk; 10th April 2011 at 21:09. Reason: updated contents

  19. #19
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QFile::copy copies only 3kb

    Quote Originally Posted by camol View Post
    Still the same I've tried the system cp but the result was the same. Even many different combinations of sleep and blocking the signal from QFileSystemWatcher also didn't help I'm doomed I really need this functionality ;(
    Blocking the signal? Surely you want all of the signals so you know when the directory/file has finished being updated so you can perform your operations after the last access? (after a set delay)

  20. #20
    Join Date
    Mar 2011
    Posts
    53
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QFile::copy copies only 3kb

    I thought that if I use the code I wrote above and when I get into if condition with lastInput then I should block the signals from watcher in order to be sure that that signals won't be emmited when I am coping and deleting the source file. But it didnt' give the result

Similar Threads

  1. QFile &QFile::operator= is private
    By Fallen_ in forum Newbie
    Replies: 1
    Last Post: 15th March 2011, 15:08
  2. QTemporaryFile and Qfile::copy()
    By ChrisW67 in forum Newbie
    Replies: 2
    Last Post: 23rd April 2009, 23:32
  3. QPrintDialog: settings number of copies
    By nedlab in forum Qt Programming
    Replies: 1
    Last Post: 2nd December 2008, 19:22
  4. copies file doesn't work well
    By dreamer in forum Qt Programming
    Replies: 3
    Last Post: 10th May 2008, 22:00
  5. How many copies of Signal parameters
    By Doug Broadwell in forum Newbie
    Replies: 3
    Last Post: 2nd December 2006, 21:34

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.