August 13, 2011

TheQtGuy TheQtGuy
Lab Rat
6 posts

QFileSystemWatcher not working with /etc/mtab

 

I’ll try to be brief:

My program should automatically sense when a USB device is plugged and also where that device was mounted. So far I’ve been able to sense the usb device plugged, as well as getting which device was inserted (i.e. /dev/sdc1).

Knowing the device (i.e. /dev/sdc1) I can then simply look inside /etc/mtab to match that device with its mount point (i.e. /media/USBDRIVE). However when I try to find that device inside /etc/mtab, I get nothing, but if I tell the program to wait 1 second before trying to search inside /etc/mtab, it works, meaning my program was actually trying to find the device before it was actually added to /etc/mtab

So what I did was instead of waiting 1 second to search in /etc/mtab, I created a QFileSystemWatcher for that file:

  1. QFileSystemWatcher watcher = new QFileSystemWatcher(QStringList() << "/etc/mtab",this);

But watcher never detects any change in /etc/mtab! Even if try /proc/mounts which is the same file… What is weird is that for any file inside /home, QFileSystemWatcher will work, but for /etc or /proc files it won’t work.

I will thank you guys for any help, since the 1 second wait is not the best approach..

11 replies

August 13, 2011

loladiro loladiro
Lab Rat
596 posts

Judging from this [qt.gitorious.org] patch applied to Qt Mobility, there is a bug in QFileSystemWatcher, preventing it from being used in /etc/mtab. Maybe the patch I liked to can help you, too.

August 13, 2011

peppe peppe
Ant Farmer
1028 posts

Works for me — are you returning to the event loop?

 Signature 

Software Engineer
KDAB (UK) Ltd., a KDAB Group company

August 14, 2011

TheQtGuy TheQtGuy
Lab Rat
6 posts

loladiro: I read that page before, but I didn’t quite understood what was going on…

peppe: I don’t know what would be to return to the event loop, but my guess is that my code is correct, otherwise it wouldn’t work with files inside /home directory right?

  1. watcher = new QFileSystemWatcher(QStringList() << "/etc/mtab",this);
  2. connect(watcher, SIGNAL(fileChanged(const QString &)), this, SLOT(getPathFromFile(const QString &&#41;));

If I change “/etc/mtab” to say a file in my home folder, the fileChanged signal is emitted, but not for “etc/mtab” file :(

August 14, 2011

loladiro loladiro
Lab Rat
596 posts

The patch to Qt Mobility I linked basically changes the QFilesSystemWatcher and uses its inotify backend for linux. I think it is pretty straight forward what is going on, and since it does exactly what you want (listening for new devices), it might be worth it for you to see what’s going on and how they replaced the old QFileSystemWatcher syntax (in red) with inotify (in green).

August 14, 2011

peppe peppe
Ant Farmer
1028 posts

Can you paste a small, correct, compilable example that shows your problem? I know QFSW is bugged, but it appears to be working fine for me (inotify backend, Linux 3.0).

 Signature 

Software Engineer
KDAB (UK) Ltd., a KDAB Group company

September 12, 2014

McLion McLion
Hobby Entomologist
190 posts

Sorry to dig this thread up and hijack it.
How do you get an event when an USB device is plugged (ie. Memory stick as dev/sda1)?

September 12, 2014

a.jafarabadi a.jafarabadi
Ant Farmer
32 posts

Hi TheQtGuy
i don’t have any efficient solution but offer some idea:

  • Run Qt Creator as root :)
    1. sudo -b qtcreator
  • your real-time access problem is with Dir o File? check it !

say more about ur prob. and share ur experience dear ;)

September 12, 2014

McLion McLion
Hobby Entomologist
190 posts

@ a.jafarabadi: ???

September 12, 2014

a.jafarabadi a.jafarabadi
Ant Farmer
32 posts

excuse me friend because of my low english writing ability
i think that you want check your USB devices, and for doing it, you watch a file usign filewatcher that OS change it when a new usb connect or disconnect, but your watch doesn’t work and does not determine any change in file.
now you want understand why this problem occured? YES?

September 12, 2014

McLion McLion
Hobby Entomologist
190 posts

Not exactly.
If I attach a UBS memorystick it does not get mounted automatically so I can not monitor /etc/mtab.
I need to somehow catch the USB plug event and get the device path (i.e. /dev/sda1) and then handle the mount from my Qt code.

September 12, 2014

McLion McLion
Hobby Entomologist
190 posts

.. or could QFileSystemWatcher used to monitor the /dev ?
This way I t could generate an event when an sd?? is added.

 
  ‹‹ [Solved]Wrong QRegExp      ZWSP bug in QTextBrowser? ››

You must log in to post a reply. Not a member yet? Register here!