[project @ 2005-03-19 02:03:26 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 "ghcconfig.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_HOST_OS
12 #define _POSIX_PTHREAD_SEMANTICS
13 #endif
14
15 #include "HsBase.h"
16
17 #if defined(mingw32_HOST_OS) || defined(__MINGW32__) || defined(_MSC_VER)
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 = (unsigned int)-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   else if (*pDirE == NULL) {
88     // end of stream
89     free(p);
90   }
91   return res;
92 #else
93
94   if (pDirE == NULL) {
95     return -1;
96   }
97
98   *pDirE = readdir((DIR*)dirPtr);
99   if (*pDirE == 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 HsInt
117 __hscore_renameFile( HsAddr src,
118                      HsAddr dest)
119 {
120 #if defined(mingw32_HOST_OS) || defined(__MINGW32__) || defined(_MSC_VER)
121     static int forNT = -1;
122     
123     /* ToDo: propagate error codes back */
124     if (MoveFileA(src, dest)) {
125         return 0;
126     } else {
127         ;
128     }
129     
130     /* Failed...it could be because the target already existed. */
131     if ( !GetFileAttributes(dest) ) {
132         /* No, it's not there - just fail. */
133         errno = toErrno(GetLastError());
134         return (-1);
135     }
136
137     if (forNT == -1) {
138         OSVERSIONINFO ovi;
139         ovi.dwOSVersionInfoSize = sizeof(ovi);
140         if ( !GetVersionEx(&ovi) ) {
141             errno = toErrno(GetLastError()); 
142             return (-1);
143         }
144         forNT = ((ovi.dwPlatformId & VER_PLATFORM_WIN32_NT) != 0);
145     }
146     
147     if (forNT) {
148         /* Easy, go for MoveFileEx() */
149         if ( MoveFileExA(src, dest, MOVEFILE_REPLACE_EXISTING) ) {
150             return 0;
151         } else {
152             errno = toErrno(GetLastError()); 
153             return (-1);
154         }
155     }
156
157     /* No MoveFileEx() for Win9x, try deleting the target. */
158     /* Similarly, if the MoveFile*() ops didn't work out under NT */
159     if (DeleteFileA(dest)) {
160         if (MoveFileA(src,dest)) {
161             return 0;
162         } else {
163             errno = toErrno(GetLastError());
164             return (-1);
165         }
166     } else {
167         errno = toErrno(GetLastError());
168         return (-1);
169     }
170 #else
171     return rename(src,dest);
172 #endif
173 }
174
175 /*
176  * Function: __hscore_getFolderPath()
177  *
178  * Late-bound version of SHGetFolderPath(), coping with OS versions
179  * that have shell32's lacking that particular API.
180  *
181  */
182 #if defined(mingw32_HOST_OS) || defined(__MINGW32__) || defined(_MSC_VER)
183 typedef HRESULT (*HSCORE_GETAPPFOLDERFUNTY)(HWND,int,HANDLE,DWORD,char*);
184 int
185 __hscore_getFolderPath(HWND hwndOwner,
186                        int nFolder,
187                        HANDLE hToken,
188                        DWORD dwFlags,
189                        char*  pszPath)
190 {
191     static int loaded_dll = 0;
192     static HMODULE hMod = (HMODULE)NULL;
193     static HSCORE_GETAPPFOLDERFUNTY funcPtr = NULL;
194     
195     if (loaded_dll < 0) {
196         return (-1);
197     } else if (loaded_dll == 0) {
198         hMod = LoadLibrary("shell32.dll");
199         if (hMod == NULL) {
200             loaded_dll = (-1);
201             return (-1);
202         } else {
203             funcPtr = (HSCORE_GETAPPFOLDERFUNTY)GetProcAddress(hMod, "SHGetFolderPathA");
204             if (!funcPtr) {
205                 loaded_dll = (-1);
206                 return (-1);
207             } else {
208                 loaded_dll = 1;
209             }
210         }
211     }
212     /* OK, if we got this far the function has been bound */
213     return (int)funcPtr(hwndOwner,nFolder,hToken,dwFlags,pszPath);
214     /* ToDo: unload the DLL? */
215 }
216 #endif