Subversion Repositories HelenOS

Compare Revisions

Ignore whitespace Rev 2375 → Rev 2376

/branches/fs/uspace/fs/path.c
0,0 → 1,331
/* This file contains the procedures that look up path names in the directory
* system and determine the inode number that goes with a given path name.
*/
 
/* Methods:
* eat_path: the 'main' routine of the path-to-inode conversion mechanism
* last_dir: find the final directory on a given path
* advance: parse one component of a path name
* search_dir: search a directory for a string and return its inode number
* search_dir_ex: used for extended versions
*/
 
 
#include <string.h>
#include "fs.h"
#include "block.h"
#include "file.h"
#include "fproc.h"
#include "inode.h"
#include "super.h"
 
 
static char *get_name(char *old_name, char *string, int string_length);
 
inode_t *eat_path(char *path)
{
/* Parse the path 'path' and put its inode in the inode table. If not possible,
* return NIL_INODE as function value and an error code in 'err_code'.
*/
register inode_t *ldip, *rip;
super_block_t *sp;
int name_len;
char string[name_len]; /* hold 1 path component name here */
name_len = NAME_MAX;
if ((sp = get_super()) == NIL_SUPER)
return NIL_INODE;
 
if (sp->s_extend) {
name_len = NAME_MAX_EX;
}
 
/* First open the path down to the final directory. */
if ( (ldip = last_dir(path, string, name_len)) == NIL_INODE)
return NIL_INODE; /* we couldn't open final directory */
 
/* The path consisting only of "/" is a special case, check for it. */
if (string[0] == '\0')
return ldip;
/* Get final component of the path. */
rip = advance(ldip, string, name_len);
put_inode(ldip);
 
return rip;
}
inode_t *last_dir(char *path, char *string, int string_length)
{
/* Given a path, 'path', located in the fs address space, parse it as
* far as the last directory, fetch the inode for the last directory into
* the inode table, and return a pointer to the inode. In
* addition, return the final component of the path in 'string'.
* If the last directory can't be opened, return NIL_INODE and
* the reason for failure in 'err_code'.
*/
register inode_t *rip;
register char *new_name;
register inode_t *new_ip;
/* Is the path absolute or relative? Initialize 'rip' accordingly. */
rip = (*path == '/' ? fp->fp_rootdir : fp->fp_workdir);
/* If dir has been removed or path is empty, return FS_ENOENT. */
if (rip->i_nlinks == 0 || *path == '\0') {
err_code = FS_ENOENT;
return NIL_INODE;
}
dup_inode(rip); /* inode will be returned with put_inode */
/* Scan the path component by component. */
while (TRUE) {
/* Extract one component. */
if ( (new_name = get_name(path, string, string_length)) == (char*) 0) {
put_inode(rip); /* bad path in user space */
return NIL_INODE;
}
if (*new_name == '\0')
{
if ( (rip->i_mode & I_TYPE) == I_DIRECTORY)
return rip; /* normal exit */
else {
/* last file of path prefix is not a directory */
put_inode(rip);
err_code = FS_ENOTDIR;
return NIL_INODE;
}
}
/* There is more path. Keep parsing. */
new_ip = advance(rip, string, string_length);
put_inode(rip);
 
if (new_ip == NIL_INODE) {
return NIL_INODE;
}
/* The call to advance() succeeded. Fetch next component. */
path = new_name;
rip = new_ip;
}
}
char *get_name(char *old_name, char *string, int string_length)
{
/* Given a pointer to a path name in fs space, 'old_name', copy the next
* component to 'string' and pad with zeros. A pointer to that part of
* the name as yet unparsed is returned. Roughly speaking,
* 'get_name' = 'old_name' - 'string'.
*
* This routine follows the standard convention that /usr/ast, /usr//ast,
* //usr///ast and /usr/ast/ are all equivalent.
*/
register int c;
register char *np, *rnp;
np = string; /* 'np' points to current position */
rnp = old_name; /* 'rnp' points to unparsed string */
while ((c = *rnp) == '/')
rnp++; /* skip leading slashes */
/* Copy the unparsed path, 'old_name', to the array, 'string'. */
while ( rnp < &old_name[PATH_MAX] && c != '/' && c != '\0') {
if (np < &string[string_length])
*np++ = c;
c = *++rnp; /* advance to next character */
}
/* To make /usr/ast/ equivalent to /usr/ast, skip trailing slashes. */
while (c == '/' && rnp < &old_name[PATH_MAX])
c = *++rnp;
/* Padding with zeroes */
if (np < &string[string_length])
*np = '\0'; /* Terminate string */
if (rnp >= &old_name[PATH_MAX]) {
err_code = FS_ENAMETOOLONG;
return((char *) 0);
}
 
return rnp;
}
inode_t *advance(inode_t *dirp, char *string, int string_length)
{
/* Given a directory and a component of a path, look up the component in
* the directory, find the inode, open it, and return a pointer to its inode
* slot. If it can't be done, return NIL_INODE.
*/
register inode_t *rip;
int r;
ino_t numb;
/* If 'string' is empty, yield same inode straight away. */
if (string[0] == '\0')
return get_inode((int) dirp->i_num);
/* Check for NIL_INODE. */
if (dirp == NIL_INODE)
return NIL_INODE;
/* If 'string' is not present in the directory, signal error. */
if (dirp->i_sp->s_extend) {
r = search_dir_ex(dirp, string, &numb, LOOK_UP);
}
else {
r = search_dir(dirp, string, &numb, LOOK_UP);
}
if (r != OK) {
err_code = r;
return NIL_INODE;
}
/* Don't go beyond the current root directory */
if (dirp == fp->fp_rootdir && strcmp(string, "..") == 0)
return(get_inode((int) dirp->i_num));
/* The component has been found in the directory. Get inode. */
if ( (rip = get_inode((int) numb)) == NIL_INODE)
return NIL_INODE;
return rip; /* return pointer to inode's component */
}
int search_dir(register inode_t *ldir_ptr, char string[NAME_MAX], ino_t *numb, int flag)
{
/* This function searches the directory whose inode is pointed to by 'ldip':
* if (flag == LOOK_UP) search for 'string' and return inode # in 'numb';
* if (flag == IS_EMPTY) return OK if only . and .. in dir else ENOTEMPTY;
*/
 
register direct_t *dp;
register block_t *bp;
int r, match;
offset_t pos;
unsigned old_slots;
block_num_t b;
/* If 'ldir_ptr' is not a pointer to a dir inode, error. */
if ((ldir_ptr->i_mode & I_TYPE) != I_DIRECTORY)
return FS_ENOTDIR;
r = OK;
/* Step through the directory one block at a time. */
old_slots = (unsigned)(ldir_ptr->i_size/DIR_ENTRY_SIZE);
match = 0; /* set when a string match occurs */
for (pos = 0; pos < ldir_ptr->i_size; pos += BLOCK_SIZE) {
b = read_map(ldir_ptr, pos); /* get block number */
/* Since directories don't have holes, 'b' cannot be NO_BLOCK. */
bp = get_block(b); /* get a dir block */
/* Search a directory block. */
for (dp = &bp->b.b__dir[0]; dp < &bp->b.b__dir[NR_DIR_ENTRIES]; dp++) {
/* Match occurs if string found. */
if (dp->d_ino != 0) {
if (flag == IS_EMPTY) {
/* If this test succeeds, dir is not empty. */
if (strcmp(dp->d_name, "." ) != 0 &&
strcmp(dp->d_name, "..") != 0)
match = 1;
}
else {
if (fs_strncmp(dp->d_name, string, NAME_MAX) == 0)
match = 1;
}
}
if (match) {
/* LOOK_UP or DELETE found what it wanted. */
r = OK;
if (flag == IS_EMPTY) {
r = FS_ENOTEMPTY;
}
else {
*numb = (int) dp->d_ino;
}
return r;
}
}
}
/* The whole directory has now been searched. */
return(flag == IS_EMPTY ? OK : FS_ENOENT);
}
 
int search_dir_ex(register inode_t *ldir_ptr, char string[NAME_MAX_EX], ino_t *numb, int flag)
{
/* Same as above, but for extened directory etries - 30 chars in name of file. */
 
register directex_t *dp;
register block_t *bp;
int r, match;
offset_t pos;
unsigned old_slots;
block_num_t b;
/* If 'ldir_ptr' is not a pointer to a dir inode, error. */
if ( (ldir_ptr->i_mode & I_TYPE) != I_DIRECTORY)
return FS_ENOTDIR;
r = OK;
/* Step through the directory one block at a time. */
old_slots = (unsigned)(ldir_ptr->i_size/DIR_ENTRY_SIZE_EX);
match = 0; /* set when a string match occurs */
for (pos = 0; pos < ldir_ptr->i_size; pos += BLOCK_SIZE) {
b = read_map(ldir_ptr, pos); /* get block number */
/* Since directories don't have holes, 'b' cannot be NO_BLOCK. */
bp = get_block(b); /* get a dir block */
/* Search a directory block. */
for (dp = &bp->b.b__direx[0]; dp < &bp->b.b__direx[NR_DIR_ENTRIES_EX]; dp++) {
/* Match occurs if string found. */
if (dp->d_ino != 0) {
if (flag == IS_EMPTY) {
/* If this test succeeds, dir is not empty. */
if (strcmp(dp->d_name, "." ) != 0 &&
strcmp(dp->d_name, "..") != 0) match = 1;
}
else {
if (fs_strncmp(dp->d_name, string, NAME_MAX_EX) == 0)
match = 1;
}
}
if (match) {
/* LOOK_UP or DELETE found what it wanted. */
r = OK;
if (flag == IS_EMPTY) {
r = FS_ENOTEMPTY;
}
else {
*numb = (int)dp->d_ino;
}
return r;
}
}
}
/* The whole directory has now been searched. */
return(flag == IS_EMPTY ? OK : FS_ENOENT);
}