2 % (c) The GRASP/AQUA Project, Glasgow University, 1995
4 \subsection[renameFile.lc]{renameFile Runtime Support}
11 #ifdef HAVE_SYS_TYPES_H
12 #include <sys/types.h>
15 #ifdef HAVE_SYS_STAT_H
24 renameFile(opath, npath)
32 /* Check for a non-directory source */
33 while (stat(opath, &sb) != 0) {
40 if (S_ISDIR(sb.st_mode)) {
41 ghc_errtype = ERR_INAPPROPRIATETYPE;
42 ghc_errstr = "file is a directory";
46 /* Ensure a non-directory destination */
48 /* First try to open without creating */
49 while ((fd = open(npath, O_RDONLY | O_NOCTTY, 0)) < 0) {
50 if (errno == ENOENT) {
51 /* Now try to create it */
52 while ((fd = open(npath, O_RDONLY | O_NOCTTY | O_CREAT | O_EXCL, 0)) < 0) {
53 if (errno == EEXIST) {
54 /* Race detected; go back and open without creating it */
56 } else if (errno != EINTR) {
64 ghc_errtype = ERR_NOSUCHTHING;
65 ghc_errstr = "no path to file";
68 ghc_errtype = ERR_PERMISSIONDENIED;
69 ghc_errstr = "unsupported owner or group";
79 } else if (errno != EINTR) {
86 ghc_errtype = ERR_NOSUCHTHING;
87 ghc_errstr = "no path to file";
90 ghc_errtype = ERR_PERMISSIONDENIED;
91 ghc_errstr = "unsupported owner or group";
98 /* Make sure that we aren't looking at a directory */
100 while (fstat(fd, &sb) < 0) {
101 /* highly unlikely */
102 if (errno != EINTR) {
105 (void) unlink(npath);
110 if (S_ISDIR(sb.st_mode)) {
111 ghc_errtype = ERR_INAPPROPRIATETYPE;
112 ghc_errstr = "destination is a directory";
113 /* We can't have created it in this case. */
118 while(rename(opath, npath) != 0) {
119 if (errno != EINTR) {
123 (void) unlink(npath);