@@ -510,14 +510,18 @@ ensure_file (const char *path,
510510 the create file will fail in the read-only
511511 case with EROFS instead of EEXIST.
512512
513- We're trying to set up a mount point for a non-directory, so any
514- non-directory, non-symlink is acceptable - it doesn't necessarily
515- have to be a regular file. */
513+ We're trying to set up a mount point for a non-directory, for which
514+ the kernel will accept any non-directory. If it's a symlink, follow
515+ it and look at the target: again, any non-directory is good enough.
516+ We'll only get S_ISLNK if the path is a dangling symlink (target
517+ doesn't exist). */
516518 if (stat (path , & buf ) == 0 &&
517519 !S_ISDIR (buf .st_mode ) &&
518520 !S_ISLNK (buf .st_mode ))
519521 return 0 ;
520522
523+ /* If the file didn't exist, create it. If it was a dangling symlink
524+ * (S_ISLNK above) then this will create the target of the symlink. */
521525 if (create_file (path , mode , NULL ) != 0 && errno != EEXIST )
522526 return -1 ;
523527
@@ -681,7 +685,8 @@ ensure_dir (const char *path,
681685 /* We check this ahead of time, otherwise
682686 the mkdir call can fail in the read-only
683687 case with EROFS instead of EEXIST on some
684- filesystems (such as NFS) */
688+ filesystems (such as NFS).
689+ We follow symlinks: it's OK if path is a symlink to a directory. */
685690 if (stat (path , & buf ) == 0 )
686691 {
687692 if (!S_ISDIR (buf .st_mode ))
0 commit comments