forked from somma/_MyLib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileIoHelper.cpp
More file actions
164 lines (141 loc) · 4.9 KB
/
FileIoHelper.cpp
File metadata and controls
164 lines (141 loc) · 4.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*-----------------------------------------------------------------------------
* FileIoHelper.cpp
*-----------------------------------------------------------------------------
*
*-----------------------------------------------------------------------------
* All rights reserved by somma (fixbrain@gmail.com, unsorted@msn.com)
*-----------------------------------------------------------------------------
* - 01.09.2010 created
**---------------------------------------------------------------------------*/
#include "stdafx.h"
#include <crtdbg.h>
#include "Win32Utils.h"
#include "FileIoHelper.h"
/**----------------------------------------------------------------------------
\brief
\param
\return
\code
\endcode
-----------------------------------------------------------------------------*/
bool OpenFileContext(IN PCWSTR FilePath, IN bool ReadOnly, OUT PFILE_CTX& Ctx)
{
_ASSERTE(NULL != FilePath);
if (NULL == FilePath) return false;
if (!is_file_existsW(FilePath)) return false;;
_ASSERT(NULL == Ctx);
if (NULL != Ctx) return false;;
Ctx = (PFILE_CTX) malloc(sizeof(FILE_CTX));
if (NULL == Ctx) return false;
RtlZeroMemory(Ctx, sizeof(FILE_CTX));
bool ret = false;
#pragma warning(disable: 4127)
do
{
Ctx->FileHandle = CreateFileW(
(LPCWSTR)FilePath,
(true == ReadOnly) ? GENERIC_READ : GENERIC_READ | GENERIC_WRITE,
NULL,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL
);
if (INVALID_HANDLE_VALUE == Ctx->FileHandle)
{
log_err
"CreateFile(%ws) failed, gle = %u",
FilePath,
GetLastError()
log_end
break;
}
// check file size
//
LARGE_INTEGER fileSize;
if (TRUE != GetFileSizeEx(Ctx->FileHandle, &fileSize))
{
log_err
"%ws, can not get file size, gle = %u",
FilePath,
GetLastError()
log_end
break;
}
// [ WARN ]
//
// 4Gb 이상의 파일의 경우 MapViewOfFile()에서 오류가 나거나
// 파일 포인터 이동이 문제가 됨
// FilIoHelperClass 모듈을 이용해야 함
//
_ASSERTE(fileSize.HighPart == 0);
if (fileSize.HighPart > 0)
{
log_err
"file size = %I64d (over 4GB) can not handle. use FileIoHelperClass",
fileSize.QuadPart
log_end
break;
}
Ctx->FileSize = (DWORD)fileSize.QuadPart;
Ctx->FileMap = CreateFileMapping(
Ctx->FileHandle,
NULL,
(true == ReadOnly) ? PAGE_READONLY : PAGE_READWRITE,
0,
0,
NULL
);
if (NULL == Ctx->FileMap)
{
log_err
"CreateFileMapping(%ws) failed, gle = %u",
FilePath,
GetLastError()
log_end
break;
}
Ctx->FileView = (PCHAR) MapViewOfFile(
Ctx->FileMap,
(true == ReadOnly) ? FILE_MAP_READ : FILE_MAP_WRITE,
0,
0,
0
);
if(Ctx->FileView == NULL)
{
log_err
"MapViewOfFile(%ws) failed, gle = %u",
FilePath,
GetLastError()
log_end
break;
}
ret = true;
} while (FALSE);
#pragma warning(default: 4127)
if (!ret)
{
if (NULL!= Ctx->FileView) UnmapViewOfFile(Ctx->FileView);
if (NULL!= Ctx->FileMap) CloseHandle(Ctx->FileMap);
if (INVALID_HANDLE_VALUE != Ctx->FileHandle) CloseHandle(Ctx->FileHandle);
free(Ctx);
}
return ret;
}
/**----------------------------------------------------------------------------
\brief
\param
\return
\code
\endcode
-----------------------------------------------------------------------------*/
void CloseFileContext(IN PFILE_CTX& Ctx)
{
_ASSERTE(NULL != Ctx);
if (NULL == Ctx) return;
if (INVALID_HANDLE_VALUE!= Ctx->FileHandle) CloseHandle(Ctx->FileHandle);
if (NULL!= Ctx->FileMap) CloseHandle(Ctx->FileMap);
if (NULL!= Ctx->FileView) UnmapViewOfFile(Ctx->FileView);
free(Ctx);
}