dep-minizip-ng/mz_os_posix.c

228 lines
4.8 KiB
C
Raw Normal View History

/* mz_os_posix.c -- System functions for posix
2018-10-28 16:47:53 -07:00
Version 2.7.0, October 28, 2018
part of the MiniZip project
2018-01-06 08:49:03 -08:00
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>
2018-08-31 23:19:54 +02:00
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
2018-10-25 21:28:26 -07:00
#if defined(__APPLE__) || defined(unix)
# include <utime.h>
# include <unistd.h>
#endif
#if defined(__APPLE__)
# include <mach/clock.h>
# include <mach/mach.h>
#endif
#include "mz.h"
2017-10-06 00:02:28 -07:00
#include "mz_strm.h"
#include "mz_os.h"
/***************************************************************************/
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;
// 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;
}
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;
}
int32_t mz_os_delete(const char *path)
2018-07-31 10:06:12 -07:00
{
if (unlink(path) == -1)
return MZ_EXIST_ERROR;
return MZ_OK;
}
int32_t mz_os_file_exists(const char *path)
{
struct stat stat_info;
memset(&stat_info, 0, sizeof(stat_info));
if (stat(path, &stat_info) == 0)
2017-10-23 18:32:53 -07:00
return MZ_OK;
2017-10-23 18:32:53 -07:00
return MZ_EXIST_ERROR;
}
int64_t mz_os_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_os_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);
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_os_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_os_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_os_set_file_attribs(const char *path, uint32_t attributes)
{
int32_t err = MZ_OK;
2018-05-09 09:42:31 -07:00
if (chmod(path, (mode_t)attributes) == -1)
err = MZ_INTERNAL_ERROR;
return err;
}
int32_t mz_os_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_os_open_dir(const char *path)
{
return opendir(path);
}
struct dirent* mz_os_read_dir(DIR *dir)
{
if (dir == NULL)
return NULL;
return readdir(dir);
}
int32_t mz_os_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_os_is_dir(const char *path)
{
struct stat path_stat;
stat(path, &path_stat);
2017-10-16 18:50:32 -07:00
if (S_ISDIR(path_stat.st_mode))
return MZ_OK;
return MZ_EXIST_ERROR;
2017-10-06 00:02:28 -07:00
}
uint64_t mz_os_ms_time(void)
2018-09-18 20:25:48 -07:00
{
struct timespec ts;
2018-10-25 21:31:23 -07:00
#if defined(__APPLE__)
clock_serv_t cclock;
mach_timespec_t mts;
2018-09-18 20:25:48 -07: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
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);
}