diff options
Diffstat (limited to 'libnetfs')
-rw-r--r-- | libnetfs/dir-lookup.c | 312 |
1 files changed, 273 insertions, 39 deletions
diff --git a/libnetfs/dir-lookup.c b/libnetfs/dir-lookup.c index ddd08e1c..87535a8b 100644 --- a/libnetfs/dir-lookup.c +++ b/libnetfs/dir-lookup.c @@ -19,9 +19,18 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA. */ #include <fcntl.h> +#include <assert.h> +#include <string.h> +#include <stdio.h> +#include <hurd/paths.h> #include "netfs.h" #include "fs_S.h" +#include "callbacks.h" +#include "misc.h" +/* XXX - Temporary hack; this belongs in a header file, probably types.h. */ +#define major(x) ((int)(((unsigned) (x) >> 8) & 0xff)) +#define minor(x) ((int)((x) & 0xff)) error_t netfs_S_dir_lookup (struct protid *diruser, @@ -35,11 +44,14 @@ netfs_S_dir_lookup (struct protid *diruser, { int create; /* true if O_CREAT flag set */ int excl; /* true if O_EXCL flag set */ - int mustbedir; /* true if the result must be S_IFDIR */ - int lastcomp; /* true if we are at the last component */ - int newnode; /* true if this node is newly created */ + int mustbedir = 0; /* true if the result must be S_IFDIR */ + int lastcomp = 0; /* true if we are at the last component */ + int newnode = 0; /* true if this node is newly created */ + int nsymlinks = 0; struct node *dnp, *np; char *nextname; + error_t error; + struct protid *newpi; if (!diruser) return EOPNOTSUPP; @@ -51,8 +63,8 @@ netfs_S_dir_lookup (struct protid *diruser, while (*filename == '/') filename++; - *retry_poly = MACH_MSG_TYPE_MAKE_SEND; - *retry = FS_RETRY_NORMAL; + *retry_port_type = MACH_MSG_TYPE_MAKE_SEND; + *do_retry = FS_RETRY_NORMAL; *retry_name = '\0'; if (*filename == '\0') @@ -94,50 +106,272 @@ netfs_S_dir_lookup (struct protid *diruser, create = 0; } else - lastcomp = 1; - - np = 0; + lastcomp = 0; + } + else + lastcomp = 1; + + np = 0; - /* Attempt a lookup on the next pathname component. */ - error = netfs_attempt_lookup (user, dnp, nextname, &np); + /* Attempt a lookup on the next pathname component. */ + error = netfs_attempt_lookup (diruser->credential, dnp, nextname, &np); - /* Implement O_EXCL flag here */ - if (lastcomp && create && excl && (!error || error == EAGAIN)) - error = EEXIST; + /* Implement O_EXCL flag here */ + if (lastcomp && create && excl && (!error || error == EAGAIN)) + error = EEXIST; + + /* If we get an error, we're done */ + if (error == EAGAIN) + { + /* This really means .. from root */ + if (diruser->po->dotdotport != MACH_PORT_NULL) + { + *do_retry = FS_RETRY_REAUTH; + *retry_port = diruser->po->dotdotport; + *retry_port_type = MACH_MSG_TYPE_COPY_SEND; + if (!lastcomp) + strcpy (retry_name, nextname); + error = 0; + goto out; + } + else + { + /* We are the global root; .. from our root is + just our root again. */ + error = 0; + np = dnp; + netfs_nref (np); + } + } + + /* Create the new node if necessary */ + if (lastcomp && create && error == ENOENT) + { + mode &= ~(S_IFMT | S_ISPARE | S_ISVTX); + mode |= S_IFREG; + error = netfs_attempt_create_file (diruser->credential, dnp, + filename, mode, &np); + newnode = 1; + } + + /* If this is translated, start the translator (if necessary) + and return. */ + if ((((flags & O_NOTRANS) == 0) || !lastcomp) + && (np->istranslated + || S_ISFIFO (np->nn_stat.st_mode) + || S_ISCHR (np->nn_stat.st_mode) + || S_ISBLK (np->nn_stat.st_mode) + || fshelp_translated (&np->transbox))) + { + mach_port_t dirport; + uid_t *uids, *gids; + int nuids, ngids; - /* If we get an error, we're done */ - if (error == EAGAIN) + /* A callback function for short-circuited translators. + S_ISLNK and S_IFSOCK are handled elsewhere. */ + error_t short_circuited_callback1 (void *cookie1, void *cookie2, + uid_t *uid, gid_t *gid, + char **argz, int *argz_len) { - /* This really means .. from root */ - if (diruser->po->dotdotport != MACH_PORT_NULL) + struct node *np = cookie1; + error_t err; + + err = netfs_validate_stat (np, diruser); + if (err) + return err; + + switch (np->nn_stat.st_mode & S_IFMT) { - *retry = FS_RETRY_REAUTH; - *retry_port = dircred->po->dotdotport; - *retry_port_type = MACH_MSG_TYPE_COPY_SEND; - if (!lastcomp) - strcpy (retry_name, nextname); - error = 0; - goto out; + case S_IFCHR: + case S_IFBLK: + asprintf (argz, "%s%c%d%c%d", + (S_ISCHR (np->nn_stat.st_mode) + ? _HURD_CHRDEV : _HURD_BLKDEV), + 0, major (np->nn_stat.st_rdev), + 0, minor (np->nn_stat.st_rdev)); + *argz_len = strlen (*argz) + 1; + *argz_len += strlen (*argz + *argz_len) + 1; + *argz_len += strlen (*argz + *argz_len) + 1; + break; + case S_IFIFO: + asprintf (argz, "%s", _HURD_FIFO); + *argz_len = strlen (*argz) + 1; + break; + default: + return ENOENT; } - else + + *uid = np->nn_stat.st_uid; + *gid = np->nn_stat.st_gid; + + return 0; + } + + /* Create an unauthenticated port for DNP, and then + unlock it. */ + newpi = + netfs_make_protid (netfs_make_peropen (dnp, 0, + diruser->po->dotdotport), + 0); + dirport = ports_get_right (newpi); + mach_port_insert_right (mach_task_self (), dirport, dirport, + MACH_MSG_TYPE_MAKE_SEND); + ports_port_deref (newpi); + if (np != dnp) + mutex_unlock (&dnp->lock); + + netfs_interpret_credential (diruser->credential, &uids, &nuids, + &gids, &ngids); + error = fshelp_fetch_root (&np->transbox, &diruser->po->dotdotport, + dirport, uids, nuids, gids, ngids, + lastcomp ? flags : 0, + (np->istranslated + ? _netfs_translator_callback1 + : short_circuited_callback1), + _netfs_translator_callback2, + do_retry, retry_name, retry_port); + free (uids); + free (gids); + if (error != ENOENT) + { + netfs_nrele (dnp); + netfs_nput (np); + *retry_port_type = MACH_MSG_TYPE_MOVE_SEND; + if (!lastcomp && !error) { - /* We are the global root; .. from our root is - just our root again. */ - error = 0; - np = dnp; - netfs_nref (np); + strcat (retry_name, "/"); + strcat (retry_name, nextname); } + return error; } - /* Create the new node if necessary */ - if (lastcomp && create) + /* ENOENT means there was a hiccup, and the translator vanished + while NP was unlocked inside fshelp_fetch_root. + Reacquire the locks and continue as normal. */ + error = 0; + if (np != dnp) { - if (error == ENOENT) + if (!strcmp (filename, "..")) + mutex_lock (&dnp->lock); + else { - mode &= ~(S_IFMT | S_ISPARE | S_ISVTX); - mode |= S_IFREG; - error = netfs_attempt_create_file (diruser, dnp, - filename, mode, &np); - newnode = 1; + mutex_unlock (&np->lock); + mutex_lock (&dnp->lock); + mutex_lock (&np->lock); } - + } + } + + netfs_validate_stat (np, diruser); + + if (S_ISLNK (np->nn_stat.st_mode) + && !(lastcomp && (flags & (O_NOLINK|O_NOTRANS)))) + { + size_t nextnamelen, newnamelen, linklen; + char *linkbuf; + + /* Handle symlink interpretation */ + if (nsymlinks++ > netfs_maxsymlinks) + { + error = ELOOP; + goto out; + } + + linklen = np->nn_stat.st_size; + + nextnamelen = nextname ? strlen (nextname) + 1 : 0; + newnamelen = nextnamelen + linklen + 1; + linkbuf = alloca (newnamelen); + + error = netfs_attempt_readlink (diruser->credential, np, linkbuf); + if (error) + goto out; + + if (nextname) + { + linkbuf[linklen] = '/'; + bcopy (nextname, linkbuf + linklen + 1, + nextnamelen - 1); + } + linkbuf[nextnamelen + linklen] = '\0'; + + if (linkbuf[0] == '/') + { + /* Punt to the caller */ + *do_retry = FS_RETRY_MAGICAL; + *retry_port = MACH_PORT_NULL; + strcpy (retry_name, linkbuf); + goto out; + } + + filename = linkbuf; + if (lastcomp) + { + lastcomp = 0; + + /* Symlinks to nonexistent files aren't allowed to cause + creation, so clear the flag here. */ + create = 0; + } + netfs_nput (np); + np = 0; + } + else + { + /* Normal nodes here */ + filename = nextname; + if (np == dnp) + netfs_nrele (dnp); + else + netfs_nput (dnp); + + if (!lastcomp) + { + dnp = np; + np = 0; + } + else + dnp = 0; + } + } + while (filename && *filename); + + /* At this point, NP is the node to return. */ + gotit: + + if (mustbedir) + { + netfs_validate_stat (np, diruser); + if (!S_ISDIR (np->nn_stat.st_mode)) + { + error = ENOTDIR; + goto out; + } + } + error = netfs_check_open_permissions (diruser->credential, np, + flags, newnode); + if (error) + goto out; + + flags &= ~OPENONLY_STATE_MODES; + + newpi = netfs_make_protid (netfs_make_peropen (np, flags, + diruser->po->dotdotport), + diruser->credential); + *retry_port = ports_get_right (newpi); + ports_port_deref (newpi); + + out: + if (np) + { + if (dnp == np) + netfs_nrele (np); + else + netfs_nput (np); + } + if (dnp) + netfs_nput (dnp); + return error; +} + + |