lookup.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * lookup.c --- ext2fs directory lookup operations
  4. *
  5. * Copyright (C) 1993, 1994, 1994, 1995 Theodore Ts'o.
  6. *
  7. * %Begin-Header%
  8. * This file may be redistributed under the terms of the GNU Public
  9. * License.
  10. * %End-Header%
  11. */
  12. #include <stdio.h>
  13. #include <string.h>
  14. #if HAVE_UNISTD_H
  15. #include <unistd.h>
  16. #endif
  17. #include "ext2_fs.h"
  18. #include "ext2fs.h"
  19. struct lookup_struct {
  20. const char *name;
  21. int len;
  22. ext2_ino_t *inode;
  23. int found;
  24. };
  25. #ifdef __TURBOC__
  26. # pragma argsused
  27. #endif
  28. static int lookup_proc(struct ext2_dir_entry *dirent,
  29. int offset EXT2FS_ATTR((unused)),
  30. int blocksize EXT2FS_ATTR((unused)),
  31. char *buf EXT2FS_ATTR((unused)),
  32. void *priv_data)
  33. {
  34. struct lookup_struct *ls = (struct lookup_struct *) priv_data;
  35. if (ls->len != (dirent->name_len & 0xFF))
  36. return 0;
  37. if (strncmp(ls->name, dirent->name, (dirent->name_len & 0xFF)))
  38. return 0;
  39. *ls->inode = dirent->inode;
  40. ls->found++;
  41. return DIRENT_ABORT;
  42. }
  43. errcode_t ext2fs_lookup(ext2_filsys fs, ext2_ino_t dir, const char *name,
  44. int namelen, char *buf, ext2_ino_t *inode)
  45. {
  46. errcode_t retval;
  47. struct lookup_struct ls;
  48. EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
  49. ls.name = name;
  50. ls.len = namelen;
  51. ls.inode = inode;
  52. ls.found = 0;
  53. retval = ext2fs_dir_iterate(fs, dir, 0, buf, lookup_proc, &ls);
  54. if (retval)
  55. return retval;
  56. return (ls.found) ? 0 : EXT2_ET_FILE_NOT_FOUND;
  57. }