1
- #include < cstring>
2
- #include < cstdlib>
3
1
#include < cerrno>
4
- #include < ctype.h>
2
+ #include < cstdlib>
3
+ #include < cstring>
5
4
#include < fcntl.h>
6
5
7
- #if defined (WINDOWS)
8
- #include < windef.h>
9
- #include < winbase.h>
10
- #include < shlobj.h>
11
- #include < objbase.h>
12
- #include < knownfolders.h>
13
- #include < shlwapi.h>
14
- #endif
15
-
16
6
#include " globals.hh"
17
7
#include " android-system.hh"
18
- #include " monodroid.h"
19
- #include " monodroid-glue-internal.hh"
20
8
#include " jni-wrappers.hh"
21
9
#include " xamarin-app.hh"
22
10
#include " cpp-util.hh"
23
11
#include " java-interop-dlfcn.h"
24
12
#include " java-interop.h"
25
13
26
- #if defined (DEBUG) || !defined (ANDROID)
14
+ #if defined (DEBUG)
27
15
namespace xamarin ::android::internal {
28
16
struct BundledProperty {
29
17
char *name;
@@ -32,22 +20,15 @@ namespace xamarin::android::internal {
32
20
struct BundledProperty *next;
33
21
};
34
22
}
35
- #endif // DEBUG || !ANDROID
23
+ #endif // DEBUG
36
24
37
25
using namespace microsoft ::java_interop;
38
26
using namespace xamarin ::android;
39
27
using namespace xamarin ::android::internal;
40
28
41
- #if defined (DEBUG) || !defined (ANDROID)
29
+ #if defined (DEBUG)
42
30
BundledProperty *AndroidSystem::bundled_properties = nullptr ;
43
- #endif // DEBUG || !ANDROID
44
31
45
- #if defined (WINDOWS)
46
- std::mutex AndroidSystem::readdir_mutex;
47
- char *AndroidSystem::libmonoandroid_directory_path = nullptr ;
48
- #endif
49
-
50
- #if defined (DEBUG) || !defined (ANDROID)
51
32
BundledProperty*
52
33
AndroidSystem::lookup_system_property (const char *name)
53
34
{
@@ -58,13 +39,13 @@ AndroidSystem::lookup_system_property (const char *name)
58
39
}
59
40
return nullptr ;
60
41
}
61
- #endif // DEBUG || !ANDROID
42
+ #endif // DEBUG
62
43
63
44
const char *
64
45
AndroidSystem::lookup_system_property (const char *name, size_t &value_len)
65
46
{
66
47
value_len = 0 ;
67
- #if defined (DEBUG) || !defined (ANDROID)
48
+ #if defined (DEBUG)
68
49
BundledProperty *p = lookup_system_property (name);
69
50
if (p != nullptr ) {
70
51
value_len = p->value_len ;
@@ -102,7 +83,7 @@ AndroidSystem::lookup_system_property (const char *name, size_t &value_len)
102
83
return nullptr ;
103
84
}
104
85
105
- #if defined (DEBUG) || !defined (ANDROID)
86
+ #if defined (DEBUG)
106
87
void
107
88
AndroidSystem::add_system_property (const char *name, const char *value)
108
89
{
@@ -138,57 +119,8 @@ AndroidSystem::add_system_property (const char *name, const char *value)
138
119
p->next = bundled_properties;
139
120
bundled_properties = p;
140
121
}
141
- #endif // DEBUG || !ANDROID
142
-
143
- #ifndef ANDROID
144
- void
145
- AndroidSystem::monodroid_strreplace (char *buffer, char old_char, char new_char)
146
- {
147
- if (buffer == nullptr )
148
- return ;
149
- while (*buffer != ' \0 ' ) {
150
- if (*buffer == old_char)
151
- *buffer = new_char;
152
- buffer++;
153
- }
154
- }
155
-
156
- int
157
- AndroidSystem::_monodroid__system_property_get (const char *name, char *sp_value, size_t sp_value_len)
158
- {
159
- if (!name || !sp_value)
160
- return -1 ;
161
-
162
- char *env_name = utils.monodroid_strdup_printf (" __XA_%s" , name);
163
- monodroid_strreplace (env_name, ' .' , ' _' );
164
- char *env_value = getenv (env_name);
165
- free (env_name);
166
-
167
- size_t env_value_len = env_value ? strlen (env_value) : 0 ;
168
- if (env_value_len == 0 ) {
169
- sp_value[0 ] = ' \0 ' ;
170
- return 0 ;
171
- }
172
-
173
- if (env_value_len >= sp_value_len)
174
- log_warn (LOG_DEFAULT, " System property buffer size too small by %u bytes" , env_value_len == sp_value_len ? 1 : env_value_len - sp_value_len);
175
-
176
- //
177
- // sp_value_len includes the terminating nul, avoid a mingw g++ warning about string truncation
178
- // by making the amount of data copied one less than the indicated length. The warning reported
179
- // is:
180
- //
181
- // In function ‘int xamarin::android::internal::AndroidSystem::_monodroid__system_property_get(const char*, char*, size_t)’,
182
- // inlined from ‘int xamarin::android::internal::AndroidSystem::monodroid_get_system_property(const char*, char**)’ at ../../../jni/android-system.cc:243:44:
183
- // ../../../jni/android-system.cc(206,10): warning G20816D19: ‘char* strncpy(char*, const char*, size_t)’ specified bound 93 equals destination size [-Wstringop-truncation] [/home/grendel/vc/xamarin/xamarin-android-worktrees/code-quality-improvements/src/monodroid/monodroid.csproj]
184
- // strncpy (sp_value, env_value, sp_value_len);
185
- //
186
- strncpy (sp_value, env_value, sp_value_len - 2 );
187
- sp_value[sp_value_len - 1 ] = ' \0 ' ;
122
+ #endif // DEBUG
188
123
189
- return static_cast <int >(strlen (sp_value));
190
- }
191
- #else
192
124
int
193
125
AndroidSystem::_monodroid__system_property_get (const char *name, char *sp_value, size_t sp_value_len)
194
126
{
@@ -211,7 +143,6 @@ AndroidSystem::_monodroid__system_property_get (const char *name, char *sp_value
211
143
212
144
return len;
213
145
}
214
- #endif
215
146
216
147
int
217
148
AndroidSystem::monodroid_get_system_property (const char *name, dynamic_local_string<PROPERTY_VALUE_BUFFER_LEN>& value)
@@ -263,7 +194,7 @@ AndroidSystem::monodroid_get_system_property (const char *name, char **value)
263
194
return len;
264
195
}
265
196
266
- #if defined (DEBUG) || !defined (ANDROID)
197
+ #if defined (DEBUG)
267
198
size_t
268
199
AndroidSystem::_monodroid_get_system_property_from_file (const char *path, char **value)
269
200
{
@@ -299,12 +230,12 @@ AndroidSystem::_monodroid_get_system_property_from_file (const char *path, char
299
230
}
300
231
return len;
301
232
}
302
- #endif
233
+ #endif // def DEBUG
303
234
304
235
size_t
305
236
AndroidSystem::monodroid_get_system_property_from_overrides ([[maybe_unused]] const char *name, [[maybe_unused]] char ** value)
306
237
{
307
- #if defined (DEBUG) || !defined (ANDROID)
238
+ #if defined (DEBUG)
308
239
for (const char *od : override_dirs) {
309
240
if (od == nullptr ) {
310
241
continue ;
@@ -319,14 +250,14 @@ AndroidSystem::monodroid_get_system_property_from_overrides ([[maybe_unused]] co
319
250
log_info (LOG_DEFAULT, " Property '%s' from %s has value '%s'." , name, od, *value);
320
251
return result;
321
252
}
322
- #endif
253
+ #endif // def DEBUG
323
254
return 0 ;
324
255
}
325
256
326
257
void
327
258
AndroidSystem::create_update_dir (char *override_dir)
328
259
{
329
- #if defined(RELEASE)
260
+ #if defined (RELEASE)
330
261
/*
331
262
* Don't create .__override__ on Release builds, because Google requires
332
263
* that pre-loaded apps not create world-writable directories.
@@ -337,7 +268,7 @@ AndroidSystem::create_update_dir (char *override_dir)
337
268
if (log_categories == 0 && monodroid_get_system_property (Debug::DEBUG_MONO_PROFILE_PROPERTY, nullptr ) == 0 ) {
338
269
return ;
339
270
}
340
- #endif
271
+ #endif // def RELEASE
341
272
342
273
override_dirs [0 ] = override_dir;
343
274
utils.create_public_directory (override_dir);
@@ -354,7 +285,7 @@ AndroidSystem::get_full_dso_path (const char *base_dir, const char *dso_path, dy
354
285
return const_cast <char *>(dso_path); // Absolute path or no base path, can't do much with it
355
286
356
287
path.assign_c (base_dir)
357
- .append (MONODROID_PATH_SEPARATOR )
288
+ .append (" / " )
358
289
.append_c (dso_path);
359
290
360
291
return true ;
@@ -411,9 +342,9 @@ AndroidSystem::load_dso_from_override_dirs ([[maybe_unused]] const char *name, [
411
342
{
412
343
#ifdef RELEASE
413
344
return nullptr ;
414
- #else
345
+ #else // def RELEASE
415
346
return load_dso_from_specified_dirs (const_cast <const char **> (AndroidSystem::override_dirs.data ()), AndroidSystem::override_dirs.size (), name, dl_flags);
416
- #endif
347
+ #endif // ndef RELEASE
417
348
}
418
349
419
350
void *
@@ -447,7 +378,7 @@ AndroidSystem::get_full_dso_path_on_disk (const char *dso_name, dynamic_local_st
447
378
if (get_existing_dso_path_on_disk (od, dso_name, path))
448
379
return true ;
449
380
}
450
- #endif
381
+ #endif // ndef RELEASE
451
382
for (const char *app_lib_dir : app_lib_directories) {
452
383
if (get_existing_dso_path_on_disk (app_lib_dir, dso_name, path)) {
453
384
return true ;
@@ -463,20 +394,20 @@ AndroidSystem::count_override_assemblies (void)
463
394
int c = 0 ;
464
395
465
396
for (const char *dir_path : override_dirs) {
466
- monodroid_dir_t *dir;
467
- monodroid_dirent_t *e;
397
+ DIR *dir;
398
+ dirent *e;
468
399
469
400
if (dir_path == nullptr || !utils.directory_exists (dir_path))
470
401
continue ;
471
402
472
- if ((dir = utils. monodroid_opendir (dir_path)) == nullptr )
403
+ if ((dir = :: opendir (dir_path)) == nullptr )
473
404
continue ;
474
405
475
- while ((e = readdir (dir)) != nullptr && e) {
406
+ while ((e = :: readdir (dir)) != nullptr && e) {
476
407
if (utils.monodroid_dirent_hasextension (e, " .dll" ))
477
408
++c;
478
409
}
479
- utils. monodroid_closedir (dir);
410
+ ::closedir (dir);
480
411
}
481
412
482
413
return c;
@@ -525,7 +456,7 @@ AndroidSystem::get_gref_gc_threshold ()
525
456
return static_cast <int > ((max_gref_count * 90LL ) / 100LL );
526
457
}
527
458
528
- #if defined (DEBUG) || !defined (ANDROID)
459
+ #if defined (DEBUG)
529
460
void
530
461
AndroidSystem::setup_environment (const char *name, const char *value)
531
462
{
@@ -548,13 +479,10 @@ AndroidSystem::setup_environment (const char *name, const char *value)
548
479
void
549
480
AndroidSystem::setup_environment_from_override_file (const char *path)
550
481
{
551
- #if WINDOWS
552
- using read_count_type = unsigned int ;
553
- #else
554
482
using read_count_type = size_t ;
555
- # endif
556
- monodroid_stat_t sbuf;
557
- if (utils. monodroid_stat (path, &sbuf) < 0 ) {
483
+
484
+ struct stat sbuf;
485
+ if (:: stat (path, &sbuf) < 0 ) {
558
486
log_warn (LOG_DEFAULT, " Failed to stat the environment override file %s: %s" , path, strerror (errno));
559
487
return ;
560
488
}
@@ -636,7 +564,7 @@ AndroidSystem::setup_environment_from_override_file (const char *path)
636
564
data_size -= data_width;
637
565
}
638
566
}
639
- #endif // DEBUG || !ANDROID
567
+ #endif // def DEBUG
640
568
641
569
void
642
570
AndroidSystem::setup_environment ()
@@ -656,12 +584,7 @@ AndroidSystem::setup_environment ()
656
584
break ;
657
585
658
586
case ' i' :
659
- #if !defined (NET)
660
- aotMode = MonoAotMode::MONO_AOT_MODE_LAST;
661
- aot_mode_last_is_interpreter = true ;
662
- #else // defined (NET)
663
587
aotMode = MonoAotMode::MONO_AOT_MODE_INTERP_ONLY;
664
- #endif // !defined (NET)
665
588
break ;
666
589
667
590
default :
@@ -701,19 +624,19 @@ AndroidSystem::setup_environment ()
701
624
702
625
#if defined (DEBUG)
703
626
log_info (LOG_DEFAULT, " Setting environment variable '%s' to '%s'" , var_name, var_value);
704
- #endif
627
+ #endif // def DEBUG
705
628
if (setenv (var_name, var_value, 1 ) < 0 )
706
629
log_warn (LOG_DEFAULT, " Failed to set environment variable: %s" , strerror (errno));
707
630
}
708
- #if defined (DEBUG) || !defined (ANDROID)
631
+ #if defined (DEBUG)
709
632
// TODO: for debug read from file in the override directory named `environment`
710
633
for (const char *od : override_dirs) {
711
634
std::unique_ptr<char []> env_override_file {utils.path_combine (od, OVERRIDE_ENVIRONMENT_FILE_NAME.data ())};
712
635
if (utils.file_exists (env_override_file.get ())) {
713
636
setup_environment_from_override_file (env_override_file.get ());
714
637
}
715
638
}
716
- #endif
639
+ #endif // def DEBUG
717
640
}
718
641
719
642
void
@@ -731,70 +654,3 @@ AndroidSystem::setup_process_args (jstring_array_wrapper &runtimeApks)
731
654
{
732
655
for_each_apk (runtimeApks, static_cast <BasicAndroidSystem::ForEachApkHandler> (&AndroidSystem::setup_process_args_apk), nullptr );
733
656
}
734
-
735
- monodroid_dirent_t *
736
- AndroidSystem::readdir (monodroid_dir_t *dir)
737
- {
738
- #if defined (WINDOWS)
739
- return readdir_windows (dir);
740
- #else
741
- return ::readdir (dir);
742
- #endif
743
- }
744
-
745
- #if defined (WINDOWS)
746
- struct _wdirent *
747
- AndroidSystem::readdir_windows (_WDIR *dirp)
748
- {
749
- std::lock_guard<std::mutex> lock (readdir_mutex);
750
- errno = 0 ;
751
- struct _wdirent *entry = _wreaddir (dirp);
752
-
753
- if (entry == nullptr && errno != 0 )
754
- return nullptr ;
755
-
756
- return entry;
757
- }
758
-
759
- // Returns the directory in which this library was loaded from
760
- char *
761
- AndroidSystem::get_libmonoandroid_directory_path ()
762
- {
763
- wchar_t module_path[MAX_PATH];
764
- HMODULE module = nullptr ;
765
-
766
- if (libmonoandroid_directory_path != nullptr )
767
- return libmonoandroid_directory_path;
768
-
769
- DWORD flags = GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT;
770
- const wchar_t *dir_path = reinterpret_cast <wchar_t *>(&libmonoandroid_directory_path);
771
- BOOL retval = GetModuleHandleExW (flags, dir_path, &module );
772
- if (!retval)
773
- return nullptr ;
774
-
775
- GetModuleFileNameW (module , module_path, sizeof (module_path) / sizeof (module_path[0 ]));
776
- PathRemoveFileSpecW (module_path);
777
- libmonoandroid_directory_path = utils.utf16_to_utf8 (module_path);
778
- return libmonoandroid_directory_path;
779
- }
780
-
781
- int
782
- AndroidSystem::setenv (const char *name, const char *value, [[maybe_unused]] int overwrite)
783
- {
784
- wchar_t *wname = utils.utf8_to_utf16 (name);
785
- wchar_t *wvalue = utils.utf8_to_utf16 (value);
786
-
787
- BOOL result = SetEnvironmentVariableW (wname, wvalue);
788
- free (wname);
789
- free (wvalue);
790
-
791
- return result ? 0 : -1 ;
792
- }
793
-
794
- int
795
- AndroidSystem::symlink (const char *target, const char *linkpath)
796
- {
797
- return utils.file_copy (target, linkpath);
798
- }
799
- #else
800
- #endif
0 commit comments