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

Thread: Restrict user to open the same file

  1. #1
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Restrict user to open the same file

    Hi all,
    How can I restrict a user to open the same file on multiple instances of the same application???? If you have word installed, then open multiple instances of it and try opening the same file on all the application...The user is restricted. How can I achieve this???

    Thanks

  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: Restrict user to open the same file

    It's called mandatory locking. AFAIK Windows uses it by default. It's not advised though. You can also use advisory locks. For example see "man 2 flock" or simmilar.

  3. #3
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Restrict user to open the same file

    Quote Originally Posted by wysota View Post
    It's called mandatory locking. AFAIK Windows uses it by default. It's not advised though. You can also use advisory locks. For example see "man 2 flock" or simmilar.
    Hi wysota,
    Thanks for your reply.

    I am working on an application where I can open multiple instances of same aplication (Qt). Presently the user is able to open the same file in any instances. The files are in text format. Can you provide me with some resources or something that can help me.

    The solution should be portable.
    Waiting eagerly

  4. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Restrict user to open the same file

    Try searching MSDN.

  5. #5
    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: Restrict user to open the same file

    Or use flock() or lockf().

  6. #6
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Restrict user to open the same file

    Quote Originally Posted by wysota View Post
    Or use flock() or lockf().
    These are perfect as long as you don't use them on samba shares ( possibly other types of shares ).

  7. #7
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Restrict user to open the same file

    Quote Originally Posted by wysota View Post
    Or use flock() or lockf().
    wysota-->
    Does flock works on windows??? I'm googling but just get links telling flock on linux...etc etc

    marcel-->msdn doesnt show me anything related to file locking. It shows stuffs related to locking buffers, locking controls etc etc

    Thanks

  8. #8
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Restrict user to open the same file


  9. The following user says thank you to marcel for this useful post:

    vermarajeev (16th May 2007)

  10. #9
    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: Restrict user to open the same file

    Quote Originally Posted by marcel View Post
    These are perfect as long as you don't use them on samba shares ( possibly other types of shares ).
    This is in general true for all locks, not only the ones mentioned here. Locks should in general be avoided if it's possible.

  11. #10
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Restrict user to open the same file

    Hi Marcel,
    The link only uses FILE* to lock and unlock the file. Is there a way by which I can use fstream or ifstream to lock a file. I read some basics to know what is locking about and have basic idea now. I tried out the solution but is not useful to me. My application uses ifstream to open and read the file. Since the application is developed using Qt, I'm looking for a portable solution.

    Wysota-->
    I too tried flock() and lockf(), and came to the conclusion that it works only on linux....Can you suggest some solution which runs both on windows and linux.

    By the way dont QFile or QIODevice provide functionality to lock and unlock the files...Or isnt there any means I can use Qt to do that stuff.

    Thanks

  12. #11
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Restrict user to open the same file

    You cannot do that with Qt and there is no portable solution. If you want something portable, do it yourself, using platform dependent API.

    I have recently done something similar at work, but unfortunately I also needed to lock files on samba shares, therefore the platform API functions were of no use ( on Mac and Win ) and I did a custom locking mechanism.

    If you only need to lock local files, go ahead and use system API.
    It is not necessary to have #ifdef's all over your app, just where you implement the functionality - for example the function(s) will be called the same and will take the same number of params both on Win and Linux.

    Regards

  13. #12
    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: Restrict user to open the same file

    You can get a FILE* from a C++ stream, you know... And then both flock and _lock_file will work.

  14. #13
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Restrict user to open the same file

    Quote Originally Posted by wysota View Post
    You can get a FILE* from a C++ stream, you know... And then both flock and _lock_file will work.
    While just implementing locking mechanism, I got one more idea.

    Since the locking is to be restricted for the same application I can (I think)achieve this simply by associating an integer variable for each opened file, or just one array(or other appropiate data structure) for all opened files. for this I can make use of QStringList. If the same file is opened twice check in QStringList to see if file already present if so display some message to user "File already opened" if not open the file.

    Will it not be feasible to implement the above said logic.
    Thanks

  15. #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: Restrict user to open the same file

    What if one of the instances of the application crashes?

  16. #15
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Restrict user to open the same file

    Quote Originally Posted by wysota View Post
    What if one of the instances of the application crashes?
    Crashes When??? I think it can happen only while loading the file. If it crashes my program closes that application. The previous instance remains as it is....

  17. #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: Restrict user to open the same file

    Crashes = gets killed

    If you kill the application before releasing the pseudolock, you won't be able to open the same file again. You have to have some safetly mechanism to prevent that.

  18. #17
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Restrict user to open the same file

    Quote Originally Posted by wysota View Post
    Crashes = gets killed

    If you kill the application before releasing the pseudolock, you won't be able to open the same file again.
    Ok, so it can be dangerous. What do you mean when you talk pseudolock???
    You have to have some safetly mechanism to prevent that
    Can I provide some safety mechanism using the above specified solution by me???

    The solution I'm looking should be portable and as per my searching I didnt get any. I came across semaphores too but found that it works for linux.

    As Marcel says, I need to use some system dependent API's... What API's????

    I'll try out the solution and let you know the status....

    Thanks

  19. #18
    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: Restrict user to open the same file

    Quote Originally Posted by vermarajeev View Post
    What do you mean when you talk pseudolock???
    Well, the mechanism you describe doesn't do any locking, so I called it a pseudolock.

    Can I provide some safety mechanism using the above specified solution by me???
    If you can get some process identifier on Windows then yes. Store the PID along the filename which the particular process handles in your lock data and then when you want to open the same file check if a process with this identifier exists. If not, it means that the entry is stale and can be removed.

    The solution I'm looking should be portable and as per my searching I didnt get any. I came across semaphores too but found that it works for linux.
    You can use different mechanisms on different platforms.

    What API's????
    http://en.wikipedia.org/wiki/API

  20. #19
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Restrict user to open the same file

    You can get a FILE* from a C++ stream, you know... And then both flock and _lock_file will work.
    How to convert C++ stream to FILE*......

  21. #20
    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: Restrict user to open the same file

    You'll have to search through available documentation of ostream probably. There should be a way to access the buffer which you can then associate with a file descriptor or file handler or whatever.

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.