-
Notifications
You must be signed in to change notification settings - Fork 361
/
File.scala
1002 lines (891 loc) · 29.6 KB
/
File.scala
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package java.io
import java.{lang => jl}
import java.net.URI
import java.nio.charset.StandardCharsets
import java.nio.file.{FileSystems, Path}
import java.nio.file.WindowsException
import java.util.ScalaOps._
import java.util.WindowsHelperMethods._
import scala.annotation.tailrec
import scala.scalanative.annotation.alwaysinline
import scala.scalanative.libc._
import scala.scalanative.libc.stdio._
import scala.scalanative.libc.string._
import scala.scalanative.nio.fs.FileHelpers
import scala.scalanative.posix.stdlib._
import scala.scalanative.posix.sys.stat
import scala.scalanative.posix.unistd._
import scala.scalanative.posix.{limits, unistd, utime}
import scala.scalanative.meta.LinktimeInfo.isWindows
import scala.scalanative.runtime.{DeleteOnExit, Platform}
import scala.scalanative.unsafe._
import scala.scalanative.unsigned._
import scala.scalanative.windows
import windows._
import windows.MinWinBaseApi.{FileTime => WinFileTime}
import windows.MinWinBaseApiOps.FileTimeOps._
import windows.WinBaseApi._
import windows.WinBaseApiExt._
import windows.SecurityBaseApi._
import windows.SecurityBaseApiOps._
import windows.AclApi._
import windows.HandleApi._
import windows.HandleApiExt._
import windows.FileApi._
import windows.FileApiExt._
import windows.winnt.{HelperMethods => WinNtHelperMethods, _}
import windows.winnt.AccessRights._
import windows.winnt.AccessToken._
import windows.winnt.TokenInformationClass
import windows.accctrl._
class File(_path: String) extends Serializable with Comparable[File] {
import File._
if (_path == null) throw new NullPointerException()
private val path: String = fixSlashes(_path)
private[io] val properPath: String = File.properPath(path)
def this(parent: String, child: String) =
this(
Option(parent).map(p => p + File.separatorChar + child).getOrElse(child)
)
def this(parent: File, child: String) =
this(Option(parent).map(_.path).orNull, child)
def this(uri: URI) =
this(File.checkURI(uri).getPath())
def compareTo(file: File): Int = {
if (caseSensitive) getPath().compareTo(file.getPath())
else getPath().compareToIgnoreCase(file.getPath())
}
def canExecute(): Boolean =
Zone.acquire { implicit z =>
if (isWindows) path.nonEmpty && exists()
else access(toCString(path), unistd.X_OK) == 0
}
def canRead(): Boolean =
Zone.acquire { implicit z =>
if (isWindows) checkWindowsAccess(FILE_GENERIC_READ)
else access(toCString(path), unistd.R_OK) == 0
}
def canWrite(): Boolean =
Zone.acquire { implicit z =>
if (isWindows)
checkWindowsAccess(FILE_GENERIC_WRITE) &&
fileAttributeIsSet(FILE_ATTRIBUTE_READONLY, checkIsNotSet = true)
else access(toCString(path), unistd.W_OK) == 0
}
def setExecutable(executable: Boolean): Boolean =
setExecutable(executable, ownerOnly = true)
def setExecutable(executable: Boolean, ownerOnly: Boolean): Boolean = {
if (isWindows) {
// Windows (JVM) does not allow for setting a file as not executable
if (!executable) false
else {
val accessRights = FILE_GENERIC_EXECUTE
updatePermissionsWindows(accessRights, executable, ownerOnly)
}
} else {
import stat._
val mask = if (!ownerOnly) S_IXUSR | S_IXGRP | S_IXOTH else S_IXUSR
updatePermissionsUnix(mask, executable)
}
}
def setReadable(readable: Boolean): Boolean =
setReadable(readable, ownerOnly = true)
def setReadable(readable: Boolean, ownerOnly: Boolean): Boolean = {
if (isWindows) {
val accessRights = FILE_GENERIC_READ
updatePermissionsWindows(accessRights, readable, ownerOnly)
} else {
import stat._
val mask = if (!ownerOnly) S_IRUSR | S_IRGRP | S_IROTH else S_IRUSR
updatePermissionsUnix(mask, readable)
}
}
def setWritable(writable: Boolean): Boolean =
setWritable(writable, ownerOnly = true)
def setWritable(writable: Boolean, ownerOnly: Boolean = true): Boolean = {
if (isWindows) {
val accessRights = FILE_GENERIC_WRITE
updatePermissionsWindows(accessRights, writable, ownerOnly)
} else {
import stat._
val mask = if (!ownerOnly) S_IWUSR | S_IWGRP | S_IWOTH else S_IWUSR
updatePermissionsUnix(mask, writable)
}
}
private def updatePermissionsUnix(
mask: stat.mode_t,
grant: Boolean
): Boolean =
Zone.acquire { implicit z =>
if (grant) {
stat.chmod(toCString(path), accessMode() | mask) == 0
} else {
stat.chmod(toCString(path), accessMode() & (~mask)) == 0
}
}
private def updatePermissionsWindows(
accessRights: windows.DWord,
grant: Boolean,
ownerOnly: Boolean
): Boolean =
Zone.acquire { implicit z =>
val filename = toCWideStringUTF16LE(properPath)
val securityDescriptorPtr = alloc[Ptr[SecurityDescriptor]]()
val previousDacl, newDacl = alloc[ACLPtr]()
val usersGroupSid = alloc[SIDPtr]()
val accessMode: AccessMode =
if (grant) AccessMode.GRANT_ACCESS
else AccessMode.DENY_ACCESS
def getSecurityDescriptor() =
GetNamedSecurityInfoW(
objectName = filename,
objectType = SE_FILE_OBJECT,
securityInfo = DACL_SECURITY_INFORMATION,
sidOwner = null,
sidGroup = null,
dacl = previousDacl,
sacl = null,
securityDescriptor = securityDescriptorPtr
) == 0
def setupNewAclEntry() = {
import accctrl.ops._
val ea = alloc[ExplicitAccessW]()
ea.accessPermisions = accessRights
ea.accessMode = accessMode
ea.inheritence = NO_PROPAGATE_INHERIT_ACE
ea.trustee.trusteeForm = TrusteeForm.TRUSTEE_IS_SID
if (ownerOnly) {
withUserToken(TOKEN_QUERY) { userToken =>
withTokenInformation(userToken, TokenInformationClass.TokenUser) {
(data: Ptr[SidAndAttributes]) =>
ea.trustee.trusteeType = TrusteeType.TRUSTEE_IS_USER
ea.trustee.sid = data.sid
}
}
} else {
WinNtHelperMethods.setupUserGroupSid(usersGroupSid)
ea.trustee.trusteeType = TrusteeType.TRUSTEE_IS_WELL_KNOWN_GROUP
ea.trustee.sid = !usersGroupSid
}
SetEntriesInAclW(1.toUInt, ea, !previousDacl, newDacl) == 0
}
def assignNewSecurityInfo() =
SetNamedSecurityInfoW(
objectName = filename,
objectType = SE_FILE_OBJECT,
securityInfo = DACL_SECURITY_INFORMATION,
sidOwner = null,
sidGroup = null,
dacl = !newDacl,
sacl = null
) == 0
try {
getSecurityDescriptor() &&
setupNewAclEntry() &&
assignNewSecurityInfo()
} finally {
FreeSid(!usersGroupSid)
}
}
def exists(): Boolean =
Zone.acquire { implicit z =>
if (isWindows) {
val filename = toCWideStringUTF16LE(properPath)
val attrs = GetFileAttributesW(filename)
val pathExists = attrs != INVALID_FILE_ATTRIBUTES
val notSymLink = (attrs & FILE_ATTRIBUTE_REPARSE_POINT) == 0
if (notSymLink) // fast path
pathExists
else {
withFileOpen(
path,
access = FILE_READ_ATTRIBUTES,
allowInvalidHandle = true
)(_ != INVALID_HANDLE_VALUE)
}
} else {
access(toCString(path), unistd.F_OK) == 0
}
}
def toPath(): Path =
FileSystems.getDefault().getPath(this.getPath(), Array.empty)
def getPath(): String = path
def delete(): Boolean =
if (path.nonEmpty && isDirectory()) {
deleteDirImpl()
} else {
deleteFileImpl()
}
private def deleteDirImpl(): Boolean = Zone.acquire { implicit z =>
if (isWindows) {
RemoveDirectoryW(toCWideStringUTF16LE(properPath))
} else
remove(toCString(path)) == 0
}
private def deleteFileImpl(): Boolean = Zone.acquire { implicit z =>
if (isWindows) {
setReadOnlyWindows(enabled = false)
DeleteFileW(toCWideStringUTF16LE(properPath))
} else {
unlink(toCString(path)) == 0
}
}
override def equals(that: Any): Boolean =
that match {
case that: File if caseSensitive =>
this.path == that.path
case that: File =>
this.path.toLowerCase == that.path.toLowerCase
case _ =>
false
}
def getAbsolutePath(): String = properPath
def getAbsoluteFile(): File = new File(this.getAbsolutePath())
def getCanonicalPath(): String =
Zone.acquire { implicit z =>
if (exists()) {
simplifyExistingPath(properPath)
} else {
simplifyNonExistingPath(fromCString(resolve(toCString(properPath))))
}
}
/** Finds the canonical path for `path`, using `realpath`. The file must
* exist, because the result of `realpath` doesn't match that of Java on
* non-existing file.
*/
private def simplifyExistingPath(path: String)(implicit z: Zone): String = {
if (isWindows) {
val resolvedName: Ptr[WChar] = alloc[WChar](FileApiExt.MAX_PATH)
GetFullPathNameW(
toCWideStringUTF16LE(properPath),
FileApiExt.MAX_PATH,
resolvedName,
null
)
fromCWideString(resolvedName, StandardCharsets.UTF_16LE)
} else {
val resolvedName: Ptr[Byte] = alloc[Byte](limits.PATH_MAX)
if (realpath(toCString(path), resolvedName) == null) {
throw new IOException(
s"realpath can't resolve: ${fromCString(resolvedName)}"
)
}
fromCString(resolvedName)
}
}
/** Finds the canonical path for `path`.
*/
private def simplifyNonExistingPath(path: String): String = {
val rootDirectory =
if (isWindows) path.take(path.indexOf(s":$separator") + 2)
else separator
path
.stripPrefix(rootDirectory)
.split(separatorChar)
.foldLeft(List.empty[String]) {
case (acc, "..") =>
if (acc.nonEmpty) acc.tail
else if (isWindows) Nil
else List("..")
case (acc, ".") => acc
case (acc, "") => acc
case (acc, seg) => seg :: acc
}
.reverse
.filterNot(_.isEmpty())
.mkString(rootDirectory, separator, "")
}
@throws(classOf[IOException])
def getCanonicalFile(): File = new File(getCanonicalPath())
def getName(): String = {
val separatorIndex: Int = path.lastIndexOf(separatorChar)
if (separatorIndex < 0) path
else path.substring(separatorIndex + 1, path.length())
}
def getParent(): String = {
val isAbsolute = this.isAbsolute()
val rootDirectory =
if (!isAbsolute) ""
else if (isWindows) path.substring(0, path.indexOf(s":$separator") + 2)
else separator
path
.stripPrefix(rootDirectory)
.split(separatorChar)
.filterNot(_.isEmpty()) match {
case Array() => null
case Array(_) if !isAbsolute => null
case parts => parts.init.mkString(rootDirectory, separator, "")
}
}
def getParentFile(): File = {
val parent = getParent()
if (parent == null) null
else new File(parent)
}
override def hashCode(): Int =
if (caseSensitive) path.hashCode ^ 1234321
else path.toLowerCase.hashCode ^ 1234321
def isAbsolute(): Boolean =
File.isAbsolute(path)
def isDirectory(): Boolean =
Zone.acquire { implicit z =>
if (isWindows)
fileAttributeIsSet(FILE_ATTRIBUTE_DIRECTORY)
else
stat.S_ISDIR(accessMode()) != 0
}
def isFile(): Boolean =
Zone.acquire { implicit z =>
if (isWindows)
fileAttributeIsSet(FILE_ATTRIBUTE_DIRECTORY, checkIsNotSet = true)
else
stat.S_ISREG(accessMode()) != 0
}
def isHidden(): Boolean = {
if (isWindows)
fileAttributeIsSet(FILE_ATTRIBUTE_HIDDEN)
else getName().startsWith(".")
}
def lastModified(): Long =
Zone.acquire { implicit z =>
if (isWindows) {
withFileOpen(
path,
access = FILE_GENERIC_READ,
allowInvalidHandle = true
) {
case INVALID_HANDLE_VALUE => 0L
case handle =>
val lastModified = stackalloc[WinFileTime]()
GetFileTime(
handle,
creationTime = null,
lastAccessTime = null,
lastWriteTime = lastModified
)
MinWinBaseApiOps.FileTimeOps.toUnixEpochMillis(!lastModified)
}
} else {
import scala.scalanative.posix.sys.statOps.statOps
val buf = alloc[stat.stat]()
if (stat.stat(toCString(path), buf) == 0) {
buf.st_mtime * 1000L
} else {
0L
}
}
}
private def accessMode()(implicit z: Zone): stat.mode_t = {
val buf = alloc[stat.stat]()
if (stat.stat(toCString(path), buf) == 0) {
buf._13
} else {
0.toUInt
}
}
@alwaysinline
private def fileAttributeIsSet(
flags: windows.DWord,
checkIsNotSet: Boolean = false
): Boolean = Zone.acquire { implicit z =>
GetFileAttributesW(toCWideStringUTF16LE(properPath)) match {
case INVALID_FILE_ATTRIBUTES => false // File does not exist
case attrsSet =>
if (checkIsNotSet)
(attrsSet & flags) != flags
else
(attrsSet & flags) == flags
}
}
def setLastModified(time: Long): Boolean =
if (time < 0) {
throw new IllegalArgumentException("Negative time")
} else
Zone.acquire { implicit z =>
if (isWindows) {
withFileOpen(
path,
access = FILE_GENERIC_WRITE,
allowInvalidHandle = true
) {
case INVALID_HANDLE_VALUE => false
case handle =>
val lastModified = stackalloc[WinFileTime]()
!lastModified =
MinWinBaseApiOps.FileTimeOps.fromUnixEpochMillis(time)
SetFileTime(
handle,
creationTime = null,
lastAccessTime = null,
lastWriteTime = lastModified
)
}
} else {
val statbuf = alloc[stat.stat]()
if (stat.stat(toCString(path), statbuf) == 0) {
val timebuf = alloc[utime.utimbuf]()
import scala.scalanative.posix.sys.statOps.statOps
timebuf._1 = statbuf.st_mtime
timebuf._2 = time.toSize / 1000
utime.utime(toCString(path), timebuf) == 0
} else {
false
}
}
}
def setReadOnly(): Boolean =
Zone.acquire { implicit z =>
if (isWindows) setReadOnlyWindows(enabled = true)
else {
import stat._
val mask =
S_ISUID | S_ISGID | S_ISVTX | S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH
val newMode = accessMode() & mask
chmod(toCString(path), newMode) == 0
}
}
private def setReadOnlyWindows(enabled: Boolean)(implicit z: Zone) = {
val filename = toCWideStringUTF16LE(properPath)
val currentAttributes = GetFileAttributesW(filename)
def newAttributes =
if (enabled) currentAttributes | FILE_ATTRIBUTE_READONLY
else currentAttributes & ~FILE_ATTRIBUTE_READONLY
def setNewAttributes() = SetFileAttributesW(filename, newAttributes)
currentAttributes != INVALID_FILE_ATTRIBUTES && setNewAttributes()
}
def length(): Long = Zone.acquire { implicit z =>
if (isWindows) {
withFileOpen(
path,
access = FILE_READ_ATTRIBUTES,
allowInvalidHandle = true
) {
case INVALID_HANDLE_VALUE => 0L
case handle =>
val size = stackalloc[windows.LargeInteger]()
if (GetFileSizeEx(handle, size)) (!size).toLong
else 0L
}
} else {
val buf = alloc[stat.stat]()
if (stat.stat(toCString(path), buf) == 0) {
buf._6.toLong
} else {
0L
}
}
}
def list(): Array[String] =
list(FilenameFilter.allPassFilter)
def list(filter: FilenameFilter): Array[String] = {
if (!isDirectory() || !canRead()) {
null
} else
Zone.acquire { implicit z =>
val elements =
FileHelpers.list(properPath, (n, _) => n, allowEmpty = true)
if (elements == null)
Array.empty[String]
else
elements.filter(filter.accept(this, _))
}
}
def listFiles(): Array[File] =
listFiles(FilenameFilter.allPassFilter)
def listFiles(filter: FilenameFilter): Array[File] = {
val files = list(filter)
if (files == null) null
else files.map(new File(this, _))
}
def listFiles(filter: FileFilter): Array[File] = {
val filenameFilter =
new FilenameFilter {
override def accept(dir: File, name: String): Boolean =
filter.accept(new File(dir, name))
}
listFiles(filenameFilter)
}
def mkdir(): Boolean =
Zone.acquire { implicit z =>
if (isWindows)
CreateDirectoryW(
toCWideStringUTF16LE(properPath),
securityAttributes = null
)
else {
val mode = octal("0777")
stat.mkdir(toCString(path), mode) == 0
}
}
def mkdirs(): Boolean =
if (exists()) {
false
} else if (mkdir()) {
true
} else {
val parent = getParentFile()
if (parent == null) {
false
} else {
parent.mkdirs() && mkdir()
}
}
def createNewFile(): Boolean =
FileHelpers.createNewFile(path, throwOnError = true)
def renameTo(dest: File): Boolean =
Zone.acquire { implicit z =>
rename(toCString(properPath), toCString(dest.properPath)) == 0
}
override def toString(): String = path
def deleteOnExit(): Unit = DeleteOnExit.addFile(this.getAbsolutePath())
def toURL(): java.net.URL = toURI().toURL()
// Ported from Apache Harmony
def toURI(): URI = {
val path = getAbsolutePath().replace('\\', '/')
if (!path.startsWith("/")) {
// start with sep.
new URI(
"file",
null,
new jl.StringBuilder(path.length + 1)
.append('/')
.append(path)
.toString,
null,
null
)
} else if (path.startsWith("//")) {
// UNC path
new URI("file", "", path, null)
} else {
new URI("file", null, path, null, null)
}
}
private def checkWindowsAccess(
access: windows.DWord
)(implicit zone: Zone): Boolean = {
// based on this article https://blog.aaronballman.com/2011/08/how-to-check-access-rights/
val accessStatus = stackalloc[Boolean]()
def withFileSecurityDescriptor(
fn: Ptr[SecurityDescriptor] => Unit
): Unit = {
val securityDescriptorPtr = stackalloc[Ptr[SecurityDescriptor]]()
val filename = toCWideStringUTF16LE(properPath)
val securityInfo =
OWNER_SECURITY_INFORMATION |
GROUP_SECURITY_INFORMATION |
DACL_SECURITY_INFORMATION
val result = GetNamedSecurityInfoW(
filename,
SE_FILE_OBJECT,
securityInfo,
sidOwner = null,
sidGroup = null,
dacl = null,
sacl = null,
securityDescriptor = securityDescriptorPtr
)
if (result != ErrorCodes.ERROR_SUCCESS)
throw WindowsException(
s"Cannot retrive file security descriptor",
errorCode = result
)
try {
fn(!securityDescriptorPtr)
} finally {
WinBaseApi.LocalFree(!securityDescriptorPtr)
}
}
if (path.isEmpty() || !exists()) false // JVM complience
else {
withFileSecurityDescriptor { securityDescriptor =>
withImpersonatedToken { impersonatedToken =>
val genericMapping = stackalloc[GenericMapping]()
genericMapping.genericRead = FILE_GENERIC_READ
genericMapping.genericWrite = FILE_GENERIC_WRITE
genericMapping.genericExecute = FILE_GENERIC_EXECUTE
genericMapping.genericAll = FILE_GENERIC_ALL
val accessMask = stackalloc[windows.DWord]()
!accessMask = access
val privilegeSetLength = stackalloc[windows.DWord]()
!privilegeSetLength = emptyPriviligesSize.toUInt
val privilegeSet: Ptr[Byte] = stackalloc[Byte](!privilegeSetLength)
memset(privilegeSet, 0, !privilegeSetLength)
val grantedAcccess = stackalloc[windows.DWord]()
!grantedAcccess = 0.toUInt
MapGenericMask(accessMask, genericMapping)
AccessCheck(
securityDescriptor = securityDescriptor,
clientToken = impersonatedToken,
desiredAccess = access,
genericMapping = genericMapping,
privilegeSet = privilegeSet,
privilegeSetLength = privilegeSetLength,
grantedAccess = grantedAcccess,
accessStatus = accessStatus
)
}
}
!accessStatus
}
}
}
object File {
private val `1U` = 1.toUInt
private def octal(v: String): UInt =
Integer.parseInt(v, 8).toUInt
private def getUserDir(): String =
Zone.acquire { implicit z =>
if (isWindows) {
val buffSize = GetCurrentDirectoryW(0.toUInt, null)
val buff: Ptr[windows.WChar] = alloc[windows.WChar](buffSize + 1.toUInt)
if (GetCurrentDirectoryW(buffSize, buff) == 0) {
throw WindowsException("error in trying to get user directory")
}
fromCWideString(buff, StandardCharsets.UTF_16LE)
} else {
val buff: CString = alloc[CChar](4096)
if (getcwd(buff, 4095.toUInt) == null) {
val errMsg = fromCString(string.strerror(errno.errno))
throw new IOException(
s"error in trying to get user directory - $errMsg"
)
}
fromCString(buff)
}
}
/** The purpose of this method is to take a path and fix the slashes up. This
* includes changing them all to the current platforms fileSeparator and
* removing duplicates.
*/
// Ported from Apache Harmony
private def fixSlashes(path: String): String = {
val length = path.length
var newLength = 0
var uncIndex =
if (separatorChar == '/') 0 // UNIX world
else if (length > 2 && path.charAt(1) == ':')
2 // Windows, but starts with C:...
else 1 // Possible UNC path name
var foundSlash = false
val newPath = path.toCharArray()
var i = 0
while (i < length) {
val currentChar = newPath(i)
if ((separatorChar == '\\' && currentChar == '\\') || currentChar == '/') {
// UNC Name requires 2 leading slashes
if ((foundSlash && i == uncIndex) || !foundSlash) {
newPath(newLength) = separatorChar
newLength += 1
foundSlash = true
}
} else {
// check for leading slashes before a drive
if (currentChar == ':'
&& uncIndex > 0
&& (newLength == 2 || (newLength == 3 && newPath(
1
) == separatorChar))
&& newPath(0) == separatorChar) {
newPath(0) = newPath(newLength - 1)
newLength = 1
// allow trailing slash after drive letter
uncIndex = 2
}
newPath(newLength) = currentChar
newLength += 1
foundSlash = false
}
i += 1
}
if (foundSlash && (newLength > (uncIndex + 1) || (newLength == 2 && newPath(
0
) != separatorChar))) {
newLength -= 1
}
new String(newPath, 0, newLength)
}
/** Returns a string representing the proper path of this file. If this file
* path is absolute, the user.dir property is not prepended, otherwise it is.
*/
// Ported from Apache Harmony
private def properPath(path: String): String = {
if (isAbsolute(path)) path
else
getUserDir() match {
case null =>
throw new IOException(
"Failed to resolve proper path, no current directory defined"
)
case userdir =>
if (userdir.endsWith(separator)) userdir + path
else userdir + separator + path
}
}
def isAbsolute(path: String): Boolean =
if (separatorChar == '\\') { // Windows. Must start with `\\` or `X:(\|/)`
(path.length > 1 && path.startsWith(separator + separator)) ||
(path.length > 2 && path(0).isLetter && path(1) == ':' && (path(
2
) == '/' || path(2) == '\\'))
} else {
path.length > 0 && path.startsWith(separator)
}
/** Resolve a symbolic link. While the path resolves to an existing path, keep
* resolving. If an absolute link is found, resolve the parent directories if
* resolveAbsolute is true.
*/
// Ported from Apache Harmony
private def resolveLink(
path: CString,
resolveAbsolute: Boolean,
restart: Boolean = false
)(implicit z: Zone): CString = {
val resolved =
readLink(path) match {
// path is not a symlink
case null =>
path
// found an absolute path. continue from there.
case link if link(0) == separatorChar =>
if (isWindows)
if (strncmp(link, c"\\\\?\\", 4.toUInt) == 0)
path
else resolveLink(link, resolveAbsolute, restart = resolveAbsolute)
else
resolveLink(link, resolveAbsolute, restart = resolveAbsolute)
// found a relative path. append to the current path, and continue.
case link =>
val linkLength = strlen(link)
val pathLength = strlen(path)
val `1US` = 1.toUSize
var last = pathLength - `1US`
while (path(last) != separatorChar) last -= `1US`
last += `1US`
// previous path up to last /, plus result of resolving the link.
val newPathLength = last + linkLength + `1US`
val newPath: Ptr[Byte] = alloc[Byte](newPathLength)
strncpy(newPath, path, last)
strncat(newPath, link, linkLength)
resolveLink(newPath, resolveAbsolute, restart)
}
if (restart) resolve(resolved)
else resolved
}
@tailrec private def resolve(path: CString, start: UInt = 0.toUInt)(implicit
z: Zone
): CString = {
val partSize =
if (isWindows) windows.FileApiExt.MAX_PATH
else limits.PATH_MAX.toUInt
val part: CString = alloc[Byte](partSize)
val `1U` = 1.toUInt
// Find the next separator
var i = start
while (i < strlen(path) && path(i) != separatorChar) i += `1U`
if (i == strlen(path)) resolveLink(path, resolveAbsolute = true)
else {
// copy path from start to next separator.
// and resolve that subpart.
strncpy(part, path, i + `1U`)
val resolved = resolveLink(part, resolveAbsolute = true)
// overlap can lead to undefined behaviour
if (resolved != part) strcpy(part, resolved)
strcat(part, path + i + `1U`)
if (strncmp(resolved, path, i + `1U`) == 0) {
// Nothing changed. Continue from the next segment.
resolve(part, i + `1U`)
} else {
// The path has changed. Start over.
resolve(part, 0.toUInt)
}
}
}
/** If `link` is a symlink, follows it and returns the path pointed to.
* Otherwise, returns `None`.
*/
private def readLink(link: CString)(implicit z: Zone): CString = {
val bufferSize: CSize =
if (isWindows) FileApiExt.MAX_PATH
else limits.PATH_MAX - `1U`
val buffer: CString = alloc[Byte](bufferSize)
if (isWindows) {
withFileOpen(
fromCString(link),
access = FILE_GENERIC_READ,
attributes = FILE_FLAG_BACKUP_SEMANTICS | FILE_ATTRIBUTE_DIRECTORY,
allowInvalidHandle = true
) { fileHandle =>
val finalPathFlags = FileApiExt.FILE_NAME_NORMALIZED
def pathLength = GetFinalPathNameByHandleA(
fileHandle,
buffer = buffer,
bufferSize = bufferSize.toUInt,
flags = finalPathFlags
)
if (fileHandle == HandleApiExt.INVALID_HANDLE_VALUE || pathLength == 0)
null
else buffer
}
} else {
readlink(link, buffer, bufferSize) match {
case -1 =>
null
case read =>
// readlink doesn't null-terminate the result.
buffer(read) = 0.toByte
buffer
}
}
}
val pathSeparatorChar: Char = if (isWindows) ';' else ':'
val pathSeparator: String = pathSeparatorChar.toString
val separatorChar: Char = if (isWindows) '\\' else '/'
val separator: String = separatorChar.toString
private val caseSensitive: Boolean = !isWindows
def listRoots(): Array[File] = {
val list = new java.util.ArrayList[File]()
FileSystems
.getDefault()
.getRootDirectories()
.scalaOps
.foreach(p => list.add(p.toFile()))
list.toArray(new Array[File](0))
}
@throws(classOf[IOException])
def createTempFile(prefix: String, suffix: String): File =
createTempFile(prefix, suffix, null)
@throws(classOf[IOException])
def createTempFile(prefix: String, suffix: String, directory: File): File =
FileHelpers.createTempFile(
prefix,
suffix,
directory,
minLength = true,
throwOnError = true
)
// Ported from Apache Harmony
private def checkURI(uri: URI): URI = {
def throwExc(msg: String): Unit =
throw new IllegalArgumentException(s"$msg: $uri")
def compMsg(comp: String): String =
s"Found $comp component in URI"
if (!uri.isAbsolute()) {
throwExc("URI is not absolute")
} else if (!uri.getRawSchemeSpecificPart().startsWith("/")) {
throwExc("URI is not hierarchical")
} else if (uri.getScheme() == null || !(uri.getScheme() == "file")) {
throwExc("Expected file scheme in URI")
} else if (uri.getRawPath() == null || uri.getRawPath().length() == 0) {
throwExc("Expected non-empty path in URI")
} else if (uri.getRawAuthority() != null) {
throwExc(compMsg("authority"))
} else if (uri.getRawQuery() != null) {
throwExc(compMsg("query"))
} else if (uri.getRawFragment() != null) {
throwExc(compMsg("fragment"))
}
uri
// else URI is ok