sourCEntral - mobile manpages

pdf

FTS

名前

fts, fts_open, fts_read, fts_children, fts_set, fts_close - ファイル階層をたどる

書式

#include <sys/types.h>
#include <sys/stat.h>
#include <fts.h>

FTS *fts_open(char * const *path_argv, int options,
int (*
compar)(const FTSENT **, const FTSENT **));

FTSENT *fts_read(FTS *ftsp);

FTSENT *fts_children(FTS *ftsp, int instr);

int fts_set(FTS *ftsp, FTSENT *f, int instr);

int fts_close(FTS *ftsp);

説明

The fts functions are provided for traversing file hierarchies. A simple overview is that the fts_open() function returns a "handle" (of type FTS *) that refers to a file hierarchy "stream". This handle is then supplied to the other fts functions. The function fts_read() returns a pointer to a structure describing one of the files in the file hierarchy. The function fts_children() returns a pointer to a linked list of structures, each of which describes one of the files contained in a directory in the hierarchy.

In general, directories are visited two distinguishable times; in preorder (before any of their descendants are visited) and in postorder (after all of their descendants have been visited). Files are visited once. It is possible to walk the hierarchy "logically" (visiting the files that symbolic links point to) or physically (visiting the symbolic links themselves), order the walk of the hierarchy or prune and/or revisit portions of the hierarchy.

Two structures (and associated types) are defined in the include file <fts.h>. The first type is FTS, the structure that represents the file hierarchy itself. The second type is FTSENT, the structure that represents a file in the file hierarchy. Normally, an FTSENT structure is returned for every file in the file hierarchy. In this manual page, "file" and "FTSENT structure" are generally interchangeable.

The FTSENT structure contains fields describing a file. The structure contains at least the following fields (there are additional fields that should be considered private to the implementation):

typedef struct _ftsent {
unsigned short fts_info; /* FTSENT 構造体のためのフラグ */
char *fts_accpath; /* アクセスパス */
char *fts_path; /* ルートパス */
short fts_pathlen; /* strlen(fts_path) +
strlen(fts_name) */
char *fts_name; /* ファイル名 */
short fts_namelen; /* strlen(fts_name) */
short fts_level; /* 深さ (-1 〜 N) */
int fts_errno; /* ファイルのエラー番号 */
long fts_number; /* ローカルな番号 */
void *fts_pointer; /* ローカルなアドレス番号 */
struct _ftsent *fts_parent; /* 親ディレクトリ */
struct _ftsent *fts_link; /* 次のファイル構造体 */
struct _ftsent *fts_cycle; /* 循環している構造体 */
struct stat *fts_statp; /* stat(2) の情報 */
} FTSENT;

これらのフィールドは、次のように定義されている。
fts_info

One of the following values describing the returned FTSENT structure and the file it represents. With the exception of directories without errors (FTS_D), all of these entries are terminal, that is, they will not be revisited, nor will any of their descendants be visited.

FTS_D

preorder でたどられるディレクトリ。

FTS_DC

ツリーの中で循環しているディレクトリ。 (FTSENT 構造体の fts_cycle フィールドも同様に埋められる。)

FTS_DEFAULT

ファイルタイプを表現する FTSENT 構造体が、 fts_info の他のいずれかの値で明示的に説明されていない。

FTS_DNR

読み込みができないディレクトリ。 これはエラーの場合の返り値であり、 何がエラーを起こしたかを示すために fts_errno フィールドが設定される。

FTS_DOT

fts_open() へのファイル名として指定されなかった "." または ".." という名前のファイル (FTS_SEEDOT を参照すること)。

FTS_DP

postorder でたどられるディレクトリ。 FTSENT 構造体の内容は、preorder のときに返された状態 (つまり、 fts_info フィールドが FTS_D に設定されている状態) から変更されない。

FTS_ERR

これはエラーの場合の返り値であり、 fts_errno フィールドは、何がエラーを起こしたかを示す値に設定される。

FTS_F

通常のファイル。

FTS_NS

stat(2) 情報が得られなかったファイル。 fts_statp フィールドの内容は定義されない。 これはエラーの場合の返り値であり、 fts_errno フィールドは、何がエラーを起こしたかを示す値に設定される。

FTS_NSOK

stat(2) 情報が要求されなかったファイル。 fts_statp フィールドの内容は定義されない。

FTS_SL

シンボリックリンク。

FTS_SLNONE

リンク先の存在しないシンボリックリンク。 fts_statp フィールドの内容は、シンボリックリンクそのもののファイル特性情報を参照する。

fts_accpath

現在のディレクトリからファイルにアクセスするためのパス。

fts_path

階層をたどるときのルートからみたファイルの相対的なパス。 このパスには、 fts_open() に指定したパスがプレフィックスとして含まれる。

fts_pathlen

The sum of the lengths of the strings referenced by fts_path and fts_name.

fts_name

ファイルの名前。

fts_namelen

fts_name で参照される文字列の長さ。

fts_level

階層をたどって、このファイルがみつかった深さ。 -1 〜 N の数値で表される。 階層をたどるときの出発点 (ルート) の親ディレクトリを表す FTSENT 構造体では -1 となる。 また、ルート自身の FTSENT 構造体では 0 になる。

fts_errno

If fts_children() or fts_read() returns an FTSENT structure whose fts_info field is set to FTS_DNR, FTS_ERR, or FTS_NS, the fts_errno field contains the error number (i.e., the errno value) specifying the cause of the error. Otherwise, the contents of the fts_errno field are undefined.

fts_number

このフィールドは、アプリケーションプログラムから使用するために提供され、 fts 関数群では変更されない。 このフィールドは 0 で初期化される。

fts_pointer

このフィールドは、アプリケーションプログラムから使用するために提供され、 fts 関数群では変更されない。 このフィールドは NULL で初期化される。

fts_parent

A pointer to the FTSENT structure referencing the file in the hierarchy immediately above the current file, that is, the directory of which this file is a member. A parent structure for the initial entry point is provided as well, however, only the fts_level, fts_number, and fts_pointer fields are guaranteed to be initialized.

fts_link

fts_children() から返される場合、 fts_link フィールドはディレクトリメンバーのヌル終端されたリンクリストの形式で、 次の構造体を指し示す。 それ以外の場合、 fts_link フィールドは定義されない。

fts_cycle

2 つのディレクトリにハードリンクが張られているため、 または、シンボリックリンクがあるディレクトリを指しているために、 ディレクトリが循環する階層構造を作っている場合 (FTS_DC を参照)、 構造体の fts_cycle フィールドは、階層中で現在の FTSENT 構造体と同じファイルを参照している FTSENT 構造体を指し示す。 それ以外の場合、 fts_cycle フィールドは定義されない。

fts_statp

このファイルの stat(2) 情報へのポインター。

ファイル階層中のすべてのファイルのパスに対して、 ただ 1 つのバッファーが使われる。 したがって、 fts_pathfts_accpath フィールドは、 fts_read() によって返された最も新しいファイルに対して「のみ」ヌル終端されることが保証される。 これらのフィールドを、他の FTSENT 構造体で表現されるファイルを参照するために使うには、 FTSENT 構造体の fts_pathlen フィールドにある情報を使ってパスのバッファーを修正する必要がある。 これらの修正は、さらに fts_read() を呼び出そうとする場合には、元に戻しておかなければならない。 fts_name フィールドは、常に NUL 終端される。

fts_open()
fts_open
() 関数は、文字列ポインターの配列へのポインターを引数に取る。 この文字列ポインターは、論理ファイル階層をつくる 1 つ以上のパスの名前になる。 配列は、 null ポインターで終端されなければならない。

多くのオプションがあり、少なくとも 1 つ (FTS_LOGICAL または FTS_PHYSICAL) が指定されなければならない。 オプションは以下の値の論理和をとって選択する。
FTS_COMFOLLOW

このオプションは、 FTS_LOGICAL の指定にかかわらず、 ルートパスに指定されたシンボリックリンクをすぐにたどらせる。

FTS_LOGICAL

このオプションは、 fts ルーチンにシンボリックリンクそのものではなく、 シンボリックリンクが指しているファイルの FTSENT 構造体を返させる。 このオプションが設定された場合、 FTSENT 構造体がアプリケーションに返されるような シンボリックリンクのみが、存在しないファイルを参照している。 FTS_LOGICAL または FTS_PHYSICAL のどちらかを、 fts_open() 関数に与えなければ「ならない」。

FTS_NOCHDIR

パフォーマンスの最適化のため、 fts 関数群はファイル階層をたどるときディレクトリを変える。 これには、階層をたどっている間は アプリケーションがある特定のディレクトリにいるということに 依存できない、という副作用がある。 FTS_NOCHDIR オプションで最適化を無効にすると、 fts 関数群は現在のディレクトリを変更しない。 FTS_NOCHDIR が指定され、かつ fts_open() の引数として絶対パス名が与えられたとき以外、アプリケーションは、 自らカレントディレクトリを変更したり、 ファイルにアクセスしたりすべきではない、という点に注意すること。

FTS_NOSTAT

デフォルトでは、返された FTSENT 構造体は、たどられた各ファイルについてのファイル特徴情報 ( statp フィールド) を参照する。 このオプションは、 fts 関数群が fts_info フィールドを FTS_NSOK に設定し statp の内容を定義されないままにすることを許すことにより、 パフォーマンスの最適化に必要なものを緩和する。

FTS_PHYSICAL

このオプションは、 fts ルーチンにシンボリックリンクが指しているファイルではなく、 シンボリックリンク自身の FTSENT 構造体を返させる。 このオプションが設定されると、階層中のすべてのシンボリックリンクの FTSENT 構造体がアプリケーションに返される。 FTS_LOGICAL または FTS_PHYSICAL のどちらかを fts_open() 関数に与えなければ「ならない」。

FTS_SEEDOT

デフォルトでは、 fts_open() のパス引数として指定されない限り、ファイル階層中にある "." または ".." という名前のファイルは無視される。 このオプションは、 fts ルーチンにこれらのファイルの FTSENT 構造体を返させる。

FTS_XDEV

このオプションは、 fts が下り始めのファイルとは異なるデバイス番号を持っている ディレクトリに下りるのを阻止する。

The argument compar() specifies a user-defined function which may be used to order the traversal of the hierarchy. It takes two pointers to pointers to FTSENT structures as arguments and should return a negative value, zero, or a positive value to indicate if the file referenced by its first argument comes before, in any order with respect to, or after, the file referenced by its second argument. The fts_accpath, fts_path, and fts_pathlen fields of the FTSENT structures may never be used in this comparison. If the fts_info field is set to FTS_NS or FTS_NSOK, the fts_statp field may not either. If the compar() argument is NULL, the directory traversal order is in the order listed in path_argv for the root paths, and in the order listed in the directory for everything else.

fts_read()
fts_read
() 関数は、階層中のファイルを記述する FTSENT 構造体へのポインターを返す。 (読み込み可能で、循環していない) ディレクトリは、 1 回は preorder で、もう 1 回は postorder で、少なくとも 2 回たどられる。 他のファイルは、少なくとも 1 回たどられる。 (ディレクトリ間のハードリンクによって 循環やシンボリックリンクへのシンボリックリンクが起こらない場合、 ファイルは 2 回以上、ディレクトリは 3 回以上たどられる。)

階層中のすべてのメンバーが返された場合、 fts_read() は NULL を返し、外部変数 errno を 0 にする。 階層中のファイルに関係しないエラーが起こった場合、 fts_read() は NULL を返し、 errno をエラーに対応した値にする。 階層中のファイルに関係したエラーが起こった場合、 FTSENT 構造体へのポインターが返され、 errno は設定される場合と設定されない場合がある (fts_info を参照すること)。

fts_read() によって返される FTSENT 構造体は、同じファイル階層ストリームへの fts_close() の呼出しの後に上書きされる。 また、同じファイル階層ストリームへの fts_read() の呼出しの後でも、構造体がディレクトリを表現していない限り上書きされる。 この場合、 fts_read() 関数によって postorder で FTSENT 構造体が返された後、 fts_read() の呼出しがあるまで、 これらの構造体は上書きされない。

fts_children()
fts_children
() 関数は、 FTSENT 構造体へのポインターを返す。 この構造体は、( fts_read() で最も新しく返された FTSENT 構造体で表現されるディレクトリにあるファイルの) ヌル終端されたリンクリストの最初のエントリーを記述する。 このリストは、 FTSENT 構造体の fts_link フィールドを使ってリンクされ、 ユーザー指定の比較関数がある場合は、それで順序づけられる。 fts_children() の呼出しを繰り返すことで、 このリンクリストは再生成される。

特別な場合として、 fts_read() がファイル階層について呼ばれていない場合、 fts_children() は fts_open() に指定された論理ディレクトリ (つまり、 fts_open() に指定された引数) の中にあるファイルへのポインターを返す。 それ以外の場合で、 fts_read() によって最も新しく返された FTSENT 構造体が preorder でたどられたディレクトリでない場合や 何も含んでいないディレクトリの場合は、 fts_children() は NULL を返し、 errno を 0 にする。 エラーが起こった場合、 fts_children() は NULL を返し、 errno をエラーに対応した値にする。

The FTSENT structures returned by fts_children() may be overwritten after a call to fts_children(), fts_close(), or fts_read() on the same file hierarchy stream.

The instr argument is either zero or the following value:
FTS_NAMEONLY

ファイル名のみが必要とされている。 返された構造体のリンクリストの fts_name, fts_namelen フィールド以外の すべてのフィールドの内容は定義されない。

fts_set()
The function fts_set() allows the user application to determine further processing for the file f of the stream ftsp. The fts_set() function returns 0 on success, and -1 if an error occurs.

The instr argument is either 0 (meaning "do nothing") or one of the following values:
FTS_AGAIN

ファイルを再びたどる。すべてのファイルタイプが再びたどられる。 次の fts_read() の呼出しにより、参照されているファイルが返される。 構造体の fts_stat, fts_info フィールドはこの時に初期化されるが、他のフィールドは変更されない。 このオプションは、 fts_read() によって最も新しく返されたファイルについてのみ意味を持つ。 通常は、postorder でディレクトリをたどる場合に使用し、 その下の階層と同様に、 ディレクトリを (preorder と postorder の両方で) 再びたどらせる。

FTS_FOLLOW

参照されてるファイルは、シンボリックリンクでなければならない。 参照されているファイルが fts_read() によって最も新しく返されたものである場合、次の fts_read() の呼出しでは、シンボリックリンクそのものではなく、 シンボリックリンクが指している先を反映するように fts_info, fts_statp を再び初期化したファイルが返される。 ファイルが fts_children() によって最も新しく返されたものの 1 つである場合、 fts_read() によって返されたとき、構造体の fts_info, fts_statp フィールドは、シンボリックリンクそのものではなく、 シンボリックリンクが指している先を反映する。 どちらの場合でも、シンボリックリンクが指している先がないときは、 返された構造体のフィールドは変更されず、 fts_info フィールドが FTS_SLNONE に設定される。

リンク先がディレクトリの場合、 ファイルが preorder で返された後、下の階層のすべてファイルが返され、 その後で postorder で返される。

FTS_SKIP

このファイルの下の階層はたどられない。 このファイルは、 fts_children() または fts_read() のどちらかによって最も新しく返されたものの 1 つである。

fts_close()
The fts_close() function closes the file hierarchy stream referred to by ftsp and restores the current directory to the directory from which fts_open() was called to open ftsp. The fts_close() function returns 0 on success, and -1 if an error occurs.

エラー

関数 fts_open() が失敗した場合、 errno は、ライブラリ関数 open(2)malloc(3) に対して指定されるエラーに設定される。

関数 fts_close() が失敗した場合、 errno は、ライブラリ関数 chdir(2)close(2) に対して指定されるエラーに設定される。

関数 fts_read() と fts_children() が失敗した場合、 errno は、ライブラリ関数 chdir(2), malloc(3), opendir(3), readdir(3), stat(2) に対して指定されるエラーに設定される。

更に、 fts_children(), fts_open(), fts_set() が失敗した場合、 errno が次の値にされる。

EINVAL

options or instr was invalid.

バージョン

これらの関数は、Linux では glibc2 から使用可能である。

属性

この節で使用されている用語の説明については、 attributes(7) を参照。

img

準拠

4.4BSD.

バグ

In versions of glibc before 2.23, all of the APIs described in this man page are not safe when compiling a program using the LFS APIs (e.g., when compiling with -D_FILE_OFFSET_BITS=64).

関連項目

find(1), chdir(2), stat(2), ftw(3), qsort(3)

この文書について

この man ページは Linux man-pages プロジェクトのリリース 5.10 の一部である。プロジェクトの説明とバグ報告に関する情報は https://www.kernel.org/doc/man-pages/ に書かれている。

pdf