1 /*
2 * get_pathname.c --- do directory/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
34 struct 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
get_pathname_proc(struct ext2_dir_entry * dirent,int offset EXT2FS_ATTR ((unused)),int blocksize EXT2FS_ATTR ((unused)),char * buf EXT2FS_ATTR ((unused)),void * priv_data)44 static 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 int name_len = ext2fs_dirent_name_len(dirent);
53
54 gp = (struct get_pathname_struct *) priv_data;
55
56 if ((name_len == 2) && !strncmp(dirent->name, "..", 2))
57 gp->parent = dirent->inode;
58 if (dirent->inode == gp->search_ino) {
59 retval = ext2fs_get_mem(name_len + 1, &gp->name);
60 if (retval) {
61 gp->errcode = retval;
62 return DIRENT_ABORT;
63 }
64 strncpy(gp->name, dirent->name, name_len);
65 gp->name[name_len] = '\0';
66 return DIRENT_ABORT;
67 }
68 return 0;
69 }
70
ext2fs_get_pathname_int(ext2_filsys fs,ext2_ino_t dir,ext2_ino_t ino,int maxdepth,char * buf,char ** name)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)
74 {
75 struct get_pathname_struct gp;
76 char *parent_name = 0, *ret;
77 errcode_t retval;
78
79 if (dir == ino) {
80 retval = ext2fs_get_mem(2, name);
81 if (retval)
82 return retval;
83 strcpy(*name, (dir == EXT2_ROOT_INO) ? "/" : ".");
84 return 0;
85 }
86
87 if (!dir || (maxdepth < 0)) {
88 retval = ext2fs_get_mem(4, name);
89 if (retval)
90 return retval;
91 strcpy(*name, "...");
92 return 0;
93 }
94
95 gp.search_ino = ino;
96 gp.parent = 0;
97 gp.name = 0;
98 gp.errcode = 0;
99
100 retval = ext2fs_dir_iterate(fs, dir, 0, buf, get_pathname_proc, &gp);
101 if (retval == EXT2_ET_NO_DIRECTORY) {
102 char tmp[32];
103
104 if (ino)
105 snprintf(tmp, sizeof(tmp), "<%u>/<%u>", dir, ino);
106 else
107 snprintf(tmp, sizeof(tmp), "<%u>", dir);
108 retval = ext2fs_get_mem(strlen(tmp)+1, name);
109 if (retval)
110 goto cleanup;
111 strcpy(*name, tmp);
112 return 0;
113 } else if (retval)
114 goto cleanup;
115 if (gp.errcode) {
116 retval = gp.errcode;
117 goto cleanup;
118 }
119
120 retval = ext2fs_get_pathname_int(fs, gp.parent, dir, maxdepth-1,
121 buf, &parent_name);
122 if (retval)
123 goto cleanup;
124 if (!ino) {
125 *name = parent_name;
126 return 0;
127 }
128
129 if (gp.name)
130 retval = ext2fs_get_mem(strlen(parent_name)+strlen(gp.name)+2,
131 &ret);
132 else
133 retval = ext2fs_get_mem(strlen(parent_name)+5, &ret);
134 if (retval)
135 goto cleanup;
136
137 ret[0] = 0;
138 if (parent_name[1])
139 strcat(ret, parent_name);
140 strcat(ret, "/");
141 if (gp.name)
142 strcat(ret, gp.name);
143 else
144 strcat(ret, "???");
145 *name = ret;
146 retval = 0;
147
148 cleanup:
149 ext2fs_free_mem(&parent_name);
150 ext2fs_free_mem(&gp.name);
151 return retval;
152 }
153
ext2fs_get_pathname(ext2_filsys fs,ext2_ino_t dir,ext2_ino_t ino,char ** name)154 errcode_t ext2fs_get_pathname(ext2_filsys fs, ext2_ino_t dir, ext2_ino_t ino,
155 char **name)
156 {
157 char *buf;
158 errcode_t retval;
159
160 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
161
162 retval = ext2fs_get_mem(fs->blocksize, &buf);
163 if (retval)
164 return retval;
165 if (dir == ino)
166 ino = 0;
167 retval = ext2fs_get_pathname_int(fs, dir, ino, 32, buf, name);
168 ext2fs_free_mem(&buf);
169 return retval;
170
171 }
172