- /* Ensure a non-directory destination */
-
- /* First try to open without creating */
- while ((fd = open(npath, O_RDONLY | O_NOCTTY, 0)) < 0) {
- if (errno == ENOENT) {
- /* Now try to create it */
- while ((fd = open(npath, O_RDONLY | O_NOCTTY | O_CREAT | O_EXCL, 0)) < 0) {
- if (errno == EEXIST) {
- /* Race detected; go back and open without creating it */
- break;
- } else if (errno != EINTR) {
- cvtErrno();
- switch (ghc_errno) {
- default:
- stdErrno();
- break;
- case GHC_ENOENT:
- case GHC_ENOTDIR:
- ghc_errtype = ERR_NOSUCHTHING;
- ghc_errstr = "no path to file";
- break;
- case GHC_EINVAL:
- ghc_errtype = ERR_PERMISSIONDENIED;
- ghc_errstr = "unsupported owner or group";
- break;
- }
- return -1;
- }
- }
- if (fd >= 0) {
- created = 1;
- break;
- }
- } else if (errno != EINTR) {
+ /* Check for a non-directory destination */
+ while (stat(npath, &sb) != 0 && errno != ENOENT) {
+ if (errno != EINTR) {