2 * (c) The GRASP/AQUA Project, Glasgow University, 1994-1998
4 * $Id: openFile.c,v 1.18 2000/05/15 09:20:11 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_NONBLOCK | O_WRONLY | O_NOCTTY | O_APPEND;
99 flags |= FILEOBJ_WRITE;
102 oflags = O_NONBLOCK | O_WRONLY | O_NOCTTY;
103 flags |= FILEOBJ_WRITE;
106 case OPENFILE_READ_ONLY:
107 oflags = O_NONBLOCK | O_RDONLY | O_NOCTTY;
108 flags |= FILEOBJ_READ;
111 case OPENFILE_READ_WRITE:
112 oflags = O_NONBLOCK | 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 /* Make sure that we aren't looking at a directory */
196 while (fstat(fd, &sb) < 0) {
197 /* highly unlikely */
198 if (errno != EINTR) {
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. */
214 /* Use our own personal locking */
216 if (lockFile(fd, for_writing, 1/*enforce single-writer, if needs be.*/) < 0) {
224 ghc_errtype = ERR_RESOURCEBUSY;
225 ghc_errstr = "file is locked";
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.
239 if ( how == OPENFILE_WRITE ) {
242 oflags2 = oflags | O_TRUNC;
243 while ((fd2 = open(file, oflags2, 0666)) < 0) {
244 if (errno != EINTR) {
254 ghc_errtype = ERR_RESOURCEBUSY;
255 ghc_errstr = "enforced lock prevents truncation";
258 ghc_errtype = ERR_NOSUCHTHING;
259 ghc_errstr = "no path to file";
262 ghc_errtype = ERR_PERMISSIONDENIED;
263 ghc_errstr = "unsupported owner or group";
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.
278 if ((fo = malloc(sizeof(IOFileObject))) == NULL)
286 fo->connectedTo = NULL;
290 /* `Lock' file descriptor and return file object. */
292 openFd(StgInt fd, StgInt oflags, StgInt flags)
297 for_writing = ( ((oflags & O_WRONLY) || (oflags & O_RDWR)) ? 1 : 0);
299 if (lockFile(fd, for_writing, 1/* enforce single-writer */ ) < 0) {
307 ghc_errtype = ERR_RESOURCEBUSY;
308 ghc_errstr = "file is locked";
314 /* See openFileObject() comment */
315 if ((fo = malloc(sizeof(IOFileObject))) == NULL)
321 fo->flags = flags | ( oflags & O_RDONLY ? FILEOBJ_READ
322 : oflags & O_RDWR ? FILEOBJ_READ
324 | ( oflags & O_WRONLY ? FILEOBJ_WRITE
325 : oflags & O_RDWR ? FILEOBJ_WRITE
327 fo->connectedTo = NULL;