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

Thread: Lock a file

  1. #1
    Join Date
    Jun 2006
    Posts
    10
    Thanks
    1

    Default Lock a file

    Hi,

    is there any way I can lock a file with Qt libraries?
    For "lock" I mean that when my app opens the file for writing, no other process can write on it. This happens for many Windows applications working with files.
    I've tried QtLockedFile, but it only works among processes using QtLockedFile to access the file.

    I've tried to call the native Windows API LockFile but it does not work because I can't give it a valid HANDLE to the file (QFile::handle() returns rubbish).

    How can I fix this?

    Thank you
    Giacomo

  2. #2
    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: Lock a file

    Quote Originally Posted by euthymos View Post
    I've tried to call the native Windows API LockFile but it does not work because I can't give it a valid HANDLE to the file (QFile::handle() returns rubbish).
    Did you cast this rubbish to a proper type?
    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
    Jun 2006
    Posts
    10
    Thanks
    1

    Default Re: Lock a file

    Quote Originally Posted by wysota View Post
    Did you cast this rubbish to a proper type?
    Rubbish was not meant to be offensive, but ironic

    However, I casted it to HANDLE, which is declared in windows.h

  4. #4
    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: Lock a file

    I didn't think it was offensive :-) Casting it to HANDLE should have worked. What exactly happens when you try to use the handle?
    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.


  5. #5
    Join Date
    Jun 2006
    Posts
    10
    Thanks
    1

    Default Re: Lock a file

    Quote Originally Posted by wysota View Post
    I didn't think it was offensive :-) Casting it to HANDLE should have worked. What exactly happens when you try to use the handle?
    It could have been offensive towards the developers...

    However, if I cast it to HANDLE and pass it to LockFile(HANDLE, DWORD, DWORD, DWORD, DWORD) Windows API, the function returns false. GetLastError() returns 6, which is "invalid handle" error code.
    I think this leaves no room to other possibilities: the handle is not valid.

  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: Lock a file

    Maybe you need to pass the result through some other function first?
    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
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Lock a file

    newbie reply...
    Win32 CreateFile() to open file in locked mode;
    QFile open again
    QFile close
    CloseFile (or whatever win api).

    i really dont think that will work but just had this tickling in my mid..

  8. #8
    Join Date
    Jun 2006
    Posts
    10
    Thanks
    1

    Default Re: Lock a file

    MrDeath: I'm a novice, too. I've been programming with Windows' APIs for a short time, and then in Java. Now I'm learning C++/Qt.

    That solution might work fine. I think I'll try it when I get back to development machine.
    However, I just can't see why QFile::handle() returns an invalid handle.

    wysota: I don't know if I have to pass it to another function, the docs say nothing about that.

    EDIT: there was a function to call! _get_osfhandle(int fd)
    I've learnt that thanks to Qxt guys. See http://doc.libqxt.org/0.5.0/classQxtFileLock.html
    Last edited by euthymos; 2nd June 2009 at 16:28.

  9. #9
    Join Date
    May 2009
    Posts
    63
    Thanks
    12
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Lock a file

    1. Win32 CreateFile() to open file in locked mode;
    2. QFile open again
    3. QFile close
    4. CloseFile (or whatever win api).

    How can step 2 work? - it is trying to open a locked file.

  10. #10
    Join Date
    Jun 2006
    Posts
    10
    Thanks
    1

    Default Re: Lock a file

    Quote Originally Posted by jonks View Post
    How can step 2 work? - it is trying to open a locked file.
    Actually I don't know... but maybe, because it's the same application and the same thread accessing the file, it can be opened after the lock. Just guessing.

    However, I solved with a great "native" solution inspired by Qxt's "QxtFileLock" class (http://doc.libqxt.org/0.5.0/classQxtFileLock.html). You should check this Qxt thing out, it's really good.

  11. #11
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Lock a file

    Quote Originally Posted by euthymos View Post
    Actually I don't know... but maybe, because it's the same application and the same thread accessing the file, it can be opened after the lock. Just guessing.
    that was what i thought. Did it worked? i am on linux so cant test it.

    Quote Originally Posted by euthymos View Post
    However, I solved with a great "native" solution inspired by Qxt's "QxtFileLock" class (http://doc.libqxt.org/0.5.0/classQxtFileLock.html). You should check this Qxt thing out, it's really good.
    this website is blocked at my office. can u please copy and paste the solution. thx..

  12. #12
    Join Date
    Jun 2006
    Posts
    10
    Thanks
    1

    Default Re: Lock a file

    Quote Originally Posted by MrDeath View Post
    that was what i thought. Did it worked? i am on linux so cant test it.
    Actually, I didn't try it yet.



    Quote Originally Posted by MrDeath View Post
    this website is blocked at my office. can u please copy and paste the solution. thx..
    This is the actual piece of code performing the trick under Windows:
    Qt Code:
    1. bool QxtFileLock::lock()
    2. {
    3. if (file() && file()->isOpen() && !isActive())
    4. {
    5. HANDLE w32FileHandle;
    6. OVERLAPPED ov1;
    7. DWORD dwflags;
    8.  
    9. w32FileHandle = (HANDLE)_get_osfhandle(file()->handle());
    10. if (w32FileHandle == INVALID_HANDLE_VALUE)
    11. return false;
    12.  
    13. switch (qxt_d().mode)
    14. {
    15. case ReadLock:
    16. dwflags = LOCKFILE_FAIL_IMMEDIATELY;
    17. break;
    18.  
    19. case ReadLockWait:
    20. dwflags = 0;
    21. break;
    22.  
    23. case WriteLock:
    24. dwflags = LOCKFILE_EXCLUSIVE_LOCK | LOCKFILE_FAIL_IMMEDIATELY;
    25. break;
    26.  
    27. case WriteLockWait:
    28. dwflags = LOCKFILE_EXCLUSIVE_LOCK;
    29. break;
    30.  
    31. default:
    32. return (false);
    33. }
    34.  
    35. memset(&ov1, 0, sizeof(ov1));
    36. ov1.Offset = qxt_d().offset;
    37.  
    38. if (LockFileEx(w32FileHandle, dwflags, 0, qxt_d().length, 0, &ov1))
    39. {
    40. qxt_d().isLocked = true;
    41. return true;
    42. }
    43. }
    44. return false;
    45. }
    To copy to clipboard, switch view to plain text mode 

    If you ignore the inner class stuff, you see it's just a call to LockFileEx. The HANDLE is get by w32FileHandle = (HANDLE)_get_osfhandle(file()->handle());

    Nice, but still I'm not satisfied. I cannot understand why this lock allows other programs to TRUNCATE the file!!! They cannot change the bytes, but they can still truncate it.
    Is there any effective solution to lock a file in Windows outside CreateFile call?

    It's incredible how Qt programmers has been "lazy" not implementing in QFile some damn decent way to lock the files without performing those hacks. Is a QFile :: open(QString path, OpenMode mode, LockingMode lmode) so impossible?

  13. #13
    Join Date
    Dec 2006
    Posts
    426
    Thanks
    8
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Lock a file

    This becomes platform dependence...

    How about creating an empty file with named such as "myfile.txt.lock", and at any time when you want to write a file, check if myfile.txt.lock exists or not, if exists, then you can wait or bail out...

    The program that creates "myfile.txt.lock" is responsible to delete it when done....

  14. #14
    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: Lock a file

    Quote Originally Posted by euthymos View Post
    It's incredible how Qt programmers has been "lazy" not implementing in QFile some damn decent way to lock the files without performing those hacks. Is a QFile :: open(QString path, OpenMode mode, LockingMode lmode) so impossible?
    It's incredible how people think everyone is just like them - thinks like them, looks like them and in general agrees with them. Windows is the only platform supporting mandatory locks natively. And in general you should avoid mandatory locks, it's a great way to lock out your application.
    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.


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

    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

  16. #16
    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: 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.


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

    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...

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

    Default Re: Lock a file

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

  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: Lock a file

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

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

    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.

Similar Threads

  1. Read binary from file
    By weldpua2008 in forum Newbie
    Replies: 2
    Last Post: 3rd April 2009, 23:50
  2. Can you specify a file engine?
    By skimber in forum Qt Programming
    Replies: 2
    Last Post: 18th September 2008, 15: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, 06:51
  4. qt-3.3.8 fail in scratchbox
    By nass in forum Installation and Deployment
    Replies: 0
    Last Post: 25th May 2007, 15:21
  5. Exclusive file creation (Samba shares)
    By marcel in forum General Programming
    Replies: 1
    Last Post: 13th April 2007, 12: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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.