99#include "win32/lazyload.h"
1010#include "../config.h"
1111#include "dir.h"
12+ #include "../attr.h"
1213
1314#define HCAST (type , handle ) ((type)(intptr_t)handle)
1415
@@ -413,6 +414,54 @@ static void process_phantom_symlinks(void)
413414 LeaveCriticalSection (& phantom_symlinks_cs );
414415}
415416
417+ static int create_phantom_symlink (wchar_t * wtarget , wchar_t * wlink )
418+ {
419+ int len ;
420+
421+ /* create file symlink */
422+ if (!CreateSymbolicLinkW (wlink , wtarget , symlink_file_flags )) {
423+ errno = err_win_to_posix (GetLastError ());
424+ return -1 ;
425+ }
426+
427+ /* convert to directory symlink if target exists */
428+ switch (process_phantom_symlink (wtarget , wlink )) {
429+ case PHANTOM_SYMLINK_RETRY : {
430+ /* if target doesn't exist, add to phantom symlinks list */
431+ wchar_t wfullpath [MAX_LONG_PATH ];
432+ struct phantom_symlink_info * psi ;
433+
434+ /* convert to absolute path to be independent of cwd */
435+ len = GetFullPathNameW (wlink , MAX_LONG_PATH , wfullpath , NULL );
436+ if (!len || len >= MAX_LONG_PATH ) {
437+ errno = err_win_to_posix (GetLastError ());
438+ return -1 ;
439+ }
440+
441+ /* over-allocate and fill phantom_symlink_info structure */
442+ psi = xmalloc (sizeof (struct phantom_symlink_info ) +
443+ sizeof (wchar_t ) * (len + wcslen (wtarget ) + 2 ));
444+ psi -> wlink = (wchar_t * )(psi + 1 );
445+ wcscpy (psi -> wlink , wfullpath );
446+ psi -> wtarget = psi -> wlink + len + 1 ;
447+ wcscpy (psi -> wtarget , wtarget );
448+
449+ EnterCriticalSection (& phantom_symlinks_cs );
450+ psi -> next = phantom_symlinks ;
451+ phantom_symlinks = psi ;
452+ LeaveCriticalSection (& phantom_symlinks_cs );
453+ break ;
454+ }
455+ case PHANTOM_SYMLINK_DIRECTORY :
456+ /* if we created a dir symlink, process other phantom symlinks */
457+ process_phantom_symlinks ();
458+ break ;
459+ default :
460+ break ;
461+ }
462+ return 0 ;
463+ }
464+
416465/* Normalizes NT paths as returned by some low-level APIs. */
417466static wchar_t * normalize_ntpath (wchar_t * wbuf )
418467{
@@ -2342,7 +2391,38 @@ int link(const char *oldpath, const char *newpath)
23422391 return 0 ;
23432392}
23442393
2345- int symlink (const char * target , const char * link )
2394+ enum symlink_type {
2395+ SYMLINK_TYPE_UNSPECIFIED = 0 ,
2396+ SYMLINK_TYPE_FILE ,
2397+ SYMLINK_TYPE_DIRECTORY ,
2398+ };
2399+
2400+ static enum symlink_type check_symlink_attr (struct index_state * index , const char * link )
2401+ {
2402+ static struct attr_check * check ;
2403+ const char * value ;
2404+
2405+ if (!index )
2406+ return SYMLINK_TYPE_UNSPECIFIED ;
2407+
2408+ if (!check )
2409+ check = attr_check_initl ("symlink" , NULL );
2410+
2411+ git_check_attr (index , link , check );
2412+
2413+ value = check -> items [0 ].value ;
2414+ if (ATTR_UNSET (value ))
2415+ return SYMLINK_TYPE_UNSPECIFIED ;
2416+ if (!strcmp (value , "file" ))
2417+ return SYMLINK_TYPE_FILE ;
2418+ if (!strcmp (value , "dir" ) || !strcmp (value , "directory" ))
2419+ return SYMLINK_TYPE_DIRECTORY ;
2420+
2421+ warning (_ ("ignoring invalid symlink type '%s' for '%s'" ), value , link );
2422+ return SYMLINK_TYPE_UNSPECIFIED ;
2423+ }
2424+
2425+ int mingw_create_symlink (struct index_state * index , const char * target , const char * link )
23462426{
23472427 wchar_t wtarget [MAX_LONG_PATH ], wlink [MAX_LONG_PATH ];
23482428 int len ;
@@ -2362,48 +2442,31 @@ int symlink(const char *target, const char *link)
23622442 if (wtarget [len ] == '/' )
23632443 wtarget [len ] = '\\' ;
23642444
2365- /* create file symlink */
2366- if (!CreateSymbolicLinkW (wlink , wtarget , symlink_file_flags )) {
2367- errno = err_win_to_posix (GetLastError ());
2368- return -1 ;
2369- }
2370-
2371- /* convert to directory symlink if target exists */
2372- switch (process_phantom_symlink (wtarget , wlink )) {
2373- case PHANTOM_SYMLINK_RETRY : {
2374- /* if target doesn't exist, add to phantom symlinks list */
2375- wchar_t wfullpath [MAX_LONG_PATH ];
2376- struct phantom_symlink_info * psi ;
2377-
2378- /* convert to absolute path to be independent of cwd */
2379- len = GetFullPathNameW (wlink , MAX_LONG_PATH , wfullpath , NULL );
2380- if (!len || len >= MAX_LONG_PATH ) {
2381- errno = err_win_to_posix (GetLastError ());
2382- return -1 ;
2383- }
2384-
2385- /* over-allocate and fill phantom_symlink_info structure */
2386- psi = xmalloc (sizeof (struct phantom_symlink_info )
2387- + sizeof (wchar_t ) * (len + wcslen (wtarget ) + 2 ));
2388- psi -> wlink = (wchar_t * )(psi + 1 );
2389- wcscpy (psi -> wlink , wfullpath );
2390- psi -> wtarget = psi -> wlink + len + 1 ;
2391- wcscpy (psi -> wtarget , wtarget );
2392-
2393- EnterCriticalSection (& phantom_symlinks_cs );
2394- psi -> next = phantom_symlinks ;
2395- phantom_symlinks = psi ;
2396- LeaveCriticalSection (& phantom_symlinks_cs );
2397- break ;
2398- }
2399- case PHANTOM_SYMLINK_DIRECTORY :
2400- /* if we created a dir symlink, process other phantom symlinks */
2445+ switch (check_symlink_attr (index , link )) {
2446+ case SYMLINK_TYPE_UNSPECIFIED :
2447+ /* Create a phantom symlink: it is initially created as a file
2448+ * symlink, but may change to a directory symlink later if/when
2449+ * the target exists. */
2450+ return create_phantom_symlink (wtarget , wlink );
2451+ case SYMLINK_TYPE_FILE :
2452+ if (!CreateSymbolicLinkW (wlink , wtarget , symlink_file_flags ))
2453+ break ;
2454+ return 0 ;
2455+ case SYMLINK_TYPE_DIRECTORY :
2456+ if (!CreateSymbolicLinkW (wlink , wtarget ,
2457+ symlink_directory_flags ))
2458+ break ;
2459+ /* There may be dangling phantom symlinks that point at this
2460+ * one, which should now morph into directory symlinks. */
24012461 process_phantom_symlinks ();
2402- break ;
2462+ return 0 ;
24032463 default :
2404- break ;
2464+ BUG ( "unhandled symlink type" ) ;
24052465 }
2406- return 0 ;
2466+
2467+ /* CreateSymbolicLinkW failed. */
2468+ errno = err_win_to_posix (GetLastError ());
2469+ return -1 ;
24072470}
24082471
24092472#ifndef _WINNT_H
0 commit comments