1#
2# This function attempts to keep track of temp mount points on a Linux
3# system.  We depend on /etc/mtab so if that file does not exist, we bail.
4# Only mount points that are not mounted at boot time or are on another
5# system are found, the assumption being that those mounted at boot time
6# and not remote will stick around and  what we really want is to keep
7# track of the ones that are temporary.
8
9# proc getTempMounts returns a list of temp mount points sorted in
10# decreasing order. This makes testing a given dir go like so:
11#
12# foreach mp [getTempMounts] {
13#    if {[regexp "^${mp}(\$|/.*)" $file] $testdir]} {
14#        <found>
15#    }
16# }
17#
18# The following two lines are for auto_mkindex ...
19proc getTempMounts {} {return {}}
20proc isTempMountPt {f} {return 0}
21
22if {$::tcl_platform(platform) == "windows"} {
23  proc getTempMounts {} {return {}}
24  proc ifTempMountPt {f} {return 0}
25} else {
26  namespace eval getTempMounts {
27    variable fstabList
28    variable fstabList
29    namespace export
30    set err {}
31    set merr {}
32    proc newList {} {
33      variable fstabList
34      # darn, work to do...
35      set mountList {}
36      # do we have the fstab list?
37      if {![info exists fstabList]} {
38	if {[catch {set cntrl [split [read [open /etc/fstab r]] \n]}] != 0} {
39	  return {}
40	}
41	set fstabList {}
42	foreach ln $cntrl {
43	  lassign $ln sys mp fs ops
44	  if {$sys == {} || [string range $sys 0 0] == "#"} {continue}
45	  if {[string match "*noauto*" $ops]} {continue}
46	  if {$fs in {cifs nfs smbfs ncpfs fuse.sshfs}} {continue}
47	  lappend fstabList $mp
48	}
49      }
50      #puts "fstab: $fstabList"
51      # we always read mtab as there is no way to detect changes otherwise.
52      set mountList {}
53      if {[catch {set cntrl [split [read [open /etc/mtab r]] \n]}] != 0} {
54	return {}
55      }
56      foreach ln $cntrl {
57	lassign $ln sys mp dm ops
58	if {$sys == {} || [string index $sys 0] == "#"} {continue}
59	if {$dm in "$sys nfsd rpc_pipefs" ||
60	    [string match "/proc/*" $mp] ||
61	    $mp in $fstabList } {continue}
62	#puts "yes: $sys $dm $mp"
63	lappend mountList $mp
64      }
65      return [lsort -decreasing -unique $mountList]
66    }
67  }
68  #
69  proc getActiveMounts {} {
70    return [::getTempMounts::newList]
71  }
72  proc isTempMountPt {file} {
73    foreach mp [::getTempMounts::newList] {
74      if {[regexp "^${mp}(\$|/.*)" $file]} {return $mp}
75    }
76    return {}
77  }
78}
79