2010/02/26

UTF8-MAC [2]

UTF8-MAC みなおし。

要は \\ があればいいんでしょ?とお気楽な発想を展開してみました。
が、以下は非常によろしくないことをしている可能性あり。
あくまでも実験なので試さない方がいいです。おい

macustr.rb

  1 #!/usr/local/bin/ruby19
  2 # coding: utf-8
  3 
  4 exit unless Encoding.default_external.name == 'UTF-8'
  5 
  6 require 'nkf'
  7 def macustr(line)
  8   str = String.new # p str.encoding
  9   line.chars.entries.each{|x| str << "\\" + x}
 10   #line.codepoints.entries.each{|x| str << "\\" + [x].pack("U*")}
 11   mustr = NKF::nkf('--ic=UTF-8 --oc=UTF8-MAC', str)
 12   return mustr
 13 end
 14 
 15 ARGF.each{|line|
 16   exit unless line.encoding.name == "UTF-8"
 17   next unless line.valid_encoding?
 18   mustr = macustr(line.strip)
 19   next unless  mustr.valid_encoding?
 20   sh = system("mkdir #{mustr}")
 21   print "Maked Directory: #{line}" if sh
 22 } 
 23 


line 4 : 環境変数LANGが UTF-8 でない場合はおいかえす。
line 16: 読み込んだ文字列が UTF-8 でなければすぐ終了。
line 17: valid でなければ次
line 18: def macustr に一行わたす
line 9 : 冒頭に"\\" を付け加えて一文字ずつ str に追加。最初は line 10 にしたのだが line 9 にしておいた。
line 11: str を UTF8-MAC に変換
line 19: valid でなければ次
line 20: mkdir コマンド
line 21: コマンドが成功したらメッセージ表示

./macustr.rb utf8_text.txt

を実行すると utf8_text.txt を一行ずつ読み込み
同名のDirectory を同じ階層に作成します。
utf8_text.txt は UTF-8 で保存されたテキストファイル。

[2010-02-27]
s/chomp/strip/g

[2010-03-08]
追記
# 失敗例

  1 #!/usr/local/bin/ruby19
  2 # coding: utf-8
  3 # test-encode.rb
  4 
  5 text = 'utf8-text.txt'
  6 # % cat utf8-text.txt 
  7 # and two &three
  8 
  9 File.open(text, "r:utf-8"){|f|
 10   f.each_line{|x|
 11     #  行末\n や行頭\s を削除
 12     line = x.strip
 13     # print out
 14     puts "Line: \"#{line}\"| Encoding: #{line.encoding}| Valid: #{line.valid_encoding?}\n"
 15     puts "# Calling encode\n"
 16     # encode で変換
 17     mustr = line.encode("UTF8-MAC")
 18     # print out
 19     puts "String: \"#{mustr}\"| Encoding: #{mustr.encoding}| Valid: #{mustr.valid_encoding?}\n"
 20     print "# Calling system\n"
 21     # Directory 作成
 22     sh = system("mkdir #{mustr}")
 23     puts "Result: #{sh}\n--\n"
 24   }
 25 }
 26 
 27 =begin
 28 % ./test-encode.rb 
 29 Line: "and two &three"| Encoding: UTF-8| Valid: true
 30 # Calling encode
 31 String: "and two &three"| Encoding: UTF8-MAC| Valid: true
 32 # Calling system
 33 sh: three: command not found
 34 Result: false
 35 
 36 % ls -F
 37 and/            test-encode.rb* two/            utf8-text.txt
 38 =end


17行目

mustr = ine.encode("UTF8-MAC")

mustr = line.force_encoding("UTF8-MAC")

にしても同様の結果だし

require 'nkf'
mustr = NKF::nkf('--ic=UTF-8 --oc=UTF8-MAC', line)

としてみても結果は同じ

検索して次を読んでみたけど よくわかってないというか...。
<http://redmine.ruby-lang.org/issues/show/1411>
<http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-dev/40233?help>

+++ 感想 +++

通常 Mac OS X を使用する上で自ら日本語名のフォルダを作る人はあまりいないだろうし、スペースが混入した文字列をわざわざフォルダの名前にする人もあまりいないはずなので、じゃあ一体どういった場面で上のような必要が生じるかというと
ずばり
iTunes のための iTunes による Folder 生成 | File 生成なのかな? と。
2010/03/08 現在ではこれ以外の場面で UTF8-MAC への変換なんて一般的には必要とされてないことなのかもしれません。

ただ、今後 iPad が登場しまた新しくiのつく名前のアプリケーションがでてきたりすると UTF8-MAC への変換に対する需要は増えるかもしれませんね?電子書籍を買って読むための iBooks.app が OS X に搭載される日は案外近い将来かもしれません。
iPad

それで、例えば iBooks.app が生成するであろう著書名のついたファイルにアクセスする際、もしも著書名に\& や\s が含まれていたり日本語だったりしたらRuby からそれらのファイルにアクセスしようとするのはちょっと困難がありそうです。いまの iTunes ファイル群と同様に。
# Ruby からはそういうファイル群にアクセスしないから大丈夫...といった意見の人もいるかもしれません。
# あるいは上に書いた失敗例を[ちゃんとした]成功例にする方法を私が知らないだけのことなのかもしれません。
# macustr.rb は一見成功してるようにみえるけどもちゃんとはしていないだろうし。

はたまた違う観点からみた場合、セキュリティ的にどうなんだろう?って問題も私にはよくわからないけどきっとあるんだろうと思います。そっちが優先された上で現在こうなっているのであればこのままの方がうれしい人も多いのかもしれません?。

ということで、幾日経過後のまとまりのない&とりとめないのない感想でした。

# 私が間違った認識をしている可能性は多分にあります。何かお気付きの方はコメントお願いします。

2010/02/24

mdiary 最近の変更点

  5     def initialize(arg)
  6       @err = false
  7       k, @h = nil, Hash.new
  8       arg.each{|x| m = /^-(.*)/.match(x); (m) ? (k = m[1].to_sym; @h[k] = nil) : @h[k] ||= x}
  9     end


arg は ARGV の値

% .mdiary-run.rb -d 2010-01 -s music

を実行した場合 arg は

arg = ["-d", "2010-01", "-s", "music"]

になる。line 8 はこの配列を

@h = {:d=>"2010-01", :s=>"music"} 

のようなハッシュにするためのもの。

arg = ["-d", "2010-01", "-l", "3"] だった場合
@h={:d=>"2010-01", :l=>"3"}

arg = ["-d", "2010-01", "-l"] だった場合
@h={:d=>"2010-01", :l=>nil}
になる。

line 7-8 をくだいて書くと

  1 # arg = ["-d", "2010-01", "-s", "music"]
  2 k, @h = nil, Hash.new
  3 arg.each{|x| 
  4   m = /^-(.*)/.match(x)
  5   if m
  6     k = m[1].to_sym
  7     @h[k] = nil
  8   else
  9     @h[k] ||= x
 10   end
 11 } 


line 4: x が '-' で始まっていたら m の値は #<MatchData "-d" 1:"d">
line 5: m の値に何か入っていたら(マッチしたら)
line 6: k に m の1番目のマッチの値を代入。
line 7: ハッシュキーが k で 値が nil のペアを @h に格納。
line 8: m の値に何もはいっていなければ(マッチしなかったら)
line 9: @h[k] が nil の場合は x を代入。@h[k] の値が nil でなく何か値が入っていたら何もしない。

+++ memo +++

1) @h[k] ||= x

line 9 の @h[k] ||= x を @h[k] = x にしてしまうと
あやまって
arg = ["-d", "2010-01", "xxx", "-s", "music"]
が与えられてしまった時
@h = {:d=>"xxx", :s=>"music"}
になってしまう。これでは意図した結果と異なる為 ||= にしている。

2) k = nil

line 2 の k = nil を書き忘れ
line 2 は @h = Hash.new だけだった場合
arg = ["-d", "2010-01", "-s", "music"] を与えると
@h={:d=>nil, nil=>"2010-01", :s=>nil}
になってしまう。
なぜなら...
{} の外側に k が存在していなければ k は {} の中だけで使える変数となり
m == nil でなければ
k = m[1].to_sym なので k には m[1].to_sym が代入されるが

m == nil だった場合、すなわち else 節においては
k = nil が代入されてしまうから。そして
line 9: @h[k] ||= x
で nil を ハッシュキーとしたペア(nil=>"2010-01")を @h に格納することになってしまう。

だから{} の外側の k = nil はとても大切。
外側で定義された k があることによって else 節においても k は m[1].to_sym を保持していてくれる。

[2010-02-26]
arg.each{|x| m = /(^-(.*))/.match(x); (m) ? (k = m[2].to_sym; @h[k] = nil) : @h[k] ||= x}
arg.each{|x| m = /^-(.*)/.match(x); (m) ? (k = m[1].to_sym; @h[k] = nil) : @h[k] ||= x}

2010/02/22

UTF8-MAC

ここを読んで

私も perl でためしてみました。
% cat test.pl

#!/usr/bin/perl
while(<>) {
    chop;
    print "Making a folder $_...\n";
    mkdir();
}

% cat 02.txt

ABC_a'bc 織田 信長
ABC_e'fg 豊臣 秀吉
ふげふが リファレンス& ドロー


% ./test.pl 02.txt
Making a folder ABC_a'bc 織田 信長...
Making a folder ABC_e'fg 豊臣 秀吉...
Making a folder ふげふが リファレンス& ドロー...

わざと\s や ' や濁点や & を入れてみましたがちゃんとおかしな名前のフォルダが出来上がりました。

で、Ruby ではどうなのでしょ。
% cat test.rb

#!/path/to/ruby19
# coding: utf-8
ARGF.each{|line|
  system ("mkdir #{line}")
}


% ./test.rb 02.txt

% ./test.rb 02.txt 
sh: -c: line 0: unexpected EOF while looking for matching `''
sh: -c: line 1: syntax error: unexpected end of file
sh: -c: line 0: unexpected EOF while looking for matching `''
sh: -c: line 1: syntax error: unexpected end of file
sh: ドロー: command not found

こうなるのは予測していたので

http://gihyo.jp/dev/serial/01/ruby/0004?page=2 を読み直し

#!/path/to/ruby19
# coding: utf-8
require 'nkf'
ARGF.each{|line|
  ustr = NKF::nkf('--ic=UTF-8 --oc=UTF8-MAC', line)
  system ("mkdir #{ustr}")
}

nkf を使ってみました。が、最初と同じエラーが現れ ふげふが/ リファレンス/ の2のフォルダができました。

結果をみると \s のところでパスが区切られてしまったようなので
gustr = ustr.gsub(/\s/, "\\ ")
を挟んでみたものの ”&” でまたパスを区切られてしまいました。結局エラーになりそうな文字(て何?)を全て gsub しないとだめなのかもしれません。いやいやそんなことじゃなく正しい指定(nkf の)さえ分かればいいだけのことなのかもしれません。
あーわからない。

このままで話が終わるのはなんだか後味が悪いので AppleScript で Ruby を呼んでみました。

tell application "Finder"
  activate
  set cf to choose file with prompt "読み込みたいテキストファイル_UTF-8_を選択してください。"
  set dst to choose folder with prompt "保存先フォルダを選択してください。"
  set f to quoted form of POSIX path of cf
  set rubyfile to quoted form of POSIX path of (((folder of file cf) as text) & "ioread.rb")
  # ---------------------------
  try
    set str to do shell script "/usr/bin/ruby" & space & rubyfile & space & f
    set ary to paragraphs of str
    if (count of ary) > 3 then # this line
      return display dialog "体力の限界です。" buttons {"OK"} default button 1 with title "Oh, NO "
    end if
    repeat with fname in ary
      make new folder at (dst) with properties {name:fname}
    end repeat
  on error error_str
    display dialog error_str buttons {"OK"} default button 1 with title "Error"
  end try
end tell
# return AppleScript's version => "2.1.1"

% cat ioread.rb 
#!/usr/bin/ruby
f = ARGV[0]
print nil unless f
ary = IO.readlines(f)
ary.delete("\n")
print ary

% ls -F
ABC_a'bc 織田 信長/                                  ふげふが リファレンス& ドロー/
ABC_e'fg 豊臣 秀吉/


ちゃんとおかしな名前のフォルダができました。
テストなんでフォルダの数は3つまでに制限かけておきました。# this line のところ。


+++ 追記 +++
2010-02-22
しばらくしたら思いつきました。文字ごとに gsub してみようと。

% cat 02.txt

ABC_&a'bc 織田 信長
ABC_e'fg 豊臣 秀吉
ふげふが リファレンス& ドロー


test.rb

  1 #!/path/to/ruby19
  2 # coding: utf-8
  3 
  4 require 'nkf'
  5 def ex(line)
  6   mu = String.new("")
  7   ary = line.each_char{|x|
  8     gx = x.gsub(
  9       /[\s\'\(\)\+\!\&\,\[\]\;]/,
 10       " "=>"\\ ", "'"=>"\\'",
 11       "("=>"\\(", ")"=>"\\)",
 12       "+"=>"\\+", "!"=>"\\!",
 13       "&"=>"\\&", "."=>"\\.",
 14       "ï"=>"\\i", ","=>"\\,",
 15       "["=>"\\[", "]"=>"\\]",
 16       ";"=>"\\;"
 17     ) 
 18     mu << gx
 19   } 
 20   p macustr = NKF::nkf('--ic=UTF-8 --oc=UTF8-MAC', mu)
 21   p macustr.encoding
 22   system ("mkdir #{macustr}")
 23 end
 24 
 25 ARGF.each{|line| ex(line)}


% ./test.rb 02.txt

"ABC_\\&a\\'bc\\ 織田\\ 信長"
#<Encoding:UTF8-MAC>
"ABC_e\\'fg\\ 豊臣\\ 秀吉"
#<Encoding:UTF8-MAC>
"ふげふが\\ リファレンス\\&\\ ドロー"
#<Encoding:UTF8-MAC>


% ls -F

02.txt                                               test.rb*
ABC_&a'bc 織田 信長/                                 ふげふが リファレンス& ドロー/
ABC_e'fg 豊臣 秀吉/


line 10 "'" を "\\'" とスラッシュバックスラッシュ2つにしてみたらうまくいった。それがなぜなのかは分かっていない。
こういうのは力技と呼ばれることなんだろう。

UTF8-MAC [2] へ続く


[2011-02-19]

2010-02 時点での敗因はただ単にchomp しわすれとsystem コマンド発行する引数を'' で囲まなかったこと。それだけ。びっくりする愚かなおち。
nkf とかね関係ありません...。
それにしても今読み返してつくづく...単純ミスから泥沼していく様は自分でもあきれました。

% cat a.txt
ふげふが &どろー
% cat test.rb
# coding: utf-8

ARGF.each{|x|
  s =  "\'#{x.strip}\'"
  p sh = system("mkdir #{s}")
}
% ruby -v test.rb a.txt
ruby 1.8.7 (2009-06-12 patchlevel 174) [universal-darwin10.0]
true
% ls
a.txt                            test.rb                          ふげふが &どろー


ruby 1.9.2
でも同様の結果。

2010/02/17

git push error

% git push origin master
を実行したら

 ! [rejected]        master -> master (non-fast-forward)

error: failed to push some refs to (...略) 
To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes before pushing again.  See the 'non-fast-forward'
section of 'git push --help' for details.


となってしまった。思い当たることとしては OS を再インストールして git repository へのパスを変えてしまったことと git のバージョンをあげたこと。

% git push --help

/non-fast-forward
next していたら次のような文章があった。

       There is another common situation where you may encounter non-fast-forward rejection when you try to push, and it is possible even
       when you are pushing into a repository nobody else pushes into. After you push commit A yourself (in the first picture in this
       section), replace it with "git commit --amend" to produce commit B, and you try to push it out, because forgot that you have pushed A
       out already. In such a case, and only if you are certain that nobody in the meantime fetched your earlier commit A (and started
       building on top of it), you can run "git push --force" to overwrite it. In other words, "git push --force" is a method reserved for a
       case where you do mean to lose history.

辞書をひきつつ 2 回読んでみたけどもなぜこうなってしまったのか?や、どうすればいいのか?は読み解けない。

Google にて "non-fast forward"
を検索してみたところ
http://blog.digital-squad.net/article/132085683.html

こういうときは --forceオプションをつけてやることで強制的にpushできる。
git push origin master --force

とありまして --force をつけて試してみるとあっさり git push に成功しました。とても助かりました。
一時は ssh-keygen で鍵を作りなおさなくちゃいけないのかと思った。

さて、落ち着いてから上の英文を読みなおすと

you can run "git push --force" to overwrite it. In other words, "git push --force" is a method reserved for a
       case where you do mean to lose history.

... "git push --force" はあなたが歴史を失うケースに備えての予備のメソッド?
間違えて読んでる可能性大だけど歴史を失うとはどういうことだろ。
OS 再インストールしてパスが変わってるから何かが失われたってことなのかな。何が?

% git log
ちゃんと動いてる。最初からのlog もみえてる。はて?

+++ 教訓 +++

% git push midore@github.com:midore/mblogger.git

などしても

...
Permission denied (publickey).
fatal: The remote end hung up unexpectedly

エラーになる。だからといって ~/.ssh/ の中の鍵のパーミションが関係してる?とか考えない。

remote 先を明示したい時は

% git push git@github.com:midore/mblogger.git


~/.gitconfig や ~/.ssh/ にゆるぎない自信があれば

% git push origin master

で OK.

[後日談]
入門Git(秀和システム) を読みましてこの時何がおこっていたか?を考えてみました。
p81 に non fast forward エラーについて解説がありました。

私の場合OS を入れ替える前にバックアップをとり、OS を新規インストールした後にこのバックアップから
local git repository を読み込んだわけだけどもバックアップした直後で新規インストールする直前に
github.com/midore/mdiary に対してgit push しちゃっていたのかもしれません(驚)
だとすればこの挙動はものすごく納得がいきます。
バックアップとった後に git push した記憶がない自分が怖い...

私がうっかり git push していたのならば上記の対処は正しいやり方じゃなかったようです。
% git pull
を使い
(pull 先は add remote した先を指し、clone したproject の場合は自動的にclone 元になるそうだ)
github.com においてあるリポジトリの最新コミットと手元のリポジトリのコミットをマージさせるべきでした。
git pull してみればどこで conflict していたか明らかになったはずなのでした。
(ただしgit push した記憶がない状態で pull する勇気をもてたかは不明)
conflict メッセージがでていたのならそのファイルを編集した上でcommit するべきだったのかもしれません。

2010/02/16

Remove resource fork in AppleScript

私は Twitter に参加していませんが何日か前に Google Reader で
急募: OS XのiTunesから、mp3ファイルを_リソースフォークなしに...
を見かけました。

早速 AppleScript で try

tell application "Finder"
  set src to quoted form of POSIX path of (choose folder)
  set dst to quoted form of POSIX path of (choose folder)
  set str to "ditto -v --norsrc  " & src & "  " & dst
  do shell script str
end tell

日本語名のフォルダを指定しても大丈夫でした。でもこれだけだと愛想がないのでいろいろ付け足し。

# remove-resource-forlk.scpt
tell application "Finder"
  # フォルダを選択
  set src to quoted form of POSIX path of (choose folder with prompt "リソースフォークをまるっと取り除きたいフォルダを選択してください。")
  # 保存先フォルダの選択
  set dst to quoted form of POSIX path of (choose folder with prompt "保存先であるフォルダを選択してください。")
  # コマンド文字列
  set str to "ditto -v --norsrc  " & src & "  " & dst
  if src is dst then
    set err_str to "指定したフォルダと保存先であるフォルダが同じです!異なるフォルダを選択してください。"
    display dialog err_str buttons {"OK"} default button 1 with icon 2 with title "Error"
    return
  end if
  set warn_str to "多くのファイルが含まれているフォルダを選択した場合時間がかかる(数分)場合があります。よろしいですか?"
  display dialog warn_str buttons {"OK", "Cancel"} default button 2 with icon 2 with title "Warning"
  try
  # コマンドの実行
  set res to do shell script str
  on error num
    display dialog "エラーが発生しました。" buttons {"OK"} default button 1
    return
  end try
  if res is "" then display dialog "おわりました。" buttons {"OK"} default button 1 with icon 1 with title "Success"
end tell


[用途]
フォルダに含まれるファイルからリソースフォークを削除したファイルを保存先フォルダに保存します。(もとのファイル名は維持されます。)
複数のフォルダを内包しているフォルダを指定した場合、保存先フォルダの階層は、もとのフォルダの階層を維持します。

# iTunes Music フォルダにあるミュージシャン(Artist) の名前がついたフォルダを選択すると、そこに含まれるアルバム名のついたフォルダにある音楽ファイル( 末尾に.m4a .mp3 などの名前のついたファイル)から、リソースフォークを削除したものを、保存先フォルダに保存します。
# ターミナルからコマンドを叩くことなく、操作できます。

[使い方]
Finder メニュ-> 移動-> アプリケーションを開く (Command + Shift + A でApplication フォルダ を表示)
->ユーティリティ ->AppleScript Editor.app(AppleScript エディタ.app) をダブルクリックで起動し新しいファイルを作り上記をコピー and ペーストします。

1, コマンドKey(⌘) + K でコンパイルします。
2, コマンドKey(⌘) + S で File 保存(必ずスクリプト として)
3, コマンドKey(⌘) + R で実行 です。

処理がおわると『おわりました。』と表示します。エラーが発生した場合は『エラーが発生しました。』と表示します。

複数のアルバムフォルダが含まれている ミュージシャン(Artist) のフォルダなどを選択するとファイル数にして何百曲などのファイルを処理することになります。その場合ものすごく時間がかかる(数分)こともあります。なので最初は下記の手順でテストしてみてどのくらい時間がかかるものかを体感してみてください。

[テスト手順]
(1) デスクトップに2つ新規フォルダを作りそれぞれ「SRC」「DST」と名付けておく。
(2) 「SRC」フォルダの中にリソースフォークを取り除きたいフォルダを1 つだけコピーしておく。
  その際極力ファイル数の少ないフォルダを選ぶ。
「DST」フォルダは空っぽのままにしておく。
(3) 保存しておいた AppleScript ファイルを開いてから コマンドKey(⌘) + R をおす。または「実行」ボタンを押す。
「リソースフォークをまるっと...」ときかれたら「SRC」を選ぶ。
「保存先...」 ときかれたら 「DST」を選ぶ。

処理している間はなんのメッセージも表示しません。
10 枚程のファイルを処理した場合 2,3 秒でおわります。

# 注意1:
OS X 10.5x 時代のファイルにはリソースフォークがついてるけど、Snow Leopard 時代に作ったファイルにはついてません。

# 注意2:
指定フォルダとしてミュージシャン(Artist)名のついたフォルダを指定し
保存先フォルダには適当な名前(「test」とか)のフォルダを指定し実行した場合

iTunes->詳細->”iTunes Media”フォルダを整理する
にチェックをいれてから保存先フォルダ(「test」とか)を iTunes に取り込むと本来のミュージシャン(Artist)名のついたフォルダ名に自動変換されます。
(iTunes 9.03)

[ターミナルで確認]

# % ditto -h
# % xattr -h

# 指定フォルダ: test-src
# 保存先フォルダ: test-dst


指定フォルダ内

% ls -FRl test-src/
total 0
drwx------  3 foo  staff  102  2 15 16:35 Makana/
drwx------  3 foo  staff  102  2  4 11:48 平沢 進/

test-src//Makana:
total 0
drwx------  17 foo  staff  578  2 15 16:33 Different Game/

test-src//Makana/Different Game:
total 92824
-rw-------@ 1 foo  staff  4428672  6  6  2009 01 Away.m4p
-rw-------@ 1 foo  staff   623834  2  4 18:09 02 Interlude I.m4p
-rw-------@ 1 foo  staff  3821305  6  6  2009 03 Mars Declares.m4p
# ... 略

test-src//平沢 進:
total 0
drwx------  4 foo  staff  136  2  4 11:48 時空の水/

test-src//平沢 進/時空の水:
total 15344
-rw-------@ 1 foo  staff  4542192  4  4  2008 01 ハルディン・ホテル.m4p
-rw-------@ 1 foo  staff  3311160  4  4  2008 10 金星.m4p

% xattr -x test-src/Makana/Different\ Game/01\ Away.m4p 
# => com.apple.FinderInfo

% xattr -x test-src/平沢\ 進/時空の水/01\ ハルディン・ホテル.m4p
# => com.apple.FinderInfo


AppleScipt 実行後
保存先フォルダ内

% ls -FRl test-dst/
total 0
drwx------  3 foo  staff  102  2 15 16:35 Makana/
drwx------  3 foo  staff  102  2  4 11:48 平沢 進/

test-dst//Makana:
total 0
drwx------  17 foo  staff  578  2 15 16:33 Different Game/

test-dst//Makana/Different Game:
total 92824
-rw-------  1 foo  staff  4428672  6  6  2009 01 Away.m4p
-rw-------  1 foo  staff   623834  2  4 18:09 02 Interlude I.m4p
-rw-------  1 foo  staff  3821305  6  6  2009 03 Mars Declares.m4p
# ... 略

test-dst//平沢 進:
total 0
drwx------  4 foo  staff  136  2  4 11:48 時空の水/

test-dst//平沢 進/時空の水:
total 15344
-rw-------  1 foo  staff  4542192  4  4  2008 01 ハルディン・ホテル.m4p
-rw-------  1 foo  staff  3311160  4  4  2008 10 金星.m4p

% xattr -x test-dst/Makana/Different\ Game/01\ Away.m4p
# => 何もかえってこない。

% xattr -x test-dst/平沢\ 進/時空の水/01\ ハルディン・ホテル.m4p
# => 何もかえってこない。


[2010-02-16]
s/errr_str/err_str/g
s/warm_str/warn_str/g
s/やめる/Cancel/g
[2010-02-18]
s/folk/fork/g

2010/02/11

Snow Leopard Re-Install memo

# backup
$ sudo launchctl bslist -j > default-launchctl-bslist.txt
# PRAM Clear
# $ sudo nvram -c -xp

0) Remove Extensions
# Bluetooth
$ sudo srm -rf /System/Library/Extensions/IOBluetooth*
# iSight
$ sudo srm -rf /System/Library/Extensions/Apple_iSight.kext/
$ sudo srm -rf /System/Library/Extensions/IOUSBFamily.kext/Contents/PlugIns/AppleUSBVideoSupport.kext/

# Touch
$ sudo touch /System/Library/Extensions/

# blued
$ sudo launchctl unload -w /System/Library/LaunchDaemons/com.apple.blued.plist
$ sudo chmod 600 /usr/sbin/blued

$ sudo reboot

1) mDNSResponder.plist
$ ps aux | grep mDNS
# => ... /usr/sbin/mDNSResponder -launchd

$ sudo vi /System/Library/com.apple.mDNSResponder.plist
$ sudo vi /System/Library/LaunchDaemons/com.apple.mDNSResponder.plist

        <array>
                <string>/usr/sbin/mDNSResponder</string>
                <string>-launchd</string>
                <string>-NoMulticastAdvertisements</string>
        </array>


# Reload com.apple.mDNSResponder.plist
$ sudo launchctl unload /System/Library/LaunchDaemons/com.apple.mDNSResponder.plist
$ sudo launchctl load /System/Library/LaunchDaemons/com.apple.mDNSResponder.plist

$ ps aux | grep mDNS
# => .... /usr/sbin/mDNSResponder -launchd -NoMulticastAdvertisements

See: support.apple.com/kb/HT3789

2) change mode
$ man samba
$ apropos smbd

$ sudo chmod 600 /usr/sbin/nmbd
$ sudo chmod 600 /usr/sbin/smbd
$ sudo chmod 600 /usr/sbin/cupsaddsmb

$ sudo chmod 600 /usr/bin/smb*
$ sudo chmod 600 /usr/bin/nmblookup
$ sudo chmod 600 /etc/smb.conf
$ sudo chmod 600 /etc/smb.conf.template

# httpd
$ sudo chmod 600 /usr/sbin/httpd

# sshd
$ sudo chmod 600 /usr/sbin/sshd

# ftpd
# sudo chmod 600 /usr/libexec/ftpd

# nfsd
$ sudo chmod 600 /sbin/nfsd

# Apple FileServer
# $ ls -la /usr/sbin/AppleFileServer
$ sudo chmod 0 /System/Library/CoreServices/AppleFileServer.app/Contents/MacOS/AppleFileServer

# ae server (Apple Event)
# $ more /System/Library/LaunchDaemons/com.apple.eppc.plist
$ sudo chmod 0 /System/Library/Frameworks/CoreServices.framework/Frameworks/AE.framework/Versions/A/Support/AEServer

# Problem Reporter
$ sudo chmod 600 /System/Library/CoreServices/Problem\ Reporter.app/Contents/MacOS/Problem\ Reporter

See: http://www.macosxhints.com/

# Plug-Ins
$ ls -la /Library/Internet\ Plug-Ins/
$ sudo chmod 700 /System/Library/Frameworks/JavaVM.framework/Resources/JavaPluginCocoa.bundle
$ sudo chmod 700 /Library/Internet\ Plug-Ins/Flash Player.plugin
$ sudo chmod 700 /Library/Internet\ Plug-Ins/QuickTime Plugin.plugin
$ sudo chmod 0 /Library/Internet\ Plug-Ins/NP-PPC-Dir-Shockwave
$ ls -la /Library/Internet\ Plug-Ins/

3) launchctl unload
$ sudo launchctl unload -w /System/Library/LaunchDaemons/org.cups.cupsd.plist
$ sudo launchctl unload -w /System/Library/LaunchDaemons/org.x.privileged_startx.plist
$ sudo launchctl unload -w /System/Library/LaunchDaemons/com.apple.webdavfs_load_kext.plist

# com.apple.smb.server.preferences.plist com.apple.smb.sharepoints.plist com.apple.smbfs_load_kext.plist
$ sudo launchctl unload -w /System/Library/LaunchDaemons/com.apple.smb.sharepoints.plist
$ sudo launchctl unload -w /System/Library/LaunchDaemons/com.apple.smb.server.preferences.plist
$ sudo launchctl unload -w /System/Library/LaunchDaemons/com.apple.smbfs_load_kext.plist

$ sudo launchctl unload -w /System/Library/LaunchDaemons/com.apple.RFBRegisterMDNS_ScreenSharing.plist
$ sudo launchctl unload -w /System/Library/LaunchDaemons/com.apple.RFBRegisterMDNS_RemoteManagement.plist

$ sudo launchctl unload -w /System/Library/LaunchDaemons/com.apple.printtool.plist
$ sudo launchctl unload -w /System/Library/LaunchDaemons/com.apple.portmap.plist
$ sudo launchctl unload -w /System/Library/LaunchDaemons/com.apple.nfsd.plist
$ sudo launchctl unload -w /System/Library/LaunchDaemons/com.apple.RemoteDesktop.PrivilegeProxy.plist
$ sudo launchctl unload -w /System/Library/LaunchDaemons/com.apple.IIDCAssistant.plist

# 2010-03-30
$ sudo launchctl unload -w /System/Library/LaunchDaemons/com.apple.dashboard.advisory.fetch.plist

# /System/Library/Filesystems/AppleShare/check_afp.app/Contents/MacOS/check_afp
# $ more /System/Library/LaunchDaemons/com.apple.afpfs_checkafp.plist

# /System/Library/Filesystems/AppleShare/afpLoad
# $ more /System/Library/LaunchDaemons/com.apple.afpfs_afpLoad.plist

# /System/Library/PrivateFrameworks/SoftwareUpdate.framework/Resources/suhelperd
# $ more /System/Library/LaunchDaemons/com.apple.suhelperd.plist

$ sudo launchctl bslist -j
$ sudo lsof -i

4) Spotlight
# as admin-user
$ sudo chmod 600 /System/Library/CoreServices/Search.bundle/Contents/MacOS/Search
$ sudo mdutil -i off /Volumes/abc
$ sudo mdutil -E /Volumes/abc
$ sudo mdutil -s /Volumes/abc

See: http://www.macosxhints.com/article.php?story=20091030173117381

5) fseventsd
$ sudo touch /Volumes/abc/.fseventsd/no_log
$ sudo touch /Users/foo/.fseventsd/no_log
$ sudo chmod 644 /Users/foo/.fseventsd/no_log
$ sudo chown -R foo:staff /Users/foo/.fseventsd/

See: ファイルシステムイベントストレージの防止

# default value "ttyskeepawake" of pmset is 1.
$ sudo pmset ttyskeepawake 0

See: http://forums.applenova.com/showthread.php?t=33172

$ sudo reboot
$ Login as admin-user
$ syslog -w 120 | more

# fseventsd[xx] <Critical>: Logging disabled completely for device:1: /Users/foo

6) Login as User foo
% rm ~/.fseventsd/000000*
% mdutil -i off /Users/foo
% mdutil -E /Users/foo
% mdutil -s /Users/foo
% pmset -g

% syslog -w 30
# What is this warning ?
# (com.apple.ReportCrash) <Warning>: Falling back to default Mach exception handler. Could not find: com.apple.ReportCrash.Self

% more /System/Library/LaunchAgents/com.apple.ReportCrash.Self.plist

% launchctl list | grep Repo

-	0	com.apple.ReportPanic
-	0	com.apple.ReportCrash.Self
-	0	com.apple.ReportCrash


See: http://www.insanelymac.com/forum/index.php?showtopic=192563

# User foo
% mkdir ~/Library/LaunchAgents
# if you did't set umask
# % chmod 700 ~/Library/LaunchAgents

% cd ~/Library/LaunchAgents
% cp /System/Library/LaunchAgents/com.apple.ReportCrash.Self.Plist .

# User admin
$ su admin-user
$ cd /path/to/backup-dir/
$ sudo mv /System/Library/LaunchAgents/com.apple.ReportCrash.Self.Plist .

# logout and login as User foo
% syslog -w
# not found, "... (com.apple.ReportCrash) <Warning>: (...) Could not find: com.apple.ReportCrash.Self"

[2010-02-11]
# RemotManagement
# $ sudo srm -rf /System/Library/CoreServices/RemoteManagement/
# or
# cd /path/to/backup-dir/
# $ sudo mv /System/Library/CoreServices/RemoteManagement/ .

[2010-02-12]
# User foo
# % launchctl list | grep Remote
# => exist.

# Admin user
# $ grep 'Remote' /System/Library/LaunchAgents/*
# $ sudo mv /System/Library/LaunchAgents/com.apple.RemoteDesktop.plist /path/to/backup-dir/
# $ sudo mv /System/Library/LaunchAgents/com.apple.ScreenSharing.plist /path/to/backup-dir/

# reboot

# User foo
# % launchctl list | grep Remote
# => not exist.


[2010-05-31]
Apple Releases Snow Leopard Security Configuration Guide
http://blog.intego.com/2010/05/26/apple-releases-snow-leopard-security-configuration-guide/

Apple has released their Snow Leopard Security Configuration Guide,


Mac OS X Security Configuration Guides
http://www.apple.com/support/security/guides/