Skip to content
Snippets Groups Projects
Commit 88d82f90 authored by raphael.bach's avatar raphael.bach
Browse files

Add Sparse_WriteImage() in sparse.h

parent c88844d5
Branches
No related tags found
No related merge requests found
Pipeline #
......@@ -120,6 +120,10 @@ void Sparse_ReadChunksHeader(struct Sparse * const sparse);
Sparse_Read()
------------------------------------------------------------------------------*/
void Sparse_Read(struct Sparse * const sparse);
/*------------------------------------------------------------------------------
Sparse_WriteImage()
------------------------------------------------------------------------------*/
void Sparse_WriteImage(const struct Sparse * const sparse, int fd);
/*------------------------------------------------------------------------------
Sparse_PrintHeader()
------------------------------------------------------------------------------*/
......
......@@ -38,7 +38,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include <errno.h>
#include <inttypes.h> // PRIu16, PRIx16, PRIu32, PRIx32
#include <stdio.h> // printf()
#include <stdlib.h> // exit(), NULL
#include <stdlib.h> // malloc(), calloc(), free(), exit(), NULL
#include <string.h> // strncpy(), strlen(), strerror()
// Internal
#include "file_io.h"
......@@ -171,6 +171,49 @@ void Sparse_Read(struct Sparse * const sparse)
}
}
}
/*------------------------------------------------------------------------------
Sparse_WriteImage()
------------------------------------------------------------------------------*/
void Sparse_WriteImage(const struct Sparse * const sparse, int fd)
{
assert(sparse != NULL);
for(size_t i = 0; i < sparse->header.chunk_count; i++) {
size_t write_size = 0;
char * buffer = NULL;
if(sparse->chunks[i].header.type == CHUNK_TYPE_RAW) {
write_size = sparse->chunks[i].size;
buffer = sparse->chunks[i].data;
} else if(sparse->chunks[i].header.type == CHUNK_TYPE_FILL) {
write_size = sparse->chunks[i].header.block_count * sparse->header.block_size;
buffer = malloc(write_size);
} else if(sparse->chunks[i].header.type == CHUNK_TYPE_DONT_CARE) {
write_size = sparse->chunks[i].header.block_count * sparse->header.block_size;
buffer = calloc(write_size, 1);
} else if(sparse->chunks[i].header.type == CHUNK_TYPE_CRC32) {
write_size = sparse->chunks[i].header.block_count * sparse->header.block_size;
} else {
}
if(buffer != NULL) {
if(sparse->chunks[i].header.type == CHUNK_TYPE_FILL) {
for(size_t j = 0; j < write_size; j++) {
buffer[j] = *sparse->chunks[i].data;
}
}
size_t bytes_written = FIO_Write(fd, buffer, write_size);
if(bytes_written == write_size) {
if(sparse->chunks[i].header.type != CHUNK_TYPE_RAW) {
free(buffer);
}
} else {
Log_Fatal(&s_logger, "FIO_Write(%d) failed! %s\n", sparse->fd, strerror(errno));
exit(-1);
}
} else {
Log_Fatal(&s_logger, "malloc() : %s\n", strerror(errno));
exit(-1);
}
}
}
/*------------------------------------------------------------------------------
Sparse_PrintHeader()
------------------------------------------------------------------------------*/
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment