Skip to content

Instantly share code, notes, and snippets.

@hiboma
Last active August 29, 2015 14:05
Show Gist options
  • Select an option

  • Save hiboma/896bf9fe34144cb5bc94 to your computer and use it in GitHub Desktop.

Select an option

Save hiboma/896bf9fe34144cb5bc94 to your computer and use it in GitHub Desktop.
chown の怪

chown の uid, gid に -1 を渡して呼び出すと、オーナー権限のないディレクトリでも ctime が更新されてしまうんだけどよいの?

手順

ルートディレクトリを対象に見てみます。

まずは stat で ctime (Change) を確認。

$ stat /
  File: `/'
  Size: 4096            Blocks: 8          IO Block: 4096   directory
Device: fd00h/64768d    Inode: 2           Links: 23
Access: (0555/dr-xr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2014-08-20 16:24:23.225278266 +0900
Modify: 2014-08-20 14:39:44.462343612 +0900
Change: 2014-08-20 16:35:02.533779300 +0900 # <= これです

perl のワンライナーで chown(2) を呼び出す

$ perl -e 'chown(-1, -1, "/") or die $!'

( 一般ユーザ (vagrant 501:501) で実行しています )

再度 stat すると ctime (Change) が更新されている

$ stat /
  File: `/'
  Size: 4096            Blocks: 8          IO Block: 4096   directory
Device: fd00h/64768d    Inode: 2           Links: 23
Access: (0555/dr-xr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2014-08-20 16:24:23.225278266 +0900
Modify: 2014-08-20 14:39:44.462343612 +0900
Change: 2014-08-20 16:50:04.448764559 +0900 # <= あれ、更新されている yo !

👻


Mac OS X (Marvcerics) だとこうはならんかった

@hiboma
Copy link
Author

hiboma commented Aug 20, 2014

http://pubs.opengroup.org/onlinepubs/7908799/xsh/chown.html を読むと

The Single UNIX ® Specification, Version 2

If owner or group is specified as (uid_t)-1 or (gid_t)-1 respectively, the corresponding ID of the file is unchanged.
Upon successful completion, chown() will mark for update the st_ctime field of the file.

とあるので、仕様として正しい感。

@hiboma
Copy link
Author

hiboma commented Aug 20, 2014

あれ http://pubs.opengroup.org/onlinepubs/9699919799/functions/chown.html を読むと

IEEE Std 1003.1, 2013 Edition (所謂 POSIX)

If owner or group is specified as (uid_t)-1 or (gid_t)-1, respectively, the corresponding ID of the file shall not be changed. If both owner and group are -1, the times need not be updated.
Upon successful completion, chown() shall mark for update the last file status change timestamp of the file.

_If both owner and group are -1, the times need not be updated_」 とあるな。やっぱ st_ctime 変える必要無いのか

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