Blame sysdeps/mach/hurd/readdir64_r.c

Packit 6c4009
/* Copyright (C) 2001-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <dirent.h>
Packit 6c4009
#include <stddef.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <hurd.h>
Packit 6c4009
#include <hurd/fs.h>
Packit 6c4009
#include <hurd/fd.h>
Packit 6c4009
#include "dirstream.h"
Packit 6c4009
Packit 6c4009
/* Read a directory entry from DIRP.  */
Packit 6c4009
int
Packit 6c4009
__readdir64_r (DIR *dirp, struct dirent64 *entry, struct dirent64 **result)
Packit 6c4009
{
Packit 6c4009
  struct dirent64 *dp;
Packit 6c4009
  error_t err = 0;
Packit 6c4009
Packit 6c4009
  if (dirp == NULL)
Packit 6c4009
    {
Packit 6c4009
      errno = EINVAL;
Packit 6c4009
      return errno;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  __libc_lock_lock (dirp->__lock);
Packit 6c4009
Packit 6c4009
  do
Packit 6c4009
    {
Packit 6c4009
      if (dirp->__ptr - dirp->__data >= dirp->__size)
Packit 6c4009
	{
Packit 6c4009
	  /* We've emptied out our buffer.  Refill it.  */
Packit 6c4009
Packit 6c4009
	  char *data = dirp->__data;
Packit 6c4009
	  int nentries;
Packit 6c4009
Packit 6c4009
	  if (err = HURD_FD_PORT_USE (dirp->__fd,
Packit 6c4009
				      __dir_readdir (port,
Packit 6c4009
						     &data, &dirp->__size,
Packit 6c4009
						     dirp->__entry_ptr,
Packit 6c4009
						     -1, 0, &nentries)))
Packit 6c4009
	    {
Packit 6c4009
	      __hurd_fail (err);
Packit 6c4009
	      dp = NULL;
Packit 6c4009
	      break;
Packit 6c4009
	    }
Packit 6c4009
Packit 6c4009
	  /* DATA now corresponds to entry index DIRP->__entry_ptr.  */
Packit 6c4009
	  dirp->__entry_data = dirp->__entry_ptr;
Packit 6c4009
Packit 6c4009
	  if (data != dirp->__data)
Packit 6c4009
	    {
Packit 6c4009
	      /* The data was passed out of line, so our old buffer is no
Packit 6c4009
		 longer useful.  Deallocate the old buffer and reset our
Packit 6c4009
		 information for the new buffer.  */
Packit 6c4009
	      __vm_deallocate (__mach_task_self (),
Packit 6c4009
			       (vm_address_t) dirp->__data,
Packit 6c4009
			       dirp->__allocation);
Packit 6c4009
	      dirp->__data = data;
Packit 6c4009
	      dirp->__allocation = round_page (dirp->__size);
Packit 6c4009
	    }
Packit 6c4009
Packit 6c4009
	  /* Reset the pointer into the buffer.  */
Packit 6c4009
	  dirp->__ptr = dirp->__data;
Packit 6c4009
Packit 6c4009
	  if (nentries == 0)
Packit 6c4009
	    {
Packit 6c4009
	      /* End of file.  */
Packit 6c4009
	      dp = NULL;
Packit 6c4009
	      break;
Packit 6c4009
	    }
Packit 6c4009
Packit 6c4009
	  /* We trust the filesystem to return correct data and so we
Packit 6c4009
	     ignore NENTRIES.  */
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      dp = (struct dirent64 *) dirp->__ptr;
Packit 6c4009
      dirp->__ptr += dp->d_reclen;
Packit 6c4009
      ++dirp->__entry_ptr;
Packit 6c4009
Packit 6c4009
      /* Loop to ignore deleted files.  */
Packit 6c4009
    } while (dp->d_fileno == 0);
Packit 6c4009
Packit 6c4009
  if (dp)
Packit 6c4009
    {
Packit 6c4009
      *entry = *dp;
Packit 6c4009
      memcpy (entry->d_name, dp->d_name, dp->d_namlen + 1);
Packit 6c4009
      *result = entry;
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    *result = NULL;
Packit 6c4009
Packit 6c4009
  __libc_lock_unlock (dirp->__lock);
Packit 6c4009
Packit 6c4009
  return dp ? 0 : err ? errno : 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
weak_alias (__readdir64_r, readdir64_r)