2 * (c) The GRASP/AQUA Project, Glasgow University, 1994-1998
4 * $Id: openFile.c,v 1.3 1998/12/02 13:27:44 simonm Exp $
6 * openFile Runtime Support
11 #include "fileObject.h"
13 #ifdef HAVE_SYS_TYPES_H
14 #include <sys/types.h>
17 #ifdef HAVE_SYS_STAT_H
30 openStdFile(fd,flags,rd)
37 if ((fo = malloc(sizeof(IOFileObject))) == NULL)
43 fo->flags = flags | FILEOBJ_STD | ( rd ? FILEOBJ_READ : FILEOBJ_WRITE);
44 fo->connectedTo = NULL;
48 #define OPENFILE_APPEND 0
49 #define OPENFILE_WRITE 1
50 #define OPENFILE_READ_ONLY 2
51 #define OPENFILE_READ_WRITE 3
54 openFile(file, how, binary, flags)
68 #ifdef __CONCURRENT_HASKELL__
69 #warning FixMe: Ignoring bogus bit 7 in openFiles 2nd argument
74 * Since we aren't supposed to succeed when we're opening for writing and
75 * there's another writer, we can't just do an open() with O_WRONLY.
80 oflags = O_WRONLY | O_NOCTTY | O_APPEND;
84 oflags = O_WRONLY | O_NOCTTY;
87 case OPENFILE_READ_ONLY:
88 oflags = O_RDONLY | O_NOCTTY;
91 case OPENFILE_READ_WRITE:
92 oflags = O_RDWR | O_NOCTTY;
96 fprintf(stderr, "openFile: unknown mode `%d'\n", how);
105 /* First try to open without creating */
106 while ((fd = open(file, oflags, 0666)) < 0) {
107 if (errno == ENOENT) {
108 if ( how == OPENFILE_READ_ONLY ) {
109 /* For ReadMode, just bail out now */
110 ghc_errtype = ERR_NOSUCHTHING;
111 ghc_errstr = "file does not exist";
114 /* If it is a dangling symlink, break off now, too. */
116 if ( lstat(file,&st) == 0) {
117 ghc_errtype = ERR_NOSUCHTHING;
118 ghc_errstr = "dangling symlink";
122 /* Now try to create it */
123 while ((fd = open(file, oflags | O_CREAT | O_EXCL, 0666)) < 0) {
124 if (errno == EEXIST) {
125 /* Race detected; go back and open without creating it */
127 } else if (errno != EINTR) {
135 ghc_errtype = ERR_NOSUCHTHING;
136 ghc_errstr = "no path to file";
139 ghc_errtype = ERR_PERMISSIONDENIED;
140 ghc_errstr = "unsupported owner or group";
150 } else if (errno != EINTR) {
157 ghc_errtype = ERR_NOSUCHTHING;
158 ghc_errstr = "no path to file";
161 ghc_errtype = ERR_PERMISSIONDENIED;
162 ghc_errstr = "unsupported owner or group";
169 /* Make sure that we aren't looking at a directory */
171 while (fstat(fd, &sb) < 0) {
172 /* highly unlikely */
173 if (errno != EINTR) {
181 if (S_ISDIR(sb.st_mode)) {
182 ghc_errtype = ERR_INAPPROPRIATETYPE;
183 ghc_errstr = "file is a directory";
184 /* We can't have created it in this case. */
189 /* Use our own personal locking */
191 if (lockFile(fd, exclusive) < 0) {
199 ghc_errtype = ERR_RESOURCEBUSY;
200 ghc_errstr = "file is locked";
210 * Write mode is supposed to truncate the file. Unfortunately, our pal
211 * ftruncate() is non-POSIX, so we truncate with a second open, which may fail.
214 if ( how == OPENFILE_WRITE ) {
217 oflags2 = oflags | O_TRUNC;
218 while ((fd2 = open(file, oflags2, 0666)) < 0) {
219 if (errno != EINTR) {
229 ghc_errtype = ERR_RESOURCEBUSY;
230 ghc_errstr = "enforced lock prevents truncation";
233 ghc_errtype = ERR_NOSUCHTHING;
234 ghc_errstr = "no path to file";
237 ghc_errtype = ERR_PERMISSIONDENIED;
238 ghc_errstr = "unsupported owner or group";
247 /* Allocate a IOFileObject to hold the information
248 we need to record per-handle for the various C stubs.
249 This chunk of memory is wrapped up inside a foreign object,
250 so it will be finalised and freed properly when we're
251 through with the handle.
253 if ((fo = malloc(sizeof(IOFileObject))) == NULL)
260 fo->flags = flags | ( (how == OPENFILE_READ_ONLY || how == OPENFILE_READ_WRITE) ? FILEOBJ_READ : 0)
261 | ( (how == OPENFILE_APPEND || how == OPENFILE_READ_WRITE) ? FILEOBJ_WRITE : 0);
262 fo->connectedTo = NULL;
266 /* `Lock' file descriptor and return file object. */
268 openFd(fd,oflags,flags)
277 if (lockFile(fd, exclusive) < 0) {
285 ghc_errtype = ERR_RESOURCEBUSY;
286 ghc_errstr = "file is locked";
292 /* See openFileObject() comment */
293 if ((fo = malloc(sizeof(IOFileObject))) == NULL)
299 fo->flags = flags | ( oflags & O_RDONLY ? FILEOBJ_READ
300 : oflags & O_RDWR ? FILEOBJ_READ
302 | ( oflags & O_WRONLY ? FILEOBJ_WRITE
303 : oflags & O_RDWR ? FILEOBJ_WRITE
305 fo->connectedTo = NULL;