malloc_checking.patch
  Perform malloc() checks

strdup_checking.patch
  Do proper strdup() error checking

journaLing.patch
  Replace "journalling" with "journaling"

remove_stale_header.patch
  Remove doc/libjio.h, it's not needed there

strdup_free.patch
  Free the memory strdup() allocated



diff -u cur-root/libjio.c cur-root/libjio.c
--- cur-root/libjio.c	2004-03-30 22:58:39.000000000 -0300
+++ cur-root/libjio.c	2004-03-30 22:58:52.000000000 -0300
@@ -1,6 +1,6 @@
 
 /*
- * libjio - A library for Journalled I/O
+ * libjio - A library for Journaled I/O
  * Alberto Bertogli (albertogli@telpin.com.ar)
  */
 
@@ -101,27 +101,51 @@
 }
 
 /* build the journal directory name out of the filename */
-static void get_jdir(char *filename, char *jdir)
+static int get_jdir(char *filename, char *jdir)
 {
-	char *base;
-	char *dir;
+	char *base, *baset;
+	char *dir, *dirt;
+
+	baset = strdup(filename);
+	if (baset == NULL)
+		return 0;
+	base = basename(baset);
+
+	dirt = strdup(filename);
+	if (baset == NULL)
+		return 0;
+	dir = dirname(dirt);
 
-	base = basename(strdup(filename));
-	dir = dirname(strdup(filename));
-	
 	snprintf(jdir, PATH_MAX, "%s/.%s.jio", dir, base);
+
+	free(baset);
+	free(dirt);
+
+	return 1;
 }
 
 /* build the filename of a given transaction */
-static void get_jtfile(char *filename, int tid, char *jtfile)
+static int get_jtfile(char *filename, int tid, char *jtfile)
 {
-	char *base;
-	char *dir;
+	char *base, *baset;
+	char *dir, *dirt;
 
-	base = basename(strdup(filename));
-	dir = dirname(strdup(filename));
+	baset = strdup(filename);
+	if (baset == NULL)
+		return 0;
+	base = basename(baset);
+
+	dirt = strdup(filename);
+	if (baset == NULL)
+		return 0;
+	dir = dirname(dirt);
 
 	snprintf(jtfile, PATH_MAX, "%s/.%s.jio/%d", dir, base, tid);
+
+	free(baset);
+	free(dirt);
+
+	return 1;
 }
 
 /* gets a new transaction id */
@@ -179,6 +203,9 @@
 	} else {
 		/* look up the new max. */
 		for (i = curid - 1; i > 0; i--) {
+			/* this can fail if we're low on mem, but we don't
+			 * care checking here because the problem will come
+			 * out later and we can fail more properly */
 			get_jtfile(fs->name, i, name);
 			if (access(name, R_OK | W_OK) == 0) {
 				curid = i;
@@ -240,13 +267,16 @@
 	void *buf_init, *bufp;
 	
 	name = (char *) malloc(PATH_MAX);
+	if (name == NULL)
+		return -1;
 	
 	id = get_tid(ts->fs);
 	if (id == 0)
 		return -1;
 	
 	/* open the transaction file */
-	get_jtfile(ts->fs->name, id, name);
+	if (!get_jtfile(ts->fs->name, id, name))
+		return -1;
 	fd = open(name, O_RDWR | O_CREAT | O_TRUNC | O_LARGEFILE, 0600);
 	if (fd < 0)
 		return -1;
@@ -264,6 +294,9 @@
 	/* first the static data */
 	
 	buf_init = malloc(J_DISKTFIXSIZE);
+	if (buf_init == NULL)
+		return -1;
+	
 	bufp = buf_init;
 	
 	memcpy(bufp, (void *) &(ts->id), sizeof(ts->id));
@@ -297,6 +330,9 @@
 	}
 	
 	ts->pdata = malloc(ts->len);
+	if (ts->pdata == NULL)
+		goto exit;
+	
 	ts->plen = ts->len;
 
 	/* copy the current content into the transaction file */
@@ -366,6 +402,9 @@
 	jtrans_init(ts->fs, &newts);
 
 	newts.name = malloc(strlen(ts->name));
+	if (newts.name == NULL)
+		return -1;
+	
 	strcpy(newts.name, ts->name);
 	newts.flags = ts->flags;
 	newts.offset = ts->offset;
@@ -418,7 +457,8 @@
 	
 	pthread_mutex_init( &(fs->lock), NULL);
 
-	get_jdir(name, jdir);
+	if (!get_jdir(name, jdir))
+		return -1;
 	rv = mkdir(jdir, 0750);
 	rv = lstat(jdir, &sinfo);
 	if (rv < 0 || !S_ISDIR(sinfo.st_mode))
@@ -557,6 +597,8 @@
 	 * of using writev() :\
 	 * maybe we should do one transaction per vector */
 	buf = malloc(sum);
+	if (buf == NULL)
+		return -1;
 	bufp = 0;
 
 	for (i = 0; i < count; i++) {
@@ -627,7 +669,8 @@
 	fs.fd = fd;
 	fs.name = name;
 
-	get_jdir(name, jdir);
+	if (!get_jdir(name, jdir))
+		return J_ENOMEM;
 	rv = lstat(jdir, &sinfo);
 	if (rv < 0 || !S_ISDIR(sinfo.st_mode))
 		return J_ENOJOURNAL;
@@ -657,6 +700,9 @@
 	/* we loop all the way up to the max transaction id */
 	for (i = 1; i <= maxtid; i++) {
 		curts = malloc(sizeof(struct jtrans));
+		if (curts == NULL)
+			return J_ENOMEM;
+		
 		jtrans_init(&fs, curts);
 		curts->id = i;
 		
@@ -664,7 +710,8 @@
 		 * really looping in order (recovering transaction in a
 		 * different order as they were applied means instant
 		 * corruption) */
-		get_jtfile(name, i, tname);
+		if (!get_jtfile(name, i, tname))
+			return J_ENOMEM;
 		tfd = open(tname, O_RDWR | O_SYNC | O_LARGEFILE, 0600);
 		if (tfd < 0) {
 			res->invalid++;
@@ -683,6 +730,11 @@
 
 		/* load from disk, header first */
 		buf = (char *) malloc(J_DISKTFIXSIZE);
+		if (buf == NULL) {
+			res->load_error++;
+			goto loop;
+		}
+		
 		rv = read(tfd, buf, J_DISKTFIXSIZE);
 		if (rv != J_DISKTFIXSIZE) {
 			res->broken_head++;
@@ -711,8 +763,22 @@
 		 * successful, so we read it to complete the transaction
 		 * structure and apply it again */
 		curts->buf = malloc(curts->len);
+		if (curts->buf == NULL) {
+			res->load_error++;
+			goto loop;
+		}
+		
 		curts->pdata = malloc(curts->plen);
+		if (curts->pdata == NULL) {
+			res->load_error++;
+			goto loop;
+		}
+		
 		curts->udata = malloc(curts->ulen);
+		if (curts->udata == NULL) {
+			res->load_error++;
+			goto loop;
+		}
 
 		/* user data */
 		offset = J_DISKTFIXSIZE;
unchanged:
--- cur-root/libjio.h	2004-03-30 22:57:50.000000000 -0300
+++ cur-root/libjio.h	2004-03-30 22:58:39.000000000 -0300
@@ -1,6 +1,6 @@
 
 /*
- * libjio - A library for Journalled I/O
+ * libjio - A library for Journaled I/O
  * Alberto Bertogli (albertogli@telpin.com.ar)
  */
 
@@ -99,6 +99,7 @@
 #define J_ESUCCESS	0	/* success - shouldn't be used */
 #define J_ENOENT	1	/* no such file */
 #define J_ENOJOURNAL	2	/* no journal associated */
+#define J_ENOMEM	3	/* no enough free memory */
 
 #endif
 
unchanged:
--- cur/doc/libjio.3~journaLing	2004-03-30 22:58:39.000000000 -0300
+++ cur-root/doc/libjio.3	2004-03-30 22:58:39.000000000 -0300
@@ -1,6 +1,6 @@
 .TH libjio 3 "21/Feb/2004"
 .SH NAME
-libjio - A library for Journalled I/O
+libjio - A library for Journaled I/O
 
 .SH FUNCTIONS
 
@@ -86,7 +86,7 @@ struct jfsck_result
 
 .SH DESCRIPTION
 
-libjio is a library to do transaction-oriented journalled I/O. This manpage
+libjio is a library to do transaction-oriented journaled I/O. This manpage
 describes it's C API very briefly, further information can be found in the
 documentation that comes along with the library itself, or on the web at
 http://auriga.wearlab.de/~alb/libjio.
unchanged:
--- cur/doc/libjio.lyx~journaLing	2004-03-30 22:58:39.000000000 -0300
+++ cur-root/doc/libjio.lyx	2004-03-30 22:58:39.000000000 -0300
@@ -25,7 +25,7 @@
 
 \layout Title
 
-libjio - A library for journalled I/O 
+libjio - A library for journaled I/O 
 \layout Author
 
 Alberto Bertogli (albertogli@telpin.com.ar) 
@@ -46,7 +46,7 @@ Introduction
 \emph on 
 libjio
 \emph default 
- is a library for doing journalled transaction-oriented I/O, providing atomicity
+ is a library for doing journaled transaction-oriented I/O, providing atomicity
  warantees and a simple to use but powerful API.
 \layout Standard
 
@@ -59,7 +59,7 @@ This document explains the design of the
 \layout Standard
 
 To the user, libjio provides two groups of functions, one UNIX-alike that
- implements the journalled versions of the classic functions (
+ implements the journaled versions of the classic functions (
 \emph on 
 open()
 \emph default 
@@ -182,7 +182,7 @@ atomically
  all.
  This is a really common word, specially if you have worked with multiprocessing
 , and should be quite familiar.
- We implement atomicity by combining fine-grained locks and journalling,
+ We implement atomicity by combining fine-grained locks and journaling,
  which can assure us both to be able to recover from crashes, and to have
  exclusive access to a portion of the file without having any other transaction
  overlap it.
unchanged:
--- cur/doc/libjio.h
+++ /dev/null	2004-03-29 12:23:41.000000000 -0300
@@ -1,104 +0,0 @@
-
-/*
- * libjio - A library for Journalled I/O
- * Alberto Bertogli (albertogli@telpin.com.ar)
- */
-
-#ifndef _LIBJIO_H
-#define _LIBJIO_H
-
-#include <stdint.h>
-#include <sys/types.h>
-#include <sys/uio.h>
-#include <pthread.h>
-
-
-/* logical structures */
-struct jfs {
-	int fd;			/* main file descriptor */
-	char *name;		/* and its name */
-	int jfd;		/* journal's lock file descriptor */
-	int flags;		/* journal mode options used in jopen() */
-	pthread_mutex_t lock;	/* a soft lock used in some operations */
-};
-
-struct jtrans {
-	struct jfs *fs;		/* journal file structure to operate on */
-	char *name;		/* name of the transaction file */
-	int id;			/* transaction id */
-	int flags;		/* misc flags */
-	void *buf;		/* buffer */
-	size_t len;		/* buffer lenght */
-	off_t offset;		/* file offset to operate on */
-	void *udata;		/* user-supplied data */
-	size_t ulen;		/* udata lenght */
-	void *pdata;		/* previous data, for rollback */
-	size_t plen;		/* pdata lenght */
-};
-
-struct jfsck_result {
-	int total;		/* total transactions files we looked at */
-	int invalid;		/* invalid files in the journal directory */
-	int in_progress;	/* transactions in progress */
-	int broken_head;	/* transactions broken (header missing) */
-	int broken_body;	/* transactions broken (body missing) */
-	int load_error;		/* errors loading the transaction */
-	int apply_error;	/* errors applying the transaction */
-	int reapplied;		/* transactions that were re-applied */
-};
-
-/* on-disk structure */
-struct disk_trans {
-	
-	/* header (fixed lenght, defined below) */
-	uint32_t id;		/* id */
-	uint32_t flags;		/* flags about this transaction */
-	uint32_t len;		/* data lenght */
-	uint32_t ulen;		/* user-supplied information lenght */
-	uint64_t offset;	/* offset relative to the BOF */
-	
-	/* payload (variable lenght) */
-	char *udata;		/* user-supplied data */
-	char *prevdata;		/* previous data, optional, for rollback */
-	char *data;		/* data */
-};
-
-
-/* basic operations */
-int jopen(struct jfs *fs, char *name, int flags, int mode, int jflags);
-ssize_t jread(struct jfs *fs, void *buf, size_t count);
-ssize_t jpread(struct jfs *fs, void *buf, size_t count, off_t offset);
-ssize_t jreadv(struct jfs *fs, struct iovec *vector, int count);
-ssize_t jwrite(struct jfs *fs, void *buf, size_t count);
-ssize_t jpwrite(struct jfs *fs, void *buf, size_t count, off_t offset);
-ssize_t jwritev(struct jfs *fs, struct iovec *vector, int count);
-int jtruncate(struct jfs *fs, off_t lenght);
-int jclose(struct jfs *fs);
-
-/* transaction operations */
-void jtrans_init(struct jfs *fs, struct jtrans *ts);
-int jtrans_commit(struct jtrans *ts);
-int jtrans_rollback(struct jtrans *ts);
-void jtrans_free(struct jtrans *ts);
-
-/* journal checker */
-int jfsck(char *name, struct jfsck_result *res);
-
-
-/* jfs constants */
-#define J_NOLOCK	1	/* don't lock the file before operating on it */
-
-/* jtrans constants */
-#define J_COMMITED	1	/* mark a transaction as commited */
-#define J_ROLLBACKED	2	/* mark a transaction as rollbacked */
-
-/* disk_trans constants */
-#define J_DISKTFIXSIZE	 24	/* lenght of disk_trans' header */ 
-
-/* jfsck constants (return values) */
-#define J_ESUCCESS	0	/* success - shouldn't be used */
-#define J_ENOENT	1	/* no such file */
-#define J_ENOJOURNAL	2	/* no journal associated */
-
-#endif
-
