int ext2fs_find_file (const char *path, struct ext2fs_node *rootnode, struct ext2fs_node **foundnode, int expecttype) { int status; int foundtype = FILETYPE_DIRECTORY; symlinknest = 0; if (!path) { return (0); } status = ext2fs_find_file1 (path, rootnode, foundnode, &foundtype); if (status == 0) { return (0); } /* Check if the node that was found was of the expected type. */ if ((expecttype == FILETYPE_REG) && (foundtype != expecttype)) { return (0); } else if ((expecttype == FILETYPE_DIRECTORY) && (foundtype != expecttype)) { return (0); } return (1); }
quik_err_t ext2fs_find_file(const char *path, ext2fs_node_t rootnode, ext2fs_node_t *foundnode, int expecttype) { quik_err_t err; int foundtype = FILETYPE_DIRECTORY; symlinknest = 0; if (!path) { return ERR_FS_NOT_FOUND; } err = ext2fs_find_file1(path, rootnode, foundnode, &foundtype); if (err != ERR_NONE) { return err; } /* Check if the node that was found was of the expected type. */ if ((expecttype == FILETYPE_REG) && (foundtype != expecttype)) { return ERR_FS_NOT_REG; } else if ((expecttype == FILETYPE_DIRECTORY) && (foundtype != expecttype)) { return ERR_FS_NOT_DIR; } return ERR_NONE; }
int ext2fs_find_file1 (const char *currpath, struct ext2fs_node *currroot, struct ext2fs_node **currfound, int *foundtype) { char fpath[strlen (currpath) + 1]; char *name = fpath; char *next; int status; int type = FILETYPE_DIRECTORY; struct ext2fs_node *currnode = currroot; struct ext2fs_node *oldnode = currroot; strncpy (fpath, currpath, strlen (currpath) + 1); /* Remove all leading slashes. */ while (*name == '/') { name++; } if (!*name) { *currfound = currnode; return (1); } for (;;) { int found; /* Extract the actual part from the pathname. */ next = strchr (name, '/'); if (next) { /* Remove all leading slashes. */ while (*next == '/') { *(next++) = '\0'; } } /* At this point it is expected that the current node is a directory, check if this is true. */ if (type != FILETYPE_DIRECTORY) { ext2fs_free_node (currnode, currroot); return (0); } oldnode = currnode; /* Iterate over the directory. */ found = ext2fs_iterate_dir (currnode, name, &currnode, &type); if (found == 0) { return (0); } if (found == -1) { break; } /* Read in the symlink and follow it. */ if (type == FILETYPE_SYMLINK) { char *symlink; /* Test if the symlink does not loop. */ if (++symlinknest == 8) { ext2fs_free_node (currnode, currroot); ext2fs_free_node (oldnode, currroot); return (0); } symlink = ext2fs_read_symlink (currnode); ext2fs_free_node (currnode, currroot); if (!symlink) { ext2fs_free_node (oldnode, currroot); return (0); } #ifdef DEBUG printf ("Got symlink >%s<\n", symlink); #endif /* of DEBUG */ /* The symlink is an absolute path, go back to the root inode. */ if (symlink[0] == '/') { ext2fs_free_node (oldnode, currroot); oldnode = &ext2fs_root->diropen; } /* Lookup the node the symlink points to. */ status = ext2fs_find_file1 (symlink, oldnode, &currnode, &type); free (symlink); if (status == 0) { ext2fs_free_node (oldnode, currroot); return (0); } } ext2fs_free_node (oldnode, currroot); /* Found the node! */ if (!next || *next == '\0') { *currfound = currnode; *foundtype = type; return (1); } name = next; } return (-1); }