2 * (c) The GRASP/AQUA Project, Glasgow University, 1994-1998
4 * $Id: openFile.c,v 1.5 1999/01/23 17:44:40 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
33 openStdFile(fd,flags,rd)
40 if ((fo = malloc(sizeof(IOFileObject))) == NULL)
46 fo->flags = flags | FILEOBJ_STD | ( rd ? FILEOBJ_READ : FILEOBJ_WRITE);
47 fo->connectedTo = NULL;
51 #define OPENFILE_APPEND 0
52 #define OPENFILE_WRITE 1
53 #define OPENFILE_READ_ONLY 2
54 #define OPENFILE_READ_WRITE 3
57 openFile(file, how, binary, flags)
72 * Since we aren't supposed to succeed when we're opening for writing and
73 * there's another writer, we can't just do an open() with O_WRONLY.
78 oflags = O_WRONLY | O_NOCTTY | O_APPEND;
82 oflags = O_WRONLY | O_NOCTTY;
85 case OPENFILE_READ_ONLY:
86 oflags = O_RDONLY | O_NOCTTY;
89 case OPENFILE_READ_WRITE:
90 oflags = O_RDWR | O_NOCTTY;
94 fprintf(stderr, "openFile: unknown mode `%d'\n", how);
103 /* First try to open without creating */
104 while ((fd = open(file, oflags, 0666)) < 0) {
105 if (errno == ENOENT) {
106 if ( how == OPENFILE_READ_ONLY ) {
107 /* For ReadMode, just bail out now */
108 ghc_errtype = ERR_NOSUCHTHING;
109 ghc_errstr = "file does not exist";
112 /* If it is a dangling symlink, break off now, too. */
114 if ( lstat(file,&st) == 0) {
115 ghc_errtype = ERR_NOSUCHTHING;
116 ghc_errstr = "dangling symlink";
120 /* Now try to create it */
121 while ((fd = open(file, oflags | O_CREAT | O_EXCL, 0666)) < 0) {
122 if (errno == EEXIST) {
123 /* Race detected; go back and open without creating it */
125 } else if (errno != EINTR) {
133 ghc_errtype = ERR_NOSUCHTHING;
134 ghc_errstr = "no path to file";
137 ghc_errtype = ERR_PERMISSIONDENIED;
138 ghc_errstr = "unsupported owner or group";
148 } 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";
167 /* Make sure that we aren't looking at a directory */
169 while (fstat(fd, &sb) < 0) {
170 /* highly unlikely */
171 if (errno != EINTR) {
179 if (S_ISDIR(sb.st_mode)) {
180 ghc_errtype = ERR_INAPPROPRIATETYPE;
181 ghc_errstr = "file is a directory";
182 /* We can't have created it in this case. */
187 /* Use our own personal locking */
189 if (lockFile(fd, exclusive) < 0) {
197 ghc_errtype = ERR_RESOURCEBUSY;
198 ghc_errstr = "file is locked";
208 * Write mode is supposed to truncate the file. Unfortunately, our pal
209 * ftruncate() is non-POSIX, so we truncate with a second open, which may fail.
212 if ( how == OPENFILE_WRITE ) {
215 oflags2 = oflags | O_TRUNC;
216 while ((fd2 = open(file, oflags2, 0666)) < 0) {
217 if (errno != EINTR) {
227 ghc_errtype = ERR_RESOURCEBUSY;
228 ghc_errstr = "enforced lock prevents truncation";
231 ghc_errtype = ERR_NOSUCHTHING;
232 ghc_errstr = "no path to file";
235 ghc_errtype = ERR_PERMISSIONDENIED;
236 ghc_errstr = "unsupported owner or group";
245 /* Allocate a IOFileObject to hold the information
246 we need to record per-handle for the various C stubs.
247 This chunk of memory is wrapped up inside a foreign object,
248 so it will be finalised and freed properly when we're
249 through with the handle.
251 if ((fo = malloc(sizeof(IOFileObject))) == NULL)
258 fo->flags = flags | ( (how == OPENFILE_READ_ONLY || how == OPENFILE_READ_WRITE) ? FILEOBJ_READ : 0)
259 | ( (how == OPENFILE_APPEND || how == OPENFILE_READ_WRITE) ? FILEOBJ_WRITE : 0);
260 fo->connectedTo = NULL;
264 /* `Lock' file descriptor and return file object. */
266 openFd(fd,oflags,flags)
275 if (lockFile(fd, exclusive) < 0) {
283 ghc_errtype = ERR_RESOURCEBUSY;
284 ghc_errstr = "file is locked";
290 /* See openFileObject() comment */
291 if ((fo = malloc(sizeof(IOFileObject))) == NULL)
297 fo->flags = flags | ( oflags & O_RDONLY ? FILEOBJ_READ
298 : oflags & O_RDWR ? FILEOBJ_READ
300 | ( oflags & O_WRONLY ? FILEOBJ_WRITE
301 : oflags & O_RDWR ? FILEOBJ_WRITE
303 fo->connectedTo = NULL;