get_pathname.c revision d1154eb460efe588eaed3d439c1caaca149fa362
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 "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 34struct 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 44static 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 53 gp = (struct get_pathname_struct *) priv_data; 54 55 if (((dirent->name_len & 0xFF) == 2) && 56 !strncmp(dirent->name, "..", 2)) 57 gp->parent = dirent->inode; 58 if (dirent->inode == gp->search_ino) { 59 retval = ext2fs_get_mem((dirent->name_len & 0xFF) + 1, 60 &gp->name); 61 if (retval) { 62 gp->errcode = retval; 63 return DIRENT_ABORT; 64 } 65 strncpy(gp->name, dirent->name, (dirent->name_len & 0xFF)); 66 gp->name[dirent->name_len & 0xFF] = '\0'; 67 return DIRENT_ABORT; 68 } 69 return 0; 70} 71 72static errcode_t ext2fs_get_pathname_int(ext2_filsys fs, ext2_ino_t dir, 73 ext2_ino_t ino, int maxdepth, 74 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, 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, 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 &ret); 121 else 122 retval = ext2fs_get_mem(strlen(parent_name)+5, &ret); 123 if (retval) 124 goto cleanup; 125 126 ret[0] = 0; 127 if (parent_name[1]) 128 strcat(ret, parent_name); 129 strcat(ret, "/"); 130 if (gp.name) 131 strcat(ret, gp.name); 132 else 133 strcat(ret, "???"); 134 *name = ret; 135 ext2fs_free_mem(&parent_name); 136 retval = 0; 137 138cleanup: 139 if (gp.name) 140 ext2fs_free_mem(&gp.name); 141 return retval; 142} 143 144errcode_t ext2fs_get_pathname(ext2_filsys fs, ext2_ino_t dir, ext2_ino_t ino, 145 char **name) 146{ 147 char *buf; 148 errcode_t retval; 149 150 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS); 151 152 retval = ext2fs_get_mem(fs->blocksize, &buf); 153 if (retval) 154 return retval; 155 if (dir == ino) 156 ino = 0; 157 retval = ext2fs_get_pathname_int(fs, dir, ino, 32, buf, name); 158 ext2fs_free_mem(&buf); 159 return retval; 160 161} 162