From: Mitsuru Ogino Date: 2003-08-15T01:36:00+09:00 Subject: [ruby-list:38114] IO の close 荻野です。 On 2003.8.14, at 13:52 Asia/Tokyo, Minero Aoki wrote: > あと、#finish で IOError を投げたりしますけど、 これで思い出したんですが、前から疑問だったんで、この機会に質問 させてください。 リファレンスマニュアルには、 close 入出力ポートをクローズします。以後のこのポートに対する入 出力操作は例外を発生します。ガーベージコレクトの際にはク ローズされていないIOポートはクローズされます。 Traps:closeをGCにまかせる self がパイプでプロセスにつながっていれば、そのプロセス の終了を待ち合わせます。 nil を返します。 とのみあり、close の失敗時については触れていません。が、例えば Debian GNU/Linux Woody の man 2 close には、 RETURN VALUE close returns zero on success, or -1 if an error occurred. (snip) NOTES Not checking the return value of close is a common but nevertheless serious programming error. File system implementations which use techniques as ``write-behind'' to increase performance may lead to write(2) succeeding, although the data has not been written yet. The error status may be reported at a later write operation, but it is guaranteed to be reported on closing the file. Not checking the return value when closing the file may lead to silent loss of data. This can especially be observed with NFS and disk quotas. A successful close does not guarantee that the data has been successfully saved to disk, as the kernel defers writes. It is not common for a filesystem to flush the buffers when the stream is closed. If you need to be sure that the data is physically stored use fsync(2) or sync(2), they will get you closer to that goal (it will depend on the disk hardware at this point). とありますが、IO の close は nil を返すとのみあります。 もし、IOError とかを発生させないのであれば、Ruby ではNFS や quota が絡むと IO は安心して使えないということになりはしないでしょうか。 例外があがるのであれば、これまたリファレンスマニュアルに明記してあ ると助かります。 qmail の Maildir 関連を読んだのがきっかけです。これらによると fsync(2) して返値をチェックして close(2) して返値をチェックすべき、 とあります。 また Perl (5.005/5.6.1) で man perlfunc すると、 close FILEHANDLE close Closes the file or pipe associated with the file handle, returning true only if stdio successfully flushes buffers and closes the system file descriptor. とあり、やはり成功かどうかを返すようです。 # もちろん、Ruby では例外の方が良いと思いますが。 すみませんが、気になるので教えていただければ幸いです。 -- 荻野 充 (おぎの みつる) ... 「萩(はぎ)」にあらず Verama Systems Key fingerprint = 7F26 5414 1805 F31B 1617 10B7 C117 07AE 1691 9BD1