bool file_copy_using_memory_map2( _In_ const wchar_t* src_file, _In_ const wchar_t* dst_file ) { _ASSERTE(NULL != src_file); _ASSERTE(NULL != dst_file); if (NULL == src_file || NULL == dst_file) return false; if (!is_file_existsW(src_file)) { print("err ] no src file = %ws", src_file); return false; } if (is_file_existsW(dst_file)) { DeleteFileW(dst_file); } // map src, dst file pmap_context src_ctx = open_map_context(src_file); pmap_context dst_ctx = create_map_context(dst_file, src_ctx->size); if (NULL == src_ctx || NULL == dst_ctx) { print("err ] open_map_context() failed."); close_map_context(src_ctx); close_map_context(dst_ctx); return false; } // copy src to dst by mmio uint32_t i = 0; uint32_t bs = 0; uint32_t rest = src_ctx->size; while (0 < rest) { if (rest > 4096) { bs = 4096; rest -= 4096; } else { bs = rest; rest = 0; } RtlCopyMemory(&dst_ctx->view[i], &src_ctx->view[i], bs); i += bs; } return true; }
/** * @brief * @param * @see * @remarks * @code * @endcode * @return **/ bool file_copy_using_memory_map( _In_ const wchar_t* src_file, _In_ const wchar_t* dst_file ) { _ASSERTE(NULL != src_file); _ASSERTE(NULL != dst_file); if (NULL == src_file || NULL == dst_file) return false; if (!is_file_existsW(src_file)) { print("err ] no src file = %ws", src_file); return false; } if (is_file_existsW(dst_file)) { DeleteFileW(dst_file); } // map src, dst file pmap_context src_ctx = open_map_context(src_file); pmap_context dst_ctx = create_map_context(dst_file, src_ctx->size); if (NULL == src_ctx || NULL == dst_ctx) { print("err ] open_map_context() failed."); close_map_context(src_ctx); close_map_context(dst_ctx); return false; } // copy src to dst by mmio for (uint32_t i = 0; i < src_ctx->size; ++i) { dst_ctx->view[i] = src_ctx->view[i]; } close_map_context(src_ctx); close_map_context(dst_ctx); return true; }
/** * @brief * @param * @see * @remarks * @code * @endcode * @return **/ bool file_copy_using_memory_map( _In_ const wchar_t* src_file, _In_ const wchar_t* dst_file ) { _ASSERTE(NULL != src_file); _ASSERTE(NULL != dst_file); if (NULL == src_file || NULL == dst_file) return false; if (!is_file_existsW(src_file)) { print("err ] no src file = %ws", src_file); return false; } if (is_file_existsW(dst_file)) { DeleteFileW(dst_file); } static DWORD AllocationGranularity = 0; if (0 == AllocationGranularity) { SYSTEM_INFO si = { 0 }; GetSystemInfo(&si); AllocationGranularity = si.dwAllocationGranularity; } pmap_context src_ctx = open_map_context(src_file); pmap_context dest_ctx = user_create_map_context(dst_file, src_ctx->size); LARGE_INTEGER FileOffset = { 0 }; LARGE_INTEGER FileSize = { dest_ctx->size }; LARGE_INTEGER i = { 0 }; LARGE_INTEGER BytesInBlock = { 0 }; PUCHAR buffer = (PUCHAR)malloc((uint64_t)(1024 * 1024)); while (FileSize.QuadPart > 0) { BytesInBlock.QuadPart = AllocationGranularity * 1; if (FileSize.QuadPart < AllocationGranularity * 1) { BytesInBlock.QuadPart = FileSize.QuadPart; } dest_ctx->view = (PCHAR)MapViewOfFile( dest_ctx->map, FILE_MAP_WRITE, (FileOffset.HighPart), (FileOffset.LowPart), BytesInBlock.QuadPart ); if (dest_ctx->view == NULL) { print("err ] MapViewOfFile( %ws ) failed. gle = %u", dst_file, GetLastError()); break; } RtlCopyMemory(buffer, &src_ctx->view[FileOffset.QuadPart], BytesInBlock.QuadPart); RtlCopyMemory(&dest_ctx->view[FileOffset.QuadPart], buffer, BytesInBlock.QuadPart); UnmapViewOfFile(dest_ctx->view); FileOffset.QuadPart += BytesInBlock.QuadPart; FileSize.QuadPart -= BytesInBlock.QuadPart; } CloseHandle(dest_ctx->map); CloseHandle(dest_ctx->handle); return true; }