2 * (c) The GRASP/AQUA Project, Glasgow University, 1994-1998
4 * $Id: openFile.c,v 1.17 2000/05/11 13:15:38 simonmar Exp $
6 * openFile Runtime Support
9 /* We use lstat, which is sadly not POSIX */
10 #define NON_POSIX_SOURCE
15 #ifdef HAVE_SYS_TYPES_H
16 #include <sys/types.h>
19 #ifdef HAVE_SYS_STAT_H
31 #if defined(mingw32_TARGET_OS) && !defined(O_NOCTTY)
36 openStdFile(StgInt fd, StgInt rd)
41 if ((fo = malloc(sizeof(IOFileObject))) == NULL)
47 fo->flags = FILEOBJ_STD | ( rd ? FILEOBJ_READ : FILEOBJ_WRITE);
48 fo->connectedTo = NULL;
50 #if !defined(_WIN32) || defined(__CYGWIN__) || defined(__CYGWIN32__)
51 /* Set the non-blocking flag on this file descriptor.
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)
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.
61 if (fd != 1 && fd != 2) {
62 fd_flags = fcntl(fd, F_GETFL);
63 fcntl(fd, F_SETFL, fd_flags | O_NONBLOCK);
70 #define OPENFILE_APPEND 0
71 #define OPENFILE_WRITE 1
72 #define OPENFILE_READ_ONLY 2
73 #define OPENFILE_READ_WRITE 3
76 openFile(StgByteArray file, StgInt how, StgInt binary)
86 #if defined(_WIN32) && !(defined(__CYGWIN__) || defined(__CYGWIN32__))
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.
97 oflags = O_WRONLY | O_NOCTTY | O_APPEND;
99 flags |= FILEOBJ_WRITE;
102 oflags = O_WRONLY | O_NOCTTY;
103 flags |= FILEOBJ_WRITE;
106 case OPENFILE_READ_ONLY:
107 oflags = O_RDONLY | O_NOCTTY;
108 flags |= FILEOBJ_READ;
111 case OPENFILE_READ_WRITE:
112 oflags = O_RDWR | O_NOCTTY;
113 flags |= FILEOBJ_READ | FILEOBJ_WRITE;
117 fprintf(stderr, "openFile: unknown mode `%d'\n", how);
124 flags |= FILEOBJ_BINARY;
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";
137 /* If it is a dangling symlink, break off now, too. */
138 #ifndef mingw32_TARGET_OS
140 if ( lstat(file,&st) == 0) {
141 ghc_errtype = ERR_NOSUCHTHING;
142 ghc_errstr = "dangling symlink";
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 */
152 } else if (errno != EINTR) {
160 ghc_errtype = ERR_NOSUCHTHING;
161 ghc_errstr = "no path to file";
164 ghc_errtype = ERR_PERMISSIONDENIED;
165 ghc_errstr = "unsupported owner or group";
175 } else if (errno != EINTR) {
182 ghc_errtype = ERR_NOSUCHTHING;
183 ghc_errstr = "no path to file";
186 ghc_errtype = ERR_PERMISSIONDENIED;
187 ghc_errstr = "unsupported owner or group";
194 /* Set non-blocking mode *after* the open. The reason is that
195 * when reading from a FIFO, if we open in non-blocking mode
196 * then any reads from the FIFO will return EOF straight away
197 * without waiting for a writing process. If we set O_NONBLOCK
198 * after doing the open, then we apparently get to wait for a
199 * writer. This broken behaviour has been observed on both Linux
202 fcntl(fd, F_SETFL, oflags | O_NONBLOCK);
204 /* Make sure that we aren't looking at a directory */
205 while (fstat(fd, &sb) < 0) {
206 /* highly unlikely */
207 if (errno != EINTR) {
215 if (S_ISDIR(sb.st_mode)) {
216 ghc_errtype = ERR_INAPPROPRIATETYPE;
217 ghc_errstr = "file is a directory";
218 /* We can't have created it in this case. */
223 /* Use our own personal locking */
225 if (lockFile(fd, for_writing, 1/*enforce single-writer, if needs be.*/) < 0) {
233 ghc_errtype = ERR_RESOURCEBUSY;
234 ghc_errstr = "file is locked";
244 * Write mode is supposed to truncate the file. Unfortunately, our pal
245 * ftruncate() is non-POSIX, so we truncate with a second open, which may fail.
248 if ( how == OPENFILE_WRITE ) {
251 oflags2 = oflags | O_TRUNC;
252 while ((fd2 = open(file, oflags2, 0666)) < 0) {
253 if (errno != EINTR) {
263 ghc_errtype = ERR_RESOURCEBUSY;
264 ghc_errstr = "enforced lock prevents truncation";
267 ghc_errtype = ERR_NOSUCHTHING;
268 ghc_errstr = "no path to file";
271 ghc_errtype = ERR_PERMISSIONDENIED;
272 ghc_errstr = "unsupported owner or group";
281 /* Allocate a IOFileObject to hold the information
282 we need to record per-handle for the various C stubs.
283 This chunk of memory is wrapped up inside a foreign object,
284 so it will be finalised and freed properly when we're
285 through with the handle.
287 if ((fo = malloc(sizeof(IOFileObject))) == NULL)
295 fo->connectedTo = NULL;
299 /* `Lock' file descriptor and return file object. */
301 openFd(StgInt fd, StgInt oflags, StgInt flags)
306 for_writing = ( ((oflags & O_WRONLY) || (oflags & O_RDWR)) ? 1 : 0);
308 if (lockFile(fd, for_writing, 1/* enforce single-writer */ ) < 0) {
316 ghc_errtype = ERR_RESOURCEBUSY;
317 ghc_errstr = "file is locked";
323 /* See openFileObject() comment */
324 if ((fo = malloc(sizeof(IOFileObject))) == NULL)
330 fo->flags = flags | ( oflags & O_RDONLY ? FILEOBJ_READ
331 : oflags & O_RDWR ? FILEOBJ_READ
333 | ( oflags & O_WRONLY ? FILEOBJ_WRITE
334 : oflags & O_RDWR ? FILEOBJ_WRITE
336 fo->connectedTo = NULL;