@@ -1271,13 +1271,38 @@ char *mingw_strbuf_realpath(struct strbuf *resolved, const char *path)
1271
1271
HANDLE h ;
1272
1272
DWORD ret ;
1273
1273
int len ;
1274
+ const char * last_component = NULL ;
1274
1275
1275
1276
if (xutftowcs_path (wpath , path ) < 0 )
1276
1277
return NULL ;
1277
1278
1278
1279
h = CreateFileW (wpath , 0 ,
1279
1280
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE , NULL ,
1280
1281
OPEN_EXISTING , FILE_FLAG_BACKUP_SEMANTICS , NULL );
1282
+
1283
+ /*
1284
+ * strbuf_realpath() allows the last path component to not exist. If
1285
+ * that is the case, now it's time to try without last component.
1286
+ */
1287
+ if (h == INVALID_HANDLE_VALUE &&
1288
+ GetLastError () == ERROR_FILE_NOT_FOUND ) {
1289
+ /* cut last component off of `wpath` */
1290
+ wchar_t * p = wpath + wcslen (wpath );
1291
+
1292
+ while (p != wpath )
1293
+ if (* (-- p ) == L'/' || * p == L'\\' )
1294
+ break ; /* found start of last component */
1295
+
1296
+ if (p != wpath && (last_component = find_last_dir_sep (path ))) {
1297
+ last_component ++ ; /* skip directory separator */
1298
+ * p = L'\0' ;
1299
+ h = CreateFileW (wpath , 0 , FILE_SHARE_READ |
1300
+ FILE_SHARE_WRITE | FILE_SHARE_DELETE ,
1301
+ NULL , OPEN_EXISTING ,
1302
+ FILE_FLAG_BACKUP_SEMANTICS , NULL );
1303
+ }
1304
+ }
1305
+
1281
1306
if (h == INVALID_HANDLE_VALUE )
1282
1307
return NULL ;
1283
1308
@@ -1292,6 +1317,13 @@ char *mingw_strbuf_realpath(struct strbuf *resolved, const char *path)
1292
1317
if (len < 0 )
1293
1318
return NULL ;
1294
1319
resolved -> len = len ;
1320
+
1321
+ if (last_component ) {
1322
+ /* Use forward-slash, like `normalize_ntpath()` */
1323
+ strbuf_addch (resolved , '/' );
1324
+ strbuf_addstr (resolved , last_component );
1325
+ }
1326
+
1295
1327
return resolved -> buf ;
1296
1328
1297
1329
}
0 commit comments