@@ -1230,6 +1230,82 @@ struct tm *localtime_r(const time_t *timep, struct tm *result)
1230
1230
}
1231
1231
#endif
1232
1232
1233
+ char * mingw_strbuf_realpath (struct strbuf * resolved , const char * path )
1234
+ {
1235
+ wchar_t wpath [MAX_PATH ];
1236
+ HANDLE h ;
1237
+ DWORD ret ;
1238
+ int len ;
1239
+ const char * last_component = NULL ;
1240
+ char * append = NULL ;
1241
+
1242
+ if (xutftowcs_path (wpath , path ) < 0 )
1243
+ return NULL ;
1244
+
1245
+ h = CreateFileW (wpath , 0 ,
1246
+ FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE , NULL ,
1247
+ OPEN_EXISTING , FILE_FLAG_BACKUP_SEMANTICS , NULL );
1248
+
1249
+ /*
1250
+ * strbuf_realpath() allows the last path component to not exist. If
1251
+ * that is the case, now it's time to try without last component.
1252
+ */
1253
+ if (h == INVALID_HANDLE_VALUE &&
1254
+ GetLastError () == ERROR_FILE_NOT_FOUND ) {
1255
+ /* cut last component off of `wpath` */
1256
+ wchar_t * p = wpath + wcslen (wpath );
1257
+
1258
+ while (p != wpath )
1259
+ if (* (-- p ) == L'/' || * p == L'\\' )
1260
+ break ; /* found start of last component */
1261
+
1262
+ if (p != wpath && (last_component = find_last_dir_sep (path ))) {
1263
+ append = xstrdup (last_component + 1 ); /* skip directory separator */
1264
+ /*
1265
+ * Do not strip the trailing slash at the drive root, otherwise
1266
+ * the path would be e.g. `C:` (which resolves to the
1267
+ * _current_ directory on that drive).
1268
+ */
1269
+ if (p [-1 ] == L':' )
1270
+ p [1 ] = L'\0' ;
1271
+ else
1272
+ * p = L'\0' ;
1273
+ h = CreateFileW (wpath , 0 , FILE_SHARE_READ |
1274
+ FILE_SHARE_WRITE | FILE_SHARE_DELETE ,
1275
+ NULL , OPEN_EXISTING ,
1276
+ FILE_FLAG_BACKUP_SEMANTICS , NULL );
1277
+ }
1278
+ }
1279
+
1280
+ if (h == INVALID_HANDLE_VALUE ) {
1281
+ realpath_failed :
1282
+ FREE_AND_NULL (append );
1283
+ return NULL ;
1284
+ }
1285
+
1286
+ ret = GetFinalPathNameByHandleW (h , wpath , ARRAY_SIZE (wpath ), 0 );
1287
+ CloseHandle (h );
1288
+ if (!ret || ret >= ARRAY_SIZE (wpath ))
1289
+ goto realpath_failed ;
1290
+
1291
+ len = wcslen (wpath ) * 3 ;
1292
+ strbuf_grow (resolved , len );
1293
+ len = xwcstoutf (resolved -> buf , normalize_ntpath (wpath ), len );
1294
+ if (len < 0 )
1295
+ goto realpath_failed ;
1296
+ resolved -> len = len ;
1297
+
1298
+ if (append ) {
1299
+ /* Use forward-slash, like `normalize_ntpath()` */
1300
+ strbuf_complete (resolved , '/' );
1301
+ strbuf_addstr (resolved , append );
1302
+ FREE_AND_NULL (append );
1303
+ }
1304
+
1305
+ return resolved -> buf ;
1306
+
1307
+ }
1308
+
1233
1309
char * mingw_getcwd (char * pointer , int len )
1234
1310
{
1235
1311
wchar_t cwd [MAX_PATH ], wpointer [MAX_PATH ];
0 commit comments