symlink-win.cpp revision 258d89b5c228e2b8c4d621991979c9df5783012f
c9579522f347fc3fda6b3f4add0b355981cd7758vboxsync * IPRT - Symbolic Links, Windows.
c9579522f347fc3fda6b3f4add0b355981cd7758vboxsync * Copyright (C) 2010 Oracle Corporation
c9579522f347fc3fda6b3f4add0b355981cd7758vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
c9579522f347fc3fda6b3f4add0b355981cd7758vboxsync * available from http://www.virtualbox.org. This file is free software;
c9579522f347fc3fda6b3f4add0b355981cd7758vboxsync * you can redistribute it and/or modify it under the terms of the GNU
c9579522f347fc3fda6b3f4add0b355981cd7758vboxsync * General Public License (GPL) as published by the Free Software
c9579522f347fc3fda6b3f4add0b355981cd7758vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
c9579522f347fc3fda6b3f4add0b355981cd7758vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
c9579522f347fc3fda6b3f4add0b355981cd7758vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
c9579522f347fc3fda6b3f4add0b355981cd7758vboxsync * The contents of this file may alternatively be used under the terms
c9579522f347fc3fda6b3f4add0b355981cd7758vboxsync * of the Common Development and Distribution License Version 1.0
c9579522f347fc3fda6b3f4add0b355981cd7758vboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
c9579522f347fc3fda6b3f4add0b355981cd7758vboxsync * VirtualBox OSE distribution, in which case the provisions of the
c9579522f347fc3fda6b3f4add0b355981cd7758vboxsync * CDDL are applicable instead of those of the GPL.
c9579522f347fc3fda6b3f4add0b355981cd7758vboxsync * You may elect to license modified versions of this file under the
c9579522f347fc3fda6b3f4add0b355981cd7758vboxsync * terms and conditions of either the GPL or the CDDL or both.
c9579522f347fc3fda6b3f4add0b355981cd7758vboxsync/*******************************************************************************
c9579522f347fc3fda6b3f4add0b355981cd7758vboxsync* Header Files *
c9579522f347fc3fda6b3f4add0b355981cd7758vboxsync*******************************************************************************/
c9579522f347fc3fda6b3f4add0b355981cd7758vboxsyncRTDECL(bool) RTSymlinkExists(const char *pszSymlink)
c9579522f347fc3fda6b3f4add0b355981cd7758vboxsync bool fRc = false;
c9579522f347fc3fda6b3f4add0b355981cd7758vboxsync int rc = RTPathQueryInfoEx(pszSymlink, &ObjInfo, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK);
c9579522f347fc3fda6b3f4add0b355981cd7758vboxsync LogFlow(("RTSymlinkExists(%p={%s}): returns %RTbool\n", pszSymlink, pszSymlink, fRc));
c9579522f347fc3fda6b3f4add0b355981cd7758vboxsyncRTDECL(bool) RTSymlinkIsDangling(const char *pszSymlink)
c9579522f347fc3fda6b3f4add0b355981cd7758vboxsync bool fRc = false;
c9579522f347fc3fda6b3f4add0b355981cd7758vboxsync int rc = RTPathQueryInfoEx(pszSymlink, &ObjInfo, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK);
c9579522f347fc3fda6b3f4add0b355981cd7758vboxsync rc = RTPathQueryInfoEx(pszSymlink, &ObjInfo, RTFSOBJATTRADD_NOTHING, RTPATH_F_FOLLOW_LINK);
c9579522f347fc3fda6b3f4add0b355981cd7758vboxsync LogFlow(("RTSymlinkIsDangling(%p={%s}): returns %RTbool\n", pszSymlink, pszSymlink, fRc));
c9579522f347fc3fda6b3f4add0b355981cd7758vboxsyncRTDECL(int) RTSymlinkCreate(const char *pszSymlink, const char *pszTarget, RTSYMLINKTYPE enmType)
c9579522f347fc3fda6b3f4add0b355981cd7758vboxsync * Validate the input.
c9579522f347fc3fda6b3f4add0b355981cd7758vboxsync AssertReturn(enmType > RTSYMLINKTYPE_INVALID && enmType < RTSYMLINKTYPE_END, VERR_INVALID_PARAMETER);
c9579522f347fc3fda6b3f4add0b355981cd7758vboxsync * Resolve the API.
c9579522f347fc3fda6b3f4add0b355981cd7758vboxsync typedef BOOLEAN (WINAPI *PFNCREATESYMBOLICLINKW)(LPCWSTR, LPCWSTR, DWORD);
c9579522f347fc3fda6b3f4add0b355981cd7758vboxsync static PFNCREATESYMBOLICLINKW s_pfnCreateSymbolicLinkW = NULL;
c9579522f347fc3fda6b3f4add0b355981cd7758vboxsync PFNCREATESYMBOLICLINKW pfn = (PFNCREATESYMBOLICLINKW)GetProcAddress(hmod, "CreateSymbolicLinkW");
c9579522f347fc3fda6b3f4add0b355981cd7758vboxsync LogFlow(("RTSymlinkCreate(%p={%s}, %p={%s}, %d): returns VERR_NOT_SUPPORTED - Windows API not found\n",
c9579522f347fc3fda6b3f4add0b355981cd7758vboxsync pszSymlink, pszSymlink, pszTarget, pszTarget, enmType));
c9579522f347fc3fda6b3f4add0b355981cd7758vboxsync * Convert the paths.
c9579522f347fc3fda6b3f4add0b355981cd7758vboxsync int rc = RTStrToUtf16(pszSymlink, &pwszNativeSymlink);
c9579522f347fc3fda6b3f4add0b355981cd7758vboxsync * Massage the target path, determin the link type.
c9579522f347fc3fda6b3f4add0b355981cd7758vboxsync size_t cchVolSpecTarget = rtPathVolumeSpecLen(pszTarget);
c9579522f347fc3fda6b3f4add0b355981cd7758vboxsync size_t cwcNativeTarget = RTUtf16Len(pwszNativeTarget);
c9579522f347fc3fda6b3f4add0b355981cd7758vboxsync && RTPATH_IS_SLASH(pszTarget[cchTarget - offFromEnd]))
c9579522f347fc3fda6b3f4add0b355981cd7758vboxsync pwszNativeTarget[cwcNativeTarget - offFromEnd] = 0;
c9579522f347fc3fda6b3f4add0b355981cd7758vboxsync DWORD dwAttr = GetFileAttributesW(pwszNativeTarget);
c9579522f347fc3fda6b3f4add0b355981cd7758vboxsync /** @todo Join the symlink directory with the target and
c9579522f347fc3fda6b3f4add0b355981cd7758vboxsync * look up the attributes on that. -lazy bird. */
c9579522f347fc3fda6b3f4add0b355981cd7758vboxsync * Create the link.
c9579522f347fc3fda6b3f4add0b355981cd7758vboxsync if (s_pfnCreateSymbolicLinkW(pwszNativeSymlink, pwszNativeTarget, enmType == RTSYMLINKTYPE_DIR))
c9579522f347fc3fda6b3f4add0b355981cd7758vboxsync LogFlow(("RTSymlinkCreate(%p={%s}, %p={%s}, %d): returns %Rrc\n", pszSymlink, pszSymlink, pszTarget, pszTarget, enmType, rc));
c9579522f347fc3fda6b3f4add0b355981cd7758vboxsync * Convert the path.
c9579522f347fc3fda6b3f4add0b355981cd7758vboxsync int rc = RTStrToUtf16(pszSymlink, &pwszNativeSymlink);
c9579522f347fc3fda6b3f4add0b355981cd7758vboxsync * We have to use different APIs depending on whether this is a
c9579522f347fc3fda6b3f4add0b355981cd7758vboxsync * directory or file link. This means we're subject to one more race
c9579522f347fc3fda6b3f4add0b355981cd7758vboxsync * than on posix at the moment. We could probably avoid this though,
c9579522f347fc3fda6b3f4add0b355981cd7758vboxsync * if we wanted to go talk with the native API layer below Win32...
c9579522f347fc3fda6b3f4add0b355981cd7758vboxsync DWORD dwAttr = GetFileAttributesW(pwszNativeSymlink);
c9579522f347fc3fda6b3f4add0b355981cd7758vboxsync LogFlow(("RTSymlinkDelete(%p={%s}): returns %Rrc\n", pszSymlink, pszSymlink, rc));
c9579522f347fc3fda6b3f4add0b355981cd7758vboxsyncRTDECL(int) RTSymlinkRead(const char *pszSymlink, char *pszTarget, size_t cbTarget)
c9579522f347fc3fda6b3f4add0b355981cd7758vboxsync LogFlow(("RTSymlinkRead(%p={%s}): returns %Rrc\n", pszSymlink, pszSymlink, rc));
c9579522f347fc3fda6b3f4add0b355981cd7758vboxsyncRTDECL(int) RTSymlinkReadA(const char *pszSymlink, char **ppszTarget)
c9579522f347fc3fda6b3f4add0b355981cd7758vboxsync int rc = RTStrToUtf16(pszSymlink, &pwszNativeSymlink);
c9579522f347fc3fda6b3f4add0b355981cd7758vboxsync FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
c9579522f347fc3fda6b3f4add0b355981cd7758vboxsync /** @todo what do we do next? FSCTL_GET_REPARSE_POINT? */
c9579522f347fc3fda6b3f4add0b355981cd7758vboxsync LogFlow(("RTSymlinkReadA(%p={%s},%p): returns %Rrc *ppszTarget=%p:{%s}\n", pszSymlink, pszSymlink, ppszTarget, rc, *ppszTarget, *ppszTarget));
c9579522f347fc3fda6b3f4add0b355981cd7758vboxsync LogFlow(("RTSymlinkReadA(%p={%s},%p): returns %Rrc\n", pszSymlink, pszSymlink, ppszTarget, rc));