Class MountPoint
In: mount_point.rb
Parent: Object

MountPoint

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

Methods

add   check_mounted   debug   get_smbpasswd   mount   new   samba   smbmount   smbumount   umount  

Attributes

@smbopts  [R] 
mountlist  [R] 

Public Class methods

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

[Source]

     # File mount_point.rb, line 218
218:   def initialize(device=nil, dir=nil, writable=nil, fstype=nil)
219:     debug(device, dir, writable, fstype)
220: 
221:     mountitem = MountItem.new(device, dir, writable, fstype)
222: 
223:     @mountlist = Array.new
224:     @mountlist.push(mountitem) if mountitem.device
225: 
226:     self.samba(nil, nil, "")
227: 
228:     debug(@mountlist)
229:   end

Public Instance methods

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

[Source]

     # File mount_point.rb, line 238
238:   def add(device=nil, dir=nil, writable=nil, fstype=nil)
239:     debug(device, dir, writable, fstype)
240: 
241:     mountitem = MountItem.new(device, dir, writable, fstype)
242:     @mountlist.push(mountitem) if mountitem.device
243: 
244:     debug(@mountlist)
245:   end

設定された全ての device が既にマウント済みかどうかをチェックする。 all に true を与えた場合、一つでもマウントされていないものがあれば、 false を返す。全てがマウントされていれば true を返す。 一方、all に false を与えた場合、一つでもマウントされていれば、 true を返す。一つもマウントされていなければ false を返す。 (これは、「全てアンマウントされているか」を調べるのに有効である)

quiet に false を渡すと、all が true の際はマウントされていないもの に関して、all が false の場合はマウントされているものに関して メッセージを表示する。

[Source]

     # File mount_point.rb, line 404
404:   def check_mounted(all=true, quiet=true)
405:     allmounted    = true
406:     partlymounted = false
407: 
408:     @mountlist.each{ |mountitem|
409:       if mountitem.check_mounted(all, quiet) then
410:         partlymounted = true
411:       else
412:         allmounted = false
413:       end
414:     }
415: 
416:     debug(allmounted, partlymounted)
417:     return allmounted    if all
418:     return partlymounted unless all
419:   end

マウントを実行する、noexec に true を与えると、実際にはコマンドを 実行せず、実行するコマンドを標準出力に出力する。

[Source]

     # File mount_point.rb, line 284
284:   def mount(noexec=nil)
285:     @mountlist.each{ |mountitem|
286:       next if mountitem.check_mounted
287: 
288:       cmdstr   =  "/usr/bin/env mount"
289:       cmdstr   << " #{mountitem.mount_opt_fstype}"
290: 
291:       debug(mountitem.mount_opt_fstype.chomp.strip)
292:       if "-t smbfs" == mountitem.mount_opt_fstype.chomp.strip
293:         cmdstr << " #{@smbopts}"
294:         cmdstr << ",#{mountitem.mount_opt_readable}"
295:       else
296:         cmdstr << " -o #{mountitem.mount_opt_readable}"
297:       end
298: 
299:       cmdstr   << " #{mountitem.device} #{mountitem.dir}"
300: 
301:       debug(cmdstr)
302:       if /^(.*?)password=.*?,(.*)$/ =~ cmdstr then
303:         cmdstr_nopass = $1 + "password=********," + $2
304:       else
305:         cmdstr_nopass = cmdstr
306:       end
307: 
308:       debug("uid=", Process.uid, " euid=", Process.euid)
309:       print "#{cmdstr_nopass}\n"
310:       system "#{cmdstr}" unless noexec
311:     }
312:   end

Samba マウントを行うよう、オプションを設定する。

[Source]

     # File mount_point.rb, line 251
251:   def samba(username=nil, uid=nil, password=nil ,
252:             codepage=nil, iocharset=nil )
253:     smb_opt_username  = (username)  ? username.chomp.strip  : ENV["USER"]
254:     smb_opt_uid       = (uid)       ? uid   : Etc.getpwuid(Process.uid).uid
255: 
256:     smb_opt_codepage  = (codepage)  ? codepage.chomp.strip  : "cp932"
257:     smb_opt_iocharset = (iocharset) ? iocharset.chomp.strip : "euc-jp"
258: 
259:     smb_opt_password  = (password)  ? password.chomp.strip  : get_smbpasswd(smb_opt_username)
260: 
261:     @smbopts   =  "-o"
262:     @smbopts   << " username=#{smb_opt_username}"
263:     @smbopts   << ",password=#{smb_opt_password}"
264:     @smbopts   << ",uid=#{smb_opt_uid},gid=#{smb_opt_uid}"
265:     @smbopts   << ",iocharset=#{smb_opt_iocharset}"
266:     @smbopts   << ",codepage=#{smb_opt_codepage}"
267:     @smbopts   << ",fmask=664,dmask=755"
268: 
269:     debug(@smbopts)
270:   end

smbmount を実行する、noexec に true を与えると、実際にはコマンドを 実行せず、実行するコマンドを標準出力に出力する。 結果的には MountPoint.mount メソッドと変わらないが、 このメソッドは一般ユーザでも実行可能である。

[Source]

     # File mount_point.rb, line 339
339:   def smbmount(noexec=nil)
340:     @mountlist.each{ |mountitem|
341:       next if mountitem.check_mounted
342: 
343:       unless "-t smbfs" == mountitem.mount_opt_fstype.chomp.strip then
344:         print "Not Mount MountItem [\"#{mountitem.device}\" ->"
345:         print " \"#{mountitem.dir}\"] so it is not \"smbfs\". "
346:         return nil
347:       end
348: 
349:       cmdstr   =  "/usr/bin/env smbmount"
350:       cmdstr   << " #{mountitem.device} #{mountitem.dir}"
351:       cmdstr   << " #{@smbopts}"
352:       cmdstr   << ",#{mountitem.mount_opt_readable}"
353: 
354:       debug(cmdstr)
355:       if /^(.*?)password=.*?,(.*)$/ =~ cmdstr then
356:         cmdstr_nopass = $1 + "password=********," + $2
357:       else
358:         cmdstr_nopass = cmdstr
359:       end
360: 
361:       debug("uid=", Process.uid, " euid=", Process.euid)
362:       print "#{cmdstr_nopass}\n"
363:       system "#{cmdstr}" unless noexec
364:     }
365:   end

アンマウントを実行する、noexec に true を与えると、実際にはコマンドを 実行せず、実行するコマンドを標準出力に出力する。

[Source]

     # File mount_point.rb, line 372
372:   def smbumount(noexec=nil)
373:     @mountlist.each{ |mountitem|
374:       next unless mountitem.check_mounted(true)
375: 
376:       unless "-t smbfs" == mountitem.mount_opt_fstype.chomp.strip then
377:         print "Not Uount MountItem [\"#{mountitem.device}\" ->"
378:         print " \"#{mountitem.dir}\"] so it is not \"smbfs\". "
379:         return nil
380:       end
381: 
382:       cmdstr =  "/usr/bin/env smbumount"
383:       cmdstr << " #{mountitem.dir}"
384: 
385:       debug(cmdstr)
386:       debug("uid=", Process.uid, " euid=", Process.euid)
387:       print "#{cmdstr}\n"
388:       system "#{cmdstr}" unless noexec
389:     }
390:   end

アンマウントを実行する、noexec に true を与えると、実際にはコマンドを 実行せず、実行するコマンドを標準出力に出力する。

[Source]

     # File mount_point.rb, line 319
319:   def umount(noexec=nil)
320:     @mountlist.each{ |mountitem|
321:       next unless mountitem.check_mounted(true)
322: 
323:       cmdstr =  "/usr/bin/env umount"
324:       cmdstr << " #{mountitem.device}"
325: 
326:       debug(cmdstr)
327:       debug("uid=", Process.uid, " euid=", Process.euid)
328:       print "#{cmdstr}\n"
329:       system "#{cmdstr}" unless noexec
330:     }
331:   end

Private Instance methods

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

[Source]

     # File mount_point.rb, line 204
204:   def debug(*args)
205:     p [caller.first, *args] if $DEBUG
206:   end

[Source]

     # File mount_point.rb, line 272
272:   def get_smbpasswd(username)
273:     print "Password for \"#{username}\" : "
274:     password = $stdin.getshide.chomp
275:     print "\n"
276:     return password
277:   end

[Validate]