forked from zlib-ng/minizip-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmz_os_posix.c
More file actions
262 lines (215 loc) · 5.66 KB
/
mz_os_posix.c
File metadata and controls
262 lines (215 loc) · 5.66 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/* mz_os_posix.c -- System functions for posix
Version 2.5.2, August 27, 2018
part of the MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
https://github.com/nmoinvaz/minizip
This program is distributed under the terms of the same license as zlib.
See the accompanying LICENSE file for the full text of the license.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#ifdef HAVE_GETRANDOM
# include <sys/random.h>
#endif
#if defined unix || defined __APPLE__
# include <unistd.h>
# include <utime.h>
# define HAVE_ARC4RANDOM_BUF
#endif
#if defined(HAVE_LIBBSD) && \
!defined(MZ_ZIP_NO_COMPRESSION) && \
!defined(MZ_ZIP_NO_ENCRYPTION)
# include <bsd/stdlib.h> // arc4random_buf
#else
# include <stdlib.h>
#endif
#include "mz.h"
#include "mz_strm.h"
#include "mz_os.h"
#include "mz_os_posix.h"
/***************************************************************************/
#if !defined(MZ_ZIP_NO_COMPRESSION) && !defined(MZ_ZIP_NO_ENCRYPTION)
#if defined(HAVE_ARC4RANDOM_BUF)
int32_t mz_posix_rand(uint8_t *buf, int32_t size)
{
arc4random_buf(buf, size);
return size;
}
#elif defined(HAVE_ARC4RANDOM)
int32_t mz_posix_rand(uint8_t *buf, int32_t size)
{
int32_t left = size;
for (; left > 2; left -= 3, buf += 3)
{
uint32_t val = arc4random();
buf[0] = (val) & 0xFF;
buf[1] = (val >> 8) & 0xFF;
buf[2] = (val >> 16) & 0xFF;
}
for (; left > 0; left--, buf++)
{
*buf = arc4random() & 0xFF;
}
return size - left;
}
#elif defined(HAVE_GETRANDOM)
int32_t mz_posix_rand(uint8_t *buf, int32_t size)
{
int32_t left = size;
int32_t written = 0;
while (left > 0)
{
written = getrandom(buf, left, 0);
if (written < 0)
return MZ_INTERNAL_ERROR;
buf += written;
left -= written;
}
return size - left;
}
#else
#if !defined(FORCE_LOWQUALITY_ENTROPY)
# error "Low quality entropy function used for encryption"
#endif
int32_t mz_posix_rand(uint8_t *buf, int32_t size)
{
static unsigned calls = 0;
int32_t i = 0;
// Ensure different random header each time
if (++calls == 1)
{
#define PI_SEED 3141592654UL
srand((unsigned)(time(NULL) ^ PI_SEED));
}
while (i < size)
buf[i++] = (rand() >> 7) & 0xff;
return size;
}
#endif
#endif
int32_t mz_posix_rename(const char *source_path, const char *target_path)
{
if (rename(source_path, target_path) == -1)
return MZ_EXIST_ERROR;
return MZ_OK;
}
int32_t mz_posix_delete(const char *path)
{
if (unlink(path) == -1)
return MZ_EXIST_ERROR;
return MZ_OK;
}
int32_t mz_posix_file_exists(const char *path)
{
struct stat stat_info;
memset(&stat_info, 0, sizeof(stat_info));
if (stat(path, &stat_info) == 0)
return MZ_OK;
return MZ_EXIST_ERROR;
}
int64_t mz_posix_get_file_size(const char *path)
{
struct stat stat_info;
memset(&stat_info, 0, sizeof(stat_info));
if (stat(path, &stat_info) == 0)
return stat_info.st_size;
return 0;
}
int32_t mz_posix_get_file_date(const char *path, time_t *modified_date, time_t *accessed_date, time_t *creation_date)
{
struct stat stat_info;
char *name = NULL;
size_t len = 0;
int32_t err = MZ_INTERNAL_ERROR;
memset(&stat_info, 0, sizeof(stat_info));
if (strcmp(path, "-") != 0)
{
// Not all systems allow stat'ing a file with / appended
len = strlen(path);
name = (char *)malloc(len + 1);
strncpy(name, path, len + 1);
name[len] = 0;
if (name[len - 1] == '/')
name[len - 1] = 0;
if (stat(name, &stat_info) == 0)
{
if (modified_date != NULL)
*modified_date = stat_info.st_mtime;
if (accessed_date != NULL)
*accessed_date = stat_info.st_atime;
// Creation date not supported
if (creation_date != NULL)
*creation_date = 0;
err = MZ_OK;
}
free(name);
}
return err;
}
int32_t mz_posix_set_file_date(const char *path, time_t modified_date, time_t accessed_date, time_t creation_date)
{
struct utimbuf ut;
ut.actime = accessed_date;
ut.modtime = modified_date;
// Creation date not supported
(void)creation_date;
if (utime(path, &ut) != 0)
return MZ_INTERNAL_ERROR;
return MZ_OK;
}
int32_t mz_posix_get_file_attribs(const char *path, uint32_t *attributes)
{
struct stat stat_info;
int32_t err = MZ_OK;
memset(&stat_info, 0, sizeof(stat_info));
if (stat(path, &stat_info) == -1)
err = MZ_INTERNAL_ERROR;
*attributes = stat_info.st_mode;
return err;
}
int32_t mz_posix_set_file_attribs(const char *path, uint32_t attributes)
{
int32_t err = MZ_OK;
if (chmod(path, (mode_t)attributes) == -1)
err = MZ_INTERNAL_ERROR;
return err;
}
int32_t mz_posix_make_dir(const char *path)
{
int32_t err = 0;
err = mkdir(path, 0755);
if (err != 0 && errno != EEXIST)
return MZ_INTERNAL_ERROR;
return MZ_OK;
}
DIR* mz_posix_open_dir(const char *path)
{
return opendir(path);
}
struct dirent* mz_posix_read_dir(DIR *dir)
{
if (dir == NULL)
return NULL;
return readdir(dir);
}
int32_t mz_posix_close_dir(DIR *dir)
{
if (dir == NULL)
return MZ_PARAM_ERROR;
if (closedir(dir) == -1)
return MZ_INTERNAL_ERROR;
return MZ_OK;
}
int32_t mz_posix_is_dir(const char *path)
{
struct stat path_stat;
stat(path, &path_stat);
if (S_ISDIR(path_stat.st_mode))
return MZ_OK;
return MZ_EXIST_ERROR;
}