2000-08-28 03:18:25 +08:00
|
|
|
/** \ingroup rpmbuild
|
|
|
|
* \file build/myftw.c
|
|
|
|
* Modified ftw() -- uses Lstat() instead of stat().
|
|
|
|
*/
|
1996-05-23 01:24:20 +08:00
|
|
|
|
|
|
|
/* Copyright (C) 1992, 1994, 1995 Free Software Foundation, Inc.
|
|
|
|
This file is part of the GNU C Library.
|
|
|
|
Contributed by Ian Lance Taylor (ian@airs.com).
|
|
|
|
|
|
|
|
The GNU C Library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Library General Public License as
|
|
|
|
published by the Free Software Foundation; either version 2 of the
|
|
|
|
License, or (at your option) any later version.
|
|
|
|
|
|
|
|
The GNU C Library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Library General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Library General Public
|
|
|
|
License along with the GNU C Library; see the file COPYING.LIB. If
|
|
|
|
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
|
|
|
|
Cambridge, MA 02139, USA. */
|
|
|
|
|
1998-07-26 05:00:26 +08:00
|
|
|
#include "system.h"
|
1996-09-26 03:17:15 +08:00
|
|
|
|
1999-11-20 02:19:41 +08:00
|
|
|
#include <rpmio.h>
|
|
|
|
|
1996-09-26 03:17:15 +08:00
|
|
|
#ifndef NAMLEN
|
|
|
|
#define NAMLEN(a) strlen((a)->d_name)
|
|
|
|
#endif
|
|
|
|
|
1997-01-18 00:23:22 +08:00
|
|
|
#ifndef PATH_MAX
|
|
|
|
#ifdef _POSIX_VERSION
|
|
|
|
#define PATH_MAX _POSIX_PATH_MAX
|
|
|
|
#else
|
|
|
|
#ifdef MAXPATHLEN
|
|
|
|
#define PATH_MAX MAXPATHLEN
|
|
|
|
#else
|
|
|
|
#define PATH_MAX 1024
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
1996-05-23 01:24:20 +08:00
|
|
|
#include "myftw.h"
|
|
|
|
|
|
|
|
/* Traverse one level of a directory tree. */
|
|
|
|
|
|
|
|
static int
|
|
|
|
myftw_dir (DIR **dirs, int level, int descriptors,
|
|
|
|
char *dir, size_t len,
|
1998-11-25 08:42:36 +08:00
|
|
|
myftwFunc func,
|
1998-01-13 05:31:29 +08:00
|
|
|
void *fl)
|
1996-05-23 01:24:20 +08:00
|
|
|
{
|
|
|
|
int got;
|
|
|
|
struct dirent *entry;
|
|
|
|
int d_namlen;
|
|
|
|
|
|
|
|
got = 0;
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
|
1999-11-24 08:03:54 +08:00
|
|
|
while ((entry = Readdir (dirs[level])) != NULL)
|
1996-05-23 01:24:20 +08:00
|
|
|
{
|
|
|
|
struct stat s;
|
|
|
|
int flag, retval, newlev = 0;
|
|
|
|
|
|
|
|
++got;
|
|
|
|
|
|
|
|
if (entry->d_name[0] == '.'
|
|
|
|
&& (entry->d_name [1] == '\0' ||
|
|
|
|
(entry->d_name [2] == '\0' && entry->d_name[1] == '.')))
|
|
|
|
{
|
|
|
|
errno = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
1996-09-26 03:17:15 +08:00
|
|
|
d_namlen = NAMLEN(entry) + 1;
|
1996-05-23 01:24:20 +08:00
|
|
|
if (d_namlen + len > PATH_MAX)
|
|
|
|
{
|
|
|
|
#ifdef ENAMETOOLONG
|
|
|
|
errno = ENAMETOOLONG;
|
|
|
|
#else
|
|
|
|
errno = ENOMEM;
|
|
|
|
#endif
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
dir[len] = '/';
|
1996-06-19 01:06:32 +08:00
|
|
|
memcpy ((void *) (dir + len + 1), (void *) entry->d_name, d_namlen);
|
1996-05-23 01:24:20 +08:00
|
|
|
|
1999-11-20 02:19:41 +08:00
|
|
|
if (Lstat (dir, &s) < 0)
|
1996-05-23 01:24:20 +08:00
|
|
|
{
|
|
|
|
/* Following POSIX.1 2.4 ENOENT is returned if the file cannot
|
1999-11-24 08:03:54 +08:00
|
|
|
* be stat'ed. This can happen for a file returned by Readdir
|
1996-05-23 01:24:20 +08:00
|
|
|
* if it's an unresolved symbolic link. This should be regarded
|
|
|
|
* as an forgivable error. -- Uli. */
|
|
|
|
if (errno != EACCES && errno != ENOENT)
|
|
|
|
return -1;
|
1996-09-29 10:29:50 +08:00
|
|
|
flag = MYFTW_NS;
|
1996-05-23 01:24:20 +08:00
|
|
|
}
|
|
|
|
else if (S_ISDIR (s.st_mode))
|
|
|
|
{
|
|
|
|
newlev = (level + 1) % descriptors;
|
|
|
|
|
|
|
|
if (dirs[newlev] != NULL)
|
1999-11-24 08:03:54 +08:00
|
|
|
Closedir (dirs[newlev]);
|
1996-05-23 01:24:20 +08:00
|
|
|
|
1999-11-24 08:03:54 +08:00
|
|
|
dirs[newlev] = Opendir (dir);
|
1996-05-23 01:24:20 +08:00
|
|
|
if (dirs[newlev] != NULL)
|
1996-09-29 10:29:50 +08:00
|
|
|
flag = MYFTW_D;
|
1996-05-23 01:24:20 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (errno != EACCES)
|
|
|
|
return -1;
|
1996-09-29 10:29:50 +08:00
|
|
|
flag = MYFTW_DNR;
|
1996-05-23 01:24:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
1996-09-29 10:29:50 +08:00
|
|
|
flag = MYFTW_F;
|
1996-05-23 01:24:20 +08:00
|
|
|
|
1998-01-13 05:31:29 +08:00
|
|
|
retval = (*func) (fl, dir, &s);
|
1996-05-23 01:24:20 +08:00
|
|
|
|
1996-09-29 10:29:50 +08:00
|
|
|
if (flag == MYFTW_D)
|
1996-05-23 01:24:20 +08:00
|
|
|
{
|
|
|
|
if (retval == 0)
|
|
|
|
retval = myftw_dir (dirs, newlev, descriptors, dir,
|
1998-01-13 05:31:29 +08:00
|
|
|
d_namlen + len, func, fl);
|
1996-05-23 01:24:20 +08:00
|
|
|
if (dirs[newlev] != NULL)
|
|
|
|
{
|
|
|
|
int save;
|
|
|
|
|
|
|
|
save = errno;
|
1999-11-24 08:03:54 +08:00
|
|
|
Closedir (dirs[newlev]);
|
1996-05-23 01:24:20 +08:00
|
|
|
errno = save;
|
|
|
|
dirs[newlev] = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (retval != 0)
|
|
|
|
return retval;
|
|
|
|
|
|
|
|
if (dirs[level] == NULL)
|
|
|
|
{
|
|
|
|
int skip;
|
|
|
|
|
|
|
|
dir[len] = '\0';
|
1999-11-24 08:03:54 +08:00
|
|
|
dirs[level] = Opendir (dir);
|
1996-05-23 01:24:20 +08:00
|
|
|
if (dirs[level] == NULL)
|
|
|
|
return -1;
|
|
|
|
skip = got;
|
|
|
|
while (skip-- != 0)
|
|
|
|
{
|
|
|
|
errno = 0;
|
1999-11-24 08:03:54 +08:00
|
|
|
if (Readdir (dirs[level]) == NULL)
|
1996-05-23 01:24:20 +08:00
|
|
|
return errno == 0 ? 0 : -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return errno == 0 ? 0 : -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Call a function on every element in a directory tree. */
|
|
|
|
|
|
|
|
|
2000-01-25 04:02:32 +08:00
|
|
|
/** */
|
1996-06-19 01:06:32 +08:00
|
|
|
int myftw (const char *dir,
|
1998-01-13 05:31:29 +08:00
|
|
|
int descriptors,
|
1998-11-25 08:42:36 +08:00
|
|
|
myftwFunc func,
|
1998-01-13 05:31:29 +08:00
|
|
|
void *fl)
|
1996-05-23 01:24:20 +08:00
|
|
|
{
|
|
|
|
DIR **dirs;
|
|
|
|
size_t len;
|
|
|
|
char buf[PATH_MAX + 1];
|
|
|
|
struct stat s;
|
|
|
|
int flag, retval;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (descriptors <= 0)
|
|
|
|
descriptors = 1;
|
|
|
|
|
1998-11-17 05:40:28 +08:00
|
|
|
/*@access DIR@*/
|
1996-09-26 03:17:15 +08:00
|
|
|
dirs = (DIR **) alloca (descriptors * sizeof (DIR *));
|
1996-05-23 01:24:20 +08:00
|
|
|
i = descriptors;
|
|
|
|
while (i-- > 0)
|
|
|
|
dirs[i] = NULL;
|
|
|
|
|
1999-11-20 02:19:41 +08:00
|
|
|
if (Lstat (dir, &s) < 0)
|
1996-05-23 01:24:20 +08:00
|
|
|
{
|
|
|
|
/* Following POSIX.1 2.4 ENOENT is returned if the file cannot
|
1999-11-24 08:03:54 +08:00
|
|
|
* be stat'ed. This can happen for a file returned by Readdir
|
1996-05-23 01:24:20 +08:00
|
|
|
* if it's an unresolved symbolic link. This should be regarded
|
|
|
|
* as an forgivable error. -- Uli. */
|
|
|
|
if (errno != EACCES && errno != ENOENT)
|
|
|
|
return -1;
|
1996-09-29 10:29:50 +08:00
|
|
|
flag = MYFTW_NS;
|
1996-05-23 01:24:20 +08:00
|
|
|
}
|
|
|
|
else if (S_ISDIR (s.st_mode))
|
|
|
|
{
|
1999-11-24 08:03:54 +08:00
|
|
|
dirs[0] = Opendir (dir);
|
1996-05-23 01:24:20 +08:00
|
|
|
if (dirs[0] != NULL)
|
1996-09-29 10:29:50 +08:00
|
|
|
flag = MYFTW_D;
|
1996-05-23 01:24:20 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (errno != EACCES)
|
|
|
|
return -1;
|
1996-09-29 10:29:50 +08:00
|
|
|
flag = MYFTW_DNR;
|
1996-05-23 01:24:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
1996-09-29 10:29:50 +08:00
|
|
|
flag = MYFTW_F;
|
1996-05-23 01:24:20 +08:00
|
|
|
|
|
|
|
len = strlen (dir);
|
1996-06-19 01:06:32 +08:00
|
|
|
memcpy ((void *) buf, (void *) dir, len + 1);
|
1996-05-23 01:24:20 +08:00
|
|
|
|
1998-01-13 05:31:29 +08:00
|
|
|
retval = (*func) (fl, buf, &s);
|
1996-05-23 01:24:20 +08:00
|
|
|
|
1996-09-29 10:29:50 +08:00
|
|
|
if (flag == MYFTW_D)
|
1996-05-23 01:24:20 +08:00
|
|
|
{
|
|
|
|
if (retval == 0)
|
1998-01-13 05:31:29 +08:00
|
|
|
retval = myftw_dir (dirs, 0, descriptors, buf, len, func, fl);
|
1996-05-23 01:24:20 +08:00
|
|
|
if (dirs[0] != NULL)
|
|
|
|
{
|
|
|
|
int save;
|
|
|
|
|
|
|
|
save = errno;
|
1999-11-24 08:03:54 +08:00
|
|
|
Closedir (dirs[0]);
|
1996-05-23 01:24:20 +08:00
|
|
|
errno = save;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|