summaryrefslogtreecommitdiffstats
path: root/tools/mtd-utils/patches/000-upstream_jffs2reader.patch
blob: bacef76210672422c99ad22d9569b84ba270b857 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
From fec81abd9593fe11ba8577d38e4143e5708e3343 Mon Sep 17 00:00:00 2001
From: Mike Frysinger <vapier@gentoo.org>
Date: Sat, 2 Oct 2010 14:58:09 -0400
Subject: [PATCH] jffs2reader: convert to common.h helpers

Signed-off-by: Mike Frysinger <vapier@gentoo.org>
Signed-off-by: Artem Bityutskiy <Artem.Bityutskiy@nokia.com>
---
 jffs2reader.c |   71 +++++++++++++++++---------------------------------------
 1 files changed, 22 insertions(+), 49 deletions(-)

diff --git a/jffs2reader.c b/jffs2reader.c
index 0cdff19..d5a3d95 100644
--- a/jffs2reader.c
+++ b/jffs2reader.c
@@ -77,6 +77,7 @@ BUGS:
 #include <sys/param.h>
 #include <dirent.h>
 #include <linux/jffs2.h>
+#include "common.h"
 
 #define SCRATCH_SIZE (5*1024*1024)
 
@@ -136,10 +137,8 @@ void putblock(char *b, size_t bsize, size_t * rsize,
 {
 	uLongf dlen = n->dsize;
 
-	if (n->isize > bsize || (n->offset + dlen) > bsize) {
-		fprintf(stderr, "File does not fit into buffer!\n");
-		exit(EXIT_FAILURE);
-	}
+	if (n->isize > bsize || (n->offset + dlen) > bsize)
+		errmsg_die("File does not fit into buffer!");
 
 	if (*rsize < n->isize)
 		bzero(b + *rsize, n->isize - *rsize);
@@ -163,8 +162,7 @@ void putblock(char *b, size_t bsize, size_t * rsize,
 			/* [DYN]RUBIN support required! */
 
 		default:
-			fprintf(stderr, "Unsupported compression method!\n");
-			exit(EXIT_FAILURE);
+			errmsg_die("Unsupported compression method!");
 	}
 
 	*rsize = n->isize;
@@ -188,7 +186,7 @@ struct dir *putdir(struct dir *dd, struct jffs2_raw_dirent *n)
 
 	if (n->ino) {
 		if (dd == NULL) {
-			d = malloc(sizeof(struct dir));
+			d = xmalloc(sizeof(struct dir));
 			d->type = n->type;
 			memcpy(d->name, n->name, n->nsize);
 			d->nsize = n->nsize;
@@ -208,7 +206,7 @@ struct dir *putdir(struct dir *dd, struct jffs2_raw_dirent *n)
 			}
 
 			if (dd->next == NULL) {
-				dd->next = malloc(sizeof(struct dir));
+				dd->next = xmalloc(sizeof(struct dir));
 				dd->next->type = n->type;
 				memcpy(dd->next->name, n->name, n->nsize);
 				dd->next->nsize = n->nsize;
@@ -344,7 +342,7 @@ void printdir(char *o, size_t size, struct dir *d, char *path, int recurse)
 		}
 		ri = find_raw_inode(o, size, d->ino);
 		if (!ri) {
-			fprintf(stderr, "bug: raw_inode missing!\n");
+			warnmsg("bug: raw_inode missing!");
 			d = d->next;
 			continue;
 		}
@@ -379,11 +377,7 @@ void printdir(char *o, size_t size, struct dir *d, char *path, int recurse)
 
 		if (d->type == DT_DIR && recurse) {
 			char *tmp;
-			tmp = malloc(BUFSIZ);
-			if (!tmp) {
-				fprintf(stderr, "memory exhausted\n");
-				exit(EXIT_FAILURE);
-			}
+			tmp = xmalloc(BUFSIZ);
 			sprintf(tmp, "%s/%s", path, d->name);
 			lsdir(o, size, tmp, recurse);		/* Go recursive */
 			free(tmp);
@@ -817,11 +811,8 @@ void lsdir(char *o, size_t size, char *path, int recurse)
 	dd = resolvepath(o, size, 1, path, &ino);
 
 	if (ino == 0 ||
-			(dd == NULL && ino == 0) || (dd != NULL && dd->type != DT_DIR)) {
-		fprintf(stderr, "%s: %s: No such file or directory\n",
-				PROGRAM_NAME, path);
-		exit(EXIT_FAILURE);
-	}
+			(dd == NULL && ino == 0) || (dd != NULL && dd->type != DT_DIR))
+		errmsg_die("%s: No such file or directory", path);
 
 	d = collectdir(o, size, ino, d);
 	printdir(o, size, d, path, recurse);
@@ -848,15 +839,11 @@ void catfile(char *o, size_t size, char *path, char *b, size_t bsize,
 
 	dd = resolvepath(o, size, 1, path, &ino);
 
-	if (ino == 0) {
-		fprintf(stderr, "%s: No such file or directory\n", path);
-		exit(EXIT_FAILURE);
-	}
+	if (ino == 0)
+		errmsg_die("%s: No such file or directory", path);
 
-	if (dd == NULL || dd->type != DT_REG) {
-		fprintf(stderr, "%s: Not a regular file\n", path);
-		exit(EXIT_FAILURE);
-	}
+	if (dd == NULL || dd->type != DT_REG)
+		errmsg_die("%s: Not a regular file", path);
 
 	ri = find_raw_inode(o, size, ino);
 	putblock(b, bsize, rsize, ri);
@@ -896,36 +883,22 @@ int main(int argc, char **argv)
 	}
 
 	fd = open(argv[optind], O_RDONLY);
-	if (fd == -1) {
-		fprintf(stderr, "%s: %s\n", argv[optind], strerror(errno));
-		exit(2);
-	}
+	if (fd == -1)
+		sys_errmsg_die("%s", argv[optind]);
 
-	if (fstat(fd, &st)) {
-		fprintf(stderr, "%s: %s\n", argv[optind], strerror(errno));
-		exit(3);
-	}
+	if (fstat(fd, &st))
+		sys_errmsg_die("%s", argv[optind]);
 
-	buf = malloc((size_t) st.st_size);
-	if (buf == NULL) {
-		fprintf(stderr, "%s: memory exhausted\n", argv[optind]);
-		exit(4);
-	}
+	buf = xmalloc((size_t) st.st_size);
 
-	if (read(fd, buf, st.st_size) != (ssize_t) st.st_size) {
-		fprintf(stderr, "%s: %s\n", argv[optind], strerror(errno));
-		exit(5);
-	}
+	if (read(fd, buf, st.st_size) != (ssize_t) st.st_size)
+		sys_errmsg_die("%s", argv[optind]);
 
 	if (dir)
 		lsdir(buf, st.st_size, dir, recurse);
 
 	if (file) {
-		scratch = malloc(SCRATCH_SIZE);
-		if (scratch == NULL) {
-			fprintf(stderr, "%s: memory exhausted\n", argv[optind]);
-			exit(6);
-		}
+		scratch = xmalloc(SCRATCH_SIZE);
 
 		catfile(buf, st.st_size, file, scratch, SCRATCH_SIZE, &ssize);
 		free(scratch);