get_pathname.c revision b5abe6fac9c9e7caf4710501d1657d30e4857ef6
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 Public
8 * License.
9 * %End-Header%
10 *
11 * 	ext2fs_get_pathname(fs, dir, ino, name)
12 *
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>.
18 *
19 */
20
21#include <stdio.h>
22#include <string.h>
23#if HAVE_UNISTD_H
24#include <unistd.h>
25#endif
26
27#if EXT2_FLAT_INCLUDES
28#include "ext2_fs.h"
29#else
30#include <linux/ext2_fs.h>
31#endif
32
33#include "ext2fs.h"
34
35struct get_pathname_struct {
36	ino_t		search_ino;
37	ino_t		parent;
38	char		*name;
39	errcode_t	errcode;
40};
41
42#ifdef __TURBOC__
43#pragma argsused
44#endif
45static int get_pathname_proc(struct ext2_dir_entry *dirent,
46			     int	offset,
47			     int	blocksize,
48			     char	*buf,
49			     void	*priv_data)
50{
51	struct get_pathname_struct	*gp;
52	errcode_t			retval;
53
54	gp = (struct get_pathname_struct *) priv_data;
55
56	if ((dirent->name_len == 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 + 1,
61					(void **) &gp->name);
62		if (!gp->name) {
63			gp->errcode = EXT2_ET_NO_MEMORY;
64			return DIRENT_ABORT;
65		}
66		strncpy(gp->name, dirent->name, dirent->name_len);
67		gp->name[dirent->name_len] = '\0';
68		return DIRENT_ABORT;
69	}
70	return 0;
71}
72
73static errcode_t ext2fs_get_pathname_int(ext2_filsys fs, ino_t dir, ino_t ino,
74					 int maxdepth, char *buf, char **name)
75{
76	struct get_pathname_struct gp;
77	char	*parent_name, *ret;
78	errcode_t	retval;
79
80	if (dir == ino) {
81		retval = ext2fs_get_mem(2, (void **)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, (void **)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)
103		goto cleanup;
104	if (gp.errcode) {
105		retval = gp.errcode;
106		goto cleanup;
107	}
108
109	retval = ext2fs_get_pathname_int(fs, gp.parent, dir, maxdepth-1,
110					 buf, &parent_name);
111	if (retval)
112		goto cleanup;
113	if (!ino) {
114		*name = parent_name;
115		return 0;
116	}
117
118	if (gp.name)
119		retval = ext2fs_get_mem(strlen(parent_name)+strlen(gp.name)+2,
120					(void **) &ret);
121	else
122		retval = ext2fs_get_mem(strlen(parent_name)+5,
123					(void **) &ret);
124	if (retval)
125		goto cleanup;
126
127	ret[0] = 0;
128	if (parent_name[1])
129		strcat(ret, parent_name);
130	strcat(ret, "/");
131	if (gp.name)
132		strcat(ret, gp.name);
133	else
134		strcat(ret, "???");
135	*name = ret;
136	ext2fs_free_mem((void **) &parent_name);
137	retval = 0;
138
139cleanup:
140	if (gp.name)
141		ext2fs_free_mem((void **) &gp.name);
142	return retval;
143}
144
145errcode_t ext2fs_get_pathname(ext2_filsys fs, ino_t dir, ino_t ino,
146			      char **name)
147{
148	char	*buf;
149	errcode_t	retval;
150
151	EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
152
153	retval = ext2fs_get_mem(fs->blocksize, (void **) &buf);
154	if (retval)
155		return retval;
156	if (dir == ino)
157		ino = 0;
158	retval = ext2fs_get_pathname_int(fs, dir, ino, 32, buf, name);
159	ext2fs_free_mem((void **) &buf);
160	return retval;
161
162}
163