comparison ffstools/tiffs-mkfs/output.c @ 708:e2e4aed24522

tiffs-mkfs: malloc file chunk buffer only once
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 23 May 2020 02:50:06 +0000
parents 805885936f62
children 178ed445021d
comparison
equal deleted inserted replaced
707:805885936f62 708:e2e4aed24522
35 memset(data_block, 0xFF, ffs_sector_size); 35 memset(data_block, 0xFF, ffs_sector_size);
36 bcopy(tiffs_header, data_block, 6); 36 bcopy(tiffs_header, data_block, 6);
37 data_block[8] = 0xBD; 37 data_block[8] = 0xBD;
38 data_fill_level = 0x10; 38 data_fill_level = 0x10;
39 objects_in_block = 0; 39 objects_in_block = 0;
40
41 chunk_buffer = malloc(chunk_size_max);
42 if (!chunk_buffer) {
43 perror("malloc of file chunk buffer");
44 exit(1);
45 }
40 } 46 }
41 47
42 void 48 void
43 open_output_file() 49 open_output_file()
44 { 50 {
135 141
136 create_file_object(to) 142 create_file_object(to)
137 struct tree_object *to; 143 struct tree_object *to;
138 { 144 {
139 int fd, cc; 145 int fd, cc;
140 u_char *data;
141 int head, seg; 146 int head, seg;
142 struct tiffs_inode *inp; 147 struct tiffs_inode *inp;
143 148
144 fd = open(to->u.f.host_pathname, O_RDONLY); 149 fd = open(to->u.f.host_pathname, O_RDONLY);
145 if (fd < 0) { 150 if (fd < 0) {
146 perror(to->u.f.host_pathname); 151 perror(to->u.f.host_pathname);
147 exit(1); 152 exit(1);
148 } 153 }
149 data = malloc(chunk_size_max); 154 cc = read(fd, chunk_buffer, chunk_size_max);
150 if (!data) {
151 perror("malloc of file chunk buffer");
152 exit(1);
153 }
154 cc = read(fd, data, chunk_size_max);
155 if (cc < 0) { 155 if (cc < 0) {
156 read_err: perror("error reading file content"); 156 read_err: perror("error reading file content");
157 exit(1); 157 exit(1);
158 } 158 }
159 if (cc == 0) { 159 if (cc == 0) {
160 /* zero length file */ 160 /* zero length file */
161 close(fd); 161 close(fd);
162 free(data);
163 return create_object(to->name, OBJTYPE_FILE, (u_char *) 0, 0); 162 return create_object(to->name, OBJTYPE_FILE, (u_char *) 0, 0);
164 } 163 }
165 head = create_object(to->name, OBJTYPE_FILE, data, cc); 164 head = create_object(to->name, OBJTYPE_FILE, chunk_buffer, cc);
166 inp = inode_array + head; 165 inp = inode_array + head;
167 for (;;) { 166 for (;;) {
168 cc = read(fd, data, chunk_size_max); 167 cc = read(fd, chunk_buffer, chunk_size_max);
169 if (cc < 0) 168 if (cc < 0)
170 goto read_err; 169 goto read_err;
171 if (cc == 0) 170 if (cc == 0)
172 break; 171 break;
173 seg = create_object((char *) 0, OBJTYPE_SEGMENT, data, cc); 172 seg = create_object((char *) 0, OBJTYPE_SEGMENT, chunk_buffer,
173 cc);
174 inp->child = htole16(seg); 174 inp->child = htole16(seg);
175 inp = inode_array + seg; 175 inp = inode_array + seg;
176 } 176 }
177 close(fd); 177 close(fd);
178 free(data);
179 return head; 178 return head;
180 } 179 }
181 180
182 create_subdir(to) 181 create_subdir(to)
183 struct tree_object *to; 182 struct tree_object *to;