Hi,

I've written the attached patch to add symlink support for shared folders
between linux guests and posix host systems.  It should be compatible with
other systems (although I've only got linux here to test it on), and there's
an option in the guest's kernel module to turn it off if the old behaviour
is needed.

The patch is MIT licensed, and made against the current subversion revision
(33179).

Cheers,

  Brian
diff --git a/include/VBox/shflsvc.h b/include/VBox/shflsvc.h
index a9db032..d5710ed 100644
--- a/include/VBox/shflsvc.h
+++ b/include/VBox/shflsvc.h
@@ -98,6 +98,12 @@
 #define SHFL_FN_SET_UTF8            (16)
 /** Map folder */
 #define SHFL_FN_MAP_FOLDER          (17)
+/** Read symlink destination */
+#define SHFL_FN_READLINK            (18)
+/** Create symlink */
+#define SHFL_FN_SYMLINK             (19)
+/** Ask host to show symlinks */
+#define SHFL_FN_SET_SYMLINKS        (20)
 
 /** @} */
 
@@ -862,6 +868,37 @@ typedef struct _VBoxSFList
 
 
 /**
+ * SHFL_FN_READLINK
+ */
+
+/** Parameters structure. */
+typedef struct _VBoxSFReadlink
+{
+    VBoxGuestHGCMCallInfo callInfo;
+
+    /** pointer, in: SHFLROOT
+     * Root handle of the mapping which name is queried.
+     */
+    HGCMFunctionParameter root;
+
+    /** pointer, in:
+     * Points to SHFLSTRING buffer.
+     */
+    HGCMFunctionParameter path;
+
+    /** pointer, out:
+     * Buffer to place data to.
+     */
+    HGCMFunctionParameter buffer;
+
+} VBoxSFReadlink;
+
+/** Number of parameters */
+#define SHFL_CPARMS_READLINK (3)
+
+
+
+/**
  * SHFL_FN_INFORMATION
  */
 
@@ -990,6 +1027,42 @@ typedef struct _VBoxSFRename
 
 #define SHFL_CPARMS_RENAME  (4)
 
+
+/**
+ * SHFL_FN_SYMLINK
+ */
+
+/** Parameters structure. */
+typedef struct _VBoxSFSymlink
+{
+    VBoxGuestHGCMCallInfo callInfo;
+
+    /** pointer, in: SHFLROOT
+     * Root handle of the mapping which name is queried.
+     */
+    HGCMFunctionParameter root;
+
+    /** pointer, in:
+     * Points to SHFLSTRING of path for the new symlink.
+     */
+    HGCMFunctionParameter newPath;
+
+    /** pointer, in:
+     * Points to SHFLSTRING of destination for symlink.
+     */
+    HGCMFunctionParameter oldPath;
+
+    /** pointer, out:
+     * Information about created symlink.
+     */
+    HGCMFunctionParameter info;
+
+} VBoxSFSymlink;
+
+#define SHFL_CPARMS_SYMLINK  (4)
+
+
+
 /**
  * SHFL_FN_ADD_MAPPING
  * Host call, no guest structure is used.
diff --git a/include/iprt/path.h b/include/iprt/path.h
index 9a845c4..86093de 100644
--- a/include/iprt/path.h
+++ b/include/iprt/path.h
@@ -632,6 +632,18 @@ RTR3DECL(int) RTPathQueryInfo(const char *pszPath, PRTFSOBJINFO pObjInfo, RTFSOB
 RTR3DECL(int) RTPathQueryInfoEx(const char *pszPath, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAdditionalAttribs, uint32_t fFlags);
 
 /**
+ * Return the destination of a symbolic link.
+ *
+ * @returns IPRT status code.
+ * @param   pszPath     Path to the file system object.
+ * @param   pszDest     Where to store the destination path.
+ * @param   cchPath     Size of the buffer.
+ */
+#ifndef RT_OS_WINDOWS
+RTR3DECL(int) RTReadlink(const char *pszPath, char *pszDest, uint32_t cchPath);
+#endif
+
+/**
  * Changes the mode flags of a file system object.
  *
  * The API requires at least one of the mode flag sets (Unix/Dos) to
@@ -785,6 +797,17 @@ RTR3DECL(int) RTPathGetOwner(const char *pszPath, uint32_t *pUid, uint32_t *pGid
  */
 RTR3DECL(int) RTPathRename(const char *pszSrc,  const char *pszDst, unsigned fRename);
 
+/**
+ * Create a symlink from new path to old path.
+ *
+ * @returns IPRT status code.
+ * @param   pszNewPath  The path for the new symlink.
+ * @param   pszOldPath  The destination path for the symlink.
+ */
+#ifndef RT_OS_WINDOWS
+RTR3DECL(int) RTSymlink(const char *pszNewPath, const char *pszOldPath);
+#endif
+
 #endif /* IN_RING3 */
 
 /** @} */
diff --git a/src/VBox/Additions/common/VBoxGuestLib/VBoxGuestR0LibSharedFolders.c b/src/VBox/Additions/common/VBoxGuestLib/VBoxGuestR0LibSharedFolders.c
index 92f3f60..93e2391 100644
--- a/src/VBox/Additions/common/VBoxGuestLib/VBoxGuestR0LibSharedFolders.c
+++ b/src/VBox/Additions/common/VBoxGuestLib/VBoxGuestR0LibSharedFolders.c
@@ -39,6 +39,7 @@
 #include <iprt/string.h>
 
 #define SHFL_CPARMS_SET_UTF8 0
+#define SHFL_CPARMS_SET_SYMLINKS 0
 
 #define VBOX_INIT_CALL(a, b, c)          \
     (a)->result      = VINF_SUCCESS;     \
@@ -680,4 +681,87 @@ DECLVBGL(int) vboxCallSetUtf8 (PVBSFCLIENT pClient)
     return rc;
 }
 
+DECLVBGL(int) vboxReadlink (PVBSFCLIENT pClient, PVBSFMAP pMap, PSHFLSTRING pParsedPath,
+                            uint32_t cbBuffer, uint8_t *pBuffer)
+{
+    int rc = VINF_SUCCESS;
+
+    VBoxSFReadlink data;
+
+    VBOX_INIT_CALL(&data.callInfo, READLINK, pClient);
+
+    data.root.type                      = VMMDevHGCMParmType_32bit;
+    data.root.u.value32                 = pMap->root;
+
+    data.path.type                      = VMMDevHGCMParmType_LinAddr_In;
+    data.path.u.Pointer.size            = ShflStringSizeOfBuffer (pParsedPath);
+    data.path.u.Pointer.u.linearAddr    = (uintptr_t)pParsedPath;
+
+    data.buffer.type                    = VMMDevHGCMParmType_LinAddr_Out;
+    data.buffer.u.Pointer.size          = cbBuffer;
+    data.buffer.u.Pointer.u.linearAddr  = (uintptr_t)pBuffer;
+
+    rc = VbglHGCMCall (pClient->handle, &data.callInfo, sizeof (data));
+
+/*    Log(("VBOXSF: VBoxSF::vboxCallReadline: "
+         "VbglHGCMCall rc = %#x, result = %#x\n", rc, data.callInfo.result));
+*/
+    if (RT_SUCCESS (rc))
+    {
+        rc = data.callInfo.result;
+    }
+    return rc;
+}
+
+DECLVBGL(int) vboxCallSymlink (PVBSFCLIENT pClient, PVBSFMAP pMap, PSHFLSTRING pNewPath, PSHFLSTRING pOldPath, PRTFSOBJINFO pBuffer)
+{
+    int rc = VINF_SUCCESS;
+
+    VBoxSFSymlink data;
+
+    VBOX_INIT_CALL(&data.callInfo, SYMLINK, pClient);
+
+    data.root.type                      = VMMDevHGCMParmType_32bit;
+    data.root.u.value32                 = pMap->root;
+
+    data.newPath.type                   = VMMDevHGCMParmType_LinAddr_In;
+    data.newPath.u.Pointer.size         = ShflStringSizeOfBuffer (pNewPath);
+    data.newPath.u.Pointer.u.linearAddr = (uintptr_t)pNewPath;
+
+    data.oldPath.type                   = VMMDevHGCMParmType_LinAddr_In;
+    data.oldPath.u.Pointer.size         = ShflStringSizeOfBuffer (pOldPath);
+    data.oldPath.u.Pointer.u.linearAddr = (uintptr_t)pOldPath;
+
+    data.info.type                      = VMMDevHGCMParmType_LinAddr_Out;
+    data.info.u.Pointer.size            = sizeof(RTFSOBJINFO);
+    data.info.u.Pointer.u.linearAddr    = (uintptr_t)pBuffer;
+
+    rc = VbglHGCMCall (pClient->handle, &data.callInfo, sizeof (data));
+
+/*    Log(("VBOXSF: VBoxSF::vboxCallSymlink: "
+         "VbglHGCMCall rc = %#x, result = %#x\n", rc, data.callInfo.result));
+*/
+    if (RT_SUCCESS (rc))
+    {
+        rc = data.callInfo.result;
+    }
+    return rc;
+}
+
+DECLVBGL(int) vboxCallSetSymlinks (PVBSFCLIENT pClient)
+{
+    int rc = VINF_SUCCESS;
+
+    VBoxGuestHGCMCallInfo callInfo;
+
+    VBOX_INIT_CALL (&callInfo, SET_SYMLINKS, pClient);
+    rc = VbglHGCMCall (pClient->handle, &callInfo, sizeof (callInfo));
+    if (RT_SUCCESS (rc))
+    {
+        rc = callInfo.result;
+    }
+    return rc;
+}
+
+
 #endif /* !VBGL_VBOXGUEST */
diff --git a/src/VBox/Additions/common/VBoxGuestLib/VBoxGuestR0LibSharedFolders.h b/src/VBox/Additions/common/VBoxGuestLib/VBoxGuestR0LibSharedFolders.h
index 14c5cf2..de3bd77 100644
--- a/src/VBox/Additions/common/VBoxGuestLib/VBoxGuestR0LibSharedFolders.h
+++ b/src/VBox/Additions/common/VBoxGuestLib/VBoxGuestR0LibSharedFolders.h
@@ -178,4 +178,8 @@ DECLVBGL(int) vboxCallMapFolder (PVBSFCLIENT pClient, PSHFLSTRING szFolderName,
 DECLVBGL(int) vboxCallUnmapFolder (PVBSFCLIENT pClient, PVBSFMAP pMap);
 DECLVBGL(int) vboxCallSetUtf8 (PVBSFCLIENT pClient);
 
+DECLVBGL(int) vboxReadlink (PVBSFCLIENT pClient, PVBSFMAP pMap, PSHFLSTRING ParsedPath, uint32_t pcbBuffer, uint8_t *pBuffer);
+DECLVBGL(int) vboxCallSymlink (PVBSFCLIENT pClient, PVBSFMAP pMap, PSHFLSTRING pNewPath, PSHFLSTRING pOldPath, PRTFSOBJINFO pBuffer);
+DECLVBGL(int) vboxCallSetSymlinks (PVBSFCLIENT pClient);
+
 #endif /* __VBOXCALLS__H */
diff --git a/src/VBox/Additions/linux/sharedfolders/Makefile.kmk b/src/VBox/Additions/linux/sharedfolders/Makefile.kmk
index ed27a3f..4fefb45 100644
--- a/src/VBox/Additions/linux/sharedfolders/Makefile.kmk
+++ b/src/VBox/Additions/linux/sharedfolders/Makefile.kmk
@@ -61,6 +61,7 @@ vboxsf_SOURCES         = \
 	vfsmod.c \
 	utils.c \
 	dirops.c \
+	lnkops.c \
 	regops.c
 vboxsf_LIBS            = \
 	$(VBOX_LIB_VBGL_R0)
diff --git a/src/VBox/Additions/linux/sharedfolders/Makefile.module b/src/VBox/Additions/linux/sharedfolders/Makefile.module
index e4b9527..118c8c8 100644
--- a/src/VBox/Additions/linux/sharedfolders/Makefile.module
+++ b/src/VBox/Additions/linux/sharedfolders/Makefile.module
@@ -62,6 +62,7 @@ override MODULE = vboxsf
 OBJS   = \
 	vfsmod.o \
 	dirops.o \
+	lnkops.o \
 	regops.o \
 	utils.o \
 	GenericRequest.o \
diff --git a/src/VBox/Additions/linux/sharedfolders/dirops.c b/src/VBox/Additions/linux/sharedfolders/dirops.c
index facb7af..06b46af 100644
--- a/src/VBox/Additions/linux/sharedfolders/dirops.c
+++ b/src/VBox/Additions/linux/sharedfolders/dirops.c
@@ -733,6 +733,64 @@ static int sf_rename(struct inode *old_parent, struct dentry *old_dentry,
     return err;
 }
 
+static int sf_symlink(struct inode *parent, struct dentry *dentry, const char *symname)
+{
+    int err;
+    int rc;
+    struct sf_inode_info *sf_i;
+    struct sf_glob_info *sf_g;
+    SHFLSTRING *path, *ssymname;
+    RTFSOBJINFO info;
+    int symname_len = strlen(symname) + 1;
+
+    TRACE();
+    sf_g = GET_GLOB_INFO(parent->i_sb);
+    sf_i = GET_INODE_INFO(parent);
+
+    BUG_ON(!sf_g);
+    BUG_ON(!sf_i);
+
+    err = sf_path_from_dentry(__func__, sf_g, sf_i, dentry, &path);
+    if (err) {
+        goto fail0;
+    }
+
+    ssymname = kmalloc(offsetof(SHFLSTRING, String.utf8) + symname_len, GFP_KERNEL);
+    if (!ssymname) {
+        LogRelFunc(("kmalloc failed, caller=sf_symlink\n"));
+        err = -ENOMEM;
+        goto fail1;
+    }
+
+    ssymname->u16Length = symname_len - 1;
+    ssymname->u16Size = symname_len;
+    memcpy(ssymname->String.utf8, symname, symname_len);
+
+    rc = vboxCallSymlink(&client_handle, &sf_g->map, path, ssymname, &info);
+    if (RT_FAILURE(rc)) {
+        if (rc == VERR_WRITE_PROTECT) {
+            err = -EROFS;
+            goto fail2;
+        }
+        err = -EPROTO;
+        goto fail2;
+    }
+
+    err = sf_instantiate(parent, dentry, path, &info, SHFL_HANDLE_NIL);
+    if (err) {
+        LogFunc(("(%d): could not instantiate dentry for %s err=%d\n",
+                 fDirectory, sf_i->path->String.utf8, err));
+        goto fail2;
+    }
+
+fail2:
+    kfree(ssymname);
+fail1:
+    kfree(path);
+fail0:
+    return err;
+}
+
 struct inode_operations sf_dir_iops =
 {
     .lookup     = sf_lookup,
@@ -745,6 +803,7 @@ struct inode_operations sf_dir_iops =
     .revalidate = sf_inode_revalidate
 #else
     .getattr    = sf_getattr,
-    .setattr    = sf_setattr
+    .setattr    = sf_setattr,
+    .symlink    = sf_symlink
 #endif
 };
diff --git a/src/VBox/Additions/linux/sharedfolders/files_vboxsf b/src/VBox/Additions/linux/sharedfolders/files_vboxsf
index 9a9e2da..391cd2b 100644
--- a/src/VBox/Additions/linux/sharedfolders/files_vboxsf
+++ b/src/VBox/Additions/linux/sharedfolders/files_vboxsf
@@ -71,6 +71,7 @@ FILES_VBOXSF_NOBIN=" \
     ${PATH_ROOT}/src/VBox/Runtime/r0drv/linux/the-linux-kernel.h=>r0drv/linux/the-linux-kernel.h \
     ${PATH_ROOT}/src/VBox/Additions/linux/sharedfolders/Makefile.module=>Makefile \
     ${PATH_ROOT}/src/VBox/Additions/linux/sharedfolders/dirops.c=>dirops.c \
+    ${PATH_ROOT}/src/VBox/Additions/linux/sharedfolders/lnkops.c=>lnkops.c \
     ${PATH_ROOT}/src/VBox/Additions/linux/sharedfolders/regops.c=>regops.c \
     ${PATH_ROOT}/src/VBox/Additions/linux/sharedfolders/utils.c=>utils.c \
     ${PATH_ROOT}/src/VBox/Additions/linux/sharedfolders/vbsfmount.h=>vbsfmount.h \
diff --git a/src/VBox/Additions/linux/sharedfolders/lnkops.c b/src/VBox/Additions/linux/sharedfolders/lnkops.c
new file mode 100644
index 0000000..b1480be
--- /dev/null
+++ b/src/VBox/Additions/linux/sharedfolders/lnkops.c
@@ -0,0 +1,36 @@
+#include "vfsmod.h"
+
+static void *sf_follow_link(struct dentry *dentry, struct nameidata *nd)
+{
+        struct inode *inode = dentry->d_inode;
+        struct sf_glob_info *sf_g = GET_GLOB_INFO(inode->i_sb);
+        struct sf_inode_info *sf_i = GET_INODE_INFO(inode);
+        int error = -ENOMEM;
+        unsigned long page = get_zeroed_page(GFP_KERNEL);
+        int rc;
+
+        if (page) {
+                error = 0;
+                rc = vboxReadlink(&client_handle, &sf_g->map, sf_i->path, PATH_MAX, (char *)page);
+                if (RT_FAILURE(rc)) {
+                        LogFunc(("vboxReadlink failed, caller=%s, rc=%Rrc\n",
+                                 __func__, rc));
+                        error = -EPROTO;
+                }
+        }
+        nd_set_link(nd, error ? ERR_PTR(error) : (char *)page);
+        return NULL;
+}
+
+static void sf_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
+{
+        char *page = nd_get_link(nd);
+        if (!IS_ERR(page))
+                free_page((unsigned long)page);
+}
+
+struct inode_operations sf_lnk_iops = {
+        .readlink       = generic_readlink,
+        .follow_link    = sf_follow_link,
+        .put_link       = sf_put_link
+};
diff --git a/src/VBox/Additions/linux/sharedfolders/utils.c b/src/VBox/Additions/linux/sharedfolders/utils.c
index 4902c7b..036e78c 100644
--- a/src/VBox/Additions/linux/sharedfolders/utils.c
+++ b/src/VBox/Additions/linux/sharedfolders/utils.c
@@ -111,15 +111,19 @@ void sf_init_inode(struct sf_glob_info *sf_g, struct inode *inode,
            in the directory plus two (. ..) */
         inode->i_nlink = 1;
     }
+    else if (RTFS_IS_SYMLINK(attr->fMode))
+    {
+        inode->i_mode  = sf_g->fmode != ~0 ? (sf_g->fmode & 0777): mode;
+        inode->i_mode &= ~sf_g->fmask;
+        inode->i_mode |= S_IFLNK;
+        inode->i_op    = &sf_lnk_iops;
+        inode->i_nlink = 1;
+    }
     else
     {
         inode->i_mode  = sf_g->fmode != ~0 ? (sf_g->fmode & 0777): mode;
         inode->i_mode &= ~sf_g->fmask;
         inode->i_mode |= S_IFREG;
-#if 0
-        if (RTFS_IS_SYMLINK(attr->fMode))
-            inode->i_mode |= S_IFLNK;
-#endif
         inode->i_op    = &sf_reg_iops;
         inode->i_fop   = &sf_reg_fops;
         inode->i_nlink = 1;
diff --git a/src/VBox/Additions/linux/sharedfolders/vfsmod.c b/src/VBox/Additions/linux/sharedfolders/vfsmod.c
index 77295e5..7d9ecdd 100644
--- a/src/VBox/Additions/linux/sharedfolders/vfsmod.c
+++ b/src/VBox/Additions/linux/sharedfolders/vfsmod.c
@@ -471,6 +471,10 @@ static struct file_system_type vboxsf_fs_type =
 };
 #endif
 
+static int follow_symlinks = 0;
+module_param(follow_symlinks, bool, 0);
+MODULE_PARM_DESC(follow_symlinks, "Let host resolve symlinks rather than showing them");
+
 /* Module initialization/finalization handlers */
 static int __init init(void)
 {
@@ -520,6 +524,16 @@ static int __init init(void)
         goto fail2;
     }
 
+    if (!follow_symlinks) {
+        rcVBox = vboxCallSetSymlinks(&client_handle);
+        if (RT_FAILURE(rcVBox)) {
+            printk(KERN_WARNING
+                     "vboxsf: Host unable to show symlinks, rc=%d\n",
+                     rcVBox);
+        }
+    }
+
+
     printk(KERN_DEBUG
             "vboxsf: Successfully loaded version " VBOX_VERSION_STRING
             " (interface " RT_XSTR(VMMDEV_VERSION) ")\n");
diff --git a/src/VBox/Additions/linux/sharedfolders/vfsmod.h b/src/VBox/Additions/linux/sharedfolders/vfsmod.h
index e20cfbe..9503bda 100644
--- a/src/VBox/Additions/linux/sharedfolders/vfsmod.h
+++ b/src/VBox/Additions/linux/sharedfolders/vfsmod.h
@@ -87,6 +87,7 @@ extern VBSFCLIENT client_handle;
 
 /* forward declarations */
 extern struct inode_operations         sf_dir_iops;
+extern struct inode_operations         sf_lnk_iops;
 extern struct inode_operations         sf_reg_iops;
 extern struct file_operations          sf_dir_fops;
 extern struct file_operations          sf_reg_fops;
diff --git a/src/VBox/Additions/x11/vboxmouse/undefined_15 b/src/VBox/Additions/x11/vboxmouse/undefined_15
index db08d62..92bc3b3 100644
--- a/src/VBox/Additions/x11/vboxmouse/undefined_15
+++ b/src/VBox/Additions/x11/vboxmouse/undefined_15
@@ -84,6 +84,7 @@ strlen
 strncmp
 strpbrk
 strstr
+symlink
 tolower
 unlink
 unsetenv
diff --git a/src/VBox/Additions/x11/vboxmouse/undefined_70 b/src/VBox/Additions/x11/vboxmouse/undefined_70
index be24665..9561647 100644
--- a/src/VBox/Additions/x11/vboxmouse/undefined_70
+++ b/src/VBox/Additions/x11/vboxmouse/undefined_70
@@ -93,6 +93,7 @@ strlen
 strncmp
 strpbrk
 strstr
+symlink
 tolower
 unlink
 unsetenv
diff --git a/src/VBox/Additions/x11/vboxmouse/undefined_71 b/src/VBox/Additions/x11/vboxmouse/undefined_71
index ea34be8..b313f0f 100644
--- a/src/VBox/Additions/x11/vboxmouse/undefined_71
+++ b/src/VBox/Additions/x11/vboxmouse/undefined_71
@@ -100,6 +100,7 @@ strncmp
 strpbrk
 strstr
 strtol
+symlink
 tolower
 unlink
 unsetenv
diff --git a/src/VBox/Additions/x11/vboxvideo/undefined_13 b/src/VBox/Additions/x11/vboxvideo/undefined_13
index 7e352f1..bf8deb0 100644
--- a/src/VBox/Additions/x11/vboxvideo/undefined_13
+++ b/src/VBox/Additions/x11/vboxvideo/undefined_13
@@ -122,6 +122,7 @@ strlen
 strncmp
 strpbrk
 strstr
+symlink
 tolower
 unlink
 unsetenv
diff --git a/src/VBox/Additions/x11/vboxvideo/undefined_70 b/src/VBox/Additions/x11/vboxvideo/undefined_70
index ca989b3..1e3385f 100644
--- a/src/VBox/Additions/x11/vboxvideo/undefined_70
+++ b/src/VBox/Additions/x11/vboxvideo/undefined_70
@@ -131,6 +131,7 @@ strlen
 strncmp
 strpbrk
 strstr
+symlink
 tolower
 unlink
 unsetenv
diff --git a/src/VBox/HostServices/SharedFolders/service.cpp b/src/VBox/HostServices/SharedFolders/service.cpp
index f9d720b..82db231 100644
--- a/src/VBox/HostServices/SharedFolders/service.cpp
+++ b/src/VBox/HostServices/SharedFolders/service.cpp
@@ -779,6 +779,55 @@ static DECLCALLBACK(void) svcCall (void *, VBOXHGCMCALLHANDLE callHandle, uint32
             break;
         }
 
+        /* Read symlink destination */
+        case SHFL_FN_READLINK:
+        {
+            Log(("svcCall: SHFL_FN_READLINK\n"));
+
+            /* Verify parameter count and types. */
+            if (cParms != SHFL_CPARMS_READLINK)
+            {
+                rc = VERR_INVALID_PARAMETER;
+            }
+            else
+            if (   paParms[0].type != VBOX_HGCM_SVC_PARM_32BIT   /* root */
+                || paParms[1].type != VBOX_HGCM_SVC_PARM_PTR     /* path */
+                || paParms[2].type != VBOX_HGCM_SVC_PARM_PTR     /* buffer */
+                    )
+            {
+                rc = VERR_INVALID_PARAMETER;
+            }
+            else
+            {
+                /* Fetch parameters. */
+                SHFLROOT  root     = (SHFLROOT)paParms[0].u.uint32;
+                SHFLSTRING *pPath  = (SHFLSTRING *)paParms[1].u.pointer.addr;
+                uint32_t cbPath    = paParms[1].u.pointer.size;
+                uint8_t   *pBuffer = (uint8_t *)paParms[2].u.pointer.addr;
+                uint32_t  cbBuffer = paParms[2].u.pointer.size;
+
+                /* Verify parameters values. */
+                if (   !ShflStringIsValidOrNull(pPath, paParms[1].u.pointer.size)
+                   )
+                {
+                    rc = VERR_INVALID_PARAMETER;
+                }
+                else
+                {
+                    /* Execute the function. */
+                    rc = vbsfReadlink (pClient, root, pPath, cbPath, pBuffer, cbBuffer);
+
+                    if (RT_SUCCESS(rc))
+                    {
+                        /* Update parameters.*/
+                        ; /* none */
+                    }
+                }
+            }
+
+            break;
+        }
+
         /* Legacy interface */
         case SHFL_FN_MAP_FOLDER_OLD:
         {
@@ -1113,6 +1162,63 @@ static DECLCALLBACK(void) svcCall (void *, VBOXHGCMCALLHANDLE callHandle, uint32
             break;
         }
 
+        case SHFL_FN_SYMLINK:
+        {
+            Log(("svnCall: SHFL_FN_SYMLINK\n"));
+            /* Verify parameter count and types. */
+            if (cParms != SHFL_CPARMS_SYMLINK)
+            {
+                rc = VERR_INVALID_PARAMETER;
+            }
+            else
+            if (   paParms[0].type != VBOX_HGCM_SVC_PARM_32BIT   /* root */
+                || paParms[1].type != VBOX_HGCM_SVC_PARM_PTR     /* newPath */
+                || paParms[2].type != VBOX_HGCM_SVC_PARM_PTR     /* oldPath */
+                || paParms[3].type != VBOX_HGCM_SVC_PARM_PTR     /* info */
+                    )
+            {
+                rc = VERR_INVALID_PARAMETER;
+            }
+            else
+            {
+                /* Fetch parameters. */
+                SHFLROOT     root     = (SHFLROOT)paParms[0].u.uint32;
+                SHFLSTRING  *pNewPath = (SHFLSTRING *)paParms[1].u.pointer.addr;
+                SHFLSTRING  *pOldPath = (SHFLSTRING *)paParms[2].u.pointer.addr;
+                RTFSOBJINFO *pInfo    = (RTFSOBJINFO *)paParms[3].u.pointer.addr;
+                uint32_t     cbInfo   = paParms[3].u.pointer.size;
+
+                /* Verify parameters values. */
+                if (    !ShflStringIsValid(pNewPath, paParms[1].u.pointer.size)
+                    ||  !ShflStringIsValid(pOldPath, paParms[2].u.pointer.size)
+                    ||  (cbInfo != sizeof(RTFSOBJINFO))
+                   )
+                {
+                    rc = VERR_INVALID_PARAMETER;
+                }
+                else
+                {
+                    /* Execute the function. */
+                    rc = vbsfSymlink (pClient, root, pNewPath, pOldPath, pInfo);
+                    if (RT_SUCCESS(rc))
+                    {
+                        /* Update parameters.*/
+                        ; /* none */
+                    }
+                }
+            }
+        }
+        break;
+
+#ifndef RT_OS_WINDOWS
+        case SHFL_FN_SET_SYMLINKS:
+        {
+            pClient->fu32Flags |= SHFL_CF_SYMLINKS;
+            rc = VINF_SUCCESS;
+            break;
+        }
+#endif
+
         default:
         {
             rc = VERR_NOT_IMPLEMENTED;
diff --git a/src/VBox/HostServices/SharedFolders/shfl.h b/src/VBox/HostServices/SharedFolders/shfl.h
index 73d07c8..500c24f 100644
--- a/src/VBox/HostServices/SharedFolders/shfl.h
+++ b/src/VBox/HostServices/SharedFolders/shfl.h
@@ -39,6 +39,9 @@
 /** Client uses UTF8 encoding, if not set then unicode 16 bit (UCS2) is used. */
 #define SHFL_CF_UTF8             (0x00000004)
 
+/** Client both supports and wants to use symlinks. */
+#define SHFL_CF_SYMLINKS         (0x00000008)
+
 /** @} */
 
 typedef struct _SHFLCLIENTDATA
diff --git a/src/VBox/HostServices/SharedFolders/vbsf.cpp b/src/VBox/HostServices/SharedFolders/vbsf.cpp
index d7baa19..9075e37 100644
--- a/src/VBox/HostServices/SharedFolders/vbsf.cpp
+++ b/src/VBox/HostServices/SharedFolders/vbsf.cpp
@@ -36,6 +36,8 @@
 #undef LogFlow
 #define LogFlow Log
 
+#define SHFL_RT_LINK(pClient) ((pClient)->fu32Flags & SHFL_CF_SYMLINKS ? RTPATH_F_ON_LINK : RTPATH_F_FOLLOW_LINK)
+
 /**
  * @todo find a better solution for supporting the execute bit for non-windows
  * guests on windows host. Search for "0111" to find all the relevant places.
@@ -99,7 +101,7 @@ void vbsfStripLastComponent (char *pszFullPath, uint32_t cbFullPathRoot)
     LogFlowFunc(("%s, %s, %s\n", pszFullPath, delimLast, delimSecondLast));
 }
 
-static int vbsfCorrectCasing(char *pszFullPath, char *pszStartComponent)
+static int vbsfCorrectCasing(SHFLCLIENTDATA *pClient, char *pszFullPath, char *pszStartComponent)
 {
     PRTDIRENTRYEX  pDirEntry = NULL;
     uint32_t       cbDirEntry, cbComponent;
@@ -138,7 +140,7 @@ static int vbsfCorrectCasing(char *pszFullPath, char *pszStartComponent)
     {
         size_t cbDirEntrySize = cbDirEntry;
 
-        rc = RTDirReadEx(hSearch, pDirEntry, &cbDirEntrySize, RTFSOBJATTRADD_NOTHING, RTPATH_F_FOLLOW_LINK);
+        rc = RTDirReadEx(hSearch, pDirEntry, &cbDirEntrySize, RTFSOBJATTRADD_NOTHING, SHFL_RT_LINK(pClient));
         if (rc == VERR_NO_MORE_FILES)
             break;
 
@@ -493,7 +495,7 @@ static int vbsfBuildFullPath (SHFLCLIENTDATA *pClient, SHFLROOT root, PSHFLSTRIN
             }
 
             /** @todo don't check when creating files or directories; waste of time */
-            rc = RTPathQueryInfoEx(pszFullPath, &info, RTFSOBJATTRADD_NOTHING, RTPATH_F_FOLLOW_LINK);
+            rc = RTPathQueryInfoEx(pszFullPath, &info, RTFSOBJATTRADD_NOTHING, SHFL_RT_LINK(pClient));
             if (rc == VERR_FILE_NOT_FOUND || rc == VERR_PATH_NOT_FOUND)
             {
                 uint32_t len = (uint32_t)strlen(pszFullPath);
@@ -507,7 +509,7 @@ static int vbsfBuildFullPath (SHFLCLIENTDATA *pClient, SHFLROOT root, PSHFLSTRIN
                     if (*src == RTPATH_DELIMITER)
                     {
                         *src = 0;
-                        rc = RTPathQueryInfoEx(pszFullPath, &info, RTFSOBJATTRADD_NOTHING, RTPATH_F_FOLLOW_LINK);
+                        rc = RTPathQueryInfoEx(pszFullPath, &info, RTFSOBJATTRADD_NOTHING, SHFL_RT_LINK(pClient));
                         *src = RTPATH_DELIMITER;
                         if (rc == VINF_SUCCESS)
                         {
@@ -543,7 +545,7 @@ static int vbsfBuildFullPath (SHFLCLIENTDATA *pClient, SHFLROOT root, PSHFLSTRIN
                         {
                             fEndOfString = false;
                             *end = 0;
-                            rc = RTPathQueryInfoEx(src, &info, RTFSOBJATTRADD_NOTHING, RTPATH_F_FOLLOW_LINK);
+                            rc = RTPathQueryInfoEx(src, &info, RTFSOBJATTRADD_NOTHING, SHFL_RT_LINK(pClient));
                             Assert(rc == VINF_SUCCESS || rc == VERR_FILE_NOT_FOUND || rc == VERR_PATH_NOT_FOUND);
                         }
                         else
@@ -555,7 +557,7 @@ static int vbsfBuildFullPath (SHFLCLIENTDATA *pClient, SHFLROOT root, PSHFLSTRIN
                         if (rc == VERR_FILE_NOT_FOUND || rc == VERR_PATH_NOT_FOUND)
                         {
                             /* path component is invalid; try to correct the casing */
-                            rc = vbsfCorrectCasing(pszFullPath, src);
+                            rc = vbsfCorrectCasing(pClient, pszFullPath, src);
                             if (RT_FAILURE(rc))
                             {
                                 if (!fEndOfString)
@@ -825,6 +827,7 @@ static int vbsfConvertFileOpenFlags(unsigned fShflFlags, RTFMODE fMode, SHFLHAND
  * Open a file or create and open a new one.
  *
  * @returns IPRT status code
+ * @param  pClient               Data structure describing the client accessing the shared folder
  * @param  pszPath               Path to the file or folder on the host.
  * @param  pParms->CreateFlags   Creation or open parameters, see include/VBox/shflsvc.h
  * @param  pParms->Info          When a new file is created this specifies the initial parameters.
@@ -835,7 +838,7 @@ static int vbsfConvertFileOpenFlags(unsigned fShflFlags, RTFMODE fMode, SHFLHAND
  *                               created
  * @retval pParms->Info          On success the parameters of the file opened or created
  */
-static int vbsfOpenFile (const char *pszPath, SHFLCREATEPARMS *pParms)
+static int vbsfOpenFile (SHFLCLIENTDATA *pClient, const char *pszPath, SHFLCREATEPARMS *pParms)
 {
     LogFlow(("vbsfOpenFile: pszPath = %s, pParms = %p\n", pszPath, pParms));
     Log(("SHFL create flags %08x\n", pParms->CreateFlags));
@@ -883,7 +886,7 @@ static int vbsfOpenFile (const char *pszPath, SHFLCREATEPARMS *pParms)
             RTFSOBJINFO info;
 
             /** @todo Possible race left here. */
-            if (RT_SUCCESS(RTPathQueryInfoEx(pszPath, &info, RTFSOBJATTRADD_NOTHING, RTPATH_F_FOLLOW_LINK)))
+            if (RT_SUCCESS(RTPathQueryInfoEx(pszPath, &info, RTFSOBJATTRADD_NOTHING, SHFL_RT_LINK(pClient))))
             {
 #ifdef RT_OS_WINDOWS
                 info.Attr.fMode |= 0111;
@@ -1151,12 +1154,12 @@ static int vbsfCloseFile (SHFLFILEHANDLE *pHandle)
  * @retval  pParms->Result Status of the operation (success or error)
  * @retval  pParms->Info   On success, information returned about the file
  */
-static int vbsfLookupFile(char *pszPath, SHFLCREATEPARMS *pParms)
+static int vbsfLookupFile(SHFLCLIENTDATA *pClient, char *pszPath, SHFLCREATEPARMS *pParms)
 {
     RTFSOBJINFO info;
     int rc;
 
-    rc = RTPathQueryInfoEx(pszPath, &info, RTFSOBJATTRADD_NOTHING, RTPATH_F_FOLLOW_LINK);
+    rc = RTPathQueryInfoEx(pszPath, &info, RTFSOBJATTRADD_NOTHING, SHFL_RT_LINK(pClient));
     LogFlow(("SHFL_CF_LOOKUP\n"));
     /* Client just wants to know if the object exists. */
     switch (rc)
@@ -1241,14 +1244,14 @@ int vbsfCreate (SHFLCLIENTDATA *pClient, SHFLROOT root, SHFLSTRING *pPath, uint3
 
         if (BIT_FLAG(pParms->CreateFlags, SHFL_CF_LOOKUP))
         {
-            rc = vbsfLookupFile(pszFullPath, pParms);
+            rc = vbsfLookupFile(pClient, pszFullPath, pParms);
         }
         else
         {
             /* Query path information. */
             RTFSOBJINFO info;
 
-            rc = RTPathQueryInfoEx(pszFullPath, &info, RTFSOBJATTRADD_NOTHING, RTPATH_F_FOLLOW_LINK);
+            rc = RTPathQueryInfoEx(pszFullPath, &info, RTFSOBJATTRADD_NOTHING, SHFL_RT_LINK(pClient));
             LogFlow(("RTPathQueryInfoEx returned %Rrc\n", rc));
 
             if (RT_SUCCESS(rc))
@@ -1307,7 +1310,7 @@ int vbsfCreate (SHFLCLIENTDATA *pClient, SHFLROOT root, SHFLSTRING *pPath, uint3
                 }
                 else
                 {
-                    rc = vbsfOpenFile (pszFullPath, pParms);
+                    rc = vbsfOpenFile (pClient, pszFullPath, pParms);
                 }
             }
             else
@@ -1527,7 +1530,7 @@ int vbsfDirList(SHFLCLIENTDATA *pClient, SHFLROOT root, SHFLHANDLE Handle, SHFLS
         {
             pDirEntry = pDirEntryOrg;
 
-            rc = RTDirReadEx(DirHandle, pDirEntry, &cbDirEntrySize, RTFSOBJATTRADD_NOTHING, RTPATH_F_FOLLOW_LINK);
+            rc = RTDirReadEx(DirHandle, pDirEntry, &cbDirEntrySize, RTFSOBJATTRADD_NOTHING, SHFL_RT_LINK(pClient));
             if (rc == VERR_NO_MORE_FILES)
             {
                 *pIndex = 0; /* listing completed */
@@ -1650,6 +1653,40 @@ end:
     return rc;
 }
 
+int vbsfReadlink(SHFLCLIENTDATA *pClient, SHFLROOT root, SHFLSTRING *pPath, uint32_t cbPath, uint8_t *pBuffer, uint32_t cbBuffer)
+{
+    int rc = VINF_SUCCESS;
+
+#ifdef RT_OS_WINDOWS
+    rc = VERR_NOT_IMPLEMENTED;
+#else
+    if (pPath == 0 || pBuffer == 0)
+    {
+        AssertFailed();
+        return VERR_INVALID_PARAMETER;
+    }
+
+    /* Build a host full path for the given path, handle file name case issues
+     * (if the guest expects case-insensitive paths but the host is
+     * case-sensitive) and convert ucs2 to utf8 if necessary.
+     */
+    char *pszFullPath = NULL;
+    uint32_t cbFullPathRoot = 0;
+
+    rc = vbsfBuildFullPath (pClient, root, pPath, cbPath, &pszFullPath, &cbFullPathRoot);
+
+    if (RT_SUCCESS (rc))
+    {
+        rc = RTReadlink(pszFullPath, (char *) pBuffer, cbBuffer);
+
+        /* free the path string */
+        vbsfFreeFullPath(pszFullPath);
+    }
+#endif /* not RT_OS_WINDOWS */
+
+    return rc;
+}
+
 int vbsfQueryFileInfo(SHFLCLIENTDATA *pClient, SHFLROOT root, SHFLHANDLE Handle, uint32_t flags, uint32_t *pcbBuffer, uint8_t *pBuffer)
 {
     SHFLFILEHANDLE *pHandle = (SHFLFILEHANDLE *)vbsfQueryHandle(Handle, SHFL_HF_TYPE_DIR|SHFL_HF_TYPE_FILE);
@@ -2106,6 +2143,35 @@ int vbsfRename(SHFLCLIENTDATA *pClient, SHFLROOT root, SHFLSTRING *pSrc, SHFLSTR
     return rc;
 }
 
+int vbsfSymlink(SHFLCLIENTDATA *pClient, SHFLROOT root, SHFLSTRING *pNewPath, SHFLSTRING *pOldPath, RTFSOBJINFO *pInfo)
+{
+    int rc = VINF_SUCCESS;
+
+#ifdef RT_OS_WINDOWS
+    rc = VERR_NOT_IMPLEMENTED;
+#else
+    char *pszFullNewPath = NULL;
+    char *pszOldPath = NULL;
+
+    /* XXX: no support for UCS2 at the moment. */
+    if (!BIT_FLAG(pClient->fu32Flags, SHFL_CF_UTF8))
+        return VERR_NOT_IMPLEMENTED;
+
+    rc = vbsfBuildFullPath (pClient, root, pNewPath, pNewPath->u16Size, &pszFullNewPath, NULL);
+    if (rc != VINF_SUCCESS)
+        return rc;
+
+    rc = RTSymlink(pszFullNewPath, (const char *)pOldPath->String.utf8);
+    if (RT_SUCCESS (rc)) {
+        rc = RTPathQueryInfoEx(pszFullNewPath, pInfo, RTFSOBJATTRADD_NOTHING, SHFL_RT_LINK(pClient));
+    }
+
+    vbsfFreeFullPath(pszFullNewPath);
+#endif /* not RT_OS_WINDOWS */
+
+    return rc;
+}
+
 /*
  * Clean up our mess by freeing all handles that are still valid.
  *
diff --git a/src/VBox/HostServices/SharedFolders/vbsf.h b/src/VBox/HostServices/SharedFolders/vbsf.h
index 10905d6..e02f717 100644
--- a/src/VBox/HostServices/SharedFolders/vbsf.h
+++ b/src/VBox/HostServices/SharedFolders/vbsf.h
@@ -37,5 +37,7 @@ int vbsfSetFSInfo(SHFLCLIENTDATA *pClient, SHFLROOT root, SHFLHANDLE Handle, uin
 int vbsfFlush(SHFLCLIENTDATA *pClient, SHFLROOT root, SHFLHANDLE Handle);
 int vbsfDisconnect(SHFLCLIENTDATA *pClient);
 int vbsfQueryFileInfo(SHFLCLIENTDATA *pClient, SHFLROOT root, SHFLHANDLE Handle, uint32_t flags, uint32_t *pcbBuffer, uint8_t *pBuffer);
+int vbsfReadlink(SHFLCLIENTDATA *pClient, SHFLROOT root, SHFLSTRING *pPath, uint32_t cbPath, uint8_t *pBuffer, uint32_t cbBuffer);
+int vbsfSymlink(SHFLCLIENTDATA *pClient, SHFLROOT root, SHFLSTRING *pNewPath, SHFLSTRING *pOldPath, RTFSOBJINFO *pInfo);
 
 #endif /* __VBSF__H */
diff --git a/src/VBox/Runtime/r0drv/linux/the-linux-kernel.h b/src/VBox/Runtime/r0drv/linux/the-linux-kernel.h
index 779e279..1e42d52 100644
--- a/src/VBox/Runtime/r0drv/linux/the-linux-kernel.h
+++ b/src/VBox/Runtime/r0drv/linux/the-linux-kernel.h
@@ -75,6 +75,7 @@
 #include <linux/kernel.h>
 #include <linux/init.h>
 #include <linux/fs.h>
+#include <linux/namei.h>
 #include <linux/mm.h>
 #include <linux/pagemap.h>
 #include <linux/slab.h>
diff --git a/src/VBox/Runtime/r3/posix/path-posix.cpp b/src/VBox/Runtime/r3/posix/path-posix.cpp
index cb1f1c7..55160e6 100644
--- a/src/VBox/Runtime/r3/posix/path-posix.cpp
+++ b/src/VBox/Runtime/r3/posix/path-posix.cpp
@@ -478,6 +478,39 @@ RTR3DECL(int) RTPathQueryInfoEx(const char *pszPath, PRTFSOBJINFO pObjInfo, RTFS
     return rc;
 }
 
+RTR3DECL(int) RTReadlink(const char *pszPath, char *pBuffer, uint32_t cchBuffer)
+{
+    char szNativeDest[RTPATH_MAX];
+    ssize_t len;
+
+    /*
+     * Validate input.
+     */
+    AssertPtrReturn(pszPath, VERR_INVALID_POINTER);
+    AssertReturn(*pszPath, VERR_INVALID_PARAMETER);
+    AssertPtrReturn(pBuffer, VERR_INVALID_POINTER);
+
+    /*
+     * Convert the filename.
+     */
+    char const *pszNativePath;
+    int rc = rtPathToNative(&pszNativePath, pszPath, NULL);
+    if (RT_SUCCESS(rc))
+    {
+        len = readlink(pszNativePath, szNativeDest, RTPATH_MAX-1);
+        if (len != -1) {
+            szNativeDest[len] = '\0';
+            rc = rtPathFromNativeCopy(pBuffer, cchBuffer, szNativeDest, NULL);
+        } else
+            rc = RTErrConvertFromErrno(errno);
+
+        rtPathFreeNative(pszNativePath, pszPath);
+    }
+
+    LogFlow(("RTReadlink(%p:{%s}, pObjInfo=%p): returns %Rrc\n",
+             pszPath, pszPath, pObjInfo, rc));
+    return rc;
+}
 
 RTR3DECL(int) RTPathSetTimes(const char *pszPath, PCRTTIMESPEC pAccessTime, PCRTTIMESPEC pModificationTime,
                              PCRTTIMESPEC pChangeTime, PCRTTIMESPEC pBirthTime)
@@ -867,6 +900,39 @@ RTR3DECL(int) RTPathRename(const char *pszSrc, const char *pszDst, unsigned fRen
 }
 
 
+RTR3DECL(int) RTSymlink(const char *pszNewPath, const char *pszOldPath)
+{
+    /*
+     * Validate input.
+     */
+    AssertMsgReturn(VALID_PTR(pszNewPath), ("%p\n", pszNewPath), VERR_INVALID_POINTER);
+    AssertMsgReturn(VALID_PTR(pszOldPath), ("%p\n", pszOldPath), VERR_INVALID_POINTER);
+    AssertMsgReturn(*pszNewPath, ("%p\n", pszNewPath), VERR_INVALID_PARAMETER);
+    AssertMsgReturn(*pszOldPath, ("%p\n", pszOldPath), VERR_INVALID_PARAMETER);
+
+    /*
+     * Convert the filenames.
+     */
+    char const *pszNativeNewPath;
+    char const *pszNativeOldPath;
+    int rc = rtPathToNative(&pszNativeNewPath, pszNewPath, NULL);
+    if (RT_SUCCESS(rc))
+    {
+        rc = rtPathToNative(&pszNativeOldPath, pszOldPath, NULL);
+        if (RT_SUCCESS(rc))
+        {
+            if (symlink(pszOldPath, pszNewPath) == -1)
+                rc = RTErrConvertFromErrno(errno);
+
+            rtPathFreeNative(pszNativeOldPath, pszOldPath);
+        }
+        rtPathFreeNative(pszNativeNewPath, pszNewPath);
+    }
+
+    return rc;
+}
+
+
 RTDECL(bool) RTPathExists(const char *pszPath)
 {
     return RTPathExistsEx(pszPath, RTPATH_F_FOLLOW_LINK);
_______________________________________________
vbox-dev mailing list
[email protected]
http://vbox.innotek.de/mailman/listinfo/vbox-dev

Reply via email to