2 .\" Copyright (c) 1992 Drew Eckhardt (drew@cs.colorado.edu), March 28, 1992
3 .\" Parts Copyright (c) 1995 Nicolai Langfeldt (janl@ifi.uio.no), 1/1/95
4 .\" and Copyright (c) 2007 Michael Kerrisk <mtk.manpages@gmail.com>
6 .\" %%%LICENSE_START(VERBATIM)
7 .\" Permission is granted to make and distribute verbatim copies of this
8 .\" manual provided the copyright notice and this permission notice are
9 .\" preserved on all copies.
11 .\" Permission is granted to copy and distribute modified versions of this
12 .\" manual under the conditions for verbatim copying, provided that the
13 .\" entire resulting derived work is distributed under the terms of a
14 .\" permission notice identical to this one.
16 .\" Since the Linux kernel and libraries are constantly changing, this
17 .\" manual page may be incorrect or out-of-date. The author(s) assume no
18 .\" responsibility for errors or omissions, or for damages resulting from
19 .\" the use of the information contained herein. The author(s) may not
20 .\" have taken the same level of care in the production of this manual,
21 .\" which is licensed free of charge, as they might when working
24 .\" Formatted or processed versions of this manual, if unaccompanied by
25 .\" the source, must acknowledge the copyright and authors of this work.
28 .\" Modified by Michael Haardt <michael@moria.de>
29 .\" Modified 1993-07-24 by Rik Faith <faith@cs.unc.edu>
30 .\" Modified 1995-05-18 by Todd Larason <jtl@molehill.org>
31 .\" Modified 1997-01-31 by Eric S. Raymond <esr@thyrsus.com>
32 .\" Modified 1995-01-09 by Richard Kettlewell <richard@greenend.org.uk>
33 .\" Modified 1998-05-13 by Michael Haardt <michael@cantor.informatik.rwth-aachen.de>
34 .\" Modified 1999-07-06 by aeb & Albert Cahalan
35 .\" Modified 2000-01-07 by aeb
36 .\" Modified 2004-06-23 by Michael Kerrisk <mtk.manpages@gmail.com>
37 .\" 2007-06-08 mtk: Added example program
38 .\" 2007-07-05 mtk: Added details on underlying system call interfaces
40 .\"*******************************************************************
42 .\" This file was generated with po4a. Translate the source file.
44 .\"*******************************************************************
46 .\" Japanese Version Copyright (c) 1998-1999 HANATAKA Shinya
47 .\" and Copyright (c) 2005-2008 Akihiro MOTOKI
48 .\" Translated 1998-06-21, HANATAKA Shinya <hanataka@abyss.rim.or.jp>
49 .\" Modified 1999-04-16, HANATAKA Shinya
50 .\" Updated 2000-01-03, Kentaro Shirakata <argrath@ub32.org>
51 .\" Updated 2000-10-06, Kentaro Shirakata
52 .\" Updated 2001-04-09, Kentaro Shirakata
53 .\" Updated 2001-06-25, Kentaro Shirakata
54 .\" Updated 2005-02-28, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>
55 .\" Updated 2005-09-06, Akihiro MOTOKI
56 .\" Updated 2005-11-19, Akihiro MOTOKI, catch up to LDP v2.14
57 .\" Updated 2006-07-21, Akihiro MOTOKI, LDP v2.36
58 .\" Updated 2007-06-13, Akihiro MOTOKI, LDP v2.55
59 .\" Updated 2008-09-19, Akihiro MOTOKI, LDP v3.08
60 .\" Updated 2012-04-30, Akihiro MOTOKI <amotoki@gmail.com>
61 .\" Updated 2012-05-29, Akihiro MOTOKI <amotoki@gmail.com>
62 .\" Updated 2013-03-26, Akihiro MOTOKI <amotoki@gmail.com>
64 .TH STAT 2 2012\-11\-11 Linux "Linux Programmer's Manual"
66 stat, fstat, lstat \- ファイルの状態を取得する
68 \fB#include <sys/types.h>\fP
70 \fB#include <sys/stat.h>\fP
72 \fB#include <unistd.h>\fP
74 \fBint stat(const char *\fP\fIpath\fP\fB, struct stat *\fP\fIbuf\fP\fB);\fP
76 \fBint fstat(int \fP\fIfd\fP\fB, struct stat *\fP\fIbuf\fP\fB);\fP
78 \fBint lstat(const char *\fP\fIpath\fP\fB, struct stat *\fP\fIbuf\fP\fB);\fP
81 glibc 向けの機能検査マクロの要件 (\fBfeature_test_macros\fP(7) 参照):
88 _BSD_SOURCE || _XOPEN_SOURCE\ >=\ 500 || _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED
90 || /* glibc 2.10 以降: */ _POSIX_C_SOURCE\ >=\ 200112L
96 これらの関数はファイルについての情報を返す。
97 ファイルそのものに対するアクセス許可は必要としないが、
98 \(em\fBstat\fP() と \fBlstat\fP() の場合には \(em
99 そのファイルへ至る \fIpath\fP を構成する全てのディレクトリに対する
102 \fBstat\fP() は \fIpath\fP で指定されたファイルの状態を取得して \fIbuf\fP へ格納する。
104 \fBlstat\fP() は \fBstat\fP() と同じであるが、 \fIpath\fP がシンボリックリンクの場合、リンクが参照しているファイルではなく、
107 \fBfstat\fP() は \fBstat\fP() と同じだが、 状態を取得するファイルをファイル・ディスクリプタ \fIfd\fP で指定する。
109 これらのシステムコールはいずれも、結果を \fIstat\fP 構造体に入れて返す。 \fIstat\fP 構造体には以下のフィールドが含まれている:
114 dev_t st_dev; /* ID of device containing file */
115 ino_t st_ino; /* inode number */
116 mode_t st_mode; /* protection */
117 nlink_t st_nlink; /* number of hard links */
118 uid_t st_uid; /* user ID of owner */
119 gid_t st_gid; /* group ID of owner */
120 dev_t st_rdev; /* device ID (if special file) */
121 off_t st_size; /* total size, in bytes */
122 blksize_t st_blksize; /* blocksize for filesystem I/O */
123 blkcnt_t st_blocks; /* number of 512B blocks allocated */
124 time_t st_atime; /* time of last access */
125 time_t st_mtime; /* time of last modification */
126 time_t st_ctime; /* time of last status change */
131 \fIst_dev\fP フィールドは、このファイルが存在するデバイスを示す (マクロ \fBmajor\fP(3), \fBminor\fP(3)
132 は、このフィールドのデバイス ID を分解するのに役立つだろう)。
134 \fIst_rdev\fP フィールドは、このファイル (inode) が表すデバイスを示す。
136 \fIst_size\fP フィールドは、(通常のファイルかシンボリックリンクの場合に)
137 ファイルの大きさをバイト単位で示す。 シンボリックリンクの大きさは、
138 シンボリックリンクに含まれている パス名の長さ (終端の NULL バイトは含まない)
141 \fIst_blocks\fP フィールドは、ファイルの大きさを 512 バイトのブロックサイズ単位で示す フィールドは、ファイルに割り当てされたブロック数を
142 512 バイト単位で示す。 (ファイルに穴があるような場合、この値は \fIst_size\fP/512 より小さくなることもある)。
144 The \fIst_blksize\fP field gives the "preferred" blocksize for efficient
145 filesystem I/O. (Writing to a file in smaller chunks may cause an
146 inefficient read\-modify\-rewrite.)
148 Not all of the Linux filesystems implement all of the time fields. Some
149 filesystem types allow mounting in such a way that file and/or directory
150 accesses do not cause an update of the \fIst_atime\fP field. (See \fInoatime\fP,
151 \fInodiratime\fP, and \fIrelatime\fP in \fBmount\fP(8), and related information in
152 \fBmount\fP(2).) In addition, \fIst_atime\fP is not updated if a file is opened
153 with the \fBO_NOATIME\fP; see \fBopen\fP(2).
155 \fIst_atime\fP フィールドはファイルアクセスがあった場合に変更される (例えば、 \fBexecve\fP(2), \fBmknod\fP(2),
156 \fBpipe\fP(2), \fButime\fP(2) を使用した場合や \fBread\fP(2) で 1 バイト以上読み込んだ場合など)。
157 \fBmmap\fP(2) などの他のルーチンでは、 \fIst_atime\fP は更新されることもあれば、そうでない場合もある。
159 \fIst_mtime\fP フィールドは、ファイルが修正された場合に変更される (例えば、 \fBmknod\fP(2), \fBtruncate\fP(2),
160 \fButime\fP(2) を使用した場合や \fBwrite\fP(2) で 1 バイト以上書き込みをした場合など)。 さらに、ディレクトリの
161 \fIst_mtime\fP は、そのディレクトリで ファイルが作成されたり削除されたりすると変更される。 \fIst_mtime\fP フィールドは
162 所有者やグループやハード・リンク数やモードの変更では変更 \fIされない。\fP
164 \fIst_ctime\fP フィールドは書き込みや inode 情報 (所有者、グループ、リンク数、モードなど) の 設定によって変更される。
166 以下の POSIX マクロは、 \fIst_mode\fP フィールド で使用されるファイル種別のチェックのために定義されている :
185 シンボリックリンクか? (POSIX.1\-1996 にはない)
188 ソケットか? (POSIX.1\-1996 にはない)
191 以下のフラグが \fIst_mode\fP フィールド用に定義されている:
195 S_IFMT 0170000 ファイル種別を示すビット領域を表すビットマスク
196 S_IFSOCK 0140000 ソケット
197 S_IFLNK 0120000 シンボリックリンク
198 S_IFREG 0100000 通常のファイル
199 S_IFBLK 0060000 ブロック・デバイス
200 S_IFDIR 0040000 ディレクトリ
201 S_IFCHR 0020000 キャラクター・デバイス
203 S_ISUID 0004000 set\-user\-ID bit
204 S_ISGID 0002000 set\-group\-ID bit (下記参照)
205 S_ISVTX 0001000 スティッキー・ビット (下記参照)
206 S_IRWXU 00700 ファイル所有者のアクセス許可用のビットマスク
207 S_IRUSR 00400 所有者の読み込み許可
208 S_IWUSR 00200 所有者の書き込み許可
209 S_IXUSR 00100 所有者の実行許可
210 S_IRWXG 00070 グループのアクセス許可用のビットマスク
211 S_IRGRP 00040 グループの読み込み許可
212 S_IWGRP 00020 グループの書き込み許可
213 S_IXGRP 00010 グループの実行許可
214 S_IRWXO 00007 他人 (others) のアクセス許可用のビットマスク
215 S_IROTH 00004 他人の読み込み許可
216 S_IWOTH 00002 他人の書き込み許可
217 S_IXOTH 00001 他人の実行許可
221 set\-group\-ID bit (\fBS_ISGID\fP) にはいくつかの特殊な使用法がある: ディレクトリに設定した場合には、そのディレクトリが
222 BSD 方式で使用される ことを示す。つまり、そのディレクトリに作成されたファイルのグループID は 作成したプロセスの実効 (effective)
223 グループID ではなく、ディレクトリの グループID を継承する。また、そのディレクトリに作成されたディレクトリにも \fBS_ISGID\fP
224 ビットが設定される。グループ実行ビット (\fBS_IXGRP\fP) が設定されていないファイルに設定された場合は、 set\-group\-ID
225 ビットはファイル/レコードの 強制的な (mandatory) ロックを表す。
227 ディレクトリにスティッキービット (S_ISVTX) が設定された場合は、 そのディレクトリのファイルの名前を変更したり、削除したりできるのは、
228 そのファイルの所有者か、そのディレクトリの所有者か、特権プロセス だけとなる。
230 成功した場合、0 が返される。 失敗した場合、 \-1 が返され、 \fIerrno\fP に適切な値がセットされる。
234 \fIpath\fP が所属するディレクトリとその上位のディレクトリのいずれかに 対する検索許可がなかった (\fBpath_resolution\fP(7)
244 パスを辿る際に解決すべきシンボリックリンクが多過ぎた。
250 \fIpath\fP の構成要素が存在しないか、 \fIpath\fP が空文字列である。
256 \fIpath\fP の前半部分 (prefix) の構成要素がディレクトリではない。
259 \fIpath\fP または \fIfd\fP が、ファイルサイズ、inode 番号、ブロック数が
260 それぞれ \fIoff_t\fP 型、 \fIino_t\fP 型、 \fIblkcnt_t\fP 型で表現できないファイルを
261 参照している。このエラーが起こるのは、例えば、32 ビットプラットフォーム上で
262 \fI\-D_FILE_OFFSET_BITS=64\fP を指定せずにコンパイルされたアプリケーションが、
263 ファイルサイズが \fI(1<<31)\-1\fP バイトを超えるファイルに対して
264 \fBstat\fP() を呼び出した場合である。
266 .\" SVr4 documents additional
268 .\" error conditions EINTR, ENOLINK, and EOVERFLOW. SVr4
269 .\" documents additional
273 .\" error conditions EINTR, EMULTIHOP, ENOLINK, and EOVERFLOW.
274 これらのシステムコールは SVr4, 4.3BSD, POSIX.1\-2001 に準拠している。 \fBstat\fP() と \fBfstat\fP()
275 コールは SVr4, SVID, POSIX, X/OPEN, 4.3BSD に準拠している。 \fBlstat\fP() コールは 4.3BSD と
278 POSIX.1\-2001 では、シンボリックリンクに対する \fBlstat\fP() で
279 有効な情報を返すように求められていたのは、 \fIstat\fP 構造体の \fIst_size\fP
280 と \fIst_mode\fP のファイル種別要素だけであった。
281 POSIX.1\-2008 では規定が厳しくなり、 \fBlstat\fP() は \fIst_mode\fP の
282 アクセス許可ビット以外の全てのフィールドに有効な情報を返すことが
285 \fIst_blocks\fP と \fIst_blksize\fP フィールドの使用はあまり移植性がない
286 (これらのフィールドは BSD によって導入された。 システムごとに解釈が
287 異なっており、 NFS マウントの場合には同じシステムでも異なる可能性がある)。
288 \fI<sys/stat.h>\fP から \fIblkcnt_t\fP の \fIblksize_t\fP 型定義を
289 読み込みたい場合は、(\fIどの\fPヘッダファイルをインクルードするよりも前に)
290 \fB_XOPEN_SOURCE\fP を 500 以上の値で定義すること。
292 POSIX.1\-1990 には \fBS_IFMT\fP, \fBS_IFSOCK\fP, \fBS_IFLNK\fP, \fBS_IFREG\fP,
293 \fBS_IFBLK\fP, \fBS_IFDIR\fP, \fBS_IFCHR\fP, \fBS_IFIFO\fP, \fBS_ISVTX\fP 定数に関する
294 記述はなかったが、代わりに \fBS_ISDIR\fP() のようなマクロを使用するように
295 要求していた。 \fBS_IF*\fP 定数は POSIX.1\-2001 以降には存在する。
297 マクロ \fBS_ISLNK\fP() と \fBS_ISSOCK\fP() は POSIX.1\-1996 にはないが、
298 POSIX.1\-2001 には両方とも存在する。 前者は SVID 4 に、後者は SUSv2 に
301 UNIX V7 (とその後のシステム) は \fBS_IREAD\fP, \fBS_IWRITE\fP, \fBS_IEXEC\fP を持っており、
302 POSIX はその同義語として \fBS_IRUSR\fP, \fBS_IWUSR\fP, \fBS_IXUSR\fP を規定している。
309 f000 S_IFMT 170000 ファイル種別フィールドのビットマスク
311 SCO では 使用不能 inode; BSD では不明なファイル種別;
312 SVID\-v2 と XPG2 では 0 と 0100000 の両方が通常のファイル
314 1000 S_IFIFO p| 010000 FIFO (名前付きパイプ)
315 2000 S_IFCHR c 020000 キャラクタ特殊ファイル (V7)
316 3000 S_IFMPC 030000 多重化されたキャラクタ特殊ファイル (V7)
317 4000 S_IFDIR d/ 040000 ディレクトリ (V7)
318 5000 S_IFNAM 050000 T{
319 XENIX の二つの副型を持つ名前付きの特殊ファイル
320 副型は \fIst_rdev\fP の値 1, 2 で区別される
322 0001 S_INSEM s 000001 XENIX の IFNAM セマフォ副型
323 0002 S_INSHD m 000002 XENIX の IFNAM 共有データ副型
324 6000 S_IFBLK b 060000 ブロック特殊ファイル (V7)
325 7000 S_IFMPB 070000 多重化されたブロック特殊ファイル (V7)
326 8000 S_IFREG \- 100000 通常ファイル (V7)
327 9000 S_IFCMP 110000 VxFS 圧縮ファイル
328 9000 S_IFNWK n 110000 ネットワーク特殊ファイル (HP\-UX)
329 a000 S_IFLNK l@ 120000 シンボリックリンク (BSD)
330 b000 S_IFSHAD 130000 T{
331 Solaris の ACL 用の隠し inode (ユーザ空間からは見えない)
333 c000 S_IFSOCK s= 140000 ソケット (BSD; VxFS の "S_IFSOC")
334 d000 S_IFDOOR D> 150000 Solaris の door ファイル
335 e000 S_IFWHT w% 160000 BSD の空白ファイル (inode を使用しない)
336 0200 S_ISVTX 001000 T{
337 スティッキービット: 使用後もスワップに残す (V7)
341 ディレクトリ以外: ファイルをキャッシュしない (SunOS)
343 ディレクトリの場合: 削除制限フラグ (SVID\-v4.2)
345 0400 S_ISGID 002000 T{
346 実行時の set\-group\-ID (V7)
348 ディレクトリの場合: GID の伝搬に BSD 方式を使用する
350 0400 S_ENFMT 002000 T{
351 System V ファイルロックを強制する (S_ISGID と共有)
353 0800 S_ISUID 004000 実行時の set\-user\-ID (V7)
355 ディレクトリがコンテキスト依存ファイル (HP\-UX)
360 スティッキー コマンドは Version 32V AT&T UNIX で登場した。
362 .\" As at kernel 2.6.25, XFS and JFS support nanosecond timestamps,
363 .\" but ext2, ext3, and Reiserfs do not.
364 Since kernel 2.5.48, the \fIstat\fP structure supports nanosecond resolution
365 for the three file timestamp fields. Glibc exposes the nanosecond component
366 of each field using names of the form \fIst_atim.tv_nsec\fP if the
367 \fB_BSD_SOURCE\fP or \fB_SVID_SOURCE\fP feature test macro is defined. These
368 fields are specified in POSIX.1\-2008, and, starting with version 2.12, glibc
369 also exposes these field names if \fB_POSIX_C_SOURCE\fP is defined with the
370 value 200809L or greater, or \fB_XOPEN_SOURCE\fP is defined with the value 700
371 or greater. If none of the aforementioned macros are defined, then the
372 nanosecond values are exposed with names of the form \fIst_atimensec\fP. On
373 filesystems that do not support subsecond timestamps, the nanosecond fields
374 are returned with the value 0.
376 Linux では、 \fBlstat\fP() は一般には自動マウント動作 (automounter action) の
377 きっかけとならないが、 \fBstat\fP() はきっかけとなる (\fBfstatat\fP(2) を参照)。
379 \fI/proc\fP ディレクトリ以下にあるファイルのほとんどでは、 \fBstat\fP() を呼び出した際に、 \fIst_size\fP
380 フィールドにファイルサイズが返されない。 代わりに \fIst_size\fP フィールドには 0 が返される。
383 .\" A note from Andries Brouwer, July 2007
385 .\" > Is the story not rather more complicated for some calls like
388 .\" Yes and no, mostly no. See /usr/include/sys/stat.h .
390 .\" The idea is here not so much that syscalls change, but that
391 .\" the definitions of struct stat and of the types dev_t and mode_t change.
392 .\" This means that libc (even if it does not call the kernel
393 .\" but only calls some internal function) must know what the
394 .\" format of dev_t or of struct stat is.
395 .\" The communication between the application and libc goes via
396 .\" the include file <sys/stat.h> that defines a _STAT_VER and
397 .\" _MKNOD_VER describing the layout of the data that user space
398 .\" uses. Each (almost each) occurrence of stat() is replaced by
399 .\" an occurrence of xstat() where the first parameter of xstat()
400 .\" is this version number _STAT_VER.
402 .\" Now, also the definitions used by the kernel change.
403 .\" But glibc copes with this in the standard way, and the
404 .\" struct stat as returned by the kernel is repacked into
405 .\" the struct stat as expected by the application.
406 .\" Thus, _STAT_VER and this setup cater for the application-libc
407 .\" interface, rather than the libc-kernel interface.
409 .\" (Note that the details depend on gcc being used as c compiler.)
410 時間の経過とともに、 \fIstat\fP 構造体のサイズが大きくなり、この影響で \fBstat\fP() には 3つのバージョンが存在する:
411 \fIsys_stat\fP() (スロットは \fI__NR_oldstat\fP)、 \fIsys_newstat\fP() (スロットは
412 \fI__NR_stat\fP)、 \fIsys_stat64\fP() (カーネル 2.4 で導入; スロットは \fI__NR_stat64\fP). glibc
413 の \fBstat\fP() ラッパー関数はこれらの詳細をアプリケーションから隠蔽してくれる。
414 具体的には、カーネルが提供しているシステムコールのうち最新のバージョンを 起動し、古いバイナリの場合には必要に応じて返された情報を再構成
415 (repack) する。 \fBfstat\fP() と \fBlstat\fP() についても同様である。
417 以下のプログラムは \fBstat\fP() を呼び出し、返ってきた \fIstat\fP 構造体のフィールドのいくつかを表示する。
420 #include <sys/types.h>
421 #include <sys/stat.h>
427 main(int argc, char *argv[])
432 fprintf(stderr, "Usage: %s <pathname>\en", argv[0]);
436 if (stat(argv[1], &sb) == \-1) {
441 printf("File type: ");
443 switch (sb.st_mode & S_IFMT) {
444 case S_IFBLK: printf("block device\en"); break;
445 case S_IFCHR: printf("character device\en"); break;
446 case S_IFDIR: printf("directory\en"); break;
447 case S_IFIFO: printf("FIFO/pipe\en"); break;
448 case S_IFLNK: printf("symlink\en"); break;
449 case S_IFREG: printf("regular file\en"); break;
450 case S_IFSOCK: printf("socket\en"); break;
451 default: printf("unknown?\en"); break;
454 printf("I\-node number: %ld\en", (long) sb.st_ino);
456 printf("Mode: %lo (octal)\en",
457 (unsigned long) sb.st_mode);
459 printf("Link count: %ld\en", (long) sb.st_nlink);
460 printf("Ownership: UID=%ld GID=%ld\en",
461 (long) sb.st_uid, (long) sb.st_gid);
463 printf("Preferred I/O block size: %ld bytes\en",
464 (long) sb.st_blksize);
465 printf("File size: %lld bytes\en",
466 (long long) sb.st_size);
467 printf("Blocks allocated: %lld\en",
468 (long long) sb.st_blocks);
470 printf("Last status change: %s", ctime(&sb.st_ctime));
471 printf("Last file access: %s", ctime(&sb.st_atime));
472 printf("Last file modification: %s", ctime(&sb.st_mtime));
478 \fBaccess\fP(2), \fBchmod\fP(2), \fBchown\fP(2), \fBfstatat\fP(2), \fBreadlink\fP(2),
479 \fButime\fP(2), \fBcapabilities\fP(7), \fBsymlink\fP(7)
481 この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.54 の一部
482 である。プロジェクトの説明とバグ報告に関する情報は
483 http://www.kernel.org/doc/man\-pages/ に書かれている。