2 * (c) The GRASP/AQUA Project, Glasgow University, 1994-1998
4 * $Id: openFile.c,v 1.7 1999/09/16 13:14:43 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 #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 /* set the non-blocking flag on this file descriptor */
55 fd_flags = fcntl(fd, F_GETFL);
56 fcntl(fd, F_SETFL, fd_flags | O_NONBLOCK);
61 #define OPENFILE_APPEND 0
62 #define OPENFILE_WRITE 1
63 #define OPENFILE_READ_ONLY 2
64 #define OPENFILE_READ_WRITE 3
67 openFile(file, how, binary, flags)
82 * Since we aren't supposed to succeed when we're opening for writing and
83 * there's another writer, we can't just do an open() with O_WRONLY.
88 oflags = O_NONBLOCK | O_WRONLY | O_NOCTTY | O_APPEND;
92 oflags = O_NONBLOCK | O_WRONLY | O_NOCTTY;
95 case OPENFILE_READ_ONLY:
96 oflags = O_NONBLOCK | O_RDONLY | O_NOCTTY;
99 case OPENFILE_READ_WRITE:
100 oflags = O_NONBLOCK | O_RDWR | O_NOCTTY;
104 fprintf(stderr, "openFile: unknown mode `%d'\n", how);
113 /* First try to open without creating */
114 while ((fd = open(file, oflags, 0666)) < 0) {
115 if (errno == ENOENT) {
116 if ( how == OPENFILE_READ_ONLY ) {
117 /* For ReadMode, just bail out now */
118 ghc_errtype = ERR_NOSUCHTHING;
119 ghc_errstr = "file does not exist";
122 /* If it is a dangling symlink, break off now, too. */
123 #ifndef mingw32_TARGET_OS
125 if ( lstat(file,&st) == 0) {
126 ghc_errtype = ERR_NOSUCHTHING;
127 ghc_errstr = "dangling symlink";
132 /* Now try to create it */
133 while ((fd = open(file, oflags | O_CREAT | O_EXCL, 0666)) < 0) {
134 if (errno == EEXIST) {
135 /* Race detected; go back and open without creating it */
137 } else if (errno != EINTR) {
145 ghc_errtype = ERR_NOSUCHTHING;
146 ghc_errstr = "no path to file";
149 ghc_errtype = ERR_PERMISSIONDENIED;
150 ghc_errstr = "unsupported owner or group";
160 } else if (errno != EINTR) {
167 ghc_errtype = ERR_NOSUCHTHING;
168 ghc_errstr = "no path to file";
171 ghc_errtype = ERR_PERMISSIONDENIED;
172 ghc_errstr = "unsupported owner or group";
179 /* Make sure that we aren't looking at a directory */
181 while (fstat(fd, &sb) < 0) {
182 /* highly unlikely */
183 if (errno != EINTR) {
191 if (S_ISDIR(sb.st_mode)) {
192 ghc_errtype = ERR_INAPPROPRIATETYPE;
193 ghc_errstr = "file is a directory";
194 /* We can't have created it in this case. */
199 /* Use our own personal locking */
201 if (lockFile(fd, for_writing, 1/*enforce single-writer, if needs be.*/) < 0) {
209 ghc_errtype = ERR_RESOURCEBUSY;
210 ghc_errstr = "file is locked";
220 * Write mode is supposed to truncate the file. Unfortunately, our pal
221 * ftruncate() is non-POSIX, so we truncate with a second open, which may fail.
224 if ( how == OPENFILE_WRITE ) {
227 oflags2 = oflags | O_TRUNC;
228 while ((fd2 = open(file, oflags2, 0666)) < 0) {
229 if (errno != EINTR) {
239 ghc_errtype = ERR_RESOURCEBUSY;
240 ghc_errstr = "enforced lock prevents truncation";
243 ghc_errtype = ERR_NOSUCHTHING;
244 ghc_errstr = "no path to file";
247 ghc_errtype = ERR_PERMISSIONDENIED;
248 ghc_errstr = "unsupported owner or group";
257 /* Allocate a IOFileObject to hold the information
258 we need to record per-handle for the various C stubs.
259 This chunk of memory is wrapped up inside a foreign object,
260 so it will be finalised and freed properly when we're
261 through with the handle.
263 if ((fo = malloc(sizeof(IOFileObject))) == NULL)
270 fo->flags = flags | ( (how == OPENFILE_READ_ONLY || how == OPENFILE_READ_WRITE) ? FILEOBJ_READ : 0)
271 | ( (how == OPENFILE_APPEND || how == OPENFILE_READ_WRITE) ? FILEOBJ_WRITE : 0);
272 fo->connectedTo = NULL;
276 /* `Lock' file descriptor and return file object. */
278 openFd(fd,oflags,flags)
287 for_writing = ( ((oflags & O_WRONLY) || (oflags & O_RDWR)) ? 1 : 0);
289 if (lockFile(fd, for_writing, 1/* enforce single-writer */ ) < 0) {
297 ghc_errtype = ERR_RESOURCEBUSY;
298 ghc_errstr = "file is locked";
304 /* See openFileObject() comment */
305 if ((fo = malloc(sizeof(IOFileObject))) == NULL)
311 fo->flags = flags | ( oflags & O_RDONLY ? FILEOBJ_READ
312 : oflags & O_RDWR ? FILEOBJ_READ
314 | ( oflags & O_WRONLY ? FILEOBJ_WRITE
315 : oflags & O_RDWR ? FILEOBJ_WRITE
317 fo->connectedTo = NULL;