forked from mmozeiko/pkg2zip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpkg2zip_sys.c
149 lines (125 loc) · 3.4 KB
/
pkg2zip_sys.c
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
#include "pkg2zip_sys.h"
#include "pkg2zip_utils.h"
#if defined(_WIN32)
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
sys_file sys_open(const char* fname, uint64_t* size)
{
WCHAR path[MAX_PATH];
MultiByteToWideChar(CP_UTF8, 0, fname, -1, path, MAX_PATH);
HANDLE handle = CreateFileW(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
if (handle == INVALID_HANDLE_VALUE)
{
fatal("ERROR: cannot open '%s' file\n", fname);
}
LARGE_INTEGER sz;
if (!GetFileSizeEx(handle, &sz))
{
fatal("ERROR: cannot get size of '%s' file\n", fname);
}
*size = sz.QuadPart;
return handle;
}
sys_file sys_create(const char* fname)
{
WCHAR path[MAX_PATH];
MultiByteToWideChar(CP_UTF8, 0, fname, -1, path, MAX_PATH);
HANDLE handle = CreateFileW(path, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
if (handle == INVALID_HANDLE_VALUE)
{
fatal("ERROR: cannot create '%s' file\n", fname);
}
return handle;
}
void sys_close(sys_file file)
{
if (!CloseHandle(file))
{
fatal("ERROR: failed to close file\n");
}
}
void sys_read(sys_file file, uint64_t offset, void* buffer, uint32_t size)
{
DWORD read;
OVERLAPPED ov;
ov.hEvent = NULL;
ov.Offset = (uint32_t)offset;
ov.OffsetHigh = (uint32_t)(offset >> 32);
if (!ReadFile(file, buffer, size, &read, &ov) || read != size)
{
fatal("ERROR: failed to read %u bytes from file\n", size);
}
}
void sys_write(sys_file file, uint64_t offset, const void* buffer, uint32_t size)
{
DWORD written;
OVERLAPPED ov;
ov.hEvent = NULL;
ov.Offset = (uint32_t)offset;
ov.OffsetHigh = (uint32_t)(offset >> 32);
if (!WriteFile(file, buffer, size, &written, &ov) || written != size)
{
fatal("ERROR: failed to write %u bytes to file\n", size);
}
}
#else
#define _FILE_OFFSET_BITS 64
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
sys_file sys_open(const char* fname, uint64_t* size)
{
int fd = open(fname, O_RDONLY);
if (fd < 0)
{
fatal("ERROR: cannot open '%s' file\n", fname);
}
struct stat st;
if (fstat(fd, &st) != 0)
{
fatal("ERROR: cannot get size of '%s' file\n", fname);
}
*size = st.st_size;
return (void*)(intptr_t)fd;
}
sys_file sys_create(const char* fname)
{
int fd = open(fname, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
if (fd < 0)
{
fatal("ERROR: cannot create '%s' file\n", fname);
}
return (void*)(intptr_t)fd;
}
void sys_close(sys_file file)
{
if (close((int)(intptr_t)file) != 0)
{
fatal("ERROR: failed to close file\n");
}
}
void sys_read(sys_file file, uint64_t offset, void* buffer, uint32_t size)
{
ssize_t read = pread((int)(intptr_t)file, buffer, size, offset);
if (read != size)
{
fatal("ERROR: failed to read %u bytes from file\n", size);
}
}
void sys_write(sys_file file, uint64_t offset, const void* buffer, uint32_t size)
{
ssize_t wrote = pwrite((int)(intptr_t)file, buffer, size, offset);
if (wrote != size)
{
fatal("ERROR: failed to read %u bytes from file\n", size);
}
}
#endif
void sys_rename(const char* oldfname, const char* newfname)
{
if (rename(oldfname, newfname) == -1)
{
fatal("ERROR: failed to rename the file from %s to %s\n", oldfname, newfname);
}
}