How to limit so that a file can only be used by one user/process?
-
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:
How to implement this in Qt? I tried to create a lot of code using
QFile
(and someQTextStream
), i am doing things withbool QFile::open(QIODevice::OpenMode mode)
andbool 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 doisOpen()
beforeopen(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:
Someone who know how to implement so two processes/users cannot open the same file?
-
@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?
-
@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?
@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. -
@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.@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? -
@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?@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
-
@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?@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 useQLockFile()
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 ;)
-
@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 useQLockFile()
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 ;)
@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.
-
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.txtIn 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? -
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.txtIn 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?@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()
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 yourQLockFile
(at least from my understanding)...Check the usage of
QLockFile
here:
(Profile
is the resource that is going to get locked bylockFile
inProfileLocker
which holds aQLockFile
)