[project @ 2003-08-23 00:08:02 by sof]
[ghc-base.git] / cbits / dirUtils.c
1 /* 
2  * (c) The University of Glasgow 2002
3  *
4  * Directory Runtime Support
5  */
6
7 #include "config.h"
8
9 // The following is required on Solaris to force the POSIX versions of
10 // the various _r functions instead of the Solaris versions.
11 #ifdef solaris2_TARGET_OS
12 #define _POSIX_PTHREAD_SEMANTICS
13 #endif
14
15 #include "HsBase.h"
16
17 #if defined(mingw32_TARGET_OS)
18 #include <windows.h>
19
20 static
21 int
22 toErrno(DWORD rc)
23 {
24     switch (rc) {
25     case ERROR_FILE_NOT_FOUND:    return ENOENT;
26     case ERROR_PATH_NOT_FOUND:    return ENOENT;
27     case ERROR_TOO_MANY_OPEN_FILES: return EMFILE;
28     case ERROR_ACCESS_DENIED:     return EACCES;
29     case ERROR_INVALID_HANDLE:    return EBADF; /* kinda sorta */
30     case ERROR_NOT_ENOUGH_MEMORY: return ENOMEM;
31     case ERROR_INVALID_ACCESS:    return EINVAL;
32     case ERROR_INVALID_DATA:      return EINVAL;
33     case ERROR_OUTOFMEMORY:       return ENOMEM;
34     case ERROR_SHARING_VIOLATION: return EACCES;
35     case ERROR_LOCK_VIOLATION:    return EACCES;
36     case ERROR_ALREADY_EXISTS:    return EEXIST;
37     case ERROR_BUSY:              return EBUSY;
38     case ERROR_BROKEN_PIPE:       return EPIPE;
39     case ERROR_PIPE_CONNECTED:    return EBUSY;
40     case ERROR_PIPE_LISTENING:    return EBUSY;
41     case ERROR_NOT_CONNECTED:     return EINVAL;
42
43     case ERROR_NOT_OWNER:         return EPERM;
44     case ERROR_DIRECTORY:         return ENOTDIR;
45     case ERROR_FILE_INVALID:      return EACCES;
46     case ERROR_FILE_EXISTS:       return EEXIST;
47
48     default:
49         return rc;
50     }
51 }
52 #endif
53
54
55 /*
56  * read an entry from the directory stream; opt for the
57  * re-entrant friendly way of doing this, if available.
58  */
59 HsInt
60 __hscore_readdir( HsAddr dirPtr, HsAddr pDirEnt )
61 {
62   struct dirent **pDirE = (struct dirent**)pDirEnt;
63 #if HAVE_READDIR_R
64   struct dirent* p;
65   int res;
66   static unsigned int nm_max = -1;
67   
68   if (pDirE == NULL) {
69     return -1;
70   }
71   if (nm_max == (unsigned int)-1) {
72 #ifdef NAME_MAX
73     nm_max = NAME_MAX + 1;
74 #else
75     nm_max = pathconf(".", _PC_NAME_MAX);
76     if (nm_max == -1) { nm_max = 255; }
77     nm_max++;
78 #endif
79   }
80   p = (struct dirent*)malloc(sizeof(struct dirent) + nm_max);
81   if (p == NULL) return -1;
82   res = readdir_r((DIR*)dirPtr, p, pDirE);
83   if (res != 0) {
84     *pDirE = NULL;
85     free(p);
86   }
87   return res;
88 #else
89
90   if (pDirE == NULL) {
91     return -1;
92   }
93
94   *pDirE = readdir((DIR*)dirPtr);
95   if (*pDirE == NULL) {
96     return -1;
97   } else {
98     return 0;
99   }  
100 #endif
101 }
102
103 /*
104  * Function: __hscore_renameFile()
105  *
106  * Provide Haskell98's semantics for renaming files and directories.
107  * It mirrors that of POSIX.1's behaviour for rename() by overwriting
108  * the target if it exists (the MS CRT implementation of rename() returns
109  * an error
110  *
111  */
112 HsInt
113 __hscore_renameFile( HsAddr src,
114                      HsAddr dest)
115 {
116 #if (defined(_MSC_VER) || defined(_WIN32))
117     static int forNT = -1;
118     
119     /* ToDo: propagate error codes back */
120     if (MoveFileA(src, dest)) {
121         return 0;
122     } else {
123         ;
124     }
125     
126     /* Failed...it could be because the target already existed. */
127     if ( !GetFileAttributes(dest) ) {
128         /* No, it's not there - just fail. */
129         errno = toErrno(GetLastError());
130         return (-1);
131     }
132
133     if (forNT == -1) {
134         OSVERSIONINFO ovi;
135         ovi.dwOSVersionInfoSize = sizeof(ovi);
136         if ( !GetVersionEx(&ovi) ) {
137             errno = toErrno(GetLastError()); 
138             return (-1);
139         }
140         forNT = ((ovi.dwPlatformId & VER_PLATFORM_WIN32_NT) != 0);
141     }
142     
143     if (forNT) {
144         /* Easy, go for MoveFileEx() */
145         if ( MoveFileExA(src, dest, MOVEFILE_REPLACE_EXISTING) ) {
146             return 0;
147         } else {
148             errno = toErrno(GetLastError()); 
149             return (-1);
150         }
151     }
152
153     /* No MoveFileEx() for Win9x, try deleting the target. */
154     /* Similarly, if the MoveFile*() ops didn't work out under NT */
155     if (DeleteFileA(dest)) {
156         if (MoveFileA(src,dest)) {
157             return 0;
158         } else {
159             errno = toErrno(GetLastError());
160             return (-1);
161         }
162     } else {
163         errno = toErrno(GetLastError());
164         return (-1);
165     }
166 #else
167     return rename(src,dest);
168 #endif
169 }
170