Kannel: Open Source WAP and SMS gateway  $Revision: 5037 $
test_file_traversal.c File Reference
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>
#include <errno.h>
#include "gwlib/gwlib.h"

Go to the source code of this file.

Functions

static int count_file (const char *filename, const struct stat *sb, int tflag, void *ftwbuf)
 
static int for_each_file (const Octstr *dir_s, int ignore_err, int(*cb)(const char *, const struct stat *, int, void *))
 
int main (int argc, char **argv)
 

Variables

static Countercounter
 

Function Documentation

static int count_file ( const char *  filename,
const struct stat *  sb,
int  tflag,
void *  ftwbuf 
)
static

Definition at line 96 of file test_file_traversal.c.

References counter_increase().

Referenced by main().

97 {
99 
100  return 0;
101 }
unsigned long counter_increase(Counter *counter)
Definition: counter.c:123
static Counter * counter
static int for_each_file ( const Octstr dir_s,
int  ignore_err,
int(*)(const char *, const struct stat *, int, void *)  cb 
)
static

Definition at line 120 of file test_file_traversal.c.

References error(), filename, octstr_destroy(), octstr_format(), and octstr_get_cstr.

Referenced by main().

122 {
123  DIR *dir;
124  struct dirent *ent;
125  int ret = 0;
126 #ifndef _DIRENT_HAVE_D_TYPE
127  struct stat stat;
128 #endif
129 
130  if ((dir = opendir(octstr_get_cstr(dir_s))) == NULL) {
131  error(errno, "Could not open directory `%s'", octstr_get_cstr(dir_s));
132  return -1;
133  }
134  while ((ent = readdir(dir)) != NULL) {
135  Octstr *filename;
136  if (!(strcmp((char*)ent->d_name, "." ) != 0 && strcmp((char*)ent->d_name, ".." ) != 0))
137  continue;
138  filename = octstr_format("%S/%s", dir_s, ent->d_name);
139 #ifdef _DIRENT_HAVE_D_TYPE
140  if (ent->d_type == DT_DIR && for_each_file(filename, ignore_err, cb) == -1) {
141  ret = -1;
142  } else if (ent->d_type == DT_REG && cb != NULL) {
143  cb(octstr_get_cstr(filename), NULL, 0, NULL);
144  }
145 #else
146  if (lstat(octstr_get_cstr(filename), &stat) == -1) {
147  if (!ignore_err)
148  error(errno, "Could not get stat for `%s'", octstr_get_cstr(filename));
149  ret = -1;
150  } else if (S_ISDIR(stat.st_mode) && for_each_file(filename, ignore_err, cb) == -1) {
151  ret = -1;
152  } else if (S_ISREG(stat.st_mode) && cb != NULL) {
153  cb(octstr_get_cstr(filename), &stat, 0, NULL);
154  }
155 #endif
156  octstr_destroy(filename);
157  if (ret == -1 && ignore_err)
158  ret = 0;
159  else if (ret == -1)
160  break;
161  }
162  closedir(dir);
163 
164  return ret;
165 }
void error(int err, const char *fmt,...)
Definition: log.c:612
static int for_each_file(const Octstr *dir_s, int ignore_err, int(*cb)(const char *, const struct stat *, int, void *))
#define octstr_get_cstr(ostr)
Definition: octstr.h:233
Octstr * octstr_format(const char *fmt,...)
Definition: octstr.c:2462
void octstr_destroy(Octstr *ostr)
Definition: octstr.c:322
char filename[FILENAME_MAX+1]
Definition: log.c:135
Definition: octstr.c:118
int main ( int  argc,
char **  argv 
)

Definition at line 168 of file test_file_traversal.c.

References count_file(), counter_create(), counter_destroy(), counter_set(), counter_value(), debug(), for_each_file(), gwlib_init(), gwlib_shutdown(), octstr_create, octstr_destroy(), and start.

169 {
170  Octstr *os1;
171  Octstr *os2;
172  time_t start, diff;
173 
174  gwlib_init();
175 
176  os1 = octstr_create(argv[1]);
177  os2 = octstr_create(argv[2]);
178 
180  start = time(NULL);
181  for_each_file(os1, 1, count_file);
182  diff = (time(NULL) - start);
183  debug("",0,"file count: %ld in %lds", (long) counter_value(counter), (long) diff);
184 
185 #ifdef HAVE_NFTW
186  counter_set(counter, 0);
187  start = time(NULL);
188  for_each_file2(os2, 1, count_file2);
189  diff = (time(NULL) - start);
190  debug("",0,"file count: %ld in %lds", (long) counter_value(counter), (long) diff);
191 #endif
192 
194  octstr_destroy(os1);
195  octstr_destroy(os2);
196  gwlib_shutdown();
197  return 0;
198 }
static int for_each_file(const Octstr *dir_s, int ignore_err, int(*cb)(const char *, const struct stat *, int, void *))
void counter_destroy(Counter *counter)
Definition: counter.c:110
unsigned long counter_set(Counter *counter, unsigned long n)
Definition: counter.c:167
Counter * counter_create(void)
Definition: counter.c:94
static Counter * counter
static int count_file(const char *filename, const struct stat *sb, int tflag, void *ftwbuf)
void octstr_destroy(Octstr *ostr)
Definition: octstr.c:322
#define octstr_create(cstr)
Definition: octstr.h:125
unsigned long counter_value(Counter *counter)
Definition: counter.c:145
Definition: octstr.c:118
void debug(const char *place, int err, const char *fmt,...)
Definition: log.c:690
void gwlib_shutdown(void)
Definition: gwlib.c:94
void gwlib_init(void)
Definition: gwlib.c:78
static int start

Variable Documentation

Counter* counter
static

Definition at line 75 of file test_file_traversal.c.

See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.