aboutsummaryrefslogtreecommitdiff
path: root/lib/util/src/w32_dir_iterator.c
blob: b1eef2c9bbce5cc2df1d77beb77c073e8ce37e49 (plain)
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
/* SPDX-License-Identifier: LGPL-3.0-or-later */
/*
 * w32_dir_iterator.c
 *
 * Copyright (C) 2023 David Oberhollenzer <goliath@infraroot.at>
 */
#include "config.h"
#include "util/dir_iterator.h"
#include "util/util.h"
#include "sqfs/error.h"

#include <windows.h>
#include <stdlib.h>
#include <stdio.h>

#define UNIX_EPOCH_ON_W32 11644473600UL
#define W32_TICS_PER_SEC 10000000UL

typedef struct {
	dir_iterator_t base;

	WIN32_FIND_DATAW ent;
	HANDLE dirhnd;
	int state;
	bool is_first;
} dir_iterator_win32_t;

static sqfs_s64 w32time_to_unix(const FILETIME *ft)
{
	sqfs_u64 w32ts;

	w32ts = ft->dwHighDateTime;
	w32ts <<= 32UL;
	w32ts |= ft->dwLowDateTime;

	w32ts /= W32_TICS_PER_SEC;

	if (w32ts <= UNIX_EPOCH_ON_W32)
		return -((sqfs_s64)(UNIX_EPOCH_ON_W32 - w32ts));

	return w32ts - UNIX_EPOCH_ON_W32;
}

static int dir_iterator_read_link(dir_iterator_t *it, char **out)
{
	(void)it;
	*out = NULL;
	return SQFS_ERROR_UNSUPPORTED;
}

static int dir_iterator_next(dir_iterator_t *it, dir_entry_t **out)
{
	dir_iterator_win32_t *w32 = (dir_iterator_win32_t *)it;
	dir_entry_t *ent = NULL;
	DWORD length;

	if (w32->state == 0 && !w32->is_first) {
		if (!FindNextFileW(w32->dirhnd, &w32->ent)) {
			if (GetLastError() == ERROR_NO_MORE_FILES) {
				w32->state = 1;
			} else {
				w32->state = SQFS_ERROR_IO;
			}
		}
	}

	w32->is_first = false;

	if (w32->state != 0)
		goto out;

	length = WideCharToMultiByte(CP_UTF8, 0, w32->ent.cFileName,
				     -1, NULL, 0, NULL, NULL);
	if (length <= 0) {
		w32->state = SQFS_ERROR_ALLOC;
		goto out;
	}

	ent = alloc_flex(sizeof(*ent), 1, length + 1);
	if (ent == NULL) {
		w32->state = SQFS_ERROR_ALLOC;
		goto out;
	}

	WideCharToMultiByte(CP_UTF8, 0, w32->ent.cFileName, -1,
			    ent->name, length + 1, NULL, NULL);

	if (w32->ent.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
		ent->mode = S_IFDIR | 0755;
	} else {
		ent->mode = S_IFREG | 0644;
	}

	ent->mtime = w32time_to_unix(&(w32->ent.ftLastWriteTime));
out:
	*out = ent;
	return w32->state;
}

static void dir_iterator_destroy(sqfs_object_t *obj)
{
	dir_iterator_win32_t *dir = (dir_iterator_win32_t *)obj;

	FindClose(dir->dirhnd);
	free(dir);
}

dir_iterator_t *dir_iterator_create(const char *path)
{
	WCHAR *wpath = NULL, *new = NULL;
	WIN32_FIND_DATAW first_entry;
	dir_iterator_win32_t *it;
	size_t len, newlen;
	HANDLE dirhnd;

	/* convert path to UTF-16, append "\\*" */
	wpath = path_to_windows(path);
	if (wpath == NULL) {
		fprintf(stderr, "%s: allocation failure.\n", path);
		return NULL;
	}

	for (len = 0; wpath[len] != '\0'; ++len)
		;

	newlen = len + 1;

	if (len > 0 && wpath[len - 1] != '\\')
		newlen += 1;

	new = realloc(wpath, sizeof(wpath[0]) * (newlen + 1));
	if (new == NULL) {
		fprintf(stderr, "%s: allocation failure.\n", path);
		free(wpath);
		return NULL;
	}

	wpath = new;

	if (len > 0 && wpath[len - 1] != '\\')
		wpath[len++] = '\\';

	wpath[len++] = '*';
	wpath[len++] = '\0';

	/* get the directory handle AND the first entry */
	dirhnd = FindFirstFileW(wpath, &first_entry);

	if (dirhnd == INVALID_HANDLE_VALUE) {
		w32_perror(path);
		free(wpath);
		return NULL;
	}

	free(wpath);
	wpath = NULL;

	/* wrap it up */
	it = calloc(1, sizeof(*it));
	if (it == NULL) {
		fprintf(stderr, "%s: allocation failure.\n", path);
		FindClose(dirhnd);
		return NULL;
	}

	sqfs_object_init(it, dir_iterator_destroy, NULL);

	((dir_iterator_t *)it)->next = dir_iterator_next;
	((dir_iterator_t *)it)->read_link = dir_iterator_read_link;

	it->is_first = true;
	it->state = 0;
	it->dirhnd = dirhnd;
	it->ent = first_entry;
	return (dir_iterator_t *)it;
}