Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to limit so that a file can only be used by one user/process?
Forum Update on Monday, May 27th 2025

How to limit so that a file can only be used by one user/process?

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 5 Posters 773 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    pga4711
    wrote on last edited by
    #1

    Hello,

    I am trying to figure out how to limit a file "read or/and write session" to only be available to one user/process. Let me describe with this image:

    alt text

    How to implement this in Qt? I tried to create a lot of code using QFile (and some QTextStream), i am doing things with bool QFile::open(QIODevice::OpenMode mode) and bool QIODevice::isOpen() const. I created a program that opens a file and has it open during the software is alive. And then i start another instance of the same program and open the same file and there i do isOpen() before open(QFile::WriteOnly)), but it seems that two programs can have the same file open at the same time. I thought that it would be impossible to open 1 file in read or/and write mode from two programs. For example in excel you can get this dialog if two users open the same file:
    alt text

    Someone who know how to implement so two processes/users cannot open the same file?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mchinand
      wrote on last edited by
      #2

      I think QLockFile will work for your situation.

      P 1 Reply Last reply
      3
      • M mchinand

        I think QLockFile will work for your situation.

        P Offline
        P Offline
        pga4711
        wrote on last edited by
        #3

        @mchinand said in How to limit so that a file can only be used by one user/process?:

        I think QLockFile will work for your situation.

        Okay i read about it. But when i read i can see that they say

        void QLockFile::unlock()
        Releases the lock, by deleting the lock file.
        Calling unlock() without locking the file first, does nothing.

        If my list.json gets deletet it will be a catastrophe. I do not want my list.json get deleted when i unlock the file. I think i unlock the file before i will void QFileDevice::close() the file for example.

        How should we resonate here, have i missed something?

        KroMignonK 1 Reply Last reply
        0
        • P pga4711

          @mchinand said in How to limit so that a file can only be used by one user/process?:

          I think QLockFile will work for your situation.

          Okay i read about it. But when i read i can see that they say

          void QLockFile::unlock()
          Releases the lock, by deleting the lock file.
          Calling unlock() without locking the file first, does nothing.

          If my list.json gets deletet it will be a catastrophe. I do not want my list.json get deleted when i unlock the file. I think i unlock the file before i will void QFileDevice::close() the file for example.

          How should we resonate here, have i missed something?

          KroMignonK Offline
          KroMignonK Offline
          KroMignon
          wrote on last edited by
          #4

          @pga4711 said in How to limit so that a file can only be used by one user/process?:

          How should we resonate here, have i missed something?

          It deletes the lock file not the locked file ;)
          There is a temporary file which is created to mark the file as locked.

          It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

          P 1 Reply Last reply
          3
          • KroMignonK KroMignon

            @pga4711 said in How to limit so that a file can only be used by one user/process?:

            How should we resonate here, have i missed something?

            It deletes the lock file not the locked file ;)
            There is a temporary file which is created to mark the file as locked.

            P Offline
            P Offline
            pga4711
            wrote on last edited by
            #5

            @KroMignon said in How to limit so that a file can only be used by one user/process?:

            @pga4711 said in How to limit so that a file can only be used by one user/process?:

            How should we resonate here, have i missed something?

            It deletes the lock file not the locked file ;)
            There is a temporary file which is created to mark the file as locked.

            Okay so we are deling with two files here.
            The resource file: list.json
            The lock file: ~$list.json
            (I prepend ~$ because MS Office software seems to name a lock file like that when i googled a bit, just as an example)

            For example, imagine i have this file that should be "locked" somehow:
            H:/directory1/subdirectoryX/list.json
            Should i then do some string manipulation so the QLockFile constructor gets a string like this? (I mean, inserting ~$ in the right place?)
            H:/directory1/subdirectoryX/~$list.json
            Or is this "create-temporary-special-lock-file" done automatically?

            jsulmJ KroMignonK 2 Replies Last reply
            0
            • P pga4711

              @KroMignon said in How to limit so that a file can only be used by one user/process?:

              @pga4711 said in How to limit so that a file can only be used by one user/process?:

              How should we resonate here, have i missed something?

              It deletes the lock file not the locked file ;)
              There is a temporary file which is created to mark the file as locked.

              Okay so we are deling with two files here.
              The resource file: list.json
              The lock file: ~$list.json
              (I prepend ~$ because MS Office software seems to name a lock file like that when i googled a bit, just as an example)

              For example, imagine i have this file that should be "locked" somehow:
              H:/directory1/subdirectoryX/list.json
              Should i then do some string manipulation so the QLockFile constructor gets a string like this? (I mean, inserting ~$ in the right place?)
              H:/directory1/subdirectoryX/~$list.json
              Or is this "create-temporary-special-lock-file" done automatically?

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @pga4711 said in How to limit so that a file can only be used by one user/process?:

              Should i then do some string manipulation so the QLockFile constructor gets a string like this?

              Yes

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • P pga4711

                @KroMignon said in How to limit so that a file can only be used by one user/process?:

                @pga4711 said in How to limit so that a file can only be used by one user/process?:

                How should we resonate here, have i missed something?

                It deletes the lock file not the locked file ;)
                There is a temporary file which is created to mark the file as locked.

                Okay so we are deling with two files here.
                The resource file: list.json
                The lock file: ~$list.json
                (I prepend ~$ because MS Office software seems to name a lock file like that when i googled a bit, just as an example)

                For example, imagine i have this file that should be "locked" somehow:
                H:/directory1/subdirectoryX/list.json
                Should i then do some string manipulation so the QLockFile constructor gets a string like this? (I mean, inserting ~$ in the right place?)
                H:/directory1/subdirectoryX/~$list.json
                Or is this "create-temporary-special-lock-file" done automatically?

                KroMignonK Offline
                KroMignonK Offline
                KroMignon
                wrote on last edited by KroMignon
                #7

                @pga4711 said in How to limit so that a file can only be used by one user/process?:

                For example, imagine i have this file that should be "locked" somehow:
                H:/directory1/subdirectoryX/list.json
                Should i then do some string manipulation so the QLockFile constructor gets a string like this? (I mean, inserting ~$ in the right place?)
                H:/directory1/subdirectoryX/~$list.json
                Or is this "create-temporary-special-lock-file" done automatically?

                I am not sure to understand what your question is...
                To use QLockFile() you have to pass the full path of the file you want to protected.
                The generated name for the lock file depends on OS (I guess)
                .

                EDIT: According to @jsulm reply (and documentation), it looks like I don't understand how QLockFile works at all! I will follow this topic to learn more about it ;)

                It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                jsulmJ 1 Reply Last reply
                0
                • KroMignonK KroMignon

                  @pga4711 said in How to limit so that a file can only be used by one user/process?:

                  For example, imagine i have this file that should be "locked" somehow:
                  H:/directory1/subdirectoryX/list.json
                  Should i then do some string manipulation so the QLockFile constructor gets a string like this? (I mean, inserting ~$ in the right place?)
                  H:/directory1/subdirectoryX/~$list.json
                  Or is this "create-temporary-special-lock-file" done automatically?

                  I am not sure to understand what your question is...
                  To use QLockFile() you have to pass the full path of the file you want to protected.
                  The generated name for the lock file depends on OS (I guess)
                  .

                  EDIT: According to @jsulm reply (and documentation), it looks like I don't understand how QLockFile works at all! I will follow this topic to learn more about it ;)

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @KroMignon said in How to limit so that a file can only be used by one user/process?:

                  To use QLockFile() you have to pass the full path of the file you want to protected.

                  No, see https://doc.qt.io/qt-5/qlockfile.html#QLockFile

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • P Offline
                    P Offline
                    pga4711
                    wrote on last edited by
                    #9

                    Hello,

                    Now i can create a “lock file”. I use txt files for simplicity. For example i have
                    C:/directory1/subdirectoryX/list.txt
                    Then i can create
                    C:/directory1/subdirectoryX/~$list.txt

                    In the documentation they say “Creating the lock file”. “Aquire the lock”, “Obtaining the lock”. For me, either a lock could be locked or unlocked by using a key. I don't understand the abstraction of the “lock” here. Do they mean that when a “lock file” exists, then the “lock” is locked by the process that created the file?

                    All locks in the world usually have a key. How does the key works in this QLockFile? Maybe there is no key? How could we abstract this then? Mutex or binary semaphores?? Someone who would like to explain? :)

                    I tried some fun things. For example i this:

                    ...
                    theLockFile = new QLockFile(lockFileFilePath);
                    
                    if (theLockFile==nullptr) { //Is this necessary???
                        emit error("Could not do theLockFile = new QLockFile(lockFileFilePath);"); 
                        return false;
                    }
                    
                    if (!theLockFile->tryLock()) {
                        qDebug()<<"The lock file error: "<<theLockFile->error();
                        qint64 *pid = new qint64(0); //Is this the right thing to fetch things from getLockInfo(...)?
                        QString *hostname = new QString("");
                        QString *appname = new QString("");
                                
                        theLockFile->getLockInfo(pid, hostname, appname);
                        emit error("File is locked: " + lockFileFilePath + "\nPID: " + QString::number(*pid) + ", " + hostname + ", " + appname);
                        return false;
                    }
                    else {
                        qDebug()<<"Did we lock? isLocked: "<<theLockFile->isLocked(); //RETURNS TRUE. Okay good.
                        qDebug()<<"We have locked the file?";
                    }
                    ...
                    

                    But i have a button that i check what theLockFile->isLocked() returns. I tap it some seconds after i ran the code above and it returns FALSE. :o . When it returns false, i call tryLock() just for fun. But tryLock() returns FALSE.
                    I thought that theLockFile->isLocked() always returns true as long as we have a successful tryLock() or lock(). But now it returns false and i cant even do a tryLock() again. What is wrong here?

                    Pl45m4P 1 Reply Last reply
                    0
                    • P pga4711

                      Hello,

                      Now i can create a “lock file”. I use txt files for simplicity. For example i have
                      C:/directory1/subdirectoryX/list.txt
                      Then i can create
                      C:/directory1/subdirectoryX/~$list.txt

                      In the documentation they say “Creating the lock file”. “Aquire the lock”, “Obtaining the lock”. For me, either a lock could be locked or unlocked by using a key. I don't understand the abstraction of the “lock” here. Do they mean that when a “lock file” exists, then the “lock” is locked by the process that created the file?

                      All locks in the world usually have a key. How does the key works in this QLockFile? Maybe there is no key? How could we abstract this then? Mutex or binary semaphores?? Someone who would like to explain? :)

                      I tried some fun things. For example i this:

                      ...
                      theLockFile = new QLockFile(lockFileFilePath);
                      
                      if (theLockFile==nullptr) { //Is this necessary???
                          emit error("Could not do theLockFile = new QLockFile(lockFileFilePath);"); 
                          return false;
                      }
                      
                      if (!theLockFile->tryLock()) {
                          qDebug()<<"The lock file error: "<<theLockFile->error();
                          qint64 *pid = new qint64(0); //Is this the right thing to fetch things from getLockInfo(...)?
                          QString *hostname = new QString("");
                          QString *appname = new QString("");
                                  
                          theLockFile->getLockInfo(pid, hostname, appname);
                          emit error("File is locked: " + lockFileFilePath + "\nPID: " + QString::number(*pid) + ", " + hostname + ", " + appname);
                          return false;
                      }
                      else {
                          qDebug()<<"Did we lock? isLocked: "<<theLockFile->isLocked(); //RETURNS TRUE. Okay good.
                          qDebug()<<"We have locked the file?";
                      }
                      ...
                      

                      But i have a button that i check what theLockFile->isLocked() returns. I tap it some seconds after i ran the code above and it returns FALSE. :o . When it returns false, i call tryLock() just for fun. But tryLock() returns FALSE.
                      I thought that theLockFile->isLocked() always returns true as long as we have a successful tryLock() or lock(). But now it returns false and i cant even do a tryLock() again. What is wrong here?

                      Pl45m4P Online
                      Pl45m4P Online
                      Pl45m4
                      wrote on last edited by Pl45m4
                      #10

                      @pga4711 said in How to limit so that a file can only be used by one user/process?:

                      In the documentation they say “Creating the lock file”. “Aquire the lock”, “Obtaining the lock”. For me, either a lock could be locked or unlocked by using a key. I don't understand the abstraction of the “lock” here. Do they mean that when a “lock file” exists, then the “lock” is locked by the process that created the file?

                      A file lock "locks" the file or resource access to prevent the file from additional access and prevents it from deletion etc... (data serialization is the "key"-word here)... There is no "real" key involved like, for example, as if you would use a door lock or some encryption "key".
                      One very common example for lock file are MS Word or Excel documents... Once you open a file, a temporary, and most of the time, hidden (depending on file system), lock file named like ~myDocument.xlsx appears... This prevents the file from being edited by, for example another user, at the same time (because on startup the program checks if a lock exists).

                      If you need a "key":

                      QLockFile::unlock()

                      • https://doc.qt.io/qt-5/qlockfile.html#unlock

                      Edit:

                      According your problem: What file path do you pass to QLockFile... You should not pass the path to the file which you want to protect, but the path where you want to place the lock...
                      The resource you want to lock has the PID of your QLockFile (at least from my understanding)...

                      Check the usage of QLockFile here:
                      ( Profile is the resource that is going to get locked by lockFile in ProfileLocker which holds a QLockFile)

                      • https://github.com/qTox/qTox/blob/master/src/persistence/profile.cpp
                      • https://github.com/qTox/qTox/blob/master/src/persistence/profilelocker.h
                      • https://github.com/qTox/qTox/blob/master/src/persistence/profilelocker.cpp

                      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                      ~E. W. Dijkstra

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved