Commit ec097b7babfa4e77b6e0bdbf81fc05e88976ad4c

lhchavez 2021-01-07T05:44:16

Use `p_pwrite`/`p_pread` consistently throughout the codebase This change stops using the seek+read/write combo to perform I/O with an offset, since this is faster by one system call (and also more atomic and therefore safer).

diff --git a/src/indexer.c b/src/indexer.c
index ebce28f..1d37e36 100644
--- a/src/indexer.c
+++ b/src/indexer.c
@@ -656,7 +656,6 @@ static int append_to_pack(git_indexer *idx, const void *data, size_t size)
 	size_t page_offset;
 	off64_t page_start;
 	off64_t current_size = idx->pack->mwf.size;
-	int fd = idx->pack->mwf.fd;
 	int error;
 
 	if (!size)
@@ -673,8 +672,7 @@ static int append_to_pack(git_indexer *idx, const void *data, size_t size)
 	page_offset = new_size % mmap_alignment;
 	page_start = new_size - page_offset;
 
-	if (p_lseek(fd, page_start + mmap_alignment - 1, SEEK_SET) < 0 ||
-	    p_write(idx->pack->mwf.fd, data, 1) < 0) {
+	if (p_pwrite(idx->pack->mwf.fd, data, 1, page_start + mmap_alignment - 1) < 0) {
 		git_error_set(GIT_ERROR_OS, "cannot extend packfile '%s'", idx->pack->pack_name);
 		return -1;
 	}
diff --git a/src/pack.c b/src/pack.c
index 1a208a0..48ca886 100644
--- a/src/pack.c
+++ b/src/pack.c
@@ -1063,8 +1063,7 @@ static int packfile_open(struct git_pack_file *p)
 
 	/* Verify the pack matches its index. */
 	if (p->num_objects != ntohl(hdr.hdr_entries) ||
-		p_lseek(p->mwf.fd, p->mwf.size - GIT_OID_RAWSZ, SEEK_SET) == -1 ||
-		p_read(p->mwf.fd, sha1.id, GIT_OID_RAWSZ) < 0)
+		p_pread(p->mwf.fd, sha1.id, GIT_OID_RAWSZ, p->mwf.size - GIT_OID_RAWSZ) < 0)
 		goto cleanup;
 
 	idx_sha1 = ((unsigned char *)p->index_map.data) + p->index_map.len - 40;