Detect Drive

Linux specific questions.
Post Reply
Dinosaur
Posts: 1481
Joined: Jul 24, 2005 1:13
Location: Hervey Bay (.au)

Detect Drive

Post by Dinosaur »

Hi All

I am trying to detect a usb stick to copy a file from within my program, but it is not being seen by the following code:

Code: Select all

If Dir("/dev/sdb1") <> "" Then
	Print "Copying Snapshot.txt"
	ErrControl.Result = Filecopy ("/home/hmi/reports/Snapshot.txt" , "/dev/sdb1/Snapshot.txt")
Endif
Looking in the browser the stick identifies itself as "/media/50E2-7AE0(/dev/sdb1)
Remming out the Dir test, still creates an error copying to the device.
Similarly using the Target as "/media/50E2-7AE0/Snapshot.txt" also creates an error.

Because the operator does not have browsing permission, and may plug a usb stick in that has a totally
different identifier, I have to use the /dev/sdb1 device description.

I can do the normal Filecopy on the booted drive by using a path and as the owner of both drives,
but have never tried to copy to another drive from FB using Linux.

Regards

Regards
nimdays
Posts: 236
Joined: May 29, 2014 22:01
Location: West Java, Indonesia

Re: Detect Drive

Post by nimdays »

I'm browsing win at the moment.
Perhaps the asterisk.
Dinosaur wrote:

Code: Select all

If Dir("/dev/sdb1/*") <> "" Then
	Print "Copying Snapshot.txt"
	ErrControl.Result = Filecopy ("/home/hmi/reports/Snapshot.txt" , "/dev/sdb1/Snapshot.txt")
Endif
Dinosaur
Posts: 1481
Joined: Jul 24, 2005 1:13
Location: Hervey Bay (.au)

Re: Detect Drive

Post by Dinosaur »

Hi All

nimdays, I have tried various formats with Dir, BUT the following works with barfresh being the user.

Code: Select all

        If FileExists("/media/barfresh/50E2-7AE0") Then                            ''Ok it is there
            Print "Copying Snapshot.txt"
            ErrControl.Result = Filecopy ("/home/barfresh/hmi/reports/Snapshot.txt" , "/media/barfresh/50E2-7AE0/Snapshot.txt")
            If ErrControl.Result > 0 Then Print "Error Copying Snapshot.txt", ErrControl.Result
        EndIf
However, I need to get away from the name of the device and address it as a device (ie:/dev/sdb1)

Regards
caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

Re: Detect Drive

Post by caseih »

There are a number of ways to detect storage devices. It all depends on what init system your distribution is using. Most distros now use udev or something compatible with udev (systemd-udev). When events happen udev sets up the necessary dev entries. Most distros's udev setup creates a bunch of device aliases under /dev/disk/. There you can find entries by a device id, volume name, or hardware bus path. I use this method for detecting my backup disks, specifically, looking for a volume label that I gave my USB disk when I formatted it.

Anther good method is to query the system message bus directly (which udev also talks to). Here you can get real-time messages when devices are plugged or unplugged, or just query the system for storage devices. Here's an article that talks about this, with some Python code, but the short examples probably could be translated to FB with the header files that I think are already translated. https://linuxmeerkat.wordpress.com/2014 ... ge-device/
nimdays
Posts: 236
Joined: May 29, 2014 22:01
Location: West Java, Indonesia

Re: Detect Drive

Post by nimdays »

I don't know what your distro is, but this works on my Tinycore
They use "/mnt"

Code: Select all

#include "file.bi"
If Dir("/mnt/sdb1/*") <> "" Then
   Print "Copying angel.jpg"
   Filecopy ("/home/tc/angel.jpg" , "/mnt/sdb1/angel.jpg")
Endif
counting_pine
Site Admin
Posts: 6323
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Re: Detect Drive

Post by counting_pine »

If the problem is simply in determining where the device is mounted, you could try parsing /proc/mounts for the drive.
I don't know if you can guarantee that your USB device will be detected as /dev/sdb though - or that the main partition will be sdb1.

If you are running systemd (e.g. a recent Ubuntu), you might find this solution helpful - it runs a script when a drive is mounted:
https://askubuntu.com/a/679600/37574
(Possibly just a specific drive, I'm not sure)
Dinosaur
Posts: 1481
Joined: Jul 24, 2005 1:13
Location: Hervey Bay (.au)

Re: Detect Drive

Post by Dinosaur »

Hi All

Thanks for the responses.
I am running Mint 18.3 which creates a folder in /media/username/Volume Name.
Because my systems are closed to others, I can see what has changed in the folder and use the volume name as part of the path.

I know the folder has one locked folder called "usb stick". So any additional folder will be the newly plugged in usb stick.
So, I decided the KISS principle should apply and just read the name of the newly created folder and put it in the path.
Worked great if you browse the folder and record the name.

Not so simple from within FB , there is no command to read Folder names.
Searching the site does not reveal an easy fix.
Does any one know of one ?

Regards
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Detect Drive

Post by MrSwiss »

Dinosaur wrote:Not so simple from within FB , there is no command to read Folder names.
Well, there is: DIR ...
Use it with a period '.' at the end, to show also, when the folder is 'empty'.
(you might have to: #Include "dir.bi")
Dinosaur
Posts: 1481
Joined: Jul 24, 2005 1:13
Location: Hervey Bay (.au)

Re: Detect Drive

Post by Dinosaur »

Hi All

Thanks MrSwiss , I have only ever used Dir() for files.
Reading the Wiki on it clarified that.

I would have thought such a crucial function would have it's own command.
GetDir()

Regards
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Detect Drive

Post by MrSwiss »

Dinosaur wrote:I would have thought such a crucial function would have it's own command.
Nope, it won't because:
a directory/folder is just a 'specially marked' file (from OS point of view).

'specially marked' = dir attribute (is set)
Dinosaur
Posts: 1481
Joined: Jul 24, 2005 1:13
Location: Hervey Bay (.au)

Re: Detect Drive

Post by Dinosaur »

Hi All
a directory/folder is just a 'specially marked' file (from OS point of view).
'specially marked' = dir attribute (is set)
That is a good explanation.

For the sake of completeness below is final code. It works regardless of what stick I plug in, and if someone was to plug some other device in
then the copy will just fail.

Code: Select all

        ChDir("/media/barfresh")
        Dim As String DirName = Dir("*",&H10)
        If DirName > "" Then
                Print "Stick plugged in = ";DirName
                If FileExists("/home/barfresh/hmi/reports/Snapshot.txt") Then   
                        Print "Copying Snapshot.txt"
                        ErrControl.Result = Filecopy ("/home/barfresh/hmi/reports/Snapshot.txt" , "/media/barfresh/" + DirName + "/Snapshot.txt")
                        If ErrControl.Result > 0 Then Print "Error Copying Snapshot.txt", ErrControl.Result
                    Else
                        Print "No Snapshot.txt file"
                EndIf
            Else
                Print "Quiting without copying Snapshot.txt"
        EndIf
Many thanks for all the help.

Regards
Post Reply