2 * (c) The GRASP/AQUA Project, Glasgow University, 1994-1998
4 * $Id: openFile.c,v 1.14 1999/12/08 15:47:08 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)
48 fo->flags = FILEOBJ_STD | ( rd ? FILEOBJ_READ : FILEOBJ_WRITE);
49 fo->connectedTo = NULL;
51 /* MS Win32 CRT doesn't support fcntl() -- the workaround is to
52 start using 'completion ports', but I'm punting on implementing
53 support for using those.
55 #if !defined(_WIN32) || defined(__CYGWIN__) || defined(__CYGWIN32__)
56 /* set the non-blocking flag on this file descriptor */
57 fd_flags = fcntl(fd, F_GETFL);
58 fcntl(fd, F_SETFL, fd_flags | O_NONBLOCK);
64 #define OPENFILE_APPEND 0
65 #define OPENFILE_WRITE 1
66 #define OPENFILE_READ_ONLY 2
67 #define OPENFILE_READ_WRITE 3
70 openFile(StgByteArray file, StgInt how, StgInt binary)
80 #if defined(_WIN32) && !(defined(__CYGWIN__) || defined(__CYGWIN32__))
85 * Since we aren't supposed to succeed when we're opening for writing and
86 * there's another writer, we can't just do an open() with O_WRONLY.
91 oflags = O_NONBLOCK | O_WRONLY | O_NOCTTY | O_APPEND;
93 flags |= FILEOBJ_WRITE;
96 oflags = O_NONBLOCK | O_WRONLY | O_NOCTTY;
97 flags |= FILEOBJ_WRITE;
100 case OPENFILE_READ_ONLY:
101 oflags = O_NONBLOCK | O_RDONLY | O_NOCTTY;
102 flags |= FILEOBJ_READ;
105 case OPENFILE_READ_WRITE:
106 oflags = O_NONBLOCK | O_RDWR | O_NOCTTY;
107 flags |= FILEOBJ_READ | FILEOBJ_WRITE;
111 fprintf(stderr, "openFile: unknown mode `%d'\n", how);
118 flags |= FILEOBJ_BINARY;
122 /* First try to open without creating */
123 while ((fd = open(file, oflags, 0666)) < 0) {
124 if (errno == ENOENT) {
125 if ( how == OPENFILE_READ_ONLY ) {
126 /* For ReadMode, just bail out now */
127 ghc_errtype = ERR_NOSUCHTHING;
128 ghc_errstr = "file does not exist";
131 /* If it is a dangling symlink, break off now, too. */
132 #ifndef mingw32_TARGET_OS
134 if ( lstat(file,&st) == 0) {
135 ghc_errtype = ERR_NOSUCHTHING;
136 ghc_errstr = "dangling symlink";
141 /* Now try to create it */
142 while ((fd = open(file, oflags | O_CREAT | O_EXCL, 0666)) < 0) {
143 if (errno == EEXIST) {
144 /* Race detected; go back and open without creating it */
146 } else if (errno != EINTR) {
154 ghc_errtype = ERR_NOSUCHTHING;
155 ghc_errstr = "no path to file";
158 ghc_errtype = ERR_PERMISSIONDENIED;
159 ghc_errstr = "unsupported owner or group";
169 } else if (errno != EINTR) {
176 ghc_errtype = ERR_NOSUCHTHING;
177 ghc_errstr = "no path to file";
180 ghc_errtype = ERR_PERMISSIONDENIED;
181 ghc_errstr = "unsupported owner or group";
188 /* Make sure that we aren't looking at a directory */
190 while (fstat(fd, &sb) < 0) {
191 /* highly unlikely */
192 if (errno != EINTR) {
200 if (S_ISDIR(sb.st_mode)) {
201 ghc_errtype = ERR_INAPPROPRIATETYPE;
202 ghc_errstr = "file is a directory";
203 /* We can't have created it in this case. */
208 /* Use our own personal locking */
210 if (lockFile(fd, for_writing, 1/*enforce single-writer, if needs be.*/) < 0) {
218 ghc_errtype = ERR_RESOURCEBUSY;
219 ghc_errstr = "file is locked";
229 * Write mode is supposed to truncate the file. Unfortunately, our pal
230 * ftruncate() is non-POSIX, so we truncate with a second open, which may fail.
233 if ( how == OPENFILE_WRITE ) {
236 oflags2 = oflags | O_TRUNC;
237 while ((fd2 = open(file, oflags2, 0666)) < 0) {
238 if (errno != EINTR) {
248 ghc_errtype = ERR_RESOURCEBUSY;
249 ghc_errstr = "enforced lock prevents truncation";
252 ghc_errtype = ERR_NOSUCHTHING;
253 ghc_errstr = "no path to file";
256 ghc_errtype = ERR_PERMISSIONDENIED;
257 ghc_errstr = "unsupported owner or group";
266 /* Allocate a IOFileObject to hold the information
267 we need to record per-handle for the various C stubs.
268 This chunk of memory is wrapped up inside a foreign object,
269 so it will be finalised and freed properly when we're
270 through with the handle.
272 if ((fo = malloc(sizeof(IOFileObject))) == NULL)
281 fo->connectedTo = NULL;
285 /* `Lock' file descriptor and return file object. */
287 openFd(StgInt fd, StgInt oflags, StgInt flags)
292 for_writing = ( ((oflags & O_WRONLY) || (oflags & O_RDWR)) ? 1 : 0);
294 if (lockFile(fd, for_writing, 1/* enforce single-writer */ ) < 0) {
302 ghc_errtype = ERR_RESOURCEBUSY;
303 ghc_errstr = "file is locked";
309 /* See openFileObject() comment */
310 if ((fo = malloc(sizeof(IOFileObject))) == NULL)
317 fo->flags = flags | ( oflags & O_RDONLY ? FILEOBJ_READ
318 : oflags & O_RDWR ? FILEOBJ_READ
320 | ( oflags & O_WRONLY ? FILEOBJ_WRITE
321 : oflags & O_RDWR ? FILEOBJ_WRITE
323 fo->connectedTo = NULL;