2 % (c) The GRASP/AQUA Project, Glasgow University, 1994
4 \subsection[openFile.lc]{openFile Runtime Support}
10 #include "fileObject.h"
12 #ifdef HAVE_SYS_TYPES_H
13 #include <sys/types.h>
16 #ifdef HAVE_SYS_STAT_H
29 openStdFile(fd,flags,rd)
36 if ((fo = malloc(sizeof(IOFileObject))) == NULL)
42 fo->flags = flags | FILEOBJ_STD | ( rd ? FILEOBJ_READ : FILEOBJ_WRITE);
43 fo->connectedTo = NULL;
47 #define OPENFILE_APPEND 0
48 #define OPENFILE_WRITE 1
49 #define OPENFILE_READ_ONLY 2
50 #define OPENFILE_READ_WRITE 3
53 openFile(file, how, binary, flags)
68 * Since we aren't supposed to succeed when we're opening for writing and
69 * there's another writer, we can't just do an open() with O_WRONLY.
74 oflags = O_WRONLY | O_NOCTTY | O_APPEND;
78 oflags = O_WRONLY | O_NOCTTY;
81 case OPENFILE_READ_ONLY:
82 oflags = O_RDONLY | O_NOCTTY;
85 case OPENFILE_READ_WRITE:
86 oflags = O_RDWR | O_NOCTTY;
90 fprintf(stderr, "openFile: unknown mode `%d'\n", how);
99 /* First try to open without creating */
100 while ((fd = open(file, oflags, 0666)) < 0) {
101 if (errno == ENOENT) {
102 if ( how == OPENFILE_READ_ONLY ) {
103 /* For ReadMode, just bail out now */
104 ghc_errtype = ERR_NOSUCHTHING;
105 ghc_errstr = "file does not exist";
108 /* If it is a dangling symlink, break off now, too. */
110 if ( lstat(file,&st) == 0) {
111 ghc_errtype = ERR_NOSUCHTHING;
112 ghc_errstr = "dangling symlink";
116 /* Now try to create it */
117 while ((fd = open(file, oflags | O_CREAT | O_EXCL, 0666)) < 0) {
118 if (errno == EEXIST) {
119 /* Race detected; go back and open without creating it */
121 } else if (errno != EINTR) {
129 ghc_errtype = ERR_NOSUCHTHING;
130 ghc_errstr = "no path to file";
133 ghc_errtype = ERR_PERMISSIONDENIED;
134 ghc_errstr = "unsupported owner or group";
144 } else if (errno != EINTR) {
151 ghc_errtype = ERR_NOSUCHTHING;
152 ghc_errstr = "no path to file";
155 ghc_errtype = ERR_PERMISSIONDENIED;
156 ghc_errstr = "unsupported owner or group";
163 /* Make sure that we aren't looking at a directory */
165 while (fstat(fd, &sb) < 0) {
166 /* highly unlikely */
167 if (errno != EINTR) {
175 if (S_ISDIR(sb.st_mode)) {
176 ghc_errtype = ERR_INAPPROPRIATETYPE;
177 ghc_errstr = "file is a directory";
178 /* We can't have created it in this case. */
183 /* Use our own personal locking */
185 if (lockFile(fd, exclusive) < 0) {
193 ghc_errtype = ERR_RESOURCEBUSY;
194 ghc_errstr = "file is locked";
204 * Write mode is supposed to truncate the file. Unfortunately, our pal
205 * ftruncate() is non-POSIX, so we truncate with a second open, which may fail.
208 if ( how == OPENFILE_WRITE ) {
211 oflags2 = oflags | O_TRUNC;
212 while ((fd2 = open(file, oflags2, 0666)) < 0) {
213 if (errno != EINTR) {
223 ghc_errtype = ERR_RESOURCEBUSY;
224 ghc_errstr = "enforced lock prevents truncation";
227 ghc_errtype = ERR_NOSUCHTHING;
228 ghc_errstr = "no path to file";
231 ghc_errtype = ERR_PERMISSIONDENIED;
232 ghc_errstr = "unsupported owner or group";
241 /* Allocate a IOFileObject to hold the information
242 we need to record per-handle for the various C stubs.
243 This chunk of memory is wrapped up inside a foreign object,
244 so it will be finalised and freed properly when we're
245 through with the handle.
247 if ((fo = malloc(sizeof(IOFileObject))) == NULL)
254 fo->flags = flags | ( (how == OPENFILE_READ_ONLY || how == OPENFILE_READ_WRITE) ? FILEOBJ_READ : 0)
255 | ( (how == OPENFILE_APPEND || how == OPENFILE_READ_WRITE) ? FILEOBJ_WRITE : 0);
256 fo->connectedTo = NULL;
260 /* `Lock' file descriptor and return file object. */
262 openFd(fd,oflags,flags)
271 if (lockFile(fd, exclusive) < 0) {
279 ghc_errtype = ERR_RESOURCEBUSY;
280 ghc_errstr = "file is locked";
286 /* See openFileObject() comment */
287 if ((fo = malloc(sizeof(IOFileObject))) == NULL)
293 fo->flags = flags | ( oflags & O_RDONLY ? FILEOBJ_READ
294 : oflags & O_RDWR ? FILEOBJ_READ
296 | ( oflags & O_WRONLY ? FILEOBJ_WRITE
297 : oflags & O_RDWR ? FILEOBJ_WRITE
299 fo->connectedTo = NULL;