@@ -1386,6 +1386,7 @@ char *mingw_strbuf_realpath(struct strbuf *resolved, const char *path)
1386
1386
DWORD ret ;
1387
1387
int len ;
1388
1388
const char * last_component = NULL ;
1389
+ char * append = NULL ;
1389
1390
1390
1391
if (xutftowcs_path (wpath , path ) < 0 )
1391
1392
return NULL ;
@@ -1408,34 +1409,46 @@ char *mingw_strbuf_realpath(struct strbuf *resolved, const char *path)
1408
1409
break ; /* found start of last component */
1409
1410
1410
1411
if (p != wpath && (last_component = find_last_dir_sep (path ))) {
1411
- last_component ++ ; /* skip directory separator */
1412
- * p = L'\0' ;
1412
+ append = xstrdup (last_component + 1 ); /* skip directory separator */
1413
+ /*
1414
+ * Do not strip the trailing slash at the drive root, otherwise
1415
+ * the path would be e.g. `C:` (which resolves to the
1416
+ * _current_ directory on that drive).
1417
+ */
1418
+ if (p [-1 ] == L':' )
1419
+ p [1 ] = L'\0' ;
1420
+ else
1421
+ * p = L'\0' ;
1413
1422
h = CreateFileW (wpath , 0 , FILE_SHARE_READ |
1414
1423
FILE_SHARE_WRITE | FILE_SHARE_DELETE ,
1415
1424
NULL , OPEN_EXISTING ,
1416
1425
FILE_FLAG_BACKUP_SEMANTICS , NULL );
1417
1426
}
1418
1427
}
1419
1428
1420
- if (h == INVALID_HANDLE_VALUE )
1429
+ if (h == INVALID_HANDLE_VALUE ) {
1430
+ realpath_failed :
1431
+ FREE_AND_NULL (append );
1421
1432
return NULL ;
1433
+ }
1422
1434
1423
1435
ret = GetFinalPathNameByHandleW (h , wpath , ARRAY_SIZE (wpath ), 0 );
1424
1436
CloseHandle (h );
1425
1437
if (!ret || ret >= ARRAY_SIZE (wpath ))
1426
- return NULL ;
1438
+ goto realpath_failed ;
1427
1439
1428
1440
len = wcslen (wpath ) * 3 ;
1429
1441
strbuf_grow (resolved , len );
1430
1442
len = xwcstoutf (resolved -> buf , normalize_ntpath (wpath ), len );
1431
1443
if (len < 0 )
1432
- return NULL ;
1444
+ goto realpath_failed ;
1433
1445
resolved -> len = len ;
1434
1446
1435
- if (last_component ) {
1447
+ if (append ) {
1436
1448
/* Use forward-slash, like `normalize_ntpath()` */
1437
- strbuf_addch (resolved , '/' );
1438
- strbuf_addstr (resolved , last_component );
1449
+ strbuf_complete (resolved , '/' );
1450
+ strbuf_addstr (resolved , append );
1451
+ FREE_AND_NULL (append );
1439
1452
}
1440
1453
1441
1454
return resolved -> buf ;
0 commit comments