LibreOffice Module tools (master) 1
fileutil.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
9
10#include <tools/fileutil.hxx>
11#if defined _WIN32
12#include <osl/file.hxx>
13#include <rtl/uri.hxx>
15#define WIN32_LEAN_AND_MEAN
16#include <Windows.h>
17#include <davclnt.h>
18#endif
19
20namespace
21{
22#if defined _WIN32
23OUString UNCToDavURL(LPCWSTR sUNC)
24{
25 DWORD nSize = 1024;
26 auto bufURL(std::make_unique<wchar_t[]>(nSize));
27 DWORD nResult = DavGetHTTPFromUNCPath(sUNC, bufURL.get(), &nSize);
28 if (nResult == ERROR_INSUFFICIENT_BUFFER)
29 {
30 bufURL = std::make_unique<wchar_t[]>(nSize);
31 nResult = DavGetHTTPFromUNCPath(sUNC, bufURL.get(), &nSize);
32 }
33 // looks like on different Windowses this may or may not be URL encoded?
34 return nResult == ERROR_SUCCESS
35 ? ::rtl::Uri::encode(OUString(o3tl::toU(bufURL.get())), rtl_UriCharClassUric,
36 rtl_UriEncodeKeepEscapes, RTL_TEXTENCODING_UTF8)
37 : OUString();
38}
39#endif
40}
41
42namespace tools
43{
44bool IsMappedWebDAVPath([[maybe_unused]] const OUString& rURL, [[maybe_unused]] OUString* pRealURL)
45{
46#if defined _WIN32
47 if (rURL.startsWithIgnoreAsciiCase("file:"))
48 {
49 OUString aSystemPath;
50 if (osl::FileBase::getSystemPathFromFileURL(rURL, aSystemPath) == osl::FileBase::E_None)
51 {
52 DWORD nSize = MAX_PATH;
53 auto bufUNC(std::make_unique<char[]>(nSize));
54 DWORD nResult = WNetGetUniversalNameW(o3tl::toW(aSystemPath.getStr()),
55 UNIVERSAL_NAME_INFO_LEVEL, bufUNC.get(), &nSize);
56 if (nResult == ERROR_MORE_DATA)
57 {
58 bufUNC = std::make_unique<char[]>(nSize);
59 nResult = WNetGetUniversalNameW(o3tl::toW(aSystemPath.getStr()),
60 UNIVERSAL_NAME_INFO_LEVEL, bufUNC.get(), &nSize);
61 }
62 if (nResult == NO_ERROR || nResult == ERROR_BAD_DEVICE)
63 {
64 NETRESOURCEW aReq{};
65 if (nResult == ERROR_BAD_DEVICE) // The path could already be an UNC
66 aReq.lpRemoteName = const_cast<LPWSTR>(o3tl::toW(aSystemPath.getStr()));
67 else
68 {
69 auto pInfo = reinterpret_cast<LPUNIVERSAL_NAME_INFOW>(bufUNC.get());
70 aReq.lpRemoteName = pInfo->lpUniversalName;
71 }
72 nSize = 1024;
73 auto bufInfo(std::make_unique<char[]>(nSize));
74 LPWSTR pSystem = nullptr;
75 nResult = WNetGetResourceInformationW(&aReq, bufInfo.get(), &nSize, &pSystem);
76 if (nResult == ERROR_MORE_DATA)
77 {
78 bufInfo = std::make_unique<char[]>(nSize);
79 nResult = WNetGetResourceInformationW(&aReq, bufInfo.get(), &nSize, &pSystem);
80 }
81 if (nResult == NO_ERROR)
82 {
83 LPNETRESOURCEW pInfo = reinterpret_cast<LPNETRESOURCEW>(bufInfo.get());
84 if (wcscmp(pInfo->lpProvider, L"Web Client Network") == 0)
85 {
86 if (pRealURL)
87 *pRealURL = UNCToDavURL(aReq.lpRemoteName);
88 return true;
89 }
90 }
91 }
92 }
93 }
94#endif
95 return false;
96}
97
98} // namespace tools
99
100/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
#define MAX_PATH
Note: this class is a true marvel of engineering: because the author could not decide whether it's be...
bool IsMappedWebDAVPath(const OUString &rURL, OUString *pRealURL)
Definition: fileutil.cxx:44
NO_ERROR