2 * (c) The GRASP/AQUA Project, Glasgow University, 1994-1998
4 * $Id: renameFile.c,v 1.5 1999/09/30 12:35:50 sof Exp $
6 * renameFile Runtime Support
12 #ifdef HAVE_SYS_TYPES_H
13 #include <sys/types.h>
16 #ifdef HAVE_SYS_STAT_H
24 #if defined(mingw32_TARGET_OS) && !defined(O_NOCTTY)
30 renameFile(opath, npath)
38 /* Check for a non-directory source */
39 while (stat(opath, &sb) != 0) {
46 if (S_ISDIR(sb.st_mode)) {
47 ghc_errtype = ERR_INAPPROPRIATETYPE;
48 ghc_errstr = "file is a directory";
52 /* Ensure a non-directory destination */
54 /* First try to open without creating */
55 while ((fd = open(npath, O_RDONLY | O_NOCTTY, 0)) < 0) {
56 if (errno == ENOENT) {
57 /* Now try to create it */
58 while ((fd = open(npath, O_RDONLY | O_NOCTTY | O_CREAT | O_EXCL, 0)) < 0) {
59 if (errno == EEXIST) {
60 /* Race detected; go back and open without creating it */
62 } else if (errno != EINTR) {
70 ghc_errtype = ERR_NOSUCHTHING;
71 ghc_errstr = "no path to file";
74 ghc_errtype = ERR_PERMISSIONDENIED;
75 ghc_errstr = "unsupported owner or group";
85 } else if (errno != EINTR) {
92 ghc_errtype = ERR_NOSUCHTHING;
93 ghc_errstr = "no path to file";
96 ghc_errtype = ERR_PERMISSIONDENIED;
97 ghc_errstr = "unsupported owner or group";
104 /* Make sure that we aren't looking at a directory */
106 while (fstat(fd, &sb) < 0) {
107 /* highly unlikely */
108 if (errno != EINTR) {
111 (void) unlink(npath);
116 if (S_ISDIR(sb.st_mode)) {
117 ghc_errtype = ERR_INAPPROPRIATETYPE;
118 ghc_errstr = "destination is a directory";
119 /* We can't have created it in this case. */
124 while(rename(opath, npath) != 0) {
125 if (errno != EINTR) {
129 (void) unlink(npath);