File: getdents.2

package info (click to toggle)
manpages-ja 0.5.0.0.20221215%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 29,304 kB
  • sloc: perl: 161; makefile: 61
file content (272 lines) | stat: -rw-r--r-- 11,705 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
.\" Copyright (C) 1995 Andries Brouwer (aeb@cwi.nl)
.\" and Copyright 2008, 2015 Michael Kerrisk <mtk.manpages@gmail.com>
.\"
.\" %%%LICENSE_START(VERBATIM)
.\" Permission is granted to make and distribute verbatim copies of this
.\" manual provided the copyright notice and this permission notice are
.\" preserved on all copies.
.\"
.\" Permission is granted to copy and distribute modified versions of this
.\" manual under the conditions for verbatim copying, provided that the
.\" entire resulting derived work is distributed under the terms of a
.\" permission notice identical to this one.
.\"
.\" Since the Linux kernel and libraries are constantly changing, this
.\" manual page may be incorrect or out-of-date.  The author(s) assume no
.\" responsibility for errors or omissions, or for damages resulting from
.\" the use of the information contained herein.  The author(s) may not
.\" have taken the same level of care in the production of this manual,
.\" which is licensed free of charge, as they might when working
.\" professionally.
.\"
.\" Formatted or processed versions of this manual, if unaccompanied by
.\" the source, must acknowledge the copyright and authors of this work.
.\" %%%LICENSE_END
.\"
.\" Written 11 June 1995 by Andries Brouwer <aeb@cwi.nl>
.\" Modified 22 July 1995 by Michael Chastain <mec@duracef.shout.net>:
.\"   Derived from 'readdir.2'.
.\" Modified Tue Oct 22 08:11:14 EDT 1996 by Eric S. Raymond <esr@thyrsus.com>
.\"
.\"*******************************************************************
.\"
.\" This file was generated with po4a. Translate the source file.
.\"
.\"*******************************************************************
.\"
.\" Japanese Version Copyright (c) 1997 HANATAKA Shinya
.\"         all rights reserved.
.\" Translated Sat Feb 22 20:15:56 JST 1997
.\"         by HANATAKA Shinya <hanataka@abyss.rim.or.jp>
.\" Updated Sun Oct 12 JST 2003 by Kentaro Shirakata <argrath@ub32.org>
.\" Updated Wed Jul 30 JST 2008 by Kentaro Shirakata <argrath@ub32.org>
.\" Updated 2009-02-12 by Kentaro Shirakata <argrath@ub32.org>
.\" Updated 2012-04-30, Akihiro MOTOKI <amotoki@gmail.com>
.\" Updated 2013-03-26, Akihiro MOTOKI <amotoki@gmail.com>
.\"
.TH GETDENTS 2 2020\-11\-01 Linux "Linux Programmer's Manual"
.SH 名前
getdents, getdents64 \- ディレクトリエントリーを取得する
.SH 書式
.nf
\fBlong getdents(unsigned int \fP\fIfd\fP\fB, struct linux_dirent *\fP\fIdirp\fP\fB,\fP
\fB             unsigned int \fP\fIcount\fP\fB);\fP
.PP
\fB#define _GNU_SOURCE\fP        /* feature_test_macros(7) 参照 */
\fB#include <dirent.h>\fP
.PP
\fBssize_t getdents64(int \fP\fIfd\fP\fB, void *\fP\fIdirp\fP\fB, size_t \fP\fIcount\fP\fB);\fP
.fi
.PP
\fI注\fP: \fBgetdents\fP() の glibc のラッパー関数は存在しない。「注意」の節を参照。
.SH 説明
これらはあなたの関心を引くようなインターフェースではないだろう。 POSIX 準拠の C ライブラリインターフェースについては
\fBreaddir\fP(3) を参照のこと。このページは、カーネルシステムコールの生のインターフェースについて記載したものである。
.SS getdents()
\fBgetdents\fP()  システムコールは、オープン済みのファイルディスクリプター \fIfd\fP で参照されるディレクトリから
\fIlinux_dirent\fP 構造体をいくつか読み出し、 \fIdirp\fP が指しているバッファーに格納する。 \fIcount\fP
引数はそのバッファーのサイズを示す。
.PP
\fIlinux_dirent\fP 構造体は以下のように宣言されている。
.PP
.in +4n
.EX
struct linux_dirent {
    unsigned long  d_ino;     /* inode 番号 */
    unsigned long  d_off;     /* 次の \fIlinux_dirent\fP へのオフセット */
    unsigned short d_reclen;  /* この \fIlinux_dirent\fP の長さ */
    char           d_name[];  /* (ヌル終端された) ファイル名 */
                      /* 実際の長さは (d_reclen \- 2 \-
                         offsetof(struct linux_dirent, d_name)) */
    /*
    char           pad;       // 値 0 のパディングバイト
    char           d_type;    // ファイル種別 (Linux 2.6.4 以降のみ);
                              // オフセットは (d_reclen \- 1)
    */
}
.EE
.in
.PP
\fId_ino\fP は inode 番号である。 \fId_off\fP はディレクトリの先頭から次の \fIlinux_dirent\fP の先頭までの距離である。
\fId_reclen\fP はこの \fIlinux_dirent\fP 全体のサイズである。 \fId_name\fP はヌル文字で終わるファイル名である。
.PP
\fId_type\fP は、構造体の最後のバイトであり、ファイルタイプを示す。 \fId_type\fP は以下の値のいずれか一つを取る
(\fI<dirent.h>\fP で定義されている)。
.TP  12
\fBDT_BLK\fP
ブロックデバイスである。
.TP 
\fBDT_CHR\fP
キャラクターデバイスである。
.TP 
\fBDT_DIR\fP
ディレクトリである。
.TP 
\fBDT_FIFO\fP
名前付きパイプ (FIFO) である。
.TP 
\fBDT_LNK\fP
シンボリックリンクである。
.TP 
\fBDT_REG\fP
通常のファイルである。
.TP 
\fBDT_SOCK\fP
UNIX ドメインソケットである。
.TP 
\fBDT_UNKNOWN\fP
ファイルタイプが不明。
.PP
\fId_type\fP フィールドは Linux 2.6.4 以降で実装されている。 このフィールドは、 \fIlinux_dirent\fP
構造体の中で以前はゼロで埋められていた空間に配置されている。 したがって、2.6.3 以前のカーネルでは、このフィールドにアクセスしようとすると 常に値
0 (\fBDT_UNKNOWN\fP) が返される。
.PP
.\" kernel 2.6.27
.\" The same sentence is in readdir.2
現在のところ、 \fId_type\fP でファイルタイプを返す機能が完全にサポートされているのは、 いくつかのファイルシステムにおいてのみである
(Btrfs, ext2, ext3, ext4 はサポートしている)。 どのアプリケーションも \fBDT_UNKNOWN\fP
が返された際に適切に処理できなければならない。
.SS getdents64()
元々の Linux の \fBgetdents\fP()
システムコールは、大きなファイルシステムと大きなファイルオフセットを扱うことができなかった。そのため、Linux 2.4 で
\fBgetdents64\fP() が追加された。 \fBgetdents64\fP() では、 \fId_ino\fP と \fId_off\fP
でビット幅の大きなデータ型が使われている。また、 \fBgetdents64\fP() では \fId_type\fP フィールドが明示的にサポートされている。
.PP
\fBgetdents64\fP() システムコールは \fBgetdents\fP() と似ているが、 2
番目の引数が以下の構造体が入ったバッファへのポインターである点が異なる。
.PP
.in +4n
.EX
struct linux_dirent64 {
    ino64_t        d_ino;    /* 64 ビットの inode 番号 */
    off64_t        d_off;    /* 次の構造体への 64 ビットのオフセット */
    unsigned short d_reclen; /* この dirent の大きさ */
    unsigned char  d_type;   /* ファイル種別 */
    char           d_name[]; /* (ヌル終端された) ファイル名 */
};
.EE
.in
.SH 返り値
成功した場合は、読み込んだバイト数が返される。 ディレクトリの終わりならば 0 が返される。 エラーの場合は \-1 が返され、 \fIerrno\fP
に適切な値が設定される。
.SH エラー
.TP 
\fBEBADF\fP
ファイルディスクリプター \fIfd\fP が不正である。
.TP 
\fBEFAULT\fP
引数が呼び出し元プロセスのアドレス空間外を指している。
.TP 
\fBEINVAL\fP
結果用のバッファーが小さすぎる。
.TP 
\fBENOENT\fP
そのようなディレクトリは存在しない。
.TP 
\fBENOTDIR\fP
ファイルディスクリプターがディレクトリを参照していない。
.SH 準拠
.\" SVr4 documents additional ENOLINK, EIO error conditions.
SVr4.
.SH 注意
\fBgetdents64\fP() に対応するライブラリのサポートは glibc 2.30 で追加された。 \fBgetdents\fP() に対する glibc
ラッパー関数は存在しない。 \fBgetdents\fP() (もしくは glibc の古いバージョンでの \fBgetdents64\fP()) を呼び出すには、
\fBsyscall\fP(2) を使う必要がある。その場合、構造体 \fIlinux_dirent\fP や \fIlinux_dirent64\fP
を自分で定義する必要があるだろう。
.PP
おそらく、あなたが使いたいのは、これらのシステムコールではなく \fBreaddir\fP(3) の方であろう。
.PP
これらのシステムコールは \fBreaddir\fP(2)  を置き換えるものである。
.SH 例
.\" FIXME The example program needs to be revised, since it uses the older
.\" getdents() system call and the structure with smaller field widths.
下記のプログラムは \fBgetdents\fP()  の使用例を示したものである。 以下は、このプログラムを ext2 ディレクトリで実行した際に得られる
出力の例である。
.PP
.in +4n
.EX
$\fB ./a.out /testfs/\fP
\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- nread=120 \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-
inode#    file type  d_reclen  d_off   d_name
       2  directory    16         12  .
       2  directory    16         24  ..
      11  directory    24         44  lost+found
      12  regular      16         56  a
  228929  directory    16         68  sub
   16353  directory    16         80  sub2
  130817  directory    16       4096  sub3
.EE
.in
.SS プログラムのソース
\&
.EX
#define _GNU_SOURCE
#include <dirent.h>     /* DT_* 定数の定義 */
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/syscall.h>

#define handle_error(msg) \e
        do { perror(msg); exit(EXIT_FAILURE); } while (0)

struct linux_dirent {
    unsigned long  d_ino;
    off_t          d_off;
    unsigned short d_reclen;
    char           d_name[];
};

#define BUF_SIZE 1024

int
main(int argc, char *argv[])
{
    int fd;
    long nread;
    char buf[BUF_SIZE];
    struct linux_dirent *d;
    char d_type;

    fd = open(argc > 1 ? argv[1] : ".", O_RDONLY | O_DIRECTORY);
    if (fd == \-1)
        handle_error("open");

    for (;;) {
        nread = syscall(SYS_getdents, fd, buf, BUF_SIZE);
        if (nread == \-1)
            handle_error("getdents");

        if (nread == 0)
            break;

        printf("\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- nread=%d \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\en", nread);
        printf("inode#    file type  d_reclen  d_off   d_name\en");
        for (long bpos = 0; bpos < nread;) {
            d = (struct linux_dirent *) (buf + bpos);
            printf("%8ld  ", d\->d_ino);
            d_type = *(buf + bpos + d\->d_reclen \- 1);
            printf("%\-10s ", (d_type == DT_REG) ?  "regular" :
                             (d_type == DT_DIR) ?  "directory" :
                             (d_type == DT_FIFO) ? "FIFO" :
                             (d_type == DT_SOCK) ? "socket" :
                             (d_type == DT_LNK) ?  "symlink" :
                             (d_type == DT_BLK) ?  "block dev" :
                             (d_type == DT_CHR) ?  "char dev" : "???");
            printf("%4d %10jd  %s\en", d\->d_reclen,
                    (intmax_t) d\->d_off, d\->d_name);
            bpos += d\->d_reclen;
        }
    }

    exit(EXIT_SUCCESS);
}
.EE
.SH 関連項目
\fBreaddir\fP(2), \fBreaddir\fP(3), \fBinode\fP(7)
.SH この文書について
この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 5.10 の一部である。プロジェクトの説明とバグ報告に関する情報は
\%https://www.kernel.org/doc/man\-pages/ に書かれている。