vis

a vi-like editor based on Plan 9's structural regular expressions

git clone https://9o.is/git/vis.git

commit 82652aa48c5438a2a2bb564a5e9dcf9c98207e87
parent 40ab96236d73ea8dad2917d4d2ead5e855012c6c
Author: Marc André Tanner <mat@brain-dump.org>
Date:   Sun, 28 Jun 2020 09:57:13 +0200

text: simplify remapping of original file content

Use mmap with MAP_FIXED which replaces existing mappings without any
race conditions between the munmap/mmap calls.

Diffstat:
Mtext.c | 10+---------
1 file changed, 1 insertion(+), 9 deletions(-)

diff --git a/text.c b/text.c @@ -960,21 +960,13 @@ static bool text_save_begin_inplace(TextSave *ctx) { ssize_t written = write_all(newfd, block->data, size); if (written == -1 || (size_t)written != size) goto err; - if (munmap(block->data, size) == -1) - goto err; - - void *data = mmap(block->data, size, PROT_READ, MAP_SHARED, newfd, 0); + void *data = mmap(block->data, size, PROT_READ, MAP_SHARED|MAP_FIXED, newfd, 0); if (data == MAP_FAILED) goto err; - if (data != block->data) { - munmap(data, size); - goto err; - } bool close_failed = (close(newfd) == -1); newfd = -1; if (close_failed) goto err; - block->data = data; block->type = MMAP; } /* overwrite the existing file content, if something goes wrong