summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLeah (ctucx) <leah@ctu.cx>2021-03-18 01:36:46 +0100
committerLeah (ctucx) <leah@ctu.cx>2021-03-18 01:36:46 +0100
commite17f48f3c8eb517d74f8fe2b360d4e8646d16454 (patch)
treefbb6e5d149475eea12d92feed65798eb08303fed
parentbb2463fe407428ae0611b36ba9f6304ae827d1dc (diff)
downloadnimgit-e17f48f3c8eb517d74f8fe2b360d4e8646d16454.tar.gz
nimgit-e17f48f3c8eb517d74f8fe2b360d4e8646d16454.tar.bz2
nimgit-e17f48f3c8eb517d74f8fe2b360d4e8646d16454.zip
utils.nim: add proc filemodeStr
-rw-r--r--nimgit/utils.nim39
1 files changed, 38 insertions, 1 deletions
diff --git a/nimgit/utils.nim b/nimgit/utils.nim
index f1fa525..32e55fc 100644
--- a/nimgit/utils.nim
+++ b/nimgit/utils.nim
@@ -1,4 +1,4 @@
-import times
+import times, posix, strutils
import nimgit2
import types
@@ -16,3 +16,40 @@ proc parseGitSignature* (signature: ptr git_signature): GitSignature =
result.name = $signature.name
result.email = $signature.email
result.when = parseGitTime(signature.when)
+
+proc filemodeStr* (m: int): string =
+ var mode = align("", sizeof(m), '-')
+
+ if S_ISREG(cast[Mode](m)):
+ mode[0] = '-'
+ elif S_ISBLK(cast[Mode](m)):
+ mode[0] = 'b';
+ elif S_ISCHR(cast[Mode](m)):
+ mode[0] = 'c';
+ elif S_ISDIR(cast[Mode](m)):
+ mode[0] = 'd';
+ elif S_ISFIFO(cast[Mode](m)):
+ mode[0] = 'p';
+ elif S_ISLNK(cast[Mode](m)):
+ mode[0] = 'l';
+ elif S_ISSOCK(cast[Mode](m)):
+ mode[0] = 's';
+ else:
+ mode[0] = '?';
+
+
+ if cast[bool](m and S_IRUSR): mode[1] = 'r'
+ if cast[bool](m and S_IWUSR): mode[2] = 'w'
+ if cast[bool](m and S_IXUSR): mode[3] = 'x'
+ if cast[bool](m and S_IRGRP): mode[4] = 'r'
+ if cast[bool](m and S_IWGRP): mode[5] = 'w'
+ if cast[bool](m and S_IXGRP): mode[6] = 'x'
+ if cast[bool](m and S_IROTH): mode[7] = 'r'
+ if cast[bool](m and S_IWOTH): mode[8] = 'w'
+ if cast[bool](m and S_IXOTH): mode[9] = 'x'
+
+ if cast[bool](m and S_ISUID): mode[3] = if(mode[3] == 'x'): 's' else: 'S'
+ if cast[bool](m and S_ISGID): mode[6] = if(mode[6] == 'x'): 's' else: 'S'
+ if cast[bool](m and S_ISVTX): mode[9] = if(mode[9] == 'x'): 't' else: 'T'
+
+ result = mode