diff options
-rw-r--r-- | src/common/compat.c | 16 | ||||
-rw-r--r-- | src/common/util.c | 8 |
2 files changed, 23 insertions, 1 deletions
diff --git a/src/common/compat.c b/src/common/compat.c index fa7b8c2b3..cc8ef3aec 100644 --- a/src/common/compat.c +++ b/src/common/compat.c @@ -126,7 +126,8 @@ typedef struct tor_mmap_impl_t { * size, rounded up to the nearest page.) */ } tor_mmap_impl_t; /** 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_file(const char *filename) { @@ -140,9 +141,11 @@ tor_mmap_file(const char *filename) fd = open(filename, O_RDONLY, 0); if (fd<0) { + int save_errno = errno; int severity = (errno == ENOENT) ? LOG_INFO : LOG_WARN; log_fn(severity, LD_FS,"Could not open \"%s\" for mmap(): %s",filename, strerror(errno)); + errno = save_errno; 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 * return NULL, and bad things will happen. So just fail. */ log_info(LD_FS,"File \"%s\" is empty. Ignoring.",filename); + errno = ERANGE; return NULL; } string = mmap(0, size, PROT_READ, MAP_PRIVATE, fd, 0); if (string == MAP_FAILED) { + int save_errno = errno; close(fd); log_warn(LD_FS,"Could not mmap file \"%s\": %s", filename, strerror(errno)); + errno = save_errno; return NULL; } @@ -196,6 +202,7 @@ tor_mmap_t * tor_mmap_file(const char *filename) { win_mmap_t *res = tor_malloc_zero(sizeof(win_mmap_t)); + int empty = 0; res->file_handle = INVALID_HANDLE_VALUE; res->mmap_handle = NULL; @@ -213,6 +220,7 @@ tor_mmap_file(const char *filename) if (res->base.size == 0) { log_info(LD_FS,"File \"%s\" is empty. Ignoring.",filename); + empty = 1; 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); tor_free(msg); } + if (e == ERROR_FILE_NOT_FOUND || e == ERROR_PATH_NOT_FOUND) + e = ENOENT; + else + e = EINVAL; err: + if (empty) + errno = ERANGE; tor_munmap_file(&res->base); return NULL; } diff --git a/src/common/util.c b/src/common/util.c index 7a5740cc9..27ff0c2ce 100644 --- a/src/common/util.c +++ b/src/common/util.c @@ -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); if (fd<0) { int severity = LOG_WARN; + int save_errno = errno; if (errno == ENOENT && (flags & RFTS_IGNORE_MISSING)) severity = LOG_INFO; log_fn(severity, LD_FS,"Could not open \"%s\": %s ",filename, strerror(errno)); + errno = save_errno; return NULL; } if (fstat(fd, &statbuf)<0) { + int save_errno = errno; close(fd); log_warn(LD_FS,"Could not fstat \"%s\".",filename); + errno = save_errno; 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); if (r<0) { + int save_errno = errno; log_warn(LD_FS,"Error reading from file \"%s\": %s", filename, strerror(errno)); tor_free(string); close(fd); + errno = save_errno; return NULL; } 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) { /* Unless we're using text mode on win32, we'd better have an exact * match for size. */ + int save_errno = errno; log_warn(LD_FS,"Could read only %d of %ld bytes of file \"%s\".", r, (long)statbuf.st_size,filename); tor_free(string); close(fd); + errno = save_errno; return NULL; } close(fd); |