-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
os.nim
1032 lines (920 loc) · 36.1 KB
/
os.nim
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
#
#
# Nim's Runtime Library
# (c) Copyright 2015 Andreas Rumpf
#
# See the file "copying.txt", included in this
# distribution, for details about the copyright.
#
## This module contains basic operating system facilities like
## retrieving environment variables, working with directories,
## running shell commands, etc.
## .. importdoc:: symlinks.nim, appdirs.nim, dirs.nim, ospaths2.nim
runnableExamples:
let myFile = "/path/to/my/file.nim"
assert splitPath(myFile) == (head: "/path/to/my", tail: "file.nim")
when defined(posix):
assert parentDir(myFile) == "/path/to/my"
assert splitFile(myFile) == (dir: "/path/to/my", name: "file", ext: ".nim")
assert myFile.changeFileExt("c") == "/path/to/my/file.c"
## **See also:**
## * `paths <paths.html>`_ and `files <files.html>`_ modules for high-level file manipulation
## * `osproc module <osproc.html>`_ for process communication beyond
## `execShellCmd proc`_
## * `uri module <uri.html>`_
## * `distros module <distros.html>`_
## * `dynlib module <dynlib.html>`_
## * `streams module <streams.html>`_
import std/private/ospaths2
export ospaths2
import std/private/osfiles
export osfiles
import std/private/osdirs
export osdirs
import std/private/ossymlinks
export ossymlinks
import std/private/osappdirs
export osappdirs
import std/private/oscommon
include system/inclrtl
import std/private/since
import std/cmdline
export cmdline
import std/[strutils, pathnorm]
when defined(nimPreviewSlimSystem):
import std/[syncio, assertions, widestrs]
const weirdTarget = defined(nimscript) or defined(js)
since (1, 1):
const
invalidFilenameChars* = {'/', '\\', ':', '*', '?', '"', '<', '>', '|', '^', '\0'} ## \
## Characters that may produce invalid filenames across Linux, Windows and Mac.
## You can check if your filename contains any of these chars and strip them for safety.
## Mac bans ``':'``, Linux bans ``'/'``, Windows bans all others.
invalidFilenames* = [
"CON", "PRN", "AUX", "NUL",
"COM0", "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9",
"LPT0", "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9"] ## \
## Filenames that may be invalid across Linux, Windows, Mac, etc.
## You can check if your filename match these and rename it for safety
## (Currently all invalid filenames are from Windows only).
when weirdTarget:
discard
elif defined(windows):
import std/[winlean, times]
elif defined(posix):
import std/[posix, times]
proc toTime(ts: Timespec): times.Time {.inline.} =
result = initTime(ts.tv_sec.int64, ts.tv_nsec.int)
else:
{.error: "OS module not ported to your operating system!".}
when weirdTarget:
{.pragma: noWeirdTarget, error: "this proc is not available on the NimScript/js target".}
else:
{.pragma: noWeirdTarget.}
when defined(nimscript):
# for procs already defined in scriptconfig.nim
template noNimJs(body): untyped = discard
elif defined(js):
{.pragma: noNimJs, error: "this proc is not available on the js target".}
else:
{.pragma: noNimJs.}
import std/oserrors
export oserrors
import std/envvars
export envvars
import std/private/osseps
export osseps
proc expandTilde*(path: string): string {.
tags: [ReadEnvEffect, ReadIOEffect].} =
## Expands ``~`` or a path starting with ``~/`` to a full path, replacing
## ``~`` with `getHomeDir()`_ (otherwise returns ``path`` unmodified).
##
## Windows: this is still supported despite the Windows platform not having this
## convention; also, both ``~/`` and ``~\`` are handled.
##
## See also:
## * `getHomeDir proc`_
## * `getConfigDir proc`_
## * `getTempDir proc`_
## * `getCurrentDir proc`_
## * `setCurrentDir proc`_
runnableExamples:
assert expandTilde("~" / "appname.cfg") == getHomeDir() / "appname.cfg"
assert expandTilde("~/foo/bar") == getHomeDir() / "foo/bar"
assert expandTilde("/foo/bar") == "/foo/bar"
if len(path) == 0 or path[0] != '~':
result = path
elif len(path) == 1:
result = getHomeDir()
elif (path[1] in {DirSep, AltSep}):
result = getHomeDir() / path.substr(2)
else:
# TODO: handle `~bob` and `~bob/` which means home of bob
result = path
proc quoteShellWindows*(s: string): string {.noSideEffect, rtl, extern: "nosp$1".} =
## Quote `s`, so it can be safely passed to Windows API.
##
## Based on Python's `subprocess.list2cmdline`.
## See `this link <https://msdn.microsoft.com/en-us/library/17w5ykft.aspx>`_
## for more details.
let needQuote = {' ', '\t'} in s or s.len == 0
result = ""
var backslashBuff = ""
if needQuote:
result.add("\"")
for c in s:
if c == '\\':
backslashBuff.add(c)
elif c == '\"':
for i in 0..<backslashBuff.len*2:
result.add('\\')
backslashBuff.setLen(0)
result.add("\\\"")
else:
if backslashBuff.len != 0:
result.add(backslashBuff)
backslashBuff.setLen(0)
result.add(c)
if backslashBuff.len > 0:
result.add(backslashBuff)
if needQuote:
result.add(backslashBuff)
result.add("\"")
proc quoteShellPosix*(s: string): string {.noSideEffect, rtl, extern: "nosp$1".} =
## Quote ``s``, so it can be safely passed to POSIX shell.
const safeUnixChars = {'%', '+', '-', '.', '/', '_', ':', '=', '@',
'0'..'9', 'A'..'Z', 'a'..'z'}
if s.len == 0:
result = "''"
elif s.allCharsInSet(safeUnixChars):
result = s
else:
result = "'" & s.replace("'", "'\"'\"'") & "'"
when defined(windows) or defined(posix) or defined(nintendoswitch):
proc quoteShell*(s: string): string {.noSideEffect, rtl, extern: "nosp$1".} =
## Quote ``s``, so it can be safely passed to shell.
##
## When on Windows, it calls `quoteShellWindows proc`_.
## Otherwise, calls `quoteShellPosix proc`_.
when defined(windows):
result = quoteShellWindows(s)
else:
result = quoteShellPosix(s)
proc quoteShellCommand*(args: openArray[string]): string =
## Concatenates and quotes shell arguments `args`.
runnableExamples:
when defined(posix):
assert quoteShellCommand(["aaa", "", "c d"]) == "aaa '' 'c d'"
when defined(windows):
assert quoteShellCommand(["aaa", "", "c d"]) == "aaa \"\" \"c d\""
# can't use `map` pending https://github.com/nim-lang/Nim/issues/8303
result = ""
for i in 0..<args.len:
if i > 0: result.add " "
result.add quoteShell(args[i])
when not weirdTarget:
proc c_system(cmd: cstring): cint {.
importc: "system", header: "<stdlib.h>".}
when not defined(windows):
proc c_free(p: pointer) {.
importc: "free", header: "<stdlib.h>".}
const
ExeExts* = ## Platform specific file extension for executables.
## On Windows ``["exe", "cmd", "bat"]``, on Posix ``[""]``.
when defined(windows): ["exe", "cmd", "bat"] else: [""]
proc findExe*(exe: string, followSymlinks: bool = true;
extensions: openArray[string]=ExeExts): string {.
tags: [ReadDirEffect, ReadEnvEffect, ReadIOEffect], noNimJs.} =
## Searches for `exe` in the current working directory and then
## in directories listed in the ``PATH`` environment variable.
##
## Returns `""` if the `exe` cannot be found. `exe`
## is added the `ExeExts`_ file extensions if it has none.
##
## If the system supports symlinks it also resolves them until it
## meets the actual file. This behavior can be disabled if desired
## by setting `followSymlinks = false`.
if exe.len == 0: return
template checkCurrentDir() =
for ext in extensions:
result = addFileExt(exe, ext)
if fileExists(result): return
when defined(posix):
if '/' in exe: checkCurrentDir()
else:
checkCurrentDir()
let path = getEnv("PATH")
for candidate in split(path, PathSep):
if candidate.len == 0: continue
when defined(windows):
var x = (if candidate[0] == '"' and candidate[^1] == '"':
substr(candidate, 1, candidate.len-2) else: candidate) /
exe
else:
var x = expandTilde(candidate) / exe
for ext in extensions:
var x = addFileExt(x, ext)
if fileExists(x):
when not (defined(windows) or defined(nintendoswitch)):
while followSymlinks: # doubles as if here
if x.symlinkExists:
var r = newString(maxSymlinkLen)
var len = readlink(x.cstring, r.cstring, maxSymlinkLen)
if len < 0:
raiseOSError(osLastError(), exe)
if len > maxSymlinkLen:
r = newString(len+1)
len = readlink(x.cstring, r.cstring, len)
setLen(r, len)
if isAbsolute(r):
x = r
else:
x = parentDir(x) / r
else:
break
return x
result = ""
when weirdTarget:
const times = "fake const"
template Time(x: untyped): untyped = string
proc getLastModificationTime*(file: string): times.Time {.rtl, extern: "nos$1", noWeirdTarget.} =
## Returns the `file`'s last modification time.
##
## See also:
## * `getLastAccessTime proc`_
## * `getCreationTime proc`_
## * `fileNewer proc`_
when defined(posix):
var res: Stat
if stat(file, res) < 0'i32: raiseOSError(osLastError(), file)
result = res.st_mtim.toTime
else:
var f: WIN32_FIND_DATA
var h = findFirstFile(file, f)
if h == -1'i32: raiseOSError(osLastError(), file)
result = fromWinTime(rdFileTime(f.ftLastWriteTime))
findClose(h)
proc getLastAccessTime*(file: string): times.Time {.rtl, extern: "nos$1", noWeirdTarget.} =
## Returns the `file`'s last read or write access time.
##
## See also:
## * `getLastModificationTime proc`_
## * `getCreationTime proc`_
## * `fileNewer proc`_
when defined(posix):
var res: Stat
if stat(file, res) < 0'i32: raiseOSError(osLastError(), file)
result = res.st_atim.toTime
else:
var f: WIN32_FIND_DATA
var h = findFirstFile(file, f)
if h == -1'i32: raiseOSError(osLastError(), file)
result = fromWinTime(rdFileTime(f.ftLastAccessTime))
findClose(h)
proc getCreationTime*(file: string): times.Time {.rtl, extern: "nos$1", noWeirdTarget.} =
## Returns the `file`'s creation time.
##
## **Note:** Under POSIX OS's, the returned time may actually be the time at
## which the file's attribute's were last modified. See
## `here <https://github.com/nim-lang/Nim/issues/1058>`_ for details.
##
## See also:
## * `getLastModificationTime proc`_
## * `getLastAccessTime proc`_
## * `fileNewer proc`_
when defined(posix):
var res: Stat
if stat(file, res) < 0'i32: raiseOSError(osLastError(), file)
result = res.st_ctim.toTime
else:
var f: WIN32_FIND_DATA
var h = findFirstFile(file, f)
if h == -1'i32: raiseOSError(osLastError(), file)
result = fromWinTime(rdFileTime(f.ftCreationTime))
findClose(h)
proc fileNewer*(a, b: string): bool {.rtl, extern: "nos$1", noWeirdTarget.} =
## Returns true if the file `a` is newer than file `b`, i.e. if `a`'s
## modification time is later than `b`'s.
##
## See also:
## * `getLastModificationTime proc`_
## * `getLastAccessTime proc`_
## * `getCreationTime proc`_
when defined(posix):
# If we don't have access to nanosecond resolution, use '>='
when not StatHasNanoseconds:
result = getLastModificationTime(a) >= getLastModificationTime(b)
else:
result = getLastModificationTime(a) > getLastModificationTime(b)
else:
result = getLastModificationTime(a) > getLastModificationTime(b)
proc isAdmin*: bool {.noWeirdTarget.} =
## Returns whether the caller's process is a member of the Administrators local
## group (on Windows) or a root (on POSIX), via `geteuid() == 0`.
when defined(windows):
# Rewrite of the example from Microsoft Docs:
# https://docs.microsoft.com/en-us/windows/win32/api/securitybaseapi/nf-securitybaseapi-checktokenmembership#examples
# and corresponding PostgreSQL function:
# https://doxygen.postgresql.org/win32security_8c.html#ae6b61e106fa5d6c5d077a9d14ee80569
var ntAuthority = SID_IDENTIFIER_AUTHORITY(value: SECURITY_NT_AUTHORITY)
var administratorsGroup: PSID
if not isSuccess(allocateAndInitializeSid(addr ntAuthority,
BYTE(2),
SECURITY_BUILTIN_DOMAIN_RID,
DOMAIN_ALIAS_RID_ADMINS,
0, 0, 0, 0, 0, 0,
addr administratorsGroup)):
raiseOSError(osLastError(), "could not get SID for Administrators group")
try:
var b: WINBOOL
if not isSuccess(checkTokenMembership(0, administratorsGroup, addr b)):
raiseOSError(osLastError(), "could not check access token membership")
result = isSuccess(b)
finally:
if freeSid(administratorsGroup) != nil:
raiseOSError(osLastError(), "failed to free SID for Administrators group")
else:
result = geteuid() == 0
proc exitStatusLikeShell*(status: cint): cint =
## Converts exit code from `c_system` into a shell exit code.
when defined(posix) and not weirdTarget:
if WIFSIGNALED(status):
# like the shell!
128 + WTERMSIG(status)
else:
WEXITSTATUS(status)
else:
status
proc execShellCmd*(command: string): int {.rtl, extern: "nos$1",
tags: [ExecIOEffect], noWeirdTarget.} =
## Executes a `shell command`:idx:.
##
## Command has the form 'program args' where args are the command
## line arguments given to program. The proc returns the error code
## of the shell when it has finished (zero if there is no error).
## The proc does not return until the process has finished.
##
## To execute a program without having a shell involved, use `osproc.execProcess proc
## <osproc.html#execProcess,string,string,openArray[string],StringTableRef,set[ProcessOption]>`_.
##
## **Examples:**
## ```Nim
## discard execShellCmd("ls -la")
## ```
result = exitStatusLikeShell(c_system(command))
proc expandFilename*(filename: string): string {.rtl, extern: "nos$1",
tags: [ReadDirEffect], noWeirdTarget.} =
## Returns the full (`absolute`:idx:) path of an existing file `filename`.
##
## Raises `OSError` in case of an error. Follows symlinks.
when defined(windows):
var bufsize = MAX_PATH.int32
var unused: WideCString = nil
var res = newWideCString(bufsize)
while true:
var L = getFullPathNameW(newWideCString(filename), bufsize, res, unused)
if L == 0'i32:
raiseOSError(osLastError(), filename)
elif L > bufsize:
res = newWideCString(L)
bufsize = L
else:
result = res$L
break
# getFullPathName doesn't do case corrections, so we have to use this convoluted
# way of retrieving the true filename
for x in walkFiles(result):
result = x
if not fileExists(result) and not dirExists(result):
# consider using: `raiseOSError(osLastError(), result)`
raise newException(OSError, "file '" & result & "' does not exist")
else:
# according to Posix we don't need to allocate space for result pathname.
# But we need to free return value with free(3).
var r = realpath(filename, nil)
if r.isNil:
raiseOSError(osLastError(), filename)
else:
result = $r
c_free(cast[pointer](r))
proc getCurrentCompilerExe*(): string {.compileTime.} = discard
## Returns the path of the currently running Nim compiler or nimble executable.
##
## Can be used to retrieve the currently executing
## Nim compiler from a Nim or nimscript program, or the nimble binary
## inside a nimble program (likewise with other binaries built from
## compiler API).
proc createHardlink*(src, dest: string) {.noWeirdTarget.} =
## Create a hard link at `dest` which points to the item specified
## by `src`.
##
## .. warning:: Some OS's restrict the creation of hard links to
## root users (administrators).
##
## See also:
## * `createSymlink proc`_
when defined(windows):
var wSrc = newWideCString(src)
var wDst = newWideCString(dest)
if createHardLinkW(wDst, wSrc, nil) == 0:
raiseOSError(osLastError(), $(src, dest))
else:
if link(src, dest) != 0:
raiseOSError(osLastError(), $(src, dest))
proc inclFilePermissions*(filename: string,
permissions: set[FilePermission]) {.
rtl, extern: "nos$1", tags: [ReadDirEffect, WriteDirEffect], noWeirdTarget.} =
## A convenience proc for:
## ```nim
## setFilePermissions(filename, getFilePermissions(filename)+permissions)
## ```
setFilePermissions(filename, getFilePermissions(filename)+permissions)
proc exclFilePermissions*(filename: string,
permissions: set[FilePermission]) {.
rtl, extern: "nos$1", tags: [ReadDirEffect, WriteDirEffect], noWeirdTarget.} =
## A convenience proc for:
## ```nim
## setFilePermissions(filename, getFilePermissions(filename)-permissions)
## ```
setFilePermissions(filename, getFilePermissions(filename)-permissions)
when not weirdTarget and (defined(freebsd) or defined(dragonfly) or defined(netbsd)):
proc sysctl(name: ptr cint, namelen: cuint, oldp: pointer, oldplen: ptr csize_t,
newp: pointer, newplen: csize_t): cint
{.importc: "sysctl",header: """#include <sys/types.h>
#include <sys/sysctl.h>""".}
const
CTL_KERN = 1
KERN_PROC = 14
MAX_PATH = 1024
when defined(freebsd):
const KERN_PROC_PATHNAME = 12
elif defined(netbsd):
const KERN_PROC_ARGS = 48
const KERN_PROC_PATHNAME = 5
else:
const KERN_PROC_PATHNAME = 9
proc getApplFreebsd(): string =
var pathLength = csize_t(0)
when defined(netbsd):
var req = [CTL_KERN.cint, KERN_PROC_ARGS.cint, -1.cint, KERN_PROC_PATHNAME.cint]
else:
var req = [CTL_KERN.cint, KERN_PROC.cint, KERN_PROC_PATHNAME.cint, -1.cint]
# first call to get the required length
var res = sysctl(addr req[0], 4, nil, addr pathLength, nil, 0)
if res < 0:
return ""
result.setLen(pathLength)
res = sysctl(addr req[0], 4, addr result[0], addr pathLength, nil, 0)
if res < 0:
return ""
let realLen = len(cstring(result))
setLen(result, realLen)
when not weirdTarget and (defined(linux) or defined(solaris) or defined(bsd) or defined(aix)):
proc getApplAux(procPath: string): string =
result = newString(maxSymlinkLen)
var len = readlink(procPath, result.cstring, maxSymlinkLen)
if len > maxSymlinkLen:
result = newString(len+1)
len = readlink(procPath, result.cstring, len)
setLen(result, len)
when not weirdTarget and defined(openbsd):
proc getApplOpenBsd(): string =
# similar to getApplHeuristic, but checks current working directory
when declared(paramStr):
result = ""
# POSIX guaranties that this contains the executable
# as it has been executed by the calling process
let exePath = paramStr(0)
if len(exePath) == 0:
return ""
if exePath[0] == DirSep:
# path is absolute
result = exePath
else:
# not an absolute path, check if it's relative to the current working directory
for i in 1..<len(exePath):
if exePath[i] == DirSep:
result = joinPath(getCurrentDir(), exePath)
break
if len(result) > 0:
return expandFilename(result)
# search in path
for p in split(getEnv("PATH"), {PathSep}):
var x = joinPath(p, exePath)
if fileExists(x):
return expandFilename(x)
else:
result = ""
when not (defined(windows) or defined(macosx) or weirdTarget):
proc getApplHeuristic(): string =
when declared(paramStr):
result = paramStr(0)
# POSIX guaranties that this contains the executable
# as it has been executed by the calling process
if len(result) > 0 and result[0] != DirSep: # not an absolute path?
# iterate over any path in the $PATH environment variable
for p in split(getEnv("PATH"), {PathSep}):
var x = joinPath(p, result)
if fileExists(x): return x
else:
result = ""
when defined(macosx):
type
cuint32* {.importc: "unsigned int", nodecl.} = int
## This is the same as the type ``uint32_t`` in *C*.
# a really hacky solution: since we like to include 2 headers we have to
# define two procs which in reality are the same
proc getExecPath1(c: cstring, size: var cuint32) {.
importc: "_NSGetExecutablePath", header: "<sys/param.h>".}
proc getExecPath2(c: cstring, size: var cuint32): bool {.
importc: "_NSGetExecutablePath", header: "<mach-o/dyld.h>".}
when defined(haiku):
const
PATH_MAX = 1024
B_FIND_PATH_IMAGE_PATH = 1000
proc find_path(codePointer: pointer, baseDirectory: cint, subPath: cstring,
pathBuffer: cstring, bufferSize: csize_t): int32
{.importc, header: "<FindDirectory.h>".}
proc getApplHaiku(): string =
result = newString(PATH_MAX)
if find_path(nil, B_FIND_PATH_IMAGE_PATH, nil, result, PATH_MAX) == 0:
let realLen = len(cstring(result))
setLen(result, realLen)
else:
result = ""
proc getAppFilename*(): string {.rtl, extern: "nos$1", tags: [ReadIOEffect], noWeirdTarget, raises: [].} =
## Returns the filename of the application's executable.
## This proc will resolve symlinks.
##
## Returns empty string when name is unavailable
##
## See also:
## * `getAppDir proc`_
## * `getCurrentCompilerExe proc`_
# Linux: /proc/<pid>/exe
# Solaris:
# /proc/<pid>/object/a.out (filename only)
# /proc/<pid>/path/a.out (complete pathname)
when defined(windows):
var bufsize = int32(MAX_PATH)
var buf = newWideCString(bufsize)
while true:
var L = getModuleFileNameW(0, buf, bufsize)
if L == 0'i32:
result = "" # error!
break
elif L > bufsize:
buf = newWideCString(L)
bufsize = L
else:
result = buf$L
break
elif defined(macosx):
var size = cuint32(0)
getExecPath1(nil, size)
result = newString(int(size))
if getExecPath2(result.cstring, size):
result = "" # error!
if result.len > 0:
try:
result = result.expandFilename
except OSError:
result = ""
else:
when defined(linux) or defined(aix):
result = getApplAux("/proc/self/exe")
elif defined(solaris):
result = getApplAux("/proc/" & $getpid() & "/path/a.out")
elif defined(genode):
result = "" # Not supported
elif defined(freebsd) or defined(dragonfly) or defined(netbsd):
result = getApplFreebsd()
elif defined(haiku):
result = getApplHaiku()
elif defined(openbsd):
result = try: getApplOpenBsd() except OSError: ""
elif defined(nintendoswitch):
result = ""
# little heuristic that may work on other POSIX-like systems:
if result.len == 0:
result = try: getApplHeuristic() except OSError: ""
proc getAppDir*(): string {.rtl, extern: "nos$1", tags: [ReadIOEffect], noWeirdTarget.} =
## Returns the directory of the application's executable.
##
## See also:
## * `getAppFilename proc`_
result = splitFile(getAppFilename()).dir
proc sleep*(milsecs: int) {.rtl, extern: "nos$1", tags: [TimeEffect], noWeirdTarget.} =
## Sleeps `milsecs` milliseconds.
## A negative `milsecs` causes sleep to return immediately.
when defined(windows):
if milsecs < 0:
return # fixes #23732
winlean.sleep(int32(milsecs))
else:
var a, b: Timespec
a.tv_sec = posix.Time(milsecs div 1000)
a.tv_nsec = (milsecs mod 1000) * 1000 * 1000
discard posix.nanosleep(a, b)
proc getFileSize*(file: string): BiggestInt {.rtl, extern: "nos$1",
tags: [ReadIOEffect], noWeirdTarget.} =
## Returns the file size of `file` (in bytes). ``OSError`` is
## raised in case of an error.
when defined(windows):
var a: WIN32_FIND_DATA
var resA = findFirstFile(file, a)
if resA == -1: raiseOSError(osLastError(), file)
result = rdFileSize(a)
findClose(resA)
else:
var rawInfo: Stat
if stat(file, rawInfo) < 0'i32:
raiseOSError(osLastError(), file)
rawInfo.st_size
when defined(windows) or weirdTarget:
type
DeviceId* = int32
FileId* = int64
else:
type
DeviceId* = Dev
FileId* = Ino
type
FileInfo* = object
## Contains information associated with a file object.
##
## See also:
## * `getFileInfo(handle) proc`_
## * `getFileInfo(file) proc`_
## * `getFileInfo(path, followSymlink) proc`_
id*: tuple[device: DeviceId, file: FileId] ## Device and file id.
kind*: PathComponent ## Kind of file object - directory, symlink, etc.
size*: BiggestInt ## Size of file.
permissions*: set[FilePermission] ## File permissions
linkCount*: BiggestInt ## Number of hard links the file object has.
lastAccessTime*: times.Time ## Time file was last accessed.
lastWriteTime*: times.Time ## Time file was last modified/written to.
creationTime*: times.Time ## Time file was created. Not supported on all systems!
blockSize*: int ## Preferred I/O block size for this object.
## In some filesystems, this may vary from file to file.
isSpecial*: bool ## Is file special? (on Unix some "files"
## can be special=non-regular like FIFOs,
## devices); for directories `isSpecial`
## is always `false`, for symlinks it is
## the same as for the link's target.
template rawToFormalFileInfo(rawInfo, path, formalInfo): untyped =
## Transforms the native file info structure into the one nim uses.
## 'rawInfo' is either a 'BY_HANDLE_FILE_INFORMATION' structure on Windows,
## or a 'Stat' structure on posix
when defined(windows):
template merge[T](a, b): untyped =
cast[T](
(uint64(cast[uint32](a))) or
(uint64(cast[uint32](b)) shl 32)
)
formalInfo.id.device = rawInfo.dwVolumeSerialNumber
formalInfo.id.file = merge[FileId](rawInfo.nFileIndexLow, rawInfo.nFileIndexHigh)
formalInfo.size = merge[BiggestInt](rawInfo.nFileSizeLow, rawInfo.nFileSizeHigh)
formalInfo.linkCount = rawInfo.nNumberOfLinks
formalInfo.lastAccessTime = fromWinTime(rdFileTime(rawInfo.ftLastAccessTime))
formalInfo.lastWriteTime = fromWinTime(rdFileTime(rawInfo.ftLastWriteTime))
formalInfo.creationTime = fromWinTime(rdFileTime(rawInfo.ftCreationTime))
formalInfo.blockSize = 8192 # xxx use Windows API instead of hardcoding
# Retrieve basic permissions
if (rawInfo.dwFileAttributes and FILE_ATTRIBUTE_READONLY) != 0'i32:
formalInfo.permissions = {fpUserExec, fpUserRead, fpGroupExec,
fpGroupRead, fpOthersExec, fpOthersRead}
else:
formalInfo.permissions = {fpUserExec..fpOthersRead}
# Retrieve basic file kind
if (rawInfo.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY) != 0'i32:
formalInfo.kind = pcDir
else:
formalInfo.kind = pcFile
if (rawInfo.dwFileAttributes and FILE_ATTRIBUTE_REPARSE_POINT) != 0'i32:
formalInfo.kind = succ(formalInfo.kind)
else:
template checkAndIncludeMode(rawMode, formalMode: untyped) =
if (rawInfo.st_mode and rawMode.Mode) != 0.Mode:
formalInfo.permissions.incl(formalMode)
formalInfo.id = (rawInfo.st_dev, rawInfo.st_ino)
formalInfo.size = rawInfo.st_size
formalInfo.linkCount = rawInfo.st_nlink.BiggestInt
formalInfo.lastAccessTime = rawInfo.st_atim.toTime
formalInfo.lastWriteTime = rawInfo.st_mtim.toTime
formalInfo.creationTime = rawInfo.st_ctim.toTime
formalInfo.blockSize = rawInfo.st_blksize
formalInfo.permissions = {}
checkAndIncludeMode(S_IRUSR, fpUserRead)
checkAndIncludeMode(S_IWUSR, fpUserWrite)
checkAndIncludeMode(S_IXUSR, fpUserExec)
checkAndIncludeMode(S_IRGRP, fpGroupRead)
checkAndIncludeMode(S_IWGRP, fpGroupWrite)
checkAndIncludeMode(S_IXGRP, fpGroupExec)
checkAndIncludeMode(S_IROTH, fpOthersRead)
checkAndIncludeMode(S_IWOTH, fpOthersWrite)
checkAndIncludeMode(S_IXOTH, fpOthersExec)
(formalInfo.kind, formalInfo.isSpecial) =
if S_ISDIR(rawInfo.st_mode):
(pcDir, false)
elif S_ISLNK(rawInfo.st_mode):
assert(path != "") # symlinks can't occur for file handles
getSymlinkFileKind(path)
else:
(pcFile, not S_ISREG(rawInfo.st_mode))
when defined(js):
when not declared(FileHandle):
type FileHandle = distinct int32
when not declared(File):
type File = object
proc getFileInfo*(handle: FileHandle): FileInfo {.noWeirdTarget.} =
## Retrieves file information for the file object represented by the given
## handle.
##
## If the information cannot be retrieved, such as when the file handle
## is invalid, `OSError` is raised.
##
## See also:
## * `getFileInfo(file) proc`_
## * `getFileInfo(path, followSymlink) proc`_
# Done: ID, Kind, Size, Permissions, Link Count
when defined(windows):
var rawInfo: BY_HANDLE_FILE_INFORMATION
# We have to use the super special '_get_osfhandle' call (wrapped above)
# To transform the C file descriptor to a native file handle.
var realHandle = get_osfhandle(handle)
if getFileInformationByHandle(realHandle, addr rawInfo) == 0:
raiseOSError(osLastError(), $handle)
rawToFormalFileInfo(rawInfo, "", result)
else:
var rawInfo: Stat
if fstat(handle, rawInfo) < 0'i32:
raiseOSError(osLastError(), $handle)
rawToFormalFileInfo(rawInfo, "", result)
proc getFileInfo*(file: File): FileInfo {.noWeirdTarget.} =
## Retrieves file information for the file object.
##
## See also:
## * `getFileInfo(handle) proc`_
## * `getFileInfo(path, followSymlink) proc`_
if file.isNil:
raise newException(IOError, "File is nil")
result = getFileInfo(file.getFileHandle())
proc getFileInfo*(path: string, followSymlink = true): FileInfo {.noWeirdTarget.} =
## Retrieves file information for the file object pointed to by `path`.
##
## Due to intrinsic differences between operating systems, the information
## contained by the returned `FileInfo object`_ will be slightly
## different across platforms, and in some cases, incomplete or inaccurate.
##
## When `followSymlink` is true (default), symlinks are followed and the
## information retrieved is information related to the symlink's target.
## Otherwise, information on the symlink itself is retrieved (however,
## field `isSpecial` is still determined from the target on Unix).
##
## If the information cannot be retrieved, such as when the path doesn't
## exist, or when permission restrictions prevent the program from retrieving
## file information, `OSError` is raised.
##
## See also:
## * `getFileInfo(handle) proc`_
## * `getFileInfo(file) proc`_
when defined(windows):
var
handle = openHandle(path, followSymlink)
rawInfo: BY_HANDLE_FILE_INFORMATION
if handle == INVALID_HANDLE_VALUE:
raiseOSError(osLastError(), path)
if getFileInformationByHandle(handle, addr rawInfo) == 0:
raiseOSError(osLastError(), path)
rawToFormalFileInfo(rawInfo, path, result)
discard closeHandle(handle)
else:
var rawInfo: Stat
if followSymlink:
if stat(path, rawInfo) < 0'i32:
raiseOSError(osLastError(), path)
else:
if lstat(path, rawInfo) < 0'i32:
raiseOSError(osLastError(), path)
rawToFormalFileInfo(rawInfo, path, result)
proc sameFileContent*(path1, path2: string): bool {.rtl, extern: "nos$1",
tags: [ReadIOEffect], noWeirdTarget.} =
## Returns true if both pathname arguments refer to files with identical
## binary content.
##
## See also:
## * `sameFile proc`_
var
a, b: File
if not open(a, path1): return false
if not open(b, path2):
close(a)
return false
let bufSize = getFileInfo(a).blockSize
var bufA = alloc(bufSize)
var bufB = alloc(bufSize)
while true:
var readA = readBuffer(a, bufA, bufSize)
var readB = readBuffer(b, bufB, bufSize)
if readA != readB:
result = false
break
if readA == 0:
result = true
break
result = equalMem(bufA, bufB, readA)
if not result: break
if readA != bufSize: break # end of file
dealloc(bufA)
dealloc(bufB)
close(a)
close(b)
proc isHidden*(path: string): bool {.noWeirdTarget.} =
## Determines whether ``path`` is hidden or not, using `this
## reference <https://en.wikipedia.org/wiki/Hidden_file_and_hidden_directory>`_.
##
## On Windows: returns true if it exists and its "hidden" attribute is set.
##
## On posix: returns true if ``lastPathPart(path)`` starts with ``.`` and is
## not ``.`` or ``..``.
##
## **Note**: paths are not normalized to determine `isHidden`.
runnableExamples:
when defined(posix):
assert ".foo".isHidden
assert not ".foo/bar".isHidden
assert not ".".isHidden
assert not "..".isHidden
assert not "".isHidden
assert ".foo/".isHidden
when defined(windows):
wrapUnary(attributes, getFileAttributesW, path)
if attributes != -1'i32:
result = (attributes and FILE_ATTRIBUTE_HIDDEN) != 0'i32
else:
let fileName = lastPathPart(path)
result = len(fileName) >= 2 and fileName[0] == '.' and fileName != ".."
proc getCurrentProcessId*(): int {.noWeirdTarget.} =
## Return current process ID.
##
## See also:
## * `osproc.processID(p: Process) <osproc.html#processID,Process>`_
when defined(windows):
proc GetCurrentProcessId(): DWORD {.stdcall, dynlib: "kernel32",
importc: "GetCurrentProcessId".}
result = GetCurrentProcessId().int
else:
result = getpid()
proc setLastModificationTime*(file: string, t: times.Time) {.noWeirdTarget.} =
## Sets the `file`'s last modification time. `OSError` is raised in case of
## an error.
when defined(posix):
let unixt = posix.Time(t.toUnix)
let micro = convert(Nanoseconds, Microseconds, t.nanosecond)
var timevals = [Timeval(tv_sec: unixt, tv_usec: micro),
Timeval(tv_sec: unixt, tv_usec: micro)] # [last access, last modification]
if utimes(file, timevals.addr) != 0: raiseOSError(osLastError(), file)
else:
let h = openHandle(path = file, writeAccess = true)
if h == INVALID_HANDLE_VALUE: raiseOSError(osLastError(), file)
var ft = t.toWinTime.toFILETIME
let res = setFileTime(h, nil, nil, ft.addr)
discard h.closeHandle
if res == 0'i32: raiseOSError(osLastError(), file)
func isValidFilename*(filename: string, maxLen = 259.Positive): bool {.since: (1, 1).} =
## Returns `true` if `filename` is valid for crossplatform use.
##
## This is useful if you want to copy or save files across Windows, Linux, Mac, etc.
## It uses `invalidFilenameChars`, `invalidFilenames` and `maxLen` to verify the specified `filename`.
##
## See also: