mirror of https://github.com/ericonr/purr-c.git
Fix some issues with the mmap impl.
Main one was using file->data instead of file->cursor in write_into_mmap. Switching to ftruncate in place of posix_fallocate was done for compat reasons (posix_fallocate isn't available everywhere _and_ it can fail on some file systems).
This commit is contained in:
parent
baa013afae
commit
1b4c19665c
13
files.c
13
files.c
|
@ -40,8 +40,6 @@ struct mmap_file create_mmap_from_file(const char *name, int prot)
|
|||
}
|
||||
rv.size = st.st_size;
|
||||
} else if (prot == PROT_WRITE) {
|
||||
// TODO: how to deal with file size here?
|
||||
// fallocate can fail in ugly ways
|
||||
fd = open(name, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
|
||||
rv.flags = MAP_PRIVATE;
|
||||
|
||||
|
@ -49,9 +47,10 @@ struct mmap_file create_mmap_from_file(const char *name, int prot)
|
|||
return rv;
|
||||
}
|
||||
|
||||
int errfa = posix_fallocate(fd, 0, OUTPUT_FILE_SIZE);
|
||||
if (errfa) {
|
||||
errno = errfa;
|
||||
// ftruncate is good enough for now
|
||||
// TODO: we can truncate again once we know the content-size,
|
||||
// otherwise this will leave the file with the wrong size
|
||||
if (0 && ftruncate(fd, OUTPUT_FILE_SIZE) == -1) {
|
||||
return rv;
|
||||
}
|
||||
rv.size = OUTPUT_FILE_SIZE;
|
||||
|
@ -110,7 +109,7 @@ int write_into_mmap(struct mmap_file *file, const uint8_t *buffer, int n)
|
|||
n = max;
|
||||
}
|
||||
|
||||
memcpy(file->data, buffer, n);
|
||||
memcpy(file->cursor, buffer, n);
|
||||
file->cursor = file->data + file->offset;
|
||||
|
||||
return n;
|
||||
|
@ -138,7 +137,7 @@ static size_t fwrite_strip(const uint8_t *buf, int rlen, struct strip_header_inf
|
|||
|
||||
if (st->header_counter < HEADER_MAX_LEN - 1) {
|
||||
// protect from buffer overflow
|
||||
// the header buffer is calloc'd, so no need to terminate it manually
|
||||
// the header buffer is calloc'd, so no need to null-terminate it manually
|
||||
st->header[st->header_counter++] = buf[i];
|
||||
}
|
||||
|
||||
|
|
2
purr.c
2
purr.c
|
@ -89,7 +89,7 @@ int main (int argc, char **argv)
|
|||
}
|
||||
|
||||
struct mmap_file input;
|
||||
struct mmap_file output = create_mmap_from_file(output_file, PROT_WRITE | PROT_READ);
|
||||
struct mmap_file output = create_mmap_from_file(NULL, PROT_WRITE | PROT_READ);
|
||||
if (ERROR_MMAP(output)) {
|
||||
perror("couldn't open output file");
|
||||
exit(EXIT_FAILURE);
|
||||
|
|
Loading…
Reference in New Issue