Discussion:
[Interest] QLockFile
Graham Labdon
2015-02-23 09:48:57 UTC
Permalink
Hi

I am trying to use QLockFile in a function that receives a file path as a QString.

When I pass the string directly to the constructor of QLockFile and the call tryLock it always fails.

However, if I hard code the string the lock succeeds (although I get compiler warnings about unknown escape sequences)

If I user the toNativeSeparators method of QDir on my file path the call to tryLock fails although in the debugger the value of the converted string appears to the same as the hard coded value.

I would be grateful if someone could explain this behaviour

FileDestination::FileDestination(const QString& filePath)
{
QLockFile lockFile(filePath); // tryLock fails

QString s = QDir::toNativeSeparators(filePath)
QLockFile lockFile2(s); // tryLock fails

QLockFile lockFile3("C:\jobs\Myjob.txt"); // tryLock succeeds bu get compiler warnings for unknown escape sequences
}
André Somers
2015-02-23 09:52:17 UTC
Permalink
Post by Graham Labdon
Hi
I am trying to use QLockFile in a function that receives a file path as a QString.
When I pass the string directly to the constructor of QLockFile and the call tryLock it always fails.
However, if I hard code the string the lock succeeds (although I get compiler warnings about unknown escape sequences)
If I user the toNativeSeparators method of QDir on my file path the call to tryLock fails although in the debugger the value of the converted string appears to the same as the hard coded value.
I would be grateful if someone could explain this behaviour
FileDestination::FileDestination(const QString& filePath)
{
QLockFile lockFile(filePath); // tryLock fails
QString s = QDir::toNativeSeparators(filePath)
QLockFile lockFile2(s); // tryLock fails
QLockFile lockFile3("C:\jobs\Myjob.txt"); // tryLock succeeds bu get compiler warnings for unknown escape sequences
}
The last one is not valid. A \ in a C/C++ string is an escape character.
If you want a literal \ in your string, you need to double them. Or, in
Qt, you just use forward slashes (/) on all platforms.

So, are you sure your filePath itself is valid?

André
André Somers
2015-02-23 10:14:46 UTC
Permalink
Yes I am sure
I have tried this with numerous file locations and different ways of constructing a QLockFile and the ONLY on that works is if I hard code an non escaped file path.
The non-escaped path will point to a different location than what you
think it points to. That may be why that one succeeds.

I have used QLockFile successfully (with "dynamic" paths). So, there
really is something wrong with your way of using it, not with QLockFile
itself.

Did you try opening a file yourself at the location you want from code
using QFile and opening it write only? What happens if you do that?

André
-----Original Message-----
Sent: 23 February 2015 10:07
To: Graham Labdon
Subject: Re: [Interest] QLockFile
Hi
The filePath is definitely valid
The only way I can get QLockFile to work at the moment is by hard coding an invalid c string as in my example.
If I use double backslashes in the hard code version tryLock fails
Are you sure you can actually write at that location?
André
André Somers
2015-02-23 10:44:01 UTC
Permalink
Hi
QString path = "C:\jobs\Test1\job.xcfg";
QFile file(path);
bool res = file.open(QIODevice::WriteOnly);
here res is true
But, the path is once again wrong. Use \\ or /, but not \ in string
literal paths.
BUT
QString path = "C:\jobs\Test1\job.xcfg";
QLockFile lock(path);
bool res = lock.tryLock();
here res is false
BUT
QLockFile lock("C:\jobs\Test1\job.xcfg");
bool res = lock.tryLock();
here res is true!
And you are sure that you are only locking once? And what does error()
return when tryLock fails?

André

P.S. Please keep the discussion on the list, not in private mail.
Thanks for your time
-----Original Message-----
Sent: 23 February 2015 10:15
Subject: Re: [Interest] QLockFile
Yes I am sure
I have tried this with numerous file locations and different ways of constructing a QLockFile and the ONLY on that works is if I hard code an non escaped file path.
The non-escaped path will point to a different location than what you think it points to. That may be why that one succeeds.
I have used QLockFile successfully (with "dynamic" paths). So, there really is something wrong with your way of using it, not with QLockFile itself.
Did you try opening a file yourself at the location you want from code using QFile and opening it write only? What happens if you do that?
André
-----Original Message-----
Sent: 23 February 2015 10:07
To: Graham Labdon
Subject: Re: [Interest] QLockFile
Hi
The filePath is definitely valid
The only way I can get QLockFile to work at the moment is by hard coding an invalid c string as in my example.
If I use double backslashes in the hard code version tryLock fails
Are you sure you can actually write at that location?
André
_______________________________________________
Interest mailing list
http://lists.qt-project.org/mailman/listinfo/interest
Graham Labdon
2015-02-23 11:06:44 UTC
Permalink
Hi

It seems that I needed to do a removeStaleLockFile when I had previously stopped my program via the debugger.

I can now get a lock on the file, however, I cannot now open it for writing.

This is the pseudo code for what I need to do

Lock file
Open file
Write text
Close file
Unlock file

Is this the correct way ? - I guess not as I cannot open the file for writing

Thanks

-----Original Message-----
From: interest-bounces+graham.labdon=***@qt-project.org [mailto:interest-bounces+graham.labdon=***@qt-project.org] On Behalf Of André Somers
Sent: 23 February 2015 10:44
To: ***@qt-project.org
Subject: Re: [Interest] QLockFile
Hi
QString path = "C:\jobs\Test1\job.xcfg"; QFile file(path);
bool res = file.open(QIODevice::WriteOnly);
here res is true
But, the path is once again wrong. Use \\ or /, but not \ in string literal paths.
BUT
QString path = "C:\jobs\Test1\job.xcfg"; QLockFile lock(path);
bool res = lock.tryLock();
here res is false
BUT
QLockFile lock("C:\jobs\Test1\job.xcfg");
bool res = lock.tryLock();
here res is true!
And you are sure that you are only locking once? And what does error() return when tryLock fails?

André

P.S. Please keep the discussion on the list, not in private mail.
Thanks for your time
-----Original Message-----
rg] On Behalf Of André Somers
Sent: 23 February 2015 10:15
Subject: Re: [Interest] QLockFile
Yes I am sure
I have tried this with numerous file locations and different ways of constructing a QLockFile and the ONLY on that works is if I hard code an non escaped file path.
The non-escaped path will point to a different location than what you think it points to. That may be why that one succeeds.
I have used QLockFile successfully (with "dynamic" paths). So, there really is something wrong with your way of using it, not with QLockFile itself.
Did you try opening a file yourself at the location you want from code using QFile and opening it write only? What happens if you do that?
André
-----Original Message-----
Sent: 23 February 2015 10:07
To: Graham Labdon
Subject: Re: [Interest] QLockFile
Hi
The filePath is definitely valid
The only way I can get QLockFile to work at the moment is by hard coding an invalid c string as in my example.
If I use double backslashes in the hard code version tryLock fails
Are you sure you can actually write at that location?
André
_______________________________________________
Interest mailing list
http://lists.qt-project.org/mailman/listinfo/interest
André Somers
2015-02-23 11:12:28 UTC
Permalink
Post by Graham Labdon
Hi
It seems that I needed to do a removeStaleLockFile when I had previously stopped my program via the debugger.
I can now get a lock on the file, however, I cannot now open it for writing.
Why would you want to write to a lock file? A lock file protects a
resource, it isn't the same as the resource itself. I have not checked,
but I guess the implementation writes the pid of the process that
created the file into the file, and expects this to be the only content
of the file.
Post by Graham Labdon
This is the pseudo code for what I need to do
Lock file
Open file
Write text
Close file
Unlock file
Is this the correct way ? - I guess not as I cannot open the file for writing
That is because the file is not yours to write to. It is owned by
QLockFile. At least, that is how I understand it.

André
Post by Graham Labdon
Thanks
-----Original Message-----
Sent: 23 February 2015 10:44
Subject: Re: [Interest] QLockFile
Hi
QString path = "C:\jobs\Test1\job.xcfg"; QFile file(path);
bool res = file.open(QIODevice::WriteOnly);
here res is true
But, the path is once again wrong. Use \\ or /, but not \ in string literal paths.
BUT
QString path = "C:\jobs\Test1\job.xcfg"; QLockFile lock(path);
bool res = lock.tryLock();
here res is false
BUT
QLockFile lock("C:\jobs\Test1\job.xcfg");
bool res = lock.tryLock();
here res is true!
And you are sure that you are only locking once? And what does error() return when tryLock fails?
André
P.S. Please keep the discussion on the list, not in private mail.
Thanks for your time
-----Original Message-----
rg] On Behalf Of André Somers
Sent: 23 February 2015 10:15
Subject: Re: [Interest] QLockFile
Yes I am sure
I have tried this with numerous file locations and different ways of constructing a QLockFile and the ONLY on that works is if I hard code an non escaped file path.
The non-escaped path will point to a different location than what you think it points to. That may be why that one succeeds.
I have used QLockFile successfully (with "dynamic" paths). So, there really is something wrong with your way of using it, not with QLockFile itself.
Did you try opening a file yourself at the location you want from code using QFile and opening it write only? What happens if you do that?
André
-----Original Message-----
Sent: 23 February 2015 10:07
To: Graham Labdon
Subject: Re: [Interest] QLockFile
Hi
The filePath is definitely valid
The only way I can get QLockFile to work at the moment is by hard coding an invalid c string as in my example.
If I use double backslashes in the hard code version tryLock fails
Are you sure you can actually write at that location?
André
_______________________________________________
Interest mailing list
http://lists.qt-project.org/mailman/listinfo/interest
_______________________________________________
Interest mailing list
http://lists.qt-project.org/mailman/listinfo/interest
Graham Labdon
2015-02-23 11:14:36 UTC
Permalink
Hi
Yes, just figured that out.

I need to get the lock then open the real file ....

Thanks for your help



-----Original Message-----
From: interest-bounces+graham.labdon=***@qt-project.org [mailto:interest-bounces+graham.labdon=***@qt-project.org] On Behalf Of André Somers
Sent: 23 February 2015 11:12
To: ***@qt-project.org
Subject: Re: [Interest] QLockFile
Post by Graham Labdon
Hi
It seems that I needed to do a removeStaleLockFile when I had previously stopped my program via the debugger.
I can now get a lock on the file, however, I cannot now open it for writing.
Why would you want to write to a lock file? A lock file protects a resource, it isn't the same as the resource itself. I have not checked, but I guess the implementation writes the pid of the process that created the file into the file, and expects this to be the only content of the file.
Post by Graham Labdon
This is the pseudo code for what I need to do
Lock file
Open file
Write text
Close file
Unlock file
Is this the correct way ? - I guess not as I cannot open the file for writing
That is because the file is not yours to write to. It is owned by QLockFile. At least, that is how I understand it.

André
Post by Graham Labdon
Thanks
-----Original Message-----
rg] On Behalf Of André Somers
Sent: 23 February 2015 10:44
Subject: Re: [Interest] QLockFile
Hi
QString path = "C:\jobs\Test1\job.xcfg"; QFile file(path);
bool res = file.open(QIODevice::WriteOnly);
here res is true
But, the path is once again wrong. Use \\ or /, but not \ in string literal paths.
BUT
QString path = "C:\jobs\Test1\job.xcfg"; QLockFile lock(path);
bool res = lock.tryLock();
here res is false
BUT
QLockFile lock("C:\jobs\Test1\job.xcfg");
bool res = lock.tryLock();
here res is true!
And you are sure that you are only locking once? And what does error() return when tryLock fails?
André
P.S. Please keep the discussion on the list, not in private mail.
Thanks for your time
-----Original Message-----
o
rg] On Behalf Of André Somers
Sent: 23 February 2015 10:15
Subject: Re: [Interest] QLockFile
Yes I am sure
I have tried this with numerous file locations and different ways of constructing a QLockFile and the ONLY on that works is if I hard code an non escaped file path.
The non-escaped path will point to a different location than what you think it points to. That may be why that one succeeds.
I have used QLockFile successfully (with "dynamic" paths). So, there really is something wrong with your way of using it, not with QLockFile itself.
Did you try opening a file yourself at the location you want from code using QFile and opening it write only? What happens if you do that?
André
-----Original Message-----
Sent: 23 February 2015 10:07
To: Graham Labdon
Subject: Re: [Interest] QLockFile
Hi
The filePath is definitely valid
The only way I can get QLockFile to work at the moment is by hard coding an invalid c string as in my example.
If I use double backslashes in the hard code version tryLock fails
Are you sure you can actually write at that location?
André
_______________________________________________
Interest mailing list
http://lists.qt-project.org/mailman/listinfo/interest
_______________________________________________
Interest mailing list
http://lists.qt-project.org/mailman/listinfo/interest
Loading...