Whamcloud - gitweb
libext2fs: require cluster size == block_size when opening a !bigalloc fs
[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 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 <stdio.h>
25 #include <string.h>
26 #if HAVE_UNISTD_H
27 #include <unistd.h>
28 #endif
29
30 #include "ext2_fs.h"
31 #include "ext2fs.h"
32
33 struct get_pathname_struct {
34         ext2_ino_t      search_ino;
35         ext2_ino_t      parent;
36         char            *name;
37         errcode_t       errcode;
38 };
39
40 #ifdef __TURBOC__
41  #pragma argsused
42 #endif
43 static int get_pathname_proc(struct ext2_dir_entry *dirent,
44                              int        offset EXT2FS_ATTR((unused)),
45                              int        blocksize EXT2FS_ATTR((unused)),
46                              char       *buf EXT2FS_ATTR((unused)),
47                              void       *priv_data)
48 {
49         struct get_pathname_struct      *gp;
50         errcode_t                       retval;
51
52         gp = (struct get_pathname_struct *) priv_data;
53
54         if (((dirent->name_len & 0xFF) == 2) &&
55             !strncmp(dirent->name, "..", 2))
56                 gp->parent = dirent->inode;
57         if (dirent->inode == gp->search_ino) {
58                 retval = ext2fs_get_mem((dirent->name_len & 0xFF) + 1,
59                                         &gp->name);
60                 if (retval) {
61                         gp->errcode = retval;
62                         return DIRENT_ABORT;
63                 }
64                 strncpy(gp->name, dirent->name, (dirent->name_len & 0xFF));
65                 gp->name[dirent->name_len & 0xFF] = '\0';
66                 return DIRENT_ABORT;
67         }
68         return 0;
69 }
70
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, *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)
102                 goto cleanup;
103         if (gp.errcode) {
104                 retval = gp.errcode;
105                 goto cleanup;
106         }
107
108         retval = ext2fs_get_pathname_int(fs, gp.parent, dir, maxdepth-1,
109                                          buf, &parent_name);
110         if (retval)
111                 goto cleanup;
112         if (!ino) {
113                 *name = parent_name;
114                 return 0;
115         }
116
117         if (gp.name)
118                 retval = ext2fs_get_mem(strlen(parent_name)+strlen(gp.name)+2,
119                                         &ret);
120         else
121                 retval = ext2fs_get_mem(strlen(parent_name)+5, &ret);
122         if (retval)
123                 goto cleanup;
124
125         ret[0] = 0;
126         if (parent_name[1])
127                 strcat(ret, parent_name);
128         strcat(ret, "/");
129         if (gp.name)
130                 strcat(ret, gp.name);
131         else
132                 strcat(ret, "???");
133         *name = ret;
134         ext2fs_free_mem(&parent_name);
135         retval = 0;
136
137 cleanup:
138         if (gp.name)
139                 ext2fs_free_mem(&gp.name);
140         return retval;
141 }
142
143 errcode_t ext2fs_get_pathname(ext2_filsys fs, ext2_ino_t dir, ext2_ino_t ino,
144                               char **name)
145 {
146         char    *buf;
147         errcode_t       retval;
148
149         EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
150
151         retval = ext2fs_get_mem(fs->blocksize, &buf);
152         if (retval)
153                 return retval;
154         if (dir == ino)
155                 ino = 0;
156         retval = ext2fs_get_pathname_int(fs, dir, ino, 32, buf, name);
157         ext2fs_free_mem(&buf);
158         return retval;
159
160 }