2 * (c) The GRASP/AQUA Project, Glasgow University, 1994-1998
4 * $Id: renameFile.c,v 1.3 1998/12/02 13:27:51 simonm Exp $
6 * renameFile Runtime Support
12 #ifdef HAVE_SYS_TYPES_H
13 #include <sys/types.h>
16 #ifdef HAVE_SYS_STAT_H
25 renameFile(opath, npath)
33 /* Check for a non-directory source */
34 while (stat(opath, &sb) != 0) {
41 if (S_ISDIR(sb.st_mode)) {
42 ghc_errtype = ERR_INAPPROPRIATETYPE;
43 ghc_errstr = "file is a directory";
47 /* Ensure a non-directory destination */
49 /* First try to open without creating */
50 while ((fd = open(npath, O_RDONLY | O_NOCTTY, 0)) < 0) {
51 if (errno == ENOENT) {
52 /* Now try to create it */
53 while ((fd = open(npath, O_RDONLY | O_NOCTTY | O_CREAT | O_EXCL, 0)) < 0) {
54 if (errno == EEXIST) {
55 /* Race detected; go back and open without creating it */
57 } else if (errno != EINTR) {
65 ghc_errtype = ERR_NOSUCHTHING;
66 ghc_errstr = "no path to file";
69 ghc_errtype = ERR_PERMISSIONDENIED;
70 ghc_errstr = "unsupported owner or group";
80 } else if (errno != EINTR) {
87 ghc_errtype = ERR_NOSUCHTHING;
88 ghc_errstr = "no path to file";
91 ghc_errtype = ERR_PERMISSIONDENIED;
92 ghc_errstr = "unsupported owner or group";
99 /* Make sure that we aren't looking at a directory */
101 while (fstat(fd, &sb) < 0) {
102 /* highly unlikely */
103 if (errno != EINTR) {
106 (void) unlink(npath);
111 if (S_ISDIR(sb.st_mode)) {
112 ghc_errtype = ERR_INAPPROPRIATETYPE;
113 ghc_errstr = "destination is a directory";
114 /* We can't have created it in this case. */
119 while(rename(opath, npath) != 0) {
120 if (errno != EINTR) {
124 (void) unlink(npath);