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