summaryrefslogtreecommitdiff
path: root/src/pal/src/cruntime/path.cpp
blob: e5b955ebd9e3eee50252071a570f9b6c8f620850 (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
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
// 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.

/*++



Module Name:

    path.c

Abstract:

    Implementation of path functions part of Windows runtime library.

Revision History:



--*/

#include "pal/palinternal.h"
#include "pal/dbgmsg.h"
#include "pal/file.h"
#include "pal/printfcpp.hpp"

#include <string.h>
#include <stdlib.h>
#include <sys/param.h>
#include <errno.h>
#include <limits.h>

SET_DEFAULT_DEBUG_CHANNEL(CRT);


/* ON_ERROR. A Helper macro for _?splitpath functions. */
#define ON_ERROR if ( drive ) \
        {\
            drive[0] = 0;\
        }\
        if(dir)\
        {\
            dir[0] = 0;\
        }\
        if(fname)\
        {\
            fname[0] = 0;\
        }\
        if(ext)\
        {\
            ext[0] = 0;\
        }\
        goto done;\

/*++
Function:
  _wsplitpath

See MSDN doc.

Notes :
    This implementation ignores drive letters as they should not be
    present. If the drive argument is non-NULL, it always returns an empty
    string.
    File names in which the only period is at the beginning (like .bashrc, but
    not .bashrc.bak), the file is treated as having no extension
    (fname is ".bashrc", ext is "")

--*/
void
__cdecl
_wsplitpath(
            const wchar_16 *dospath,
            wchar_16 *drive,
            wchar_16 *dir,
            wchar_16 *fname,
            wchar_16 *ext)
{
    WCHAR path[_MAX_PATH+1];
    LPCWSTR slash_ptr = NULL;
    LPCWSTR period_ptr = NULL;
    INT size = 0;

    PERF_ENTRY(_wsplitpath);
    ENTRY("_wsplitpath (path=%p (%S), drive=%p, dir=%p, fname=%p, ext=%p)\n",
          dospath?dospath:W16_NULLSTRING,
          dospath?dospath:W16_NULLSTRING, drive, dir, fname, ext);

    /* Do performance intensive error checking only in debug builds.
    
    NOTE: This function must fail predictably across all platforms.
    Under Windows this function throw an access violation if NULL
    was passed in as the value for path.

    */
#if _DEBUG
    if ( !dospath )
    {
        ERROR( "path cannot be NULL!\n" );
    }
#endif
    
    if( lstrlenW( dospath ) >= _MAX_PATH )
    {
        ERROR("Path length is > _MAX_PATH (%d)!\n", _MAX_PATH);
        ON_ERROR;
    }


    PAL_wcscpy(path, dospath);
    FILEDosToUnixPathW(path);

    /* no drive letters in the PAL */
    if( drive != NULL )
    {
        drive[0] = 0;
    }

    /* find last path separator char */
    slash_ptr = PAL_wcsrchr(path, '/');

    if( slash_ptr == NULL )
    {
        TRACE("No path separator in path\n");
        slash_ptr = path - 1;
    }
    /* find extension separator, if any */
    period_ptr = PAL_wcsrchr(path, '.');

    /* make sure we only consider periods after the last path separator */
    if( period_ptr < slash_ptr )
    {
        period_ptr = NULL;
    }

    /* if the only period in the file is a leading period (denoting a hidden
       file), don't treat what follows as an extension */
    if( period_ptr == slash_ptr+1 )
    {
        period_ptr = NULL;
    }

    if( period_ptr == NULL )
    {
        TRACE("No extension in path\n");
        period_ptr = path + lstrlenW(path);
    }

    size = slash_ptr - path + 1;
    if( dir != NULL )
    {
        INT i;

        if( (size + 1 ) > _MAX_DIR )
        {
            ERROR("Directory component needs %d characters, _MAX_DIR is %d\n",
                  size+1, _MAX_DIR);
            ON_ERROR;
        }
        
        memcpy(dir, path, size*sizeof(WCHAR));
        dir[size] = 0;

        /* only allow / separators in returned path */
        i = 0;
        while( dir[ i ] )
        {
            if( dir[ i ] == '\\' )
            {
                dir[i]='/';
            }
            i++;
        }
    }

    size = period_ptr-slash_ptr-1;
    if( fname != NULL )
    {
        if( (size+1) > _MAX_FNAME )
        {
            ERROR("Filename component needs %d characters, _MAX_FNAME is %d\n",
                 size+1, _MAX_FNAME);
            ON_ERROR;
        }
        memcpy(fname, slash_ptr+1, size*sizeof(WCHAR));
        fname[size] = 0;
    }

    size = 1 + lstrlenW( period_ptr );
    if( ext != NULL )
    {
        if( size > _MAX_EXT )
        {
            ERROR("Extension component needs %d characters, _MAX_EXT is %d\n",
                 size, _MAX_EXT);
            ON_ERROR;
        }
        memcpy(ext, period_ptr, size*sizeof(WCHAR));
        ext[size-1] = 0;
    }
    
    TRACE("Path components are '%S' '%S' '%S'\n", dir, fname, ext);

done:
    
    LOGEXIT("_wsplitpath returns.\n");
    PERF_EXIT(_wsplitpath);
}


/*++
Function:
  _splitpath

See description above for _wsplitpath.

--*/
void
__cdecl
_splitpath(
           const char *path,
           char *drive,
           char *dir,
           char *fname,
           char *ext)
{
    WCHAR w_path[_MAX_PATH];
    WCHAR w_dir[_MAX_DIR];
    WCHAR w_fname[_MAX_FNAME];
    WCHAR w_ext[_MAX_EXT];

    PERF_ENTRY(_splitpath);
    ENTRY("_splitpath (path=%p (%s), drive=%p, dir=%p, fname=%p, ext=%p)\n",
          path?path:"NULL",
          path?path:"NULL", drive, dir, fname, ext);

   /* Do performance intensive error checking only in debug builds.
    
    NOTE: This function must fail predictably across all platforms.
    Under Windows this function throw an access violation if NULL
    was passed in as the value for path.
    
    */
#if _DEBUG
    if ( !path )
    {
        ERROR( "path cannot be NULL!\n" );
    }
    
    if( strlen( path ) >= _MAX_PATH )
    {
        ERROR( "Path length is > _MAX_PATH (%d)!\n", _MAX_PATH);
    }
#endif    

    /* no drive letters in the PAL */
    if(drive)
    {
        drive[0] = '\0';
    }

    if(0 == MultiByteToWideChar(CP_ACP, 0, path, -1, w_path, _MAX_PATH))
    {
        ASSERT("MultiByteToWideChar failed!\n");
        ON_ERROR;
    }

    /* Call up to Unicode version; pass NULL for parameters the caller doesn't
       care about */
    _wsplitpath(w_path, NULL, dir?w_dir:NULL, 
	                fname?w_fname:NULL, ext?w_ext:NULL);

    /* Convert result back to MultiByte; report conversion errors but don't
       stop because of them */

    if(dir)
    {
        if(0 == WideCharToMultiByte(CP_ACP, 0, w_dir, -1, dir, _MAX_DIR,
                                NULL, NULL))
        {
            ASSERT("WideCharToMultiByte failed!\n");
            ON_ERROR;
        }
    }
    if(fname)
    {
        if(0 == WideCharToMultiByte(CP_ACP, 0, w_fname, -1, fname, _MAX_FNAME,
                                    NULL, NULL))
        {
            ASSERT("WideCharToMultiByte failed!\n");
            ON_ERROR;
        }
    }
    if(ext)
    {
        if(0 == WideCharToMultiByte(CP_ACP, 0, w_ext, -1, ext, _MAX_EXT,
                                NULL, NULL))
        {
            ASSERT("WideCharToMultiByte failed!\n");
            ON_ERROR;
        }
    }

done:
    LOGEXIT("_splitpath returns.\n");
    PERF_EXIT(_splitpath);
}



/*++
Function:
  _makepath

See MSDN doc.

--*/
void   
__cdecl 
_makepath(
          char *path, 
          const char *drive, 
          const char *dir, 
          const char *fname, 
          const char *ext)
{
    UINT Length = 0;

    PERF_ENTRY(_makepath);
    ENTRY( "_makepath (path=%p, drive=%p (%s), dir=%p (%s), fname=%p (%s), ext=%p (%s))\n", 
           path, drive ? drive:"NULL", drive ? drive:"NULL", dir ? dir:"NULL", dir ? dir:"NULL", fname ? fname:"NULL", fname ? fname:"NULL", 
           ext ? ext:"NULL", 
           ext ? ext:"NULL");

    path[ 0 ] = '\0';

    /* According to the pal documentation, host operating systems that
    don't support drive letters, the "drive" parameter must always be null. */
    if ( drive != NULL  && drive[0] != '\0' )
    {
        ASSERT( "The drive parameter must always be NULL on systems that don't"
              "support drive letters. drive is being ignored!.\n" );
    }

    if ( dir != NULL && dir[ 0 ] != '\0' )
    {
        UINT DirLength = strlen( dir );
        Length += DirLength ;
        
        if ( Length < _MAX_PATH )
        {
            strncat( path, dir, DirLength );
            if ( dir[ DirLength - 1 ] != '/' && dir[ DirLength - 1 ] != '\\' )
            {
                if ( Length + 1 < _MAX_PATH )
                {
                    path[ Length ] = '/';
                    Length++;
                    path[ Length ] = '\0';
                }
                else
                {
                    goto Max_Path_Error;
                }
            }
        }
        else
        {
            goto Max_Path_Error;
        }
    }

    if ( fname != NULL && fname[ 0 ] != '\0' )
    {
        UINT fNameLength = strlen( fname );
        Length += fNameLength;
        
        if ( Length < _MAX_PATH )
        {
            strncat( path, fname, fNameLength );
        }
        else
        {
            goto Max_Path_Error;
        }
    }

    if ( ext != NULL && ext[ 0 ] != '\0' )
    {
        UINT ExtLength = strlen( ext );
        Length += ExtLength;
        
        if ( ext[ 0 ] !=  '.' )
        {
            /* Add a '.' */
            if ( Length + 1 < _MAX_PATH )
            {
                path[ Length - ExtLength ] = '.';
                Length++;
                path[ Length - ExtLength ] = '\0';
                strncat( path, ext, ExtLength );
            }
            else
            {
                goto Max_Path_Error;
            }
        }
        else
        {
            /* Already has a '.' */
            if ( Length < _MAX_PATH )
            {
                strncat( path, ext, ExtLength );    
            }
            else
            {
                goto Max_Path_Error;
            }
        }
    }

    FILEDosToUnixPathA( path );
    LOGEXIT( "_makepath returning void.\n" );
    PERF_EXIT(_makepath);
    return;

Max_Path_Error:

    ERROR( "path cannot be greater then _MAX_PATH\n" ); 
    path[ 0 ] = '\0';
    LOGEXIT( "_makepath returning void \n" );
    PERF_EXIT(_makepath);
    return;
}

/*++
Function:
  _wmakepath

See MSDN doc.

--*/
void   
__cdecl 
_wmakepath(
          wchar_16 *path, 
          const wchar_16 *drive, 
          const wchar_16 *dir, 
          const wchar_16 *fname, 
          const wchar_16 *ext)
{
    CHAR Dir[ _MAX_DIR ]={0};
    CHAR FileName[ _MAX_FNAME ]={0};
    CHAR Ext[ _MAX_EXT ]={0};
    CHAR Path[ _MAX_PATH ]={0};
    
    PERF_ENTRY(_wmakepath);
    ENTRY("_wmakepath (path=%p, drive=%p (%S), dir=%p (%S), fname=%p (%S), ext=%p (%S))\n",
          path, drive ? drive:W16_NULLSTRING, drive ? drive:W16_NULLSTRING, dir ? dir:W16_NULLSTRING, dir ? dir:W16_NULLSTRING,
          fname ? fname:W16_NULLSTRING,
          fname ? fname:W16_NULLSTRING, ext ? ext:W16_NULLSTRING, ext ? ext:W16_NULLSTRING);

    /* According to the pal documentation, host operating systems that
    don't support drive letters, the "drive" parameter must always be null. */
    if ( drive != NULL  && drive[0] != '\0' )
    {
        ASSERT( "The drive parameter must always be NULL on systems that don't"
              "support drive letters. drive is being ignored!.\n" );
    }

    if ((dir != NULL) &&  WideCharToMultiByte( CP_ACP, 0, dir, -1, Dir, 
                                               _MAX_DIR, NULL, NULL ) == 0 )
    {
        ASSERT( "An error occurred while converting dir to multibyte."
               "Possible error: Length of dir is greater than _MAX_DIR.\n" );
        goto error;
    }

    if ((fname != NULL) && WideCharToMultiByte( CP_ACP, 0, fname, -1, FileName,
                                                _MAX_FNAME, NULL, NULL ) == 0 )
    {
        ASSERT( "An error occurred while converting fname to multibyte."
               "Possible error: Length of fname is greater than _MAX_FNAME.\n" );
        goto error;
    }

    if ((ext != NULL) && WideCharToMultiByte( CP_ACP, 0, ext, -1, Ext,
                                              _MAX_EXT, NULL, NULL ) == 0 )
    {
        ASSERT( "An error occurred while converting ext to multibyte."
               "Possible error: Length of ext is greater than _MAX_EXT.\n" );
        goto error;
    }

    /* Call up to the ANSI _makepath. */
    _makepath_s( Path, sizeof(Path), NULL, Dir, FileName, Ext );

    if ( MultiByteToWideChar( CP_ACP, 0, Path, -1, path, _MAX_PATH ) == 0 )
    {
        ASSERT( "An error occurred while converting the back wide char."
               "Possible error: The length of combined path is greater "
               "than _MAX_PATH.\n" );
        goto error;
    }

    LOGEXIT("_wmakepath returns void\n");
    PERF_EXIT(_wmakepath);
    return;

error:
    *path = '\0';
    LOGEXIT("_wmakepath returns void\n");
    PERF_EXIT(_wmakepath);
}


/*++
Function:
  _fullpath

See MSDN doc.

--*/
char *   
__cdecl 
_fullpath(
          char *absPath, 
          const char *relPath, 
          size_t maxLength)
{
    char realpath_buf[PATH_MAX+1];
    char path_copy[PATH_MAX+1];
    char *retval = NULL;
    DWORD cPathCopy = sizeof(path_copy)/sizeof(path_copy[0]);
    size_t min_length;
    BOOL fBufAllocated = FALSE;

    PERF_ENTRY(_fullpath);
    ENTRY("_fullpath (absPath=%p, relPath=%p (%s), maxLength = %lu)\n",
          absPath, relPath ? relPath:"NULL", relPath ? relPath:"NULL", maxLength);
    
    if (strncpy_s(path_copy, sizeof(path_copy), relPath ? relPath : ".", cPathCopy) != SAFECRT_SUCCESS)
    {
        TRACE("_fullpath: strncpy_s failed!\n");
        goto fullpathExit;
    }

    FILEDosToUnixPathA(path_copy);

    if(NULL == realpath(path_copy, realpath_buf))
    {
        ERROR("realpath() failed; problem path is '%s'. errno is %d (%s)\n",
                realpath_buf, errno, strerror(errno));
        goto fullpathExit;
    }   

    TRACE("real path is %s\n", realpath_buf);
    min_length = strlen(realpath_buf)+1; // +1 for the NULL terminator

    if(NULL == absPath)
    {
        absPath = static_cast<char *>(
            PAL_malloc(_MAX_PATH * sizeof(char)));
        if (!absPath)
        {
            ERROR("PAL_malloc failed with error %d\n", errno);
            goto fullpathExit;
        }
        maxLength = _MAX_PATH;
        fBufAllocated = TRUE;
    }

    if(min_length > maxLength)
    {
        ERROR("maxLength is %lu, we need at least %lu\n",
                maxLength, min_length);
        if (fBufAllocated)
        {
            PAL_free(absPath);
            fBufAllocated = FALSE;
        }
        goto fullpathExit;
    }

    strcpy_s(absPath, maxLength, realpath_buf);
    retval = absPath;
    
fullpathExit:
    LOGEXIT("_fullpath returns char * %p\n", retval);
    PERF_EXIT(_fullpath);
    return retval;
}