Class MountItem
In: mount_point.rb
Parent: Object

MountItem

 デバイスとマウント先のディレクトリ、デバイスのファイルシステム、
 および書き込み情報を格納。

Methods

check_mounted   chompslash   debug   new  

Attributes

device  [R] 
dir  [R] 
mount_opt_fstype  [R] 
mount_opt_readable  [R] 

Public Class methods

device にマウントするデバイス、もしくは共有フォルダを与える。 device に String クラス以外のものや空白のみのものは無視される。 dir にはマウント先のディレクトリを与える。与えない場合には "/mnt" +「device の basename 部分」を設定する。 writable には読み込み専用ならば nil を、書き込みも可能とするならば true を与える。デフォルトは nil である。

[Source]

     # File mount_point.rb, line 109
109:   def initialize(device=nil, dir=nil, writable=nil, fstype=nil)
110:     debug(device, dir, writable, fstype)
111: 
112:     if !device.instance_of?(String) || !(/\w+/ =~ device.chomp.strip) then
113:       @device             = nil
114:       @dir                = nil
115:       @fstype             = nil
116:       @mount_opt_readable = nil
117:       return
118:     else
119:       @device  = device.chomp.strip
120:     end
121: 
122:     if !dir.instance_of?(String) then
123:       @dir = "/mnt/" + File.basename(@device)
124:     else
125:       @dir = dir.chomp.strip
126:     end
127: 
128:     if !writable then
129:       @mount_opt_readable = "ro"
130:     else
131:       @mount_opt_readable = "rw"
132:     end
133: 
134:     if !fstype.instance_of?(String) then
135:       @mount_opt_fstype = "-t smbfs"
136:     else
137:       if /^.*(-t)\s+.*$/ =~ fstype.chomp.strip
138:         @mount_opt_fstype = fstype.chomp.strip
139:       else
140:         @mount_opt_fstype = "-t " + fstype.chomp.strip
141:       end
142:     end
143: 
144:     debug(@device, @dir, @mount_opt_readable, @mount_opt_fstype)
145:   end

Public Instance methods

device が既にマウント済みかどうかをチェックする。 マウントされていれば、マウント先のディレクトリと fstype を配列にして 返し、されていなければ false を返す。 expected に true を渡すと、マウントされていない場合にメッセージを 表示し、false を渡すと、マウントされている場合にメッセージを 表示する。quiet に true を渡すとメッセージを表示しない。

[Source]

     # File mount_point.rb, line 155
155:   def check_mounted(expected=nil, quiet=nil)
156:     deviceitem = chompslash(@device)
157:     debug("check #{deviceitem} is mounted.")
158:     mounted = false
159:     open("/etc/mtab", "r") { |etc_mtab|
160:       while line = etc_mtab.gets
161:         debug(line)
162:         if /^#{deviceitem}\s+(.+?)\s+(.+?)\s+/ =~ line then
163:           print "\"#{deviceitem}\" is already mounted.\n" unless quiet || expected
164:           dir, fstype = $1, $2
165:           debug(dir, fstype)
166:           mounted = [dir, fstype]
167:         end
168:       end
169:     }
170:     print "\"#{deviceitem}\" is not mounted.\n" if !quiet && expected && !mounted 
171:     debug(mounted)
172:     return mounted
173:   end

Private Instance methods

ファイル名の末尾のスラッシュを取り除く。同時に、末尾に改行が ある場合は改行も取り除く。末尾に改行が無く、またスラッシュも ついていない場合は代入された文字列をそのまま返す。

[Source]

     # File mount_point.rb, line 180
180:   def chompslash(filename)
181:     filename.chomp
182:     if /.*\/$/ =~ filename then
183:       len = filename.length
184:       len -= 1
185:       filename = filename.unpack("a#{len}")
186:     end
187:     return filename
188:   end

デバッグ出力用メソッド。組み込み関数 $DEBUG が真の場合 (つまり、 プログラムを $ ruby -d ./xxxxxx.rb として呼び出した場合) に debug メソッドに代入された変数を出力する。

[Source]

    # File mount_point.rb, line 95
95:   def debug(*args)
96:     p [caller.first, *args] if $DEBUG
97:   end

[Validate]