stat FILEHANDLE
stat EXPR
stat EXPR
FILEHANDLEを通じてオープンされているファイルか、EXPRで指定されるファイルの情報を与える、13要素の配列を返します。statに失敗した場合には、空リストを返します。
statの使い方
($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime, $mtime,$ctime,$blksize,$blocks) = stat($filename);
下線だけの_という、特別なファイルハンドルを、statに渡すと、実際にはstatを行なわず、stat構造体に残っている、前回のstatやファイルテストの情報が返されます。
stat の返却値
| 要素 | 意味 |
|---|---|
| 0 | ファイルシステムのデバイス番号 |
| 1 | i ノード番号 |
| 2 | ファイルのモード |
| 3 | ファイルへのハードリンク数 |
| 4 | ファイル所有者のユーザID |
| 5 | ファイル所有者のグループID |
| 6 | 特殊ファイルのデバイス識別子 |
| 7 | ファイルのサイズ(バイト単位) |
| 8 | 最終アクセス時刻 |
| 9 | 最終変更時刻 |
| 10 | 最終 i ノード変更時刻 |
| 11 | 標準のファイルシステム入出力時の標準ブロックサイズ |
| 12 | ファイルに割り当てられてりるブロック数 |
下線だけの _ という特別なファイルハンドルをstatに渡すと、実際にはstatを行なわず、stat構造体に残っている前回のstatやファイルテストの情報が返されます。
if (-x $file && (($d) = stat(_)) && $d < 0) {
print "$file is executable NFS file\n";
}
(これは、NFSのもとでデバイス番号が負になるマシンでのみ動作します。)
File::statモジュールは、便利な名前によるアクセス機構を提供します。
use File::stat; $sb = stat($filename); printf "File is %s, size is %s, perm %04o, mtime %s\n", $filename, $sb->size, $sb->mode & 07777, scalar localtime $sb->mtime;
モード定数 (S_IF*) と関数 (S_IS*) をFcntlモジュールからインポートできます。
use Fcntl ':mode'; use Fcntl ':mode'; $mode = (stat($filename))[2]; $user_rwx = ($mode & S_IRWXU) >> 6; $group_read = ($mode & S_IRGRP) >> 3; $other_execute = $mode & S_IXOTH; printf "Permissions are %04o\n", S_ISMODE($mode), "\n"; $is_setuid = $mode & S_ISUID; $is_setgid = S_ISDIR($mode);
最後の二つは-uと-d演算子を使っても書けます。一般に利用可能なS_IF*定数は以下のものです。
# Permissions: read, write, execute, for user, group, others. S_IRWXU S_IRUSR S_IWUSR S_IXUSR S_IRWXG S_IRGRP S_IWGRP S_IXGRP S_IRWXO S_IROTH S_IWOTH S_IXOTH # Setuid/Setgid/Stickiness. S_ISUID S_ISGID S_ISVTX S_ISTXT # File types. Not necessarily all are available on your system. S_IFREG S_IFDIR S_IFLNK S_IFBLK S_ISCHR S_IFIFO S_IFSOCK S_IFWHT S_ENFMT # The following are compatibility aliases for S_IRUSR, S_IWUSR, S_IXUSR. S_IREAD S_IWRITE S_IEXEC
一般に利用可能なS_IF*関数は以下のものです。
S_IFMODE($mode) the part of $mode containing the permission bits and the setuid/setgid/sticky bits S_IFMT($mode) the part of $mode containing the file type which can be bit-anded with e.g. S_IFREG or with the following functions # The operators -f, -d, -l, -b, -c, -p, and -s. S_ISREG($mode) S_ISDIR($mode) S_ISLNK($mode) S_ISBLK($mode) S_ISCHR($mode) S_ISFIFO($mode) S_ISSOCK($mode) # No direct -X operator counterpart, but for the first one # the -g operator is often equivalent. The ENFMT stands for # record flocking enforcement, a platform-dependent feature. S_ISENFMT($mode) S_ISWHT($mode)