Unix-system-calls-fstat

提供:Dev Guides
移動先:案内検索

[top]#

|http://www.google.com/[Google] | a|

Web This Site
  • 初心者向けのUnix *
  • 高度なUnix *

選択した読書

Copyright©2014 by finddevguides

[cols=",,,,,,,",]

| |  Home   | |  References   | |  Discussion Forums   | |  About TP  

[width="100%",cols="100%",]

a| == stat()-Unix、Linuxシステムコール

[[File:]] image :http://www.finddevguides.com/images/next.gif [next] image:http://www.finddevguides.com/add- this.gif [AddThisソーシャルブックマークボタン]

広告

NAME

stat、fstat、lstat-ファイルの状態を取得

概要

#include <sys/types.h> #include <sys/stat.h> #include <unistd.h> int stat(const char* path, struct stat *buf); int fstat(int filedes, struct stat *buf); int lstat(const char *path, struct stat *buf);

説明

これらの関数は、ファイルに関する情報を返します。 ファイル自体に対する権限は必要ありませんが、- stat ()および lstat ()の場合-ファイルにつながる_path_内のすべてのディレクトリに対する実行(検索)権限が必要です。

*stat* ()は、_path_が指すファイルを統計し、_buf_を埋めます。 *lstat* ()は *stat* ()と同じですが、_path_がシンボリックリンクの場合、参照するファイルではなく、リンク自体がstat-edになります。
*fstat* ()は、stat-edするファイルがファイル記述子_filedes_によって指定されることを除いて、 *stat* ()と同じです。

これらのシステムコールはすべて、次のフィールドを含む_stat_構造体を返します。

struct stat { dev_t st_dev; /*ID of device containing file*/ ino_t st_ino; /*inode number*/ mode_t st_mode; /*protection*/ nlink_t st_nlink; /*number of hard links*/ uid_t st_uid; /*user ID of owner*/ gid_t st_gid; /*group ID of owner*/ dev_t st_rdev; /*device ID (if special file)*/ off_t st_size; /*total size, in bytes*/ blksize_t st_blksize;/*blocksize for filesystem I/O*/ blkcnt_t st_blocks; /*number of blocks allocated*/ time_t st_atime; /*time of last access*/ time_t st_mtime; /*time of last modification*/ time_t st_ctime; /*time of last status change*/ };

_st_dev_フィールドは、このファイルが存在するデバイスを示します。

_st_rdev_フィールドは、このファイル(inode)が表すデバイスを示します。

_st_size_フィールドは、ファイルのサイズ(通常のファイルまたはシンボリックリンクの場合)をバイト単位で示します。 シンボリックリンクのサイズは、それに含まれるパス名の長さであり、末尾のヌルバイトはありません。

st_blocks_フィールドは、ファイルに割り当てられたブロック数を512バイト単位で示します。 (これは、ファイルに穴がある場合など、_st_size/512よりも小さい場合があります。)

_st_blksize_フィールドは、効率的なファイルシステムI/Oの「優先」ブロックサイズを提供します。 (小さなチャンクでファイルに書き込むと、非効率的な読み取り-変更-書き換えが発生する場合があります。)

すべてのLinuxファイルシステムがすべての時間フィールドを実装しているわけではありません。 一部のファイルシステムタイプでは、ファイルアクセスによって_st_atime_フィールドが更新されないようにマウントできます。 ( mount (8)の「noatime」を参照してください。)

フィールド_st_atime_は、ファイルアクセスによって変更されます。 execve (2)、 mknod (2)、 pipe (2)、 utime (2)および read (2)(0バイト以上)。 mmap (2)などの他のルーチンは、_st_atime_を更新する場合としない場合があります。

フィールド_st_mtime_は、ファイルの変更によって変更されます。 mknod (2)、 truncate (2)、 utime (2)および write (2)(0バイト以上)。 さらに、ディレクトリの_st_mtime_は、そのディレクトリ内のファイルの作成または削除によって変更されます。 _st_mtime_フィールドは、所有者、グループ、ハードリンクカウント、またはモードの変更に対しては変更されません。

フィールド_st_ctime_は、書き込みまたはinode情報(所有者、グループ、リンクカウント、モードなど)の設定によって変更されます。

_st_mode_フィールドを使用してファイルタイプをチェックするために、次のPOSIXマクロが定義されています。

Tag Description
S_ISREG(m) is it a regular file?
S_ISDIR(m) directory?
S_ISCHR(m) character device?
S_ISBLK(m) block device?
S_ISFIFO(m) FIFO (named pipe)?
S_ISLNK(m) symbolic link? (Not in POSIX.1-1996.)
S_ISSOCK(m) socket? (Not in POSIX.1-1996.)

_st_mode_フィールドには、次のフラグが定義されています。

S_IFMT 0170000 bitmask for the file type bitfields
S_IFSOCK 0140000 socket
S_IFLNK 0120000 symbolic link
S_IFREG 0100000 regular file
S_IFBLK 0060000 block device
S_IFDIR 0040000 directory
S_IFCHR 0020000 character device
S_IFIFO 0010000 FIFO
S_ISUID 0004000 set UID bit
S_ISGID 0002000 set-group-ID bit (see below)
S_ISVTX 0001000 sticky bit (see below)
S_IRWXU 00700 mask for file owner permissions
S_IRUSR 00400 owner has read permission
S_IWUSR 00200 owner has write permission
S_IXUSR 00100 owner has execute permission
S_IRWXG 00070 mask for group permissions
S_IRGRP 00040 group has read permission
S_IWGRP 00020 group has write permission
S_IXGRP 00010 group has execute permission
S_IRWXO 00007 mask for permissions for others (not in group)
S_IROTH 00004 others have read permission
S_IWOTH 00002 others have write permission
S_IXOTH 00001 others have execute permission

set-group-IDビット(S_ISGID)にはいくつかの特別な用途があります。 ディレクトリの場合、BSDセマンティクスがそのディレクトリに使用されることを示します:そこに作成されたファイルは、作成プロセスの有効なグループIDからではなく、ディレクトリからグループIDを継承し、そこに作成されたディレクトリもS_ISGIDビットセットを取得します。 グループ実行ビット(S_IXGRP)が設定されていないファイルの場合、set-group-IDビットは必須のファイル/レコードロックを示します。

ディレクトリの「スティッキー」ビット(S_ISVTX)は、そのディレクトリ内のファイルの名前を変更または削除できるのは、ファイルの所有者、ディレクトリの所有者、および特権プロセスのみであることを意味します。

Linuxノート

カーネル2.5.48以降、_stat_構造は3つのファイルタイムスタンプフィールドのナノ秒の解像度をサポートします。 Glibcは、_BSD_SOURCEまたは_SVID_SOURCE機能テストマクロが定義されている場合は_st_atim.tv_nsec_形式の名前を使用して、またはこれらのマクロのどちらも定義されていない場合は_st_atimensec_形式の名前を使用して各フィールドのナノ秒コンポーネントを公開します。 1秒未満のタイムスタンプをサポートしないファイルシステムでは、これらのナノ秒フィールドは値0で返されます。

_/proc_ディレクトリにあるほとんどのファイルでは、 stat ()は_st_size_フィールドにファイルサイズを返しません。代わりに、フィールドは値0で返されます。

返り値

成功すると、ゼロが返されます。 エラーの場合、-1が返され、_errno_が適切に設定されます。

エラー

Tag Description
*EACCES * Search permission is denied for one of the directories in the path prefix of path. (See also* path_resolution*(2).)
EBADF filedes is bad.
EFAULT Bad address.
ELOOP Too many symbolic links encountered while traversing the path.
ENAMETOOLONG File name too long.
ENOENT A component of the path path does not exist, or the path is an empty string.
ENOMEM Out of memory (i.e. kernel memory).
ENOTDIR A component of the path is not a directory.

準拠

これらのシステムコールは、SVr4、4.3BSD、POSIX.1-2001に準拠しています。

_st_blocks_および_st_blksize_フィールドを使用すると、移植性が低下する場合があります。 (BSDで導入されました。 解釈はシステム間で異なり、NFSマウントが関係する場合はおそらく単一のシステム上で行われます。

POSIXはS_IFMT、S_IFSOCK、S_IFLNK、S_IFREG、S_IFBLK、S_IFDIR、S_IFCHR、S_IFIFO、S_ISVTXビットを記述しませんが、代わりにマクロS_ISDIR()などの使用を要求します。 S_ISLNKおよびS_ISSOCKマクロはPOSIX.1-1996にはありませんが、両方ともPOSIX.1-2001にあります。前者はSVID 4から、後者はSUSv2からです。

Unix V7(およびそれ以降のシステム)にはS_IREAD、S_IWRITE、S_IEXECがあり、POSIXは同義語S_IRUSR、S_IWUSR、S_IXUSRを規定しています。

その他のシステム

さまざまなシステムで使用されている(または使用されている)値:

hex name ls octal description
f000 S_IFMT   170000 mask for file type
0000     000000 SCO out-of-service inode, BSD unknown type
        SVID-v2 and XPG2 have both 0 and 0100000 for ordinary file
1000 S_IFIFO p 010000
FIFO (named pipe) 2000 S_IFCHR c 020000
character special (V7) 3000 S_IFMPC   030000
multiplexed character special (V7) 4000 S_IFDIR d/ 040000
directory (V7) 5000 S_IFNAM   050000
XENIX named special file        
with two subtypes, distinguished by st_rdev values 1, 2: 0001 S_INSEM s 000001
XENIX semaphore subtype of IFNAM 0002 S_INSHD m 000002
XENIX shared data subtype of IFNAM 6000 S_IFBLK b 060000
block special (V7) 7000 S_IFMPB   070000
multiplexed block special (V7) 8000 S_IFREG - 100000
regular (V7) 9000 S_IFCMP   110000
VxFS compressed 9000 S_IFNWK n 110000
network special (HP-UX) a000 S_IFLNK l@ 120000
symbolic link (BSD) b000 S_IFSHAD   130000
Solaris shadow inode for ACL (not seen by userspace) c000 S_IFSOCK s= 140000
socket (BSD; also "S_IFSOC" on VxFS) d000 S_IFDOOR D> 150000
Solaris door e000 S_IFWHT w% 160000
BSD whiteout (not used for inode) 0200 S_ISVTX   001000
‘sticky bit’: save swapped text even after use (V7)        
reserved (SVID-v2)        
On non-directories: don’t cache this file (SunOS)        
On directories: restricted deletion flag (SVID-v4.2) 0400 S_ISGID   002000
set-group-ID on execution (V7)        
for directories: use BSD semantics for propagation of GID 0400 S_ENFMT   002000
SysV file locking enforcement (shared with S_ISGID) 0800 S_ISUID   004000
set-user-ID on execution (V7) 0800 S_CDF   004000

スティッキーコマンドは、バージョン32V AT&T UNIXで登場しました。

関連項目

[[File:]] image :http://www.finddevguides.com/images/next.gif [next] [[File:]]

広告

|  

[cols="^",]

|Advertisements