2 * get_pathname.c --- do directry/inode -> name translation
4 * Copyright (C) 1993, 1994, 1995 Theodore Ts'o.
7 * This file may be redistributed under the terms of the GNU Public
11 * ext2fs_get_pathname(fs, dir, ino, name)
13 * This function translates takes two inode numbers into a
14 * string, placing the result in <name>. <dir> is the containing
15 * directory inode, and <ino> is the inode number itself. If
16 * <ino> is zero, then ext2fs_get_pathname will return pathname
17 * of the the directory <dir>.
27 #if EXT2_FLAT_INCLUDES
30 #include <linux/ext2_fs.h>
35 struct get_pathname_struct {
45 static int get_pathname_proc(struct ext2_dir_entry *dirent,
51 struct get_pathname_struct *gp;
54 gp = (struct get_pathname_struct *) priv_data;
56 if (((dirent->name_len & 0xFF) == 2) &&
57 !strncmp(dirent->name, "..", 2))
58 gp->parent = dirent->inode;
59 if (dirent->inode == gp->search_ino) {
60 retval = ext2fs_get_mem((dirent->name_len & 0xFF) + 1,
66 strncpy(gp->name, dirent->name, (dirent->name_len & 0xFF));
67 gp->name[dirent->name_len & 0xFF] = '\0';
73 static errcode_t ext2fs_get_pathname_int(ext2_filsys fs, ino_t dir, ino_t ino,
74 int maxdepth, char *buf, char **name)
76 struct get_pathname_struct gp;
77 char *parent_name, *ret;
81 retval = ext2fs_get_mem(2, (void **)name);
84 strcpy(*name, (dir == EXT2_ROOT_INO) ? "/" : ".");
88 if (!dir || (maxdepth < 0)) {
89 retval = ext2fs_get_mem(4, (void **)name);
101 retval = ext2fs_dir_iterate(fs, dir, 0, buf, get_pathname_proc, &gp);
109 retval = ext2fs_get_pathname_int(fs, gp.parent, dir, maxdepth-1,
119 retval = ext2fs_get_mem(strlen(parent_name)+strlen(gp.name)+2,
122 retval = ext2fs_get_mem(strlen(parent_name)+5,
129 strcat(ret, parent_name);
132 strcat(ret, gp.name);
136 ext2fs_free_mem((void **) &parent_name);
141 ext2fs_free_mem((void **) &gp.name);
145 errcode_t ext2fs_get_pathname(ext2_filsys fs, ino_t dir, ino_t ino,
151 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
153 retval = ext2fs_get_mem(fs->blocksize, (void **) &buf);
158 retval = ext2fs_get_pathname_int(fs, dir, ino, 32, buf, name);
159 ext2fs_free_mem((void **) &buf);