Results 1 to 20 of 23

Thread: Lock a file

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    May 2009
    Posts
    61
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    5
    Thanked 6 Times in 6 Posts

    Default Re: Lock a file

    How would you avoid the reading of a file which currentely is written by another program? Let's say something like the iTunes database file, about 10MB plain text?
    When you are working on win32, you can't simply create a tmp file, write it down, and then rename it. First, the file system of win32 seems to be too slow to realize, that a file is removed and you really fast get an "file exists" error when renaming the tmp file to the original name. Second, you'll get a small time span when the file does not exist as the old content is backuped and the new content is not yet renamed. Does that fact matter?

    But I agree with you, both (wysota and euthmos). If I had mandatory locks, life would be much easier. But to say, that the Trolls are too lazy to implement them is the wrong way

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Lock a file

    Quote Originally Posted by auba View Post
    How would you avoid the reading of a file which currentely is written by another program?
    I would use proper IPC mechanisms on local filesystems. On remote filesystems there is nothing you can do, even on Windows they don't support locking.
    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.


  3. #3
    Join Date
    May 2009
    Posts
    61
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    5
    Thanked 6 Times in 6 Posts

    Default Re: Lock a file

    Well... with qt3.x we patched qfile_win.cpp to allow locking by sopen - and it even works on network drives. But I don't want to do that in the future...

  4. #4
    Join Date
    Jul 2010
    Posts
    30
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    6
    Thanked 2 Times in 2 Posts

    Default Re: Lock a file

    What is the conclusion?
    Can I lock the file until my thread finish writing on it? and how?

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

    Default Re: Lock a file

    Sure, but the solution can be platform dependant depending on HOW you want to lock the file.

  6. #6
    Join Date
    Jul 2010
    Posts
    30
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    6
    Thanked 2 Times in 2 Posts

    Default Re: Lock a file

    I work on Linux.
    I need to lock the file until my thread finish writing on it. Then unlock it.

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

    Default Re: Lock a file

    You still have not defined HOW you want to lock it.

    For example, do you want to lock it to prevent your other threads from writing to the same file? Or writing and reading? Or prevent your other thread from even opening the file? Or prevent any application in the entire system from opening the file? Or perhaps allowing some apps to read the file but not write to it? etc...

  8. #8
    Join Date
    Jul 2010
    Posts
    30
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    6
    Thanked 2 Times in 2 Posts

    Default Re: Lock a file

    do you want to lock it to prevent your other threads from writing to the same file?
    Yes , this what I want to lock it for.
    I am sorry if I was not clear.

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Lock a file

    On Unix systems there is no mandatory locking which means in a general case you cannot prevent someone else from writing to the same file while you have it open. It requires cooperation of all involved sides and use of advisory locking (e.g. using flock). Some filesystems may implement mandatory locking (through fcntl calls) but others will not and on some systems (like Linux) mandatory locks are unreliable (see the man page for fcntl).
    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.


  10. The following user says thank you to wysota for this useful post:

    Malek (29th December 2010)

Similar Threads

  1. Read binary from file
    By weldpua2008 in forum Newbie
    Replies: 2
    Last Post: 4th April 2009, 00:50
  2. Can you specify a file engine?
    By skimber in forum Qt Programming
    Replies: 2
    Last Post: 18th September 2008, 16:54
  3. Set up the Qt4.3.2 with Visual Studio 2005
    By lamoda in forum Installation and Deployment
    Replies: 6
    Last Post: 30th January 2008, 07:51
  4. qt-3.3.8 fail in scratchbox
    By nass in forum Installation and Deployment
    Replies: 0
    Last Post: 25th May 2007, 16:21
  5. Exclusive file creation (Samba shares)
    By marcel in forum General Programming
    Replies: 1
    Last Post: 13th April 2007, 13:26

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
  •  
Qt is a trademark of The Qt Company.