summaryrefslogtreecommitdiff
path: root/src/vm/crossgenroresolvenamespace.cpp
blob: 3ddd8f72a396998351261510af9da5fb2f219baf (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

//+----------------------------------------------------------------------------  
//  

//  
//  Adapted from Windows sources.  Modified to run on Windows version < Win8, so
//  that we can use this in CrossGen.
//  

//
//-----------------------------------------------------------------------------    

#include "common.h"
#include "crossgenroresolvenamespace.h"
#include "stringarraylist.h"

namespace Crossgen
{
    
#define WINDOWS_NAMESPACE W("Windows")  
#define WINDOWS_NAMESPACE_PREFIX WINDOWS_NAMESPACE W(".")  
#define WINMD_FILE_EXTENSION_L       W(".winmd")

StringArrayList* g_wszWindowsNamespaceDirectories;
StringArrayList* g_wszUserNamespaceDirectories;

BOOL 
IsWindowsNamespace(const WCHAR * wszNamespace)
{
    LIMITED_METHOD_CONTRACT;
    
    if (wcsncmp(wszNamespace, WINDOWS_NAMESPACE_PREFIX, (_countof(WINDOWS_NAMESPACE_PREFIX) - 1)) == 0)  
    {  
        return TRUE;  
    }  
    else if (wcscmp(wszNamespace, WINDOWS_NAMESPACE) == 0)  
    {  
        return TRUE;
    }  

    return FALSE;
}


BOOL 
DoesFileExist(
    const WCHAR * wszFileName)
{
    LIMITED_METHOD_CONTRACT;
    
    BOOL  fFileExists = TRUE;
    DWORD dwFileAttributes;
    dwFileAttributes = GetFileAttributesW(wszFileName);
    
    if ((dwFileAttributes == INVALID_FILE_ATTRIBUTES) || 
        (dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
    {
        fFileExists = FALSE;
    }
    
    return fFileExists;
}


HRESULT 
FindNamespaceFileInDirectory(
    const WCHAR * wszNamespace, 
    const WCHAR * wszDirectory, 
    DWORD *       pcMetadataFiles, 
    SString **    ppMetadataFiles)
{
    LIMITED_METHOD_CONTRACT;

    if (wszDirectory == nullptr)
        return ERROR_NOT_SUPPORTED;

    WCHAR wszFilePath[MAX_LONGPATH + 1];
    wcscpy_s(
        wszFilePath, 
        _countof(wszFilePath), 
        wszDirectory);

    WCHAR * wszFirstFileNameChar = wszFilePath + wcslen(wszFilePath);

    // If there's no backslash, add one.
    if (*(wszFirstFileNameChar - 1) != '\\')
        *wszFirstFileNameChar++ = '\\';

    WCHAR wszRemainingNamespace[MAX_PATH_FNAME +1];
    wcscpy_s(
        wszRemainingNamespace, 
        _countof(wszRemainingNamespace), 
        wszNamespace);
    
    do
    {
        *wszFirstFileNameChar = W('\0');
        wcscat_s(
            wszFilePath, 
            _countof(wszFilePath), 
            wszRemainingNamespace);
        wcscat_s(
            wszFilePath, 
            _countof(wszFilePath), 
            WINMD_FILE_EXTENSION_L);

        if (DoesFileExist(wszFilePath))
        {
            *ppMetadataFiles = new SString(wszFilePath);
            *pcMetadataFiles = 1;
            return S_OK;
        }

        WCHAR * wszLastDotChar = wcsrchr(wszRemainingNamespace, W('.'));
        if (wszLastDotChar == nullptr)
        {
            *ppMetadataFiles = nullptr;
            *pcMetadataFiles = 0;
            return S_FALSE;
        }
        *wszLastDotChar = W('\0');
    } while (true);
}


__checkReturn 
HRESULT WINAPI CrossgenRoResolveNamespace(
    const LPCWSTR   wszNamespace, 
    DWORD *         pcMetadataFiles, 
    SString **      ppMetadataFiles)
{
    LIMITED_METHOD_CONTRACT;
    HRESULT hr = S_OK;
    
    if (IsWindowsNamespace(wszNamespace))
    {
        DWORD cAppPaths = g_wszWindowsNamespaceDirectories->GetCount();
        
        for (DWORD i = 0; i < cAppPaths; i++)
        {
            // Returns S_FALSE on file not found so we continue proving app directory graph
            IfFailRet(FindNamespaceFileInDirectory(
                wszNamespace, 
                g_wszWindowsNamespaceDirectories->Get(i).GetUnicode(),
                pcMetadataFiles, 
                ppMetadataFiles));

            if (hr == S_OK)
            {
                return hr;
            }
        }
    }
    else 
    {
        DWORD cAppPaths = g_wszUserNamespaceDirectories->GetCount();
        
        for (DWORD i = 0; i < cAppPaths; i++)
        {
            // Returns S_FALSE on file not found so we continue proving app directory graph
            IfFailRet(FindNamespaceFileInDirectory(
                wszNamespace, 
                g_wszUserNamespaceDirectories->Get(i).GetUnicode(),
                pcMetadataFiles, 
                ppMetadataFiles));

            if (hr == S_OK)
            {
                return hr;
            }
        }
    }
    
    return hr;
} // RoResolveNamespace

void SetFirstPartyWinMDPaths(StringArrayList* saAppPaths)
{
    LIMITED_METHOD_CONTRACT;

    g_wszWindowsNamespaceDirectories = saAppPaths;
}

void SetAppPaths(StringArrayList* saAppPaths)
{
    LIMITED_METHOD_CONTRACT;
    
    g_wszUserNamespaceDirectories = saAppPaths;
}

}// Namespace Crossgen