@@ -1137,6 +1137,82 @@ struct tm *localtime_r(const time_t *timep, struct tm *result)
1137
1137
}
1138
1138
#endif
1139
1139
1140
+ char * mingw_strbuf_realpath (struct strbuf * resolved , const char * path )
1141
+ {
1142
+ wchar_t wpath [MAX_PATH ];
1143
+ HANDLE h ;
1144
+ DWORD ret ;
1145
+ int len ;
1146
+ const char * last_component = NULL ;
1147
+ char * append = NULL ;
1148
+
1149
+ if (xutftowcs_path (wpath , path ) < 0 )
1150
+ return NULL ;
1151
+
1152
+ h = CreateFileW (wpath , 0 ,
1153
+ FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE , NULL ,
1154
+ OPEN_EXISTING , FILE_FLAG_BACKUP_SEMANTICS , NULL );
1155
+
1156
+ /*
1157
+ * strbuf_realpath() allows the last path component to not exist. If
1158
+ * that is the case, now it's time to try without last component.
1159
+ */
1160
+ if (h == INVALID_HANDLE_VALUE &&
1161
+ GetLastError () == ERROR_FILE_NOT_FOUND ) {
1162
+ /* cut last component off of `wpath` */
1163
+ wchar_t * p = wpath + wcslen (wpath );
1164
+
1165
+ while (p != wpath )
1166
+ if (* (-- p ) == L'/' || * p == L'\\' )
1167
+ break ; /* found start of last component */
1168
+
1169
+ if (p != wpath && (last_component = find_last_dir_sep (path ))) {
1170
+ append = xstrdup (last_component + 1 ); /* skip directory separator */
1171
+ /*
1172
+ * Do not strip the trailing slash at the drive root, otherwise
1173
+ * the path would be e.g. `C:` (which resolves to the
1174
+ * _current_ directory on that drive).
1175
+ */
1176
+ if (p [-1 ] == L':' )
1177
+ p [1 ] = L'\0' ;
1178
+ else
1179
+ * p = L'\0' ;
1180
+ h = CreateFileW (wpath , 0 , FILE_SHARE_READ |
1181
+ FILE_SHARE_WRITE | FILE_SHARE_DELETE ,
1182
+ NULL , OPEN_EXISTING ,
1183
+ FILE_FLAG_BACKUP_SEMANTICS , NULL );
1184
+ }
1185
+ }
1186
+
1187
+ if (h == INVALID_HANDLE_VALUE ) {
1188
+ realpath_failed :
1189
+ FREE_AND_NULL (append );
1190
+ return NULL ;
1191
+ }
1192
+
1193
+ ret = GetFinalPathNameByHandleW (h , wpath , ARRAY_SIZE (wpath ), 0 );
1194
+ CloseHandle (h );
1195
+ if (!ret || ret >= ARRAY_SIZE (wpath ))
1196
+ goto realpath_failed ;
1197
+
1198
+ len = wcslen (wpath ) * 3 ;
1199
+ strbuf_grow (resolved , len );
1200
+ len = xwcstoutf (resolved -> buf , normalize_ntpath (wpath ), len );
1201
+ if (len < 0 )
1202
+ goto realpath_failed ;
1203
+ resolved -> len = len ;
1204
+
1205
+ if (append ) {
1206
+ /* Use forward-slash, like `normalize_ntpath()` */
1207
+ strbuf_complete (resolved , '/' );
1208
+ strbuf_addstr (resolved , append );
1209
+ FREE_AND_NULL (append );
1210
+ }
1211
+
1212
+ return resolved -> buf ;
1213
+
1214
+ }
1215
+
1140
1216
char * mingw_getcwd (char * pointer , int len )
1141
1217
{
1142
1218
wchar_t cwd [MAX_PATH ], wpointer [MAX_PATH ];
0 commit comments