summaryrefslogtreecommitdiff
path: root/3rdparty/plibsys/src/pdir-os2.c
blob: 8869329f5c9f42e88bafbec9e74c2cd01ada0daa (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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/*
 * The MIT License
 *
 * Copyright (C) 2017 Alexander Saprykin <saprykin.spb@gmail.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * 'Software'), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include "pdir.h"
#include "perror.h"
#include "pmem.h"
#include "pstring.h"
#include "perror-private.h"

#define INCL_DOSFILEMGR
#define INCL_DOSERRORS
#include <os2.h>

#include <stdlib.h>
#include <string.h>

struct PDir_ {
	FILEFINDBUF3	find_data;
	HDIR		search_handle;
	pboolean	cached;
	pchar		path[CCHMAXPATH];
	pchar		*orig_path;
};

P_LIB_API PDir *
p_dir_new (const pchar	*path,
	   PError	**error)
{
	PDir	*ret;
	pchar	*pathp;
	pchar	*adj_path;
	pint	path_len;
	APIRET	ulrc;
	ULONG	find_count;

	if (P_UNLIKELY (path == NULL)) {
		p_error_set_error_p (error,
				     (pint) P_ERROR_IO_INVALID_ARGUMENT,
				     0,
				     "Invalid input argument");
		return NULL;
	}

	if (P_UNLIKELY ((ret = p_malloc0 (sizeof (PDir))) == NULL)) {
		p_error_set_error_p (error,
				     (pint) P_ERROR_IO_NO_RESOURCES,
				     0,
				     "Failed to allocate memory for directory structure");
		return NULL;
	}

	adj_path = NULL;
	path_len = strlen (path);

	if (P_UNLIKELY (path[path_len - 1] == '\\' || path[path_len - 1] == '/') && path_len > 1) {
		while ((path[path_len - 1] == '\\' || path[path_len - 1] == '/') && path_len > 1)
			--path_len;

		if (P_UNLIKELY ((adj_path = p_malloc0 (path_len + 1)) == NULL)) {
			p_free (ret);
			p_error_set_error_p (error,
					     (pint) P_ERROR_IO_NO_RESOURCES,
					     0,
					     "Failed to allocate memory for directory path");
			return NULL;
		}

		memcpy (adj_path, path, path_len);
		
		adj_path[path_len] = '\0';
		ret->orig_path = p_strdup (path);
		path = (const pchar *) adj_path;
	}

	ret->search_handle = HDIR_CREATE;

	ulrc = DosQueryPathInfo ((PSZ) path, FIL_QUERYFULLNAME, ret->path, sizeof (ret->path) - 2);

	if (P_UNLIKELY (ulrc != NO_ERROR)) {
		p_error_set_error_p (error,
				     (pint) p_error_get_io_from_system ((pint) ulrc),
				     (pint) ulrc,
				     "Failed to call DosQueryPathInfo() to get directory path");

		if (P_UNLIKELY (adj_path != NULL)) {
			p_free (adj_path);
			p_free (ret->orig_path);
		}

		p_free (ret);
		return NULL;
	}

	/* Append the search pattern "\\*\0" to the directory name */
	pathp = strchr (ret->path, '\0');

	if (ret->path < pathp  &&  *(pathp - 1) != '\\'  &&  *(pathp - 1) != ':')
		*pathp++ = '\\';

	*pathp++ = '*';
	*pathp = '\0';

	find_count = 1;

	/* Open directory stream and retrieve the first entry */
	ulrc = DosFindFirst (ret->path,
			     &ret->search_handle,
			     FILE_NORMAL | FILE_DIRECTORY,
			     &ret->find_data,
			     sizeof (ret->find_data),
			     &find_count,
			     FIL_STANDARD);

	if (P_UNLIKELY (ulrc != NO_ERROR && ulrc != ERROR_NO_MORE_FILES)) {
		p_error_set_error_p (error,
				     (pint) p_error_get_io_from_system ((pint) ulrc),
				     (pint) ulrc,
				     "Failed to call DosFindFirst() to open directory stream");

		if (P_UNLIKELY (adj_path != NULL)) {
			p_free (adj_path);
			p_free (ret->orig_path);
		}

		p_free (ret);
		return NULL;
	}

	ret->cached = TRUE;

	if (P_UNLIKELY (adj_path != NULL))
		p_free (adj_path);
	else
		ret->orig_path = p_strdup (path);

	return ret;
}

P_LIB_API pboolean
p_dir_create (const pchar	*path,
	      pint		mode,
	      PError		**error)
{
	APIRET ulrc;

	P_UNUSED (mode);

	if (P_UNLIKELY (path == NULL)) {
		p_error_set_error_p (error,
				     (pint) P_ERROR_IO_INVALID_ARGUMENT,
				     0,
				     "Invalid input argument");
		return FALSE;
	}

	if (p_dir_is_exists (path))
		return TRUE;

	if (P_UNLIKELY ((ulrc = DosCreateDir ((PSZ) path, NULL)) != NO_ERROR)) {
		p_error_set_error_p (error,
				     (pint) p_error_get_io_from_system ((pint) ulrc),
				     (pint) ulrc,
				     "Failed to call DosCreateDir() to create directory");
		return FALSE;
	} else
		return TRUE;
}

P_LIB_API pboolean
p_dir_remove (const pchar	*path,
	      PError		**error)
{
	APIRET ulrc;

	if (P_UNLIKELY (path == NULL)) {
		p_error_set_error_p (error,
				     (pint) P_ERROR_IO_INVALID_ARGUMENT,
				     0,
				     "Invalid input argument");
		return FALSE;
	}

	if (!p_dir_is_exists (path)) {
		p_error_set_error_p (error,
				     (pint) P_ERROR_IO_NOT_EXISTS,
				     0,
				     "Specified directory doesn't exist");
		return FALSE;
	}

	if (P_UNLIKELY ((ulrc = DosDeleteDir ((PSZ) path)) != NO_ERROR)) {
		p_error_set_error_p (error,
				     (pint) p_error_get_io_from_system ((pint) ulrc),
				     (pint) ulrc,
				     "Failed to call DosDeleteDir() to remove directory");
		return FALSE;
	} else
		return TRUE;
}

P_LIB_API pboolean
p_dir_is_exists (const pchar *path)
{
	FILESTATUS3	status;

	if (P_UNLIKELY (path == NULL))
		return FALSE;

	if (DosQueryPathInfo ((PSZ) path, FIL_STANDARD, (PVOID) &status, sizeof (status)) != NO_ERROR)
		return FALSE;

	return (status.attrFile & FILE_DIRECTORY) != 0;
}

P_LIB_API pchar *
p_dir_get_path (const PDir *dir)
{
	if (P_UNLIKELY (dir == NULL))
		return NULL;

	return p_strdup (dir->orig_path);
}

P_LIB_API PDirEntry *
p_dir_get_next_entry (PDir	*dir,
		      PError	**error)
{
	PDirEntry	*ret;
	APIRET		ulrc;
	ULONG		find_count;

	if (P_UNLIKELY (dir == NULL)) {
		p_error_set_error_p (error,
				     (pint) P_ERROR_IO_INVALID_ARGUMENT,
				     0,
				     "Invalid input argument");
		return NULL;
	}

	if (dir->cached == TRUE) {
		dir->cached = FALSE;

		/* Opened directory is empty */
		if (P_UNLIKELY (dir->search_handle == HDIR_CREATE)) {
			p_error_set_error_p (error,
					     (pint) P_ERROR_IO_NO_MORE,
					     (pint) ERROR_NO_MORE_FILES,
					     "Directory is empty to get the next entry");
			return NULL;
		}
	} else {
		if (P_UNLIKELY (dir->search_handle == HDIR_CREATE)) {
			p_error_set_error_p (error,
					     (pint) P_ERROR_IO_INVALID_ARGUMENT,
					     0,
					     "Not a valid (or closed) directory stream");
			return NULL;
		}

		find_count = 1;

		ulrc = DosFindNext (dir->search_handle,
				    (PVOID) &dir->find_data,
				    sizeof (dir->find_data),
				    &find_count);

		if (P_UNLIKELY (ulrc != NO_ERROR)) {
			p_error_set_error_p (error,
					     (pint) p_error_get_io_from_system ((pint) ulrc),
					     (pint) ulrc,
					     "Failed to call DosFindNext() to read directory stream");
			DosFindClose (dir->search_handle);
			dir->search_handle = HDIR_CREATE;
			return NULL;
		}
	}

	if (P_UNLIKELY ((ret = p_malloc0 (sizeof (PDirEntry))) == NULL)) {
		p_error_set_error_p (error,
				     (pint) P_ERROR_IO_NO_RESOURCES,
				     0,
				     "Failed to allocate memory for directory entry");
		return NULL;
	}

	ret->name = p_strdup (dir->find_data.achName);

	if ((dir->find_data.attrFile & FILE_DIRECTORY) != 0)
		ret->type = P_DIR_ENTRY_TYPE_DIR;
	else
		ret->type = P_DIR_ENTRY_TYPE_FILE;

	return ret;
}

P_LIB_API pboolean
p_dir_rewind (PDir	*dir,
	      PError	**error)
{
	APIRET	ulrc;
	ULONG	find_count;

	if (P_UNLIKELY (dir == NULL)) {
		p_error_set_error_p (error,
				     (pint) P_ERROR_IO_INVALID_ARGUMENT,
				     0,
				     "Invalid input argument");
		return FALSE;
	}

	if (dir->search_handle != HDIR_CREATE) {
		if (P_UNLIKELY ((ulrc = DosFindClose (dir->search_handle)) != NO_ERROR)) {
			p_error_set_error_p (error,
					     (pint) p_error_get_io_from_system ((pint) ulrc),
					     (pint) ulrc,
					     "Failed to call DosFindClose() to close directory stream");
			return FALSE;
		}

		dir->search_handle = HDIR_CREATE;
	}

	find_count = 1;

	ulrc = DosFindFirst (dir->path,
			     &dir->search_handle,
			     FILE_NORMAL | FILE_DIRECTORY,
			     &dir->find_data,
			     sizeof (dir->find_data),
			     &find_count,
			     FIL_STANDARD);

	if (P_UNLIKELY (ulrc != NO_ERROR && ulrc != ERROR_NO_MORE_FILES)) {
		p_error_set_error_p (error,
				     (pint) p_error_get_io_from_system ((pint) ulrc),
				     (pint) ulrc,
				     "Failed to call DosFindFirst() to open directory stream");
		dir->cached = FALSE;
		return FALSE;
	} else {
		dir->cached = TRUE;
		return TRUE;
	}
}

P_LIB_API void
p_dir_free (PDir *dir)
{
	if (dir == NULL)
		return;

	if (P_LIKELY (dir->search_handle != HDIR_CREATE)) {
		if (P_UNLIKELY (DosFindClose (dir->search_handle) != NO_ERROR))
			P_ERROR ("PDir::p_dir_free: DosFindClose() failed");
	}

	p_free (dir->orig_path);
	p_free (dir);
}