PDA

View Full Version : Check file if the file is writable or used by another process



Naahmi
11th July 2012, 11:08
Hello,

is there a way to check if the file is used by another process like soffice?
Under Windows it works perfect but under mac/linux is doesnt work. I think mac/linux doesn't make the file only readable.

My Testcase:



QString fileName = childElement.text();
fileName = QDir::tempPath() + "/" + fileName;

for(int i=0; i < 100; i++)
{
QFile file(fileName);

if(file.open(QIODevice::WriteOnly))
{
qDebug() << "JA";
} else {
qDebug() << "NEIN";
}

Charvi
11th July 2012, 11:21
Unfortunately there is no file locking in Unix.
What you can do is call a system command and use a combination of fuser and grep to find if some other process is using the file.

Naahmi
11th July 2012, 12:14
Unfortunately there is no file locking in Unix.
What you can do is call a system command and use a combination of fuser and grep to find if some other process is using the file.

unix create a ".~lock.filename#" logfile. but i have hope there is a better way to handle this. We are open the file with QDesktopService::openUrl(...);

wysota
11th July 2012, 12:40
Unix doesn't create any lock files. If an application doesn't lock the file for exclusive access (and on Unix applications rarely do that), there is no way of knowing if a file is open other than processing the /proc tree (if it exists and you have access to it).