]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blobdiff - libhandle/handle.c
add lpath_to_handle to libhandle
[thirdparty/xfsprogs-dev.git] / libhandle / handle.c
index f93ac2f4c6340800bdca8064e4b211f21adf8fb8..6c9380de3b29049fde4942b46bf5dc60f8bac6b8 100644 (file)
@@ -1,42 +1,24 @@
 /*
- * Copyright (c) 1995, 2001-2003 Silicon Graphics, Inc.  All Rights Reserved.
+ * Copyright (c) 1995, 2001-2003, 2005 Silicon Graphics, Inc.
+ * All Rights Reserved.
  *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of version 2.1 of the GNU Lesser General Public License
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
  * as published by the Free Software Foundation.
  *
- * This program is distributed in the hope that it would be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ * This program is distributed in the hope that it would be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
  *
- * Further, this software is distributed without any warranty that it is
- * free of the rightful claim of any third person regarding infringement
- * or the like.  Any license provided herein, whether implied or
- * otherwise, applies only to this software file.  Patent licenses, if
- * any, provided herein do not apply to combinations of this program with
- * other software, or any other product whatsoever.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this program; if not, write the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307,
- * USA.
- *
- * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
- * Mountain View, CA  94043, or:
- *
- * http://www.sgi.com
- *
- * For further information regarding this notice, see:
- *
- * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write the Free Software Foundation,
+ * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  */
 
-#include <xfs/libxfs.h>
-
-/* attributes.h (purposefully) unavailable to xfsprogs, make do */
-struct attrlist_cursor { __u32 opaque[4]; };
-
+#include <xfs/xfs.h>
 #include <xfs/handle.h>
+#include <xfs/parent.h>
 
 /* just pick a value we know is more than big enough */
 #define        MAXHANSIZ       64
@@ -100,7 +82,7 @@ path_to_fshandle(
                close(fd);
                return result;
        }
-               
+
        if (handle_to_fsfd(*fshanp, &tmppath) >= 0) {
                /* this filesystem is already in the cache */
                close(fd);
@@ -128,22 +110,49 @@ path_to_handle(
        char            *path,          /* input,  path to convert */
        void            **hanp,         /* output, pointer to data */
        size_t          *hlen)          /* output, size of returned data */
+{
+       return lpath_to_handle(path, path, hanp, hlen);
+}
+
+/* Like path_to_handle, but reliable for paths which are either dangling
+ * symlinks or symlinks whose targets are not in XFS filesystems.
+ */
+int
+lpath_to_handle(
+       char            *fspath,        /* input,  path in filesystem */
+       char            *path,          /* input,  path to convert */
+       void            **hanp,         /* output, pointer to data */
+       size_t          *hlen)          /* output, size of returned data */
 {
        int             fd;
        int             result;
        comarg_t        obj;
 
-       fd = open(path, O_RDONLY);
+       fd = open(fspath, O_RDONLY);
        if (fd < 0)
                return -1;
 
        obj.path = path;
-       result = obj_to_handle(path, fd, XFS_IOC_PATH_TO_HANDLE,
+       result = obj_to_handle(fspath, fd, XFS_IOC_PATH_TO_HANDLE,
                                obj, hanp, hlen);
        close(fd);
        return result;
 }
 
+
+int
+fd_to_handle (
+       int             fd,             /* input,  file descriptor */
+       void            **hanp,         /* output, pointer to data */
+       size_t          *hlen)          /* output, size of returned data */
+{
+       comarg_t        obj;
+
+       obj.fd = fd;
+       return obj_to_handle(NULL, fd, XFS_IOC_FD_TO_HANDLE, obj, hanp, hlen);
+}
+
+
 int
 handle_to_fshandle(
        void            *hanp,
@@ -170,6 +179,12 @@ handle_to_fsfd(void *hanp, char **path)
 {
        struct fdhash   *fdhp;
 
+       /*
+        * Look in cache for matching fsid field in the handle
+        * (which is at the start of the handle).
+        * When found return the file descriptor and path that
+        * we have in the cache.
+        */
        for (fdhp = fdhash_head; fdhp != NULL; fdhp = fdhp->fnxt) {
                if (memcmp(fdhp->fsh, hanp, FSIDSIZE) == 0) {
                        *path = fdhp->fspath;
@@ -191,6 +206,7 @@ obj_to_handle(
 {
        char            hbuf [MAXHANSIZ];
        int             ret;
+       __uint32_t      handlen;
        xfs_fsop_handlereq_t hreq;
 
        if (opcode == XFS_IOC_FD_TO_HANDLE) {
@@ -205,19 +221,20 @@ obj_to_handle(
        hreq.ihandle  = NULL;
        hreq.ihandlen = 0;
        hreq.ohandle  = hbuf;
-       hreq.ohandlen = (__u32 *)hlen;
+       hreq.ohandlen = &handlen;
 
        ret = xfsctl(fspath, fsfd, opcode, &hreq);
        if (ret)
                return ret;
 
-       *hanp = malloc(*hlen);
+       *hanp = malloc(handlen);
        if (*hanp == NULL) {
                errno = ENOMEM;
                return -1;
        }
 
-       memcpy(*hanp, hbuf, (int) *hlen);
+       memcpy(*hanp, hbuf, handlen);
+       *hlen = handlen;
        return 0;
 }
 
@@ -282,6 +299,7 @@ readlink_by_handle(
        size_t          bufsiz)
 {
        int             fd;
+       __u32           buflen = (__u32)bufsiz;
        char            *path;
        xfs_fsop_handlereq_t hreq;
 
@@ -294,11 +312,12 @@ readlink_by_handle(
        hreq.ihandle  = hanp;
        hreq.ihandlen = hlen;
        hreq.ohandle  = buf;
-       hreq.ohandlen = (__u32 *)&bufsiz;
+       hreq.ohandlen = &buflen;
 
        return xfsctl(path, fd, XFS_IOC_READLINK_BY_HANDLE, &hreq);
 }
 
+/*ARGSUSED4*/
 int
 attr_multi_by_handle(
        void            *hanp,
@@ -337,7 +356,7 @@ attr_list_by_handle(
        int             flags,
        struct attrlist_cursor *cursor)
 {
-       int             fd;
+       int             error, fd;
        char            *path;
        xfs_fsop_attrlist_handlereq_t alhreq;
 
@@ -354,10 +373,41 @@ attr_list_by_handle(
 
        memcpy(&alhreq.pos, cursor, sizeof(alhreq.pos));
        alhreq.flags = flags;
-       alhreq.buflen = bufsize;
        alhreq.buffer = buf;
+       alhreq.buflen = bufsize;
+       /* prevent needless EINVAL from the kernel */
+       if (alhreq.buflen > XATTR_LIST_MAX)
+               alhreq.buflen = XATTR_LIST_MAX;
+
+       error = xfsctl(path, fd, XFS_IOC_ATTRLIST_BY_HANDLE, &alhreq);
+
+       memcpy(cursor, &alhreq.pos, sizeof(alhreq.pos));
+       return error;
+}
+
+int
+parents_by_handle(
+       void            *hanp,
+       size_t          hlen,
+       parent_t        *buf,
+       size_t          bufsiz,
+       unsigned int    *count)
+       
+{
+       errno = EOPNOTSUPP;
+       return -1;
+}
 
-       return xfsctl(path, fd, XFS_IOC_ATTRLIST_BY_HANDLE, &alhreq);
+int
+parentpaths_by_handle(
+       void            *hanp,
+       size_t          hlen,
+       parent_t        *buf,
+       size_t          bufsiz,
+       unsigned int    *count)
+{
+       errno = EOPNOTSUPP;
+       return -1;
 }
 
 int
@@ -386,6 +436,7 @@ fssetdm_by_handle(
        return xfsctl(path, fd, XFS_IOC_FSSETDM_BY_HANDLE, &dmhreq);
 }
 
+/*ARGSUSED1*/
 void
 free_handle(
        void            *hanp,