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 Library
8 * General Public License, version 2.
14 * ext2fs_get_pathname(fs, dir, ino, name)
16 * This function translates takes two inode numbers into a
17 * string, placing the result in <name>. <dir> is the containing
18 * directory inode, and <ino> is the inode number itself. If
19 * <ino> is zero, then ext2fs_get_pathname will return pathname
20 * of the the directory <dir>.
33 struct get_pathname_struct {
34 ext2_ino_t search_ino;
43 static int get_pathname_proc(struct ext2_dir_entry *dirent,
44 int offset EXT2FS_ATTR((unused)),
45 int blocksize EXT2FS_ATTR((unused)),
46 char *buf EXT2FS_ATTR((unused)),
49 struct get_pathname_struct *gp;
52 gp = (struct get_pathname_struct *) priv_data;
54 if (((dirent->name_len & 0xFF) == 2) &&
55 !strncmp(dirent->name, "..", 2))
56 gp->parent = dirent->inode;
57 if (dirent->inode == gp->search_ino) {
58 retval = ext2fs_get_mem((dirent->name_len & 0xFF) + 1,
64 strncpy(gp->name, dirent->name, (dirent->name_len & 0xFF));
65 gp->name[dirent->name_len & 0xFF] = '\0';
71 static errcode_t ext2fs_get_pathname_int(ext2_filsys fs, ext2_ino_t dir,
72 ext2_ino_t ino, int maxdepth,
73 char *buf, char **name)
75 struct get_pathname_struct gp;
76 char *parent_name, *ret;
80 retval = ext2fs_get_mem(2, name);
83 strcpy(*name, (dir == EXT2_ROOT_INO) ? "/" : ".");
87 if (!dir || (maxdepth < 0)) {
88 retval = ext2fs_get_mem(4, name);
100 retval = ext2fs_dir_iterate(fs, dir, 0, buf, get_pathname_proc, &gp);
108 retval = ext2fs_get_pathname_int(fs, gp.parent, dir, maxdepth-1,
118 retval = ext2fs_get_mem(strlen(parent_name)+strlen(gp.name)+2,
121 retval = ext2fs_get_mem(strlen(parent_name)+5, &ret);
127 strcat(ret, parent_name);
130 strcat(ret, gp.name);
134 ext2fs_free_mem(&parent_name);
139 ext2fs_free_mem(&gp.name);
143 errcode_t ext2fs_get_pathname(ext2_filsys fs, ext2_ino_t dir, ext2_ino_t ino,
149 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
151 retval = ext2fs_get_mem(fs->blocksize, &buf);
156 retval = ext2fs_get_pathname_int(fs, dir, ino, 32, buf, name);
157 ext2fs_free_mem(&buf);