Skip to content

Conversation

@1mori
Copy link
Owner

@1mori 1mori commented Jun 17, 2024

関連ページ

https://bootcamp.fjord.jp/practices/224

最終更新

2024/06/20

やりたいこと

  • -lオプションの実装

やったこと

  • これまでの出力結果とは形が異なるため、-lオプションが指定されているときとそれ以外でif文で分岐をした
  • ファイルのオプションを求め、hashの形で格納した
  • ファイルの合計サイズを求め、total xxxの形で出力した
  • 表示される文字の列を合わせるため、最大の文字列の長さを計算し、ljust,rjustで列を揃えた
  • 特殊権限の存在確認をし、apply_special_modesで文字列に変更を加える処理を追加した
  • 複数の特殊権限の付与に対応するため、権限が確認された場合に特殊権限の数値を減算する処理を実装した

1mori added 17 commits June 13, 2024 16:58
8進数に変換したファイルモードの数値列から、lsコマンドで表示する文字列形式に変換するメソッド
- ファイルの種類はfile_mode.ftypeから取得し、予め作成しているFILE_TYPEのhashから対応する文字をとってくる
- ファイルの読み書き権限などは8進数数値列の4~6番目が対応しているため、その部分を取り出してFILE_MODEのhashから対応する文字列を取得した
ファイルモードの書き換えを実行するために、SPECIAL_MODEを作成、置き換える文字を定義
置き換えが発生するかどうかは8進数変換した場合の3文字目に存在するため、そこが'0'であればこの処理は実行されない
map関数でfile_pathひとつを送り、返り血としてファイルモードの情報を得る
Copy link

@yoshitsugu yoshitsugu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

コメントしました。

04.ls/ls.rb Outdated
'characterSpecial' => 'c',
'directory' => 'd',
'blockSpecial' => 'b',
'file' => 'f',

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

通常のファイルのときは f じゃなく - だと思います。

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

指摘ありがとうございます。修正しました。

Comment on lines 62 to 67
SPECIAL_MODE.each do |mode|
if special_modes == mode[:bit]
permissions[mode[:index]] == 'x' ? mode[:exec] : mode[:no_exec]
end
end
permissions

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

スティッキービットなどの判別にもとりくまれているようですね 👍
ただ、うまく動いていないようです。手元で組み込みのlsでは sT などになるファイルを作ってみましたが、判別できませんでした。

組み込みls(Linux環境)

合計 12
-rw-r--r-- 1 yoshitsugu yoshitsugu 21  2月  9  2023 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.txt
lrwxrwxrwx 1 yoshitsugu yoshitsugu  9  4月 20  2023 symlink1.txt -> test1.txt
-rw-r--r-- 1 yoshitsugu yoshitsugu 13  2月  9  2023 test1.txt
-rw-r--r-- 1 yoshitsugu yoshitsugu  0  2月 14  2023 test10.txt
-rw-r--r-- 1 yoshitsugu yoshitsugu  0  2月  9  2023 test111.txt
-rw-r--r-- 1 yoshitsugu yoshitsugu 30  2月  9  2023 test2.txt
-rw-r--r-- 1 yoshitsugu yoshitsugu  0  2月  7  2023 test3.txt
-rw-r--r-- 1 yoshitsugu yoshitsugu  0  2月  9  2023 test4.txt
-rw-r--r-- 1 yoshitsugu yoshitsugu  0  2月  9  2023 test5.txt
-rw-r--r-T 1 yoshitsugu yoshitsugu  0  1月 19 09:16 test6.txt
-rwxr-xr-t 1 yoshitsugu yoshitsugu  0  2月  9  2023 test7.txt
-rwsr-sr-x 1 yoshitsugu yoshitsugu  0  2月  9  2023 test8.txt
-rwSr--r-- 1 yoshitsugu yoshitsugu  0  2月  9  2023 test9.txt

今回実装してもらったもの

total 24
frw-r--r-- 1 yoshitsugu  yoshitsugu  21  2 09 14:30 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.txt
lrwxrwxrwx 1 yoshitsugu  yoshitsugu   9  4 20 08:02 symlink1.txt
frw-r--r-- 1 yoshitsugu  yoshitsugu  13  2 09 14:29 test1.txt
frw-r--r-- 1 yoshitsugu  yoshitsugu   0  2 14 08:41 test10.txt
frw-r--r-- 1 yoshitsugu  yoshitsugu   0  2 09 14:30 test111.txt
frw-r--r-- 1 yoshitsugu  yoshitsugu  30  2 09 14:29 test2.txt
frw-r--r-- 1 yoshitsugu  yoshitsugu   0  2 07 07:35 test3.txt
frw-r--r-- 1 yoshitsugu  yoshitsugu   0  2 09 14:29 test4.txt
frw-r--r-- 1 yoshitsugu  yoshitsugu   0  2 09 14:29 test5.txt
frw-r--r-- 1 yoshitsugu  yoshitsugu   0  1 19 09:16 test6.txt
frwxr-xr-x 1 yoshitsugu  yoshitsugu   0  2 09 14:29 test7.txt
frwxr-xr-x 1 yoshitsugu  yoshitsugu   0  2 09 14:29 test8.txt
frw-r--r-- 1 yoshitsugu  yoshitsugu   0  2 09 14:30 test9.txt

Copy link
Owner Author

@1mori 1mori Jun 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

指摘ありがとうございます。
確認したところ、権限の判別はできていたのですが、その後の文字列に対して反映する処置が正しくありませんでした。失礼しました。

現在、手元で確認のための環境を作成し、正常に動作していることを確認しました。
実行例に関しては課題ページに追記する形で反映しておきます。

Copy link

@yoshitsugu yoshitsugu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

コメントしました!

04.ls/ls.rb Outdated
file_mode = FILE_TYPE[file_stat.ftype].to_s
file_permissions = file_mode_octal.slice(3..-1).chars.map { |char| FILE_MODE[char] }.join
# 特殊権限の確認
file_permissions = apply_special_modes(file_mode_octal[2], file_permissions) if file_mode_octal[2] != '0'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

特殊権限もowner, group, otherの3通りチェックしないといけないですね。

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

このコードでは最初に特殊権限なしの0であるかどうかを判定し、0であった場合はスキップするように書いていました。

実際のowner, group, otherの3通りのチェックはapply_special_modesでやっているのですが、
if文の判定の位置と相まって少し処理の流れがわかりづらいと考えたので、0を弾く判定をapply_special_modes内に移動しました。

コード自体はほぼ手を加えていませんが、私としてはapply_special_modesで3通りのチェックが実行できていると考えています。
フィヨルドに載せている実行結果においても、本家lsコマンドとの実行結果に違いはないように見られるのですが、もし現状でチェックしきれていないことがあるのであればコメントいただけるとありがたいです。

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@1mori なるほど。現状認知している問題としては、組み込みのlsで

-rwsr-sr-x 1 yoshitsugu yoshitsugu  0  2月  9  2023 test8.txt

となるものに対して

-rwxr-xr-x 1 yoshitsugu  yoshitsugu   0  2 09 14:29 test8.txt

となっています。 s の表示ロジックがおかしいのかもしれませんね。

Copy link
Owner Author

@1mori 1mori Jun 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

指摘ありがとうございます。
前提として、特殊権限は1つしか付与できないと勘違いした状態で実装をしておりました。

そのため、8進数変換された状態で特殊権限の完全一致検索(1か2か4)をしていたため、SUID+GUID(8進数変換された数値では'6')などを検知できないジックで実装していました。

このような場合2進数のビット演算で実装するのが適しているとは思いますが、今回は変換の手間を削減するため、8進数(処理上は10進数)のまま処理を行いました。

1mori added 3 commits June 19, 2024 10:18
0, 1, 2, 4の判定はapply_special_modes内で行った方が処理としてわかりやすいと考えたから
特殊権限は1つしか付与できないと勘違いしていたため、それぞれの権限の確認を1回ずつしかしてこなかった
今回は特殊権限をSUID(4), GUID(2), Sticky Bit(1)の順で確認し、存在していた場合は数値を減算するロジックを組んだ

このような計算であれば2進数の方が適しているが、今回はわかりやすさのために8進数変換された状態(コードは10進数)で計算するようにした
Copy link

@yoshitsugu yoshitsugu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

コメントしました。

04.ls/ls.rb Outdated
return permissions if special_modes.zero?

SPECIAL_MODE.each do |mode|
next unless special_modes >= mode[:bit]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

細かいですが、

next if special_modes < mode[:bit]

のほうが見やすいかなと思いました。

04.ls/ls.rb Outdated
Comment on lines 66 to 73
next unless special_modes >= mode[:bit]

special_modes -= mode[:bit]
permissions[mode[:index]] = if permissions[mode[:index]] == 'x'
mode[:exec]
else
mode[:no_exec]
end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

書いてもらってもいますが、ビット演算のほうが簡単じゃないですか?
ある程度の大きさまでの整数であれば Fixnum クラスとなり、これは & でビット演算の論理積ができます。
https://docs.ruby-lang.org/ja/2.1.0/method/Fixnum/i/=26.html

たとえば3ビット目が1のもの(4)を確認したかったら、 special_modes & 4 > 0 で大丈夫です。

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

確かに論理積を使うと計算量が削減されてすっきりと書けました。指摘ありがとうございます!

&を用い、ビット演算の論理積で2進数ごとの桁の判定を行った
Copy link

@yoshitsugu yoshitsugu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OKです!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants