2 * (c) The GRASP/AQUA Project, Glasgow University, 1994-1998
4 * $Id: openFile.c,v 1.12 1999/11/26 16:25:56 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)
43 if ((fo = malloc(sizeof(IOFileObject))) == NULL)
49 fo->flags = FILEOBJ_STD | ( rd ? FILEOBJ_READ : FILEOBJ_WRITE);
50 fo->connectedTo = NULL;
52 /* MS Win32 CRT doesn't support fcntl() -- the workaround is to
53 start using 'completion ports', but I'm punting on implementing
54 support for using those.
56 #if !defined(_WIN32) || defined(__CYGWIN__) || defined(__CYGWIN32__)
57 /* set the non-blocking flag on this file descriptor */
58 fd_flags = fcntl(fd, F_GETFL);
59 fcntl(fd, F_SETFL, fd_flags | O_NONBLOCK);
65 #define OPENFILE_APPEND 0
66 #define OPENFILE_WRITE 1
67 #define OPENFILE_READ_ONLY 2
68 #define OPENFILE_READ_WRITE 3
71 openFile(file, how, binary)
85 #if defined(_WIN32) && !(defined(__CYGWIN__) || defined(__CYGWIN32__))
90 * Since we aren't supposed to succeed when we're opening for writing and
91 * there's another writer, we can't just do an open() with O_WRONLY.
96 oflags = O_NONBLOCK | O_WRONLY | O_NOCTTY | O_APPEND;
98 flags |= FILEOBJ_WRITE;
101 oflags = O_NONBLOCK | O_WRONLY | O_NOCTTY;
102 flags |= FILEOBJ_WRITE;
105 case OPENFILE_READ_ONLY:
106 oflags = O_NONBLOCK | O_RDONLY | O_NOCTTY;
107 flags |= FILEOBJ_READ;
110 case OPENFILE_READ_WRITE:
111 oflags = O_NONBLOCK | O_RDWR | O_NOCTTY;
112 flags |= FILEOBJ_READ | FILEOBJ_WRITE;
116 fprintf(stderr, "openFile: unknown mode `%d'\n", how);
123 flags |= FILEOBJ_BINARY;
127 /* First try to open without creating */
128 while ((fd = open(file, oflags, 0666)) < 0) {
129 if (errno == ENOENT) {
130 if ( how == OPENFILE_READ_ONLY ) {
131 /* For ReadMode, just bail out now */
132 ghc_errtype = ERR_NOSUCHTHING;
133 ghc_errstr = "file does not exist";
136 /* If it is a dangling symlink, break off now, too. */
137 #ifndef mingw32_TARGET_OS
139 if ( lstat(file,&st) == 0) {
140 ghc_errtype = ERR_NOSUCHTHING;
141 ghc_errstr = "dangling symlink";
146 /* Now try to create it */
147 while ((fd = open(file, oflags | O_CREAT | O_EXCL, 0666)) < 0) {
148 if (errno == EEXIST) {
149 /* Race detected; go back and open without creating it */
151 } else if (errno != EINTR) {
159 ghc_errtype = ERR_NOSUCHTHING;
160 ghc_errstr = "no path to file";
163 ghc_errtype = ERR_PERMISSIONDENIED;
164 ghc_errstr = "unsupported owner or group";
174 } else if (errno != EINTR) {
181 ghc_errtype = ERR_NOSUCHTHING;
182 ghc_errstr = "no path to file";
185 ghc_errtype = ERR_PERMISSIONDENIED;
186 ghc_errstr = "unsupported owner or group";
193 /* Make sure that we aren't looking at a directory */
195 while (fstat(fd, &sb) < 0) {
196 /* highly unlikely */
197 if (errno != EINTR) {
205 if (S_ISDIR(sb.st_mode)) {
206 ghc_errtype = ERR_INAPPROPRIATETYPE;
207 ghc_errstr = "file is a directory";
208 /* We can't have created it in this case. */
213 /* Use our own personal locking */
215 if (lockFile(fd, for_writing, 1/*enforce single-writer, if needs be.*/) < 0) {
223 ghc_errtype = ERR_RESOURCEBUSY;
224 ghc_errstr = "file is locked";
234 * Write mode is supposed to truncate the file. Unfortunately, our pal
235 * ftruncate() is non-POSIX, so we truncate with a second open, which may fail.
238 if ( how == OPENFILE_WRITE ) {
241 oflags2 = oflags | O_TRUNC;
242 while ((fd2 = open(file, oflags2, 0666)) < 0) {
243 if (errno != EINTR) {
253 ghc_errtype = ERR_RESOURCEBUSY;
254 ghc_errstr = "enforced lock prevents truncation";
257 ghc_errtype = ERR_NOSUCHTHING;
258 ghc_errstr = "no path to file";
261 ghc_errtype = ERR_PERMISSIONDENIED;
262 ghc_errstr = "unsupported owner or group";
271 /* Allocate a IOFileObject to hold the information
272 we need to record per-handle for the various C stubs.
273 This chunk of memory is wrapped up inside a foreign object,
274 so it will be finalised and freed properly when we're
275 through with the handle.
277 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)
298 for_writing = ( ((oflags & O_WRONLY) || (oflags & O_RDWR)) ? 1 : 0);
300 if (lockFile(fd, for_writing, 1/* enforce single-writer */ ) < 0) {
308 ghc_errtype = ERR_RESOURCEBUSY;
309 ghc_errstr = "file is locked";
315 /* See openFileObject() comment */
316 if ((fo = malloc(sizeof(IOFileObject))) == NULL)
323 fo->flags = flags | ( oflags & O_RDONLY ? FILEOBJ_READ
324 : oflags & O_RDWR ? FILEOBJ_READ
326 | ( oflags & O_WRONLY ? FILEOBJ_WRITE
327 : oflags & O_RDWR ? FILEOBJ_WRITE
329 fo->connectedTo = NULL;