aboutsummaryrefslogtreecommitdiff
path: root/src/platform/windows/win_file.c
blob: e10f2848ceb05add9eaaa0b02c6817c17c7b4324 (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
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
263
264
265
266
267
268
269
270
//
// Copyright 2017 Garrett D'Amore <garrett@damore.org>
// Copyright 2017 Capitar IT Group BV <info@capitar.com>
//
// This software is supplied under the terms of the MIT License, a
// copy of which should be located in the distribution where this
// file was obtained (LICENSE.txt).  A copy of the license may also be
// found online at https://opensource.org/licenses/MIT.
//

#include "core/nng_impl.h"

#ifdef NNG_PLATFORM_WINDOWS

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

// File support.

// nni_plat_file_put writes the named file, with the provided data,
// and the given size.  If the file already exists it is overwritten.
// The permissions on the file should be limited to read and write
// access by the entity running the application only.
int
nni_plat_file_put(const char *name, const void *data, size_t len)
{
	HANDLE h;
	int    rv = 0;
	DWORD  nwrite;

	h = CreateFile(name, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
	    FILE_ATTRIBUTE_NORMAL, NULL);
	if (h == INVALID_HANDLE_VALUE) {
		return (nni_win_error(GetLastError()));
	}

	if (!WriteFile(h, data, (DWORD) len, &nwrite, NULL)) {
		rv = nni_win_error(GetLastError());
		(void) DeleteFile(name);
		goto done;
	}
	// These are regular files, synchronous operations.  If we got a
	// short write, then we should have gotten an error!
	NNI_ASSERT(nwrite == len);

done:
	(void) CloseHandle(h);
	return (rv);
}

// nni_plat_file_get reads the entire named file, allocating storage
// to receive the data and returning the data and the size in the
// reference arguments.
int
nni_plat_file_get(const char *name, void **datap, size_t *lenp)
{
	int    rv = 0;
	void * data;
	DWORD  sz;
	DWORD  nread;
	HANDLE h;

	h = CreateFile(name, GENERIC_READ, 0, NULL, OPEN_EXISTING,
	    FILE_ATTRIBUTE_NORMAL, NULL);
	if (h == INVALID_HANDLE_VALUE) {
		return (nni_win_error(GetLastError()));
	}
	// We choose not to support extraordinarily large files (>4GB)
	if ((sz = GetFileSize(h, NULL)) == INVALID_FILE_SIZE) {
		rv = nni_win_error(GetLastError());
		goto done;
	}
	if ((data = nni_alloc((size_t) sz)) == NULL) {
		rv = NNG_ENOMEM;
		goto done;
	}
	if (!ReadFile(h, data, sz, &nread, NULL)) {
		rv = nni_win_error(GetLastError());
		nni_free(data, sz);
		goto done;
	}

	// We can get a short read, indicating end of file.  We return
	// the actual number of bytes read.  The fact that the data buffer
	// is larger than this is ok, because our nni_free() routine just
	// uses HeapFree(), which doesn't need a matching size.

	*datap = data;
	*lenp  = (size_t) nread;
done:
	(void) CloseHandle(h);
	return (rv);
}

// nni_plat_file_delete deletes the named file.
int
nni_plat_file_delete(const char *name)
{
	if (!DeleteFile(name)) {
		return (nni_win_error(GetLastError()));
	}
	return (0);
}

// nni_plat_dir_open attempts to "open a directory" for listing.  The
// handle for further operations is returned in the first argument, and
// the directory name is supplied in the second.
struct dirhandle {
	HANDLE          dirh;
	WIN32_FIND_DATA data;
	int             cont; // zero on first read, 1 thereafter.
};

int
nni_plat_dir_open(void **dhp, const char *name)
{
	struct dirhandle *dh;
	char              fullpath[MAX_PATH + 1];

	if ((dh = NNI_ALLOC_STRUCT(dh)) == NULL) {
		return (NNG_ENOMEM);
	}

	// Append wildcard to directory name
	_snprintf(fullpath, sizeof(fullpath), "%s\\*", name);

	if ((dh->dirh = FindFirstFile(fullpath, &dh->data)) ==
	    INVALID_HANDLE_VALUE) {
		int rv;
		rv = nni_win_error(GetLastError());
		NNI_FREE_STRUCT(dh);
		return (rv);
	}
	dh->cont = 0;
	*dhp     = dh;

	return (0);
}

// nni_plat_dir_next gets the next directory entry.  Each call returns
// a new entry (arbitrary order).  When no more entries exist, it returns
// NNG_ENOENT.
int
nni_plat_dir_next(void *dir, const char **namep)
{
	struct dirhandle *dh = dir;
	int               rv;

	if (dh->dirh == INVALID_HANDLE_VALUE) {
		return (NNG_ENOENT);
	}
	if (dh->cont) {
		// We need to read another entry
		if (!FindNextFile(dh->dirh, &dh->data)) {
			rv = GetLastError();
			FindClose(dh->dirh);
			dh->dirh = INVALID_HANDLE_VALUE;
			if (rv == ERROR_NO_MORE_FILES) {
				return (NNG_ENOENT);
			}
			return (nni_win_error(rv));
		}
	}
	dh->cont = 1;

	// Skip over directories.
	while (dh->data.dwFileAttributes &
	    (FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_HIDDEN)) {
		if (!FindNextFile(dh->dirh, &dh->data)) {
			rv = GetLastError();
			FindClose(dh->dirh);
			dh->dirh = INVALID_HANDLE_VALUE;
			if (rv == ERROR_NO_MORE_FILES) {
				return (NNG_ENOENT);
			}
			return (nni_win_error(rv));
		}
	}

	// Got a good entry.
	*namep = dh->data.cFileName;
	return (0);
}

// nni_plat_dir_close closes the directory handle, freeing all
// resources associated with it.
void
nni_plat_dir_close(void *dir)
{
	struct dirhandle *dh = dir;
	if (dh->dirh != INVALID_HANDLE_VALUE) {
		FindClose(dh->dirh);
	}
	NNI_FREE_STRUCT(dh);
}

int
nni_plat_dir_create(const char *name)
{
	char   parent[MAX_PATH + 1];
	size_t len;

	nni_strlcpy(parent, name, sizeof(parent));
	len = strlen(parent);
	while (len > 0) {
		if ((parent[len - 1] == '/') || (parent[len - 1] == '\\')) {
			parent[len - 1] = '\0';
			break;
		}
		len--;
	}

	if (!CreateDirectoryEx(parent, name, NULL)) {
		int rv = GetLastError();
		if (rv == ERROR_ALREADY_EXISTS) {
			return (0);
		}
		return (nni_win_error(rv));
	}
	return (0);
}

int
nni_plat_dir_remove(const char *name)
{
	if (!RemoveDirectory(name)) {
		int rv = GetLastError();
		if (rv == ERROR_PATH_NOT_FOUND) {
			return (0);
		}
		return (nni_win_error(rv));
	}
	return (0);
}

char *
nni_plat_temp_dir(void)
{
	char path[MAX_PATH + 1];

	if (!GetTempPath(MAX_PATH + 1, path)) {
		return (NULL);
	}
	return (nni_strdup(path));
}

char *
nni_plat_home_dir(void)
{
	char *homedrv;
	char *homedir;
	char  stuff[MAX_PATH + 1];

	if (((homedrv = getenv("HOMEDRIVE")) == NULL) ||
	    ((homedir = getenv("HOMEPATH")) == NULL)) {
		return (NULL);
	}
	_snprintf(stuff, sizeof(stuff), "%s%s", homedrv, homedir);
	return (nni_strdup(stuff));
}

char *
nni_plat_join_dir(const char *prefix, const char *suffix)
{
	char stuff[MAX_PATH + 1];

	_snprintf(stuff, sizeof(stuff), "%s\\%s", prefix, suffix);
	return (nni_strdup(stuff));
}
#endif // NNG_PLATFORM_WINDOWS