#include <windows.h>
#include <sys/stat.h>
#include <sys/file.h>
#include "file.h"
#include "out.h"
#include "os.h"
int
util_tmpfile(const char *dir, const char *templ, int flags)
{
LOG(3, "dir \"%s\" template \"%s\" flags %x", dir, templ, flags);
ASSERT(flags == 0 || flags == O_EXCL);
int oerrno;
int fd = -1;
size_t len = strlen(dir) + strlen(templ) + 1;
char *fullname = Malloc(sizeof(*fullname) * len);
if (fullname == NULL) {
ERR("!Malloc");
return -1;
}
int ret = _snprintf(fullname, len, "%s%s", dir, templ);
if (ret < 0 || ret >= len) {
ERR("!snprintf");
goto err;
}
LOG(4, "fullname \"%s\"", fullname);
fd = os_mkstemp(fullname);
if (fd < 0) {
ERR("!os_mkstemp");
goto err;
}
Free(fullname);
return fd;
err:
Free(fullname);
oerrno = errno;
if (fd != -1)
(void) os_close(fd);
errno = oerrno;
return -1;
}
int
util_is_absolute_path(const char *path)
{
LOG(3, "path \"%s\"", path);
if (path == NULL || path[0] == '\0')
return 0;
if (path[0] == '\\' || path[1] == ':')
return 1;
return 0;
}
int
util_file_mkdir(const char *path, mode_t mode)
{
UNREFERENCED_PARAMETER(mode);
LOG(3, "path: %s mode: %d", path, mode);
return _mkdir(path);
}
int
util_file_dir_open(struct dir_handle *handle, const char *path)
{
handle->handle = NULL;
handle->path = path;
return 0;
}
int
util_file_dir_next(struct dir_handle *handle, struct file_info *info)
{
WIN32_FIND_DATAA data;
if (handle->handle == NULL) {
handle->handle = FindFirstFileA(handle->path, &data);
if (handle->handle == NULL)
return 1;
} else {
if (FindNextFileA(handle->handle, &data) == 0)
return 1;
}
info->filename[NAME_MAX] = '\0';
strncpy(info->filename, data.cFileName, NAME_MAX + 1);
if (info->filename[NAME_MAX] != '\0')
return -1;
info->is_dir = data.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY;
return 0;
}
int
util_file_dir_close(struct dir_handle *handle)
{
return FindClose(handle->handle);
}
int
util_file_dir_remove(const char *path)
{
return RemoveDirectoryA(path) == 0 ? -1 : 0;
}
size_t
util_file_device_dax_alignment(const char *path)
{
LOG(3, "path \"%s\"", path);
return 0;
}