2 * (c) The GRASP/AQUA Project, Glasgow University, 1994-1998
4 * $Id: openFile.c,v 1.11 1999/11/25 16:54:14 simonmar Exp $
6 * openFile Runtime Support
9 /* We use lstat, which is sadly not POSIX */
10 #define NON_POSIX_SOURCE
14 #include "fileObject.h"
16 #ifdef HAVE_SYS_TYPES_H
17 #include <sys/types.h>
20 #ifdef HAVE_SYS_STAT_H
32 #if defined(mingw32_TARGET_OS) && !defined(O_NOCTTY)
44 if ((fo = malloc(sizeof(IOFileObject))) == NULL)
50 fo->flags = FILEOBJ_STD | ( rd ? FILEOBJ_READ : FILEOBJ_WRITE);
51 fo->connectedTo = NULL;
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.
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);
66 #define OPENFILE_APPEND 0
67 #define OPENFILE_WRITE 1
68 #define OPENFILE_READ_ONLY 2
69 #define OPENFILE_READ_WRITE 3
72 openFile(file, how, 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)
287 fo->connectedTo = NULL;
291 /* `Lock' file descriptor and return file object. */
293 openFd(StgInt fd,StgInt oflags,StgInt flags)
299 for_writing = ( ((oflags & O_WRONLY) || (oflags & O_RDWR)) ? 1 : 0);
301 if (lockFile(fd, for_writing, 1/* enforce single-writer */ ) < 0) {
309 ghc_errtype = ERR_RESOURCEBUSY;
310 ghc_errstr = "file is locked";
316 /* See openFileObject() comment */
317 if ((fo = malloc(sizeof(IOFileObject))) == NULL)
324 fo->flags = flags | ( oflags & O_RDONLY ? FILEOBJ_READ
325 : oflags & O_RDWR ? FILEOBJ_READ
327 | ( oflags & O_WRONLY ? FILEOBJ_WRITE
328 : oflags & O_RDWR ? FILEOBJ_WRITE
330 fo->connectedTo = NULL;