home *** CD-ROM | disk | FTP | other *** search
/ PC-Online 1998 February / PCOnline_02_1998.iso / filesbbs / win95 / ext2tool.exe / EXT2FS / GET_PATH.C < prev    next >
C/C++ Source or Header  |  1995-05-10  |  3KB  |  137 lines

  1. /*
  2.  * get_pathname.c --- do directry/inode -> name translation
  3.  * 
  4.  * Copyright (C) 1993 Theodore Ts'o.  This file may be redistributed
  5.  * under the terms of the GNU Public License.
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <unistd.h>
  11. #include <stdlib.h>
  12.  
  13. #include <errno.h>
  14. #include <linux/ext2_fs.h>
  15.  
  16. #include "ext2fs.h"
  17.  
  18. struct get_pathname_struct {
  19.     int        search_ino;
  20.     int        parent;
  21.     char        *name;
  22.     errcode_t    errcode;
  23. };
  24.  
  25. static int get_pathname_proc(struct ext2_dir_entry *dirent,
  26.                  int    offset,
  27.                  int    blocksize,
  28.                  char    *buf,
  29.                  void    *private)
  30. {
  31.     struct get_pathname_struct    *gp;
  32.  
  33.     gp = (struct get_pathname_struct *) private;
  34.  
  35.     if ((dirent->name_len == 2) &&
  36.         !strncmp(dirent->name, "..", 2))
  37.         gp->parent = dirent->inode;
  38.     if (dirent->inode == gp->search_ino) {
  39.         gp->name = malloc(dirent->name_len + 1);
  40.         if (!gp->name) {
  41.             gp->errcode = ENOMEM;
  42.             return DIRENT_ABORT;
  43.         }
  44.         strncpy(gp->name, dirent->name, dirent->name_len);
  45.         gp->name[dirent->name_len] = '\0';
  46.         return DIRENT_ABORT;
  47.     }
  48.     return 0;
  49. }
  50.  
  51. static errcode_t ext2fs_get_pathname_int(ext2_filsys fs, ino_t dir, ino_t ino,
  52.                      int maxdepth, char *buf, char **name)
  53. {
  54.     struct get_pathname_struct gp;
  55.     char    *parent_name, *ret;
  56.     errcode_t    retval;
  57.  
  58.     if (dir == ino) {
  59.         *name = malloc(2);
  60.         if (!*name)
  61.             return ENOMEM;
  62.         strcpy(*name, (dir == EXT2_ROOT_INO) ? "/" : ".");
  63.         return 0;
  64.     }
  65.  
  66.     if (!dir || (maxdepth < 0)) {
  67.         *name = malloc(4);
  68.         if (!*name)
  69.             return ENOMEM;
  70.         strcpy(*name, "...");
  71.         return 0;
  72.     }
  73.  
  74.     gp.search_ino = ino;
  75.     gp.parent = 0;
  76.     gp.name = 0;
  77.     gp.errcode = 0;
  78.     
  79.     retval = ext2fs_dir_iterate(fs, dir, 0, buf, get_pathname_proc, &gp);
  80.     if (retval)
  81.         goto cleanup;
  82.     if (gp.errcode) {
  83.         retval = gp.errcode;
  84.         goto cleanup;
  85.     }
  86.  
  87.     retval = ext2fs_get_pathname_int(fs, gp.parent, dir, maxdepth-1,
  88.                      buf, &parent_name);
  89.     if (retval)
  90.         goto cleanup;
  91.     if (!ino) {
  92.         *name = parent_name;
  93.         return 0;
  94.     }
  95.     
  96.     ret = malloc(strlen(parent_name)+strlen(gp.name)+2);
  97.     if (!ret) {
  98.         retval = ENOMEM;
  99.         goto cleanup;
  100.     }
  101.     ret[0] = 0;
  102.     if (parent_name[1])
  103.         strcat(ret, parent_name);
  104.     strcat(ret, "/");
  105.     if (gp.name)
  106.         strcat(ret, gp.name);
  107.     else
  108.         strcat(ret, "???");
  109.     *name = ret;
  110.     free(parent_name);
  111.     retval = 0;
  112.     
  113. cleanup:
  114.     if (gp.name)
  115.         free(gp.name);
  116.     return retval;
  117. }
  118.  
  119. errcode_t ext2fs_get_pathname(ext2_filsys fs, ino_t dir, ino_t ino,
  120.                   char **name)
  121. {
  122.     char    *buf;
  123.     errcode_t    retval;
  124.  
  125.     EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
  126.  
  127.     buf = malloc(fs->blocksize);
  128.     if (!buf)
  129.         return ENOMEM;
  130.     if (dir == ino)
  131.         ino = 0;
  132.     retval = ext2fs_get_pathname_int(fs, dir, ino, 32, buf, name);
  133.     free(buf);
  134.     return retval;
  135.     
  136. }
  137.