Man page - ftw(3)

Packages contains this manual

Available languages:

en fr ja ru

Manual

FTW

名 前
書 式
説 明
ftw()
返 り 値
バ ー ジ ョ ン
属 性
準 拠
注 意
バ グ

プ ロ グ ラ ム の ソ ー ス
関 連 項 目
こ の 文 書 に つ い て

名 前

ftw, nftw - フ ァ イ ル ツ リ ー を 歩 き ま わ る

書 式

#include <ftw.h>

int nftw(const char * dirpath ,
int (*
fn ) (const char * fpath , const struct stat * sb ,
int
typeflag , struct FTW * ftwbuf ),
int
nopenfd , int flags );

#include <ftw.h>

int ftw(const char * dirpath ,
int (*
fn ) (const char * fpath , const struct stat * sb ,
int
typeflag ),
int
nopenfd );

glibc 向 け の 機 能 検 査 マ ク ロ の 要 件 ( feature_test_macros (7) 参 照 ):

nftw (): _XOPEN_SOURCE >= 500

説 明

nftw () walks through the directory tree that is located under the directory dirpath , and calls fn () once for each entry in the tree. By default, directories are handled before the files and subdirectories they contain (preorder traversal).

To avoid using up all of the calling process’s file descriptors, nopenfd specifies the maximum number of directories that nftw () will hold open simultaneously. When the search depth exceeds this, nftw () will become slower because directories have to be closed and reopened. nftw () uses at most one file descriptor for each level in the directory tree.

For each entry found in the tree, nftw () calls fn () with four arguments: fpath , sb , typeflag , and ftwbuf . fpath is the pathname of the entry, and is expressed either as a pathname relative to the calling process’s current working directory at the time of the call to nftw (), if dirpath was expressed as a relative pathname, or as an absolute pathname, if dirpath was expressed as an absolute pathname. sb is a pointer to the stat structure returned by a call to stat (2) for fpath .

The typeflag argument passed to fn () is an integer that has one of the following values:

FTW_F

fpath が 通 常 の フ ァ イ ル で あ る

FTW_D

fpath が デ ィ レ ク ト リ で あ る

FTW_DNR

fpath が 読 み 込 み で き な い デ ィ レ ク ト リ で あ る

FTW_DP

fpath が デ ィ レ ク ト リ で 、 か つ flags FTW_DEPTH が 指 定 さ れ て い た ( FTW_DEPTH flags に 指 定 さ れ て い な か っ た 場 合 、 デ ィ レ ク ト リ に 対 し て は 常 に typeflag FTW_D fn () が 呼 び 出 さ れ る )。 fpath 配 下 の フ ァ イ ル と サ ブ デ ィ レ ク ト リ は 全 て 処 理 が 終 わ っ て い る 。

FTW_NS

The stat (2) call failed on fpath , which is not a symbolic link. The probable cause for this is that the caller had read permission on the parent directory, so that the filename fpath could be seen, but did not have execute permission, so that the file could not be reached for stat (2). The contents of the buffer pointed to by sb are undefined.

FTW_SL

fpath が シ ン ボ リ ッ ク リ ン ク で 、 か つ FTW_PHYS flags に セ ッ ト さ れ て い た 。

FTW_SLN

fpath is a symbolic link pointing to a nonexistent file. (This occurs only if FTW_PHYS is not set.) In this case the sb argument passed to fn () contains information returned by performing lstat (2) on the "dangling" symbolic link. (But see BUGS.)

The fourth argument ( ftwbuf ) that nftw () supplies when calling fn () is a pointer to a structure of type FTW :

struct FTW {
int base;
int level;
};

base は 、 フ ァ イ ル 名 (basename 要 素 ) の 、 fpath で 渡 さ れ る パ ス 名 の 中 で の オ フ セ ッ ト で あ る 。 level は デ ィ レ ク ト リ ツ リ ー で の fpath の 深 さ を 示 す 。 深 さ は デ ィ レ ク ト リ ツ リ ー の ト ッ プ (root) か ら の 相 対 値 で あ る ( dirpath は 深 さ 0 で あ る )。

ツ リ ー の 探 索 を 止 め た い 場 合 は 、 fn () が 0 以 外 の 値 を 返 せ ば 良 い (こ の 値 は nftw () 自 身 の 戻 り 値 と な る )。 そ れ 以 外 の 場 合 は nftw () は ツ リ ー 全 体 の 探 索 を 続 け 、 す べ て の ツ リ ー を 探 索 し 終 え た と こ ろ で 0 を 返 す 。 探 索 中 に ( malloc (3) の 失 敗 な ど の ) エ ラ ー が 起 こ る と -1 を 返 す 。

nftw () は 動 的 な デ ー タ 構 造 を 用 い る の で 、 ツ リ ー 探 索 を 安 全 に 中 断 す る 唯 一 の 方 法 は 0 以 外 の 値 を fn () の 返 り 値 と す る こ と で あ る 。 割 り 込 み を 扱 う に は 、 例 え ば 発 生 し た 割 り 込 み を マ ー ク し て お い て 、 0 以 外 の 値 を 返 す よ う に す る シ グ ナ ル に よ り メ モ リ ー リ ー ク を 起 こ さ ず に 探 索 を 終 了 で き る よ う に す る に は 、 シ グ ナ ル ハ ン ド ラ ー で fn () が チ ェ ッ ク す る グ ロ ー バ ル な フ ラ グ を セ ッ ト す る よ う に す れ ば よ い 。 プ ロ グ ラ ム を 終 了 さ せ る 場 合 以 外 は 、 longjmp (3) を 使 用 し な い こ と 。

The flags argument of nftw () is formed by ORing zero or more of the following flags:
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.h> FTW_ACTIONRETVAL の 定 義 が 有 効 に す る た め に は 、 (「 ど の 」 ヘ ッ ダ ー フ ァ イ ル を イ ン ク ル ー ド す る よ り も 前 に ) 機 能 検 査 マ ク ロ _GNU_SOURCE を 定 義 し な け れ ば な ら な い 。

FTW_CHDIR

セ ッ ト さ れ る と 、 デ ィ レ ク ト リ の 内 容 を 処 理 す る 前 に そ の デ ィ レ ク ト リ に chdir (2) す る 。 こ の フ ラ グ は 、 fpath が 属 す デ ィ レ ク ト リ で 何 ら か の 動 作 を 実 行 す る 必 要 が あ る 場 合 に 便 利 で あ る 。 (こ の フ ラ グ を 指 定 し て も fn fpath 引 数 で 渡 さ れ る パ ス 名 に は 影 響 し な い 。 )

FTW_DEPTH

セ ッ ト さ れ る と 、 帰 り が け 順 探 索 (post-order traversal) を 行 う 。 つ ま り 、 デ ィ レ ク ト リ そ の も の を 引 数 と し た fn () 呼 出 し は 、 そ の デ ィ レ ク ト リ に 含 ま れ る フ ァ イ ル と サ ブ デ ィ レ ク ト リ に 対 す る 処 理 の 「 後 で 」 行 わ れ る (デ フ ォ ル ト で は 、 デ ィ レ ク ト リ 自 身 の 処 理 は デ ィ レ ク ト リ 内 の エ ン ト リ ー よ り 「 前 に 」 行 な わ れ る )。

FTW_MOUNT

セ ッ ト さ れ る と 、 同 じ フ ァ イ ル シ ス テ ム の 中 だ け を 探 索 対 象 と す る (つ ま り 、 マ ウ ン ト ポ イ ン ト を ま た ぐ こ と は な い )。

FTW_PHYS

セ ッ ト さ れ る と 、 シ ン ボ リ ッ ク リ ン ク を 辿 ら な い (お そ ら く こ ち ら が 通 常 望 ま し い 動 作 だ ろ う )。 セ ッ ト さ れ て い な い と シ ン ボ リ ッ ク リ ン ク を 辿 る が 、 同 じ フ ァ イ ル が 二 回 報 告 さ れ る こ と は な い 。

FTW_PHYS が セ ッ ト さ れ ず に FTW_DEPTH が セ ッ ト さ れ る と 、 自 分 自 身 に 対 す る シ ン ボ リ ッ ク リ ン ク を 配 下 に 持 つ デ ィ レ ク ト リ に 対 し て fn () が 呼 び 出 さ れ る こ と は 決 し て な い 。

ftw()

ftw () is an older function that offers a subset of the functionality of nftw (). The notable differences are as follows:

*

ftw () has no flags argument. It behaves the same as when nftw () is called with flags specified as zero.

*

The callback function, fn (), is not supplied with a fourth argument.

*

The range of values that is passed via the typeflag argument supplied to fn () is smaller: just FTW_F , FTW_D , FTW_DNR , FTW_NS , and (possibly) FTW_SL .

返 り 値

こ れ ら の 関 数 は 、 成 功 す る と 0 を 、 エ ラ ー が 発 生 す る と -1 を 返 す 。

fn () が 0 以 外 を 返 し た 場 合 、 デ ィ レ ク ト リ ツ リ ー の 探 索 を 終 了 し 、 fn () が 返 し た 値 を ftw () や nftw () の 結 果 と し て 返 す 。

nftw () が FTW_ACTIONRETVAL フ ラ グ 付 き で 呼 ば れ た 場 合 、 ツ リ ー の 探 索 を 終 了 さ せ る た め に fn () が 使 用 で き る 、 非 0 の 値 は FTW_STOP だ け で あ り 、 こ の 値 は nftw () の 返 り 値 と し て 返 さ れ る 。

バ ー ジ ョ ン

nftw () は バ ー ジ ョ ン 2.1 以 降 の glibc で 利 用 で き る 。

属 性

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

Image grohtml-43293-1.png

準 拠

POSIX.1-2001, POSIX.1-2008, SVr4, SUSv1. POSIX.1-2008 は ftw () を 廃 止 予 定 と し て い る 。

注 意

POSIX.1-2008 notes that the results are unspecified if fn does not preserve the current working directory.

nftw () 関 数 と 、 ftw () に お け る FTW_SL は 、 SUSv1 で 導 入 さ れ た 。

In some implementations (e.g., glibc), 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. If fpath is a symbolic link and stat (2) failed, POSIX.1-2008 states that it is undefined whether FTW_NS or FTW_SL is passed in typeflag . For predictable results, use nftw ().

バ グ

According to POSIX.1-2008, when the typeflag argument passed to fn () contains FTW_SLN , the buffer pointed to by sb should contain information about the dangling symbolic link (obtained by calling lstat (2) on the link). Early glibc versions correctly followed the POSIX specification on this point. However, as a result of a regression introduced in glibc 2.4, the contents of the buffer pointed to by sb were undefined when FTW_SLN is passed in typeflag . (More precisely, the contents of the buffer were left unchanged in this case.) This regression was eventually fixed in glibc 2.30, so that the glibc implementation (once more) follows the POSIX specification.

以 下 の プ ロ グ ラ ム は 、 一 つ 目 の コ マ ン ド ラ イ ン 引 数 を 名 前 に 持 つ パ ス 以 下 の デ ィ レ ク ト リ ツ リ ー を 探 索 す る 。 引 数 が 指 定 さ れ な か っ た 場 合 は 、 カ レ ン ト デ ィ レ ク ト リ 以 下 を 探 索 す る 。 各 々 の フ ァ イ ル に つ い て 様 々 の 情 報 が 表 示 さ れ る 。 二 番 目 の コ マ ン ド ラ イ ン 引 数 に 文 字 を 指 定 す る こ と で 、 nftw () を 呼 び 出 す 際 に flags 引 数 に 渡 す 値 を 制 御 す る こ と が で き る 。

プ ロ グ ラ ム の ソ ー ス

#define _XOPEN_SOURCE 500
#include <ftw.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>

static int
display_info(const char *fpath, const struct stat *sb,
int tflag, struct FTW *ftwbuf)
{
printf("%-3s %2d ",
(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);

if (tflag == FTW_NS)
printf("-------");
else
printf("%7jd", (intmax_t) sb->st_size);

printf(" %-40s %d %s\n",
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)

こ の 文 書 に つ い て

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