Skip to content

Commit df3458e

Browse files
hanwengitster
authored andcommitted
refs API: make parse_loose_ref_contents() not set errno
Change the parse_loose_ref_contents() function to stop setting "errno" and failure, and to instead pass up a "failure_errno" via a parameter. This requires changing its callers to do the same. The EINVAL error from parse_loose_ref_contents is used in files-backend to create a custom error message. In untangling this we discovered a tricky edge case. The refs_read_special_head() function was relying on parse_loose_ref_contents() setting EINVAL. By converting it to use "saved_errno" we can migrate away from "errno" in this part of the code entirely, and do away with an existing "save_errno" pattern, its only purpose was to not clobber the "errno" we previously needed at the end of files_read_raw_ref(). Let's assert that we can do that by not having files_read_raw_ref() itself operate on *failure_errno in addition to passing it on. Instead we'll assert that if we return non-zero we actually do set errno, thus assuring ourselves and callers that they can trust the resulting "failure_errno". Signed-off-by: Han-Wen Nienhuys <[email protected]> Signed-off-by: Ævar Arnfjörð Bjarmason <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 8b72fea commit df3458e

File tree

3 files changed

+29
-16
lines changed

3 files changed

+29
-16
lines changed

refs.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1648,7 +1648,8 @@ int for_each_fullref_in_prefixes(const char *namespace,
16481648

16491649
static int refs_read_special_head(struct ref_store *ref_store,
16501650
const char *refname, struct object_id *oid,
1651-
struct strbuf *referent, unsigned int *type)
1651+
struct strbuf *referent, unsigned int *type,
1652+
int *failure_errno)
16521653
{
16531654
struct strbuf full_path = STRBUF_INIT;
16541655
struct strbuf content = STRBUF_INIT;
@@ -1658,7 +1659,8 @@ static int refs_read_special_head(struct ref_store *ref_store,
16581659
if (strbuf_read_file(&content, full_path.buf, 0) < 0)
16591660
goto done;
16601661

1661-
result = parse_loose_ref_contents(content.buf, oid, referent, type);
1662+
result = parse_loose_ref_contents(content.buf, oid, referent, type,
1663+
failure_errno);
16621664

16631665
done:
16641666
strbuf_release(&full_path);
@@ -1673,7 +1675,7 @@ int refs_read_raw_ref(struct ref_store *ref_store, const char *refname,
16731675
assert(failure_errno);
16741676
if (!strcmp(refname, "FETCH_HEAD") || !strcmp(refname, "MERGE_HEAD")) {
16751677
return refs_read_special_head(ref_store, refname, oid, referent,
1676-
type);
1678+
type, failure_errno);
16771679
}
16781680

16791681
return ref_store->be->read_raw_ref(ref_store, refname, oid, referent,

refs/files-backend.c

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,7 @@ static int files_read_raw_ref(struct ref_store *ref_store, const char *refname,
355355
int fd;
356356
int ret = -1;
357357
int remaining_retries = 3;
358+
int myerr = 0;
358359

359360
*type = 0;
360361
strbuf_reset(&sb_path);
@@ -382,11 +383,13 @@ static int files_read_raw_ref(struct ref_store *ref_store, const char *refname,
382383

383384
if (lstat(path, &st) < 0) {
384385
int ignore_errno;
385-
if (errno != ENOENT)
386+
myerr = errno;
387+
errno = 0;
388+
if (myerr != ENOENT)
386389
goto out;
387390
if (refs_read_raw_ref(refs->packed_ref_store, refname, oid,
388391
referent, type, &ignore_errno)) {
389-
errno = ENOENT;
392+
myerr = ENOENT;
390393
goto out;
391394
}
392395
ret = 0;
@@ -397,7 +400,9 @@ static int files_read_raw_ref(struct ref_store *ref_store, const char *refname,
397400
if (S_ISLNK(st.st_mode)) {
398401
strbuf_reset(&sb_contents);
399402
if (strbuf_readlink(&sb_contents, path, st.st_size) < 0) {
400-
if (errno == ENOENT || errno == EINVAL)
403+
myerr = errno;
404+
errno = 0;
405+
if (myerr == ENOENT || myerr == EINVAL)
401406
/* inconsistent with lstat; retry */
402407
goto stat_ref;
403408
else
@@ -427,7 +432,7 @@ static int files_read_raw_ref(struct ref_store *ref_store, const char *refname,
427432
*/
428433
if (refs_read_raw_ref(refs->packed_ref_store, refname, oid,
429434
referent, type, &ignore_errno)) {
430-
errno = EISDIR;
435+
myerr = EISDIR;
431436
goto out;
432437
}
433438
ret = 0;
@@ -440,34 +445,38 @@ static int files_read_raw_ref(struct ref_store *ref_store, const char *refname,
440445
*/
441446
fd = open(path, O_RDONLY);
442447
if (fd < 0) {
443-
if (errno == ENOENT && !S_ISLNK(st.st_mode))
448+
myerr = errno;
449+
if (myerr == ENOENT && !S_ISLNK(st.st_mode))
444450
/* inconsistent with lstat; retry */
445451
goto stat_ref;
446452
else
447453
goto out;
448454
}
449455
strbuf_reset(&sb_contents);
450456
if (strbuf_read(&sb_contents, fd, 256) < 0) {
451-
int save_errno = errno;
457+
myerr = errno;
452458
close(fd);
453-
errno = save_errno;
454459
goto out;
455460
}
456461
close(fd);
457462
strbuf_rtrim(&sb_contents);
458463
buf = sb_contents.buf;
459464

460-
ret = parse_loose_ref_contents(buf, oid, referent, type);
465+
ret = parse_loose_ref_contents(buf, oid, referent, type, &myerr);
461466

462467
out:
463-
*failure_errno = errno;
468+
if (ret && !myerr)
469+
BUG("returning non-zero %d, should have set myerr!", ret);
470+
*failure_errno = myerr;
471+
464472
strbuf_release(&sb_path);
465473
strbuf_release(&sb_contents);
466474
return ret;
467475
}
468476

469477
int parse_loose_ref_contents(const char *buf, struct object_id *oid,
470-
struct strbuf *referent, unsigned int *type)
478+
struct strbuf *referent, unsigned int *type,
479+
int *failure_errno)
471480
{
472481
const char *p;
473482
if (skip_prefix(buf, "ref:", &buf)) {
@@ -486,7 +495,7 @@ int parse_loose_ref_contents(const char *buf, struct object_id *oid,
486495
if (parse_oid_hex(buf, oid, &p) ||
487496
(*p != '\0' && !isspace(*p))) {
488497
*type |= REF_ISBROKEN;
489-
errno = EINVAL;
498+
*failure_errno = EINVAL;
490499
return -1;
491500
}
492501
return 0;

refs/refs-internal.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -706,10 +706,12 @@ struct ref_store {
706706
};
707707

708708
/*
709-
* Parse contents of a loose ref file.
709+
* Parse contents of a loose ref file. *failure_errno maybe be set to EINVAL for
710+
* invalid contents.
710711
*/
711712
int parse_loose_ref_contents(const char *buf, struct object_id *oid,
712-
struct strbuf *referent, unsigned int *type);
713+
struct strbuf *referent, unsigned int *type,
714+
int *failure_errno);
713715

714716
/*
715717
* Fill in the generic part of refs and add it to our collection of

0 commit comments

Comments
 (0)