get_pathname.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * get_pathname.c --- do directry/inode -> name translation
  4. *
  5. * Copyright (C) 1993, 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. * ext2fs_get_pathname(fs, dir, ino, name)
  13. *
  14. * This function translates takes two inode numbers into a
  15. * string, placing the result in <name>. <dir> is the containing
  16. * directory inode, and <ino> is the inode number itself. If
  17. * <ino> is zero, then ext2fs_get_pathname will return pathname
  18. * of the the directory <dir>.
  19. *
  20. */
  21. #include <stdio.h>
  22. #include <string.h>
  23. #if HAVE_UNISTD_H
  24. #include <unistd.h>
  25. #endif
  26. #include "ext2_fs.h"
  27. #include "ext2fs.h"
  28. struct get_pathname_struct {
  29. ext2_ino_t search_ino;
  30. ext2_ino_t parent;
  31. char *name;
  32. errcode_t errcode;
  33. };
  34. #ifdef __TURBOC__
  35. # pragma argsused
  36. #endif
  37. static int get_pathname_proc(struct ext2_dir_entry *dirent,
  38. int offset EXT2FS_ATTR((unused)),
  39. int blocksize EXT2FS_ATTR((unused)),
  40. char *buf EXT2FS_ATTR((unused)),
  41. void *priv_data)
  42. {
  43. struct get_pathname_struct *gp;
  44. errcode_t retval;
  45. gp = (struct get_pathname_struct *) priv_data;
  46. if (((dirent->name_len & 0xFF) == 2) &&
  47. !strncmp(dirent->name, "..", 2))
  48. gp->parent = dirent->inode;
  49. if (dirent->inode == gp->search_ino) {
  50. retval = ext2fs_get_mem((dirent->name_len & 0xFF) + 1,
  51. &gp->name);
  52. if (retval) {
  53. gp->errcode = retval;
  54. return DIRENT_ABORT;
  55. }
  56. strncpy(gp->name, dirent->name, (dirent->name_len & 0xFF));
  57. gp->name[dirent->name_len & 0xFF] = '\0';
  58. return DIRENT_ABORT;
  59. }
  60. return 0;
  61. }
  62. static errcode_t ext2fs_get_pathname_int(ext2_filsys fs, ext2_ino_t dir,
  63. ext2_ino_t ino, int maxdepth,
  64. char *buf, char **name)
  65. {
  66. struct get_pathname_struct gp;
  67. char *parent_name, *ret;
  68. errcode_t retval;
  69. if (dir == ino) {
  70. retval = ext2fs_get_mem(2, name);
  71. if (retval)
  72. return retval;
  73. strcpy(*name, (dir == EXT2_ROOT_INO) ? "/" : ".");
  74. return 0;
  75. }
  76. if (!dir || (maxdepth < 0)) {
  77. retval = ext2fs_get_mem(4, name);
  78. if (retval)
  79. return retval;
  80. strcpy(*name, "...");
  81. return 0;
  82. }
  83. gp.search_ino = ino;
  84. gp.parent = 0;
  85. gp.name = 0;
  86. gp.errcode = 0;
  87. retval = ext2fs_dir_iterate(fs, dir, 0, buf, get_pathname_proc, &gp);
  88. if (retval)
  89. goto cleanup;
  90. if (gp.errcode) {
  91. retval = gp.errcode;
  92. goto cleanup;
  93. }
  94. retval = ext2fs_get_pathname_int(fs, gp.parent, dir, maxdepth-1,
  95. buf, &parent_name);
  96. if (retval)
  97. goto cleanup;
  98. if (!ino) {
  99. *name = parent_name;
  100. return 0;
  101. }
  102. if (gp.name)
  103. retval = ext2fs_get_mem(strlen(parent_name)+strlen(gp.name)+2,
  104. &ret);
  105. else
  106. retval = ext2fs_get_mem(strlen(parent_name)+5, &ret);
  107. if (retval)
  108. goto cleanup;
  109. ret[0] = 0;
  110. if (parent_name[1])
  111. strcat(ret, parent_name);
  112. strcat(ret, "/");
  113. if (gp.name)
  114. strcat(ret, gp.name);
  115. else
  116. strcat(ret, "???");
  117. *name = ret;
  118. ext2fs_free_mem(&parent_name);
  119. retval = 0;
  120. cleanup:
  121. ext2fs_free_mem(&gp.name);
  122. return retval;
  123. }
  124. errcode_t ext2fs_get_pathname(ext2_filsys fs, ext2_ino_t dir, ext2_ino_t ino,
  125. char **name)
  126. {
  127. char *buf;
  128. errcode_t retval;
  129. EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
  130. retval = ext2fs_get_mem(fs->blocksize, &buf);
  131. if (retval)
  132. return retval;
  133. if (dir == ino)
  134. ino = 0;
  135. retval = ext2fs_get_pathname_int(fs, dir, ino, 32, buf, name);
  136. ext2fs_free_mem(&buf);
  137. return retval;
  138. }