Welcome to mirror list, hosted at ThFree Co, Russian Federation.

mountpoints.cpp « windirstat - github.com/windirstat/windirstat.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: deebc651b0038c90a44a3814ecb784a9aceb67ea (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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
// mountpoints.cpp - Implementation of CMountPoints
//
// WinDirStat - Directory Statistics
// Copyright (C) 2003-2005 Bernhard Seifert
// Copyright (C) 2004-2017 WinDirStat Team (windirstat.net)
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//

#include "stdafx.h"
#include "osspecific.h"
#include "mountpoints.h"
#include "globalhelpers.h"
#include <common/tracer.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

CReparsePoints::~CReparsePoints()
{
    Clear();
}

void CReparsePoints::Clear()
{
    m_drive.RemoveAll();

    POSITION pos = m_volume.GetStartPosition();
    while(pos != NULL)
    {
        CString volume;
        PointVolumeArray *pva = NULL;
        m_volume.GetNextAssoc(pos, volume, pva);
        ASSERT_VALID(pva);
        delete pva;
    }
    m_volume.RemoveAll();
}

void CReparsePoints::Initialize()
{
    Clear();

    GetDriveVolumes();
    GetAllMountPoints();
}

void CReparsePoints::GetDriveVolumes()
{
    m_drive.SetSize(wds::iNumDriveLetters);

    DWORD drives = ::GetLogicalDrives();
    int i;
    DWORD mask = 0x00000001;
    for(i = 0; i < wds::iNumDriveLetters; i++, mask <<= 1)
    {
        TCHAR volume[_MAX_PATH];
        volume[0] = 0;

        if((drives & mask) != 0)
        {
            CString s;
            s.Format(_T("%c:\\"), i + wds::chrCapA);

            BOOL b = ::GetVolumeNameForVolumeMountPoint(s, volume, _countof(volume));

            if(!b)
            {
#               ifdef _DEBUG
                if(ERROR_NOT_READY == ::GetLastError())
                    VTRACE(_T("GetVolumeNameForVolumeMountPoint(%s): not ready (%d)."), volume, ::GetLastError());
                else
                    VTRACE(_T("GetVolumeNameForVolumeMountPoint(%s): unexpected error (%d)."), volume, ::GetLastError());
#               endif // _DEBUG
                volume[0] = 0;
            }
        }

        m_drive[i]= volume;
    }
}

void CReparsePoints::GetAllMountPoints()
{
    TCHAR volume[_MAX_PATH];
    HANDLE hvol = ::FindFirstVolume(volume, _countof(volume));
    if(hvol == INVALID_HANDLE_VALUE)
    {
        VTRACE(_T("No volumes found."));
        return;
    }

    for(BOOL bContinue = true; bContinue; bContinue = ::FindNextVolume(hvol, volume, _countof(volume)))
    {
        TCHAR fsname[_MAX_PATH], vname[_MAX_PATH];
        PointVolumeArray *pva = new PointVolumeArray;
        ASSERT_VALID(pva);

        DWORD fsflags;
        BOOL b = ::GetVolumeInformation(volume, vname, _countof(vname), NULL, NULL, &fsflags, fsname, _countof(fsname));

        if(!b)
        {
#           ifdef _DEBUG
            if(ERROR_NOT_READY == ::GetLastError())
                VTRACE(_T("%s (%s) is not ready (%d)."), vname, volume, ::GetLastError());
            else
                VTRACE(_T("Unexpected error on %s (%s, %d)."), vname, volume, ::GetLastError());
#           endif // _DEBUG
            m_volume.SetAt(volume, pva);
            continue;
        }

        if((fsflags & FILE_SUPPORTS_REPARSE_POINTS) == 0)
        {
            // No support for reparse points, and therefore for volume
            // mount points, which are implemented using reparse points.
            VTRACE(_T("%s, %s, does not support reparse points (%d)."), volume, fsname, ::GetLastError());
            m_volume.SetAt(volume, pva);
            continue;
        }

        TCHAR point[_MAX_PATH];
        HANDLE h = ::FindFirstVolumeMountPoint(volume, point, _countof(point));
        if(h == INVALID_HANDLE_VALUE)
        {
#           ifdef _DEBUG
            if(ERROR_ACCESS_DENIED == ::GetLastError())
            {
                if(IsAdmin())
                    VTRACE(_T("Access denied for admin to %s (%d)."), volume, ::GetLastError());
                else
                    VTRACE(_T("Access denied to %s (%d)."), volume, ::GetLastError());
            }
            else if(ERROR_NO_MORE_FILES != ::GetLastError())
            {
                VTRACE(_T("Unexpected error for %s (%d)."), volume, ::GetLastError());
            }
#           endif // _DEBUG
            m_volume.SetAt(volume, pva);
            continue;
        }

        for(BOOL bCont = TRUE; bCont; bCont = ::FindNextVolumeMountPoint(h, point, _countof(point)))
        {
            CString uniquePath = volume;
            uniquePath += point;
            TCHAR mountedVolume[_MAX_PATH];

            const BOOL bGotMountPoints = ::GetVolumeNameForVolumeMountPoint(uniquePath, mountedVolume, _countof(mountedVolume));

            if(!bGotMountPoints)
            {
                VTRACE(_T("GetVolumeNameForVolumeMountPoint(%s) failed (%d)."), uniquePath.GetBuffer(), ::GetLastError());
                continue;
            }

            SPointVolume pv;
            pv.point = point;
            pv.volume = mountedVolume;
            pv.flags = fsflags;
            VTRACE(_T("%s (%s) -> %08X"), point, mountedVolume, fsflags);

            pv.point.MakeLower();

            pva->Add(pv);
        }
        ::FindVolumeMountPointClose(h);

        m_volume.SetAt(volume, pva);
    }

    (void)::FindVolumeClose(hvol);

#ifdef _DEBUG
    POSITION pos = m_volume.GetStartPosition();
    while(pos != NULL)
    {
        CString lvolume;
        PointVolumeArray *pva = NULL;
        m_volume.GetNextAssoc(pos, lvolume, pva);
        pva->AssertValid();
    }
#endif
}


bool CReparsePoints::IsVolumeMountPoint(CString path)
{
    if(path.GetLength() < 3 || path[1] != wds::chrColon || path[2] != wds::chrBackslash)
    {
        // Don't know how to make out mount points on UNC paths ###
        return false;
    }

    ASSERT(path.GetLength() >= 3);
    ASSERT(path[1] == wds::chrColon);
    ASSERT(path[2] == wds::chrBackslash);

    if(path.Right(1) != wds::chrBackslash)
    {
        path += _T("\\");
    }

    path.MakeLower();

    CString volume = m_drive[path[0] - wds::chrSmallA];
    path = path.Mid(3);

    return IsVolumeMountPoint(volume, path);
}

// Check whether the current item is a junction point but no volume mount point
// as the latter ones are treated differently (see above).
bool CReparsePoints::IsFolderJunction(DWORD attr)
{
    if (attr == INVALID_FILE_ATTRIBUTES)
    {
        return false;
    }

    return ((attr & FILE_ATTRIBUTE_REPARSE_POINT) != 0);
}

// ... same as before, but based on the full path
bool CReparsePoints::IsFolderJunction(CString path)
{
    if(IsVolumeMountPoint(path))
    {
        return false;
    }

    return IsFolderJunction(::GetFileAttributes(path));
}

bool CReparsePoints::IsVolumeMountPoint(CString volume, CString path)
{
    while(true)
    {
        int i = 0;
        PointVolumeArray *pva;
        if(!m_volume.Lookup(volume, pva))
        {
            VTRACE(_T("CMountPoints: Volume(%s) unknown!"), volume.GetString());
            return false;
        }

        CString point;
        for(i  =  0; i < pva->GetSize(); i++)
        {
            point = (*pva)[i].point;
            if(path.Left(point.GetLength()) == point)
            {
                break;
            }
        }
        if(i >= pva->GetSize())
        {
            return false;
        }

        if(path.GetLength() == point.GetLength())
        {
            return true;
        }

        volume = (*pva)[i].volume;
        path = path.Mid(point.GetLength());
    }
}