FTS(3) | Linux Programmer's Manual | FTS(3) |
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;
これらのフィールドは、次のように定義されている。
ファイル階層中のすべてのファイルのパスに対して、 ただ 1 つのバッファーが使われる。 したがって、 fts_path と fts_accpath フィールドは、 fts_read() によって返された最も新しいファイルに対して「のみ」ヌル終端されることが保証される。 これらのフィールドを、他の FTSENT 構造体で表現されるファイルを参照するために使うには、 FTSENT 構造体の fts_pathlen フィールドにある情報を使ってパスのバッファーを修正する必要がある。 これらの修正は、さらに fts_read() を呼び出そうとする場合には、元に戻しておかなければならない。 fts_name フィールドは、常に NUL 終端される。
fts_open() 関数は、文字列ポインターの配列へのポインターを引数に取る。 この文字列ポインターは、論理ファイル階層をつくる 1 つ以上のパスの名前になる。 配列は、 null ポインターで終端されなければならない。
多くのオプションがあり、少なくとも 1 つ (FTS_LOGICAL または FTS_PHYSICAL) が指定されなければならない。 オプションは以下の値の論理和をとって選択する。
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() 関数は、階層中のファイルを記述する 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() 関数は、 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:
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:
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 が次の値にされる。
これらの関数は、Linux では glibc2 から使用可能である。
この節で使用されている用語の説明については、 attributes(7) を参照。
インターフェース | 属性 | 値 |
fts_open(), fts_set(), fts_close() | Thread safety | MT-Safe |
fts_read(), fts_children() | Thread safety | MT-Unsafe |
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/ に書かれている。
2020-04-11 | Linux |