This repository was archived by the owner on Aug 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathfilesystem.cpp
More file actions
259 lines (207 loc) · 6.05 KB
/
filesystem.cpp
File metadata and controls
259 lines (207 loc) · 6.05 KB
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
/* Copyright (C) 2009-2015 LiveCode Ltd.
This file is part of LiveCode.
LiveCode is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License v3 as published by the Free
Software Foundation.
LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
#include "core.h"
#include "filesystem.h"
#if defined _WINDOWS || defined _WINDOWS_SERVER
#include <windows.h>
bool MCFileSystemPathToNative(const char *p_path, void*& r_native_path)
{
unichar_t *t_w_path;
t_w_path = nil;
if (!MCCStringToUnicode(p_path, t_w_path))
return false;
for(uint32_t i = 0; t_w_path[i] != 0; i++)
if (t_w_path[i] == '/')
t_w_path[i] = '\\';
r_native_path = t_w_path;
return true;
}
bool MCFileSystemPathFromNative(const void *p_native_path, char*& r_path)
{
char *t_path;
t_path = nil;
if (!MCCStringFromUnicode((const unichar_t *)p_native_path, t_path))
return false;
for(uint32_t i = 0; t_path[i] != 0; i++)
if (t_path[i] == '\\')
t_path[i] = '/';
r_path = t_path;
return true;
}
bool MCFileSystemListEntries(const char *p_folder, uint32_t p_options, MCFileSystemListCallback p_callback, void *p_context)
{
bool t_success;
t_success = true;
char *t_pattern;
t_pattern = nil;
if (t_success)
t_success = MCCStringFormat(t_pattern, "%s%s", p_folder, MCCStringEndsWith(p_folder, "/") ? "*" : "/*");
void *t_native_pattern;
t_native_pattern = nil;
if (t_success)
t_success = MCFileSystemPathToNative(t_pattern, t_native_pattern);
HANDLE t_find_handle;
WIN32_FIND_DATAW t_find_data;
t_find_handle = INVALID_HANDLE_VALUE;
if (t_success)
{
t_find_handle = FindFirstFileW((LPCWSTR)t_native_pattern, &t_find_data);
if (t_find_handle == INVALID_HANDLE_VALUE)
t_success = false;
}
while(t_success)
{
char *t_entry_filename;
if (t_success)
t_success = MCFileSystemPathFromNative(t_find_data . cFileName, t_entry_filename);
MCFileSystemEntry t_entry;
if (t_success)
{
t_entry . type = (t_find_data . dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0 ? kMCFileSystemEntryFolder : kMCFileSystemEntryFile;
t_entry . filename = t_entry_filename;
t_success = p_callback(p_context, t_entry);
}
MCCStringFree(t_entry_filename);
////
if (!FindNextFileW(t_find_handle, &t_find_data))
{
if (GetLastError() == ERROR_NO_MORE_FILES)
break;
t_success = false;
}
}
if (t_find_handle != INVALID_HANDLE_VALUE)
FindClose(t_find_handle);
MCMemoryDeallocate(t_native_pattern);
MCCStringFree(t_pattern);
return t_success;
}
bool MCFileSystemPathResolve(const char *p_path, char*& r_resolved_path)
{
return MCCStringClone(p_path, r_resolved_path);
}
bool MCFileSystemPathExists(const char *p_path, bool p_folder, bool& r_exists)
{
bool t_success;
t_success = true;
void *t_native_path;
t_native_path = nil;
if (t_success)
t_success = MCFileSystemPathToNative(p_path, t_native_path);
if (t_success)
{
DWORD t_result;
t_result = GetFileAttributesW((LPCWSTR)t_native_path);
if (t_result != INVALID_FILE_ATTRIBUTES)
{
r_exists =
((t_result & (FILE_ATTRIBUTE_DIRECTORY)) == 0 && !p_folder) ||
((t_result & (FILE_ATTRIBUTE_DIRECTORY)) != 0 && p_folder);
}
else
{
if (GetLastError() == ERROR_FILE_NOT_FOUND)
r_exists = false;
else
t_success = false;
}
}
MCMemoryDeleteArray(t_native_path);
return t_success;
}
#elif defined(_MACOSX) || defined(_LINUX) || defined(TARGET_SUBPLATFORM_ANDROID) || defined(TARGET_SUBPLATFORM_IPHONE)
#if defined(_MACOSX) || defined(TARGET_SUBPLATFORM_IPHONE)
#include <sys/syslimits.h>
#endif
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#ifdef _LINUX
#define PATH_MAX 4096
#endif
bool MCFileSystemPathResolve(const char *p_path, char*& r_resolved_path)
{
char t_path[PATH_MAX];
ssize_t t_size;
t_size = readlink(p_path, t_path, PATH_MAX);
if (t_size == -1 || t_size == PATH_MAX)
{
if (errno == EINVAL)
return MCCStringClone(p_path, r_resolved_path);
return false;
}
t_path[t_size] = '\0';
return MCCStringClone(t_path, r_resolved_path);
}
bool MCFileSystemPathExists(const char *p_path, bool p_folder, bool& r_exists)
{
char *t_resolved_path;
if (!MCFileSystemPathResolve(p_path, t_resolved_path))
return false;
struct stat t_stat;
if (stat(t_resolved_path, &t_stat) == 0)
{
r_exists =
(p_folder && (t_stat . st_mode & S_IFDIR) != 0) ||
(!p_folder && (t_stat . st_mode & S_IFDIR) == 0);
}
else
r_exists = false;
MCCStringFree(t_resolved_path);
return true;
}
#include <sys/types.h>
#include <dirent.h>
bool MCFileSystemListEntries(const char *p_folder, uint32_t p_options, MCFileSystemListCallback p_callback, void *p_context)
{
bool t_success = true;
char *t_resolved_path = nil;
DIR *t_dir = nil;
t_success = MCFileSystemPathResolve(p_folder, t_resolved_path);
if (t_success)
{
t_dir = opendir(t_resolved_path);
t_success = t_dir != nil;
}
if (t_success)
{
struct dirent *t_entry = nil;
struct stat t_entry_stat;
MCFileSystemEntry t_fs_entry;
while (t_success && nil != (t_entry = readdir(t_dir)))
{
if (!MCCStringEqual(t_entry->d_name, ".") && !MCCStringEqual(t_entry->d_name, ".."))
{
char *t_child_path = nil;
t_success = MCCStringFormat(t_child_path, "%s/%s", t_resolved_path, t_entry->d_name);
if (t_success)
t_success = -1 != lstat(t_child_path, &t_entry_stat);
MCCStringFree(t_child_path);
if (t_success)
{
t_fs_entry.filename = t_entry->d_name;
if (S_ISLNK(t_entry_stat.st_mode))
t_fs_entry.type = kMCFileSystemEntryLink;
else if (S_ISDIR(t_entry_stat.st_mode))
t_fs_entry.type = kMCFileSystemEntryFolder;
else
t_fs_entry.type = kMCFileSystemEntryFile;
t_success = p_callback(p_context, t_fs_entry);
}
}
}
closedir(t_dir);
}
MCCStringFree(t_resolved_path);
return t_success;
}
#endif