LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   How to know where a flash disk that was discovered, is mounted(from C++ application)? (http://www.linuxquestions.org/questions/programming-9/how-to-know-where-a-flash-disk-that-was-discovered-is-mounted-from-c-application-680672/)

Sharon Olshanksy 11-02-2008 12:49 PM

How to know where a flash disk that was discovered, is mounted(from C++ application)?
 
Hello, I have a question:

How to know where a flash disk that was discovered, is mounted(from C++ application)?

I write a project in C++/Linux (Debian), in which I have to identify when USB flash disk is insterted/removed, and when the flash disk is inserted,I have to read the files in the flash drive and copy them.

I found many ways to identify that the flash disk is insterted (I'm going to use usblib for that), but I haven't found a way to link between the data I get on the drive, to it's mapping in the file-system.

thanks,
Sharon :)

Sergei Steshenko 11-02-2008 01:06 PM

Quote:

Originally Posted by Sharon Olshanksy (Post 3329265)
Hello, I have a question:

How to know where a flash disk that was discovered, is mounted(from C++ application)?

I write a project in C++/Linux (Debian), in which I have to identify when USB flash disk is insterted/removed, and when the flash disk is inserted,I have to read the files in the flash drive and copy them.

I found many ways to identify that the flash disk is insterted (I'm going to use usblib for that), but I haven't found a way to link between the data I get on the drive, to it's mapping in the file-system.

thanks,
Sharon :)

What about parsing stdout of 'mount' command ?

Sergei Steshenko 11-02-2008 01:34 PM

Actually, it's even simpler; from "man mount":

/etc/mtab table of mounted file systems
.

ta0kira 11-02-2008 04:16 PM

Neither of those are really reliable since neither will tell you if 1) something was mounted from chroot, 2) something was mounted when / was read-only, 3) something was mounted with -n. Really the only reliable way to check for mounts is /proc/mounts, but some of the /proc files won't give you a size, so you just have to read linearly until they're empty.
ta0kira

PS That's the Linux-only solution; Sergei's is more portable.

Sergei Steshenko 11-02-2008 11:45 PM

Quote:

Originally Posted by ta0kira (Post 3329423)
Neither of those are really reliable since neither will tell you if 1) something was mounted from chroot, 2) something was mounted when / was read-only, 3) something was mounted with -n. Really the only reliable way to check for mounts is /proc/mounts, but some of the /proc files won't give you a size, so you just have to read linearly until they're empty.
ta0kira

PS That's the Linux-only solution; Sergei's is more portable.

/etc/mtab and/or 'mount' can be good starting points; the next logical step would be to look into 'mount' source and see which library functions are used to report mounted media.

To make it portable, if possible, one would have to look into various 'mount' flavors (Linux, *BSD) - maybe the both use just POSIX-compliant functions.

OTOH, if /etc/mtab is present on all UNIXes and has the same format, maybe it's not worth bothering with 'mount' source code and library functions.

I hope the OP has enough inputs for now :-).


All times are GMT -5. The time now is 06:09 PM.