Back to index

glibc  2.9
readdir64_r.c
Go to the documentation of this file.
00001 /* Copyright (C) 2001,02 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.  */
00018 
00019 #include <dirent.h>
00020 #include <stddef.h>
00021 #include <string.h>
00022 #include <hurd.h>
00023 #include <hurd/fs.h>
00024 #include <hurd/fd.h>
00025 #include "dirstream.h"
00026 
00027 /* Read a directory entry from DIRP.  */
00028 int
00029 __readdir64_r (DIR *dirp, struct dirent64 *entry, struct dirent64 **result)
00030 {
00031   struct dirent64 *dp;
00032   error_t err = 0;
00033 
00034   if (dirp == NULL)
00035     {
00036       errno = EINVAL;
00037       return errno;
00038     }
00039 
00040   __libc_lock_lock (dirp->__lock);
00041 
00042   do
00043     {
00044       if (dirp->__ptr - dirp->__data >= dirp->__size)
00045        {
00046          /* We've emptied out our buffer.  Refill it.  */
00047 
00048          char *data = dirp->__data;
00049          int nentries;
00050 
00051          if (err = HURD_FD_PORT_USE (dirp->__fd,
00052                                   __dir_readdir (port,
00053                                                &data, &dirp->__size,
00054                                                dirp->__entry_ptr,
00055                                                -1, 0, &nentries)))
00056            {
00057              __hurd_fail (err);
00058              dp = NULL;
00059              break;
00060            }
00061 
00062          /* DATA now corresponds to entry index DIRP->__entry_ptr.  */
00063          dirp->__entry_data = dirp->__entry_ptr;
00064 
00065          if (data != dirp->__data)
00066            {
00067              /* The data was passed out of line, so our old buffer is no
00068                longer useful.  Deallocate the old buffer and reset our
00069                information for the new buffer.  */
00070              __vm_deallocate (__mach_task_self (),
00071                             (vm_address_t) dirp->__data,
00072                             dirp->__allocation);
00073              dirp->__data = data;
00074              dirp->__allocation = round_page (dirp->__size);
00075            }
00076 
00077          /* Reset the pointer into the buffer.  */
00078          dirp->__ptr = dirp->__data;
00079 
00080          if (nentries == 0)
00081            {
00082              /* End of file.  */
00083              dp = NULL;
00084              break;
00085            }
00086 
00087          /* We trust the filesystem to return correct data and so we
00088             ignore NENTRIES.  */
00089        }
00090 
00091       dp = (struct dirent64 *) dirp->__ptr;
00092       dirp->__ptr += dp->d_reclen;
00093       ++dirp->__entry_ptr;
00094 
00095       /* Loop to ignore deleted files.  */
00096     } while (dp->d_fileno == 0);
00097 
00098   if (dp)
00099     {
00100       *entry = *dp;
00101       memcpy (entry->d_name, dp->d_name, dp->d_namlen + 1);
00102       *result = entry;
00103     }
00104   else
00105     *result = NULL;
00106 
00107   __libc_lock_unlock (dirp->__lock);
00108 
00109   return dp ? 0 : err ? errno : 0;
00110 }
00111 
00112 weak_alias (__readdir64_r, readdir64_r)