lookup.c 1.4 KB

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