
Allow to open files read-only.

---

 cur-root/libjio.h |    1 
 cur-root/trans.c  |   56 ++++++++++++++++++++++++++++++++++++++----------------
 2 files changed, 41 insertions(+), 16 deletions(-)

diff -puN trans.c~jopen_readonly trans.c
--- cur/trans.c~jopen_readonly	2004-10-10 20:58:49.750104368 -0300
+++ cur-root/trans.c	2004-10-10 20:58:49.755103608 -0300
@@ -138,9 +138,16 @@ int jtrans_add(struct jtrans *ts, const 
 {
 	struct joper *jop, *tmpop;
 
+	pthread_mutex_lock(&(ts->lock));
+
+	/* fail for read-only accesses */
+	if (ts->flags & J_RDONLY) {
+		pthread_mutex_unlock(&(ts->lock));
+		return 0;
+	}
+
 	/* find the last operation in the transaction and create a new one at
 	 * the end */
-	pthread_mutex_lock(&(ts->lock));
 	if (ts->op == NULL) {
 		ts->op = malloc(sizeof(struct joper));
 		if (ts->op == NULL) {
@@ -201,6 +208,10 @@ ssize_t jtrans_commit(struct jtrans *ts)
 	ts->flags = ts->flags & ~J_COMMITTED;
 	ts->flags = ts->flags & ~J_ROLLBACKED;
 
+	/* fail for read-only accesses */
+	if (ts->flags & J_RDONLY)
+		goto exit;
+
 	name = (char *) malloc(PATH_MAX);
 	if (name == NULL)
 		goto exit;
@@ -525,12 +536,18 @@ int jopen(struct jfs *fs, const char *na
 	fs->jdirfd = -1;
 	fs->jmap = MAP_FAILED;
 
-	/* we always need read and write access, because when we commit a
-	 * transaction we read the current contents before applying, and write
-	 * access is needed for locking with fcntl */
-	flags = flags & ~O_WRONLY;
-	flags = flags & ~O_RDONLY;
-	flags = flags | O_RDWR;
+	/* we provide either read-only or read-write access, because when we
+	 * commit a transaction we read the current contents before applying,
+	 * and write access is needed for locking with fcntl; the test is done
+	 * this way because O_RDONLY is usually 0, so "if (flags & O_RDONLY)"
+	 * will fail. */
+	if ((flags & O_WRONLY) || (flags & O_RDWR)) {
+		flags = flags & ~O_WRONLY;
+		flags = flags & ~O_RDONLY;
+		flags = flags | O_RDWR;
+	} else {
+		jflags = jflags | J_RDONLY;
+	}
 
 	fs->name = strdup(name);
 	fs->flags = jflags;
@@ -559,6 +576,11 @@ int jopen(struct jfs *fs, const char *na
 	if (fs->fd < 0)
 		goto error_exit;
 
+	/* nothing else to do for read-only access */
+	if (flags & O_RDONLY) {
+		return fs->fd;
+	}
+
 	if (!get_jdir(name, jdir))
 		goto error_exit;
 	rv = mkdir(jdir, 0750);
@@ -645,20 +667,22 @@ int jclose(struct jfs *fs)
 
 	ret = 0;
 
-	if (jsync(fs))
-		ret = -1;
+	if (! (fs->flags & J_RDONLY)) {
+		if (jsync(fs))
+			ret = -1;
+		if (fs->jfd < 0 || close(fs->jfd))
+			ret = -1;
+		if (fs->jdirfd < 0 || close(fs->jdirfd))
+			ret = -1;
+		if (fs->jmap != MAP_FAILED)
+			munmap(fs->jmap, sizeof(unsigned int));
+	}
+
 	if (fs->fd < 0 || close(fs->fd))
 		ret = -1;
-	if (fs->jfd < 0 || close(fs->jfd))
-		ret = -1;
-	if (fs->jdirfd < 0 || close(fs->jdirfd))
-		ret = -1;
 	if (fs->name)
 		/* allocated by strdup() in jopen() */
 		free(fs->name);
-	if (fs->jmap != MAP_FAILED)
-		munmap(fs->jmap, sizeof(unsigned int));
-
 	pthread_mutex_destroy(&(fs->lock));
 
 	return ret;
diff -puN libjio.h~jopen_readonly libjio.h
--- cur/libjio.h~jopen_readonly	2004-10-10 20:58:49.751104216 -0300
+++ cur-root/libjio.h	2004-10-10 20:58:49.755103608 -0300
@@ -150,6 +150,7 @@ FILE *jfsopen(struct jfs *stream, const 
 #define J_COMMITTED	8	/* mark a transaction as committed */
 #define J_ROLLBACKED	16	/* mark a transaction as rollbacked */
 #define J_ROLLBACKING	32	/* mark a transaction as rollbacking */
+#define J_RDONLY	64	/* mark a file as read-only */
 
 /* disk constants */
 #define J_DISKHEADSIZE	 12	/* length of disk_header */
_
