summaryrefslogblamecommitdiffstats
path: root/private/oleutest/cfmex/cdir.cxx
blob: 2504046764b327bed113619934c4a0dc9f6a3227 (plain) (tree)
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418

































































































































































































































































































































































































































                                                                                   

//+==========================================================================
//
//  File:       CDir.cxx
//
//  Purpose:    Define the CDirectory class.
//
//              This class is used to represent a directory name.
//              Along with maintaining the name, it can determine
//              the type of FileSystem.
//
//+==========================================================================


//  --------
//  Includes
//  --------

#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <wchar.h>
#include <wtypes.h>

#include "CFMEx.hxx"
#include "CDir.hxx"


//+------------------------------------------------------------------------------
//
//  Function:   CDirectory::Initialize (no arguments)
//
//  Synopsis:   Generate a directory name, using the TEMP environment
//              variable, and use it to initialize this object.
//
//  Inputs:     None.
//
//  Outputs:    TRUE if the function succeeds, FALSE otherwise.
//
//+------------------------------------------------------------------------------

BOOL CDirectory::Initialize()
{
    //  ---------------
    //  Local Variables
    //  ---------------

    // Assume failure for now.
    BOOL bSuccess = FALSE;

    // The TEMP environment variable.
    WCHAR wszSystemTempPath[ MAX_PATH + sizeof( L'\0' )];

    // Reset the error code.
    m_lError = 0L;

    //  ----------
    //  Get %TEMP% 
    //  ----------

    if( !GetTempPath( MAX_PATH,
                      wszSystemTempPath )
      )
    {
        m_lError = GetLastError();
        EXIT( L"GetTempPath() failed (%d)" );
    }

    //  ----------------------
    //  Initialize this object
    //  ----------------------

    // Initialize using the temporary path.  We must never pass a NULL here,
    // or we'll cause an infinite recursion.

    if( wszSystemTempPath == NULL )
        EXIT( L"Invalid temporary path" );
    bSuccess = Initialize( wszSystemTempPath );

    //  ----
    //  Exit
    //  ----

Exit:

    return( bSuccess );

}

//+-----------------------------------------------------------------------
//
//  Function:   CDirectory::Initialize (with an ANSI string)
//
//  Synopsis:   This function converts the ANSI string to a Unicode
//              string, then initializes the object with it.
//
//  Inputs:     A Unicode string.
//
//  Outputs:    TRUE if the function succeeds, FALSE otherwise.
//
//+-----------------------------------------------------------------------

BOOL CDirectory::Initialize( LPCSTR szDirectory )
{
    //  ---------------
    //  Local Variables
    //  ---------------

    // Assume failure.
    BOOL bSuccess = FALSE;

    // A buffer for the Unicode path
    WCHAR wszDirectory[ MAX_UNICODE_PATH + sizeof( L'\0' )];

    //  -----
    //  Begin
    //  -----

    // Initialize the error code.
    m_lError = 0L;


    // If we were givin a NULL path, use the version of Initialize
    // that requires no path.

    if( szDirectory == NULL )
    {
        bSuccess = Initialize();
        goto Exit;
    }

    // Convert the Ansi name to Unicode.

    if( m_lError = (long) AnsiToUnicode( szDirectory,
                                         wszDirectory,
                                         strlen( szDirectory )
                                       )
      )
    {
        EXIT( L"Unable to convert directory to Unicode" );
    }

    // Initialize using the temporary path.  We must never pass a NULL here,
    // or we'll cause an infinite recursion.

    if( wszDirectory == NULL )
        EXIT( L"Invalid Directory (internal error)" );
    bSuccess = Initialize( wszDirectory );

    //  ----
    //  Exit
    //  ----

Exit:

    return( bSuccess );

}


//+-----------------------------------------------------------------------
//
//  Function:   CDirectory::Initialize (with a Unicode string)
//
//  Synopsis:   This function is the only form of Initialize
//              (there are several variations of the Initialize member)
//              which really initializes the object.  It stores the
//              directory name, and determines the type of filesystem
//              on which it resides.
//
//  Inputs:     A Unicode string.
//
//  Outputs:    TRUE if the function succeeds, FALSE otherwise.
//
//+-----------------------------------------------------------------------

BOOL CDirectory::Initialize( LPCWSTR wszDirectory )
{

    //  ---------------
    //  Local Variables
    //  ---------------

    // Assume failure.
    BOOL bSuccess = FALSE;

    // Buffers for the root of the path and for the volume name.
    WCHAR wszDirectoryRoot[ MAX_PATH + sizeof( L'\0' )];
    WCHAR wszVolumeName[ MAX_PATH + sizeof( L'\0' )];

    // Parameters to GetVolumeInformation which we won't use.
    DWORD   dwMaxComponentLength = 0L;
    DWORD   dwFileSystemFlags = 0L;

    //  -----
    //  Begin
    //  -----

    // Initialize the error code.
    m_lError = 0L;

    // If we were given a NULL path, use the variation of Initialization()
    // which does not require one.  Note that we will then be called again,
    // but this time with a path.

    if( wszDirectory == NULL )
    {
        bSuccess = Initialize();
        goto Exit;
    }

    // Validate the path.

    if( wcslen( wszDirectory ) > MAX_PATH )
    {
        m_lError = wcslen( wszDirectory );
        EXIT( L"Input path is too long (%d)\n" );
    }

    // Save the path to our member buffer

    wcscpy( m_wszDirectory, wszDirectory );


    //  ------------------------
    //  Get the file system name
    //  ------------------------

    // Get the root path to the directory.

    wcscpy( wszDirectoryRoot, wszDirectory );
    MakeRoot( wszDirectoryRoot );

    // Get the volume information, which will include the filesystem name.

    if( !GetVolumeInformation(  wszDirectoryRoot,   // Root path name.
                                wszVolumeName,      // Buffer for volume name
                                MAX_PATH,           // Length of the above buffer
                                NULL,               // Buffer for serial number
                                                    // Longest filename length.
                                &dwMaxComponentLength,
                                &dwFileSystemFlags, // Compression, etc.
                                m_wszFileSystemName,// Buffer for the FS name.
                                MAX_PATH )          // Length of above buffer
      )
    {
        m_lError = GetLastError();
        EXIT( L"GetVolumeInformation() failed" );
    }


    // Determine the file system type from the name.

    if( !wcscmp( m_wszFileSystemName, L"FAT" ))
        m_FileSystemType = fstFAT;

    else if( !wcscmp( m_wszFileSystemName, L"NTFS" ))
        m_FileSystemType = fstNTFS;

    else if( !wcscmp( m_wszFileSystemName, L"OFS" ))
        m_FileSystemType = fstOFS;

    else
        m_FileSystemType = fstUnknown;

    bSuccess = TRUE;

    //  ----
    //  Exit
    //  ----

Exit:

    DisplayErrors( bSuccess, L"CDirectory::Initialize( wszDirectory )" );
    return( bSuccess );

}



//
//  GetRootLength
//
//  This routine was simply copied from private\windows\shell\shelldll\tracker.cxx,
//  and should not be modified here.
//

unsigned
CDirectory::GetRootLength(const WCHAR *pwszPath)
{
    ULONG   cwcRoot = 0;
    m_lError = 0L;

    if (pwszPath == 0)
        pwszPath = L"";

    if (*pwszPath == L'\\')
    {
        //  If the first character is a path separator (backslash), this
        //  must be a UNC drive designator which must be of the form:
        //      <path-separator><path-separator>(<alnum>+)
        //          <path-separator>(<alnum>+)<path-separator>
        //
        //  This covers drives like these: \\worf\scratch\ and
        //  \\savik\win4dev\.
        //
        pwszPath++;
        cwcRoot++;

        BOOL    fMachine = FALSE;
        BOOL    fShare   = FALSE;

        if (*pwszPath == L'\\')
        {
            cwcRoot++;
            pwszPath++;

            while (*pwszPath != '\0' && *pwszPath != L'\\')
            {
                cwcRoot++;
                pwszPath++;

                fMachine = TRUE;
            }

            if (*pwszPath == L'\\')
            {
                cwcRoot++;
                pwszPath++;

                while (*pwszPath != '\0' && *pwszPath != L'\\')
                {
                    cwcRoot++;
                    pwszPath++;

                    fShare = TRUE;
                }

                //  If there weren't any characters in the machine or
                //  share portions of the UNC name, then the drive
                //  designator is bogus.
                //
                if (!fMachine || !fShare)
                {
                    cwcRoot = 0;
                }
            }
            else
            {
                cwcRoot = 0;
            }
        }
        else
        {
            cwcRoot = 0;
        }
    }
    else
    if (iswalpha(*pwszPath))
    {
        //  If the first character is an alphanumeric, we must have
        //  a drive designator of this form:
        //      (<alnum>)+<drive-separator><path-separator>
        //
        //  This covers drives like these: a:\, c:\, etc
        //

        pwszPath++;
        cwcRoot++;

        if (*pwszPath == L':')
        {
            cwcRoot++;
            pwszPath++;
        }
        else
        {
            cwcRoot = 0;
        }
    }

    //  If we have counted one or more characters in the root and these
    //  are followed by a component separator, we need to add the separator
    //  to the root length.  Otherwise this is not a valid root and we need
    //  to return a length of zero.
    //
    if ((cwcRoot > 0) && (*pwszPath == L'\\'))
    {
        cwcRoot++;
    }
    else
    {
        cwcRoot = 0;
    }

    return (cwcRoot);
}

//
//  MakeRoot
//
//  This routine was simply copied from private\windows\shell\shelldll\tracker.cxx,
//  and should not be modified here.
//


VOID
CDirectory::MakeRoot(WCHAR *pwszPath)
{
    unsigned rootlength = GetRootLength(pwszPath);
    m_lError = 0L;

    if (rootlength)
    {
        pwszPath[rootlength] = L'\0';
    }
}