2 * (c) The GRASP/AQUA Project, Glasgow University, 1994-1998
4 * $Id: openFile.c,v 1.8 1999/09/17 09:38:35 sof 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 #ifdef mingw32_TARGET_OS
37 openStdFile(fd,flags,rd)
45 if ((fo = malloc(sizeof(IOFileObject))) == NULL)
51 fo->flags = flags | FILEOBJ_STD | ( rd ? FILEOBJ_READ : FILEOBJ_WRITE);
52 fo->connectedTo = NULL;
54 /* MS Win32 CRT doesn't support fcntl() -- the workaround is to
55 start using 'completion ports', but I'm punting on implementing
56 support for using those.
58 #if !defined(_WIN32) || defined(__CYGWIN__) || defined(__CYGWIN32__)
59 /* set the non-blocking flag on this file descriptor */
60 fd_flags = fcntl(fd, F_GETFL);
61 fcntl(fd, F_SETFL, fd_flags | O_NONBLOCK);
67 #define OPENFILE_APPEND 0
68 #define OPENFILE_WRITE 1
69 #define OPENFILE_READ_ONLY 2
70 #define OPENFILE_READ_WRITE 3
73 openFile(file, how, binary, flags)
87 #if defined(_WIN32) && !(defined(__CYGWIN__) || defined(__CYGWIN32__))
92 * Since we aren't supposed to succeed when we're opening for writing and
93 * there's another writer, we can't just do an open() with O_WRONLY.
98 oflags = O_NONBLOCK | O_WRONLY | O_NOCTTY | O_APPEND;
102 oflags = O_NONBLOCK | O_WRONLY | O_NOCTTY;
105 case OPENFILE_READ_ONLY:
106 oflags = O_NONBLOCK | O_RDONLY | O_NOCTTY;
109 case OPENFILE_READ_WRITE:
110 oflags = O_NONBLOCK | O_RDWR | O_NOCTTY;
114 fprintf(stderr, "openFile: unknown mode `%d'\n", how);
123 /* First try to open without creating */
124 while ((fd = open(file, oflags, 0666)) < 0) {
125 if (errno == ENOENT) {
126 if ( how == OPENFILE_READ_ONLY ) {
127 /* For ReadMode, just bail out now */
128 ghc_errtype = ERR_NOSUCHTHING;
129 ghc_errstr = "file does not exist";
132 /* If it is a dangling symlink, break off now, too. */
133 #ifndef mingw32_TARGET_OS
135 if ( lstat(file,&st) == 0) {
136 ghc_errtype = ERR_NOSUCHTHING;
137 ghc_errstr = "dangling symlink";
142 /* Now try to create it */
143 while ((fd = open(file, oflags | O_CREAT | O_EXCL, 0666)) < 0) {
144 if (errno == EEXIST) {
145 /* Race detected; go back and open without creating it */
147 } else if (errno != EINTR) {
155 ghc_errtype = ERR_NOSUCHTHING;
156 ghc_errstr = "no path to file";
159 ghc_errtype = ERR_PERMISSIONDENIED;
160 ghc_errstr = "unsupported owner or group";
170 } else if (errno != EINTR) {
177 ghc_errtype = ERR_NOSUCHTHING;
178 ghc_errstr = "no path to file";
181 ghc_errtype = ERR_PERMISSIONDENIED;
182 ghc_errstr = "unsupported owner or group";
189 /* Make sure that we aren't looking at a directory */
191 while (fstat(fd, &sb) < 0) {
192 /* highly unlikely */
193 if (errno != EINTR) {
201 if (S_ISDIR(sb.st_mode)) {
202 ghc_errtype = ERR_INAPPROPRIATETYPE;
203 ghc_errstr = "file is a directory";
204 /* We can't have created it in this case. */
209 /* Use our own personal locking */
211 if (lockFile(fd, for_writing, 1/*enforce single-writer, if needs be.*/) < 0) {
219 ghc_errtype = ERR_RESOURCEBUSY;
220 ghc_errstr = "file is locked";
230 * Write mode is supposed to truncate the file. Unfortunately, our pal
231 * ftruncate() is non-POSIX, so we truncate with a second open, which may fail.
234 if ( how == OPENFILE_WRITE ) {
237 oflags2 = oflags | O_TRUNC;
238 while ((fd2 = open(file, oflags2, 0666)) < 0) {
239 if (errno != EINTR) {
249 ghc_errtype = ERR_RESOURCEBUSY;
250 ghc_errstr = "enforced lock prevents truncation";
253 ghc_errtype = ERR_NOSUCHTHING;
254 ghc_errstr = "no path to file";
257 ghc_errtype = ERR_PERMISSIONDENIED;
258 ghc_errstr = "unsupported owner or group";
267 /* Allocate a IOFileObject to hold the information
268 we need to record per-handle for the various C stubs.
269 This chunk of memory is wrapped up inside a foreign object,
270 so it will be finalised and freed properly when we're
271 through with the handle.
273 if ((fo = malloc(sizeof(IOFileObject))) == NULL)
280 fo->flags = flags | ( (how == OPENFILE_READ_ONLY || how == OPENFILE_READ_WRITE) ? FILEOBJ_READ : 0)
281 | ( (how == OPENFILE_APPEND || how == OPENFILE_READ_WRITE) ? FILEOBJ_WRITE : 0);
282 fo->connectedTo = NULL;
286 /* `Lock' file descriptor and return file object. */
288 openFd(fd,oflags,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;