Loading src/common/compat.c +15 −1 Original line number Diff line number Diff line Loading @@ -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) { Loading @@ -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; } Loading @@ -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; } Loading Loading @@ -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; Loading @@ -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; } Loading Loading @@ -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; } Loading src/common/util.c +8 −0 Original line number Diff line number Diff line Loading @@ -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; } Loading @@ -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. */ Loading @@ -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); Loading Loading
src/common/compat.c +15 −1 Original line number Diff line number Diff line Loading @@ -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) { Loading @@ -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; } Loading @@ -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; } Loading Loading @@ -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; Loading @@ -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; } Loading Loading @@ -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; } Loading
src/common/util.c +8 −0 Original line number Diff line number Diff line Loading @@ -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; } Loading @@ -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. */ Loading @@ -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); Loading