shithub: 9pro

Download patch

ref: fdd8f1021298e7080e943a4bdbfc4a84952d9c0b
parent: c0a474b6bf77fa260be4b9dbf6ec0e35a5121ea6
parent: 765c1cc184b74e5f3fa52281079afb4a17405227
author: Sigrid Solveig Haflínudóttir <ftrvxmtrx@gmail.com>
date: Fri Jan 29 21:56:52 EST 2021

Merge branch 'master' of ssh://git.sr.ht/~ft/9pro

--- 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 *
--- a/c9/c9.c
+++ b/c9/c9.c
@@ -5,8 +5,13 @@
  * any sense to copy-paste protocol documentation, which
  * you can read at http://man.cat-v.org/plan_9/5/, see 'intro'.
  */
+#ifdef __plan9__
+#include <u.h>
+#include <libc.h>
+#else
 #include <stdint.h>
 #include <string.h>
+#endif
 #include "c9.h"
 
 enum
@@ -717,10 +722,9 @@
 C9error
 s9flush(C9ctx *c, C9tag tag)
 {
-	uint8_t *b;
 	C9error err;
 
-	if((b = R(c, 0, Rflush, tag, &err)) != NULL)
+	if(R(c, 0, Rflush, tag, &err) != NULL)
 		err = c->end(c);
 	return err;
 }
@@ -880,10 +884,9 @@
 C9error
 s9clunk(C9ctx *c, C9tag tag)
 {
-	uint8_t *b;
 	C9error err;
 
-	if((b = R(c, 0, Rclunk, tag, &err)) != NULL)
+	if(R(c, 0, Rclunk, tag, &err) != NULL)
 		err = c->end(c);
 	return err;
 }
@@ -891,10 +894,9 @@
 C9error
 s9remove(C9ctx *c, C9tag tag)
 {
-	uint8_t *b;
 	C9error err;
 
-	if((b = R(c, 0, Rremove, tag, &err)) != NULL)
+	if(R(c, 0, Rremove, tag, &err) != NULL)
 		err = c->end(c);
 	return err;
 }
@@ -943,10 +945,9 @@
 C9error
 s9wstat(C9ctx *c, C9tag tag)
 {
-	uint8_t *b;
 	C9error err;
 
-	if((b = R(c, 0, Rwstat, tag, &err)) != NULL)
+	if(R(c, 0, Rwstat, tag, &err) != NULL)
 		err = c->end(c);
 	return err;
 }
--- a/c9/c9.h
+++ b/c9/c9.h
@@ -1,3 +1,12 @@
+#ifdef __plan9__
+typedef u64int uint64_t;
+typedef u32int uint32_t;
+typedef u16int uint16_t;
+typedef u8int uint8_t;
+#define __attribute__(...)
+#define NULL nil
+#endif
+
 struct C9aux;
 
 typedef struct C9r C9r;