2020-10-27 11:04:17 +00:00
|
|
|
import chronicles
|
|
|
|
import stew/io2
|
|
|
|
export io2
|
|
|
|
|
|
|
|
when defined(windows):
|
|
|
|
import stew/[windows/acl]
|
|
|
|
|
|
|
|
proc secureCreatePath*(path: string): IoResult[void] =
|
|
|
|
when defined(windows):
|
|
|
|
let sres = createFoldersUserOnlySecurityDescriptor()
|
|
|
|
if sres.isErr():
|
|
|
|
error "Could not allocate security descriptor", path = path,
|
|
|
|
errorMsg = ioErrorMsg(sres.error), errorCode = $sres.error
|
|
|
|
err(sres.error)
|
|
|
|
else:
|
|
|
|
var sd = sres.get()
|
2020-10-30 00:36:47 +00:00
|
|
|
createPath(path, 0o700, secDescriptor = sd.getDescriptor())
|
2020-10-27 11:04:17 +00:00
|
|
|
else:
|
2020-10-30 00:36:47 +00:00
|
|
|
createPath(path, 0o700)
|
2020-10-27 11:04:17 +00:00
|
|
|
|
|
|
|
proc secureWriteFile*[T: byte|char](path: string,
|
|
|
|
data: openArray[T]): IoResult[void] =
|
|
|
|
when defined(windows):
|
|
|
|
let sres = createFilesUserOnlySecurityDescriptor()
|
|
|
|
if sres.isErr():
|
|
|
|
error "Could not allocate security descriptor", path = path,
|
|
|
|
errorMsg = ioErrorMsg(sres.error), errorCode = $sres.error
|
|
|
|
err(sres.error)
|
|
|
|
else:
|
|
|
|
var sd = sres.get()
|
|
|
|
writeFile(path, data, 0o600, secDescriptor = sd.getDescriptor())
|
|
|
|
else:
|
|
|
|
writeFile(path, data, 0o600)
|