2017-10-05 23:32:57 -07:00
|
|
|
/* mz_os_posix.c -- System functions for posix
|
2019-04-08 13:26:32 -07:00
|
|
|
Version 2.8.6, April 8, 2019
|
2017-10-05 23:32:57 -07:00
|
|
|
part of the MiniZip project
|
|
|
|
|
|
2019-01-08 16:07:10 -08:00
|
|
|
Copyright (C) 2010-2019 Nathan Moinvaziri
|
2017-10-05 23:32:57 -07:00
|
|
|
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.
|
|
|
|
|
*/
|
|
|
|
|
|
2018-11-20 16:56:21 -08:00
|
|
|
#include "mz.h"
|
|
|
|
|
#include "mz_strm.h"
|
|
|
|
|
#include "mz_os.h"
|
|
|
|
|
|
2018-11-21 15:05:58 -08:00
|
|
|
#include <stdio.h> /* rename */
|
2017-10-05 23:32:57 -07:00
|
|
|
#include <errno.h>
|
2018-10-30 15:31:49 -07:00
|
|
|
#include <iconv.h>
|
2017-10-05 23:32:57 -07:00
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
2018-11-20 16:56:21 -08:00
|
|
|
#if defined(__APPLE__) || defined(__unix__)
|
2018-10-25 21:28:26 -07:00
|
|
|
# include <utime.h>
|
|
|
|
|
# include <unistd.h>
|
|
|
|
|
#endif
|
|
|
|
|
#if defined(__APPLE__)
|
|
|
|
|
# include <mach/clock.h>
|
|
|
|
|
# include <mach/mach.h>
|
2018-09-18 12:06:54 +02:00
|
|
|
#endif
|
|
|
|
|
|
2017-10-05 23:32:57 -07:00
|
|
|
/***************************************************************************/
|
|
|
|
|
|
2018-10-30 15:31:49 -07:00
|
|
|
uint8_t *mz_os_utf8_string_create(const char *string, int32_t encoding)
|
|
|
|
|
{
|
|
|
|
|
iconv_t cd;
|
|
|
|
|
const char *from_encoding = NULL;
|
2019-05-01 09:10:29 -07:00
|
|
|
size_t result = 0;
|
2018-10-30 15:31:49 -07:00
|
|
|
size_t string_length = 0;
|
|
|
|
|
size_t string_utf8_size = 0;
|
|
|
|
|
uint8_t *string_utf8 = NULL;
|
|
|
|
|
uint8_t *string_utf8_ptr = NULL;
|
|
|
|
|
|
|
|
|
|
if (string == NULL)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
if (encoding == MZ_ENCODING_CODEPAGE_437)
|
|
|
|
|
from_encoding = "CP437";
|
|
|
|
|
else if (encoding == MZ_ENCODING_CODEPAGE_932)
|
|
|
|
|
from_encoding = "CP932";
|
|
|
|
|
else if (encoding == MZ_ENCODING_CODEPAGE_936)
|
|
|
|
|
from_encoding = "CP936";
|
|
|
|
|
else if (encoding == MZ_ENCODING_CODEPAGE_950)
|
|
|
|
|
from_encoding = "CP950";
|
|
|
|
|
else if (encoding == MZ_ENCODING_UTF8)
|
|
|
|
|
from_encoding = "UTF-8";
|
|
|
|
|
else
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
cd = iconv_open("UTF-8", from_encoding);
|
|
|
|
|
if (cd == (iconv_t)-1)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
string_length = strlen(string);
|
|
|
|
|
string_utf8_size = string_length * 2;
|
|
|
|
|
string_utf8 = (uint8_t *)MZ_ALLOC((int32_t)(string_utf8_size + 1));
|
|
|
|
|
string_utf8_ptr = string_utf8;
|
|
|
|
|
|
|
|
|
|
if (string_utf8)
|
|
|
|
|
{
|
|
|
|
|
memset(string_utf8, 0, string_utf8_size + 1);
|
|
|
|
|
|
|
|
|
|
result = iconv(cd, (char **)&string, &string_length,
|
|
|
|
|
(char **)&string_utf8_ptr, &string_utf8_size);
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-01 09:10:29 -07:00
|
|
|
iconv_close(cd);
|
|
|
|
|
|
|
|
|
|
if (result == (size_t)-1)
|
2018-10-30 15:31:49 -07:00
|
|
|
{
|
|
|
|
|
MZ_FREE(string_utf8);
|
|
|
|
|
string_utf8 = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return string_utf8;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void mz_os_utf8_string_delete(uint8_t **string)
|
|
|
|
|
{
|
|
|
|
|
if (string != NULL)
|
|
|
|
|
{
|
|
|
|
|
MZ_FREE(*string);
|
|
|
|
|
*string = NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/***************************************************************************/
|
|
|
|
|
|
2018-10-25 19:35:50 -07:00
|
|
|
int32_t mz_os_rand(uint8_t *buf, int32_t size)
|
2018-07-22 12:15:04 -07:00
|
|
|
{
|
|
|
|
|
static unsigned calls = 0;
|
|
|
|
|
int32_t i = 0;
|
|
|
|
|
|
2018-11-20 16:56:21 -08:00
|
|
|
/* Ensure different random header each time */
|
2018-07-22 12:15:04 -07:00
|
|
|
if (++calls == 1)
|
|
|
|
|
{
|
|
|
|
|
#define PI_SEED 3141592654UL
|
|
|
|
|
srand((unsigned)(time(NULL) ^ PI_SEED));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (i < size)
|
|
|
|
|
buf[i++] = (rand() >> 7) & 0xff;
|
|
|
|
|
|
|
|
|
|
return size;
|
|
|
|
|
}
|
2017-10-05 23:32:57 -07:00
|
|
|
|
2018-10-25 19:35:50 -07:00
|
|
|
int32_t mz_os_rename(const char *source_path, const char *target_path)
|
2018-07-31 10:06:12 -07:00
|
|
|
{
|
|
|
|
|
if (rename(source_path, target_path) == -1)
|
|
|
|
|
return MZ_EXIST_ERROR;
|
|
|
|
|
|
|
|
|
|
return MZ_OK;
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-02 21:07:39 -07:00
|
|
|
int32_t mz_os_unlink(const char *path)
|
2018-07-31 10:06:12 -07:00
|
|
|
{
|
|
|
|
|
if (unlink(path) == -1)
|
|
|
|
|
return MZ_EXIST_ERROR;
|
|
|
|
|
|
|
|
|
|
return MZ_OK;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-25 19:35:50 -07:00
|
|
|
int32_t mz_os_file_exists(const char *path)
|
2017-10-23 18:27:18 -07:00
|
|
|
{
|
2018-11-24 17:08:11 -08:00
|
|
|
struct stat path_stat;
|
2017-10-23 18:27:18 -07:00
|
|
|
|
2018-11-24 17:08:11 -08:00
|
|
|
memset(&path_stat, 0, sizeof(path_stat));
|
|
|
|
|
if (stat(path, &path_stat) == 0)
|
2017-10-23 18:32:53 -07:00
|
|
|
return MZ_OK;
|
|
|
|
|
return MZ_EXIST_ERROR;
|
2017-10-23 18:27:18 -07:00
|
|
|
}
|
|
|
|
|
|
2018-10-25 19:35:50 -07:00
|
|
|
int64_t mz_os_get_file_size(const char *path)
|
2017-10-23 18:27:18 -07:00
|
|
|
{
|
2018-11-24 17:08:11 -08:00
|
|
|
struct stat path_stat;
|
2017-10-23 18:27:18 -07:00
|
|
|
|
2018-11-24 17:08:11 -08:00
|
|
|
memset(&path_stat, 0, sizeof(path_stat));
|
|
|
|
|
if (stat(path, &path_stat) == 0)
|
|
|
|
|
{
|
|
|
|
|
/* Stat returns size taken up by directory entry, so return 0 */
|
|
|
|
|
if (S_ISDIR(path_stat.st_mode))
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
return path_stat.st_size;
|
|
|
|
|
}
|
2017-10-23 18:27:18 -07:00
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-25 19:35:50 -07:00
|
|
|
int32_t mz_os_get_file_date(const char *path, time_t *modified_date, time_t *accessed_date, time_t *creation_date)
|
2017-10-05 23:32:57 -07:00
|
|
|
{
|
2018-11-24 17:08:11 -08:00
|
|
|
struct stat path_stat;
|
2017-10-22 14:32:37 -07:00
|
|
|
char *name = NULL;
|
|
|
|
|
size_t len = 0;
|
2017-10-20 07:59:39 -07:00
|
|
|
int32_t err = MZ_INTERNAL_ERROR;
|
2017-10-05 23:32:57 -07:00
|
|
|
|
2018-11-24 17:08:11 -08:00
|
|
|
memset(&path_stat, 0, sizeof(path_stat));
|
2017-10-05 23:32:57 -07:00
|
|
|
|
|
|
|
|
if (strcmp(path, "-") != 0)
|
|
|
|
|
{
|
2018-11-20 16:56:21 -08:00
|
|
|
/* Not all systems allow stat'ing a file with / appended */
|
2017-10-22 14:32:37 -07:00
|
|
|
len = strlen(path);
|
|
|
|
|
name = (char *)malloc(len + 1);
|
2019-05-01 11:37:21 -07:00
|
|
|
strncpy(name, path, len + 1);
|
2019-05-02 21:07:39 -07:00
|
|
|
mz_path_remove_slash(name);
|
2017-10-05 23:32:57 -07:00
|
|
|
|
2018-11-24 17:08:11 -08:00
|
|
|
if (stat(name, &path_stat) == 0)
|
2017-10-05 23:32:57 -07:00
|
|
|
{
|
2017-10-22 14:32:37 -07:00
|
|
|
if (modified_date != NULL)
|
2018-11-24 17:08:11 -08:00
|
|
|
*modified_date = path_stat.st_mtime;
|
2017-10-22 14:32:37 -07:00
|
|
|
if (accessed_date != NULL)
|
2018-11-24 17:08:11 -08:00
|
|
|
*accessed_date = path_stat.st_atime;
|
2018-11-20 16:56:21 -08:00
|
|
|
/* Creation date not supported */
|
2017-10-22 14:32:37 -07:00
|
|
|
if (creation_date != NULL)
|
|
|
|
|
*creation_date = 0;
|
|
|
|
|
|
2017-10-05 23:32:57 -07:00
|
|
|
err = MZ_OK;
|
|
|
|
|
}
|
2017-10-22 14:32:37 -07:00
|
|
|
|
2017-10-05 23:32:57 -07:00
|
|
|
free(name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-25 19:35:50 -07:00
|
|
|
int32_t mz_os_set_file_date(const char *path, time_t modified_date, time_t accessed_date, time_t creation_date)
|
2017-10-05 23:32:57 -07:00
|
|
|
{
|
|
|
|
|
struct utimbuf ut;
|
|
|
|
|
|
2017-10-22 14:32:37 -07:00
|
|
|
ut.actime = accessed_date;
|
|
|
|
|
ut.modtime = modified_date;
|
2018-11-24 17:08:11 -08:00
|
|
|
|
2018-11-20 16:56:21 -08:00
|
|
|
/* Creation date not supported */
|
2018-11-24 17:08:11 -08:00
|
|
|
MZ_UNUSED(creation_date);
|
2017-10-05 23:32:57 -07:00
|
|
|
|
|
|
|
|
if (utime(path, &ut) != 0)
|
|
|
|
|
return MZ_INTERNAL_ERROR;
|
|
|
|
|
|
|
|
|
|
return MZ_OK;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-25 19:35:50 -07:00
|
|
|
int32_t mz_os_get_file_attribs(const char *path, uint32_t *attributes)
|
2018-05-01 13:45:08 -07:00
|
|
|
{
|
2018-11-24 17:08:11 -08:00
|
|
|
struct stat path_stat;
|
2018-05-01 13:45:08 -07:00
|
|
|
int32_t err = MZ_OK;
|
|
|
|
|
|
2018-11-24 17:08:11 -08:00
|
|
|
memset(&path_stat, 0, sizeof(path_stat));
|
|
|
|
|
if (stat(path, &path_stat) == -1)
|
2018-05-01 13:45:08 -07:00
|
|
|
err = MZ_INTERNAL_ERROR;
|
2018-11-24 17:08:11 -08:00
|
|
|
*attributes = path_stat.st_mode;
|
2018-05-01 13:45:08 -07:00
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-25 19:35:50 -07:00
|
|
|
int32_t mz_os_set_file_attribs(const char *path, uint32_t attributes)
|
2018-05-01 13:45:08 -07:00
|
|
|
{
|
|
|
|
|
int32_t err = MZ_OK;
|
|
|
|
|
|
2018-05-09 09:42:31 -07:00
|
|
|
if (chmod(path, (mode_t)attributes) == -1)
|
2018-05-01 13:45:08 -07:00
|
|
|
err = MZ_INTERNAL_ERROR;
|
|
|
|
|
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-25 19:35:50 -07:00
|
|
|
int32_t mz_os_make_dir(const char *path)
|
2017-10-05 23:32:57 -07:00
|
|
|
{
|
2017-10-20 07:59:39 -07:00
|
|
|
int32_t err = 0;
|
2017-10-05 23:32:57 -07:00
|
|
|
|
|
|
|
|
err = mkdir(path, 0755);
|
|
|
|
|
|
|
|
|
|
if (err != 0 && errno != EEXIST)
|
|
|
|
|
return MZ_INTERNAL_ERROR;
|
|
|
|
|
|
|
|
|
|
return MZ_OK;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-25 19:35:50 -07:00
|
|
|
DIR* mz_os_open_dir(const char *path)
|
2017-10-05 23:32:57 -07:00
|
|
|
{
|
|
|
|
|
return opendir(path);
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-25 19:35:50 -07:00
|
|
|
struct dirent* mz_os_read_dir(DIR *dir)
|
2017-10-05 23:32:57 -07:00
|
|
|
{
|
|
|
|
|
if (dir == NULL)
|
|
|
|
|
return NULL;
|
|
|
|
|
return readdir(dir);
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-25 19:35:50 -07:00
|
|
|
int32_t mz_os_close_dir(DIR *dir)
|
2017-10-05 23:32:57 -07:00
|
|
|
{
|
|
|
|
|
if (dir == NULL)
|
|
|
|
|
return MZ_PARAM_ERROR;
|
|
|
|
|
if (closedir(dir) == -1)
|
|
|
|
|
return MZ_INTERNAL_ERROR;
|
|
|
|
|
return MZ_OK;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-25 19:35:50 -07:00
|
|
|
int32_t mz_os_is_dir(const char *path)
|
2017-10-05 23:32:57 -07:00
|
|
|
{
|
|
|
|
|
struct stat path_stat;
|
2018-11-24 17:08:11 -08:00
|
|
|
|
|
|
|
|
memset(&path_stat, 0, sizeof(path_stat));
|
2017-10-05 23:32:57 -07:00
|
|
|
stat(path, &path_stat);
|
2017-10-16 18:50:32 -07:00
|
|
|
if (S_ISDIR(path_stat.st_mode))
|
2017-10-05 23:32:57 -07:00
|
|
|
return MZ_OK;
|
2018-11-24 17:08:11 -08:00
|
|
|
|
2017-10-05 23:32:57 -07:00
|
|
|
return MZ_EXIST_ERROR;
|
2017-10-06 00:02:28 -07:00
|
|
|
}
|
2018-09-18 12:06:54 +02:00
|
|
|
|
2019-05-02 21:07:39 -07:00
|
|
|
int32_t mz_os_is_symlink(const char *path)
|
|
|
|
|
{
|
|
|
|
|
struct stat path_stat;
|
|
|
|
|
|
|
|
|
|
memset(&path_stat, 0, sizeof(path_stat));
|
|
|
|
|
stat(path, &path_stat);
|
|
|
|
|
if (S_ISLNK(path_stat.st_mode))
|
|
|
|
|
return MZ_OK;
|
|
|
|
|
|
|
|
|
|
return MZ_EXIST_ERROR;
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-02 21:57:46 -07:00
|
|
|
int32_t mz_os_make_symlink(const char *path, const char *target_path)
|
2019-05-02 21:07:39 -07:00
|
|
|
{
|
|
|
|
|
if (symlink(target_path, path) != 0)
|
|
|
|
|
return MZ_INTERNAL_ERROR;
|
|
|
|
|
return MZ_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int32_t mz_os_read_symlink(const char *path, char *target_path, int32_t max_target_path)
|
|
|
|
|
{
|
|
|
|
|
int32_t length = 0;
|
|
|
|
|
|
|
|
|
|
if ((length = readlink(path, target_path, max_target_path - 1)) < 0)
|
|
|
|
|
return MZ_EXIST_ERROR;
|
|
|
|
|
|
|
|
|
|
target_path[length] = 0;
|
|
|
|
|
return MZ_OK;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-25 19:35:50 -07:00
|
|
|
uint64_t mz_os_ms_time(void)
|
2018-09-18 20:25:48 -07:00
|
|
|
{
|
2018-09-18 12:06:54 +02:00
|
|
|
struct timespec ts;
|
|
|
|
|
|
2018-10-25 21:31:23 -07:00
|
|
|
#if defined(__APPLE__)
|
2018-09-18 12:06:54 +02:00
|
|
|
clock_serv_t cclock;
|
|
|
|
|
mach_timespec_t mts;
|
2018-09-18 20:25:48 -07:00
|
|
|
|
2018-09-18 12:06:54 +02:00
|
|
|
host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
|
|
|
|
|
clock_get_time(cclock, &mts);
|
|
|
|
|
mach_port_deallocate(mach_task_self(), cclock);
|
2018-09-18 20:25:48 -07:00
|
|
|
|
2018-09-18 12:06:54 +02:00
|
|
|
ts.tv_sec = mts.tv_sec;
|
|
|
|
|
ts.tv_nsec = mts.tv_nsec;
|
|
|
|
|
#else
|
|
|
|
|
clock_gettime(CLOCK_MONOTONIC, &ts);
|
|
|
|
|
#endif
|
|
|
|
|
|
2018-10-08 22:41:16 -07:00
|
|
|
return ((uint64_t)ts.tv_sec * 1000) + ((uint64_t)ts.tv_nsec / 1000000);
|
2018-09-18 12:06:54 +02:00
|
|
|
}
|