summaryrefslogtreecommitdiffstats
path: root/bftw.h
blob: 45cc0c3cfa2282681de61f14c7f620ade2ad90e3 (plain)
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
/*********************************************************************
 * bfs                                                               *
 * Copyright (C) 2015 Tavian Barnes <tavianator@tavianator.com>      *
 *                                                                   *
 * This program is free software. It comes without any warranty, to  *
 * the extent permitted by applicable law. You can redistribute it   *
 * and/or modify it under the terms of the Do What The Fuck You Want *
 * To Public License, Version 2, as published by Sam Hocevar. See    *
 * the COPYING file or http://www.wtfpl.net/ for more details.       *
 *********************************************************************/

#ifndef BFS_BFTW_H
#define BFS_BFTW_H

#include <sys/stat.h>

/**
 * Data about the current file for the bftw() callback.
 */
struct BFTW {
        /** A stat() buffer; may be NULL if no stat() call was needed. */
	const struct stat *statbuf;
	/** A typeflag value (see below). */
	int typeflag;
	/** The string offset of the filename in the path. */
	int base;
	/** The depth of this file in the walk. */
	int level;
	/** The errno that occurred, if typeflag == BFTW_ERROR. */
	int error;
};

/**
 * Callback function type for bftw().
 *
 * @param fpath
 *         The path to the encountered file.
 * @param ftwbuf
 *         Additional data about the current file.
 * @param ptr
 *         The pointer passed to bftw().
 * @return
 *         An action value (see below).
 */
typedef int bftw_fn(const char *fpath, const struct BFTW *ftwbuf, void *ptr);

/**
 * Breadth First Tree Walk (or Better File Tree Walk).
 *
 * Like ftw(3) and nftw(3), this function walks a directory tree recursively,
 * and invokes a callback for each path it encounters.  However, bftw() operates
 * breadth-first.
 *
 * @param dirpath
 *         The starting path.
 * @param fn
 *         The callback to invoke.
 * @param nopenfd
 *         The maximum number of file descriptors to keep open.
 * @param flags
 *         Flags that control bftw() behavior (see below).
 * @param ptr
 *         A generic pointer which is passed to fn().
 * @return
 *         0 on success, or -1 on failure.
 */
int bftw(const char *dirpath, bftw_fn *fn, int nopenfd, int flags, void *ptr);

/** typeflag: Directory. */
#define BFTW_D        0
/** typeflag: Regular file. */
#define BFTW_R        1
/** typeflag: Symbolic link. */
#define BFTW_SL       2
/** typeflag: Unknown type. */
#define BFTW_UNKNOWN  3
/** typeflag: An error occurred for this file. */
#define BFTW_ERROR    4

/** action: Keep walking. */
#define BFTW_CONTINUE       0
/** action: Skip this path's siblings. */
#define BFTW_SKIP_SIBLINGS  1
/** action: Skip this path's children. */
#define BFTW_SKIP_SUBTREE   2
/** action: Stop walking. */
#define BFTW_STOP           3

/** flag: stat() each encountered file. */
#define BFTW_STAT     (1 << 0)
/** flag: Attempt to recover from encountered errors. */
#define BFTW_RECOVER  (1 << 1)

#endif // BFS_BFTW_H