Committing this as part 2: 2008-10-09 Paolo Bonzini <[EMAIL PROTECTED]> Bruno Haible <[EMAIL PROTECTED]>
* lib/fchdir.c (_gl_unregister_fd): New functions, extracted from rpl_close. (_gl_register_fd): New function, extracted from rpl_open. (rpl_close, rpl_closedir): Use _gl_unregister_fd. (rpl_open, rpl_opendir): Use _gl_register_fd. *** lib/fchdir.c.orig 2008-10-09 13:28:07.000000000 +0200 --- lib/fchdir.c 2008-10-09 13:27:26.000000000 +0200 *************** *** 1,5 **** /* fchdir replacement. ! Copyright (C) 2006, 2007 Free Software Foundation, Inc. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by --- 1,5 ---- /* fchdir replacement. ! Copyright (C) 2006-2008 Free Software Foundation, Inc. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by *************** *** 75,95 **** } } ! /* Override open() and close(), to keep track of the open file descriptors. */ ! int ! rpl_close (int fd) ! #undef close { ! int retval = close (fd); ! ! if (retval >= 0 && fd >= 0 && fd < dirs_allocated) { if (dirs[fd].name != NULL) free (dirs[fd].name); dirs[fd].name = NULL; dirs[fd].saved_errno = ENOTDIR; } return retval; } --- 75,120 ---- } } ! /* Hook into the gnulib replacements for open() and close() to keep track ! of the open file descriptors. */ ! void ! _gl_unregister_fd (int fd) { ! if (fd >= 0 && fd < dirs_allocated) { if (dirs[fd].name != NULL) free (dirs[fd].name); dirs[fd].name = NULL; dirs[fd].saved_errno = ENOTDIR; } + } + + void + _gl_register_fd (int fd, const char *filename) + { + struct stat statbuf; + + ensure_dirs_slot (fd); + if (fd < dirs_allocated + && fstat (fd, &statbuf) >= 0 && S_ISDIR (statbuf.st_mode)) + { + dirs[fd].name = canonicalize_file_name (filename); + if (dirs[fd].name == NULL) + dirs[fd].saved_errno = errno; + } + } + + /* Override open() and close(), to keep track of the open file descriptors. */ + + int + rpl_close (int fd) + #undef close + { + int retval = close (fd); + + if (retval >= 0) + _gl_unregister_fd (fd); return retval; } *************** *** 122,137 **** #endif fd = open (filename, flags, mode); if (fd >= 0) ! { ! ensure_dirs_slot (fd); ! if (fd < dirs_allocated ! && fstat (fd, &statbuf) >= 0 && S_ISDIR (statbuf.st_mode)) ! { ! dirs[fd].name = canonicalize_file_name (filename); ! if (dirs[fd].name == NULL) ! dirs[fd].saved_errno = errno; ! } ! } return fd; } --- 147,153 ---- #endif fd = open (filename, flags, mode); if (fd >= 0) ! _gl_register_fd (fd, filename); return fd; } *************** *** 145,157 **** int fd = dirfd (dp); int retval = closedir (dp); ! if (retval >= 0 && fd >= 0 && fd < dirs_allocated) ! { ! if (dirs[fd].name != NULL) ! free (dirs[fd].name); ! dirs[fd].name = NULL; ! dirs[fd].saved_errno = ENOTDIR; ! } return retval; } --- 161,168 ---- int fd = dirfd (dp); int retval = closedir (dp); ! if (retval >= 0) ! _gl_unregister_fd (fd); return retval; } *************** *** 166,180 **** { int fd = dirfd (dp); if (fd >= 0) ! { ! ensure_dirs_slot (fd); ! if (fd < dirs_allocated) ! { ! dirs[fd].name = canonicalize_file_name (filename); ! if (dirs[fd].name == NULL) ! dirs[fd].saved_errno = errno; ! } ! } } return dp; } --- 177,183 ---- { int fd = dirfd (dp); if (fd >= 0) ! _gl_register_fd (fd, filename); } return dp; }