Back to index

glibc  2.9
readdir64.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 <hurd.h>
00022 #include <hurd/fs.h>
00023 #include <hurd/fd.h>
00024 #include "dirstream.h"
00025 
00026 /* Read a directory entry from DIRP.  */
00027 struct dirent64 *
00028 __readdir64 (DIR *dirp)
00029 {
00030   struct dirent64 *dp;
00031 
00032   if (dirp == NULL)
00033     {
00034       errno = EINVAL;
00035       return NULL;
00036     }
00037 
00038   __libc_lock_lock (dirp->__lock);
00039 
00040   do
00041     {
00042       if (dirp->__ptr - dirp->__data >= dirp->__size)
00043        {
00044          /* We've emptied out our buffer.  Refill it.  */
00045 
00046          char *data = dirp->__data;
00047          int nentries;
00048          error_t err;
00049 
00050          if (err = HURD_FD_PORT_USE (dirp->__fd,
00051                                   __dir_readdir (port,
00052                                                &data, &dirp->__size,
00053                                                dirp->__entry_ptr,
00054                                                -1, 0, &nentries)))
00055            {
00056              __hurd_fail (err);
00057              dp = NULL;
00058              break;
00059            }
00060 
00061          /* DATA now corresponds to entry index DIRP->__entry_ptr.  */
00062          dirp->__entry_data = dirp->__entry_ptr;
00063 
00064          if (data != dirp->__data)
00065            {
00066              /* The data was passed out of line, so our old buffer is no
00067                longer useful.  Deallocate the old buffer and reset our
00068                information for the new buffer.  */
00069              __vm_deallocate (__mach_task_self (),
00070                             (vm_address_t) dirp->__data,
00071                             dirp->__allocation);
00072              dirp->__data = data;
00073              dirp->__allocation = round_page (dirp->__size);
00074            }
00075 
00076          /* Reset the pointer into the buffer.  */
00077          dirp->__ptr = dirp->__data;
00078 
00079          if (nentries == 0)
00080            {
00081              /* End of file.  */
00082              dp = NULL;
00083              break;
00084            }
00085 
00086          /* We trust the filesystem to return correct data and so we
00087             ignore NENTRIES.  */
00088        }
00089 
00090       dp = (struct dirent64 *) dirp->__ptr;
00091       dirp->__ptr += dp->d_reclen;
00092       ++dirp->__entry_ptr;
00093 
00094       /* Loop to ignore deleted files.  */
00095     } while (dp->d_fileno == 0);
00096 
00097   __libc_lock_unlock (dirp->__lock);
00098 
00099   return dp;
00100 }
00101 
00102 weak_alias (__readdir64, readdir64)