summaryrefslogtreecommitdiff
path: root/tools/source/fsys/fileutil.cxx
blob: c4937be924732cacc71805388ea20600be59b157 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#include <tools/fileutil.hxx>
#include <tools/urlobj.hxx>
#if defined _WIN32
#include <osl/file.hxx>
#include <string.h>
#include <o3tl/make_unique.hxx>
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#endif

namespace tools
{
bool IsMappedWebDAVPath(const INetURLObject& aURL)
{
#if defined _WIN32
    if (aURL.GetProtocol() == INetProtocol::File)
    {
        OUString sURL = aURL.GetMainURL(INetURLObject::DecodeMechanism::NO_DECODE);
        OUString aSystemPath;
        if (osl::FileBase::getSystemPathFromFileURL(sURL, aSystemPath) == osl::FileBase::E_None)
        {
            DWORD nSize = MAX_PATH;
            std::unique_ptr<char[]> bufUNC(new char[nSize]);
            DWORD nResult = WNetGetUniversalNameW(static_cast<LPCWSTR>(aSystemPath.getStr()),
                                                  UNIVERSAL_NAME_INFO_LEVEL, bufUNC.get(), &nSize);
            if (nResult == ERROR_MORE_DATA)
            {
                bufUNC.reset(new char[nSize]);
                nResult = WNetGetUniversalNameW(static_cast<LPCWSTR>(aSystemPath.getStr()),
                                                UNIVERSAL_NAME_INFO_LEVEL, bufUNC.get(), &nSize);
            }
            if (nResult == NO_ERROR || nResult == ERROR_BAD_DEVICE)
            {
                NETRESOURCEW aReq{};
                if (nResult == ERROR_BAD_DEVICE) // The path could already be an UNC
                    aReq.lpRemoteName = const_cast<LPWSTR>(static_cast<LPCWSTR>(aSystemPath.getStr()));
                else
                {
                    auto pInfo = reinterpret_cast<LPUNIVERSAL_NAME_INFOW>(bufUNC.get());
                    aReq.lpRemoteName = pInfo->lpUniversalName;
                }
                nSize = 1024;
                std::unique_ptr<char[]> bufInfo(new char[nSize]);
                LPWSTR pSystem = nullptr;
                nResult = WNetGetResourceInformationW(&aReq, bufInfo.get(), &nSize, &pSystem);
                if (nResult == ERROR_MORE_DATA)
                {
                    bufInfo.reset(new char[nSize]);
                    nResult = WNetGetResourceInformationW(&aReq, bufInfo.get(), &nSize, &pSystem);
                }
                if (nResult == NO_ERROR)
                {
                    LPNETRESOURCEW pInfo = reinterpret_cast<LPNETRESOURCEW>(bufInfo.get());
                    if (wcscmp(pInfo->lpProvider, L"Web Client Network") == 0)
                        return true;
                }
            }
        }
    }
#else
    (void)aURL;
#endif
    return false;
}

} // namespace tools

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */