Results 1 to 12 of 12

Thread: detecting that a file is currently being written to...

  1. #1
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    258
    Thanks
    22
    Thanked 19 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default detecting that a file is currently being written to...

    I need to write a programm that monitors a directory, and upon the arrival of a file, opens it, parses the text, does something with it and moves it to another directory afterwards. During testing the code I encountered that sometimes the content read was empty even though there should be some data.

    After a few hours I found out, that the QFileSystemWatcher emits the signal directoryChanged() as soon as a file start being written to the directory. In case I try to open the file at that very moment, the returned QByteArray is empty. If the file has completely been written to disk however, there is no problem .

    How can I work around this? I could wait a few seconds before opening the file but this is an ugly workaround especially since the file may be larger and a few seconds may not suffice in that case ...


    Thanx in advance

    Edit: What I need is a way to make sure no-one else is writing to the file, before I attempt to open it.
    Last edited by momesana; 30th October 2007 at 17:54.

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: detecting that a file is currently being written to...

    Well, you can try to open it for writing and reading - this will fail as long it is being written by someone else.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Jan 2007
    Posts
    177
    Thanks
    8
    Thanked 10 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: detecting that a file is currently being written to...

    you could wait until the file is writable with that:
    Qt Code:
    1. QFileInfo::isWritable ()
    To copy to clipboard, switch view to plain text mode 
    but beware, if its write protected it will end in an infinite loop.
    or you do that for waiting some seconds:
    Qt Code:
    1. public slots:
    2. void fileProcessing(QString&);
    3.  
    4. void MyApp::fileProcessing(QString & filePath)
    5. {
    6. //Do sth with the file
    7. }
    8.  
    9. void MyApp::fileModified(QString & filePath)
    10. {
    11. QTimer::singleShot(5000, this, SLOT(fileProcessing(filePath)));
    12. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by kernel_panic; 31st October 2007 at 09:57.

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    258
    Thanks
    22
    Thanked 19 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: detecting that a file is currently being written to...

    I tried a combination of:
    Qt Code:
    1. /* Open file. return false if open fails. */
    2. if (!input.open(QIODevice::Text | QIODevice::ReadWrite)) {
    3. qDebug() << "couldn't open file ReadWrite" << input.fileName();
    4. return false;
    5. }
    6.  
    7. /* Check the size of the file. If its zero, return */
    8. if (!input.isWritable()) {
    9. qDebug() << "Skipping file" << endl;
    10. return false;
    11. }
    To copy to clipboard, switch view to plain text mode 
    but it doesn't work. That means I am opening the file before the content is written by the copy command and reading out an empty QByteArray half of time (depending on the speed of the Operating system. Sometimes the file is already written when I open the QFile and in that case the file can be read properly). :'(

    I am using Linux btw, though the code is supposed to be run on both environments (windoze,linux). Right now, the only alternative I see is checking the QFileInfo lastModified field to check whether the file is older then 5 minutes or so and then handle the file. Ugly workaround especially since operating on remote files requires syncing the clock of the two machines or keeping a QFileInfo map and refresh and compare the values to see whethere they were changed since the last time checked or not.

  5. #5
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: detecting that a file is currently being written to...

    Hi,

    Is the program that insert the files your?
    Òscar Llarch i Galán

  6. #6
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    258
    Thanks
    22
    Thanked 19 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: detecting that a file is currently being written to...

    Quote Originally Posted by ^NyAw^ View Post
    Hi,

    Is the program that insert the files your?
    The files are written somewhere and copied to the directory I am monitoring. It is a simple copy as far as I know.

    The process is as follows:
    Medical Data is being analysed by a program that then writes the results of the analysis as a PCL File to the mentioned directory. I open the files and if necessary split them into individual files according to a simple pattern. Then I write these resulting files into a directory of its own where it is fetched by another application (a fax application). Then this data is being downloaded by medical offices via modems.

    Consequently it is very very important that nothing gets screwed up or lost during the process.

  7. #7
    Join Date
    Jan 2007
    Posts
    177
    Thanks
    8
    Thanked 10 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: detecting that a file is currently being written to...

    i dont know if it works when other applications are writing data into the file, but try
    Qt Code:
    1. QFile::waitForBytesWritten ( int msecs )
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    258
    Thanks
    22
    Thanked 19 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: detecting that a file is currently being written to...

    Quote Originally Posted by kernel_panic View Post
    i dont know if it works when other applications are writing data into the file, but try
    Qt Code:
    1. QFile::waitForBytesWritten ( int msecs )
    To copy to clipboard, switch view to plain text mode 
    What if file is being just written, a few bytes are there, but the file is not complete yet? This way I would end up with a truncated file.

  9. #9
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: detecting that a file is currently being written to...

    Hi,

    So you don't had wirtten the program that is copying the files, you only wirte a program that is monitoring them. I'm right?

    So you could try to make a bucle that only gets out when the file size is the same at two consecutive iterations, waiting a little time evrey iteration.
    Òscar Llarch i Galán

  10. #10
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    258
    Thanks
    22
    Thanked 19 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Cool Re: detecting that a file is currently being written to...

    Ok, simple and ugly workaround:
    I forget about QFileSystemWatcher and use QTimer. Every five minutes I read the entries in the source directory and save them into a variable but before I do that, I process the entries read by the last timerEvent. The first time the application is launched the list is empty so all I do is saving the entries. Five minutes later I process these entries and save a new entrylist generated by QDir::entryList(). This way every file processed is at least 5 minutes old (unless the file was overwritten after being queued in the list but I think that will not happen) and the I assume the copy process will never take more than five minutes. If it really happens, though, that is an act of fate and the patient should die! :-P

  11. #11
    Join Date
    Jan 2007
    Posts
    177
    Thanks
    8
    Thanked 10 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: detecting that a file is currently being written to...

    uuuuh!Yeah! Very ugly workaround.... it must be possible to do this without timers...
    I looked into the docu and just saw qbuffer. if you use the readyRead signal and do than your file processing, it could workbut im not sure...
    but this version with the timers.... i wouldnt use it for medical apps.... it has to be secure.

  12. #12
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    258
    Thanks
    22
    Thanked 19 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: detecting that a file is currently being written to...

    Yep. Indeed very ugly. I will read about QBuffer when I get to work in half an hour. Thanks

Similar Threads

  1. file renaming on windows
    By jdd81 in forum Qt Programming
    Replies: 9
    Last Post: 2nd October 2007, 19:41
  2. qt-3.3.8 fail in scratchbox
    By nass in forum Installation and Deployment
    Replies: 0
    Last Post: 25th May 2007, 15:21
  3. Sending Binary File with QFTP
    By nbkhwjm in forum Newbie
    Replies: 2
    Last Post: 7th March 2007, 18:10
  4. How To Extract A File
    By deekayt in forum General Programming
    Replies: 7
    Last Post: 5th December 2006, 18:27
  5. Detecting the last empty line in a file
    By Ti_Thom in forum Qt Programming
    Replies: 2
    Last Post: 19th October 2006, 05:14

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.