-
Notifications
You must be signed in to change notification settings - Fork 1
/
extract_file_item.cpp
107 lines (96 loc) · 2.47 KB
/
extract_file_item.cpp
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
// CEZEO software Ltd. https://www.cezeo.com
#include "extract_file_item.h"
#include <fcntl.h>
#include "string_utils.h"
namespace cab
{
extract_file_item::extract_file_item(const std::wstring& name, const size_t size) : fileHandle(INVALID_HANDLE_VALUE), extract_item(name, size)
{
}
extract_file_item::~extract_file_item()
{
// fileHandle is stored in the class
fnFileClose(0);
}
FNOPEN(extract_file_item::fnFileOpen)
{
if (fileHandle == INVALID_HANDLE_VALUE)
{
DWORD desiredAccess = 0;
DWORD creationDisposition = 0;
UNREFERENCED_PARAMETER(pmode);
if (oflag & _O_RDWR)
{
desiredAccess = GENERIC_READ | GENERIC_WRITE;
}
else if (oflag & _O_WRONLY)
{
desiredAccess = GENERIC_WRITE;
}
else
{
desiredAccess = GENERIC_READ;
}
if (oflag & _O_CREAT)
{
creationDisposition = CREATE_ALWAYS;
}
else
{
creationDisposition = OPEN_EXISTING;
}
// convert from UTF-8 because we use it in worker
fileName = StringUtils::MbToWide(pszFile, CP_UTF8);
fileHandle = CreateFile(fileName.c_str(), desiredAccess, FILE_SHARE_READ, NULL, creationDisposition, FILE_ATTRIBUTE_NORMAL, NULL);
}
// always return this, we'll use it as pointer to this file.
return (INT_PTR)this;
}
FNREAD(extract_file_item::fnFileRead)
{
// hf - this pointer
DWORD bytesRead = (DWORD)-1;
if (fileHandle != INVALID_HANDLE_VALUE)
{
if (ReadFile(fileHandle, pv, cb, &bytesRead, NULL) == FALSE)
{
bytesRead = (DWORD)-1;
}
}
return bytesRead;
}
FNWRITE(extract_file_item::fnFileWrite)
{
// hf - this pointer
DWORD bytesWritten = (DWORD)-1;
if (fileHandle != INVALID_HANDLE_VALUE)
{
if (WriteFile(fileHandle, pv, cb, &bytesWritten, NULL) == FALSE)
{
bytesWritten = (DWORD)-1;
}
}
return bytesWritten;
}
FNSEEK(extract_file_item::fnFileSeek)
{
// hf - this pointer, but we don't need it
long result = -1;
if (fileHandle != INVALID_HANDLE_VALUE)
{
result = (long)SetFilePointer(fileHandle, dist, NULL, seektype);
}
return result;
}
FNCLOSE(extract_file_item::fnFileClose)
{
// hf - this pointer
int result = -1;
if (fileHandle != INVALID_HANDLE_VALUE)
{
result = CloseHandle(fileHandle) ? 0 : -1;
fileHandle = INVALID_HANDLE_VALUE;
}
return result;
}
}; // namespace cab