MCPcopy Create free account
hub / github.com/git/git / check_collision

Function check_collision

object-file.c:345–397  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

343#define CHECK_COLLISION_DEST_VANISHED -2
344
345static int check_collision(const char *source, const char *dest)
346{
347 char buf_source[4096], buf_dest[4096];
348 int fd_source = -1, fd_dest = -1;
349 int ret = 0;
350
351 fd_source = open(source, O_RDONLY);
352 if (fd_source < 0) {
353 ret = error_errno(_("unable to open %s"), source);
354 goto out;
355 }
356
357 fd_dest = open(dest, O_RDONLY);
358 if (fd_dest < 0) {
359 if (errno != ENOENT)
360 ret = error_errno(_("unable to open %s"), dest);
361 else
362 ret = CHECK_COLLISION_DEST_VANISHED;
363 goto out;
364 }
365
366 while (1) {
367 ssize_t sz_a, sz_b;
368
369 sz_a = read_in_full(fd_source, buf_source, sizeof(buf_source));
370 if (sz_a < 0) {
371 ret = error_errno(_("unable to read %s"), source);
372 goto out;
373 }
374
375 sz_b = read_in_full(fd_dest, buf_dest, sizeof(buf_dest));
376 if (sz_b < 0) {
377 ret = error_errno(_("unable to read %s"), dest);
378 goto out;
379 }
380
381 if (sz_a != sz_b || memcmp(buf_source, buf_dest, sz_a)) {
382 ret = error(_("files '%s' and '%s' differ in contents"),
383 source, dest);
384 goto out;
385 }
386
387 if ((size_t) sz_a < sizeof(buf_source))
388 break;
389 }
390
391out:
392 if (fd_source > -1)
393 close(fd_source);
394 if (fd_dest > -1)
395 close(fd_dest);
396 return ret;
397}
398
399/*
400 * Move the just written object into its final resting place.

Callers 1

Calls 3

error_errnoFunction · 0.85
read_in_fullFunction · 0.85
errorFunction · 0.85

Tested by

no test coverage detected