Moved memorymapping files into a separate function
This commit is contained in:
@ -5,31 +5,39 @@
|
||||
|
||||
#include <utils.h>
|
||||
|
||||
void utils_map_file(const wchar_t *path, struct file_mapping *map) {
|
||||
map->file = CreateFileW(path, FILE_READ_ACCESS, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
|
||||
if (map->file == INVALID_HANDLE_VALUE) {
|
||||
msg_err_w(L"Could not open file: %ls", path);
|
||||
}
|
||||
|
||||
map->mapping = CreateFileMappingA(map->file, NULL, PAGE_READONLY, 0, 0, NULL);
|
||||
map->data = MapViewOfFile(map->mapping, FILE_MAP_READ, 0, 0, 0);
|
||||
if (!map->data) {
|
||||
msg_err_w(L"Could not map view of file %ls", path);
|
||||
}
|
||||
}
|
||||
|
||||
void utils_unmap_file(struct file_mapping *map) {
|
||||
UnmapViewOfFile(map->data);
|
||||
CloseHandle(map->mapping);
|
||||
CloseHandle(map->file);
|
||||
}
|
||||
|
||||
int utils_path_exists(const wchar_t *filePath) {
|
||||
return GetFileAttributesW(filePath) != INVALID_FILE_ATTRIBUTES;
|
||||
}
|
||||
|
||||
uint32_t utils_file_crc32c(const wchar_t *filePath) {
|
||||
HANDLE file = CreateFileW(filePath, FILE_READ_ACCESS, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
|
||||
if (!file) {
|
||||
msg_err_w(L"Could not open file: %ls", filePath);
|
||||
}
|
||||
struct file_mapping map;
|
||||
utils_map_file(filePath, &map);
|
||||
|
||||
LARGE_INTEGER fileSize;
|
||||
GetFileSizeEx(file, &fileSize);
|
||||
GetFileSizeEx(map.file, &fileSize);
|
||||
|
||||
HANDLE hMap = CreateFileMappingA(file, NULL, PAGE_READONLY, 0, 0, NULL);
|
||||
char *map = MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0);
|
||||
if (!map) {
|
||||
msg_err_w(L"Could not create file mapping for %ls", filePath);
|
||||
}
|
||||
|
||||
uint32_t crc = crc32c(0, map, fileSize.QuadPart);
|
||||
|
||||
UnmapViewOfFile(map);
|
||||
CloseHandle(hMap);
|
||||
CloseHandle(file);
|
||||
uint32_t crc = crc32c(0, map.data, fileSize.QuadPart);
|
||||
|
||||
utils_unmap_file(&map);
|
||||
return crc;
|
||||
}
|
||||
|
||||
@ -51,8 +59,8 @@ void utils_create_parent_dirs(const wchar_t *path) {
|
||||
|
||||
void utils_save_to_file(const wchar_t *filePath, const void *buf, size_t length) {
|
||||
HANDLE file = CreateFileW(filePath, FILE_WRITE_ACCESS, FILE_SHARE_READ, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
|
||||
if (!file) {
|
||||
msg_err_w(L"Could not open file: %ls", filePath);
|
||||
if (file == INVALID_HANDLE_VALUE) {
|
||||
msg_err_w(L"Could not create file: %ls", filePath);
|
||||
}
|
||||
|
||||
WriteFile(file, buf, length, NULL, FALSE);
|
||||
|
||||
Reference in New Issue
Block a user