Manpage of FTS

FTS

Section: Linux Programmer's Manual (3)
Updated: 2016-03-15
Index
 

NAME

fts, fts_open, fts_read, fts_children, fts_set, fts_close - traverse a file hierarchy  

SYNOPSIS

#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 options);int fts_set(FTS *ftsp, FTSENT *f, int options);int fts_close(FTS *ftsp);
 

DESCRIPTION

The fts functions are provided for traversing file hierarchies. A simple overview is that the fts_open() function returns a "handle" on a file hierarchy, which 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 are defined (and typedef'd) in the include file <fts.h>. The first is FTS, the structure that represents the file hierarchy itself. The second is FTSENT, the structure that represents a file in the file hierarchy. Normally, an FTSENTstructure is returned for every file in the file hierarchy. In this manual page, "file" and "FTSENT structure" are generally interchangeable. The FTSENTstructure contains at least the following fields, which are described in greater detail below:

typedef struct _ftsent {
    unsigned short fts_info;     /* flags for FTSENT structure */
    char          *fts_accpath;  /* access path */
    char          *fts_path;     /* root path */
    short          fts_pathlen;  /* strlen(fts_path) */
    char          *fts_name;     /* filename */
    short          fts_namelen;  /* strlen(fts_name) */
    short          fts_level;    /* depth (-1 to N) */
    int            fts_errno;    /* file errno */
    long           fts_number;   /* local numeric value */
    void          *fts_pointer;  /* local address value */
    struct ftsent *fts_parent;   /* parent directory */
    struct ftsent *fts_link;     /* next file structure */
    struct ftsent *fts_cycle;    /* cycle structure */
    struct stat   *fts_statp;    /* stat(2) information */
} FTSENT;

These fields are defined as follows:

fts_info
One of the following flags describing the returned FTSENTstructure 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
A directory being visited in preorder.
FTS_DC
A directory that causes a cycle in the tree. (The fts_cyclefield of the FTSENTstructure will be filled in as well.)
FTS_DEFAULT
Any FTSENTstructure that represents a file type not explicitly described by one of the other fts_infovalues.
FTS_DNR
A directory which cannot be read. This is an error return, and the fts_errnofield will be set to indicate what caused the error.
FTS_DOT
A file named "." or ".." which was not specified as a filename to fts_open() (see FTS_SEEDOT).
FTS_DP
A directory being visited in postorder. The contents of the FTSENTstructure will be unchanged from when it was returned in preorder, that is, with the fts_infofield set to FTS_D.
FTS_ERR
This is an error return, and the fts_errnofield will be set to indicate what caused the error.
FTS_F
A regular file.
FTS_NS
A file for which no stat(2) information was available. The contents of the fts_statpfield are undefined. This is an error return, and the fts_errnofield will be set to indicate what caused the error.
FTS_NSOK
A file for which no stat(2) information was requested. The contents of the fts_statpfield are undefined.
FTS_SL
A symbolic link.
FTS_SLNONE
A symbolic link with a nonexistent target. The contents of the fts_statpfield reference the file characteristic information for the symbolic link itself.
fts_accpath
A path for accessing the file from the current directory.
fts_path
The path for the file relative to the root of the traversal. This path contains the path specified to fts_open() as a prefix.
fts_pathlen
The length of the string referenced by fts_path.
fts_name
The name of the file.
fts_namelen
The length of the string referenced by fts_name.
fts_level
The depth of the traversal, numbered from -1 to N, where this file was found. The FTSENTstructure representing the parent of the starting point (or root) of the traversal is numbered -1, and the FTSENTstructure for the root itself is numbered 0.
fts_errno
Upon return of a FTSENTstructure from the fts_children() or fts_read() functions, with its fts_infofield set to FTS_DNR, FTS_ERRor FTS_NS, the fts_errnofield contains the value of the external variable errnospecifying the cause of the error. Otherwise, the contents of the fts_errnofield are undefined.
fts_number
This field is provided for the use of the application program and is not modified by the fts functions. It is initialized to 0.
fts_pointer
This field is provided for the use of the application program and is not modified by the fts functions. It is initialized to NULL.
fts_parent
A pointer to the FTSENTstructure 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_numberand fts_pointerfields are guaranteed to be initialized.
fts_link
Upon return from the fts_children() function, the fts_linkfield points to the next structure in the NULL-terminated linked list of directory members. Otherwise, the contents of the fts_linkfield are undefined.
fts_cycle
If a directory causes a cycle in the hierarchy (see FTS_DC), either because of a hard link between two directories, or a symbolic link pointing to a directory, the fts_cyclefield of the structure will point to the FTSENTstructure in the hierarchy that references the same file as the current FTSENTstructure. Otherwise, the contents of the fts_cyclefield are undefined.
fts_statp
A pointer to stat(2) information for the file.

A single buffer is used for all of the paths of all of the files in the file hierarchy. Therefore, the fts_pathand fts_accpathfields are guaranteed to be null-terminated onlyfor the file most recently returned by fts_read(). To use these fields to reference any files represented by other FTSENTstructures will require that the path buffer be modified using the information contained in that FTSENTstructure's fts_pathlenfield. Any such modifications should be undone before further calls to fts_read() are attempted. The fts_namefield is always null-terminated.  

fts_open()

The fts_open() function takes a pointer to an array of character pointers naming one or more paths which make up a logical file hierarchy to be traversed. The array must be terminated by a null pointer.

There are a number of options, at least one of which (either FTS_LOGICALor FTS_PHYSICAL) must be specified. The options are selected by ORing the following values:

FTS_COMFOLLOW
This option causes any symbolic link specified as a root path to be followed immediately whether or not FTS_LOGICALis also specified.
FTS_LOGICAL
This option causes the fts routines to return FTSENTstructures for the targets of symbolic links instead of the symbolic links themselves. If this option is set, the only symbolic links for which FTSENTstructures are returned to the application are those referencing nonexistent files. Either FTS_LOGICALor FTS_PHYSICALmustbe provided to the fts_open() function.
FTS_NOCHDIR
As a performance optimization, the fts functions change directories as they walk the file hierarchy. This has the side-effect that an application cannot rely on being in any particular directory during the traversal. The FTS_NOCHDIRoption turns off this optimization, and the fts functions will not change the current directory. Note that applications should not themselves change their current directory and try to access files unless FTS_NOCHDIRis specified and absolute pathnames were provided as arguments to fts_open().
FTS_NOSTAT
By default, returned FTSENTstructures reference file characteristic information (the statpfield) for each file visited. This option relaxes that requirement as a performance optimization, allowing the fts functions to set the fts_infofield to FTS_NSOKand leave the contents of the statpfield undefined.
FTS_PHYSICAL
This option causes the fts routines to return FTSENTstructures for symbolic links themselves instead of the target files they point to. If this option is set, FTSENTstructures for all symbolic links in the hierarchy are returned to the application. Either FTS_LOGICALor FTS_PHYSICALmustbe provided to the fts_open() function.
FTS_SEEDOT
By default, unless they are specified as path arguments to fts_open(), any files named "." or ".." encountered in the file hierarchy are ignored. This option causes the fts routines to return FTSENTstructures for them.
FTS_XDEV
This option prevents fts from descending into directories that have a different device number than the file from which the descent began.

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 FTSENTstructures 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_pathand fts_pathlenfields of the FTSENTstructures may neverbe used in this comparison. If the fts_infofield is set to FTS_NSor FTS_NSOK, the fts_statpfield may not either. If the compar() argument is NULL, the directory traversal order is in the order listed in path_argvfor the root paths, and in the order listed in the directory for everything else.  

fts_read()

The fts_read() function returns a pointer to an FTSENTstructure describing a file in the hierarchy. Directories (that are readable and do not cause cycles) are visited at least twice, once in preorder and once in postorder. All other files are visited at least once. (Hard links between directories that do not cause cycles or symbolic links to symbolic links may cause files to be visited more than once, or directories more than twice.)

If all the members of the hierarchy have been returned, fts_read() returns NULL and sets the external variable errnoto 0. If an error unrelated to a file in the hierarchy occurs, fts_read() returns NULL and sets errnoappropriately. If an error related to a returned file occurs, a pointer to an FTSENTstructure is returned, and errnomay or may not have been set (see fts_info).

The FTSENTstructures returned by fts_read() may be overwritten after a call to fts_close() on the same file hierarchy stream, or, after a call to fts_read() on the same file hierarchy stream unless they represent a file of type directory, in which case they will not be overwritten until after a call to fts_read() after the FTSENTstructure has been returned by the function fts_read() in postorder.  

fts_children()

The fts_children() function returns a pointer to an FTSENTstructure describing the first entry in a NULL-terminated linked list of the files in the directory represented by the FTSENTstructure most recently returned by fts_read(). The list is linked through the fts_linkfield of the FTSENTstructure, and is ordered by the user-specified comparison function, if any. Repeated calls to fts_children() will re-create this linked list.

As a special case, if fts_read() has not yet been called for a hierarchy, fts_children() will return a pointer to the files in the logical directory specified to fts_open(), that is, the arguments specified to fts_open(). Otherwise, if the FTSENTstructure most recently returned by fts_read() is not a directory being visited in preorder, or the directory does not contain any files, fts_children() returns NULL and sets errnoto zero. If an error occurs, fts_children() returns NULL and sets errnoappropriately.

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

Optionmay be set to the following value:

FTS_NAMEONLY
Only the names of the files are needed. The contents of all the fields in the returned linked list of structures are undefined with the exception of the fts_nameand fts_namelenfields.
 

fts_set()

The function fts_set() allows the user application to determine further processing for the file fof the stream ftsp. The fts_set() function returns 0 on success, and -1 if an error occurs. Optionmust be set to one of the following values:
FTS_AGAIN
Revisit the file; any file type may be revisited. The next call to fts_read() will return the referenced file. The fts_statand fts_infofields of the structure will be reinitialized at that time, but no other fields will have been changed. This option is meaningful only for the most recently returned file from fts_read(). Normal use is for postorder directory visits, where it causes the directory to be revisited (in both preorder and postorder) as well as all of its descendants.
FTS_FOLLOW
The referenced file must be a symbolic link. If the referenced file is the one most recently returned by fts_read(), the next call to fts_read() returns the file with the fts_infoand fts_statpfields reinitialized to reflect the target of the symbolic link instead of the symbolic link itself. If the file is one of those most recently returned by fts_children(), the fts_infoand fts_statpfields of the structure, when returned by fts_read(), will reflect the target of the symbolic link instead of the symbolic link itself. In either case, if the target of the symbolic link does not exist, the fields of the returned structure will be unchanged and the fts_infofield will be set to FTS_SLNONE.
If the target of the link is a directory, the preorder return, followed by the return of all of its descendants, followed by a postorder return, is done.
FTS_SKIP
No descendants of this file are visited. The file may be one of those most recently returned by either fts_children() or fts_read().
 

fts_close()

The fts_close() function closes a file hierarchy stream ftspand 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.  

ERRORS

The function fts_open() may fail and set errnofor any of the errors specified for open(2) and malloc(3).

The function fts_close() may fail and set errnofor any of the errors specified for chdir(2) and close(2).

The functions fts_read() and fts_children() may fail and set errnofor any of the errors specified for chdir(2), malloc(3), opendir(3), readdir(3), and stat(2).

In addition, fts_children(), fts_open(), and fts_set() may fail and set errnoas follows:

EINVAL
The options were invalid.
 

VERSIONS

These functions are available in Linux since glibc2.  

ATTRIBUTES

For an explanation of the terms used in this section, see attributes(7).
InterfaceAttributeValue
fts_open(), fts_set(), fts_close() Thread safetyMT-Safe
fts_read(), fts_children() Thread safetyMT-Unsafe

 

CONFORMING TO

4.4BSD.  

BUGS

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).  

SEE ALSO

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


 

Index

NAME
SYNOPSIS
DESCRIPTION
fts_open()
fts_read()
fts_children()
fts_set()
fts_close()
ERRORS
VERSIONS
ATTRIBUTES
CONFORMING TO
BUGS
SEE ALSO

This document was created by man2html, using the manual pages.
Time: 16:30:21 GMT, October 09, 2016 Click Here!