2 * (c) The GRASP/AQUA Project, Glasgow University, 1994-1998
4 * $Id: renameFile.c,v 1.1 1998/04/10 10:54:48 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(StgByteArray opath, StgByteArray npath)
31 /* Check for a non-directory source */
32 while (stat(opath, &sb) != 0) {
39 if (S_ISDIR(sb.st_mode)) {
40 ghc_errtype = ERR_INAPPROPRIATETYPE;
41 ghc_errstr = "file is a directory";
45 /* Ensure a non-directory destination */
47 /* First try to open without creating */
48 while ((fd = open(npath, O_RDONLY | O_NOCTTY, 0)) < 0) {
49 if (errno == ENOENT) {
50 /* Now try to create it */
51 while ((fd = open(npath, O_RDONLY | O_NOCTTY | O_CREAT | O_EXCL, 0)) < 0) {
52 if (errno == EEXIST) {
53 /* Race detected; go back and open without creating it */
55 } else if (errno != EINTR) {
63 ghc_errtype = ERR_NOSUCHTHING;
64 ghc_errstr = "no path to file";
67 ghc_errtype = ERR_PERMISSIONDENIED;
68 ghc_errstr = "unsupported owner or group";
78 } else if (errno != EINTR) {
85 ghc_errtype = ERR_NOSUCHTHING;
86 ghc_errstr = "no path to file";
89 ghc_errtype = ERR_PERMISSIONDENIED;
90 ghc_errstr = "unsupported owner or group";
97 /* Make sure that we aren't looking at a directory */
99 while (fstat(fd, &sb) < 0) {
100 /* highly unlikely */
101 if (errno != EINTR) {
104 (void) unlink(npath);
109 if (S_ISDIR(sb.st_mode)) {
110 ghc_errtype = ERR_INAPPROPRIATETYPE;
111 ghc_errstr = "destination is a directory";
112 /* We can't have created it in this case. */
117 while(rename(opath, npath) != 0) {
118 if (errno != EINTR) {
122 (void) unlink(npath);