get_pathname.c revision 1f572d1f88628c1e0dc88cb8cfaf2bb480b81e4f
1/*
2 * get_pathname.c --- do directry/inode -> name translation
3 *
4 * Copyright (C) 1993, 1994, 1995 Theodore Ts'o.
5 *
6 * %Begin-Header%
7 * This file may be redistributed under the terms of the GNU Library
8 * General Public License, version 2.
9 * %End-Header%
10 */
11
12/*
13 *
14 * 	ext2fs_get_pathname(fs, dir, ino, name)
15 *
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>.
21 *
22 */
23
24#include "config.h"
25#include <stdio.h>
26#include <string.h>
27#if HAVE_UNISTD_H
28#include <unistd.h>
29#endif
30
31#include "ext2_fs.h"
32#include "ext2fs.h"
33
34struct get_pathname_struct {
35	ext2_ino_t	search_ino;
36	ext2_ino_t	parent;
37	char		*name;
38	errcode_t	errcode;
39};
40
41#ifdef __TURBOC__
42 #pragma argsused
43#endif
44static int get_pathname_proc(struct ext2_dir_entry *dirent,
45			     int	offset EXT2FS_ATTR((unused)),
46			     int	blocksize EXT2FS_ATTR((unused)),
47			     char	*buf EXT2FS_ATTR((unused)),
48			     void	*priv_data)
49{
50	struct get_pathname_struct	*gp;
51	errcode_t			retval;
52
53	gp = (struct get_pathname_struct *) priv_data;
54
55	if (((dirent->name_len & 0xFF) == 2) &&
56	    !strncmp(dirent->name, "..", 2))
57		gp->parent = dirent->inode;
58	if (dirent->inode == gp->search_ino) {
59		retval = ext2fs_get_mem((dirent->name_len & 0xFF) + 1,
60					&gp->name);
61		if (retval) {
62			gp->errcode = retval;
63			return DIRENT_ABORT;
64		}
65		strncpy(gp->name, dirent->name, (dirent->name_len & 0xFF));
66		gp->name[dirent->name_len & 0xFF] = '\0';
67		return DIRENT_ABORT;
68	}
69	return 0;
70}
71
72static errcode_t ext2fs_get_pathname_int(ext2_filsys fs, ext2_ino_t dir,
73					 ext2_ino_t ino, int maxdepth,
74					 char *buf, char **name)
75{
76	struct get_pathname_struct gp;
77	char	*parent_name = 0, *ret;
78	errcode_t	retval;
79
80	if (dir == ino) {
81		retval = ext2fs_get_mem(2, name);
82		if (retval)
83			return retval;
84		strcpy(*name, (dir == EXT2_ROOT_INO) ? "/" : ".");
85		return 0;
86	}
87
88	if (!dir || (maxdepth < 0)) {
89		retval = ext2fs_get_mem(4, name);
90		if (retval)
91			return retval;
92		strcpy(*name, "...");
93		return 0;
94	}
95
96	gp.search_ino = ino;
97	gp.parent = 0;
98	gp.name = 0;
99	gp.errcode = 0;
100
101	retval = ext2fs_dir_iterate(fs, dir, 0, buf, get_pathname_proc, &gp);
102	if (retval == EXT2_ET_NO_DIRECTORY) {
103		char buf[32];
104
105		if (ino)
106			snprintf(buf, sizeof(buf), "<%u>/<%u>", dir, ino);
107		else
108			snprintf(buf, sizeof(buf), "<%u>", dir);
109		retval = ext2fs_get_mem(strlen(buf)+1, name);
110		if (retval)
111			goto cleanup;
112		strcpy(*name, buf);
113		return 0;
114	} else if (retval)
115		goto cleanup;
116	if (gp.errcode) {
117		retval = gp.errcode;
118		goto cleanup;
119	}
120
121	retval = ext2fs_get_pathname_int(fs, gp.parent, dir, maxdepth-1,
122					 buf, &parent_name);
123	if (retval)
124		goto cleanup;
125	if (!ino) {
126		*name = parent_name;
127		return 0;
128	}
129
130	if (gp.name)
131		retval = ext2fs_get_mem(strlen(parent_name)+strlen(gp.name)+2,
132					&ret);
133	else
134		retval = ext2fs_get_mem(strlen(parent_name)+5, &ret);
135	if (retval)
136		goto cleanup;
137
138	ret[0] = 0;
139	if (parent_name[1])
140		strcat(ret, parent_name);
141	strcat(ret, "/");
142	if (gp.name)
143		strcat(ret, gp.name);
144	else
145		strcat(ret, "???");
146	*name = ret;
147	retval = 0;
148
149cleanup:
150	ext2fs_free_mem(&parent_name);
151	ext2fs_free_mem(&gp.name);
152	return retval;
153}
154
155errcode_t ext2fs_get_pathname(ext2_filsys fs, ext2_ino_t dir, ext2_ino_t ino,
156			      char **name)
157{
158	char	*buf;
159	errcode_t	retval;
160
161	EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
162
163	retval = ext2fs_get_mem(fs->blocksize, &buf);
164	if (retval)
165		return retval;
166	if (dir == ino)
167		ino = 0;
168	retval = ext2fs_get_pathname_int(fs, dir, ino, 32, buf, name);
169	ext2fs_free_mem(&buf);
170	return retval;
171
172}
173