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