@@ -79,6 +79,36 @@ static int fill_buffer(int fd, unsigned char *buf, unsigned int nbytes, unsigned
return count;
}
+/*
+ * Read padding that could exists between the cpio trailer and the end-of-file.
+ * cpio aligns the file to 512 bytes
+ */
+int extract_padding(int fd, unsigned long *offset)
+{
+ int padding;
+ ssize_t len;
+ unsigned char buf[512];
+
+ if (fd < 0 || !offset)
+ return 0;
+
+ padding = (512 - (*offset % 512)) % 512;
+ if (padding) {
+ TRACE("Expecting %d padding bytes at end-of-file", padding);
+ len = read(fd, buf, padding);
+ if (len < 0) {
+ ERROR("Failure while reading padding %d: %s", fd, strerror(errno));
+ return 0;
+ }
+ if (len == 0) {
+ /* No padding found */
+ return 0;
+ }
+ }
+
+ return 0;
+}
+
/*
* Export the copy_write{,_*} functions to be used in other modules
* for copying a buffer to a file.
@@ -760,7 +790,6 @@ int cpio_scan(int fd, struct swupdate_cfg *cfg, off_t start)
int file_listed;
uint32_t checksum;
-
while (1) {
file_listed = 0;
start = offset;
@@ -174,6 +174,9 @@ static int extract_files(int fd, struct swupdate_cfg *software)
return -1;
}
if (strcmp("TRAILER!!!", fdh.filename) == 0) {
+ /* Keep reading the cpio padding, if any, up to 512 bytes from
+ * the socket until the client stops writing */
+ extract_padding(fd, &offset);
status = STREAM_END;
break;
}
@@ -53,5 +53,6 @@ int get_cpiohdr(unsigned char *buf, unsigned long *size,
unsigned long *namesize, unsigned long *chksum);
int extract_cpio_header(int fd, struct filehdr *fhdr, unsigned long *offset);
int extract_img_from_cpio(int fd, unsigned long offset, struct filehdr *fdh);
+int extract_padding(int fd, unsigned long *offset);
#endif