Sunday, November 30, 2008

ftw example for systems comparison.

ftw(3) has some inconsistencies.

For example ftw don't call its processing function for symlinks pointed from starting directory to starting directory, or its subdirs.

However, link to parent dir from starting directory reported in further walk.

From the other side, ftw checks and doesn't segfault in looping symlinks.

Checked under Linux 2.6.

source of example:

#include <ftw.h>
#include <stdio.h>
/*
* example for
* int ftw(const char *path, int (*fn)(const char *,
* const struct stat *ptr, int flag, int ndirs);
*/

int ffn(const char *path, const struct stat *ptr, int flag)
{
printf("Found\n path:%s\nflag%d\n", path, flag);
return 0;
}

int main(int argc, char ** argv)
{
char * inipath;
// example:
if (argv[1] && *argv[1])
{
inipath = argv[1];
}
else
{
fprintf(stderr,"usage: exftw PATH\n");
exit(-1);
}

if(ftw(inipath,ffn,20) != 0)
{
perror("ftw");exit(2);
}

return 0;
}

note: on my system it processed homedir with links for one minute.
when i have removed output by printf in ffn function from code, the time was just 1 second;

It looks like it is fast enough in comparison with output.

to check previous statement:


gcc exftw.c -o exftw
ln -s . dirlnsym
./exftw
./exftw | grep dirlnsym
# no reported dirlnsym reported
rm dirlnsym
ln -s ../../ dirlnsym
./exftw | grep dirlnsym
# ok; all reported and no looping

No comments: