Skip to content

Commit 5e8f408

Browse files
authored
Merge pull request #737 from smcv/issue733
utils: Clarify that ensure_file, ensure_dir intentionally follow symlinks
2 parents c7a1bec + 8ed0ce5 commit 5e8f408

1 file changed

Lines changed: 9 additions & 4 deletions

File tree

utils.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)