少年修仙传客户端基础资源
hch
2024-04-01 d01413b00ef631ac20347716b23818b0b811f65f
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/**
 * \file
 */
 
#include "config.h"
 
#include <string.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <errno.h>
#include <mono/utils/mono-io-portability.h>
#include <mono/metadata/profiler-private.h>
#include <mono/utils/mono-compiler.h>
 
#ifndef DISABLE_PORTABILITY
 
#include <dirent.h>
 
int mono_io_portability_helpers = PORTABILITY_UNKNOWN;
 
static inline gchar *mono_portability_find_file_internal (GString **report, const gchar *pathname, gboolean last_exists);
 
void mono_portability_helpers_init (void)
{
        gchar *env;
 
    if (mono_io_portability_helpers != PORTABILITY_UNKNOWN)
        return;
    
        mono_io_portability_helpers = PORTABILITY_NONE;
    
        env = g_getenv ("MONO_IOMAP");
        if (env != NULL) {
                /* parse the environment setting and set up some vars
                 * here
                 */
                gchar **options = g_strsplit (env, ":", 0);
                int i;
                
                if (options == NULL) {
                        /* This shouldn't happen */
                        return;
                }
                
                for (i = 0; options[i] != NULL; i++) {
#ifdef DEBUG
                        g_message ("%s: Setting option [%s]", __func__,
                                   options[i]);
#endif
                        if (!strncasecmp (options[i], "drive", 5)) {
                                mono_io_portability_helpers |= PORTABILITY_DRIVE;
                        } else if (!strncasecmp (options[i], "case", 4)) {
                                mono_io_portability_helpers |= PORTABILITY_CASE;
                        } else if (!strncasecmp (options[i], "all", 3)) {
                                mono_io_portability_helpers |= (PORTABILITY_DRIVE | PORTABILITY_CASE);
            }
                }
        g_free (env);
    }
}
 
/* Returns newly allocated string, or NULL on failure */
static gchar *find_in_dir (DIR *current, const gchar *name)
{
    struct dirent *entry;
 
#ifdef DEBUG
    g_message ("%s: looking for [%s]\n", __func__, name);
#endif
    
    while((entry = readdir (current)) != NULL) {
#ifdef DEBUGX
        g_message ("%s: found [%s]\n", __func__, entry->d_name);
#endif
        
        if (!g_ascii_strcasecmp (name, entry->d_name)) {
            char *ret;
            
#ifdef DEBUG
            g_message ("%s: matched [%s] to [%s]\n", __func__,
                   entry->d_name, name);
#endif
 
            ret = g_strdup (entry->d_name);
            closedir (current);
            return ret;
        }
    }
    
#ifdef DEBUG
    g_message ("%s: returning NULL\n", __func__);
#endif
    
    closedir (current);
    
    return(NULL);
}
 
static inline void append_report (GString **report, const gchar *format, ...)
{
    va_list ap;
    if (!*report)
        *report = g_string_new ("");
 
    va_start (ap, format);
    g_string_append_vprintf (*report, format, ap);
    va_end (ap);
}
 
static inline void do_mono_profiler_iomap (GString **report, const char *pathname, const char *new_pathname)
{
    char *rep = NULL;
    GString *tmp = report ? *report : NULL;
 
    if (tmp) {
        if (tmp->len > 0)
            rep = g_string_free (tmp, FALSE);
        else
            g_string_free (tmp, TRUE);
        *report = NULL;
    }
 
    MONO_PROFILER_RAISE (iomap_report, (rep, pathname, new_pathname));
    g_free (rep);
}
 
gchar *mono_portability_find_file (const gchar *pathname, gboolean last_exists)
{
    GString *report = NULL;
    gchar *ret;
    
    if (!pathname || !pathname [0])
        return NULL;
    ret = mono_portability_find_file_internal (&report, pathname, last_exists);
 
    if (report)
        g_string_free (report, TRUE);
 
    return ret;
}
 
/* Returns newly-allocated string or NULL on failure */
static inline gchar *mono_portability_find_file_internal (GString **report, const gchar *pathname, gboolean last_exists)
{
    gchar *new_pathname, **components, **new_components;
    int num_components = 0, component = 0;
    DIR *scanning = NULL;
    size_t len;
    gboolean drive_stripped = FALSE;
    gboolean do_report = MONO_PROFILER_ENABLED (iomap_report);
 
    if (IS_PORTABILITY_NONE) {
        return(NULL);
    }
 
    if (do_report)
        append_report (report, " - Requested file path: '%s'\n", pathname);
 
    new_pathname = g_strdup (pathname);
    
#ifdef DEBUG
    g_message ("%s: Finding [%s] last_exists: %s\n", __func__, pathname,
           last_exists?"TRUE":"FALSE");
#endif
    
    if (last_exists &&
        access (new_pathname, F_OK) == 0) {
#ifdef DEBUG
        g_message ("%s: Found it without doing anything\n", __func__);
#endif
        return(new_pathname);
    }
    
    /* First turn '\' into '/' and strip any drive letters */
    g_strdelimit (new_pathname, "\\", '/');
 
#ifdef DEBUG
    g_message ("%s: Fixed slashes, now have [%s]\n", __func__,
           new_pathname);
#endif
    
    if (IS_PORTABILITY_DRIVE &&
        g_ascii_isalpha (new_pathname[0]) &&
        (new_pathname[1] == ':')) {
        int len = strlen (new_pathname);
        
        g_memmove (new_pathname, new_pathname+2, len - 2);
        new_pathname[len - 2] = '\0';
 
        if (do_report) {
            append_report (report, " - Stripped drive letter.\n");
            drive_stripped = TRUE;
        }
#ifdef DEBUG
        g_message ("%s: Stripped drive letter, now looking for [%s]\n",
               __func__, new_pathname);
#endif
    }
 
    len = strlen (new_pathname);
    if (len > 1 && new_pathname [len - 1] == '/') {
        new_pathname [len - 1] = 0;
#ifdef DEBUG
        g_message ("%s: requested name had a trailing /, rewritten to '%s'\n",
               __func__, new_pathname);
#endif
    }
 
    if (last_exists &&
        access (new_pathname, F_OK) == 0) {
#ifdef DEBUG
        g_message ("%s: Found it\n", __func__);
#endif
        if (do_report && drive_stripped)
            do_mono_profiler_iomap (report, pathname, new_pathname);
 
        return(new_pathname);
    }
 
    /* OK, have to work harder.  Take each path component in turn
     * and do a case-insensitive directory scan for it
     */
 
    if (!(IS_PORTABILITY_CASE)) {
        g_free (new_pathname);
        return(NULL);
    }
 
    components = g_strsplit (new_pathname, "/", 0);
    if (components == NULL) {
        /* This shouldn't happen */
        g_free (new_pathname);
        return(NULL);
    }
    
    while(components[num_components] != NULL) {
        num_components++;
    }
    g_free (new_pathname);
    
    if (num_components == 0){
        return NULL;
    }
    
 
    new_components = (gchar **)g_new0 (gchar **, num_components + 1);
 
    if (num_components > 1) {
        if (strcmp (components[0], "") == 0) {
            /* first component blank, so start at / */
            scanning = opendir ("/");
            if (scanning == NULL) {
#ifdef DEBUG
                g_message ("%s: opendir 1 error: %s", __func__,
                       g_strerror (errno));
#endif
                g_strfreev (new_components);
                g_strfreev (components);
                return(NULL);
            }
        
            new_components[component++] = g_strdup ("");
        } else {
            DIR *current;
            gchar *entry;
        
            current = opendir (".");
            if (current == NULL) {
#ifdef DEBUG
                g_message ("%s: opendir 2 error: %s", __func__,
                       g_strerror (errno));
#endif
                g_strfreev (new_components);
                g_strfreev (components);
                return(NULL);
            }
        
            entry = find_in_dir (current, components[0]);
            if (entry == NULL) {
                g_strfreev (new_components);
                g_strfreev (components);
                return(NULL);
            }
        
            scanning = opendir (entry);
            if (scanning == NULL) {
#ifdef DEBUG
                g_message ("%s: opendir 3 error: %s", __func__,
                       g_strerror (errno));
#endif
                g_free (entry);
                g_strfreev (new_components);
                g_strfreev (components);
                return(NULL);
            }
        
            new_components[component++] = entry;
        }
    } else {
        if (last_exists) {
            if (strcmp (components[0], "") == 0) {
                /* First and only component blank */
                new_components[component++] = g_strdup ("");
            } else {
                DIR *current;
                gchar *entry;
                
                current = opendir (".");
                if (current == NULL) {
#ifdef DEBUG
                    g_message ("%s: opendir 4 error: %s",
                           __func__,
                           g_strerror (errno));
#endif
                    g_strfreev (new_components);
                    g_strfreev (components);
                    return(NULL);
                }
                
                entry = find_in_dir (current, components[0]);
                if (entry == NULL) {
                    g_strfreev (new_components);
                    g_strfreev (components);
                    return(NULL);
                }
                
                new_components[component++] = entry;
            }
        } else {
                new_components[component++] = g_strdup (components[0]);
        }
    }
 
#ifdef DEBUG
    g_message ("%s: Got first entry: [%s]\n", __func__, new_components[0]);
#endif
 
    g_assert (component == 1);
    
    for(; component < num_components; component++) {
        gchar *entry;
        gchar *path_so_far;
        
        if (!last_exists &&
            component == num_components -1) {
            entry = g_strdup (components[component]);
            closedir (scanning);
        } else {
            entry = find_in_dir (scanning, components[component]);
            if (entry == NULL) {
                g_strfreev (new_components);
                g_strfreev (components);
                return(NULL);
            }
        }
        
        new_components[component] = entry;
        
        if (component < num_components -1) {
            path_so_far = g_strjoinv ("/", new_components);
 
            scanning = opendir (path_so_far);
            g_free (path_so_far);
            if (scanning == NULL) {
                g_strfreev (new_components);
                g_strfreev (components);
                return(NULL);
            }
        }
    }
    
    g_strfreev (components);
 
    new_pathname = g_strjoinv ("/", new_components);
 
#ifdef DEBUG
    g_message ("%s: pathname [%s] became [%s]\n", __func__, pathname,
           new_pathname);
#endif
    
    g_strfreev (new_components);
 
    if ((last_exists &&
         access (new_pathname, F_OK) == 0) ||
        (!last_exists)) {
        if (do_report && strcmp (pathname, new_pathname) != 0)
            do_mono_profiler_iomap (report, pathname, new_pathname);
 
        return(new_pathname);
    }
 
    g_free (new_pathname);
    return(NULL);
}
 
#else /* DISABLE_PORTABILITY */
 
MONO_EMPTY_SOURCE_FILE (mono_io_portability);
 
#endif /* DISABLE_PORTABILITY */