IKVM11  11
Java SE 11 Virtual Machine for .NET
Loading...
Searching...
No Matches
UnixFileSystem.cs
Go to the documentation of this file.
1using System;
2using System.IO;
3using System.Linq;
4using System.Runtime.InteropServices;
5using System.Security;
6
7using IKVM.Runtime;
11
13{
14
15 static class UnixFileSystem
16 {
17
18#if FIRST_PASS == false
19
20 static UnixFileSystemAccessor unixFileSystemAccessor;
21
22 static UnixFileSystemAccessor UnixFileSystemAccessor => JVM.Internal.BaseAccessors.Get(ref unixFileSystemAccessor);
23
24 static global::ikvm.@internal.CallerID __callerID;
25 delegate IntPtr __jniDelegate__canonicalize0(IntPtr jniEnv, IntPtr self, IntPtr pathname);
26 static __jniDelegate__canonicalize0 __jniPtr__canonicalize0;
27 delegate byte __jniDelegate__delete0(IntPtr jniEnv, IntPtr self, IntPtr file);
28 static __jniDelegate__delete0 __jniPtr__delete0;
29 delegate byte __jniDelegate__rename0(IntPtr jniEnv, IntPtr self, IntPtr f1, IntPtr f2);
30 static __jniDelegate__rename0 __jniPtr__rename0;
31 delegate int __jniDelegate__getBooleanAttributes0(IntPtr jniEnv, IntPtr self, IntPtr file);
32 static __jniDelegate__getBooleanAttributes0 __jniPtr__getBooleanAttributes0;
33 delegate byte __jniDelegate__checkAccess(IntPtr jniEnv, IntPtr self, IntPtr file, int a);
34 static __jniDelegate__checkAccess __jniPtr__checkAccess;
35 delegate long __jniDelegate__getLastModifiedTime(IntPtr jniEnv, IntPtr self, IntPtr file);
36 static __jniDelegate__getLastModifiedTime __jniPtr__getLastModifiedTime;
37 delegate long __jniDelegate__getLength(IntPtr jniEnv, IntPtr self, IntPtr file);
38 static __jniDelegate__getLength __jniPtr__getLength;
39 delegate byte __jniDelegate__setPermission(IntPtr jniEnv, IntPtr self, IntPtr file, int access, byte enable, byte owneronly);
40 static __jniDelegate__setPermission __jniPtr__setPermission;
41 delegate byte __jniDelegate__createFileExclusively(IntPtr jniEnv, IntPtr self, IntPtr pathname);
42 static __jniDelegate__createFileExclusively __jniPtr__createFileExclusively;
43 delegate IntPtr __jniDelegate__list(IntPtr jniEnv, IntPtr self, IntPtr file);
44 static __jniDelegate__list __jniPtr__list;
45 delegate byte __jniDelegate__createDirectory(IntPtr jniEnv, IntPtr self, IntPtr file);
46 static __jniDelegate__createDirectory __jniPtr__createDirectory;
47 delegate byte __jniDelegate__setLastModifiedTime(IntPtr jniEnv, IntPtr self, IntPtr file, long time);
48 static __jniDelegate__setLastModifiedTime __jniPtr__setLastModifiedTime;
49 delegate byte __jniDelegate__setReadOnly(IntPtr jniEnv, IntPtr self, IntPtr f);
50 static __jniDelegate__setReadOnly __jniPtr__setReadOnly;
51
52#endif
53
59 static string CanonicalizePath(string path)
60 {
61#if FIRST_PASS
62 throw new NotImplementedException();
63#else
64 try
65 {
66 // begin by processing parent element
67 var parent = Path.GetDirectoryName(path);
68
69 // root paths with drive letter should have driver letter upper cased
70 if (parent == null)
71 return path.Length > 1 && path[1] == ':' ? $"{char.ToUpper(path[0])}:{Path.DirectorySeparatorChar}" : path;
72 else
73 parent = CanonicalizePath(parent);
74
75 // trailing slash would result in a last path element of empty string
76 var name = Path.GetFileName(path);
77 if (name == "" || name == ".")
78 return parent;
79 if (name == "..")
80 return Path.GetDirectoryName(parent);
81
82 try
83 {
84 if (JVM.Vfs.IsPath(path) == false)
85 {
86 // consult the file system for an actual node with the appropriate name
87 var all = Directory.EnumerateFileSystemEntries(parent, name);
88 if (all.FirstOrDefault() is string one)
89 name = Path.GetFileName(one);
90 }
91 }
92 catch (UnauthorizedAccessException)
93 {
94
95 }
96 catch (IOException)
97 {
98
99 }
100
101 return Path.Combine(parent, name);
102 }
103 catch (UnauthorizedAccessException)
104 {
105
106 }
107 catch (IOException)
108 {
109
110 }
111 catch (SecurityException)
112 {
113
114 }
115 catch (NotSupportedException)
116 {
117
118 }
119
120 return path;
121#endif
122 }
123
131 public static string canonicalize0(object self, string pathname)
132 {
133#if FIRST_PASS
134 throw new NotImplementedException();
135#else
136 if (JVM.Vfs.IsPath(pathname))
137 {
138 return CanonicalizePath(Path.IsPathRooted(pathname) == false ? Path.GetFullPath(pathname) : pathname);
139 }
140 else
141 {
142 __callerID ??= global::ikvm.@internal.CallerID.create(UnixFileSystemAccessor.Type.TypeHandle);
143 __jniPtr__canonicalize0 ??= Marshal.GetDelegateForFunctionPointer<__jniDelegate__canonicalize0>(JNIFrame.GetFuncPtr(__callerID, "java/io/UnixFileSystem", nameof(canonicalize0), "(Ljava/lang/String;)Ljava/lang/String;"));
144 var jniFrm = new JNIFrame();
145 var jniEnv = jniFrm.Enter(__callerID);
146 try
147 {
148 return (string)jniFrm.UnwrapLocalRef(__jniPtr__canonicalize0(jniEnv, jniFrm.MakeLocalRef(self), jniFrm.MakeLocalRef(pathname)));
149 }
150 catch (Exception ex)
151 {
152 System.Console.WriteLine("*** exception in native code ***");
153 System.Console.WriteLine(ex);
154 throw;
155 }
156 finally
157 {
158 jniFrm.Leave();
159 }
160 }
161#endif
162 }
163
170 public static int getBooleanAttributes0(object self, object file)
171 {
172#if FIRST_PASS
173 throw new NotImplementedException();
174#else
175 if (JVM.Vfs.IsPath(((global::java.io.File)file).getPath()))
176 {
177 return JVM.Vfs.GetBooleanAttributes(((global::java.io.File)file).getPath());
178 }
179 else
180 {
181 __callerID ??= global::ikvm.@internal.CallerID.create(UnixFileSystemAccessor.Type.TypeHandle);
182 __jniPtr__getBooleanAttributes0 ??= Marshal.GetDelegateForFunctionPointer<__jniDelegate__getBooleanAttributes0>(JNIFrame.GetFuncPtr(__callerID, "java/io/UnixFileSystem", nameof(getBooleanAttributes0), "(Ljava/io/File;)I"));
183 var jniFrm = new JNIFrame();
184 var jniEnv = jniFrm.Enter(__callerID);
185 try
186 {
187 return __jniPtr__getBooleanAttributes0(jniEnv, jniFrm.MakeLocalRef(self), jniFrm.MakeLocalRef(file));
188 }
189 catch (Exception ex)
190 {
191 System.Console.WriteLine("*** exception in native code ***");
192 System.Console.WriteLine(ex);
193 throw;
194 }
195 finally
196 {
197 jniFrm.Leave();
198 }
199 }
200#endif
201 }
202
210 public static bool checkAccess(object self, object file, int a)
211 {
212#if FIRST_PASS
213 throw new NotImplementedException();
214#else
215 if (JVM.Vfs.IsPath(((global::java.io.File)file).getPath()))
216 {
217 const int ACCESS_READ = 0x04;
218 return JVM.Vfs.GetEntry(((global::java.io.File)file).getPath()) switch
219 {
220 VfsFile f => a == ACCESS_READ && f.CanOpen(FileMode.Open, FileAccess.Read),
221 VfsDirectory => true,
222 _ => false,
223 };
224 }
225 else
226 {
227 __callerID ??= global::ikvm.@internal.CallerID.create(UnixFileSystemAccessor.Type.TypeHandle);
228 __jniPtr__checkAccess ??= Marshal.GetDelegateForFunctionPointer<__jniDelegate__checkAccess>(JNIFrame.GetFuncPtr(__callerID, "java/io/UnixFileSystem", nameof(checkAccess), "(Ljava/io/File;I)Z"));
229 var jniFrm = new JNIFrame();
230 var jniEnv = jniFrm.Enter(__callerID);
231 try
232 {
233 return __jniPtr__checkAccess(jniEnv, jniFrm.MakeLocalRef(self), jniFrm.MakeLocalRef(file), a) != 0;
234 }
235 catch (Exception ex)
236 {
237 System.Console.WriteLine("*** exception in native code ***");
238 System.Console.WriteLine(ex);
239 throw;
240 }
241 finally
242 {
243 jniFrm.Leave();
244 }
245 }
246#endif
247 }
248
258 public static bool setPermission(object self, object file, int access, bool enable, bool owneronly)
259 {
260#if FIRST_PASS
261 throw new NotImplementedException();
262#else
263 if (JVM.Vfs.IsPath(((global::java.io.File)file).getPath()))
264 {
265 return false;
266 }
267 else
268 {
269 __callerID ??= global::ikvm.@internal.CallerID.create(UnixFileSystemAccessor.Type.TypeHandle);
270 __jniPtr__setPermission ??= Marshal.GetDelegateForFunctionPointer<__jniDelegate__setPermission>(JNIFrame.GetFuncPtr(__callerID, "java/io/UnixFileSystem", nameof(setPermission), "(Ljava/io/File;IZZ)Z"));
271 var jniFrm = new JNIFrame();
272 var jniEnv = jniFrm.Enter(__callerID);
273 try
274 {
275 return __jniPtr__setPermission(jniEnv, jniFrm.MakeLocalRef(self), jniFrm.MakeLocalRef(file), access, enable ? (byte)1 : (byte)0, owneronly ? (byte)1 : (byte)0) != 0;
276 }
277 catch (Exception ex)
278 {
279 System.Console.WriteLine("*** exception in native code ***");
280 System.Console.WriteLine(ex);
281 throw;
282 }
283 finally
284 {
285 jniFrm.Leave();
286 }
287 }
288#endif
289 }
290
297 public static long getLastModifiedTime(object self, object file)
298 {
299#if FIRST_PASS
300 throw new NotImplementedException();
301#else
302 if (JVM.Vfs.IsPath(((global::java.io.File)file).getPath()))
303 {
304 return 0;
305 }
306 else
307 {
308 __callerID ??= global::ikvm.@internal.CallerID.create(UnixFileSystemAccessor.Type.TypeHandle);
309 __jniPtr__getLastModifiedTime ??= Marshal.GetDelegateForFunctionPointer<__jniDelegate__getLastModifiedTime>(JNIFrame.GetFuncPtr(__callerID, "java/io/UnixFileSystem", nameof(getLastModifiedTime), "(Ljava/io/File;)J"));
310 var jniFrm = new JNIFrame();
311 var jniEnv = jniFrm.Enter(__callerID);
312 try
313 {
314 return __jniPtr__getLastModifiedTime(jniEnv, jniFrm.MakeLocalRef(self), jniFrm.MakeLocalRef(file));
315 }
316 catch (Exception ex)
317 {
318 System.Console.WriteLine("*** exception in native code ***");
319 System.Console.WriteLine(ex);
320 throw;
321 }
322 finally
323 {
324 jniFrm.Leave();
325 }
326 }
327#endif
328 }
329
336 public static long getLength(object self, object file)
337 {
338#if FIRST_PASS
339 throw new NotImplementedException();
340#else
341 if (JVM.Vfs.IsPath(((global::java.io.File)file).getPath()))
342 {
343 return JVM.Vfs.GetEntry(((global::java.io.File)file).getPath()) is VfsFile f ? f.Size : 0;
344 }
345 else
346 {
347 __callerID ??= global::ikvm.@internal.CallerID.create(UnixFileSystemAccessor.Type.TypeHandle);
348 __jniPtr__getLength ??= Marshal.GetDelegateForFunctionPointer<__jniDelegate__getLength>(JNIFrame.GetFuncPtr(__callerID, "java/io/UnixFileSystem", nameof(getLength), "(Ljava/io/File;)J"));
349 var jniFrm = new JNIFrame();
350 var jniEnv = jniFrm.Enter(__callerID);
351 try
352 {
353 return __jniPtr__getLength(jniEnv, jniFrm.MakeLocalRef(self), jniFrm.MakeLocalRef(file));
354 }
355 catch (Exception ex)
356 {
357 System.Console.WriteLine("*** exception in native code ***");
358 System.Console.WriteLine(ex);
359 throw;
360 }
361 finally
362 {
363 jniFrm.Leave();
364 }
365 }
366#endif
367 }
368
375 public static bool createFileExclusively(object self, string pathname)
376 {
377#if FIRST_PASS
378 throw new NotImplementedException();
379#else
380 if (JVM.Vfs.IsPath(pathname))
381 {
382 return false;
383 }
384 else
385 {
386 __callerID ??= global::ikvm.@internal.CallerID.create(UnixFileSystemAccessor.Type.TypeHandle);
387 __jniPtr__createFileExclusively ??= Marshal.GetDelegateForFunctionPointer<__jniDelegate__createFileExclusively>(JNIFrame.GetFuncPtr(__callerID, "java/io/UnixFileSystem", nameof(createFileExclusively), "(Ljava/lang/String;)Z"));
388 var jniFrm = new JNIFrame();
389 var jniEnv = jniFrm.Enter(__callerID);
390 try
391 {
392 return __jniPtr__createFileExclusively(jniEnv, jniFrm.MakeLocalRef(self), jniFrm.MakeLocalRef(pathname)) != 0;
393 }
394 catch (Exception ex)
395 {
396 System.Console.WriteLine("*** exception in native code ***");
397 System.Console.WriteLine(ex);
398 throw;
399 }
400 finally
401 {
402 jniFrm.Leave();
403 }
404 }
405#endif
406 }
407
415 public static bool delete0(object self, object file)
416 {
417#if FIRST_PASS
418 throw new NotImplementedException();
419#else
420 if (JVM.Vfs.IsPath(((global::java.io.File)file).getPath()))
421 {
422 throw new NotImplementedException();
423 }
424 else
425 {
426 __callerID ??= global::ikvm.@internal.CallerID.create(UnixFileSystemAccessor.Type.TypeHandle);
427 __jniPtr__delete0 ??= Marshal.GetDelegateForFunctionPointer<__jniDelegate__delete0>(JNIFrame.GetFuncPtr(__callerID, "java/io/UnixFileSystem", nameof(delete0), "(Ljava/io/File;)Z"));
428 var jniFrm = new JNIFrame();
429 var jniEnv = jniFrm.Enter(__callerID);
430 try
431 {
432 return __jniPtr__delete0(jniEnv, jniFrm.MakeLocalRef(self), jniFrm.MakeLocalRef(file)) != 0;
433 }
434 catch (Exception ex)
435 {
436 System.Console.WriteLine("*** exception in native code ***");
437 System.Console.WriteLine(ex);
438 throw;
439 }
440 finally
441 {
442 jniFrm.Leave();
443 }
444 }
445#endif
446 }
447
454 public static string[] list(object self, object file)
455 {
456#if FIRST_PASS
457 throw new NotImplementedException();
458#else
459 if (JVM.Vfs.IsPath(((global::java.io.File)file).getPath()))
460 {
461 if (JVM.Vfs.GetEntry(((global::java.io.File)file).getPath()) is VfsDirectory vfs)
462 return vfs.List();
463
464 return null;
465 }
466 else
467 {
468 __callerID ??= global::ikvm.@internal.CallerID.create(UnixFileSystemAccessor.Type.TypeHandle);
469 __jniPtr__list ??= Marshal.GetDelegateForFunctionPointer<__jniDelegate__list>(JNIFrame.GetFuncPtr(__callerID, "java/io/UnixFileSystem", nameof(list), "(Ljava/io/File;)[Ljava/lang/String;"));
470 var jniFrm = new JNIFrame();
471 var jniEnv = jniFrm.Enter(__callerID);
472 try
473 {
474 return (string[])jniFrm.UnwrapLocalRef(__jniPtr__list(jniEnv, jniFrm.MakeLocalRef(self), jniFrm.MakeLocalRef(file)));
475 }
476 catch (Exception ex)
477 {
478 System.Console.WriteLine("*** exception in native code ***");
479 System.Console.WriteLine(ex);
480 throw;
481 }
482 finally
483 {
484 jniFrm.Leave();
485 }
486 }
487#endif
488 }
489
496 public static bool createDirectory(object self, object file)
497 {
498#if FIRST_PASS
499 throw new NotImplementedException();
500#else
501 if (JVM.Vfs.IsPath(((global::java.io.File)file).getPath()))
502 {
503 return false;
504 }
505 else
506 {
507 __callerID ??= global::ikvm.@internal.CallerID.create(UnixFileSystemAccessor.Type.TypeHandle);
508 __jniPtr__createDirectory ??= Marshal.GetDelegateForFunctionPointer<__jniDelegate__createDirectory>(JNIFrame.GetFuncPtr(__callerID, "java/io/UnixFileSystem", nameof(createDirectory), "(Ljava/io/File;)Z"));
509 var jniFrm = new JNIFrame();
510 var jniEnv = jniFrm.Enter(__callerID);
511 try
512 {
513 return __jniPtr__createDirectory(jniEnv, jniFrm.MakeLocalRef(self), jniFrm.MakeLocalRef(file)) != 0;
514 }
515 catch (Exception ex)
516 {
517 System.Console.WriteLine("*** exception in native code ***");
518 System.Console.WriteLine(ex);
519 throw;
520 }
521 finally
522 {
523 jniFrm.Leave();
524 }
525 }
526#endif
527 }
528
537 public static bool rename0(object self, object from, object to)
538 {
539#if FIRST_PASS
540 throw new NotImplementedException();
541#else
542 if (JVM.Vfs.IsPath(((global::java.io.File)from).getPath()) || JVM.Vfs.IsPath(((global::java.io.File)to).getPath()))
543 {
544 return false;
545 }
546 else
547 {
548 __callerID ??= global::ikvm.@internal.CallerID.create(UnixFileSystemAccessor.Type.TypeHandle);
549 __jniPtr__rename0 ??= Marshal.GetDelegateForFunctionPointer<__jniDelegate__rename0>(JNIFrame.GetFuncPtr(__callerID, "java/io/UnixFileSystem", nameof(rename0), "(Ljava/io/File;Ljava/io/File;)Z"));
550 var jniFrm = new JNIFrame();
551 var jniEnv = jniFrm.Enter(__callerID);
552 try
553 {
554 return __jniPtr__rename0(jniEnv, jniFrm.MakeLocalRef(self), jniFrm.MakeLocalRef(from), jniFrm.MakeLocalRef(to)) != 0;
555 }
556 catch (Exception ex)
557 {
558 System.Console.WriteLine("*** exception in native code ***");
559 System.Console.WriteLine(ex);
560 throw;
561 }
562 finally
563 {
564 jniFrm.Leave();
565 }
566 }
567#endif
568 }
569
577 public static bool setLastModifiedTime(object self, object file, long time)
578 {
579#if FIRST_PASS
580 throw new NotImplementedException();
581#else
582 if (JVM.Vfs.IsPath(((global::java.io.File)file).getPath()))
583 {
584 return false;
585 }
586 else
587 {
588 __callerID ??= global::ikvm.@internal.CallerID.create(UnixFileSystemAccessor.Type.TypeHandle);
589 __jniPtr__setLastModifiedTime ??= Marshal.GetDelegateForFunctionPointer<__jniDelegate__setLastModifiedTime>(JNIFrame.GetFuncPtr(__callerID, "java/io/UnixFileSystem", nameof(setLastModifiedTime), "(Ljava/io/File;J)Z"));
590 var jniFrm = new JNIFrame();
591 var jniEnv = jniFrm.Enter(__callerID);
592 try
593 {
594 return __jniPtr__setLastModifiedTime(jniEnv, jniFrm.MakeLocalRef(self), jniFrm.MakeLocalRef(file), time) != 0;
595 }
596 catch (Exception ex)
597 {
598 System.Console.WriteLine("*** exception in native code ***");
599 System.Console.WriteLine(ex);
600 throw;
601 }
602 finally
603 {
604 jniFrm.Leave();
605 }
606 }
607#endif
608 }
609
616 public static bool setReadOnly(object self, object file)
617 {
618#if FIRST_PASS
619 throw new NotImplementedException();
620#else
621 if (JVM.Vfs.IsPath(((global::java.io.File)file).getPath()))
622 {
623 return false;
624 }
625 else
626 {
627 __callerID ??= global::ikvm.@internal.CallerID.create(UnixFileSystemAccessor.Type.TypeHandle);
628 __jniPtr__setReadOnly ??= Marshal.GetDelegateForFunctionPointer<__jniDelegate__setReadOnly>(JNIFrame.GetFuncPtr(__callerID, "java/io/UnixFileSystem", nameof(setReadOnly), "(Ljava/io/File;)Z"));
629 var jniFrm = new JNIFrame();
630 var jniEnv = jniFrm.Enter(__callerID);
631 try
632 {
633 return __jniPtr__setReadOnly(jniEnv, jniFrm.MakeLocalRef(self), jniFrm.MakeLocalRef(file)) != 0;
634 }
635 catch (Exception ex)
636 {
637 System.Console.WriteLine("*** exception in native code ***");
638 System.Console.WriteLine(ex);
639 throw;
640 }
641 finally
642 {
643 jniFrm.Leave();
644 }
645 }
646#endif
647 }
648
649 }
650
651}
static int getBooleanAttributes0(object self, object file)
Implements the native method 'getBooleanAttributes0'.
static long getLastModifiedTime(object self, object file)
Implements the native method 'getLastModifiedTime'.
static string[] list(object self, object file)
Implements the native method 'list'.
static bool checkAccess(object self, object file, int a)
Implements the native method 'checkAccess'.
static bool delete0(object self, object file)
Implements the native method 'delete0'.
static bool rename0(object self, object from, object to)
Implements the native method 'rename0'.
static bool createDirectory(object self, object file)
Implements the native method 'createDirectory'.
static bool setPermission(object self, object file, int access, bool enable, bool owneronly)
Implements the native method 'setPermission'.
static string canonicalize0(object self, string pathname)
Implements the native method 'canonicalize0'.
static bool createFileExclusively(object self, string pathname)
Implements the native method 'createFileExclusively'.
static bool setReadOnly(object self, object file)
Implements the native method 'setReadOnly'.
static long getLength(object self, object file)
Implements the native method 'getLength'.
static bool setLastModifiedTime(object self, object file, long time)
Implements the native method 'setLastModifiedTime'.
Main state of the running JVM.
static nint GetFuncPtr(object callerID, string clazz, string name, string sig)
Invoked by managed code to acquire a function pointer to a native JNI method.
Definition JNIFrame.cs:43