ftwのヘルプ・マニュアル
日本語 英語
ftw --help
man ftw
FTW(3) Linux Programmer’s Manual FTW(3)
名前
ftw, nftw - ファイルツリーを歩きまわる
書式
#include
int ftw(const char *dirpath,
int (*fn) (const char *fpath, const struct stat *sb,
int typeflag),
int nopenfd);
#define _XOPEN_SOURCE 500
#include
int nftw(const char *dirpath,
int (*fn) (const char *fpath, const struct stat *sb,
int typeflag, struct FTW *ftwbuf),
int nopenfd, int flags);
説明
ftw() は、 dirpath で指定されたディレクトリ以下のディレクトリツリー全体
を歩きまわり、ツリー中でエントリが見付かるごとに、 fn() を呼び出す。 デ
フ ォルトでは、ディレクトリそのものが、そのディレクトリにあるファイルや
サブディレクトリよりも先に処理される ( 行 き が け 順 探 索; pre-order
traversal)。
呼 び出し元プロセスが利用可能なファイルディスクリプタを使い切ってしまわ
ないようにするため、 ftw() が同時にオープンするディレクトリの最大 数 を
nopenfd で指定することができる。探索の深さがこの値を越えると、一つのデ
ィレクトリを閉じてから他のディレクトリをオープンし直すことになるので 、
ftw() の動作は遅くなる。 ftw() は、ディレクトリツリーの階層 1 レベルに
つき、最大でも一つのファイルディスクリプタしか使用しない。
ディレクトリツリーで見つかったエントリ毎に、 ftw() は fpath, sb, type-
flag の 3つを引き数として fn() を呼び出す。 fpath はエントリの dirpath
からの相対パス名である。 sb は fpath に対する stat(2) の呼び出しで返 さ
れ る stat 構造体へのポインタである。 typeflag は整数で、以下の値のいず
れか一つである:
FTW_F fpath が通常のファイルである
FTW_D fpath がディレクトリである
FTW_DNR
fpath が読み込みできないディレクトリである
FTW_NS シンボリックリンクではない fpath に対する stat(2) 呼び出しが失敗
した。
fpath が シ ン ボリックリンクで、かつ stat(2) が失敗した場合、
FTW_NS と FTW_SL (後述) のどちらが typeflag に渡されるかは未定義
であると、POSIX.1-2001 には書かれている。
ツリーの探索を止めたい場合は、 fn() が 0 以外の値を返せば良い (この値は
ftw() 自身の戻り値となる)。それ以外の場合は ftw() はツリー全体の探索 を
続 け 、 す べてのツリーを探索し終えたところで 0 を返す。探索中に (mal-
loc(3) の失敗などの) エラーが起こると -1 を返す。
ftw() は動的なデータ構造を用いるので、ツリー探索を安全に中断する唯一 の
方法は 0 以外の値を fn() の返り値とすることである。割り込みを扱うには、
例えば発生した割り込みをマークしておいて、 0 以外の値を返すようにするシ
グ ナルによりメモリリークを起こさずに探索を終了できるようにするには、シ
グナルハンドラで fn() がチェックするグローバルなフラグをセットするよ う
に すればよい。プログラムを終了させる場合以外は、 longjmp(3) を使用しな
いこと。
nftw()
関数 nftw() は ftw() と同じだが、引き数 flags が追加される点と 、 fn()
の引き数に ftwbuf が追加される点が異なる。
この flags 引き数は下記のフラグの 0 個以上の論理和を取ったものである:
FTW_ACTIONRETVAL (glibc 2.3.3 以降)
このフラグは glibc 固有である。このフラグをセットすると、 nftw()
の fn() の返り値の扱いが変わる。 fn() は以下の値のいずれか一つを
返す必要がある。
FTW_CONTINUE
nftw() は通常通り処理を続ける。
FTW_SKIP_SIBLINGS
fn() がこの値を返した場合、処理中のエントリの兄弟 (同じ階
層のエントリ) の処理はスキップされ、親ディレクトリで続 き
の処理が行われる。
FTW_SKIP_SUBTREE
fn() が 呼び出されたエントリがディレクトリ (typeflag が
FTW_D) の場合に、この値を返すと fn() の引き数として渡され
たディレクトリ内のエントリの処理が行われなくなる。 nftw()
は処理中のディレクトリの兄弟 (同じ階層のエントリ) から 処
理を続ける。
FTW_STOP
nftw () は、返り値 FTW_STOP ですぐに復帰する。
他の返り値は将来新しい動作に対応付けられる可能性がある。 fn() は
上記のリストにある値以外を返さないようにすべきである。
で FTW_ACTIONRETVAL が定義されるようにするには、機能検査
マクロ _GNU_SOURCE を定義しなければならない。
FTW_CHDIR
セットされると、ディレクトリの内容を処理する前にそのディレクトリ
に chdir(2) する。このフラグは、 fpath が属すディレクトリで何 ら
かの動作を実行する必要がある場合に便利である。
FTW_DEPTH
セットされると、帰りがけ順探索 (post-order traversal) を行う。つ
まり、ディレクトリそのものを引き数とした fn() 呼出しは、そのディ
レクトリに含まれるファイルとサブディレクトリに対する処理の「後で
」行われる (デフォルトでは、ディレクトリ自身の処理はディレクトリ
内のエントリより「前に」行なわれる)。
FTW_MOUNT
セットされると、同じファイルシステムの中だけを探索対象とする (つ
まり、マウントポイントをまたぐことはない)。
FTW_PHYS
セットされると、シンボリックリンクを辿らない (おそらくこちらが通
常 望ましい動作だろう)。セットされていないとシンボリックリンクを
辿るが、同じファイルが二回報告されることはない。
FTW_PHYS がセットされずに FTW_DEPTH がセットされると、自分自身に
対するシンボリックリンクを配下に持つディレクトリに対して fn() が
呼び出されることは決してない。
ディレクトリツリーのエントリ毎に、 nftw() は 4つの引き数で fn() を呼 び
出す。 fpath と sb は ftw() と同じである。 typeflag には、 ftw() で取り
得る値のいずれか、または以下の値のいずれかが渡される:
FTW_DP fpath がディレクトリで、かつ flags で FTW_DEPTH が指定されていた
。 fpath 配下のファイルとサブディレクトリは全て処理が終わってい
る。
FTW_SL fpath がシンボリックリンクで、かつ FTW_PHYS が flags にセット さ
れていた。
FTW_SLN
fpath がシンボリックリンクで、存在しないファイルを指している (こ
れがセットされるのは FTW_PHYS がセットされていない場合だけ で あ
る)。
nftw() が fn() を呼び出す際に渡す 4つめの引き数は FTW 型の構造体である
。
struct FTW {
int base;
int level;
};
base は、ファイル名 (basename 要素) の、 fpath で渡されるパス名の中での
オ フセットである。 level はディレクトリツリーでの fpath の深さを示す。
深さはディレクトリツリーのトップ (root) からの相対値である (dirpath は
深さ 0 である)。
返り値
これらの関数は、成功すると 0 を、エラーが発生すると -1 を返す。
fn() が 0 以外を返した場合、ディレクトリツリーの探索を終了し、 fn() が
返した値を ftw() や nftw() の結果として返す。
nftw() が FTW_ACTIONRETVAL フラグ付きで呼ばれた場合、ツリーの探索を終了
させるために fn() が使用できる、非 0 の値は FTW_STOP だけであり、この値
は nftw() の返り値として返される。
準拠
POSIX.1-2001, SVr4, SUSv1. POSIX.1-2008 は ftw() を廃止予定としてい る
。
注意
nftw() 関数と、 ftw() における FTW_SL は、SUSv1 で導入された。
ftw() で FTW_SL を一切使わないシステムや、存在しないファイルを指してい
るシンボリックリンクの場合にのみ FTW_SL を使うシステム、また ftw() が全
て のシンボリックリンクに対して FTW_SL を使うシステムもある。予測可能な
動作をさせるためには、 nftw() を使うこと。
Linux では、 libc4, libc5, glibc 2.0.6 は「stat できるがディレクトリ で
はないオブジェクト」 (ファイル, シンボリックリンク, fifo 等) に対しては
すべて FTW_F を使う。
nftw() 関数は glibc 2.1 以降で利用できる。
FTW_ACTIONRETVAL は glibc 固有である。
例
以下のプログラムは、一つ目のコマンドライン引き数を名前に持つパス以下 の
デ ィレクトリツリーを探索する。引き数が指定されなかった場合は、カレント
ディレクトリ以下を探索する。各々のファイルについて様々の情報が表示さ れ
る 。二番目のコマンドライン引き数に文字を指定することで、 nftw() を呼び
出す際に flags 引き数に渡す値を制御することができる。
#define _XOPEN_SOURCE 500
#include
#include
#include
#include
#include
static int
display_info(const char *fpath, const struct stat *sb,
int tflag, struct FTW *ftwbuf)
{
printf("%-3s %2d %7jd %-40s %d %s\n",
(tflag == FTW_D) ? "d" : (tflag == FTW_DNR) ? "dnr" :
(tflag == FTW_DP) ? "dp" : (tflag == FTW_F) ? "f" :
(tflag == FTW_NS) ? "ns" : (tflag == FTW_SL) ? "sl" :
(tflag == FTW_SLN) ? "sln" : "???",
ftwbuf->level, (intmax_t) sb->st_size,
fpath, ftwbuf->base, fpath + ftwbuf->base);
return 0; /* To tell nftw() to continue */
}
int
main(int argc, char *argv[])
{
int flags = 0;
if (argc > 2 && strchr(argv[2], 'd') != NULL)
flags |= FTW_DEPTH;
if (argc > 2 && strchr(argv[2], 'p') != NULL)
flags |= FTW_PHYS;
if (nftw((argc < 2) ? "." : argv[1], display_info, 20, flags)
== -1) {
perror("nftw");
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}
関連項目
stat(2), fts(3), readdir(3), feature_test_macros(7)
Linux 2008-08-06 FTW(3)
FTW(3) Linux Programmer’s Manual FTW(3)
NAME
ftw, nftw - file tree walk
SYNOPSIS
#include
int ftw(const char *dirpath,
int (*fn) (const char *fpath, const struct stat *sb,
int typeflag),
int nopenfd);
#define _XOPEN_SOURCE 500
#include
int nftw(const char *dirpath,
int (*fn) (const char *fpath, const struct stat *sb,
int typeflag, struct FTW *ftwbuf),
int nopenfd, int flags);
DESCRIPTION
ftw() walks through the directory tree that is located under the direc-
tory dirpath, and calls fn() once for each entry in the tree. By
default, directories are handled before the files and subdirectories
they contain (pre-order traversal).
To avoid using up all of the calling process’s file descriptors,
nopenfd specifies the maximum number of directories that ftw() will
hold open simultaneously. When the search depth exceeds this, ftw()
will become slower because directories have to be closed and reopened.
ftw() uses at most one file descriptor for each level in the directory
tree.
For each entry found in the tree, ftw() calls fn() with three argu-
ments: fpath, sb, and typeflag. fpath is the pathname of the entry
relative to dirpath. sb is a pointer to the stat structure returned by
a call to stat(2) for fpath. typeflag is an integer that has one of
the following values:
FTW_F fpath is a regular file.
FTW_D fpath is a directory.
FTW_DNR
fpath is a directory which can’t be read.
FTW_NS The stat(2) call failed on fpath, which is not a symbolic link.
If fpath is a symbolic link and stat(2) failed, POSIX.1-2001
states that it is undefined whether FTW_NS or FTW_SL (see below)
is passed in typeflag.
To stop the tree walk, fn() returns a non-zero value; this value will
become the return value of ftw(). As long as fn() returns 0, ftw()
will continue either until it has traversed the entire tree, in which
case it will return zero, or until it encounters an error (such as a
malloc(3) failure), in which case it will return -1.
Because ftw() uses dynamic data structures, the only safe way to exit
out of a tree walk is to return a non-zero value from fn(). To allow a
signal to terminate the walk without causing a memory leak, have the
handler set a global flag that is checked by fn(). Don’t use
longjmp(3) unless the program is going to terminate.
nftw()
The function nftw() is the same as ftw(), except that it has one addi-
tional argument, flags, and calls fn() with one more argument, ftwbuf.
This flags argument is formed by ORing zero or more of the following
flags:
FTW_ACTIONRETVAL (since glibc 2.3.3)
If this glibc-specific flag is set, then nftw() handles the
return value from fn() differently. fn() should return one of
the following values:
FTW_CONTINUE
Instructs nftw() to continue normally.
FTW_SKIP_SIBLINGS
If fn() returns this value, then siblings of the current
entry will be skipped, and processing continues in the
parent.
FTW_SKIP_SUBTREE
If fn() is called with an entry that is a directory
(typeflag is FTW_D), this return value will prevent
objects within that directory from being passed as argu-
ments to fn(). nftw() continues processing with the next
sibling of the directory.
FTW_STOP
Causes nftw() to return immediately with the return value
FTW_STOP.
Other return values could be associated with new actions in the
future; fn() should not return values other than those listed
above.
The feature test macro _GNU_SOURCE must be defined in order to
obtain the definition of FTW_ACTIONRETVAL from .
FTW_CHDIR
If set, do a chdir(2) to each directory before handling its con-
tents. This is useful if the program needs to perform some
action in the directory in which fpath resides.
FTW_DEPTH
If set, do a post-order traversal, that is, call fn() for the
directory itself after handling the contents of the directory
and its subdirectories. (By default, each directory is handled
before its contents.)
FTW_MOUNT
If set, stay within the same file system (i.e., do not cross
mount points).
FTW_PHYS
If set, do not follow symbolic links. (This is what you want.)
If not set, symbolic links are followed, but no file is reported
twice.
If FTW_PHYS is not set, but FTW_DEPTH is set, then the function
fn() is never called for a directory that would be a descendant
of itself.
For each entry in the directory tree, nftw() calls fn() with four argu-
ments. fpath and sb are as for ftw(). typeflag may receive any of the
same values as with ftw(), or any of the following values:
FTW_DP fpath is a directory, and FTW_DEPTH was specified in flags. All
of the files and subdirectories within fpath have been pro-
cessed.
FTW_SL fpath is a symbolic link, and FTW_PHYS was set in flags.
FTW_SLN
fpath is a symbolic link pointing to a nonexistent file. (This
occurs only if FTW_PHYS is not set.)
The fourth argument that nftw() supplies when calling fn() is a struc-
ture of type FTW:
struct FTW {
int base;
int level;
};
base is the offset of the filename (i.e., basename component) in the
pathname given in fpath. level is the depth of fpath in the directory
tree, relative to the root of the tree (dirpath, which has depth 0).
RETURN VALUE
These functions return 0 on success, and -1 if an error occurs.
If fn() returns non-zero, then the tree walk is terminated and the
value returned by fn() is returned as the result of ftw() or nftw().
If nftw() is called with the FTW_ACTIONRETVAL flag, then the only non-
zero value that should be used by fn() to terminate the tree walk is
FTW_STOP, and that value is returned as the result of nftw().
CONFORMING TO
POSIX.1-2001, SVr4, SUSv1. POSIX.1-2008 marks ftw() as obsolete.
NOTES
The function nftw() and the use of FTW_SL with ftw() were introduced in
SUSv1.
On some systems ftw() will never use FTW_SL, on other systems FTW_SL
occurs only for symbolic links that do not point to an existing file,
and again on other systems ftw() will use FTW_SL for each symbolic
link. For predictable control, use nftw().
Under Linux, libc4 and libc5 and glibc 2.0.6 will use FTW_F for all
objects (files, symbolic links, FIFOs, etc.) that can be stat’ed but
are not a directory.
The function nftw() is available since glibc 2.1.
FTW_ACTIONRETVAL is glibc-specific.
EXAMPLE
The following program traverses the directory tree under the path named
in its first command-line argument, or under the current directory if
no argument is supplied. It displays various information about each
file. The second command-line argument can be used to specify charac-
ters that control the value assigned to the flags argument when calling
nftw().
#define _XOPEN_SOURCE 500
#include
#include
#include
#include
#include
static int
display_info(const char *fpath, const struct stat *sb,
int tflag, struct FTW *ftwbuf)
{
printf("%-3s %2d %7jd %-40s %d %s\n",
(tflag == FTW_D) ? "d" : (tflag == FTW_DNR) ? "dnr" :
(tflag == FTW_DP) ? "dp" : (tflag == FTW_F) ? "f" :
(tflag == FTW_NS) ? "ns" : (tflag == FTW_SL) ? "sl" :
(tflag == FTW_SLN) ? "sln" : "???",
ftwbuf->level, (intmax_t) sb->st_size,
fpath, ftwbuf->base, fpath + ftwbuf->base);
return 0; /* To tell nftw() to continue */
}
int
main(int argc, char *argv[])
{
int flags = 0;
if (argc > 2 && strchr(argv[2], 'd') != NULL)
flags |= FTW_DEPTH;
if (argc > 2 && strchr(argv[2], 'p') != NULL)
flags |= FTW_PHYS;
if (nftw((argc < 2) ? "." : argv[1], display_info, 20, flags)
== -1) {
perror("nftw");
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}
SEE ALSO
stat(2), fts(3), readdir(3), feature_test_macros(7)
COLOPHON
This page is part of release 3.22 of the Linux man-pages project. A
description of the project, and information about reporting bugs, can
be found at http://www.kernel.org/doc/man-pages/.
Linux 2008-08-06 FTW(3)