[project @ 1999-09-30 12:35:04 by sof]
[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.10 1999/09/30 12:35:04 sof 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 #include "fileObject.h"
15
16 #ifdef HAVE_SYS_TYPES_H
17 #include <sys/types.h>
18 #endif
19
20 #ifdef HAVE_SYS_STAT_H
21 #include <sys/stat.h>
22 #endif
23
24 #ifdef HAVE_UNISTD_H
25 #include <unistd.h>
26 #endif
27
28 #ifdef HAVE_FCNTL_H
29 #include <fcntl.h>
30 #endif
31
32 #if defined(mingw32_TARGET_OS) && !defined(O_NOCTTY)
33 #define O_NOCTTY 0
34 #endif
35
36 IOFileObject*
37 openStdFile(fd,rd)
38 StgInt fd;
39 StgInt rd;
40 {
41     IOFileObject* fo;
42     long fd_flags;
43
44     if ((fo = malloc(sizeof(IOFileObject))) == NULL)
45        return NULL;
46     fo->fd       = fd;
47     fo->buf      = NULL;
48     fo->bufWPtr  = 0;
49     fo->bufRPtr  = 0;
50     fo->flags    = FILEOBJ_STD | ( rd ? FILEOBJ_READ : (FILEOBJ_WRITE | FILEOBJ_FLUSH));
51     fo->connectedTo = NULL;
52  
53     /* MS Win32 CRT doesn't support fcntl() -- the workaround is to
54        start using 'completion ports', but I'm punting on implementing
55        support for using those.
56     */
57 #if !defined(_WIN32) || defined(__CYGWIN__) || defined(__CYGWIN32__)
58     /* set the non-blocking flag on this file descriptor */
59     fd_flags = fcntl(fd, F_GETFL);
60     fcntl(fd, F_SETFL, fd_flags | O_NONBLOCK);
61 #endif
62
63    return fo;
64 }
65
66 #define OPENFILE_APPEND 0
67 #define OPENFILE_WRITE 1
68 #define OPENFILE_READ_ONLY 2
69 #define OPENFILE_READ_WRITE 3
70
71 IOFileObject*
72 openFile(file, how, binary)
73 StgByteArray file;
74 StgInt how;
75 StgInt binary;
76 {
77     FILE *fp;
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 | FILEOBJ_FLUSH;
100         break;
101       case OPENFILE_WRITE:
102         oflags = O_NONBLOCK | O_WRONLY | O_NOCTTY;
103         flags |= FILEOBJ_WRITE | FILEOBJ_FLUSH;
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 | FILEOBJ_FLUSH;
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(fd,oflags,flags)
293 StgInt fd;
294 StgInt oflags;
295 StgInt flags;
296 {
297     int for_writing;
298     FILE* fp;
299     IOFileObject* fo;
300
301     for_writing = ( ((oflags & O_WRONLY) || (oflags & O_RDWR)) ? 1 : 0);
302
303     if (lockFile(fd, for_writing, 1/* enforce single-writer */ ) < 0) {
304         cvtErrno();
305         switch (ghc_errno) {
306         default:
307             stdErrno();
308             break;
309         case GHC_EACCES:
310         case GHC_EAGAIN:
311             ghc_errtype = ERR_RESOURCEBUSY;
312             ghc_errstr = "file is locked";
313             break;
314         }
315         return NULL;
316     }
317
318     /* See openFileObject() comment */
319     if ((fo = malloc(sizeof(IOFileObject))) == NULL)
320        return NULL;
321     fo->fd      = fd;
322     fo->buf     = NULL;
323     fo->bufWPtr = 0;
324     fo->bufRPtr = 0;
325     fo->flags   = flags | ( oflags & O_RDONLY ? FILEOBJ_READ 
326                           : oflags & O_RDWR   ? FILEOBJ_READ 
327                           : 0)
328                         | ( oflags & O_WRONLY ? FILEOBJ_WRITE
329                           : oflags & O_RDWR   ? FILEOBJ_WRITE 
330                           : 0);
331     fo->connectedTo = NULL;
332     return fo;
333 }