config.c: avoid xmmap error messages
The config-writing code uses xmmap to map the existing config file, which will die if the map fails. This has two downsides: 1. The error message is not very helpful, as it lacks any context about the file we are mapping: $ mkdir foo $ git config --file=foo some.key value fatal: Out of memory? mmap failed: No such device 2. We normally do not die in this code path; instead, we'd rather report the error and return an appropriate exit status (which is part of the public interface documented in git-config.1). This patch introduces a "gentle" form of xmmap which lets us produce our own error message. We do not want to use mmap directly, because we would like to use the other compatibility elements of xmmap (e.g., handling 0-length maps portably). The end result is: $ git.compile config --file=foo some.key value error: unable to mmap 'foo': No such device $ echo $? 3 Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
parent
3a1b3126ed
commit
1570856b51
11
config.c
11
config.c
@ -2048,8 +2048,15 @@ int git_config_set_multivar_in_file(const char *config_filename,
|
|||||||
|
|
||||||
fstat(in_fd, &st);
|
fstat(in_fd, &st);
|
||||||
contents_sz = xsize_t(st.st_size);
|
contents_sz = xsize_t(st.st_size);
|
||||||
contents = xmmap(NULL, contents_sz, PROT_READ,
|
contents = xmmap_gently(NULL, contents_sz, PROT_READ,
|
||||||
MAP_PRIVATE, in_fd, 0);
|
MAP_PRIVATE, in_fd, 0);
|
||||||
|
if (contents == MAP_FAILED) {
|
||||||
|
error("unable to mmap '%s': %s",
|
||||||
|
config_filename, strerror(errno));
|
||||||
|
ret = CONFIG_INVALID_FILE;
|
||||||
|
contents = NULL;
|
||||||
|
goto out_free;
|
||||||
|
}
|
||||||
close(in_fd);
|
close(in_fd);
|
||||||
|
|
||||||
if (chmod(lock->filename.buf, st.st_mode & 07777) < 0) {
|
if (chmod(lock->filename.buf, st.st_mode & 07777) < 0) {
|
||||||
|
@ -623,6 +623,7 @@ extern char *xstrndup(const char *str, size_t len);
|
|||||||
extern void *xrealloc(void *ptr, size_t size);
|
extern void *xrealloc(void *ptr, size_t size);
|
||||||
extern void *xcalloc(size_t nmemb, size_t size);
|
extern void *xcalloc(size_t nmemb, size_t size);
|
||||||
extern void *xmmap(void *start, size_t length, int prot, int flags, int fd, off_t offset);
|
extern void *xmmap(void *start, size_t length, int prot, int flags, int fd, off_t offset);
|
||||||
|
extern void *xmmap_gently(void *start, size_t length, int prot, int flags, int fd, off_t offset);
|
||||||
extern ssize_t xread(int fd, void *buf, size_t len);
|
extern ssize_t xread(int fd, void *buf, size_t len);
|
||||||
extern ssize_t xwrite(int fd, const void *buf, size_t len);
|
extern ssize_t xwrite(int fd, const void *buf, size_t len);
|
||||||
extern ssize_t xpread(int fd, void *buf, size_t len, off_t offset);
|
extern ssize_t xpread(int fd, void *buf, size_t len, off_t offset);
|
||||||
|
15
sha1_file.c
15
sha1_file.c
@ -707,8 +707,8 @@ static void mmap_limit_check(size_t length)
|
|||||||
(uintmax_t)length, (uintmax_t)limit);
|
(uintmax_t)length, (uintmax_t)limit);
|
||||||
}
|
}
|
||||||
|
|
||||||
void *xmmap(void *start, size_t length,
|
void *xmmap_gently(void *start, size_t length,
|
||||||
int prot, int flags, int fd, off_t offset)
|
int prot, int flags, int fd, off_t offset)
|
||||||
{
|
{
|
||||||
void *ret;
|
void *ret;
|
||||||
|
|
||||||
@ -719,12 +719,19 @@ void *xmmap(void *start, size_t length,
|
|||||||
return NULL;
|
return NULL;
|
||||||
release_pack_memory(length);
|
release_pack_memory(length);
|
||||||
ret = mmap(start, length, prot, flags, fd, offset);
|
ret = mmap(start, length, prot, flags, fd, offset);
|
||||||
if (ret == MAP_FAILED)
|
|
||||||
die_errno("Out of memory? mmap failed");
|
|
||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void *xmmap(void *start, size_t length,
|
||||||
|
int prot, int flags, int fd, off_t offset)
|
||||||
|
{
|
||||||
|
void *ret = xmmap_gently(start, length, prot, flags, fd, offset);
|
||||||
|
if (ret == MAP_FAILED)
|
||||||
|
die_errno("Out of memory? mmap failed");
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
void close_pack_windows(struct packed_git *p)
|
void close_pack_windows(struct packed_git *p)
|
||||||
{
|
{
|
||||||
while (p->windows) {
|
while (p->windows) {
|
||||||
|
Loading…
Reference in New Issue
Block a user