2 .\" Hey Emacs! This file is -*- nroff -*- source.
4 .\" Copyright (c) 1992 Drew Eckhardt (drew@cs.colorado.edu), March 28, 1992
5 .\" Parts Copyright (c) 1995 Nicolai Langfeldt (janl@ifi.uio.no), 1/1/95
6 .\" and Copyright (c) 2007 Michael Kerrisk <mtk.manpages@gmail.com>
8 .\" Permission is granted to make and distribute verbatim copies of this
9 .\" manual provided the copyright notice and this permission notice are
10 .\" preserved on all copies.
12 .\" Permission is granted to copy and distribute modified versions of this
13 .\" manual under the conditions for verbatim copying, provided that the
14 .\" entire resulting derived work is distributed under the terms of a
15 .\" permission notice identical to this one.
17 .\" Since the Linux kernel and libraries are constantly changing, this
18 .\" manual page may be incorrect or out-of-date. The author(s) assume no
19 .\" responsibility for errors or omissions, or for damages resulting from
20 .\" the use of the information contained herein. The author(s) may not
21 .\" have taken the same level of care in the production of this manual,
22 .\" which is licensed free of charge, as they might when working
25 .\" Formatted or processed versions of this manual, if unaccompanied by
26 .\" 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 .\" Japanese Version Copyright (c) 1998-1999 HANATAKA Shinya
41 .\" and Copyright (c) 2005-2008 Akihiro MOTOKI
43 .\" Translated 1998-06-21, HANATAKA Shinya <hanataka@abyss.rim.or.jp>
44 .\" Modified 1999-04-16, HANATAKA Shinya
45 .\" Updated 2000-01-03, Kentaro Shirakata <argrath@ub32.org>
46 .\" Updated 2000-10-06, Kentaro Shirakata
47 .\" Updated 2001-04-09, Kentaro Shirakata
48 .\" Updated 2001-06-25, Kentaro Shirakata
49 .\" Updated 2005-02-28, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>
50 .\" Updated 2005-09-06, Akihiro MOTOKI
51 .\" Updated 2005-11-19, Akihiro MOTOKI, catch up to LDP v2.14
52 .\" Updated 2006-07-21, Akihiro MOTOKI, LDP v2.36
53 .\" Updated 2007-06-13, Akihiro MOTOKI, LDP v2.55
54 .\" Updated 2008-09-19, Akihiro MOTOKI, LDP v3.08
57 .\"WORD: directory ディレクトリ
64 .\"WORD: hard link count ハード・リンク数
65 .\"WORD: symbolic link シンボリック・リンク
68 .\"WORD: sticky bit スティッキー・ビット
69 .\"WORD: mandatory locking 強制ロック
71 .TH STAT 2 2010-12-03 "Linux" "Linux Programmer's Manual"
73 stat, fstat, lstat \- ファイルの状態を取得する
75 .B #include <sys/types.h>
77 .B #include <sys/stat.h>
79 .B #include <unistd.h>
81 .BI "int stat(const char *" path ", struct stat *" buf );
83 .BI "int fstat(int " fd ", struct stat *" buf );
85 .BI "int lstat(const char *" path ", struct stat *" buf );
89 .RB ( feature_test_macros (7)
94 _BSD_SOURCE || _XOPEN_SOURCE\ >=\ 500
97 これらの関数はファイルについての情報を返す。
98 ファイルそのものに対するアクセス許可は必要としないが、
107 を構成する全てのディレクトリに対する実行 (検索) 許可が必要である。
121 がシンボリックリンクの場合、リンクが参照しているファイルではなく、
128 状態を取得するファイルをファイル・ディスクリプタ
136 構造体には以下のフィールドが含まれている:
141 dev_t st_dev; /* ファイルがあるデバイスの ID */
142 ino_t st_ino; /* inode 番号 */
143 mode_t st_mode; /* アクセス保護 */
144 nlink_t st_nlink; /* ハードリンクの数 */
145 uid_t st_uid; /* 所有者のユーザ ID */
146 gid_t st_gid; /* 所有者のグループ ID */
147 dev_t st_rdev; /* デバイス ID (特殊ファイルの場合) */
148 off_t st_size; /* 全体のサイズ (バイト単位) */
149 blksize_t st_blksize; /* ファイルシステム I/O での
151 blkcnt_t st_blocks; /* 割り当てられた 512B のブロック数 */
152 time_t st_atime; /* 最終アクセス時刻 */
153 time_t st_mtime; /* 最終修正時刻 */
154 time_t st_ctime; /* 最終状態変更時刻 */
160 フィールドは、このファイルが存在するデバイスを示す
164 は、このフィールドのデバイス ID を分解するのに役立つだろう)。
167 フィールドは、このファイル (inode) が表すデバイスを示す。
170 フィールドは、(通常のファイルかシンボリックリンクの場合に)
172 シンボリックリンクの大きさは、シンボリックリンクに含まれている
173 パス名の長さ (最後の NULL バイトは含まない) である。
176 フィールドは、ファイルの大きさを 512 バイトのブロックサイズ単位で示す
177 フィールドは、ファイルに割り当てされたブロック数を 512 バイト単位で示す。
183 フィールドは、効率的にファイル・システム I/O ができる「好ましい」
184 ブロックサイズを示す (もっと小さい単位でファイルに書き込みを行うと、
185 読み出し--修正--再書き込みといった非効率な動作になってしまうかもしれない)。
187 全ての Linux のファイル・システムが全ての時間フィールドを
191 フィールドを更新しないようなかたちでマウントできるファイルシステムもある。
209 フィールドはファイルアクセスがあった場合に変更される
221 は更新されることもあれば、そうでない場合もある。
224 フィールドは、ファイルが修正された場合に変更される
231 で 1 バイト以上書き込みをした場合など)。
235 ファイルが作成されたり削除されたりすると変更される。
238 所有者やグループやハード・リンク数やモードの変更では変更
243 (所有者、グループ、リンク数、モードなど) の
249 で使用されるファイル種別のチェックのために定義されている :
268 シンボリック・リンクか? (POSIX.1-1996 にはない)
271 ソケットか? (POSIX.1-1996 にはない)
280 S_IFMT 0170000 ファイル種別を示すビット領域を表すビットマスク
281 S_IFSOCK 0140000 ソケット
282 S_IFLNK 0120000 シンボリック・リンク
283 S_IFREG 0100000 通常のファイル
284 S_IFBLK 0060000 ブロック・デバイス
285 S_IFDIR 0040000 ディレクトリ
286 S_IFCHR 0020000 キャラクター・デバイス
288 S_ISUID 0004000 set-user-ID bit
289 S_ISGID 0002000 set-group-ID bit (下記参照)
290 S_ISVTX 0001000 スティッキー・ビット (下記参照)
291 S_IRWXU 00700 ファイル所有者のアクセス許可用のビットマスク
292 S_IRUSR 00400 所有者の読み込み許可
293 S_IWUSR 00200 所有者の書き込み許可
294 S_IXUSR 00100 所有者の実行許可
295 S_IRWXG 00070 グループのアクセス許可用のビットマスク
296 S_IRGRP 00040 グループの読み込み許可
297 S_IWGRP 00020 グループの書き込み許可
298 S_IXGRP 00010 グループの実行許可
299 S_IRWXO 00007 他人 (others) のアクセス許可用のビットマスク
300 S_IROTH 00004 他人の読み込み許可
301 S_IWOTH 00002 他人の書き込み許可
302 S_IXOTH 00001 他人の実行許可
309 ディレクトリに設定した場合には、そのディレクトリが BSD 方式で使用される
310 ことを示す。つまり、そのディレクトリに作成されたファイルのグループID は
311 作成したプロセスの実効 (effective) グループID ではなく、ディレクトリの
312 グループID を継承する。また、そのディレクトリに作成されたディレクトリにも
316 が設定されていないファイルに設定された場合は、
317 set-group-ID ビットはファイル/レコードの
318 強制的な (mandatory) ロックを表す。
320 ディレクトリにスティッキービット (S_ISVTX) が設定された場合は、
321 そのディレクトリのファイルの名前を変更したり、削除したりできるのは、
322 そのファイルの所有者か、そのディレクトリの所有者か、特権プロセス
325 成功した場合は 0 を返す。エラーの場合は \-1 を返し、
332 が所属するディレクトリとその上位のディレクトリのいずれかに
334 .RB ( path_resolution (7)
345 パスを辿る際に解決すべきシンボリック・リンクが多過ぎた。
362 の前半部分 (prefix) の構成要素がディレクトリではない。
370 このエラーが起こるのは、32 ビットプラットフォーム上で
371 .I -D_FILE_OFFSET_BITS=64
372 を指定せずにコンパイルされたアプリケーションが、ファイルサイズが
378 これらのシステムコールは SVr4, 4.3BSD, POSIX.1-2001 に準拠している。
382 コールは SVr4, SVID, POSIX, X/OPEN, 4.3BSD に準拠している。
384 コールは 4.3BSD と SVr4 に準拠している。
387 .\" のエラーとして EINTR, ENOLINK, EOVERFLOW が記載されている。
392 .\" のエラーとして EINTR, EMULTIHOP, ENOLINK, EOVERFLOW が記載されている。
398 (これらのフィールドは BSD によって導入された。
400 NFS マウントの場合には同じシステムでも異なる可能性がある)
414 のようなマクロを使用するように要求している。
419 は POSIX.1-1996 にはないが、
420 POSIX.1-2001 には両方とも存在する。
421 前者は SVID 4 に、後者は SUSv2 に由来している。
423 Unix V7 (とその後のシステム) は
438 f000 S_IFMT 170000 ファイル種別フィールドのビットマスク
439 0000 000000 SCO では 使用不能 inode;
441 SVID-v2 と XPG2 では 0 と 0100000 の
443 1000 S_IFIFO p| 010000 FIFO (名前付きパイプ)
444 2000 S_IFCHR c 020000 キャラクタ特殊ファイル (V7)
445 3000 S_IFMPC 030000 多重化されたキャラクタ特殊ファイル (V7)
446 4000 S_IFDIR d/ 040000 ディレクトリ (V7)
447 5000 S_IFNAM 050000 XENIX 二つの副型を持つ名前付きの
449 副型は \fIst_rdev\fP の値 1,2 で区別される:
450 0001 S_INSEM s 000001 XENIX IFNAMのセマフォー副型
451 0002 S_INSHD m 000002 XENIX IFNAMの共有データ副型
452 6000 S_IFBLK b 060000 ブロック特殊ファイル (V7)
453 7000 S_IFMPB 070000 多重化されたブロック特殊ファイル (V7)
454 8000 S_IFREG - 100000 通常ファイル (V7)
455 9000 S_IFCMP 110000 VxFS 圧縮ファイル
456 9000 S_IFNWK n 110000 ネットワーク特殊ファイル (HP-UX)
457 a000 S_IFLNK l@ 120000 シンボリック・リンク (BSD)
458 b000 S_IFSHAD 130000 Solaris ACL のための隠された inode
460 c000 S_IFSOCK s= 140000 ソケット (BSD; VxFS の "S_IFSOC")
461 d000 S_IFDOOR D> 150000 Solaris ドア・ファイル
462 e000 S_IFWHT w% 160000 BSD 空白ファイル (inode を使用しない)
463 0200 S_ISVTX 001000 `スティッキー・ビット':使用後も
468 ディレクトリ: 削除制限フラグ (SVID-v4.2)
469 0400 S_ISGID 002000 実行時の set-group-ID (V7)
472 0400 S_ENFMT 002000 System V ファイル・ロックを強制する
474 0800 S_ISUID 004000 実行時の set-user-ID (V7)
475 0800 S_CDF 004000 ディレクトリが状況依存ファイル (HP-UX)
478 スティッキー コマンドは Version 32V AT&T UNIX で登場した。
482 構造体は 3つのファイルのタイムスタンプ関連のフィールドで
484 glibc では、各フィールドのナノ秒の情報を
498 秒より細かいタイムスタンプをサポートしていないファイルシステムでは、
499 これらのナノ秒のフィールドは 0 に設定される。
500 .\" As at kernel 2.6.25, XFS and JFS support nanosecond timestamps,
501 .\" but ext2, ext3, and Reiserfs do not.
502 .\" FIXME . SUSv4 specifies nanosecond timestamps.
506 は一般には自動マウント動作 (automounter action) のきっかけとならないが、
511 ディレクトリ以下にあるファイルのほとんどでは、
536 ラッパー関数はこれらの詳細をアプリケーションから隠蔽してくれる。
537 具体的には、カーネルが提供しているシステムコールのうち最新のバージョンを
538 起動し、古いバイナリの場合には必要に応じて返された情報を再構成 (repack) する。
544 .\" A note from Andries Brouwer, July 2007
546 .\" > Is the story not rather more complicated for some calls like
549 .\" Yes and no, mostly no. See /usr/include/sys/stat.h .
551 .\" The idea is here not so much that syscalls change, but that
552 .\" the definitions of struct stat and of the types dev_t and mode_t change.
553 .\" This means that libc (even if it does not call the kernel
554 .\" but only calls some internal function) must know what the
555 .\" format of dev_t or of struct stat is.
556 .\" The communication between the application and libc goes via
557 .\" the include file <sys/stat.h> that defines a _STAT_VER and
558 .\" _MKNOD_VER describing the layout of the data that user space
559 .\" uses. Each (almost each) occurrence of stat() is replaced by
560 .\" an occurrence of xstat() where the first parameter of xstat()
561 .\" is this version number _STAT_VER.
563 .\" Now, also the definitions used by the kernel change.
564 .\" But glibc copes with this in the standard way, and the
565 .\" struct stat as returned by the kernel is repacked into
566 .\" the struct stat as expected by the application.
567 .\" Thus, _STAT_VER and this setup cater for the application-libc
568 .\" interface, rather than the libc-kernel interface.
570 .\" (Note that the details depend on gcc being used as c compiler.)
579 #include <sys/types.h>
580 #include <sys/stat.h>
586 main(int argc, char *argv[])
591 fprintf(stderr, "Usage: %s <pathname>\\n", argv[0]);
595 if (stat(argv[1], &sb) == \-1) {
600 printf("File type: ");
602 switch (sb.st_mode & S_IFMT) {
603 case S_IFBLK: printf("block device\\n"); break;
604 case S_IFCHR: printf("character device\\n"); break;
605 case S_IFDIR: printf("directory\\n"); break;
606 case S_IFIFO: printf("FIFO/pipe\\n"); break;
607 case S_IFLNK: printf("symlink\\n"); break;
608 case S_IFREG: printf("regular file\\n"); break;
609 case S_IFSOCK: printf("socket\\n"); break;
610 default: printf("unknown?\\n"); break;
613 printf("I\-node number: %ld\\n", (long) sb.st_ino);
615 printf("Mode: %lo (octal)\\n",
616 (unsigned long) sb.st_mode);
618 printf("Link count: %ld\\n", (long) sb.st_nlink);
619 printf("Ownership: UID=%ld GID=%ld\\n",
620 (long) sb.st_uid, (long) sb.st_gid);
622 printf("Preferred I/O block size: %ld bytes\\n",
623 (long) sb.st_blksize);
624 printf("File size: %lld bytes\\n",
625 (long long) sb.st_size);
626 printf("Blocks allocated: %lld\\n",
627 (long long) sb.st_blocks);
629 printf("Last status change: %s", ctime(&sb.st_ctime));
630 printf("Last file access: %s", ctime(&sb.st_atime));
631 printf("Last file modification: %s", ctime(&sb.st_mtime));
643 .BR capabilities (7),