Whamcloud - gitweb
9b5ce26193542c847e6e6c50ed85d97afdb8a76d
[tools/e2fsprogs.git] / lib / ext2fs / get_pathname.c
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 #include <stdlib.h>
27
28 #include <linux/ext2_fs.h>
29
30 #include "ext2fs.h"
31
32 struct get_pathname_struct {
33         ino_t           search_ino;
34         ino_t           parent;
35         char            *name;
36         errcode_t       errcode;
37 };
38
39 #ifdef __TURBOC__
40 #pragma argsused
41 #endif
42 static int get_pathname_proc(struct ext2_dir_entry *dirent,
43                              int        offset,
44                              int        blocksize,
45                              char       *buf,
46                              void       *private)
47 {
48         struct get_pathname_struct      *gp;
49         errcode_t                       retval;
50
51         gp = (struct get_pathname_struct *) private;
52
53         if ((dirent->name_len == 2) &&
54             !strncmp(dirent->name, "..", 2))
55                 gp->parent = dirent->inode;
56         if (dirent->inode == gp->search_ino) {
57                 retval = ext2fs_get_mem(dirent->name_len + 1,
58                                         (void **) &gp->name);
59                 if (!gp->name) {
60                         gp->errcode = EXT2_NO_MEMORY;
61                         return DIRENT_ABORT;
62                 }
63                 strncpy(gp->name, dirent->name, dirent->name_len);
64                 gp->name[dirent->name_len] = '\0';
65                 return DIRENT_ABORT;
66         }
67         return 0;
68 }
69
70 static errcode_t ext2fs_get_pathname_int(ext2_filsys fs, ino_t dir, ino_t ino,
71                                          int maxdepth, char *buf, char **name)
72 {
73         struct get_pathname_struct gp;
74         char    *parent_name, *ret;
75         errcode_t       retval;
76
77         if (dir == ino) {
78                 retval = ext2fs_get_mem(2, (void **)name);
79                 if (retval)
80                         return retval;
81                 strcpy(*name, (dir == EXT2_ROOT_INO) ? "/" : ".");
82                 return 0;
83         }
84
85         if (!dir || (maxdepth < 0)) {
86                 retval = ext2fs_get_mem(4, (void **)name);
87                 if (retval)
88                         return retval;
89                 strcpy(*name, "...");
90                 return 0;
91         }
92
93         gp.search_ino = ino;
94         gp.parent = 0;
95         gp.name = 0;
96         gp.errcode = 0;
97         
98         retval = ext2fs_dir_iterate(fs, dir, 0, buf, get_pathname_proc, &gp);
99         if (retval)
100                 goto cleanup;
101         if (gp.errcode) {
102                 retval = gp.errcode;
103                 goto cleanup;
104         }
105
106         retval = ext2fs_get_pathname_int(fs, gp.parent, dir, maxdepth-1,
107                                          buf, &parent_name);
108         if (retval)
109                 goto cleanup;
110         if (!ino) {
111                 *name = parent_name;
112                 return 0;
113         }
114         
115         if (gp.name) 
116                 retval = ext2fs_get_mem(strlen(parent_name)+strlen(gp.name)+2,
117                                         (void **) &ret);
118         else
119                 retval = ext2fs_get_mem(strlen(parent_name)+5,
120                                         (void **) &ret);
121         if (retval)
122                 goto cleanup;
123         
124         ret[0] = 0;
125         if (parent_name[1])
126                 strcat(ret, parent_name);
127         strcat(ret, "/");
128         if (gp.name)
129                 strcat(ret, gp.name);
130         else
131                 strcat(ret, "???");
132         *name = ret;
133         ext2fs_free_mem((void **) &parent_name);
134         retval = 0;
135         
136 cleanup:
137         if (gp.name)
138                 ext2fs_free_mem((void **) &gp.name);
139         return retval;
140 }
141
142 errcode_t ext2fs_get_pathname(ext2_filsys fs, ino_t dir, ino_t ino,
143                               char **name)
144 {
145         char    *buf;
146         errcode_t       retval;
147
148         EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
149
150         retval = ext2fs_get_mem(fs->blocksize, (void **) &buf);
151         if (retval)
152                 return retval;
153         if (dir == ino)
154                 ino = 0;
155         retval = ext2fs_get_pathname_int(fs, dir, ino, 32, buf, name);
156         ext2fs_free_mem((void **) &buf);
157         return retval;
158         
159 }