Mounted Drives

Post

Posted
Rating:
#1 (In Topic #358)
Avatar
Guru
cogier is in the usergroup ‘Guru’
Following a comment by Sholzy regarding moving a cache to another drive, I started looking into ways to access all mounted drives and as a start came up with the code below. As usual I am looking for feedback. Does this code work on your system?

All you need to do is paste the code into a 'Graphical Application' and run it. All it does is look at the drives and display the drive you click on in a DirChooser. There is a timer so that if you plug in (or remove) a USB drive /Mount (or unmount) a drive it should change the display. It will only display mounted drives.

I look forward to your comments.

Code (gambas)

  1. ' Gambas class file
  2.  
  3. sDriveToWorkWith As New String[]
  4. HSplit1 As HSplit
  5. DirChooser1 As DirChooser
  6. GridViewDrives As GridView
  7. LabelPath As Label
  8. TimerGetData As Timer
  9. siChange As Short
  10.  
  11. Public Sub Form_Open()
  12.  
  13.   BuildForm
  14.   HSplit1.Layout = [70, 30]
  15.   TimerGetData_Timer
  16.   TimerGetData.Start
  17.  
  18.  
  19. Public Sub GridViewDrives_Click()
  20.  
  21.   Dim siRow, siCol As Short
  22.  
  23.   DirChooser1.Root = GridViewDrives[Last.Row, 6].Text
  24.  
  25.   For siRow = 0 To GridViewDrives.Rows.Max
  26.     For siCol = 0 To 6
  27.       If siRow <> Last.Row Then
  28.         GridViewDrives[siRow, siCol].Background = Color.Default
  29.       Else
  30.         GridViewDrives[siRow, siCol].Background = Color.LightGray
  31.       End If
  32.     Next
  33.   Next
  34.  
  35.  
  36. Public Sub DisplayData()
  37.  
  38.   Dim sHeader As String[] = ["Drive", "Type", "Size", "Used", "Available", "Used", "Mounted"]
  39.   Dim siRow, siCol As Short
  40.  
  41.   With GridViewDrives
  42.     .Clear
  43.     .Columns.Count = 7
  44.     .Rows.Count = 0
  45.  
  46.   For siCol = 0 To 6
  47.     GridViewDrives.Columns[siCol].Title = sHeader[siCol]
  48.   Next
  49.  
  50.   For siRow = 0 To sDriveToWorkWith.Max
  51.     Inc GridViewDrives.Rows.Count
  52.     For siCol = 0 To 6
  53.       GridViewDrives[siRow, siCol].Text = Split(sDriveToWorkWith[siRow], "`")[siCol]
  54.     Next
  55.   Next
  56.  
  57.   GridViewDrives.Columns.Width = -1
  58.   GridViewDrives.Height = (GridViewDrives.Rows[0].Height * sDriveToWorkWith.Count) + 35
  59.  
  60.  
  61. Public Sub TimerGetData_Timer()
  62.  
  63.   Dim sData, sHold, sPart1, sPart2 As String
  64.   Dim sDrives As String[]
  65.   Dim siLoop, siSplit As Short
  66.  
  67.   siChange = sDriveToWorkWith.Count
  68.   sDriveToWorkWith.Clear
  69.  
  70.   Shell "df -aTh" To sData
  71.   sDrives = Split(sData, gb.NewLine, "", True)
  72.  
  73.   For siLoop = 0 To sDrives.Max
  74.  
  75.     If sDrives[siLoop] Begins "/dev/s" Then
  76.       sPart2 = Mid(sDrives[siLoop], InStr(sDrives[siLoop], "/", 6))
  77.       sPart1 = Mid(sDrives[siLoop], 1, InStr(sDrives[siLoop], "/", 6) - 1)
  78.       sData = ""
  79.  
  80.       For siSplit = 0 To Split(sPart1, " ").Max
  81.         sData &= Split(sPart1, " ")[siSplit] & "`"
  82.       Next
  83.  
  84.       Repeat
  85.         sHold = sData
  86.         sData = Replace(sData, "``", "`")
  87.       Until sData = sHold
  88.  
  89.       If sData Ends "`" Then sData = Left(sData, -1)
  90.       sDriveToWorkWith.Add(sData & "`" & sPart2)
  91.     Endif
  92.  
  93.   Next
  94.  
  95.   If siChange <> sDriveToWorkWith.Count Then DisplayData
  96.  
  97.  
  98. Public Sub DirChooser1_Change()
  99.  
  100.   LabelPath.Text = DirChooser1.SelectedPath
  101.  
  102.  
  103. Public Sub BuildForm()
  104.  
  105.   With Me
  106.     .Height = 575
  107.     .Width = 1100
  108.     .Arrangement = Arrange.Vertical
  109.     .Padding = 5
  110.  
  111.   With HSplit1 = New HSplit(Me)
  112.     .Expand = True
  113.     .Layout = [70, 30]
  114.  
  115.   With GridViewDrives = New GridView(HSplit1) As "GridViewDrives"
  116.     .Header = GridView.Both
  117.     .Padding = 2
  118.     .AutoResize = True
  119.  
  120.   With DirChooser1 = New DirChooser(HSplit1) As "DirChooser1"
  121.     .Expand = True
  122.  
  123.   With LabelPath = New Label(Me) As "LabelPath"
  124.     .Alignment = Align.Right
  125.     .Font.Bold = True
  126.     .Height = 28
  127.  
  128.   With TimerGetData = New Timer As "TimerGetData"
  129.     .delay = 250
  130.  
Online now: No Back to the top

Post

Posted
Rating:
#2
Avatar
Administrator
sholzy is in the usergroup ‘unknown’
The code as is wasn't able to find my Raid partitions. Raid partitions show up as /dev/md[x].

One small change to line 79 allowed your code to find all my partitions, including the Raid partitions.

Code (gambas)

  1. If sDrives[siLoop] Begins "/dev/s" Then
  2.  
changed to:

Code (gambas)

  1. If sDrives[siLoop] Begins "/dev/" Then
  2.  

sholzy
Gambas One Site Director

To report bugs in the Gambas IDE:
Official Gambas Bug Tracker
Online now: No Back to the top

Post

Posted
Rating:
#3
Avatar
Guru
cogier is in the usergroup ‘Guru’
If sDrives[siLoop] Begins "/dev/" Then

This solved one issue but unfortunately I ended up with all sorts of irrelevant stuff so have a go with this code.

Code (gambas)

  1. ' Gambas class file
  2.  
  3. sDriveToWorkWith As New String[]
  4. HSplit1 As HSplit
  5. DirChooser1 As DirChooser
  6. GridViewDrives As GridView
  7. LabelPath As Label
  8. TimerGetData As Timer
  9. siChange As Short
  10.  
  11. Public Sub Form_Open()
  12.  
  13.   BuildForm
  14.   TimerGetData_Timer
  15.   TimerGetData.Start
  16.  
  17.  
  18. Public Sub GridViewDrives_Click()
  19.  
  20.   Dim siRow, siCol As Short
  21.  
  22.   DirChooser1.Root = GridViewDrives[Last.Row, 6].Text
  23.  
  24.   For siRow = 0 To GridViewDrives.Rows.Max
  25.     For siCol = 0 To 6
  26.       If siRow <> Last.Row Then
  27.         GridViewDrives[siRow, siCol].Background = Color.Default
  28.       Else
  29.         GridViewDrives[siRow, siCol].Background = Color.LightGray
  30.       End If
  31.     Next
  32.   Next
  33.  
  34.  
  35. Public Sub DisplayData()
  36.  
  37.   Dim sHeader As String[] = ["Drive", "Type", "Size", "Used", "Available", "Used", "Mounted"]
  38.   Dim siRow, siCol As Short
  39.  
  40.   With GridViewDrives
  41.     .Clear
  42.     .Columns.Count = 7
  43.     .Rows.Count = 0
  44.  
  45.   For siCol = 0 To 6
  46.     GridViewDrives.Columns[siCol].Title = sHeader[siCol]
  47.   Next
  48.  
  49.   For siRow = 0 To sDriveToWorkWith.Max
  50.     Inc GridViewDrives.Rows.Count
  51.     For siCol = 0 To 6
  52.       GridViewDrives[siRow, siCol].Text = Split(sDriveToWorkWith[siRow], "`")[siCol]
  53.     Next
  54.   Next
  55.  
  56.   GridViewDrives.Columns.Width = -1
  57.   GridViewDrives.Height = (GridViewDrives.Rows[0].Height * sDriveToWorkWith.Count) + 35
  58.  
  59.  
  60. Public Sub TimerGetData_Timer()
  61.  
  62.   Dim sData, sHold, sPart1, sPart2, sCtrl As String
  63.   Dim sDrives As String[]
  64.   Dim siLoop, siSplit As Short
  65.  
  66.   siChange = sDriveToWorkWith.Count
  67.   sDriveToWorkWith.Clear
  68.  
  69.   Shell "df -aTh" To sData
  70.   sDrives = Split(sData, gb.NewLine, "", True)
  71.  
  72.   For siLoop = 0 To sDrives.Max
  73.  
  74.     If sDrives[siLoop] Begins "/dev/" Then
  75.  
  76.       sCtrl = Trim(Split(sDrives[siLoop], " ")[0])
  77.       If Len(sCtrl) <> 9 Or Not IsNumber(Right(sCtrl)) Then Continue
  78.  
  79.       sPart2 = Mid(sDrives[siLoop], InStr(sDrives[siLoop], "/", 6))
  80.       sPart1 = Mid(sDrives[siLoop], 1, InStr(sDrives[siLoop], "/", 6) - 1)
  81.       sData = ""
  82.  
  83.       For siSplit = 0 To Split(sPart1, " ").Max
  84.         sData &= Split(sPart1, " ")[siSplit] & "`"
  85.       Next
  86.  
  87.       Repeat
  88.         sHold = sData
  89.         sData = Replace(sData, "``", "`")
  90.       Until sData = sHold
  91.  
  92.       If sData Ends "`" Then sData = Left(sData, -1)
  93.       sDriveToWorkWith.Add(sData & "`" & sPart2)
  94.     Endif
  95.  
  96.   Next
  97.  
  98.   If siChange <> sDriveToWorkWith.Count Then DisplayData
  99.  
  100.  
  101. Public Sub DirChooser1_Change()
  102.  
  103.   LabelPath.Text = DirChooser1.SelectedPath
  104.  
  105.  
  106. Public Sub BuildForm()
  107.  
  108.   With Me
  109.     .Height = 575
  110.     .Width = 1100
  111.     .Arrangement = Arrange.Vertical
  112.     .Padding = 5
  113.  
  114.   With HSplit1 = New HSplit(Me)
  115.     .Expand = True
  116.     .Layout = [70, 30]
  117.  
  118.   With GridViewDrives = New GridView(HSplit1) As "GridViewDrives"
  119.     .Header = GridView.Both
  120.     .Padding = 2
  121.     .AutoResize = True
  122.  
  123.   With DirChooser1 = New DirChooser(HSplit1) As "DirChooser1"
  124.     .Expand = True
  125.  
  126.   With LabelPath = New Label(Me) As "LabelPath"
  127.     .Alignment = Align.Right
  128.     .Font.Bold = True
  129.     .Height = 28
  130.  
  131.   With TimerGetData = New Timer As "TimerGetData"
  132.     .delay = 250
  133.  
  134.  
Online now: No Back to the top

Post

Posted
Rating:
#4
Avatar
Administrator
sholzy is in the usergroup ‘unknown’
Same as before, didn't find the /dev/md[x] Raid partitions.

Here's the output of df if it helps any.

Code

$ df -aTh
Filesystem     Type         Size  Used Avail Use% Mounted on
sysfs          sysfs           0     0     0    - /sys
proc           proc            0     0     0    - /proc
udev           devtmpfs     7.8G     0  7.8G   0% /dev
devpts         devpts          0     0     0    - /dev/pts
tmpfs          tmpfs        1.6G   58M  1.6G   4% /run
/dev/sdd2      ext4         110G  8.0G   96G   8% /
securityfs     securityfs      0     0     0    - /sys/kernel/security
tmpfs          tmpfs        7.9G   54M  7.8G   1% /dev/shm
tmpfs          tmpfs        5.0M  4.0K  5.0M   1% /run/lock
tmpfs          tmpfs        7.9G     0  7.9G   0% /sys/fs/cgroup
cgroup2        cgroup2         0     0     0    - /sys/fs/cgroup/unified
cgroup         cgroup          0     0     0    - /sys/fs/cgroup/systemd
pstore         pstore          0     0     0    - /sys/fs/pstore
efivarfs       efivarfs        0     0     0    - /sys/firmware/efi/efivars
bpf            bpf             0     0     0    - /sys/fs/bpf
cgroup         cgroup          0     0     0    - /sys/fs/cgroup/devices
cgroup         cgroup          0     0     0    - /sys/fs/cgroup/freezer
cgroup         cgroup          0     0     0    - /sys/fs/cgroup/pids
cgroup         cgroup          0     0     0    - /sys/fs/cgroup/perf_event
cgroup         cgroup          0     0     0    - /sys/fs/cgroup/cpu,cpuacct
cgroup         cgroup          0     0     0    - /sys/fs/cgroup/rdma
cgroup         cgroup          0     0     0    - /sys/fs/cgroup/memory
cgroup         cgroup          0     0     0    - /sys/fs/cgroup/net_cls,net_prio
cgroup         cgroup          0     0     0    - /sys/fs/cgroup/cpuset
cgroup         cgroup          0     0     0    - /sys/fs/cgroup/blkio
systemd-1      -               -     -     -    - /proc/sys/fs/binfmt_misc
mqueue         mqueue          0     0     0    - /dev/mqueue
debugfs        debugfs         0     0     0    - /sys/kernel/debug
hugetlbfs      hugetlbfs       0     0     0    - /dev/hugepages
/dev/sde2      ext4         720G  516M  682G   1% /Recovery
/dev/md0       ext4         196G   62M  186G   1% /Shared
/dev/md1       ext4         720G  160G  524G  24% /Storage
/dev/sdd1      vfat         285M  5.1M  280M   2% /boot/efi
/dev/sde3      ext4          55G  3.4G   49G   7% /home
tmpfs          tmpfs        1.6G   12K  1.6G   1% /run/user/1000
fusectl        fusectl         0     0     0    - /sys/fs/fuse/connections
tmpfs          tmpfs        1.6G     0  1.6G   0% /run/user/0
sunrpc         rpc_pipefs      0     0     0    - /run/rpc_pipefs
nfsd           nfsd            0     0     0    - /proc/fs/nfsd
binfmt_misc    binfmt_misc     0     0     0    - /proc/sys/fs/binfmt_misc

sholzy
Gambas One Site Director

To report bugs in the Gambas IDE:
Official Gambas Bug Tracker
Online now: No Back to the top
1 guest and 0 members have just viewed this.