ref: 765c1cc184b74e5f3fa52281079afb4a17405227
parent: 44782f552869c57f38876fdeafcce9de167df6c3
author: Sigrid Solveig Haflínudóttir <ftrvxmtrx@gmail.com>
date: Mon Jan 25 20:19:37 EST 2021
9pex: fix up modes, prepare to add writing support
--- a/9pex.c
+++ b/9pex.c
@@ -1,4 +1,8 @@
+#ifdef __linux__
+#define _GNU_SOURCE
+#else
#define _DEFAULT_SOURCE
+#endif
#define _FILE_OFFSET_BITS 64
#include <dirent.h>
#include <errno.h>
@@ -422,11 +426,39 @@
}
static int
+mode2unix(C9mode mode, int qidtype)
+{
+ int omode;
+
+ omode = O_NOFOLLOW;
+ if ((mode & 3) == C9read)
+ omode |= O_RDONLY;
+ else if ((mode & 3) == C9write)
+ omode |= O_WRONLY;
+ else if ((mode & 3) == C9rdwr)
+ omode |= O_RDWR;
+ if (mode & C9trunc)
+ omode |= O_TRUNC;
+#ifdef __linux__
+ if (mode & C9rclose)
+ omode |= O_TMPFILE;
+#endif
+ if ((qidtype & C9qtappend) != 0)
+ omode |= O_APPEND;
+
+ return omode;
+}
+
+static int
openfid(Fid *f, C9mode mode, char **err)
{
struct stat st;
int omode;
+ if (stat(f->path, &st) != 0 || !hasperm(&st, mode, err))
+ goto error;
+ stat2qid(&st, &f->qid, &f->iounit);
+
if ((f->qid.type & C9qtdir) != 0) {
if ((f->dir = opendir(f->path)) == NULL) {
*err = strerror(errno);
@@ -434,29 +466,27 @@
}
f->fd = dirfd(f->dir);
} else {
- omode = O_RDONLY;
- if ((f->qid.type & C9qtappend) != 0)
- omode |= O_APPEND;
+ omode = mode2unix(mode, f->qid.type);
f->fd = open(f->path, omode);
}
- if (f->fd < 0 || fstat(f->fd, &st) != 0 || !hasperm(&st, mode, err)) {
- if (*err == NULL)
- *err = strerror(errno);
-
- if (f->dir != NULL)
- closedir(f->dir);
- else if (f->fd >= 0)
- close(f->fd);
-
- f->dir = NULL;
- f->fd = -1;
- return -1;
- }
- stat2qid(&st, &f->qid, &f->iounit);
+ if (f->fd < 0)
+ goto error;
f->mode = mode;
return 0;
+error:
+ if (*err == NULL)
+ *err = strerror(errno);
+
+ if (f->dir != NULL)
+ closedir(f->dir);
+ else if (f->fd >= 0)
+ close(f->fd);
+
+ f->dir = NULL;
+ f->fd = -1;
+ return -1;
}
static char *