Hide DIR definition by making it an opaque struct typedef.

Introduce dirfd() libc exported symbol replacing macro with same name,
preserve _dirfd() macro for internal use.

Replace dirp->dd_fd with dirfd() call. Avoid using dirfd as variable
name to prevent shadowing global symbol.

Sponsored by:	Google Summer Of Code 2011
This commit is contained in:
Gleb Kurtsou 2012-05-19 12:44:27 +00:00
parent 157bb65afd
commit 0bb2aabf26
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=235647
17 changed files with 139 additions and 34 deletions

View file

@ -55,24 +55,8 @@
/* definitions for library routines operating on directories. */
#define DIRBLKSIZ 1024
struct _telldir; /* see telldir.h */
struct pthread_mutex;
/* structure describing an open directory. */
typedef struct _dirdesc {
int dd_fd; /* file descriptor associated with directory */
long dd_loc; /* offset in current buffer */
long dd_size; /* amount of data returned by getdirentries */
char *dd_buf; /* data buffer */
int dd_len; /* size of data buffer */
long dd_seek; /* magic cookie returned by getdirentries */
long dd_rewind; /* magic cookie for rewinding */
int dd_flags; /* flags for readdir */
struct pthread_mutex *dd_lock; /* lock */
struct _telldir *dd_td; /* telldir position recording */
} DIR;
#define dirfd(dirp) ((dirp)->dd_fd)
struct _dirdesc;
typedef struct _dirdesc DIR;
/* flags for opendir2 */
#define DTF_HIDEW 0x0001 /* hide whiteout entries */
@ -91,6 +75,7 @@ typedef void * DIR;
__BEGIN_DECLS
#if __POSIX_VISIBLE >= 200809 || __XSI_VISIBLE >= 700
int alphasort(const struct dirent **, const struct dirent **);
int dirfd(DIR *);
#endif
#if __BSD_VISIBLE
DIR *__opendir2(const char *, int);

View file

@ -9,7 +9,7 @@ SRCS+= __getosreldate.c __xuname.c \
_thread_init.c \
alarm.c arc4random.c assert.c aux.c basename.c check_utility_compat.c \
clock.c closedir.c confstr.c \
crypt.c ctermid.c daemon.c devname.c dirname.c disklabel.c \
crypt.c ctermid.c daemon.c devname.c dirfd.c dirname.c disklabel.c \
dlfcn.c drand48.c elf_utils.c erand48.c err.c errlst.c errno.c \
exec.c fdevname.c feature_present.c fmtcheck.c fmtmsg.c fnmatch.c \
fpclassify.c frexp.c fstab.c ftok.c fts.c fts-compat.c ftw.c \

View file

@ -382,6 +382,7 @@ FBSD_1.2 {
};
FBSD_1.3 {
dirfd;
fdlopen;
__FreeBSD_libc_enter_restricted_mode;
getcontextx;

View file

@ -42,6 +42,7 @@ __FBSDID("$FreeBSD$");
#include "un-namespace.h"
#include "libc_private.h"
#include "gen-private.h"
#include "telldir.h"
/*

48
lib/libc/gen/dirfd.c Normal file
View file

@ -0,0 +1,48 @@
/*-
* Copyright (c) 1989, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $FreeBSD$
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include "namespace.h"
#include <sys/param.h>
#include <dirent.h>
#include "un-namespace.h"
#include "gen-private.h"
int
dirfd(DIR *dirp)
{
return (_dirfd(dirp));
}

View file

@ -52,6 +52,8 @@ __FBSDID("$FreeBSD$");
#include "fts-compat.h"
#include "un-namespace.h"
#include "gen-private.h"
FTSENT *__fts_children_44bsd(FTS *, int);
int __fts_close_44bsd(FTS *);
void *__fts_get_clientptr_44bsd(FTS *);
@ -711,7 +713,7 @@ fts_build(sp, type)
*/
cderrno = 0;
if (nlinks || type == BREAD) {
if (fts_safe_changedir(sp, cur, dirfd(dirp), NULL)) {
if (fts_safe_changedir(sp, cur, _dirfd(dirp), NULL)) {
if (nlinks && type == BREAD)
cur->fts_errno = errno;
cur->fts_flags |= FTS_DONTCHDIR;

View file

@ -52,6 +52,8 @@ __FBSDID("$FreeBSD$");
#include <unistd.h>
#include "un-namespace.h"
#include "gen-private.h"
static FTSENT *fts_alloc(FTS *, char *, size_t);
static FTSENT *fts_build(FTS *, int);
static void fts_lfree(FTSENT *);
@ -697,7 +699,7 @@ fts_build(FTS *sp, int type)
*/
cderrno = 0;
if (nlinks || type == BREAD) {
if (fts_safe_changedir(sp, cur, dirfd(dirp), NULL)) {
if (fts_safe_changedir(sp, cur, _dirfd(dirp), NULL)) {
if (nlinks && type == BREAD)
cur->fts_errno = errno;
cur->fts_flags |= FTS_DONTCHDIR;

View file

@ -0,0 +1,59 @@
/*-
* Copyright (c) 1989, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $FreeBSD$
*/
#ifndef _GEN_PRIVATE_H_
#define _GEN_PRIVATE_H_
struct _telldir; /* see telldir.h */
struct pthread_mutex;
/*
* Structure describing an open directory.
*
* NOTE. Change structure layout with care, at least dd_fd field has to
* remain unchanged to guarantee backward compatibility.
*/
struct _dirdesc {
int dd_fd; /* file descriptor associated with directory */
long dd_loc; /* offset in current buffer */
long dd_size; /* amount of data returned by getdirentries */
char *dd_buf; /* data buffer */
int dd_len; /* size of data buffer */
long dd_seek; /* magic cookie returned by getdirentries */
long dd_rewind; /* magic cookie for rewinding */
int dd_flags; /* flags for readdir */
struct pthread_mutex *dd_lock; /* lock */
struct _telldir *dd_td; /* telldir position recording */
};
#define _dirfd(dirp) ((dirp)->dd_fd)
#endif /* !_GEN_PRIVATE_H_ */

View file

@ -46,6 +46,8 @@ __FBSDID("$FreeBSD$");
#include <unistd.h>
#include "un-namespace.h"
#include "gen-private.h"
#define ISDOT(dp) \
(dp->d_name[0] == '.' && (dp->d_name[1] == '\0' || \
(dp->d_name[1] == '.' && dp->d_name[2] == '\0')))
@ -117,7 +119,7 @@ getcwd(pt, size)
for (first = 1;; first = 0) {
/* Stat the current level. */
if (dir != NULL ? _fstat(dirfd(dir), &s) : lstat(".", &s))
if (dir != NULL ? _fstat(_dirfd(dir), &s) : lstat(".", &s))
goto err;
/* Save current node values. */
@ -139,13 +141,13 @@ getcwd(pt, size)
}
/* Open and stat parent directory. */
fd = _openat(dir != NULL ? dirfd(dir) : AT_FDCWD,
fd = _openat(dir != NULL ? _dirfd(dir) : AT_FDCWD,
"..", O_RDONLY);
if (fd == -1)
goto err;
if (dir)
(void) closedir(dir);
if (!(dir = fdopendir(fd)) || _fstat(dirfd(dir), &s)) {
if (!(dir = fdopendir(fd)) || _fstat(_dirfd(dir), &s)) {
_close(fd);
goto err;
}
@ -171,7 +173,7 @@ getcwd(pt, size)
continue;
/* Save the first error for later. */
if (fstatat(dirfd(dir), dp->d_name, &s,
if (fstatat(_dirfd(dir), dp->d_name, &s,
AT_SYMLINK_NOFOLLOW)) {
if (!save_errno)
save_errno = errno;

View file

@ -46,6 +46,7 @@ __FBSDID("$FreeBSD$");
#include <unistd.h>
#include "un-namespace.h"
#include "gen-private.h"
#include "telldir.h"
static DIR * __opendir_common(int, const char *, int);

View file

@ -42,6 +42,7 @@ __FBSDID("$FreeBSD$");
#include "un-namespace.h"
#include "libc_private.h"
#include "gen-private.h"
#include "telldir.h"
/*

View file

@ -36,6 +36,7 @@ __FBSDID("$FreeBSD$");
#include <sys/types.h>
#include <dirent.h>
#include "gen-private.h"
#include "telldir.h"
void

View file

@ -40,6 +40,7 @@ __FBSDID("$FreeBSD$");
#include "un-namespace.h"
#include "libc_private.h"
#include "gen-private.h"
#include "telldir.h"
/*

View file

@ -43,6 +43,7 @@ __FBSDID("$FreeBSD$");
#include "un-namespace.h"
#include "libc_private.h"
#include "gen-private.h"
#include "telldir.h"
/*

View file

@ -292,7 +292,7 @@ do_update(const char *dev)
int error;
struct ucode_handler *handler;
struct datadir *dir;
DIR *dirfd;
DIR *dirp;
struct dirent *direntry;
char buf[MAXPATHLEN];
@ -321,12 +321,12 @@ do_update(const char *dev)
* Process every image in specified data directories.
*/
SLIST_FOREACH(dir, &datadirs, next) {
dirfd = opendir(dir->path);
if (dirfd == NULL) {
dirp = opendir(dir->path);
if (dirp == NULL) {
WARNX(1, "skipping directory %s: not accessible", dir->path);
continue;
}
while ((direntry = readdir(dirfd)) != NULL) {
while ((direntry = readdir(dirp)) != NULL) {
if (direntry->d_namlen == 0)
continue;
error = snprintf(buf, sizeof(buf), "%s/%s", dir->path,
@ -340,7 +340,7 @@ do_update(const char *dev)
}
handler->update(dev, buf);
}
error = closedir(dirfd);
error = closedir(dirp);
if (error != 0)
WARN(0, "closedir(%s)", dir->path);
}

View file

@ -130,7 +130,7 @@ getq(const struct printer *pp, struct jobqueue *(*namelist[]))
seteuid(uid);
return (-1);
}
if (fstat(dirp->dd_fd, &stbuf) < 0)
if (fstat(dirfd(dirp), &stbuf) < 0)
goto errdone;
seteuid(uid);

View file

@ -1451,7 +1451,7 @@ static void
delete_oldest_timelog(const struct conf_entry *ent, const char *archive_dir)
{
char *logfname, *s, *dir, errbuf[80];
int dirfd, i, logcnt, max_logcnt, valid;
int dir_fd, i, logcnt, max_logcnt, valid;
struct oldlog_entry *oldlogs;
size_t logfname_len;
struct dirent *dp;
@ -1486,7 +1486,7 @@ delete_oldest_timelog(const struct conf_entry *ent, const char *archive_dir)
/* First we create a 'list' of all archived logfiles */
if ((dirp = opendir(dir)) == NULL)
err(1, "Cannot open log directory '%s'", dir);
dirfd = dirfd(dirp);
dir_fd = dirfd(dirp);
while ((dp = readdir(dirp)) != NULL) {
if (dp->d_type != DT_REG)
continue;
@ -1578,7 +1578,7 @@ delete_oldest_timelog(const struct conf_entry *ent, const char *archive_dir)
if (noaction)
printf("\trm -f %s/%s\n", dir,
oldlogs[i].fname);
else if (unlinkat(dirfd, oldlogs[i].fname, 0) != 0) {
else if (unlinkat(dir_fd, oldlogs[i].fname, 0) != 0) {
snprintf(errbuf, sizeof(errbuf),
"Could not delet old logfile '%s'",
oldlogs[i].fname);