[project @ 2001-05-18 14:18:34 by simonmar]
[ghc-hetmet.git] / ghc / lib / std / cbits / openFile.c
1 /* 
2  * (c) The GRASP/AQUA Project, Glasgow University, 1994-1998
3  *
4  * $Id: openFile.c,v 1.20 2001/04/02 16:10:33 rrt Exp $
5  *
6  * openFile Runtime Support
7  */
8
9 /* We use lstat, which is sadly not POSIX */
10 #define NON_POSIX_SOURCE
11
12 #include "Rts.h"
13 #include "stgio.h"
14
15 #ifdef HAVE_SYS_TYPES_H
16 #include <sys/types.h>
17 #endif
18
19 #ifdef HAVE_SYS_STAT_H
20 #include <sys/stat.h>
21 #endif
22
23 #ifdef HAVE_UNISTD_H
24 #include <unistd.h>
25 #endif
26
27 #ifdef HAVE_FCNTL_H
28 #include <fcntl.h>
29 #endif
30
31 #if defined(mingw32_TARGET_OS) && !defined(O_NOCTTY)
32 #define O_NOCTTY 0
33 #endif
34
35 IOFileObject*
36 openStdFile(StgInt fd, StgInt rd)
37 {
38     IOFileObject* fo;
39     long fd_flags;
40
41     if ((fo = malloc(sizeof(IOFileObject))) == NULL)
42        return NULL;
43     fo->fd       = fd;
44     fo->buf      = NULL;
45     fo->bufWPtr  = 0;
46     fo->bufRPtr  = 0;
47     fo->flags    = FILEOBJ_STD | ( rd ? FILEOBJ_READ : FILEOBJ_WRITE);
48     fo->connectedTo = NULL;
49  
50 #if !defined(_WIN32) || defined(__CYGWIN__) || defined(__CYGWIN32__)
51     /* Set the non-blocking flag on this file descriptor.
52      *
53      * Don't do it for stdout and stderr: some shells (actually most)
54      * don't reset the nonblocking flag after running a program, and
55      * this causes all sorts of problems.  --SDM (12/99)
56      *
57      * MS Win32 CRT doesn't support fcntl() -- the workaround is to
58      * start using 'completion ports', but I'm punting on implementing
59      * support for using those.
60      */
61     if (fd != 1 && fd != 2) {
62       fd_flags = fcntl(fd, F_GETFL);
63       fcntl(fd, F_SETFL, fd_flags | O_NONBLOCK);
64     }
65 #endif
66
67    return fo;
68 }
69
70 #define OPENFILE_APPEND 0
71 #define OPENFILE_WRITE 1
72 #define OPENFILE_READ_ONLY 2
73 #define OPENFILE_READ_WRITE 3
74
75 IOFileObject*
76 openFile(StgByteArray file, StgInt how, StgInt binary)
77 {
78     int fd;
79     int oflags;
80     int for_writing;
81     int created = 0;
82     struct stat sb;
83     IOFileObject* fo;
84     int flags = 0;
85
86 #if defined(_WIN32) && !(defined(__CYGWIN__) || defined(__CYGWIN32__))
87 #define O_NONBLOCK 0
88 #endif
89
90     /*
91      * Since we aren't supposed to succeed when we're opening for writing and
92      * there's another writer, we can't just do an open() with O_WRONLY.
93      */
94
95     switch (how) {
96       case OPENFILE_APPEND:
97         oflags = O_NONBLOCK | O_WRONLY | O_NOCTTY | O_APPEND; 
98         for_writing = 1;
99         flags |= FILEOBJ_WRITE;
100         break;
101       case OPENFILE_WRITE:
102         oflags = O_NONBLOCK | O_WRONLY | O_NOCTTY;
103         flags |= FILEOBJ_WRITE;
104         for_writing = 1;
105         break;
106     case OPENFILE_READ_ONLY:
107         oflags = O_NONBLOCK | O_RDONLY | O_NOCTTY;
108         flags |= FILEOBJ_READ;
109         for_writing = 0;
110         break;
111     case OPENFILE_READ_WRITE:
112         oflags = O_NONBLOCK | O_RDWR | O_NOCTTY;
113         flags |= FILEOBJ_READ | FILEOBJ_WRITE;
114         for_writing = 1;
115         break;
116     default:
117         fprintf(stderr, "openFile: unknown mode `%d'\n", how);
118         exit(EXIT_FAILURE);
119     }
120
121 #if HAVE_O_BINARY
122     if (binary) {
123       oflags |= O_BINARY;
124       flags  |= FILEOBJ_BINARY;
125     }
126 #endif
127
128     /* First try to open without creating */
129     while ((fd = open(file, oflags, 0666)) < 0) {
130         if (errno == ENOENT) {
131             if ( how == OPENFILE_READ_ONLY ) {
132                 /* For ReadMode, just bail out now */
133                 ghc_errtype = ERR_NOSUCHTHING;
134                 ghc_errstr = "file does not exist";
135                 return NULL;
136             } else {
137                 /* If it is a dangling symlink, break off now, too. */
138 #ifndef mingw32_TARGET_OS
139                 struct stat st;
140                 if ( lstat(file,&st) == 0) {
141                    ghc_errtype = ERR_NOSUCHTHING;
142                    ghc_errstr = "dangling symlink";
143                    return NULL;
144                 }
145 #endif
146             }
147             /* Now try to create it */
148             while ((fd = open(file, oflags | O_CREAT | O_EXCL, 0666)) < 0) {
149                 if (errno == EEXIST) {
150                     /* Race detected; go back and open without creating it */
151                     break;
152                 } else if (errno != EINTR) {
153                     cvtErrno();
154                     switch (ghc_errno) {
155                     default:
156                         stdErrno();
157                         break;
158                     case GHC_ENOENT:
159                     case GHC_ENOTDIR:
160                         ghc_errtype = ERR_NOSUCHTHING;
161                         ghc_errstr = "no path to file";
162                         break;
163                     case GHC_EINVAL:
164                         ghc_errtype = ERR_PERMISSIONDENIED;
165                         ghc_errstr = "unsupported owner or group";
166                         break;
167                     }
168                     return NULL;
169                 }
170             }
171             if (fd >= 0) {
172                 created = 1;
173                 break;
174             }
175         } else if (errno != EINTR) {
176             cvtErrno();
177             switch (ghc_errno) {
178             default:
179                 stdErrno();
180                 break;
181             case GHC_ENOTDIR:
182                 ghc_errtype = ERR_NOSUCHTHING;
183                 ghc_errstr = "no path to file";
184                 break;
185             case GHC_EINVAL:
186                 ghc_errtype = ERR_PERMISSIONDENIED;
187                 ghc_errstr = "unsupported owner or group";
188                 break;
189             }
190             return NULL;
191         }
192     }
193
194     /* Make sure that we aren't looking at a directory */
195
196     while (fstat(fd, &sb) < 0) {
197         /* highly unlikely */
198         if (errno != EINTR) {
199             cvtErrno();
200             if (created)
201                 (void) unlink(file);
202             (void) close(fd);
203             return NULL;
204         }
205     }
206     if (S_ISDIR(sb.st_mode)) {
207         ghc_errtype = ERR_INAPPROPRIATETYPE;
208         ghc_errstr = "file is a directory";
209         /* We can't have created it in this case. */
210         (void) close(fd);
211
212         return NULL;
213     }
214     /* Use our own personal locking */
215
216     if (lockFile(fd, for_writing, 1/*enforce single-writer, if needs be.*/) < 0) {
217         cvtErrno();
218         switch (ghc_errno) {
219         default:
220             stdErrno();
221             break;
222         case GHC_EACCES:
223         case GHC_EAGAIN:
224             ghc_errtype = ERR_RESOURCEBUSY;
225             ghc_errstr = "file is locked";
226             break;
227         }
228         if (created)
229             (void) unlink(file);
230         (void) close(fd);
231         return NULL;
232     }
233
234     /*
235      * Write mode is supposed to truncate the file.  Unfortunately, our pal
236      * ftruncate() is non-POSIX, so we truncate with a second open, which may fail.
237      */
238
239     if ( how == OPENFILE_WRITE ) {
240         int fd2, oflags2;
241
242         oflags2 = oflags | O_TRUNC;
243         while ((fd2 = open(file, oflags2, 0666)) < 0) {
244             if (errno != EINTR) {
245                 cvtErrno();
246                 if (created)
247                     (void) unlink(file);
248                 (void) close(fd);
249                 switch (ghc_errno) {
250                 default:
251                     stdErrno();
252                     break;
253                 case GHC_EAGAIN:
254                     ghc_errtype = ERR_RESOURCEBUSY;
255                     ghc_errstr = "enforced lock prevents truncation";
256                     break;
257                 case GHC_ENOTDIR:
258                     ghc_errtype = ERR_NOSUCHTHING;
259                     ghc_errstr = "no path to file";
260                     break;
261                 case GHC_EINVAL:
262                     ghc_errtype = ERR_PERMISSIONDENIED;
263                     ghc_errstr = "unsupported owner or group";
264                     break;
265                 }
266                 return NULL;
267             }
268         }
269         close(fd2);
270     }
271
272     /* Allocate a IOFileObject to hold the information
273        we need to record per-handle for the various C stubs.
274        This chunk of memory is wrapped up inside a foreign object,
275        so it will be finalised and freed properly when we're
276        through with the handle.
277     */
278     if ((fo = malloc(sizeof(IOFileObject))) == NULL)
279        return NULL;
280
281     fo->fd       = fd;
282     fo->buf      = NULL;
283     fo->bufWPtr  = 0;
284     fo->bufRPtr  = 0;
285     fo->flags    = flags;
286     fo->connectedTo = NULL;
287     return fo;
288 }
289
290 /* `Lock' file descriptor and return file object. */
291 IOFileObject*
292 openFd(StgInt fd, StgInt oflags, StgInt flags)
293 {
294     int for_writing;
295     IOFileObject* fo;
296
297     for_writing = ( ((oflags & O_WRONLY) || (oflags & O_RDWR)) ? 1 : 0);
298
299     if (lockFile(fd, for_writing, 1/* enforce single-writer */ ) < 0) {
300         cvtErrno();
301         switch (ghc_errno) {
302         default:
303             stdErrno();
304             break;
305         case GHC_EACCES:
306         case GHC_EAGAIN:
307             ghc_errtype = ERR_RESOURCEBUSY;
308             ghc_errstr = "file is locked";
309             break;
310         }
311         return NULL;
312     }
313
314     /* See openFileObject() comment */
315     if ((fo = malloc(sizeof(IOFileObject))) == NULL)
316        return NULL;
317     fo->fd       = fd;
318     fo->buf      = NULL;
319     fo->bufWPtr  = 0;
320     fo->bufRPtr  = 0;
321     fo->flags    = flags | ( oflags & O_RDONLY ? FILEOBJ_READ 
322                           : oflags & O_RDWR   ? FILEOBJ_READ 
323                           : 0)
324                         | ( oflags & O_WRONLY ? FILEOBJ_WRITE
325                           : oflags & O_RDWR   ? FILEOBJ_WRITE 
326                           : 0);
327     fo->connectedTo = NULL;
328     return fo;
329 }