1 /* vi: set sw=4 ts=4: */
3 * get_pathname.c --- do directry/inode -> name translation
5 * Copyright (C) 1993, 1994, 1995 Theodore Ts'o.
8 * This file may be redistributed under the terms of the GNU Public
12 * ext2fs_get_pathname(fs, dir, ino, name)
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 directory <dir>.
31 struct get_pathname_struct
{
32 ext2_ino_t search_ino
;
41 static int get_pathname_proc(struct ext2_dir_entry
*dirent
,
42 int offset
EXT2FS_ATTR((unused
)),
43 int blocksize
EXT2FS_ATTR((unused
)),
44 char *buf
EXT2FS_ATTR((unused
)),
47 struct get_pathname_struct
*gp
;
50 gp
= (struct get_pathname_struct
*) priv_data
;
52 if (((dirent
->name_len
& 0xFF) == 2) &&
53 !strncmp(dirent
->name
, "..", 2))
54 gp
->parent
= dirent
->inode
;
55 if (dirent
->inode
== gp
->search_ino
) {
56 retval
= ext2fs_get_mem((dirent
->name_len
& 0xFF) + 1,
62 strncpy(gp
->name
, dirent
->name
, (dirent
->name_len
& 0xFF));
63 gp
->name
[dirent
->name_len
& 0xFF] = '\0';
69 static errcode_t
ext2fs_get_pathname_int(ext2_filsys fs
, ext2_ino_t dir
,
70 ext2_ino_t ino
, int maxdepth
,
71 char *buf
, char **name
)
73 struct get_pathname_struct gp
;
74 char *parent_name
, *ret
;
78 retval
= ext2fs_get_mem(2, name
);
81 strcpy(*name
, (dir
== EXT2_ROOT_INO
) ? "/" : ".");
85 if (!dir
|| (maxdepth
< 0)) {
86 retval
= ext2fs_get_mem(4, name
);
98 retval
= ext2fs_dir_iterate(fs
, dir
, 0, buf
, get_pathname_proc
, &gp
);
106 retval
= ext2fs_get_pathname_int(fs
, gp
.parent
, dir
, maxdepth
-1,
116 retval
= ext2fs_get_mem(strlen(parent_name
)+strlen(gp
.name
)+2,
119 retval
= ext2fs_get_mem(strlen(parent_name
)+5, &ret
);
125 strcat(ret
, parent_name
);
128 strcat(ret
, gp
.name
);
132 ext2fs_free_mem(&parent_name
);
136 ext2fs_free_mem(&gp
.name
);
140 errcode_t
ext2fs_get_pathname(ext2_filsys fs
, ext2_ino_t dir
, ext2_ino_t ino
,
146 EXT2_CHECK_MAGIC(fs
, EXT2_ET_MAGIC_EXT2FS_FILSYS
);
148 retval
= ext2fs_get_mem(fs
->blocksize
, &buf
);
153 retval
= ext2fs_get_pathname_int(fs
, dir
, ino
, 32, buf
, name
);
154 ext2fs_free_mem(&buf
);