mirror of
https://gitlab.torproject.org/tpo/core/tor.git
synced 2024-11-24 12:23:32 +01:00
r16100@catbus: nickm | 2007-10-24 11:33:52 -0400
Make tor_mmap_file() set and preserve errno in a useful way. svn:r12153
This commit is contained in:
parent
ab3fddd11e
commit
99d72f7295
@ -126,7 +126,8 @@ typedef struct tor_mmap_impl_t {
|
|||||||
* size, rounded up to the nearest page.) */
|
* size, rounded up to the nearest page.) */
|
||||||
} tor_mmap_impl_t;
|
} tor_mmap_impl_t;
|
||||||
/** Try to create a memory mapping for <b>filename</b> and return it. On
|
/** Try to create a memory mapping for <b>filename</b> and return it. On
|
||||||
* failure, return NULL. */
|
* failure, return NULL. Sets errno properly, using ERANGE to mean
|
||||||
|
* "empty file". */
|
||||||
tor_mmap_t *
|
tor_mmap_t *
|
||||||
tor_mmap_file(const char *filename)
|
tor_mmap_file(const char *filename)
|
||||||
{
|
{
|
||||||
@ -140,9 +141,11 @@ tor_mmap_file(const char *filename)
|
|||||||
|
|
||||||
fd = open(filename, O_RDONLY, 0);
|
fd = open(filename, O_RDONLY, 0);
|
||||||
if (fd<0) {
|
if (fd<0) {
|
||||||
|
int save_errno = errno;
|
||||||
int severity = (errno == ENOENT) ? LOG_INFO : LOG_WARN;
|
int severity = (errno == ENOENT) ? LOG_INFO : LOG_WARN;
|
||||||
log_fn(severity, LD_FS,"Could not open \"%s\" for mmap(): %s",filename,
|
log_fn(severity, LD_FS,"Could not open \"%s\" for mmap(): %s",filename,
|
||||||
strerror(errno));
|
strerror(errno));
|
||||||
|
errno = save_errno;
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -156,14 +159,17 @@ tor_mmap_file(const char *filename)
|
|||||||
/* Zero-length file. If we call mmap on it, it will succeed but
|
/* Zero-length file. If we call mmap on it, it will succeed but
|
||||||
* return NULL, and bad things will happen. So just fail. */
|
* return NULL, and bad things will happen. So just fail. */
|
||||||
log_info(LD_FS,"File \"%s\" is empty. Ignoring.",filename);
|
log_info(LD_FS,"File \"%s\" is empty. Ignoring.",filename);
|
||||||
|
errno = ERANGE;
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
string = mmap(0, size, PROT_READ, MAP_PRIVATE, fd, 0);
|
string = mmap(0, size, PROT_READ, MAP_PRIVATE, fd, 0);
|
||||||
if (string == MAP_FAILED) {
|
if (string == MAP_FAILED) {
|
||||||
|
int save_errno = errno;
|
||||||
close(fd);
|
close(fd);
|
||||||
log_warn(LD_FS,"Could not mmap file \"%s\": %s", filename,
|
log_warn(LD_FS,"Could not mmap file \"%s\": %s", filename,
|
||||||
strerror(errno));
|
strerror(errno));
|
||||||
|
errno = save_errno;
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -196,6 +202,7 @@ tor_mmap_t *
|
|||||||
tor_mmap_file(const char *filename)
|
tor_mmap_file(const char *filename)
|
||||||
{
|
{
|
||||||
win_mmap_t *res = tor_malloc_zero(sizeof(win_mmap_t));
|
win_mmap_t *res = tor_malloc_zero(sizeof(win_mmap_t));
|
||||||
|
int empty = 0;
|
||||||
res->file_handle = INVALID_HANDLE_VALUE;
|
res->file_handle = INVALID_HANDLE_VALUE;
|
||||||
res->mmap_handle = NULL;
|
res->mmap_handle = NULL;
|
||||||
|
|
||||||
@ -213,6 +220,7 @@ tor_mmap_file(const char *filename)
|
|||||||
|
|
||||||
if (res->base.size == 0) {
|
if (res->base.size == 0) {
|
||||||
log_info(LD_FS,"File \"%s\" is empty. Ignoring.",filename);
|
log_info(LD_FS,"File \"%s\" is empty. Ignoring.",filename);
|
||||||
|
empty = 1;
|
||||||
goto err;
|
goto err;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -243,7 +251,13 @@ tor_mmap_file(const char *filename)
|
|||||||
log_fn(severity, LD_FS, "Couldn't mmap file \"%s\": %s", filename, msg);
|
log_fn(severity, LD_FS, "Couldn't mmap file \"%s\": %s", filename, msg);
|
||||||
tor_free(msg);
|
tor_free(msg);
|
||||||
}
|
}
|
||||||
|
if (e == ERROR_FILE_NOT_FOUND || e == ERROR_PATH_NOT_FOUND)
|
||||||
|
e = ENOENT;
|
||||||
|
else
|
||||||
|
e = EINVAL;
|
||||||
err:
|
err:
|
||||||
|
if (empty)
|
||||||
|
errno = ERANGE;
|
||||||
tor_munmap_file(&res->base);
|
tor_munmap_file(&res->base);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
@ -1782,16 +1782,20 @@ read_file_to_str(const char *filename, int flags, struct stat *stat_out)
|
|||||||
fd = open(filename,O_RDONLY|(bin?O_BINARY:O_TEXT),0);
|
fd = open(filename,O_RDONLY|(bin?O_BINARY:O_TEXT),0);
|
||||||
if (fd<0) {
|
if (fd<0) {
|
||||||
int severity = LOG_WARN;
|
int severity = LOG_WARN;
|
||||||
|
int save_errno = errno;
|
||||||
if (errno == ENOENT && (flags & RFTS_IGNORE_MISSING))
|
if (errno == ENOENT && (flags & RFTS_IGNORE_MISSING))
|
||||||
severity = LOG_INFO;
|
severity = LOG_INFO;
|
||||||
log_fn(severity, LD_FS,"Could not open \"%s\": %s ",filename,
|
log_fn(severity, LD_FS,"Could not open \"%s\": %s ",filename,
|
||||||
strerror(errno));
|
strerror(errno));
|
||||||
|
errno = save_errno;
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fstat(fd, &statbuf)<0) {
|
if (fstat(fd, &statbuf)<0) {
|
||||||
|
int save_errno = errno;
|
||||||
close(fd);
|
close(fd);
|
||||||
log_warn(LD_FS,"Could not fstat \"%s\".",filename);
|
log_warn(LD_FS,"Could not fstat \"%s\".",filename);
|
||||||
|
errno = save_errno;
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1802,10 +1806,12 @@ read_file_to_str(const char *filename, int flags, struct stat *stat_out)
|
|||||||
|
|
||||||
r = read_all(fd,string,(size_t)statbuf.st_size,0);
|
r = read_all(fd,string,(size_t)statbuf.st_size,0);
|
||||||
if (r<0) {
|
if (r<0) {
|
||||||
|
int save_errno = errno;
|
||||||
log_warn(LD_FS,"Error reading from file \"%s\": %s", filename,
|
log_warn(LD_FS,"Error reading from file \"%s\": %s", filename,
|
||||||
strerror(errno));
|
strerror(errno));
|
||||||
tor_free(string);
|
tor_free(string);
|
||||||
close(fd);
|
close(fd);
|
||||||
|
errno = save_errno;
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
string[r] = '\0'; /* NUL-terminate the result. */
|
string[r] = '\0'; /* NUL-terminate the result. */
|
||||||
@ -1825,10 +1831,12 @@ read_file_to_str(const char *filename, int flags, struct stat *stat_out)
|
|||||||
if (r != statbuf.st_size) {
|
if (r != statbuf.st_size) {
|
||||||
/* Unless we're using text mode on win32, we'd better have an exact
|
/* Unless we're using text mode on win32, we'd better have an exact
|
||||||
* match for size. */
|
* match for size. */
|
||||||
|
int save_errno = errno;
|
||||||
log_warn(LD_FS,"Could read only %d of %ld bytes of file \"%s\".",
|
log_warn(LD_FS,"Could read only %d of %ld bytes of file \"%s\".",
|
||||||
r, (long)statbuf.st_size,filename);
|
r, (long)statbuf.st_size,filename);
|
||||||
tor_free(string);
|
tor_free(string);
|
||||||
close(fd);
|
close(fd);
|
||||||
|
errno = save_errno;
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
close(fd);
|
close(fd);
|
||||||
|
Loading…
Reference in New Issue
Block a user