23 #ifdef HAVE_SYS_STAT_H
40 #if defined(HAVE_MMAP) || defined(RUNNING_DOXYGEN)
46 tor_mmap_file,(
const char *filename))
52 size_t size, filesize;
59 int save_errno = errno;
61 log_fn(severity,
LD_FS,
"Could not open \"%s\" for mmap(): %s",filename,
68 result = fstat(fd, &st);
70 int save_errno = errno;
72 "Couldn't fstat opened descriptor for \"%s\" during mmap: %s",
73 filename, strerror(errno));
78 size = filesize = (size_t)(st.st_size);
81 log_warn(
LD_FS,
"File \"%s\" is too large. Ignoring.",filename);
89 log_info(
LD_FS,
"File \"%s\" is empty. Ignoring.",filename);
95 string = mmap(0, size, PROT_READ, MAP_PRIVATE, fd, 0);
97 if (
string == MAP_FAILED) {
98 int save_errno = errno;
99 log_warn(
LD_FS,
"Could not mmap file \"%s\": %s", filename,
107 res->
size = filesize;
108 res->mapping_size = size;
122 res = munmap((
char*)handle->
data, handle->mapping_size);
127 log_warn(
LD_FS,
"Failed to munmap() in tor_munmap_file(): %s",
134 #elif defined(_WIN32)
136 tor_mmap_file,(
const char *filename))
138 TCHAR tfilename[MAX_PATH]= {0};
141 HANDLE file_handle = INVALID_HANDLE_VALUE;
142 DWORD size_low, size_high;
144 res->mmap_handle = NULL;
146 mbstowcs(tfilename,filename,MAX_PATH);
148 strlcpy(tfilename,filename,MAX_PATH);
150 file_handle = CreateFile(tfilename,
151 GENERIC_READ, FILE_SHARE_READ,
154 FILE_ATTRIBUTE_NORMAL,
157 if (file_handle == INVALID_HANDLE_VALUE)
160 size_low = GetFileSize(file_handle, &size_high);
162 if (size_low == INVALID_FILE_SIZE && GetLastError() != NO_ERROR) {
163 log_warn(
LD_FS,
"Error getting size of \"%s\".",filename);
166 if (size_low == 0 && size_high == 0) {
167 log_info(
LD_FS,
"File \"%s\" is empty. Ignoring.",filename);
171 real_size = (((uint64_t)size_high)<<32) | size_low;
172 if (real_size > SIZE_MAX) {
173 log_warn(
LD_FS,
"File \"%s\" is too big to map; not trying.",filename);
176 res->
size = real_size;
178 res->mmap_handle = CreateFileMapping(file_handle,
184 if (res->mmap_handle == NULL)
186 res->
data = (
char*) MapViewOfFile(res->mmap_handle,
192 CloseHandle(file_handle);
195 DWORD e = GetLastError();
196 int severity = (e == ERROR_FILE_NOT_FOUND || e == ERROR_PATH_NOT_FOUND) ?
198 char *msg = format_win32_error(e);
199 log_fn(severity,
LD_FS,
"Couldn't mmap file \"%s\": %s", filename, msg);
201 if (e == ERROR_FILE_NOT_FOUND || e == ERROR_PATH_NOT_FOUND)
209 if (file_handle != INVALID_HANDLE_VALUE)
210 CloseHandle(file_handle);
211 tor_munmap_file(res);
225 BOOL ok = UnmapViewOfFile( (LPVOID) handle->
data);
227 log_warn(
LD_FS,
"Failed to UnmapViewOfFile() in tor_munmap_file(): %d",
228 (
int)GetLastError());
232 if (handle->mmap_handle != NULL)
233 CloseHandle(handle->mmap_handle);
239 #error "cannot implement tor_mmap_file"
Header for compat_string.c.
Wrappers for reading and writing data to files on disk.
int tor_open_cloexec(const char *path, int flags, unsigned mode)
#define log_fn(severity, domain, args,...)
Headers for util_malloc.c.
#define MOCK_IMPL(rv, funcname, arglist)
Macros to manage assertions, fatal and non-fatal.